[Biopython] convert to interleaved nexus

natassa natassa_g_2000 at yahoo.com
Tue Mar 26 19:43:42 UTC 2013


Thanks Zheng, 
Both your suggestions worked fine. It is weird though that the script works only if I want to convert a phylip-relaxed sequential file to a nexus 
interleaved, while when I try the same on a phylip interleaved (that I got 
from the phylip-relaxed using the same function but going through the 
first if block ) , then I get this error message (which i suspect has to 
do with line wrapping? )
This is my function:

def Convert_alnformat(alnfile, out, informat, outformat): 

    alignment=AlignIO.read(alnfile, informat,  alphabet=Gapped(IUPAC.unambiguous_dna)) 
    if outformat !='nexus':
        SeqIO.write(alignment,out, outformat)
    else:
        #output=StringIO()
        #AlignIO.write(alignment, output, 'nexus')
        #p = Nexus.Nexus()
        #p.read(output.getvalue())
        #p.write_nexus_data(out, interleave=True)
      
        minimal_record = "#NEXUS\nbegin data; dimensions ntax=0 nchar=0; format datatype=%s; end;" % "dna"
        n = Nexus.Nexus(minimal_record)
        n.alphabet = alignment._alphabet
        for record in alignment:
            n.add_sequence(record.id, record.seq.tostring())
            n.write_nexus_data(out, interleave=True)


and the error message


File "/bigdata/agioti/scripts/Align_manips.py", line 261, in <module>
    Convert_alnformat(alignment, outaln, "phylip", "nexus")
  File "/bigdata/agioti/scripts/Align_manips.py", line 48, in Convert_alnformat
    alignment=AlignIO.read(alnfile, informat,  alphabet=Gapped(IUPAC.unambiguous_dna)) ##hardcoded 
  File "/opt/Python/2.7.3/lib/python2.7/site-packages/Bio/AlignIO/__init__.py", line 418, in read
    first = iterator.next()
  File "/opt/Python/2.7.3/lib/python2.7/site-packages/Bio/AlignIO/__init__.py", line 366, in parse
    for a in i:
  File "/opt/Python/2.7.3/lib/python2.7/site-packages/Bio/AlignIO/PhylipIO.py", line 257, in next
    raise ValueError("End of file mid-block")
ValueError: End of file mid-block


Anyways, problem solved, I was just curious. ...
Thanks again, 

Natassa


________________________________
 From: 阮铮 <rz1991 at foxmail.com>
To: natassa <natassa_g_2000 at yahoo.com> 
Cc: Biopython <Biopython at lists.open-bio.org> 
Sent: Monday, March 25, 2013 10:06 PM
Subject: Re:[Biopython] convert to interleaved nexus
 

Hi Natassa,

I'm just reading the biopython source code. It seems NexusIO does not support interleaved nexus output. Internally, NexusIO.NexusWriter.write_alignment uses Bio.Nexus module to write out nexus format. And it doesn't allow you to specify interleave option.

You can try the following code (This is how write_alignment works):

from Bio.Nexus import Nexus
from Bio import AlignIO

alignment = AlignIO.read('...', format='...', format = ...)
minimal_record = "#NEXUS\nbegin data; dimensions ntax=0 nchar=0; format datatype=%s; end;" % "dna"
n = Nexus.Nexus(minimal_record)
n.alphabet = alignment._alphabet
for record in alignment:
    n.add_sequence(record.id, record.seq.tostring())
n.write_nexus_data('filename', interleave=True)

Alternatively, you may write MultipleSeqAlignment instance to a StringIO instance and then give it to Nexus.Nexus
Here is the code you may try:

from StringIO import StringIO
from Bio.Nexus import Nexus
from Bio import AlignIO

alignment = AlignIO.read('...', format='...', format = ...)
output = StringIO()
AlignIO.write(alignment, output, 'nexus')
p = Nexus.Nexus()
p.read(output.getvalue())
p.write_nexus_data('filename', interleave=True)


Hope this helps,

Zheng

------------------ Original ------------------
From:  "natassa"<natassa_g_2000 at yahoo.com>;
Date:  Mar 26, 2013
To:  "biopython at biopython.org"<biopython at biopython.org>; 
Subject:  [Biopython] convert to interleaved nexus
Hi, 

I am trying to convert a phylip alignment to interleaved nexus and I cannot. Something like:
def Convert_alnformat(alnfile, out, informat, outformat):
 alignment=AlignIO.read(alnfile, informat,  alphabet=Gapped(IUPAC.unambiguous_dna)) 
SeqIO.write(alignment,out, outformat)

will give me a sequential nexus, while I can;'t seem to get hold of the write_nexus_data method of the Bio:Nexus:Nexus:Nexus class. 

As this is called internally by AlignIO or SeqIO , I am unsure of how to even call it within my function. I tried:
from Bio.Nexus import Nexus 

then in the function 

Nexus.Nexus.write_nexus_data( alignment,interleave=True) 

and I get 

TypeError: unbound method write_nexus_data() must be called with Nexus instance as first argument (got MultipleSeqAlignment instance instead)

I understand that the alignment object is not a nexus instance, but I don't see what i should do.

Can you please help?
thanks, 

Natassa
_______________________________________________
Biopython mailing list  -  Biopython at lists.open-bio.org
http://lists.open-bio.org/mailman/listinfo/biopython



More information about the Biopython mailing list