Skip to content

Commit d4438c9

Browse files
Create error_debugging
1 parent b4a7d4e commit d4438c9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
The provided script has one main issue: it doesn't correctly handle the last sequence in the FASTA file. Here's the corrected version:
2+
3+
import csv
4+
5+
def read_fasta(file_path):
6+
sequences = {}
7+
with open(file_path, 'r') as file:
8+
sequence_id = None
9+
sequence = ''
10+
for line in file:
11+
if line.startswith('>'):
12+
if sequence_id:
13+
sequences[sequence_id] = sequence
14+
sequence_id = line[1:].strip()
15+
sequence = ''
16+
else:
17+
sequence += line.strip()
18+
# Add the last sequence
19+
if sequence_id: # Important check in case the file is empty
20+
sequences[sequence_id] = sequence
21+
return sequences
22+
23+
def main():
24+
file_path = 'sequences.fasta'
25+
output_file = 'sequence_lengths.csv'
26+
27+
sequences = read_fasta(file_path)
28+
29+
with open(output_file, 'w', newline='') as file:
30+
writer = csv.writer(file)
31+
writer.writerow(['Sequence ID', 'Sequence Length'])
32+
for seq_id, sequence in sequences.items():
33+
length = len(sequence)
34+
writer.writerow([seq_id, length])
35+
36+
if __name__ == '__main__':
37+
main()
38+
The problem was in the read_fasta function. After reading the last line of a sequence, the code needs to add that sequence to the sequences dictionary. The original code only added a sequence when it encountered a new sequence header (a line starting with '>'). The solution adds a check after the loop to ensure the last sequence is added. The if sequence_id: condition handles cases where the input FASTA file might be empty, preventing potential errors.

0 commit comments

Comments
 (0)