<pre><code>from Bio import AlignIO
from pathlib import Path
from Bio.AlignIO.PhylipIO import SequentialPhylipWriter
import re

file = 'input.mafft.fasta'
fileoutnex = re.sub('fasta', 'nex', file)
pathway = '/mypath/'
alignment = AlignIO.read(Path(pathway, file), 'fasta')

# write out in nexus
AlignIO.write(alignment, Path(pathway, fileoutnex), 'nexus')

# write out in relaxed phylip
fileoutphy = re.sub('fasta', 'phy', file)
with open(Path(pathway, fileoutphy), 'w') as output_handle:        
    SequentialPhylipWriter(output_handle).write_alignment(alignment, id_width=30)
</code></pre>
<p>The advantage here is you can select what phylip format you want. That is a BIG advantage, traditional phylip format is terrible, particularly with taxa ids lengths. Here relaxed phylip format is being used.</p>
<p>The other advantages,</p>
<ul>
<li>Easily parse, change, alter the taxa names;</li>
<li>Phylip taxa id can be ANY length (<code>id_width=</code>);</li>
<li>save it as a script;</li>
<li>no need to write an outfile (ok minor);</li>
<li>pipe the alignment straight into downstream stuff;</li>
<li>very easy manipulate the alignment.</li>
</ul>

CC BY-SA 4.0
2022-08-30T01:42:16.613Z
2022-08-30T14:34:53.630Z
2022-08-30T14:34:53.630Z
M__
<p>EMBO's bioinformatic suite <a href="https://www.ebi.ac.uk/Tools/emboss/" rel="nofollow noreferrer">EMBOSS</a> has <a href="http://emboss.sourceforge.net/apps/cvs/emboss/apps/seqret.html" rel="nofollow noreferrer">seqret</a> program that can do exactly that. You can install it via conda (<code>conda install -c bioconda emboss</code>) and then the conversion to <code>nexus</code> is</p>
<pre><code>seqret -sequence input.mafft.fasta -outseq output.mafft.nex -osformat nexus
</code></pre>

CC BY-SA 4.0
2022-08-29T23:14:15.770Z
2022-08-29T23:14:15.770Z
Kamil S Jaron
<p>I generated a multiple sequence alignment using mafft, but I would like to convert it to nexus or phylip format (required by a downstream software). What's the easiest way to do the conversion?</p>

<phylogenetics><format-conversion>
How to convert fasta alignment to nexus or phylip format?
# How to convert fasta alignment to nexus or phylip format?

(Asked by: username_0 on 2022-08-29)

I generated a multiple sequence alignment using mafft, but I would like to convert it to nexus or phylip format (required by a downstream software). What's the easiest way to do the conversion?

**Tags:** phylogenetics, format-conversion

---

## 2 answers

### Answer by username_1 on 2022-08-30. Score: 1

from Bio import AlignIO
from pathlib import Path
from Bio.AlignIO.PhylipIO import SequentialPhylipWriter
import re

file = 'input.mafft.fasta'
fileoutnex = re.sub('fasta', 'nex', file)
pathway = '/mypath/'
alignment = AlignIO.read(Path(pathway, file), 'fasta')

# write out in nexus
AlignIO.write(alignment, Path(pathway, fileoutnex), 'nexus')

# write out in relaxed phylip
fileoutphy = re.sub('fasta', 'phy', file)
with open(Path(pathway, fileoutphy), 'w') as output_handle:        
    SequentialPhylipWriter(output_handle).write_alignment(alignment, id_width=30)

The advantage here is you can select what phylip format you want. That is a BIG advantage, traditional phylip format is terrible, particularly with taxa ids lengths. Here relaxed phylip format is being used.
The other advantages,

Easily parse, change, alter the taxa names;
Phylip taxa id can be ANY length (id_width=);
save it as a script;
no need to write an outfile (ok minor);
pipe the alignment straight into downstream stuff;
very easy manipulate the alignment.


---

### Answer by username_0 on 2022-08-29. Score: 1

EMBO's bioinformatic suite EMBOSS has seqret program that can do exactly that. You can install it via conda (conda install -c bioconda emboss) and then the conversion to nexus is
seqret -sequence input.mafft.fasta -outseq output.mafft.nex -osformat nexus


---

