Skip to content

Commit 038187f

Browse files
committed
Pass tqdm through logger
1 parent 918aa2f commit 038187f

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

extend.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from Bio.SeqIO import SeqRecord
44
from Bio.Align import MultipleSeqAlignment
55
from glob import glob
6+
import logging
67
import tempfile
78
from pathlib import Path
89
import re
@@ -15,7 +16,7 @@
1516
import numpy as np
1617
from Bio.AlignIO.MafIO import MafWriter, MafIterator
1718
from Bio.AlignIO.MauveIO import MauveWriter, MauveIterator
18-
from logger import logger
19+
from logger import logger, TqdmToLogger, MIN_TQDM_INTERVAL
1920
from tqdm import tqdm
2021
import time
2122
import spoa
@@ -77,7 +78,7 @@ def xmfa_to_covered(xmfa_file, index_to_gid, gid_to_index_to_cid):
7778
seqid_parser = re.compile(r'^cluster(\d+) s(\d+):p(\d+)/.*')
7879
idpair_to_segments = defaultdict(list)
7980
cluster_to_named_segments = defaultdict(list)
80-
for aln in tqdm(AlignIO.parse(xmfa_file, "mauve")):
81+
for aln in AlignIO.parse(xmfa_file, "mauve"):
8182
for seq in aln:
8283
# Skip reference for now...
8384
aln_len = seq.annotations["end"] - seq.annotations["start"]
@@ -146,7 +147,11 @@ def extend_clusters(xmfa_file, gid_to_index, gid_to_cid_to_index, idpair_to_segm
146147
ret_lcbs = []
147148
seqid_parser = re.compile(r'^cluster(\d+) s(\d+):p(\d+)/.*')
148149

149-
for aln_idx, aln in enumerate(tqdm(AlignIO.parse(xmfa_file, "mauve"), total=len(cluster_to_named_segments))):
150+
for aln_idx, aln in enumerate(tqdm(
151+
AlignIO.parse(xmfa_file, "mauve"),
152+
total=len(cluster_to_named_segments),
153+
file=TqdmToLogger(logger, level=logging.INFO),
154+
mininterval=MIN_TQDM_INTERVAL)):
150155
# validate_lcb(aln, gid_to_records, parsnp_header=True)
151156
seq = aln[0]
152157
cluster_idx, contig_idx, startpos = [int(x) for x in seqid_parser.match(seq.id).groups()]

logger.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import io
23
############################################# Logging ##############################################
34
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
45
#These are the sequences need to get colored ouput
@@ -14,6 +15,28 @@
1415
COLOR_SEQ = "\033[1;%dm"
1516
BOLD_SEQ = "\033[1m"
1617

18+
MIN_TQDM_INTERVAL=30
19+
20+
21+
# Logging redirect copied from https://stackoverflow.com/questions/14897756/python-progress-bar-through-logging-module
22+
class TqdmToLogger(io.StringIO):
23+
"""
24+
Output stream for TQDM which will output to logger module instead of
25+
the StdOut.
26+
"""
27+
logger = None
28+
level = None
29+
buf = ''
30+
def __init__(self,logger,level=None):
31+
super(TqdmToLogger, self).__init__()
32+
self.logger = logger
33+
self.level = level or logging.INFO
34+
def write(self,buf):
35+
self.buf = buf.strip('\r\n\t ')
36+
def flush(self):
37+
self.logger.log(self.level, self.buf)
38+
39+
1740
def formatter_message(message, use_color = True):
1841
if use_color:
1942
message = message.replace("$RESET", RESET_SEQ).replace("$BOLD", BOLD_SEQ)

parsnp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import shlex
1313
from tempfile import TemporaryDirectory
1414
import re
1515
import logging
16-
from logger import logger
16+
from logger import logger, TqdmToLogger, MIN_TQDM_INTERVAL
1717
import multiprocessing
1818
import argparse
1919
import signal
@@ -1650,7 +1650,11 @@ SETTINGS:
16501650
logger.info("Running partitions...")
16511651
good_chunks = set(chunk_labels)
16521652
with Pool(args.threads) as pool:
1653-
return_codes = tqdm(pool.imap(run_parsnp_aligner, chunk_output_dirs, chunksize=1), total=len(chunk_output_dirs))
1653+
return_codes = tqdm(
1654+
pool.imap(run_parsnp_aligner, chunk_output_dirs, chunksize=1),
1655+
total=len(chunk_output_dirs),
1656+
file=TqdmToLogger(logger,level=logging.INFO),
1657+
mininterval=MIN_TQDM_INTERVAL)
16541658
for cl, rc in zip(chunk_labels, return_codes):
16551659
if rc != 0:
16561660
logger.error(f"Partition {cl} failed...")

partition.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import copy
99
import math
10+
import logging
1011
from multiprocessing import Pool
1112
from functools import partial
1213
from collections import namedtuple, defaultdict, Counter
@@ -18,7 +19,7 @@
1819
from Bio.Seq import Seq
1920
from Bio.SeqRecord import SeqRecord
2021
from Bio.Align import MultipleSeqAlignment
21-
from logger import logger
22+
from logger import logger, TqdmToLogger, MIN_TQDM_INTERVAL
2223
from tqdm import tqdm
2324

2425

@@ -634,7 +635,11 @@ def trim_xmfas(
634635
trim_partial = partial(trim_single_xmfa, intersected_interval_dict=intersected_interval_dict)
635636
orig_xmfa_files = [f"{partition_output_dir}/{CHUNK_PREFIX}-{cl}-out/parsnp.xmfa" for cl in chunk_labels]
636637
with Pool(threads) as pool:
637-
num_clusters_per_xmfa = list(tqdm(pool.imap_unordered(trim_partial, orig_xmfa_files), total=len(orig_xmfa_files)))
638+
num_clusters_per_xmfa = list(tqdm(
639+
pool.imap_unordered(trim_partial, orig_xmfa_files),
640+
total=len(orig_xmfa_files),
641+
file=TqdmToLogger(logger,level=logging.INFO),
642+
mininterval=MIN_TQDM_INTERVAL))
638643
#TODO clean up
639644
if not all(num_clusters_per_xmfa[0][1] == nc for xmfa, nc in num_clusters_per_xmfa):
640645
logger.critical("One of the partitions has a different number of clusters after trimming...")
@@ -712,7 +717,9 @@ def merge_xmfas(
712717
with Pool(threads) as pool:
713718
tmp_xmfas = list(tqdm(
714719
pool.imap_unordered(merge_single_LCB_star_partial, enumerate(pairs_list)),
715-
total=num_clusters)
720+
total=num_clusters,
721+
file=TqdmToLogger(logger,level=logging.INFO),
722+
mininterval=MIN_TQDM_INTERVAL)
716723
)
717724

718725
with open(xmfa_out_f, 'a') as xmfa_out_handle:

0 commit comments

Comments
 (0)