Skip to content

Commit

Permalink
Contig stitcher: fix visualisation of non-overlapping contigs
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Jan 19, 2024
1 parent c4537ed commit f068e2f
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions micall/core/plot_contigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def plot_stitcher_coverage(logs, genome_coverage_svg_path):
def build_stitcher_figure(logs) -> None:
contig_map: Dict[str, Contig] = {}
name_mappings: Dict[str, str] = {}
parent_graph: Dict[str, List[str]] = defaultdict(list)
parent_graph: Dict[str, List[str]] = {}
morphism_graph: Dict[str, List[str]] = {}
reduced_parent_graph: Dict[str, List[str]] = {}
transitive_parent_graph: Dict[str, List[str]] = {}
Expand All @@ -420,6 +420,7 @@ def build_stitcher_figure(logs) -> None:
combine_right_edge: Dict[str, str] = {}
synthetic: Set[str] = set()
sinks: Dict[str, bool] = {}
returned: List[str] = []

def get_oldest_ancestors(recur, graph, ancestor_name):
if ancestor_name in recur:
Expand Down Expand Up @@ -503,6 +504,9 @@ def record_contig(contig: Contig, parents: List[Contig]):
if [contig.name] != [parent.name for parent in parents]:
for parent in parents:
contig_map[parent.name] = parent
if contig.name not in parent_graph:
parent_graph[contig.name] = []

parent_graph[contig.name].append(parent.name)

def record_morphism(contig: Contig, original: Contig):
Expand All @@ -512,13 +516,11 @@ def record_morphism(contig: Contig, original: Contig):
if contig.name not in lst:
lst.append(contig.name)

def unwrap_final(contig):
yield contig

for event in logs:
if not hasattr(event, "action"):
pass
elif event.action == "finalcombine":
for part in event.contigs: returned.append(part.name)
record_contig(event.result, event.contigs)
elif event.action == "splitgap":
record_contig(event.left, [event.contig])
Expand Down Expand Up @@ -626,6 +628,16 @@ def copy_takes_one_side(edge_table, overlap_xtake_map, overlap_xparent_map):
elif contig in discarded or contig in anomaly or contig in unknown:
final_parts[contig] = True

for contig in returned:
[contig] = reduced_morphism_graph.get(contig, [contig])

transitive_parent = transitive_parent_graph.get(contig, [])
if any(parent in transitive_parent for parent in final_parts):
continue

final_parts[contig] = True


final_parent_mapping: Dict[str, List[str]] = {}
for parent_name in sorted_roots:
children = []
Expand All @@ -645,12 +657,18 @@ def copy_takes_one_side(edge_table, overlap_xtake_map, overlap_xparent_map):
max_position = max(max_position, len(contig.seq) + 3 * position_offset)

Check warning on line 657 in micall/core/plot_contigs.py

View check run for this annotation

Codecov / codecov/patch

micall/core/plot_contigs.py#L657

Added line #L657 was not covered by tests

name_mappings = {}
def part_relative_position(name):
part = contig_map[name]
if isinstance(part, AlignedContig):
return part.alignment.q_st
else:
return -1

Check warning on line 665 in micall/core/plot_contigs.py

View check run for this annotation

Codecov / codecov/patch

micall/core/plot_contigs.py#L665

Added line #L665 was not covered by tests

for i, (parent, children) in enumerate(sorted(final_parent_mapping.items(), key=lambda p: p[0])):
name_mappings[parent] = f"{i + 1}"
mapped_children = [child for child in children]
for k, child in enumerate(mapped_children):
if len(mapped_children) > 1:
children = list(sorted(children, key=part_relative_position))
for k, child in enumerate(children):
if len(children) > 1:
name_mappings[child] = f"{i + 1}.{k + 1}"

Check warning on line 672 in micall/core/plot_contigs.py

View check run for this annotation

Codecov / codecov/patch

micall/core/plot_contigs.py#L672

Added line #L672 was not covered by tests
else:
name_mappings[child] = f"{i + 1}"
Expand Down

0 comments on commit f068e2f

Please sign in to comment.