biopython pairwise2 result to a file
1
0
Entering edit mode
9.5 years ago
mbk0asis ▴ 700

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 • 5.4k views
ADD COMMENT
4
Entering edit mode
9.5 years ago
>>> from Bio.pairwise2 import format_alignment
>>> oh = open("out.txt", "w")
>>> for a in pairwise2.align.globalxx("ACCGT", "ACG"):
...     oh.write(format_alignment(*a))
ADD COMMENT
0
Entering edit mode

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!

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

It's Python 2.7.6 on Ubuntu 14.04.

Thanks!

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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
ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1533 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6