[Biopython] convert to interleaved nexus

Peter Cock p.j.a.cock at googlemail.com
Tue Mar 26 22:46:22 UTC 2013


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