Files
obitools4/pkg/obioptions/options.go

252 lines
7.7 KiB
Go
Raw Normal View History

2022-01-13 23:27:39 +01:00
package obioptions
import (
"fmt"
"os"
"runtime"
"sync"
2022-01-13 23:27:39 +01:00
2025-01-24 18:09:59 +01:00
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obidefault"
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiformats"
2022-02-24 12:14:52 +01:00
log "github.com/sirupsen/logrus"
2022-01-13 23:27:39 +01:00
"github.com/DavidGamba/go-getoptions"
"net/http"
_ "net/http/pprof"
2022-01-13 23:27:39 +01:00
)
var _Debug = false
var _Pprof = false
var _PprofMudex = 10
var _PprofGoroutine = 6060
var __seq_as_taxa__ = false
2022-01-13 23:27:39 +01:00
var __defaut_taxonomy_mutex__ sync.Mutex
type ArgumentParser func([]string) (*getoptions.GetOpt, []string)
2022-01-13 23:27:39 +01:00
func GenerateOptionParser(program string,
documentation string,
optionset ...func(*getoptions.GetOpt)) ArgumentParser {
2022-01-13 23:27:39 +01:00
options := getoptions.New()
options.Self(program, documentation)
options.SetMode(getoptions.Bundling)
options.SetUnknownMode(getoptions.Fail)
2022-01-13 23:27:39 +01:00
options.Bool("help", false, options.Alias("h", "?"))
options.Bool("version", false,
options.Description("Prints the version and exits."))
options.BoolVar(&_Debug, "debug", false,
options.GetEnv("OBIDEBUG"),
options.Description("Enable debug mode, by setting log level to debug."))
options.BoolVar(&_Pprof, "pprof", false,
options.Description("Enable pprof server. Look at the log for details."))
// options.IntVar(&_ParallelWorkers, "workers", _ParallelWorkers,
// options.Alias("w"),
// options.Description("Number of parallele threads computing the result"))
2022-02-06 18:52:53 +01:00
2025-01-27 17:12:45 +01:00
options.IntVar(obidefault.MaxCPUPtr(), "max-cpu", obidefault.MaxCPU(),
options.GetEnv("OBIMAXCPU"),
options.Description("Number of parallele threads computing the result"))
2022-01-13 23:27:39 +01:00
options.BoolVar(&_Pprof, "force-one-cpu", false,
options.Description("Force to use only one cpu core for parallel processing"))
options.IntVar(&_PprofMudex, "pprof-mutex", _PprofMudex,
options.GetEnv("OBIPPROFMUTEX"),
options.Description("Enable profiling of mutex lock."))
options.IntVar(&_PprofGoroutine, "pprof-goroutine", _PprofGoroutine,
options.GetEnv("OBIPPROFGOROUTINE"),
options.Description("Enable profiling of goroutine blocking profile."))
2025-01-27 17:12:45 +01:00
options.IntVar(obidefault.BatchSizePtr(), "batch-size", obidefault.BatchSize(),
options.GetEnv("OBIBATCHSIZE"),
options.Description("Number of sequence per batch for paralelle processing"))
options.Bool("solexa", false,
options.GetEnv("OBISOLEXA"),
options.Description("Decodes quality string according to the Solexa specification."))
options.BoolVar(obidefault.SilentWarningPtr(), "silent-warning", obidefault.SilentWarning(),
options.GetEnv("OBIWARNING"),
options.Description("Stop printing of the warning message"),
)
2022-01-13 23:27:39 +01:00
for _, o := range optionset {
o(options)
}
return func(args []string) (*getoptions.GetOpt, []string) {
2022-01-13 23:27:39 +01:00
remaining, err := options.Parse(args[1:])
if options.Called("help") {
fmt.Fprint(os.Stderr, options.Help())
os.Exit(0)
}
if options.Called("version") {
fmt.Fprintf(os.Stderr, "OBITools %s\n", VersionString())
os.Exit(0)
}
if options.Called("taxonomy") {
__defaut_taxonomy_mutex__.Lock()
defer __defaut_taxonomy_mutex__.Unlock()
taxonomy, err := obiformats.LoadTaxonomy(
obidefault.SelectedTaxonomy(),
!obidefault.AreAlternativeNamesSelected(),
SeqAsTaxa(),
)
if err != nil {
log.Fatalf("Cannot load default taxonomy: %v", err)
}
taxonomy.SetAsDefault()
}
log.SetLevel(log.InfoLevel)
if options.Called("debug") {
log.SetLevel(log.DebugLevel)
log.Debugln("Switch to debug level logging")
}
if options.Called("pprof") {
url := "localhost:6060"
go http.ListenAndServe(url, nil)
log.Infof("Start a pprof server at address %s/debug/pprof", url)
log.Info("Profil can be followed running concurrently the command :")
log.Info(" go tool pprof -http=127.0.0.1:8080 'http://localhost:6060/debug/pprof/profile?seconds=30'")
}
if options.Called("pprof-mutex") {
url := "localhost:6060"
go http.ListenAndServe(url, nil)
runtime.SetMutexProfileFraction(_PprofMudex)
log.Infof("Start a pprof server at address %s/debug/pprof", url)
log.Info("Profil can be followed running concurrently the command :")
log.Info(" go tool pprof -http=127.0.0.1:8080 'http://localhost:6060/debug/pprof/mutex'")
}
if options.Called("pprof-goroutine") {
url := "localhost:6060"
go http.ListenAndServe(url, nil)
runtime.SetBlockProfileRate(_PprofGoroutine)
log.Infof("Start a pprof server at address %s/debug/pprof", url)
log.Info("Profil can be followed running concurrently the command :")
log.Info(" go tool pprof -http=127.0.0.1:8080 'http://localhost:6060/debug/pprof/block'")
}
// Handle user errors
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
fmt.Fprint(os.Stderr, options.Help(getoptions.HelpSynopsis))
os.Exit(1)
}
2022-02-06 18:52:53 +01:00
// Setup the maximum number of CPU usable by the program
2025-01-27 17:12:45 +01:00
if obidefault.MaxCPU() == 1 {
log.Warn("Limitating the Maximum number of CPU to 1 is not recommanded")
log.Warn("The number of CPU requested has been set to 2")
2025-01-27 17:12:45 +01:00
obidefault.SetMaxCPU(2)
}
if options.Called("force-one-cpu") {
log.Warn("Limitating the Maximum number of CPU to 1 is not recommanded")
log.Warn("The number of CPU has been forced to 1")
log.Warn("This can lead to unexpected behavior")
2025-01-27 17:12:45 +01:00
obidefault.SetMaxCPU(1)
}
2025-01-27 17:12:45 +01:00
runtime.GOMAXPROCS(obidefault.MaxCPU())
if options.Called("max-cpu") || options.Called("force-one-cpu") {
2025-01-27 17:12:45 +01:00
log.Printf("CPU number limited to %d", obidefault.MaxCPU())
2022-02-06 18:52:53 +01:00
}
2022-02-24 12:14:52 +01:00
if options.Called("no-singleton") {
log.Printf("No singleton option set")
}
2025-01-27 17:12:45 +01:00
log.Printf("Number of workers set %d", obidefault.ParallelWorkers())
// if options.Called("workers") {
// }
if options.Called("solexa") {
2025-01-24 18:09:59 +01:00
obidefault.SetReadQualitiesShift(64)
}
return options, remaining
2022-01-13 23:27:39 +01:00
}
}
2024-11-16 10:01:07 +01:00
func LoadTaxonomyOptionSet(options *getoptions.GetOpt, required, alternatiive bool) {
if required {
2025-01-27 17:12:45 +01:00
options.StringVar(obidefault.SelectedTaxonomyPtr(), "taxonomy", obidefault.SelectedTaxonomy(),
2024-11-16 10:01:07 +01:00
options.Alias("t"),
options.Required(),
options.Description("Path to the taxonomy database."))
2024-11-16 10:01:07 +01:00
} else {
2025-01-27 17:12:45 +01:00
options.StringVar(obidefault.SelectedTaxonomyPtr(), "taxonomy", obidefault.SelectedTaxonomy(),
2024-11-16 10:01:07 +01:00
options.Alias("t"),
options.Description("Path to the taxonomy database."))
2024-11-16 10:01:07 +01:00
}
if alternatiive {
2025-01-27 17:12:45 +01:00
options.BoolVar(obidefault.AlternativeNamesSelectedPtr(), "alternative-names", obidefault.AreAlternativeNamesSelected(),
2024-11-16 10:01:07 +01:00
options.Alias("a"),
options.Description("Enable the search on all alternative names and not only scientific names."))
}
options.BoolVar(obidefault.FailOnTaxonomyPtr(), "fail-on-taxonomy",
obidefault.FailOnTaxonomy(),
options.Description("Make obitools failing on error if a used taxid is not a currently valid one"),
)
options.BoolVar(obidefault.UpdateTaxidPtr(), "update-taxid", obidefault.UpdateTaxid(),
options.Description("Make obitools automatically updating the taxid that are declared merged to a newest one."),
)
2025-03-08 09:40:06 +01:00
options.BoolVar(obidefault.UseRawTaxidsPtr(), "raw-taxid", obidefault.UseRawTaxids(),
options.Description("When set, taxids are printed in files with any supplementary information (taxon name and rank)"),
)
options.BoolVar(&__seq_as_taxa__, "with-leaves", __seq_as_taxa__,
options.Description("If taxonomy is extracted from a sequence file, sequences are added as leave of their taxid annotation"),
)
2024-11-16 10:01:07 +01:00
}
// CLIIsDebugMode returns whether the CLI is in debug mode.
//
// The debug mode is activated by the command line option --debug or
// the environment variable OBIDEBUG.
// It can be activated programmatically by the SetDebugOn() function.
//
// No parameters.
// Returns a boolean indicating if the CLI is in debug mode.
2022-02-01 17:31:28 +01:00
func CLIIsDebugMode() bool {
return _Debug
}
func SeqAsTaxa() bool {
return __seq_as_taxa__
}
// SetDebugOn sets the debug mode on.
func SetDebugOn() {
_Debug = true
2022-01-13 23:27:39 +01:00
}
// SetDebugOff sets the debug mode off.
func SetDebugOff() {
_Debug = false
2022-01-13 23:27:39 +01:00
}