File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55own faster implementation.
66"""
77
8- from . baseline import find_matches as _baseline
8+ from __future__ import annotations
99
1010
1111def find_matches (fasta_path : str , pattern : bytes ) -> list [tuple [str , list [int ]]]:
1212 """Find every FASTA record whose sequence contains ``pattern``.
1313
1414 Returns ``[(record_id, [positions...]), ...]`` in file order.
1515 """
16- # TODO: remove this delegation and write your own implementation here.
17- return _baseline (fasta_path , pattern )
16+ with open (fasta_path , "rb" ) as f :
17+ text = f .read ()
18+
19+ matches : list [tuple [str , list [int ]]] = []
20+
21+ for record in text .split (b">" ):
22+ if not record .strip ():
23+ continue
24+
25+ lines = record .split (b"\n " )
26+ record_id = lines [0 ].strip ().decode ("ascii" )
27+ sequence = b"" .join (lines [1 :]).replace (b" " , b"" )
28+
29+ positions : list [int ] = []
30+ start = 0
31+ while True :
32+ pos = sequence .find (pattern , start )
33+ if pos == - 1 :
34+ break
35+ positions .append (pos )
36+ start = pos + 1
37+
38+ if positions :
39+ matches .append ((record_id , positions ))
40+
41+ return matches
You can’t perform that action at this time.
0 commit comments