text: user57: 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?
  
  user57: 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
  
  
  user3848: 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.
  
  
original_text: 57: <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>
   57: <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>
   3848: <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>
  