Skip to content

Commit

Permalink
Contig stitcher: remove all logging.info calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Jan 24, 2024
1 parent 6bd0bfe commit 3419b9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 84 deletions.
44 changes: 17 additions & 27 deletions micall/core/contig_stitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def align_to_reference(contig: GenotypedContig) -> Iterable[GenotypedContig]:
"""

if contig.ref_seq is None:
logger.info("Contig %r not aligned - no reference.", contig.name)
logger.debug("Contig %r not aligned - no reference.", contig.name)
context.get().emit(events.NoRef(contig))
yield contig
return
Expand All @@ -234,37 +234,33 @@ def align_to_reference(contig: GenotypedContig) -> Iterable[GenotypedContig]:
connected = connect_cigar_hits(list(map(lambda p: p[0], hits_array))) if hits_array else []

if not connected:
logger.info("Contig %r not aligned - backend's choice.", contig.name)
logger.debug("Contig %r not aligned - backend's choice.", contig.name)
context.get().emit(events.ZeroHits(contig))
yield contig
return

if len(set(map(lambda p: p[1], hits_array))) > 1:
logger.info("Discarding contig %r because it aligned both in forward and reverse sense.", contig.name)
logger.debug("Discarding contig %r because it aligned both in forward and reverse sense.", contig.name)
context.get().emit(events.StrandConflict(contig))
yield contig
return

Check warning on line 246 in micall/core/contig_stitcher.py

View check run for this annotation

Codecov / codecov/patch

micall/core/contig_stitcher.py#L243-L246

Added lines #L243 - L246 were not covered by tests

logger.info("Contig %r produced %s aligner hits. After connecting them, the number became %s.",
contig.name, len(hits_array), len(connected))
logger.debug("Contig %r produced %s aligner hits. After connecting them, the number became %s.",
contig.name, len(hits_array), len(connected))
context.get().emit(events.HitNumber(contig, hits_array, connected))

strand = hits_array[0][1]
if strand == "reverse":
rc = str(Seq(contig.seq).reverse_complement())
new_contig = replace(contig, seq=rc)
logger.info("Reverse complemented contig %r.", contig.name)
logger.debug("Reverse complemented contig %r.", contig.name)
context.get().emit(events.ReverseComplement(contig, new_contig))
contig = new_contig

Check warning on line 258 in micall/core/contig_stitcher.py

View check run for this annotation

Codecov / codecov/patch

micall/core/contig_stitcher.py#L254-L258

Added lines #L254 - L258 were not covered by tests

for i, single_hit in enumerate(connected):
query = replace(contig, name=context.get().generate_new_name())
part = AlignedContig.make(query, single_hit, strand)

logger.info("Part %r of contig %r aligned as %r at [%s, %s]->[%s, %s]%s.",
i, contig.name,part.name,part.alignment.q_st,
part.alignment.q_ei,part.alignment.r_st,part.alignment.r_ei,
" (rev)" if strand == "reverse" else "")
logger.debug("Part %r of contig %r aligned as %r at %s%s.", i, contig.name,
part.name,part.alignment, " (rev)" if strand == "reverse" else "")
context.get().emit(events.Hit(contig, part, i))
Expand Down Expand Up @@ -483,7 +479,7 @@ def combine_overlaps(contigs: List[AlignedContig]) -> Iterable[AlignedContig]:
# Find overlap. If there isn't one - we are done with the current contig.
overlapping_contig = find_overlapping_contig(current, contigs)
if not overlapping_contig:
logger.info("Nothing overlaps with %r.", current.name)
logger.debug("Nothing overlaps with %r.", current.name)
context.get().emit(events.NoOverlap(current))
yield current
continue
Expand All @@ -493,10 +489,6 @@ def combine_overlaps(contigs: List[AlignedContig]) -> Iterable[AlignedContig]:
contigs.remove(overlapping_contig)
contigs.insert(0, new_contig)

logger.info("Stitching %r with %r results in %r at [%s,%s]->[%s,%s].",
current.name, overlapping_contig.name,
new_contig.name, new_contig.alignment.q_st, new_contig.alignment.q_ei,
new_contig.alignment.r_st, new_contig.alignment.r_ei)
logger.debug("Stitching %r with %r results in %r at %s (len %s).",
current.name, overlapping_contig.name,
new_contig.name, new_contig.alignment, len(new_contig.seq))
Expand Down Expand Up @@ -568,8 +560,8 @@ def drop_completely_covered(contigs: List[AlignedContig]) -> List[AlignedContig]
covered, covering = find_covered_contig(contigs)
if covered:
contigs.remove(covered)
logger.info("Droped contig %r as it is completely covered by these contigs: %s.",
covered.name, ", ".join(repr(x.name) for x in covering))
logger.debug("Droped contig %r as it is completely covered by these contigs: %s.",
covered.name, ", ".join(repr(x.name) for x in covering))
context.get().emit(events.Drop(covered, covering))
else:
break

Check warning on line 567 in micall/core/contig_stitcher.py

View check run for this annotation

Codecov / codecov/patch

micall/core/contig_stitcher.py#L567

Added line #L567 was not covered by tests
Expand Down Expand Up @@ -620,14 +612,14 @@ def try_split(contig):
contigs.append(right_part)
process_queue.put(right_part)

logger.info("Split contig %r around its gap at [%s, %s]->[%s, %s]. "
"Left part: %r at [%s, %s]->[%s, %s], "
"right part: %r at [%s, %s]->[%s, %s].",
contig.name, gap.q_st, gap.q_ei, gap.r_st, gap.r_ei,
left_part.name, left_part.alignment.q_st, left_part.alignment.q_ei,
left_part.alignment.r_st, left_part.alignment.r_ei,
right_part.name, right_part.alignment.q_st, right_part.alignment.q_ei,
right_part.alignment.r_st, right_part.alignment.r_ei)
logger.debug("Split contig %r around its gap at [%s, %s]->[%s, %s]. "
"Left part: %r at [%s, %s]->[%s, %s], "
"right part: %r at [%s, %s]->[%s, %s].",
contig.name, gap.q_st, gap.q_ei, gap.r_st, gap.r_ei,
left_part.name, left_part.alignment.q_st, left_part.alignment.q_ei,
left_part.alignment.r_st, left_part.alignment.r_ei,
right_part.name, right_part.alignment.q_st, right_part.alignment.q_ei,
right_part.alignment.r_st, right_part.alignment.r_ei)
context.get().emit(events.SplitGap(contig, gap, left_part, right_part))
return

Expand All @@ -644,8 +636,6 @@ def try_split(contig):
def stitch_contigs(contigs: Iterable[GenotypedContig]) -> Iterable[GenotypedContig]:
contigs = list(contigs)
for contig in contigs:
logger.info("Introduced contig %r of ref %r, group_ref %r, and length %s.",
contig.name, contig.ref_name, contig.group_ref, len(contig.seq))
logger.debug("Introduced contig %r (seq = %s) of ref %r, group_ref %r (seq = %s), and length %s.",
contig.name, contig.seq, contig.ref_name,
contig.group_ref, contig.ref_seq, len(contig.seq))
Expand Down
57 changes: 0 additions & 57 deletions micall/tests/test_contig_stitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,63 +688,6 @@ def test_partial_align_consensus_multiple_overlaping_sequences(exact_aligner, vi
assert len(visualizer().elements) > len(contigs)


def test_correct_processing_complex_logs(exact_aligner, visualizer):
# Scenario: There are two reference organisms.
# Each with 4 contigs.
# For each, three overlapping contigs are stitched together, the non-overlapping is kept separate.
# Tested before, but this time we check the logs

ref_seq = 'A' * 100 + 'C' * 100 + 'T' * 100 + 'G' * 100

contigs = [[
GenotypedContig(name='a',
seq='A' * 50 + 'C' * 20,
ref_name=ref_name,
group_ref=ref_name,
ref_seq=ref_seq,
match_fraction=0.5,
),
GenotypedContig(name='b',
seq='A' * 20 + 'C' * 50,
ref_name=ref_name,
group_ref=ref_name,
ref_seq=ref_seq,
match_fraction=0.5,
),
GenotypedContig(name='c',
seq='C' * 70 + 'T' * 20,
ref_name=ref_name,
group_ref=ref_name,
ref_seq=ref_seq,
match_fraction=0.5,
),
GenotypedContig(name='d',
seq='T' * 20 + 'G' * 50,
ref_name=ref_name,
group_ref=ref_name,
ref_seq=ref_seq,
match_fraction=0.5,
),
] for ref_name in ['testref-1', 'testref-2']]

contigs = sum(contigs, start=[])

logger = logging.getLogger("micall.core.contig_stitcher")
logger.setLevel(logging.DEBUG)
handler = add_structured_handler(logger)

assert len(handler.logs) == 0
list(stitch_consensus(contigs))
assert len(handler.logs) == 150

info_messages = [m for m in handler.logs if m.levelname == 'INFO']
debug_messages = [m for m in handler.logs if m.levelname == 'DEBUG']
assert len(info_messages) == 32
assert len(debug_messages) == len(handler.logs) - len(info_messages)

assert len(visualizer().elements) > len(contigs)


def test_main_invocation(exact_aligner, tmp_path, hcv_db):
pwd = os.path.dirname(__file__)
contigs = os.path.join(pwd, "data", "exact_parts_contigs.csv")
Expand Down

0 comments on commit 3419b9b

Please sign in to comment.