11import io
2+ from collections .abc import Generator
3+ from typing import Self
24
3- from tola .assembly .fragment import Fragment
5+ from tola .assembly .fragment import Fragment , Junction
46from tola .assembly .gap import Gap
57
68
@@ -20,11 +22,11 @@ def __init__(
2022 self .rows = [* rows ]
2123 else :
2224 self .rows = []
23- self .tag = tag
24- self .haplotype = haplotype
25- self .rank = rank
26- self .original_name = original_name
27- self .original_tags = original_tags
25+ self .tag : str | None = tag
26+ self .haplotype : str | None = haplotype
27+ self .rank : int = rank
28+ self .original_name : str | None = original_name
29+ self .original_tags : set [ str ] | None = original_tags
2830
2931 def __repr__ (self ):
3032 txt = io .StringIO ()
@@ -59,54 +61,57 @@ def add_row(self, row):
5961 self .rows .append (row )
6062
6163 @property
62- def length (self ):
64+ def length (self ) -> int :
6365 return sum (r .length for r in self .rows )
6466
6567 @property
66- def fragments_length (self ):
68+ def fragments_length (self ) -> int :
6769 return sum (f .length for f in self .fragments ())
6870
6971 @property
70- def gaps_length (self ):
72+ def gaps_length (self ) -> int :
7173 return sum (g .length for g in self .gaps ())
7274
7375 @property
74- def last_row_is_fragment (self ):
76+ def last_row_is_fragment (self ) -> bool :
7577 if self .rows :
7678 return isinstance (self .rows [- 1 ], Fragment )
7779 else :
7880 return False
7981
80- def fragments (self ):
82+ def fragments (self ) -> Generator [ Fragment ] :
8183 for row in self .rows :
8284 if isinstance (row , Fragment ):
8385 yield row
8486
85- def idx_fragments (self ):
87+ def idx_fragments (self ) -> Generator [ tuple [ int , Fragment ]] :
8688 for i , row in enumerate (self .rows ):
8789 if isinstance (row , Fragment ):
8890 yield i , row
8991
90- def gaps (self ):
92+ def gaps (self ) -> Generator [ Fragment ] :
9193 for row in self .rows :
9294 if isinstance (row , Gap ):
9395 yield row
9496
95- def idx_gaps (self ):
97+ def idx_gaps (self ) -> Generator [ tuple [ int , Gap ]] :
9698 for i , row in enumerate (self .rows ):
9799 if isinstance (row , Gap ):
98100 yield i , row
99101
100- def fragment_tags (self ):
102+ def fragment_tags (self ) -> set [ str ] :
101103 tag_set = set ()
102104 for frag in self .fragments ():
103105 for t in frag .tags :
104106 tag_set .add (t )
105107 return tag_set
106108
107- def reverse (self ):
109+ def reverse (self ) -> Self :
108110 new = self .__class__ (
109111 self .name ,
112+ tag = self .tag ,
113+ haplotype = self .haplotype ,
114+ rank = self .rank ,
110115 original_name = self .original_name ,
111116 original_tags = self .original_tags ,
112117 )
@@ -121,7 +126,7 @@ def append_scaffold(self, othr, gap=None):
121126 self .add_row (gap )
122127 self .rows .extend (othr .rows )
123128
124- def fragment_junction_set (self ):
129+ def fragment_junction_set (self ) -> set [ Junction ] :
125130 junctions = set ()
126131 itr = self .fragments ()
127132
@@ -130,12 +135,8 @@ def fragment_junction_set(self):
130135 except StopIteration :
131136 return junctions
132137
133- while True :
134- try :
135- this = next (itr )
136- junctions .add (prev .junction_tuple (this ))
137- prev = this
138- except StopIteration :
139- break
138+ for this in itr :
139+ junctions .add (prev .junction_tuple (this ))
140+ prev = this
140141
141142 return junctions
0 commit comments