add the --download-ncbi option to obitaxonomy

This commit is contained in:
Eric Coissac
2025-01-29 12:38:39 +01:00
parent b6b18c0fa1
commit 8a28c9ae7c
5 changed files with 87 additions and 3 deletions

View File

@ -1,13 +1,14 @@
package main
import (
"log"
"os"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obioptions"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obitax"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obitools/obitaxonomy"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
log "github.com/sirupsen/logrus"
)
func main() {
@ -19,6 +20,15 @@ func main() {
switch {
case obitaxonomy.CLIDownloadNCBI():
err := obitaxonomy.CLIDownloadNCBITaxdump()
if err != nil {
log.Errorf("Cannot download NCBI taxonomy: %s", err.Error())
os.Exit(1)
}
os.Exit(0)
case obitaxonomy.CLIDumpSubtaxonomy():
iterator = obitaxonomy.CLISubTaxonomyIterator()

View File

@ -8,7 +8,7 @@ import (
// corresponds to the last commit, and not the one when the file will be
// commited
var _Commit = "67e2758"
var _Commit = "b6b18c0"
var _Version = "Release 4.2.0"
// Version returns the version of the obitools package.

View File

@ -1,10 +1,15 @@
package obitaxonomy
import (
"fmt"
"time"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obidefault"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiitercsv"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obitax"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obitools/obiconvert"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obitools/obicsv"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
log "github.com/sirupsen/logrus"
)
@ -73,3 +78,18 @@ func CLICSVTaxaIterator(iterator *obitax.ITaxon) *obiitercsv.ICSVRecord {
func CLICSVTaxaWriter(iterator *obitax.ITaxon, terminalAction bool) *obiitercsv.ICSVRecord {
return obicsv.CLICSVWriter(CLICSVTaxaIterator(iterator), terminalAction)
}
func CLIDownloadNCBITaxdump() error {
now := time.Now()
dateStr := now.Format("20060102") // In Go, this specific date is used as reference for formatting
filename := fmt.Sprintf("ncbitaxo_%s.tgz", dateStr)
if obiconvert.CLIOutPutFileName() != "-" {
filename = obiconvert.CLIOutPutFileName()
}
log.Infof("Downloading NCBI Taxdump to %s", filename)
return obiutils.DownloadFile("https://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz", filename)
}

View File

@ -22,6 +22,7 @@ var __taxid_path__ = "NA"
var __taxid_sons__ = "NA"
var __restrict_rank__ = ""
var __to_dump__ = ""
var __download_ncbi__ = false
func FilterTaxonomyOptionSet(options *getoptions.GetOpt) {
options.BoolVar(&__rank_list__, "rank-list", false,
@ -34,7 +35,7 @@ func FilterTaxonomyOptionSet(options *getoptions.GetOpt) {
}
func OptionSet(options *getoptions.GetOpt) {
obioptions.LoadTaxonomyOptionSet(options, true, true)
obioptions.LoadTaxonomyOptionSet(options, false, true)
FilterTaxonomyOptionSet(options)
options.BoolVar(&__fixed_pattern__, "fixed", false,
options.Alias("F"),
@ -70,6 +71,10 @@ func OptionSet(options *getoptions.GetOpt) {
options.ArgName("TAXID"),
options.Description("Dump a sub-taxonomy corresponding to the precised clade"),
)
options.BoolVar(&__download_ncbi__, "download-ncbi", __download_ncbi__,
options.Description("Download the current NCBI taxonomy taxdump"),
)
}
func CLITaxonomicalRestrictions() (*obitax.TaxonSet, error) {
@ -144,3 +149,7 @@ func CLIDumpSubtaxonomy() bool {
func CLISubTaxonomyNode() string {
return __to_dump__
}
func CLIDownloadNCBI() bool {
return __download_ncbi__
}

45
pkg/obiutils/download.go Normal file
View File

@ -0,0 +1,45 @@
package obiutils
import (
"fmt"
"io"
"net/http"
"os"
"github.com/schollz/progressbar/v3"
)
func DownloadFile(url string, filepath string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Create progress bar
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading",
)
// Write the body to file while updating the progress bar
_, err = io.Copy(io.MultiWriter(out, bar), resp.Body)
if err != nil {
return err
}
return nil
}