mirror of
https://github.com/metabarcoding/obitools4.git
synced 2026-03-25 05:20:52 +00:00
Add progress bar configuration and conditional display
This commit introduces a new configuration module `obidefault` to manage progress bar settings, allowing users to disable progress bars via a `--no-progressbar` option. It updates various packages to conditionally display progress bars based on this new configuration, improving user experience by providing control over progress bar output. The changes also include improvements to progress bar handling in several packages, ensuring they are only displayed when appropriate (e.g., when stderr is a terminal and stdout is not piped).
This commit is contained in:
@@ -27,6 +27,8 @@ func AhoCorazickWorker(slot string, patterns []string) obiseq.SeqWorker {
|
||||
npar := min(obidefault.ParallelWorkers(), nmatcher)
|
||||
mutex.Add(npar)
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -36,14 +38,16 @@ func AhoCorazickWorker(slot string, patterns []string) obiseq.SeqWorker {
|
||||
progressbar.OptionSetDescription("Building AhoCorasick matcher..."),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(nmatcher, pbopt...)
|
||||
bar.Add(0)
|
||||
bar = progressbar.NewOptions(nmatcher, pbopt...)
|
||||
}
|
||||
|
||||
builder := func() {
|
||||
for i := range ieme {
|
||||
matchers[i] = ahocorasick.CompileStrings(patterns[i*sizebatch:min((i+1)*sizebatch,len(patterns))])
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
}
|
||||
}
|
||||
mutex.Done()
|
||||
}
|
||||
|
||||
|
||||
19
pkg/obidefault/progressbar.go
Normal file
19
pkg/obidefault/progressbar.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package obidefault
|
||||
|
||||
var __no_progress_bar__ = false
|
||||
|
||||
func ProgressBar() bool {
|
||||
return !__no_progress_bar__
|
||||
}
|
||||
|
||||
func NoProgressBar() bool {
|
||||
return __no_progress_bar__
|
||||
}
|
||||
|
||||
func SetNoProgressBar(b bool) {
|
||||
__no_progress_bar__ = b
|
||||
}
|
||||
|
||||
func NoProgressBarPtr() *bool {
|
||||
return &__no_progress_bar__
|
||||
}
|
||||
@@ -5,18 +5,30 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obidefault"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
func (iterator IBioSequence) Speed(message string, size ...int) IBioSequence {
|
||||
|
||||
// If the STDERR is redicted and doesn't end up to a terminal
|
||||
// If the progress bar is disabled via --no-progressbar option
|
||||
if !obidefault.ProgressBar() {
|
||||
return iterator
|
||||
}
|
||||
|
||||
// If the STDERR is redirected and doesn't end up to a terminal
|
||||
// No progress bar is printed.
|
||||
o, _ := os.Stderr.Stat()
|
||||
if (o.Mode() & os.ModeCharDevice) != os.ModeCharDevice {
|
||||
return iterator
|
||||
}
|
||||
|
||||
// If stdout is piped, no progress bar is printed.
|
||||
oo, _ := os.Stdout.Stat()
|
||||
if (oo.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe {
|
||||
return iterator
|
||||
}
|
||||
|
||||
newIter := MakeIBioSequence()
|
||||
|
||||
newIter.Add(1)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"sort"
|
||||
"unsafe"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obidefault"
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obifp"
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obilog"
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiseq"
|
||||
@@ -267,6 +268,8 @@ func NewKmerMap[T obifp.FPUint[T]](
|
||||
}
|
||||
|
||||
n := len(sequences)
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -276,11 +279,12 @@ func NewKmerMap[T obifp.FPUint[T]](
|
||||
progressbar.OptionSetDescription("Indexing kmers"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(n, pbopt...)
|
||||
bar = progressbar.NewOptions(n, pbopt...)
|
||||
}
|
||||
|
||||
for i, sequence := range sequences {
|
||||
kmap.Push(sequence, maxoccurs)
|
||||
if i%100 == 0 {
|
||||
if bar != nil && i%100 == 0 {
|
||||
bar.Add(100)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obialign"
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obidefault"
|
||||
"git.metabarcoding.org/obitools/obitools4/obitools4/pkg/obiutils"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
@@ -69,6 +70,8 @@ func EmpiricalDistCsv(filename string, data [][]Ratio, compressed bool) {
|
||||
}
|
||||
defer destfile.Close()
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -77,8 +80,8 @@ func EmpiricalDistCsv(filename string, data [][]Ratio, compressed bool) {
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
progressbar.OptionSetDescription("[Save CSV stat ratio file]"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(len(data), pbopt...)
|
||||
bar = progressbar.NewOptions(len(data), pbopt...)
|
||||
}
|
||||
|
||||
fmt.Fprintln(destfile, "Sample,Origin_id,Origin_status,Origin,Mutant,Origin_Weight,Mutant_Weight,Origin_Count,Mutant_Count,Position,Origin_length,A,C,G,T")
|
||||
for code, dist := range data {
|
||||
@@ -101,9 +104,11 @@ func EmpiricalDistCsv(filename string, data [][]Ratio, compressed bool) {
|
||||
ratio.T,
|
||||
)
|
||||
}
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// It takes a slice of sequences, a sample name and a statistical threshold and returns a string
|
||||
// containing a GML representation of the graph
|
||||
@@ -181,6 +186,8 @@ func SaveGMLGraphs(dirname string,
|
||||
}
|
||||
}
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -189,8 +196,8 @@ func SaveGMLGraphs(dirname string,
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
progressbar.OptionSetDescription("[Save GML Graph files]"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(len(samples), pbopt...)
|
||||
bar = progressbar.NewOptions(len(samples), pbopt...)
|
||||
}
|
||||
|
||||
for name, seqs := range samples {
|
||||
|
||||
@@ -204,8 +211,10 @@ func SaveGMLGraphs(dirname string,
|
||||
file.WriteString(Gml(seqs, name, statThreshold))
|
||||
file.Close()
|
||||
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -495,6 +504,8 @@ func BuildSeqGraph(samples map[string]*[]*seqPCR,
|
||||
npairs += nseq * (nseq - 1) / 2
|
||||
}
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -503,16 +514,19 @@ func BuildSeqGraph(samples map[string]*[]*seqPCR,
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
progressbar.OptionSetDescription("[One error graph]"),
|
||||
)
|
||||
bar = progressbar.NewOptions(npairs, pbopt...)
|
||||
}
|
||||
|
||||
bar := progressbar.NewOptions(npairs, pbopt...)
|
||||
for _, seqs := range samples {
|
||||
np := buildSamplePairs(seqs, workers)
|
||||
|
||||
if bar != nil {
|
||||
bar.Add(np)
|
||||
}
|
||||
}
|
||||
|
||||
if maxError > 1 {
|
||||
pbopt = make([]progressbar.Option, 0, 5)
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
progressbar.OptionSetWidth(15),
|
||||
@@ -520,12 +534,14 @@ func BuildSeqGraph(samples map[string]*[]*seqPCR,
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
progressbar.OptionSetDescription("[Adds multiple errors]"),
|
||||
)
|
||||
|
||||
bar = progressbar.NewOptions(npairs, pbopt...)
|
||||
}
|
||||
|
||||
for _, seqs := range samples {
|
||||
np := extendSimilarityGraph(seqs, maxError, workers)
|
||||
if bar != nil {
|
||||
bar.Add(np)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ var __output_in_json__ = false
|
||||
var __output_fastjson_format__ = false
|
||||
var __output_fastobi_format__ = false
|
||||
|
||||
var __no_progress_bar__ = false
|
||||
var __skip_empty__ = false
|
||||
var __skip_on_error__ = false
|
||||
|
||||
@@ -82,7 +81,7 @@ func InputOptionSet(options *getoptions.GetOpt) {
|
||||
}
|
||||
|
||||
func OutputModeOptionSet(options *getoptions.GetOpt, compressed bool) {
|
||||
options.BoolVar(&__no_progress_bar__, "no-progressbar", false,
|
||||
options.BoolVar(obidefault.NoProgressBarPtr(), "no-progressbar", obidefault.NoProgressBar(),
|
||||
options.Description("Disable the progress bar printing"))
|
||||
|
||||
if compressed {
|
||||
@@ -233,7 +232,7 @@ func CLIProgressBar() bool {
|
||||
oo, _ := os.Stdout.Stat()
|
||||
toPipe := (oo.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe
|
||||
|
||||
return onTerminal && !toPipe && !__no_progress_bar__
|
||||
return onTerminal && !toPipe && obidefault.ProgressBar()
|
||||
}
|
||||
|
||||
func CLIOutPutFileName() string {
|
||||
|
||||
@@ -210,9 +210,7 @@ func CLIReadBioSequences(filenames ...string) (obiiter.IBioSequence, error) {
|
||||
|
||||
}
|
||||
|
||||
if CLIProgressBar() {
|
||||
iterator = iterator.Speed("Reading sequences")
|
||||
}
|
||||
|
||||
return iterator, nil
|
||||
}
|
||||
|
||||
@@ -12,9 +12,7 @@ import (
|
||||
func CLIWriteSequenceCSV(iterator obiiter.IBioSequence,
|
||||
terminalAction bool, filenames ...string) *obiitercsv.ICSVRecord {
|
||||
|
||||
if obiconvert.CLIProgressBar() {
|
||||
iterator = iterator.Speed("Writing CSV")
|
||||
}
|
||||
|
||||
opts := make([]WithOption, 0, 10)
|
||||
|
||||
|
||||
@@ -42,6 +42,8 @@ func MapOnLandmarkSequences(library obiseq.BioSequenceSlice, landmark_idx []int,
|
||||
|
||||
seqworld := obiutils.Make2DArray[float64](library_size, n_landmark)
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -51,7 +53,8 @@ func MapOnLandmarkSequences(library obiseq.BioSequenceSlice, landmark_idx []int,
|
||||
progressbar.OptionSetDescription("[Sequence mapping]"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(library_size, pbopt...)
|
||||
bar = progressbar.NewOptions(library_size, pbopt...)
|
||||
}
|
||||
|
||||
waiting := sync.WaitGroup{}
|
||||
waiting.Add(nworkers)
|
||||
@@ -66,8 +69,10 @@ func MapOnLandmarkSequences(library obiseq.BioSequenceSlice, landmark_idx []int,
|
||||
match, lalign := obialign.FastLCSScore(landmark, seq, -1, &buffer)
|
||||
coord[j] = float64(lalign - match)
|
||||
}
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
}
|
||||
}
|
||||
waiting.Done()
|
||||
}
|
||||
|
||||
@@ -170,6 +175,8 @@ func CLISelectLandmarkSequences(iterator obiiter.IBioSequence) obiiter.IBioSeque
|
||||
taxa.Set(i, taxon)
|
||||
}
|
||||
|
||||
var bar2 *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -179,14 +186,15 @@ func CLISelectLandmarkSequences(iterator obiiter.IBioSequence) obiiter.IBioSeque
|
||||
progressbar.OptionSetDescription("[Sequence Indexing]"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(len(library), pbopt...)
|
||||
bar2 = progressbar.NewOptions(len(library), pbopt...)
|
||||
}
|
||||
|
||||
for i, seq := range library {
|
||||
idx := obirefidx.GeomIndexSesquence(i, library, taxa, taxo)
|
||||
seq.SetOBITagGeomRefIndex(idx)
|
||||
|
||||
if i%10 == 0 {
|
||||
bar.Add(10)
|
||||
if bar2 != nil && i%10 == 0 {
|
||||
bar2.Add(10)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,6 +207,8 @@ func IndexFamilyDB(iterator obiiter.IBioSequence) obiiter.IBioSequence {
|
||||
|
||||
log.Infof("Done. Found %d clusters", clusters.Len())
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -216,7 +218,8 @@ func IndexFamilyDB(iterator obiiter.IBioSequence) obiiter.IBioSequence {
|
||||
progressbar.OptionSetDescription("Cluster indexing"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(len(clusters), pbopt...)
|
||||
bar = progressbar.NewOptions(len(clusters), pbopt...)
|
||||
}
|
||||
|
||||
limits := make(chan [2]int)
|
||||
waiting := sync.WaitGroup{}
|
||||
@@ -233,9 +236,11 @@ func IndexFamilyDB(iterator obiiter.IBioSequence) obiiter.IBioSequence {
|
||||
for i := l[0]; i < l[1]; i++ {
|
||||
idx := IndexSequence(i, clusters, &kcluster, taxa, taxonomy)
|
||||
clusters[i].SetOBITagRefIndex(idx)
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
waiting.Done()
|
||||
}
|
||||
|
||||
@@ -239,6 +239,8 @@ func IndexReferenceDB(iterator obiiter.IBioSequence) obiiter.IBioSequence {
|
||||
|
||||
log.Info("done")
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if obidefault.ProgressBar() {
|
||||
pbopt := make([]progressbar.Option, 0, 5)
|
||||
pbopt = append(pbopt,
|
||||
progressbar.OptionSetWriter(os.Stderr),
|
||||
@@ -248,7 +250,8 @@ func IndexReferenceDB(iterator obiiter.IBioSequence) obiiter.IBioSequence {
|
||||
progressbar.OptionSetDescription("[Sequence Processing]"),
|
||||
)
|
||||
|
||||
bar := progressbar.NewOptions(len(references), pbopt...)
|
||||
bar = progressbar.NewOptions(len(references), pbopt...)
|
||||
}
|
||||
|
||||
limits := make(chan [2]int)
|
||||
indexed := obiiter.MakeIBioSequence()
|
||||
@@ -267,8 +270,10 @@ func IndexReferenceDB(iterator obiiter.IBioSequence) obiiter.IBioSequence {
|
||||
iref := references[i].Copy()
|
||||
iref.SetOBITagRefIndex(idx)
|
||||
sl = append(sl, iref)
|
||||
if bar != nil {
|
||||
bar.Add(1)
|
||||
}
|
||||
}
|
||||
indexed.Push(obiiter.MakeBioSequenceBatch(source, l[0]/10, sl))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user