<p>I have nutted out a solution for you.</p>
<p>If this is the content of input.fa:</p>
<pre><code>&gt;virusA_detailed_description
ACGTGACGATCCCCAAACGTGACGATCCCCAA
&gt;virusB_detailed_description
ACGTGACGATCGGGAAACGTGACGATCGGGAA
&gt;virusC_detailed_description
ACGTGACGATCAAAAAACGTGACGATCAAAAA
&gt;virusD_detailed_description
ACGTGACGATTTTTAAACGTGACGATTTTTAA
</code></pre>
<p>And this is the script:</p>
<pre><code>#!/usr/bin/env python3

'''
Converts a fasta file to phylip format
'''

from Bio import AlignIO
from Bio.AlignIO.PhylipIO import PhylipWriter
import sys

infile = sys.argv[1]
outfile = sys.argv[2]
idwidth = int(sys.argv[3])
with open(infile, 'r') as input_handle:
    align = AlignIO.read(input_handle, &quot;fasta&quot;)
    with open(outfile, 'w') as output_handle:
        PhylipWriter(output_handle).write_alignment(align, id_width=idwidth)
</code></pre>
<p>Then, save this script as <code>phylpwrtr.py</code> and do:</p>
<pre><code>chmod +x phylpwrtr.py
./phylpwrtr.py input.fa output_longids.phy 30 # 30 is the width of the header, adjust the value to adjust header length
</code></pre>
<p>The contents of <code>output_longids.phy</code> looks like:</p>
<pre><code> 4 32
virusA_detailed_description    ACGTGACGAT CCCCAAACGT GACGATCCCC AA
virusB_detailed_description    ACGTGACGAT CGGGAAACGT GACGATCGGG AA
virusC_detailed_description    ACGTGACGAT CAAAAAACGT GACGATCAAA AA
virusD_detailed_description    ACGTGACGAT TTTTAAACGT GACGATTTTT AA
</code></pre>

CC BY-SA 4.0
2020-07-02T10:03:33.423Z
2020-07-02T10:03:33.423Z
user3479780
<p>I routinely use AlignIO in BioPython for manipulating alignments and in context here moving from FASTA format to phylip format. A lot of phylogeny packages accept a relaxed (sequence ID) &amp; sequential (sequence) phylip format. This format is user-friendly, helps double-check for alignment errors, and can be the essential input format.</p>
<p>A typical bit of code is,</p>
<pre><code>'''
Converts a fasta file to phylip format
'''
from Bio import AlignIO

Class Convert (inheritClass):
   
    def fa2phy (self):
        fileOut, fileIn = self._inOut() # builds the path name and deletes any pre-exising outfile
        align = AlignIO.read(fileIn, &quot;fasta&quot;) # reads the alignment in fasta
        AlignIO.write(align, fileOut, &quot;phylip-relaxed&quot;) # writes in phylip
</code></pre>
<p>The output of &quot;phylip-relaxed&quot; is interleaved format</p>
<pre><code>4 120
virusA_detailed_description ACGTGACG .... ATCCCCAA
virusB_detailed_description ACGTGACG .... ATCGGGAA
virusC_detailed_description ACGTGACG .... ATCAAAAA
virusD_detailed_description ACGTGACG .... ATTTTTAA

ACGTGACG .... ATCCCCAA
ACGTGACG .... ATCGGGAA
ACGTGACG .... ATCAAAAA
ACGTGACG .... ATTTTTAA
</code></pre>
<p>Output sought is relaxed-sequential</p>
<pre><code>4 120
virusA_detailed_description ACGTGACG .... ATCCCCAAACGTGACG .... ATCCCCAA
virusB_detailed_description ACGTGACG .... ATCGGGAAACGTGACG .... ATCGGGAA
virusC_detailed_description ACGTGACG .... ATCAAAAAACGTGACG .... ATCAAAAA
virusD_detailed_description ACGTGACG .... ATTTTTAAACGTGACG .... ATTTTTAA
</code></pre>
<p>There is an option to allow a sequential format (above). <em>However,</em> a <code>&quot;phylip-sequential&quot;</code> option output is as follows (it truncates the sequence ID to 10 characters,</p>
<pre><code>4 120
virusA_det ACGTGACG .... ATCCCCAAACGTGACG .... ATCCCCAA
virusB_det ACGTGACG .... ATCGGGAAACGTGACG .... ATCGGGAA
virusC_det ACGTGACG .... ATCAAAAAACGTGACG .... ATCAAAAA
virusD_det ACGTGACG .... ATTTTTAAACGTGACG .... ATTTTTAA
</code></pre>
<p>No matter how I look through AlignIO options the above is the nearest format.  Could anyone point to a IO style solution?
I could write it, but that defeats the object (i.e. purpose not an instance of a method).</p>
<hr />
<p><em>Note</em> the <code>&quot;....&quot;</code> in the outputs above are not a typical &quot;sequence identity&quot; symbols, it just means that total length of a line in an interleaved format is 60 nucleotides (it might be 80).</p>

CC BY-SA 4.0
2020-06-30T12:07:35.257Z
I think the solution is to output using SeqIO, i.e. one sequence a time
M__
<phylogenetics><python><format-conversion>
Relaxed and sequential Phylip format conversion
# Relaxed and sequential Phylip format conversion

(Asked by: username_0 on 2020-06-27)

I routinely use AlignIO in BioPython for manipulating alignments and in context here moving from FASTA format to phylip format. A lot of phylogeny packages accept a relaxed (sequence ID) & sequential (sequence) phylip format. This format is user-friendly, helps double-check for alignment errors, and can be the essential input format.
A typical bit of code is,
'''
Converts a fasta file to phylip format
'''
from Bio import AlignIO

Class Convert (inheritClass):
   
    def fa2phy (self):
        fileOut, fileIn = self._inOut() # builds the path name and deletes any pre-exising outfile
        align = AlignIO.read(fileIn, "fasta") # reads the alignment in fasta
        AlignIO.write(align, fileOut, "phylip-relaxed") # writes in phylip

The output of "phylip-relaxed" is interleaved format
4 120
virusA_detailed_description ACGTGACG .... ATCCCCAA
virusB_detailed_description ACGTGACG .... ATCGGGAA
virusC_detailed_description ACGTGACG .... ATCAAAAA
virusD_detailed_description ACGTGACG .... ATTTTTAA

ACGTGACG .... ATCCCCAA
ACGTGACG .... ATCGGGAA
ACGTGACG .... ATCAAAAA
ACGTGACG .... ATTTTTAA

Output sought is relaxed-sequential
4 120
virusA_detailed_description ACGTGACG .... ATCCCCAAACGTGACG .... ATCCCCAA
virusB_detailed_description ACGTGACG .... ATCGGGAAACGTGACG .... ATCGGGAA
virusC_detailed_description ACGTGACG .... ATCAAAAAACGTGACG .... ATCAAAAA
virusD_detailed_description ACGTGACG .... ATTTTTAAACGTGACG .... ATTTTTAA

There is an option to allow a sequential format (above). However, a "phylip-sequential" option output is as follows (it truncates the sequence ID to 10 characters,
4 120
virusA_det ACGTGACG .... ATCCCCAAACGTGACG .... ATCCCCAA
virusB_det ACGTGACG .... ATCGGGAAACGTGACG .... ATCGGGAA
virusC_det ACGTGACG .... ATCAAAAAACGTGACG .... ATCAAAAA
virusD_det ACGTGACG .... ATTTTTAAACGTGACG .... ATTTTTAA

No matter how I look through AlignIO options the above is the nearest format.  Could anyone point to a IO style solution?
I could write it, but that defeats the object (i.e. purpose not an instance of a method).

Note the "...." in the outputs above are not a typical "sequence identity" symbols, it just means that total length of a line in an interleaved format is 60 nucleotides (it might be 80).

**Tags:** phylogenetics, python, format-conversion
> **Comments:**
> **username_0**: I think the solution is to output using SeqIO, i.e. one sequence a time  

---

## 1 answers

### Answer by username_1 on 2020-07-02. Score: 2 | Accepted answer

I have nutted out a solution for you.
If this is the content of input.fa:
>virusA_detailed_description
ACGTGACGATCCCCAAACGTGACGATCCCCAA
>virusB_detailed_description
ACGTGACGATCGGGAAACGTGACGATCGGGAA
>virusC_detailed_description
ACGTGACGATCAAAAAACGTGACGATCAAAAA
>virusD_detailed_description
ACGTGACGATTTTTAAACGTGACGATTTTTAA

And this is the script:
#!/usr/bin/env python3

'''
Converts a fasta file to phylip format
'''

from Bio import AlignIO
from Bio.AlignIO.PhylipIO import PhylipWriter
import sys

infile = sys.argv[1]
outfile = sys.argv[2]
idwidth = int(sys.argv[3])
with open(infile, 'r') as input_handle:
    align = AlignIO.read(input_handle, "fasta")
    with open(outfile, 'w') as output_handle:
        PhylipWriter(output_handle).write_alignment(align, id_width=idwidth)

Then, save this script as phylpwrtr.py and do:
chmod +x phylpwrtr.py
./phylpwrtr.py input.fa output_longids.phy 30 # 30 is the width of the header, adjust the value to adjust header length

The contents of output_longids.phy looks like:
 4 32
virusA_detailed_description    ACGTGACGAT CCCCAAACGT GACGATCCCC AA
virusB_detailed_description    ACGTGACGAT CGGGAAACGT GACGATCGGG AA
virusC_detailed_description    ACGTGACGAT CAAAAAACGT GACGATCAAA AA
virusD_detailed_description    ACGTGACGAT TTTTAAACGT GACGATTTTT AA


---

