Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python3 – IndexError: string index out of range

import sys
from Bio import SeqIO

f = sys.argv[1]

seq_records = SeqIO.parse(f, 'fasta')
refseq_record = next(seq_records)

for seq_record in seq_records:
    for i in range(0, len(refseq_record)):
        nt1 = refseq_record[i]
        nt2 = seq_record[i]
        if nt1 != nt2:
            print (seq_record.id, i+1, nt2, nt1)

Hello,
I get IndexError: string index out of range error at line 12:
nt2 = seq_record[i]

Could you please help me with this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Force the min value length between the reference sequence and the current sequence

import sys
from Bio import SeqIO

f = sys.argv[1]

seq_records = SeqIO.parse(f, 'fasta')
refseq_record = next(seq_records)

for seq_record in seq_records:
    min_length = min(len(refseq_record), len(seq_record))
    for i in range(0, min_length):
        nt1 = refseq_record[i]
        nt2 = seq_record[i]
        if nt1 != nt2:
            print(seq_record.id, i + 1, nt2, nt1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading