This is a test version of Biostars. For the public version, visit https://www.biostars.org.
biopython pairwise2 result to a file

Hello!

Would somebody tell me how to save "pairwise2" result to a file instead of being printed on screen.

>>> from Bio.pairwise2 import format_alignment
>>> for a in pairwise2.align.globalxx("ACCGT", "ACG"):
...     print(format_alignment(*a))

Thank you!

biopython pairwise2

1 answer

>>> from Bio.pairwise2 import format_alignment
>>> oh = open("out.txt", "w")
>>> for a in pairwise2.align.globalxx("ACCGT", "ACG"):
...     oh.write(format_alignment(*a))

Thank you!

I'm not sure it was your code or not, but I took a code from this site and your answer and put them together.

Here's the code:

from itertools import product
from Bio import SeqIO
from Bio import pairwise2
from Bio.pairwise2 import format_alignment

seqs1 = SeqIO.to_dict(SeqIO.parse(open('/home/bio1/Desktop/seq.fa'),'fasta'))
seqs2 = SeqIO.to_dict(SeqIO.parse(open('/home/bio1/Desktop/primer2.fa'),'fasta'))

for sr1, sr2 in product(seqs1,seqs2):
    oh = open("/home/bio1/Desktop/result.txt", "w")
    for a in pairwise2.align.localxx(str(seqs1[sr1].seq), str(seqs2[sr2].seq)):
           oh.write(format_alignment(*a))

and Result:

<generator object parse at 0x7f6249c4e-e10>
|||||||||||||||||||||||||||||||||||||||||||
<generator object parse at 0x7f6249c4ed7-0>
  Score=40
<generator object parse at 0x7f6249c4ee1-0>
|||||||||||||||||||||||||||||||||||||||||||
<generator object parse at 0x7f6249c4e-d70>
  Score=40
<generator object parse at 0x7f6249c4ee1-0>
|||||||||||||||||||||||||||||||||||||||||||
<generator object parse at 0x7f6249c4-ed70>
  Score=40
<generator object parse at 0x7f6249c4ee-10>
|||||||||||||||||||||||||||||||||||||||||||
<generator object parse at 0x7f6249c4-ed70>
  Score=40
<generator object parse at 0x7f6249c4ee10>
||||||||||||||||||||||||||||||||||||||||||
<generator object parse at 0x7f6249c4ed70>
  Score=40

Instead of DNA sequences, something else were aligned.

Would you please look into it?

Thank you, again!

Not sure what you've done wrong here (which version of Python was it?), but you should open the file outside both the for loops. As it is, it will restart a new output file for each pair of sequences, overwriting the alignments from past pairs of sequences.

It's Python 2.7.6 on Ubuntu 14.04.

Thanks!

Oops! I saved the files as plain txt files. After I added '>seq name' at the top, it worked. Thank you, Peter!

Another problem occurred!

'local' alignment gave me 'global' alignment results.

seq.fa and primer.fa are

> seq
CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG

>primer
CCTCAACCTTCCAG

Modified code is

from itertools import product
from Bio import SeqIO
from Bio import pairwise2
from Bio.pairwise2 import format_alignment

seqs1 = SeqIO.to_dict(SeqIO.parse(open('./seq.fa'),'fasta'))
seqs2 = SeqIO.to_dict(SeqIO.parse(open('./primer.fa'),'fasta'))

result = open("./result.txt","w")
for sr1, sr2 in product(seqs1,seqs2):
    for a in pairwise2.align.localxx(str(seqs1[sr1].seq), str(seqs2[sr2].seq)):
           result.write(format_alignment(*a))

and results are

CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG
||||||||||||||||||||||||||||||||||||||||||||||||||
CCTC-A------A----C----C-T--T-------------C-C--A--G
  Score=14
CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG
||||||||||||||||||||||||||||||||||||||||||||||||||
CCTCA-------A----C----C-T--T-------------C-C--A--G
  Score=14
CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG
||||||||||||||||||||||||||||||||||||||||||||||||||
CCTCAA-----------C----C-T--T-------------C-C--A--G
  Score=14
CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG
||||||||||||||||||||||||||||||||||||||||||||||||||
CCTC-A------A--C------C-T--T-------------C-C--A--G
  Score=14
CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG
||||||||||||||||||||||||||||||||||||||||||||||||||
CCTCA-------A--C------C-T--T-------------C-C--A--G
  Score=14
CCTCAACCTTCCAGGCTCGAGACATCCTCCCACCCCAGCCTCCCTAATAG
||||||||||||||||||||||||||||||||||||||||||||||||||
CCTCAA---------C------C-T--T-------------C-C--A--G
  Score=14

This is not an answer to the original question - it's a revised question, or even a new question.

OK. I will ask again in a new post. Thanks.

Log in to answer this question.