[Biopython] convert to interleaved nexus

natassa natassa_g_2000 at yahoo.com
Wed Mar 27 00:07:54 UTC 2013


Hi Peter, 
There is a single call to write_nexus_data within the call, the second one is quoted out. But it is within an if block-do you mean this is incorrect?
I can't really tell if there is a specific need for interleaved versus sequential nexus, I use the file for a MrBayes analysis and the error message i got when running it with a sequential nexus had  something to do with max characters per line , similarly to what was discussed here:
http://biopython.org/pipermail/biopython-dev/2010-December/008480.html
I thus decided to go for an interleaved format. 
But it would be good if NexusIO supports more formats or in general, that the functionalities of the write_nexus_data were more easily accessible compared to the 'workaround' that I did here. I am not a biopython expert to assess this though :-)
Thanks, 
Natassa




________________________________
 From: Peter Cock <p.j.a.cock at googlemail.com>
To: natassa <natassa_g_2000 at yahoo.com> 
Cc: 阮铮 <rz1991 at foxmail.com>; "biopython at biopython.org" <biopython at biopython.org> 
Sent: Tuesday, March 26, 2013 3:46 PM
Subject: Re: [Biopython] convert to interleaved nexus
 
On Tue, Mar 26, 2013 at 7:43 PM, natassa <natassa_g_2000 at yahoo.com> wrote:
> 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)

It may be just the email formatting messing up, but there
should be ONE call to write_nexus_data - it should be
outside the for loop as in Zheng's example, or the
write_alignment method in Bio/AlignIO/NexusIO.py

That might explain things (I've not checked).

If might be useful to make interleave an option on
the NexusWriter class in  Bio/AlignIO/NexusIO.py -
especially if there is a good reason to sometimes
need interleaved NEXUS. My impression was that
non-interleaved was better for reliable parsing in
most tools (from memory - I've not looked at this
for a while).

Regards,

Peter



More information about the Biopython mailing list