Skip to content

Commit 5681db1

Browse files
committed
Improve behavior of --verbose and --debug.
1 parent dd200d2 commit 5681db1

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

bin/phylociraptor_modules/usageinfo.py

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def hi():
6363
6464
--dry Make a dry run.
6565
--verbose Display more output.
66+
--debug Display even more output. (Used mainly for debugging)
6667
-h, --help Display help.
6768
"""
6869

phylociraptor

+60-38
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,23 @@ def check_required_files(runmode):
9898
for f in outfile_dict[runmode]:
9999
if not os.path.isfile(f):
100100
if debug:
101-
print(now(),"DEBUG, check_required_files: File not found:", f)
101+
print(now(),"DEBUG: check_required_files: File not found:", f)
102102
return f
103103
return
104104

105105
def check_directories(mode):
106106
for directory in outdir_dict[mode]:
107107
if not os.path.isdir(directory):
108108
if debug:
109-
print(now(), "DEBUG, check_directories: Directory not found:", directory)
109+
print(now(), "DEBUG: check_directories: Directory not found:", directory)
110110
return directory
111111
return
112112

113113
def check_hasrun(mode):
114114
f = checkpoint_file_dict[mode]
115115
if not os.path.isfile(f):
116116
if debug:
117-
print(now(), "DEBUG, check_hasrun: Not found:", f)
117+
print(now(), "DEBUG: check_hasrun: Not found:", f)
118118
return f
119119
else:
120120
return
@@ -128,15 +128,15 @@ def check_config_file(f):
128128

129129
def check_for_errors(result):
130130
if result.startswith("WorkflowError"):
131-
return now()+" ERROR: phylociraptor encountered an error. You may try to run with --verbose to diagnose what has gone wrong.\n"
131+
return now()+" ERROR: phylociraptor encountered an error. Use --debug and --verbose to diagnose.\n"
132132
if result.startswith("The singularity command"):
133133
return result
134134
if result.startswith("Error") or result.startswith("error"):
135-
return now()+" ERROR: There was an error. Maybe run with --verbose to diagnose. The error occurred here: %s " % result
135+
return now()+" ERROR: There was an error. Use --debug and --verbose to diagnose. The error occurred here: %s " % result
136136
if result.startswith("Directory cannot be locked."):
137137
return now()+" ERROR: "+result
138138
if result.startswith("IncompleteFilesException"):
139-
return now()+" ERROR: There seems to be a problem with incomplete output files from a previous run.\nYou can run --verbose to see which files are incomplete and how to resolve the problem.\n"
139+
return now()+" ERROR: There seems to be a problem with incomplete output files from a previous run. You can use --rerun-incomplete to force a rerun of incomplete tasks.\n"
140140
if result.startswith("KeyError"):
141141
return now()+" ERROR: "+result
142142
return ""
@@ -174,9 +174,11 @@ def execute_command(cmd, verbose, isutil=False):
174174
print(now(), "INFO: --dry specified, will only perform a dry run of the analysis")
175175
for line in io.TextIOWrapper(popen.stdout, encoding="utf-8"):
176176
nline += 1
177-
if verbose:
178-
if debug:
179-
yield line
177+
if debug:
178+
yield line
179+
elif verbose:
180+
if check_for_errors(line):
181+
yield check_for_errors(line)
180182
else:
181183
if line.startswith("rule") and not debug:
182184
yield now()+" Task: %s\n" % line.strip().split(" ")[1].rstrip(":")
@@ -192,7 +194,7 @@ def execute_command(cmd, verbose, isutil=False):
192194
jobcounts = True
193195
elif jobcounts and result != "" and nline <= 30: #only keep jobcount info from the beginning of the output
194196
if len(result.split("\t")) == 2: # the line with the total number of jobs has two elements
195-
yield now()+" Total number of tasks to run/submit: %s\n" % result.split("\t")[1].strip()
197+
yield now()+" Total number of tasks for this step: %s\n" % result.split("\t")[1].strip()
196198
elif jobcounts and result == "":
197199
jobcounts = False
198200
line=""
@@ -204,9 +206,11 @@ def execute_command(cmd, verbose, isutil=False):
204206
for line in io.TextIOWrapper(popen.stdout, encoding="utf-8"):
205207
nline += 1
206208
logging.debug(line.strip())
207-
if verbose:
208-
if debug:
209-
yield line
209+
if debug:
210+
yield line
211+
elif verbose:
212+
if check_for_errors(line):
213+
yield check_for_errors(line)
210214
else:
211215
if line.startswith("Pulling singularity"):
212216
container = line.rstrip().split(" ")[-1].strip(".")
@@ -246,17 +250,16 @@ def execute_command(cmd, verbose, isutil=False):
246250
for line in io.TextIOWrapper(popen.stdout, encoding="utf-8"):
247251
nline += 1
248252
logging.debug(line.strip())
249-
if verbose:
250-
if debug:
251-
yield line
252-
else:
253-
if line.startswith("Pulling singularity"):
254-
container = line.rstrip().split(" ")[-1].strip(".")
255-
yield now() + " Please be patient as singularity container " + container + " is downloaded. This can take several minutes.\n"
256-
if line.startswith("rule") and not debug:
257-
yield now()+" Will run task: %s\n" % line.strip().split(" ")[1].rstrip(":")
258-
if line.lstrip().startswith("wildcards:"):
259-
yield "\tParameters: %s\n" % line.split(":")[1]
253+
if debug:
254+
yield line
255+
elif verbose:
256+
if line.startswith("Pulling singularity"):
257+
container = line.rstrip().split(" ")[-1].strip(".")
258+
yield now() + " Please be patient as singularity container " + container + " is downloaded. This can take several minutes.\n"
259+
if line.startswith("rule") and not debug:
260+
yield now()+" Will run task: %s\n" % line.strip().split(" ")[1].rstrip(":")
261+
if line.lstrip().startswith("wildcards:"):
262+
yield "\tParameters: %s\n" % line.split(":")[1]
260263
else:
261264
result = line
262265
if check_for_errors(result):
@@ -298,20 +301,21 @@ def get_additional_snakemake_flags(flags, rerun, unlock):
298301
else:
299302
flags += "-p"
300303
if flags:
301-
print(now(), "INFO: Additional flags will be passed on to snakemake: ", flags)
304+
if debug:
305+
print(now(), "DEBUG: Additional flags will be passed on to snakemake: ", flags)
302306
return flags.split(" ")
303307
else:
304308
return []
305309

306310
def get_additional_singularity_flags(flags):
307311
if flags:
308-
print(now(), "INFO: Additional flags will be passed on to singularity: ", flags)
312+
if debug:
313+
print(now(), "DEBUG: Additional flags will be passed on to singularity: ", flags)
309314
return ["--singularity-args"]+[singularity_bindpoints +" " + flags]
310315
else:
311316
return ["--singularity-args"]+ [singularity_bindpoints]
312317

313318
pars = argparse.ArgumentParser(usage=help_message(default_help))
314-
pars.add_argument('--debug', action='store_true', dest="debug", required=False)
315319
pars.add_argument('-v', '--version', action='store_true', dest='version', required=False)
316320
pars.add_argument('command', action='store', nargs="?")
317321
pars.add_argument('arguments', action='store', nargs=argparse.REMAINDER)
@@ -326,9 +330,6 @@ if args.version == True:
326330
print("Git commit:", commit)
327331
sys.exit(0)
328332

329-
if args.debug == True:
330-
print(now(), "DEBUG: Addditional debugging output enabled.")
331-
debug=True
332333

333334
if not args.command:
334335
print(default_help)
@@ -349,12 +350,21 @@ class PhyloParser(argparse.ArgumentParser):
349350
self.add_argument("--snakemake", action="store",dest="sm_args", default="")
350351
self.add_argument("--rerun-incomplete", action="store_true", dest="rerun", default=False)
351352
self.add_argument("--unlock", action="store_true", dest="unlock", default=False)
353+
self.add_argument('--debug', action='store_true', dest="debug", required=False)
354+
355+
def check_debug(debug):
356+
if debug:
357+
#print(now(), "DEBUG: Addditional debugging output enabled.")
358+
return True
359+
else:
360+
return False
352361

353362
class UtilParser(argparse.ArgumentParser):
354363
def __init__(self, **kwargs):
355364
super().__init__(**kwargs)
356365
self.add_argument("-h", "--help", action="store_true")
357366
self.add_argument("--verbose", action="store_true", default=False)
367+
self.add_argument('--debug', action='store_true', dest="debug", required=False)
358368

359369

360370
if args.command == "setup":
@@ -366,6 +376,7 @@ if args.command == "setup":
366376
setup_parser.add_argument("--add_genomes", action="store_true",default=False, dest="add_genomes")
367377

368378
setup_args = setup_parser.parse_args(args.arguments)
379+
debug = check_debug(setup_args.debug) #check if debug is enabled
369380
if setup_args.help or len(sys.argv) <= 2:
370381
print(help_message(setup_help))
371382
sys.exit(0)
@@ -407,6 +418,7 @@ elif args.command=="orthology":
407418
orthology_parser.add_argument("--augustus_species", action="store")
408419
orthology_parser.add_argument("--additional_params", action="store")
409420
orthology_args = orthology_parser.parse_args(args.arguments)
421+
debug = check_debug(orthology_args.debug) #check if debug is enabled
410422
if orthology_args.help or len(sys.argv) <= 2: # help is specified
411423
print(help_message(orthology_help))
412424
sys.exit(0)
@@ -451,6 +463,7 @@ elif args.command=="filter-orthology":
451463
forthology_parser.add_argument("--seqtype", action="store")
452464

453465
forthology_args = forthology_parser.parse_args(args.arguments)
466+
debug = check_debug(forthology_args.debug) #check if debug is enabled
454467
if forthology_args.help or len(sys.argv) <= 2: # help is specified
455468
print(help_message(forthology_help))
456469
sys.exit(0)
@@ -506,6 +519,7 @@ elif args.command=="align":
506519
align_parser.add_argument("--threads", action="store")
507520
align_parser.add_argument("--parameters", action="store")
508521
align_args = align_parser.parse_args(args.arguments)
522+
debug = check_debug(align_args.debug) #check if debug is enabled
509523
if align_args.help or len(sys.argv) <= 2: # help is specified
510524
print(help_message(align_help))
511525
sys.exit(0)
@@ -553,6 +567,7 @@ elif args.command=="filter-align":
553567
falign_parser.add_argument("--min_parsimony_sites", action="store")
554568

555569
falign_args = falign_parser.parse_args(args.arguments)
570+
debug = check_debug(falign_args.debug) #check if debug is enabled
556571
if falign_args.help or len(sys.argv) <= 2: # help is specified
557572
print(help_message(falign_help))
558573
sys.exit(0)
@@ -592,6 +607,7 @@ elif args.command=="speciestree":
592607
sptree_parser = PhyloParser(add_help=False)
593608
sptree_parser.add_argument("--config-file", action="store", dest="config_file", default="data/config.yaml")
594609
sptree_args = sptree_parser.parse_args(args.arguments)
610+
debug = check_debug(sptree_args.debug) #check if debug is enabled
595611
if sptree_args.help or len(sys.argv) <= 2: # help is specified
596612
print(help_message(sptree_help))
597613
sys.exit(0)
@@ -620,6 +636,8 @@ elif args.command=="njtree":
620636
njtree_parser = PhyloParser(add_help=False)
621637
njtree_parser.add_argument("--config-file", action="store", dest="config_file", default="data/config.yaml")
622638
njtree_args = njtree_parser.parse_args(args.arguments)
639+
debug = check_debug(njtree_args.debug) #check if debug is enabled
640+
623641
if njtree_args.help or len(sys.argv) <= 2: # help is specified
624642
print(help_message(njtree_help))
625643
sys.exit(0)
@@ -648,6 +666,7 @@ elif args.command=="mltree":
648666
tree_parser = PhyloParser(add_help=False)
649667
tree_parser.add_argument("--config-file", action="store", dest="config_file", default="data/config.yaml")
650668
tree_args = tree_parser.parse_args(args.arguments)
669+
debug = check_debug(tree_args.debug) #check if debug is enabled
651670
if tree_args.help or len(sys.argv) <= 2: # help is specified
652671
print(help_message(tree_help))
653672
sys.exit(0)
@@ -677,6 +696,7 @@ elif args.command=="modeltest":
677696
model_parser = PhyloParser(add_help=False)
678697
model_parser.add_argument("--config-file", action="store", dest="config_file", default="data/config.yaml")
679698
model_args = model_parser.parse_args(args.arguments)
699+
debug = check_debug(model_args.debug) #check if debug is enabled
680700
if model_args.help or len(sys.argv) <= 2: # help is specified
681701
print(help_message(model_help))
682702
sys.exit(0)
@@ -708,6 +728,7 @@ elif args.command=="report":
708728
report_parser.add_argument("--figure", action="store_true", default=False)
709729
report_parser.add_argument("--config-file", action="store", dest="config_file", default="data/config.yaml")
710730
report_args = report_parser.parse_args(args.arguments)
731+
debug = check_debug(report_args.debug) #check if debug is enabled
711732
print(now(), "Will create a phylociraptor report...")
712733

713734
if report_args.help: # help is specified
@@ -745,6 +766,7 @@ elif args.command == "check":
745766
check_parser.add_argument("-h", "--help", action="store_true")
746767
check_parser.add_argument("--verbose", action="store_true", default=False)
747768
check_args = check_parser.parse_args(args.arguments)
769+
debug = check_debug(check_args.debug) #check if debug is enabled
748770

749771
if check_args.help:
750772
print(help_message(check_help))
@@ -992,7 +1014,7 @@ elif args.command == "util":
9921014
sys.exit(0)
9931015
else:
9941016
which_util = args.arguments.pop(0)
995-
1017+
debug = check_debug(True) #for util debugging output is enabled by default
9961018
if which_util == "get-lineage":
9971019
gl_parser = UtilParser(add_help=False)
9981020
gl_parser.add_argument("-d","--datafile", action="store", default="data/data.csv")
@@ -1024,7 +1046,7 @@ elif args.command == "util":
10241046
print(line, end="\r")
10251047
print(now(), "Will now reformat lineage information for downstream applications like phylociraptor util plot-tree")
10261048
cmd = ["singularity", "exec", "-B", os.getcwd(), "docker://reslp/biopython_plus:1.77", "python", "bin/reformat_lineage.py", "data/lineage_information/lineage_information.csv", gl_args.outfile]
1027-
if debug:
1049+
if debug and gl_args.verbose:
10281050
print(cmd)
10291051
for line in execute_command(cmd, not gl_args.quiet):
10301052
print(line, end="\r")
@@ -1074,7 +1096,7 @@ elif args.command == "util":
10741096
sys.exit(1)
10751097

10761098

1077-
if debug:
1099+
if debug and qs_args.verbose:
10781100
print(cmd)
10791101
for line in execute_command(cmd, not qs_args.quiet):
10801102
print(line, end="\r")
@@ -1112,7 +1134,7 @@ elif args.command == "util":
11121134
cmd = ["singularity", "exec", "-B", os.getcwd(), rphylocontainer, "Rscript", "bin/plot-tree.R"]
11131135
cmd += [os.getcwd(), qs_args.seed, all_trees, qs_args.outgroup, qs_args.lineagefile, qs_args.level, qs_args.outprefix, single]
11141136

1115-
if debug:
1137+
if debug and qs_args.verbose:
11161138
print(cmd)
11171139
for line in execute_command(cmd, not qs_args.quiet):
11181140
print(line, end="\r")
@@ -1149,7 +1171,7 @@ elif args.command == "util":
11491171
cmd = ["singularity", "exec", "-B", os.getcwd(), rphylocontainer, "Rscript", "bin/plot-conflict.R"]
11501172
cmd += [os.getcwd(), qs_args.seed, all_trees, qs_args.outgroup, qs_args.lineagefile, qs_args.level, qs_args.quartetfile, qs_args.treelist]
11511173

1152-
if debug:
1174+
if debug and qs_args.verbose:
11531175
print(cmd)
11541176
for line in execute_command(cmd, not qs_args.quiet):
11551177
print(line, end="\r")
@@ -1187,7 +1209,7 @@ elif args.command == "util":
11871209
print(help_message(util_plot_pca_help))
11881210
sys.exit(1)
11891211
cmd = ["singularity", "exec", "-B", os.getcwd(), rphylocontainer, "Rscript", "bin/plot-t2t-pca.R", os.getcwd(), qs_args.treelist, qs_args.threads, qs_args.ndistances, qs_args.seed]
1190-
if debug:
1212+
if debug and qs_args.verbose:
11911213
print(cmd)
11921214
for line in execute_command(cmd, not qs_args.quiet):
11931215
print(line, end="\r")
@@ -1217,7 +1239,7 @@ elif args.command == "util":
12171239
if qs_args.genes and qs_args.genes != "none":
12181240
first_arg = "genes="+qs_args.genes
12191241
cmd = ["bash", "bin/modify-busco.sh", first_arg, qs_args.buscoset, qs_args.seed]
1220-
if debug:
1242+
if debug and qs_args.verbose:
12211243
print(cmd)
12221244
for line in execute_command(cmd, not qs_args.quiet):
12231245
print(line, end="\r")
@@ -1240,7 +1262,7 @@ elif args.command == "util":
12401262
cmd += ["--cancel"]
12411263
if qs_args.verbose:
12421264
cmd += ["--verbose"]
1243-
if debug:
1265+
if debug and qs_args.verbose:
12441266
print(cmd)
12451267
for line in execute_command(cmd, not qs_args.quiet, True):
12461268
print(line, end="\r")

0 commit comments

Comments
 (0)