Question: Need help with Pairwise Alignment module in iterating over the alignment
0
gravatar for naman.misanthropist
3 months ago by
naman.misanthropist0 wrote:
from Bio import SeqIO
from Bio import Align

ref_seq_1 = SeqIO.read('C:/Users/King_Arthur/Desktop/ref_seq/segment 1/ref_seq_8.fasta','fasta')

seq1 = SeqIO.read('C:/Users/King_Arthur/Desktop/file/segment 1/Myfile_1 (1).fasta','fasta')

aligner = Align.PairwiseAligner()
aligner.mode = 'global'
aligner.match_score = 1
aligner.mismatch_score = -2
aligner.gap_score = -2

alignments = aligner.score(ref_seq_1.seq , seq1.seq)

print(alignments)
for alignment in sorted(alignments):
    print(alignment)

So this is my code and as you can see in the last section i am trying to iterate over my alignment but I am getting this error

TypeError: 'float' object is not iterable

I have tried various things like using str() but it gives some strange values and I also tried to read the source code by using the inspect module but I can't figure out the problem.

Any help would be really appreciated.

My final objective is to find out how many matches, mismatches and gaps are present in the final alignment using biopython.

if there is any other better way to do it in python please feel free to suggest.

ADD COMMENTlink modified 3 months ago by a.zielezinski9.6k • written 3 months ago by naman.misanthropist0

Replace alignments = aligner.score(ref_seq_1.seq, seq1.seq) with alignments = aligner.align(ref_seq_1.seq, seq1.seq).

ADD REPLYlink written 3 months ago by a.zielezinski9.6k

Thank you that worked, I know it was a silly mistake. Do you have anything for this- My final objective is to find out how many matches, mismatches and gaps are present in the final alignment using biopython.

if there is any other better way to do it in python please feel free to suggest.

ADD REPLYlink written 3 months ago by naman.misanthropist0
3
gravatar for a.zielezinski
3 months ago by
a.zielezinski9.6k
a.zielezinski9.6k wrote:

Here's my suggestion:

from Bio import SeqIO
from Bio import Align

ref_seq_1 = SeqIO.read('seq1.fasta','fasta')

seq1 = SeqIO.read('seq2.fasta','fasta')

aligner = Align.PairwiseAligner()
aligner.mode = 'global'
aligner.match_score = 1
aligner.mismatch_score = -2
aligner.gap_score = -2

alignments = aligner.align(ref_seq_1.seq , seq1.seq)
alignment = alignments[0]
string = alignment._format_psl()
lst = string.split('\t')
matches = int(lst[0])
mismatches = int(lst[1])
qgaps = int(lst[5])
tgaps = int(lst[7])


print('Alignment score: {}'.format(alignment.score))
print('Matches        : {}'.format(matches))
print('Mismatches     : {}'.format(mismatches))
print('Gaps           : {}'.format(qgaps + tgaps))
ADD COMMENTlink written 3 months ago by a.zielezinski9.6k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 954 users visited in the last hour
_