@@ -402,12 +402,12 @@ def plot_stitcher_coverage(logs, genome_coverage_svg_path):
402
402
403
403
404
404
from types import SimpleNamespace
405
- from typing import Union , Dict , Tuple , List , Optional , Set
405
+ from typing import Union , Dict , Tuple , List , Optional , Set , Iterable
406
406
from micall .core .contig_stitcher import Contig , GenotypedContig , AlignedContig
407
407
import random
408
408
409
- def build_stitcher_figure (logs ) -> None :
410
- contig_map : Dict [str , Contig ] = {}
409
+ def build_stitcher_figure (logs : Iterable [ events . EventType ] ) -> None :
410
+ contig_map : Dict [str , GenotypedContig ] = {}
411
411
name_mappings : Dict [str , str ] = {}
412
412
parent_graph : Dict [str , List [str ]] = {}
413
413
morphism_graph : Dict [str , List [str ]] = {}
@@ -515,7 +515,7 @@ def graph_sum(graph_a, graph_b):
515
515
def symmetric_closure (graph ):
516
516
return graph_sum (graph , inverse_graph (graph ))
517
517
518
- def record_contig (contig : Contig , parents : List [Contig ]):
518
+ def record_contig (contig : GenotypedContig , parents : List [GenotypedContig ]):
519
519
contig_map [contig .name ] = contig
520
520
if [contig .name ] != [parent .name for parent in parents ]:
521
521
for parent in parents :
@@ -532,7 +532,7 @@ def record_morphism(contig: Contig, original: Contig):
532
532
if contig .name not in lst :
533
533
lst .append (contig .name )
534
534
535
- def record_bad_contig (contig : Contig , lst : List [Contig ]):
535
+ def record_bad_contig (contig : GenotypedContig , lst : List [str ]):
536
536
contig_map [contig .name ] = contig
537
537
lst .append (contig .name )
538
538
@@ -610,23 +610,23 @@ def record_bad_contig(contig: Contig, lst: List[Contig]):
610
610
eqv_morphism_graph = reflexive_closure (symmetric_closure (transitive_closure (morphism_graph )))
611
611
reduced_morphism_graph = reduced_closure (morphism_graph )
612
612
613
- for contig in overlaps_list :
614
- temporary .add (contig )
615
- for child in transitive_children_graph .get (contig , []):
613
+ for contig_name in overlaps_list :
614
+ temporary .add (contig_name )
615
+ for child in transitive_children_graph .get (contig_name , []):
616
616
temporary .add (child )
617
617
618
- for contig , parents in parent_graph .items ():
618
+ for contig_name , parents in parent_graph .items ():
619
619
if len (parents ) > 2 :
620
- children_join_points .append (contig )
621
- for contig , children in children_graph .items ():
620
+ children_join_points .append (contig_name )
621
+ for contig_name , children in children_graph .items ():
622
622
if len (children ) > 2 :
623
- children_meet_points .append (contig )
623
+ children_meet_points .append (contig_name )
624
624
625
- last_join_points_parent = {contig for join in children_join_points for contig in transitive_parent_graph .get (join , [])}
625
+ last_join_points_parent = {contig_name for join in children_join_points for contig_name in transitive_parent_graph .get (join , [])}
626
626
last_join_points = []
627
- for contig in children_join_points :
628
- if contig not in last_join_points_parent :
629
- last_join_points .append (contig )
627
+ for contig_name in children_join_points :
628
+ if contig_name not in last_join_points_parent :
629
+ last_join_points .append (contig_name )
630
630
631
631
def set_query_position (contig : Contig ):
632
632
if contig .name in query_position_map :
@@ -644,7 +644,7 @@ def set_query_position(contig: Contig):
644
644
if parent .name not in query_position_map :
645
645
set_query_position (parent )
646
646
647
- average = sum (query_position_map [parent_name ] for parent_name in parent_names ) / len (parent_names )
647
+ average = round ( sum (query_position_map [parent_name ] for parent_name in parent_names ) / len (parent_names ) )
648
648
query_position_map [contig .name ] = average
649
649
else :
650
650
query_position_map [contig .name ] = (contig .alignment .q_st + contig .alignment .q_ei ) // 2
@@ -653,9 +653,9 @@ def set_query_position(contig: Contig):
653
653
set_query_position (contig )
654
654
655
655
# Closing `temporary'
656
- for contig in contig_map :
657
- if contig in temporary :
658
- for clone in eqv_morphism_graph .get (contig , [contig ]):
656
+ for contig_name in contig_map :
657
+ if contig_name in temporary :
658
+ for clone in eqv_morphism_graph .get (contig_name , [contig_name ]):
659
659
temporary .add (clone )
660
660
661
661
def copy_takes_one_side (edge_table , overlap_xtake_map , overlap_xparent_map ):
@@ -676,42 +676,42 @@ def copy_takes_one_side(edge_table, overlap_xtake_map, overlap_xparent_map):
676
676
while list (copy_takes_one_side (combine_left_edge , overlap_righttake_map , overlap_rightparent_map )): pass
677
677
678
678
final_parts : Dict [str , bool ] = {}
679
- for contig in contig_map :
680
- if contig in temporary :
679
+ for contig_name in contig_map :
680
+ if contig_name in temporary :
681
681
continue
682
682
683
- if contig in overlap_sibling_map :
684
- finals = reduced_morphism_graph .get (contig , [contig ])
683
+ if contig_name in overlap_sibling_map :
684
+ finals = reduced_morphism_graph .get (contig_name , [contig_name ])
685
685
if len (finals ) == 1 :
686
686
[final ] = finals
687
687
parents = reduced_parent_graph .get (final , [])
688
688
if len (parents ) == 1 :
689
689
final_parts [final ] = True
690
690
691
- elif contig in bad_contigs :
692
- final_parts [contig ] = True
691
+ elif contig_name in bad_contigs :
692
+ final_parts [contig_name ] = True
693
693
694
694
for join in last_join_points + sorted_sinks :
695
695
parents = parent_graph .get (join , [join ])
696
696
if not any (isinstance (contig_map [parent ], AlignedContig ) for parent in parents ):
697
697
parents = [join ]
698
698
699
- for contig in parents :
700
- for contig in reduced_morphism_graph .get (contig , [contig ]):
701
- if contig in bad_contigs :
699
+ for contig_name in parents :
700
+ for contig_name in reduced_morphism_graph .get (contig_name , [contig_name ]):
701
+ if contig_name in bad_contigs :
702
702
continue
703
703
704
- if any (contig in transitive_parent_graph .get (bad , []) for bad in bad_contigs ):
704
+ if any (contig_name in transitive_parent_graph .get (bad , []) for bad in bad_contigs ):
705
705
continue
706
706
707
- if any (eqv in temporary for eqv in eqv_morphism_graph .get (contig , [contig ])):
707
+ if any (eqv in temporary for eqv in eqv_morphism_graph .get (contig_name , [contig_name ])):
708
708
continue
709
709
710
- transitive_parent = eqv_parent_graph .get (contig , [contig ])
710
+ transitive_parent = eqv_parent_graph .get (contig_name , [contig_name ])
711
711
if any (parent in transitive_parent for parent in final_parts ):
712
712
continue
713
713
714
- final_parts [contig ] = True
714
+ final_parts [contig_name ] = True
715
715
716
716
final_parent_mapping : Dict [str , List [str ]] = {}
717
717
for parent_name in sorted_roots :
@@ -725,7 +725,7 @@ def copy_takes_one_side(edge_table, overlap_xtake_map, overlap_xparent_map):
725
725
726
726
min_position , max_position = 1 , 1
727
727
position_offset = 100
728
- for contig in contig_map .values ():
728
+ for _ , contig in contig_map .items ():
729
729
if isinstance (contig , GenotypedContig ) and contig .ref_seq is not None :
730
730
max_position = max (max_position , len (contig .ref_seq ) + 3 * position_offset )
731
731
else :
@@ -748,8 +748,8 @@ def copy_takes_one_side(edge_table, overlap_xtake_map, overlap_xparent_map):
748
748
k += 1
749
749
name_mappings [child ] = f"{ i + 1 } .{ k + 1 } "
750
750
751
- for contig , name in name_mappings .items ():
752
- logger .debug (f"Contig name { contig !r} is displayed as { name !r} ." )
751
+ for contig_name , name in name_mappings .items ():
752
+ logger .debug (f"Contig name { contig_name !r} is displayed as { name !r} ." )
753
753
754
754
def get_neighbours (part , lookup ):
755
755
for clone in eqv_morphism_graph .get (part .name , [part .name ]):
@@ -771,8 +771,8 @@ def get_neighbour(part, lookup):
771
771
full_size_map : Dict [str , Tuple [int , int ]] = {}
772
772
773
773
for parent_name in sorted_roots :
774
- parts = final_parent_mapping [parent_name ]
775
- parts = [contig_map [part ] for part in parts ]
774
+ parts_names = final_parent_mapping [parent_name ]
775
+ parts = [contig_map [part ] for part in parts_names ]
776
776
777
777
for part in parts :
778
778
if not isinstance (part , AlignedContig ):
@@ -801,8 +801,8 @@ def get_neighbour(part, lookup):
801
801
802
802
aligned_size_map [part .name ] = (r_st , r_ei )
803
803
804
- sibling = ([overlap_sibling_map [name ] for name in eqv_morphism_graph .get (part .name , [part .name ]) if name in overlap_sibling_map ] or [None ])[0 ]
805
- sibling = sibling and contig_map [sibling ]
804
+ sibling_name = ([overlap_sibling_map [name ] for name in eqv_morphism_graph .get (part .name , [part .name ]) if name in overlap_sibling_map ] or ["" ])[0 ]
805
+ sibling = sibling_name and contig_map [sibling_name ]
806
806
prev_part = get_neighbour (sibling , overlap_lefttake_map )
807
807
next_part = get_neighbour (sibling , overlap_righttake_map )
808
808
@@ -820,7 +820,7 @@ def get_neighbour(part, lookup):
820
820
821
821
full_size_map [part .name ] = (r_st , r_ei )
822
822
823
- def get_contig_coordinates (contig ) :
823
+ def get_contig_coordinates (contig : GenotypedContig ) -> Tuple [ int , int , int , int ] :
824
824
if isinstance (contig , AlignedContig ):
825
825
r_st = position_offset + contig .alignment .r_st
826
826
r_ei = position_offset + contig .alignment .r_ei
@@ -841,7 +841,7 @@ def get_contig_coordinates(contig):
841
841
a_r_ei = f_r_ei
842
842
return (a_r_st , a_r_ei , f_r_st , f_r_ei )
843
843
844
- def get_tracks (repeatset , group_ref , contig_name ) :
844
+ def get_tracks (repeatset : Set [ str ] , group_ref : str , contig_name : str ) -> Iterable [ Track ] :
845
845
parts = final_parent_mapping [contig_name ]
846
846
for part_name in parts :
847
847
part = contig_map [part_name ]
@@ -863,7 +863,7 @@ def get_tracks(repeatset, group_ref, contig_name):
863
863
(a_r_st , a_r_ei , f_r_st , f_r_ei ) = get_contig_coordinates (part )
864
864
yield Track (f_r_st , f_r_ei , label = f"{ indexes } " )
865
865
866
- def get_arrows (repeatset , group_ref , contig_name , labels ) :
866
+ def get_arrows (repeatset : Set [ str ] , group_ref : str , contig_name : str , labels : bool ) -> Iterable [ Arrow ] :
867
867
parts = final_parent_mapping [contig_name ]
868
868
for part_name in parts :
869
869
part = contig_map [part_name ]
@@ -890,8 +890,8 @@ def get_arrows(repeatset, group_ref, contig_name, labels):
890
890
h = height ,
891
891
label = indexes )
892
892
893
- def get_all_arrows (group_ref , labels ) :
894
- repeatset = set ()
893
+ def get_all_arrows (group_ref : str , labels : bool ) -> Iterable [ Arrow ] :
894
+ repeatset : Set [ str ] = set ()
895
895
for parent_name in sorted_roots :
896
896
yield from get_arrows (repeatset , group_ref , parent_name , labels )
897
897
@@ -966,8 +966,8 @@ def get_all_arrows(group_ref, labels):
966
966
# Contigs #
967
967
###########
968
968
969
- repeatset1 = set ()
970
- repeatset2 = set ()
969
+ repeatset1 : Set [ str ] = set ()
970
+ repeatset2 : Set [ str ] = set ()
971
971
for parent_name in sorted_roots :
972
972
arrows = list (get_arrows (repeatset1 , group_ref , parent_name , labels = False ))
973
973
if arrows :
@@ -992,7 +992,7 @@ def get_all_arrows(group_ref, labels):
992
992
993
993
contig = contig_map [contig_name ]
994
994
(r_st , r_ei , f_r_st , f_r_ei ) = get_contig_coordinates (contig )
995
- name = name_mappings .get (contig . name , contig . name )
995
+ name = name_mappings .get (contig_name , contig_name )
996
996
figure .add (Arrow (r_st , r_ei , elevation = - 20 , h = 1 ))
997
997
figure .add (Track (f_r_st , f_r_ei , label = name ))
998
998
@@ -1021,7 +1021,7 @@ def get_all_arrows(group_ref, labels):
1021
1021
else :
1022
1022
colour = "red"
1023
1023
1024
- name = name_mappings .get (contig . name , contig . name )
1024
+ name = name_mappings .get (contig_name , contig_name )
1025
1025
figure .add (Track (a_r_st , a_r_ei , color = colour , label = name ))
1026
1026
1027
1027
###########
@@ -1042,7 +1042,7 @@ def get_all_arrows(group_ref, labels):
1042
1042
r_st = position_offset
1043
1043
r_ei = position_offset + len (contig .seq )
1044
1044
colour = "red"
1045
- name = name_mappings .get (contig . name , contig . name )
1045
+ name = name_mappings .get (contig_name , contig_name )
1046
1046
figure .add (Track (r_st , r_ei , color = colour , label = name ))
1047
1047
1048
1048
if not figure .elements :
0 commit comments