Skip to content

Commit d4e230f

Browse files
committed
improve logging
1 parent c174715 commit d4e230f

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

read2tree/Mapper.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def __init__(self, args, ref_set=None, og_set=None, species_name=None, step=None
5555
self.elapsed_time = 0
5656

5757
self.logger = logging.getLogger(__name__)
58+
if self.args.debug:
59+
self.logger.setLevel(logging.DEBUG)
60+
self.logger.debug("Debug mode is on")
61+
5862

5963
self._reads = self.args.reads
6064
if not species_name:
@@ -126,7 +130,7 @@ def _call_wrapper(self, ref_file_handle, reads, tmp_output_folder):
126130
line_minimap= minimap2_ex +" "+ minimap_argm + " -t " + str(self.args.threads) +" "+ ref_file_handle + " " + reads_str + " > " + sam_file
127131
#self._rm_file(ref_file_handle + "-enc.2.ngm", ignore_error=True)
128132
self._output_shell(line_minimap)
129-
self.logger.info('mapping with ' + line_minimap)
133+
self.logger.debug('mapping with ' + line_minimap)
130134

131135
# if len(self._reads) == 2:
132136
# ngm_wrapper = NGM(ref_file_handle, reads, tmp_output_folder)
@@ -180,19 +184,20 @@ def _read_mapping_from_folder(self, mapping_name=None, ref_records=None):
180184
map_reads_species = {}
181185
if not mapping_name:
182186
mapping_name = self._mapping_name
183-
self.logger.debug('--- Retrieve mapped consensus sequences ---'+str(mapping_name))
187+
self.logger.info('--- Retrieve mapped consensus sequences ---'+str(mapping_name))
184188
in_folder = os.path.join(self.args.output_path, "04_mapping_"+mapping_name)
185189

186190
bam_files = glob.glob(os.path.join(in_folder, "*.bam"))
187191
if self.args.min_cons_coverage >= 2 and bam_files: # default self.args.min_cons_coverage is 1
188-
self.logger.debug('Generating consensus from bam files' )
192+
self.logger.info('Generating consensus from bam files' )
189193
for file in tqdm(bam_files, desc='Generating consensus from bam files ', unit=' species'):
190194
species = file.split("/")[-1].split("_")[0]
191195
ref_file = os.path.join(self.args.output_path, '02_ref_dna',species+'_OGs.fa')
192196
map_reads_species[species] = Reference()
193197
line_samtools_index = samtools+' index -@ ' + str(self.args.threads) + ' ' +file
194-
self._output_shell(line_samtools_index)
195198
self.logger.debug(line_samtools_index)
199+
self._output_shell(line_samtools_index)
200+
196201

197202
consensus = self._build_consensus_seq_v2(ref_file, file)
198203
records = []
@@ -213,7 +218,7 @@ def _read_mapping_from_folder(self, mapping_name=None, ref_records=None):
213218
seqC.write_seq_completeness(os.path.join(in_folder,species + "_OGs_sc.txt"))
214219
self.all_sc.update(seqC.seq_completeness)
215220
else:
216-
self.logger.debug('Loading consensus read mappings '+in_folder+ "*_consensus.fa")
221+
self.logger.info('Loading consensus read mappings '+in_folder+ "*_consensus.fa")
217222
for file in tqdm(glob.glob(os.path.join(in_folder, "*_consensus.fa")), desc='Loading consensus read mappings ', unit=' species'):
218223
species = file.split("/")[-1].split("_")[0]
219224
map_reads_species[species] = Reference()
@@ -266,7 +271,7 @@ def _make_tmpdir(self):
266271
# .format(self._species_name))
267272
# else:
268273
# tmp_output_folder = tempfile.TemporaryDirectory(prefix='ngm_')
269-
# self.logger.debug('--- Creating tmp directory on local node ---')
274+
# self.logger.info('--- Creating tmp directory on local node ---')
270275
tmp_output_folder=self.args.output_path
271276
return tmp_output_folder
272277

@@ -340,14 +345,14 @@ def _map_reads_to_references(self, ref):
340345
species+"_OGs_sc.txt"))
341346
self.all_sc.update(seqC.seq_completeness)
342347
except AttributeError as a:
343-
self.logger.debug('Reads not properly processed for further steps.')
344-
self.logger.debug('AttributeError: {}'.format(a))
348+
self.logger.info('Reads not properly processed for further steps.')
349+
self.logger.info('AttributeError: {}'.format(a))
345350
except ValueError as v:
346-
self.logger.debug('Reads not properly processed for further steps.')
347-
self.logger.debug('ValueError: {}'.format(v))
351+
self.logger.info('Reads not properly processed for further steps.')
352+
self.logger.info('ValueError: {}'.format(v))
348353
except TypeError as t:
349-
self.logger.debug('Reads not properly processed for further steps.')
350-
self.logger.debug('TypeError: {}'.format(t))
354+
self.logger.info('Reads not properly processed for further steps.')
355+
self.logger.info('TypeError: {}'.format(t))
351356
else:
352357
mapped_reads = []
353358

@@ -462,7 +467,7 @@ def _get_mapping_stats(self, bam_file):
462467
# """
463468
# bam_file = sam_file_base+ '_sorted.bam'
464469
#
465-
# self.logger.debug("{}: --- Binning reads ---".format(self._species_name))
470+
# self.logger.info("{}: --- Binning reads ---".format(self._species_name))
466471
# output_folder = os.path.join(self.args.output_path, "04_read_ogs_" +
467472
# self._species_name)
468473
# if not os.path.exists(output_folder):
@@ -574,38 +579,41 @@ def _post_process_read_mapping(self, ref_file, sam_file):
574579
# ref_file.split('/')[-1].split('.')[0] +
575580
# "_post")
576581
# if self.args.single_mapping:
577-
# self.logger.debug("{}: --- POSTPROCESSING MAPPING ---".format(self._species_name))
582+
# self.logger.info("{}: --- POSTPROCESSING MAPPING ---".format(self._species_name))
578583

579584
# ngmlr doesn't have the option to write in bam file directly
580585
#if 'sam' in bam_file.split(".")[-1]:
581586
# sam_file = bam_file
582587
sam_file_base = sam_file[:-4]
583588
if os.path.exists(sam_file):
584589
line_samtools_view = samtools+' view -F 4 -bh -S -@ ' + str(self.args.threads) +' -o ' + sam_file_base + ".bam " + sam_file
585-
self._output_shell(line_samtools_view)
586590
self.logger.debug(line_samtools_view)
591+
self._output_shell(line_samtools_view)
592+
587593
else:
588-
self.logger.debug("Sam file is not generated", sam_file)
594+
self.logger.info("Sam file is not generated", sam_file)
589595

590596
# if self.args.single_mapping:
591597
# self.logger.error("single mapping is not tested with this version ")
592-
#self.logger.debug("{}: ---- Samtools view completed".format(self._species_name))
598+
#self.logger.info("{}: ---- Samtools view completed".format(self._species_name))
593599

594600
if os.path.exists(sam_file_base+".bam"):
595601

596602
line_samtools_sort = samtools+' sort -@ ' + str(self.args.threads) + ' -o ' + sam_file_base + "_sorted.bam " + sam_file_base+".bam"
603+
self.logger.debug("Running "+ line_samtools_sort)
597604
self._output_shell(line_samtools_sort)
598-
self.logger.debug("running "+ line_samtools_sort)
605+
599606
else:
600-
self.logger.debug("bam file is not generated", sam_file_base+".bam")
607+
self.logger.info("BAM file is not generated", sam_file_base+".bam")
601608

602609
# if self.args.single_mapping:
603-
# self.logger.debug("{}: ---- Samtools sort completed".format(self._species_name))
610+
# self.logger.info("{}: ---- Samtools sort completed".format(self._species_name))
604611

605612
if os.path.exists(sam_file_base + "_sorted.bam"):
606613
line_samtools_index= samtools+' index -@ ' + str(self.args.threads) + ' ' + sam_file_base + "_sorted.bam"
607-
self._output_shell(line_samtools_index)
608614
self.logger.debug(line_samtools_index)
615+
self._output_shell(line_samtools_index)
616+
609617

610618
self._rm_file(sam_file_base + ".bam", ignore_error=True)
611619
self._rm_file(sam_file_base + ".sam", ignore_error=True)
@@ -682,27 +690,27 @@ def _output_shell(self, line):
682690
shell_command = subprocess.Popen( line, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
683691

684692
#except:
685-
# self.logger.debug("Shell command failed to execute by running ")
693+
# self.logger.info("Shell command failed to execute by running ")
686694
# return None
687695

688696
# try:
689-
# self.logger.debug("Running " + line)
697+
# self.logger.info("Running " + line)
690698
# shell_command = subprocess.Popen(
691699
# line, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
692700
# shell=True)
693701
# except:
694-
# self.logger.debug("Shell command failed to execute by running ")
702+
# self.logger.info("Shell command failed to execute by running ")
695703
# return None
696704

697705
(output, err) = shell_command.communicate()
698706
if output:
699707
self.logger.debug("Shell output: "+ str(output))
700-
print("Shell output: " + str(output))
708+
#print("Shell output: " + str(output))
701709
if err:
702710
self.logger.debug("Shell err: " + str(err))
703-
print("Shell err: " + str(err))
711+
#print("Shell err: " + str(err))
704712
# if err:
705-
# self.logger.debug("Shell err: " + str(err))
713+
# self.logger.info("Shell err: " + str(err))
706714

707715
shell_command.wait()
708716
if shell_command.returncode != 0:

read2tree/TreeInference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class TreeInference(object):
1818

1919
def __init__(self, args, concat_alignment=None):
20-
print('--- Tree inference ---')
20+
#print('--- Tree inference ---')
2121

2222
self.args = args
2323

read2tree/main.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@
3434
COPYRIGHT = '(C) 2017-{:d} David V Dylus'.format(date.today().year)
3535

3636
# logger = logging.getLogger(__name__)
37-
logger_level = "DEBUG" # DEBUG INFO # TRACE DEBUG INFO WARN ERROR FATAL
37+
3838
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
3939
logger = logging.getLogger(__name__)
4040

41-
if logger_level == "INFO":
42-
logger.setLevel(logging.INFO)
4341

4442
# logger.disabled = True
4543

@@ -78,7 +76,7 @@ def parse_args(argv, exe_name, desc):
7876
'Examples: -ax sr , -ax map-hifi , -ax map-pb or -ax map-ont ')
7977

8078
arg_parser.add_argument('--threads', type=int, default=1,
81-
help='[Default is 1] Number of threads for the mapping ')
79+
help='[Default is 1] Number of threads for gene marker alignment (mafft) and read mapping (minimap2) and tree inference (iqtree) ')
8280

8381
# arg_parser.add_argument('--split_reads', action='store_true',
8482
# help='[Default is off] Splits reads as defined by split_len (200) '
@@ -213,6 +211,10 @@ def parse_args(argv, exe_name, desc):
213211
# Parse the arguments.
214212
args = arg_parser.parse_args(argv)
215213

214+
if args.debug: # "DEBUG" # DEBUG INFO # TRACE DEBUG INFO WARN ERROR FATAL
215+
logger.setLevel(logging.DEBUG)
216+
logger.debug("Debug mode is on")
217+
216218
_reads = ""
217219
_species_name = ""
218220

@@ -331,7 +333,7 @@ def main(argv, exe_name, desc=''):
331333
logger.error("read file doesn't exist")
332334
sys.exit()
333335

334-
if args.step == "all":
336+
if args.step == "all":
335337
logger.info('{}: ------- NEW RUN -------'.format(args.species_name))
336338
oma_output = OMAOutputParser(args)
337339
args.oma_output_path = oma_output.oma_output_path

0 commit comments

Comments
 (0)