Question: biopython pairwise2 result to a file
0
gravatar for mbk0asis
2.2 years ago by
mbk0asis220
Korea, Republic Of
mbk0asis220 wrote:

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 • 949 views
ADD COMMENTlink modified 2.2 years ago • written 2.2 years ago by mbk0asis220
4
gravatar for a.zielezinski
2.2 years ago by
a.zielezinski7.0k
a.zielezinski7.0k wrote:
>>> 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 COMMENTlink written 2.2 years ago by a.zielezinski7.0k

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 REPLYlink modified 2.2 years ago • written 2.2 years ago by mbk0asis220

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 REPLYlink written 2.2 years ago by Peter5.5k

It's Python 2.7.6 on Ubuntu 14.04.

Thanks!

ADD REPLYlink written 2.2 years ago by mbk0asis220

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

ADD REPLYlink written 2.2 years ago by mbk0asis220
0
gravatar for mbk0asis
2.2 years ago by
mbk0asis220
Korea, Republic Of
mbk0asis220 wrote:

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 COMMENTlink modified 2.2 years ago • written 2.2 years ago by mbk0asis220

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

ADD REPLYlink written 2.2 years ago by Peter5.5k

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

ADD REPLYlink written 2.2 years ago by mbk0asis220
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: 807 users visited in the last hour