[Biopython] convert to interleaved nexus
    阮铮 
    rz1991 at foxmail.com
       
    Tue Mar 26 05:06:50 UTC 2013
    
    
  
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