<p>A simple Biopython solution- iterate over the sequence records, identify the species, open the file handle using append mode to ensure no data is overwritten, and write the record:</p>
<pre><code>from Bio import SeqIO

for record in SeqIO.parse(&quot;myfile.fa&quot;, &quot;fasta&quot;):
    species = record.id.split('_')[0]
    with open(f&quot;{species}.fa&quot;, &quot;a&quot;) as f:
        SeqIO.write(record, f, &quot;fasta&quot;)
</code></pre>

CC BY-SA 4.0
2020-07-26T14:38:48.333Z
Thanks. I tried: 

`from Bio import SeqIO

for record in SeqIO.parse("test.fasta", "fasta"):
    species = record.id.split('Species-')[0]
    with open(f"{species}.fasta", "a") as f:
        SeqIO.write(record, f, "fasta") `


But I got as output every sample in a separate file and not combined all species one in one file.
Julie
CC BY-SA 4.0
2020-07-26T14:41:54.420Z
@Julie your code on the line `species =` does not match my code, try what I suggested (if your input format is as described)
Chris_Rands
CC BY-SA 4.0
2020-07-26T14:44:28.140Z
Sorry. I made a mistake when posting the question, my fasta headers actually look like: 
>Species-1-samplenameA ... I edited my post.
Julie
CC BY-SA 4.0
2020-07-26T14:46:34.837Z
@Julie `species = '-'.join(record.id.split('-', 2)[:2])`
Chris_Rands
CC BY-SA 4.0
2020-07-26T14:48:20.510Z
Works perfectly! Thanks a lot.
Julie
CC BY-SA 4.0
2020-07-26T14:24:15.787Z
2020-07-26T14:24:15.787Z
Chris_Rands
<p>I have a fasta file containing the sequence of a gene across different species. In total there are around 900 samples and 12 species. (Each sequences is over multiple lines and longer than 100bp.)</p>
<p>My fasta file looks like:</p>
<pre><code>&gt;Species-1-samplenameA
CTATCCTTAAACGCATATCTCGCACAGTAACTCCCCAATATGTGAGCATCTGATGTTGCCCGGGCCGAGTTAGTCTTGTGCTCACGGAACTTATTGTATG
&gt;Species-2-samplenameB
AGTAGTGATTTGAAAGAGTTGTCAGTTAGCTCGTTCAGGTAATGGTTCCTCACACTACGTCAAAATAAGAGAGCGGTCGTGACATTATCCGTGATTTTCT
&gt;Species-3-samplenameC
CACTACTATCAGTACTCACGACTCGATTCTGCCGCAGCCACGTATCGCCAGAAAGCCAGTCAGCATTAAGGAGTGCTCTGGGCAGGACAACTCGCATAGT
&gt;Species-3-samplenameD
GAGAGTTACATGTTCGTTGGGCTCTTCCGACACGAACCTCAGTTGGCCTACATCCTACCTGAGGTCTGTGCCCCGGTGGTGAGAAGTGCGCATTTCGTTC
</code></pre>
<p>I want to split this file in one fasta file per species.
I think it’s possible to use the awk function for this but I’m stuck. Does anyone have a script/code that might help me?</p>
<p>Thanks a lot.</p>

<python><fasta><bash><awk>
Split fasta file based on groups in header information and output as separate files
# Split fasta file based on groups in header information and output as separate files

(Asked by: username_0 on 2020-07-26)

I have a fasta file containing the sequence of a gene across different species. In total there are around 900 samples and 12 species. (Each sequences is over multiple lines and longer than 100bp.)
My fasta file looks like:
>Species-1-samplenameA
CTATCCTTAAACGCATATCTCGCACAGTAACTCCCCAATATGTGAGCATCTGATGTTGCCCGGGCCGAGTTAGTCTTGTGCTCACGGAACTTATTGTATG
>Species-2-samplenameB
AGTAGTGATTTGAAAGAGTTGTCAGTTAGCTCGTTCAGGTAATGGTTCCTCACACTACGTCAAAATAAGAGAGCGGTCGTGACATTATCCGTGATTTTCT
>Species-3-samplenameC
CACTACTATCAGTACTCACGACTCGATTCTGCCGCAGCCACGTATCGCCAGAAAGCCAGTCAGCATTAAGGAGTGCTCTGGGCAGGACAACTCGCATAGT
>Species-3-samplenameD
GAGAGTTACATGTTCGTTGGGCTCTTCCGACACGAACCTCAGTTGGCCTACATCCTACCTGAGGTCTGTGCCCCGGTGGTGAGAAGTGCGCATTTCGTTC

I want to split this file in one fasta file per species.
I think it’s possible to use the awk function for this but I’m stuck. Does anyone have a script/code that might help me?
Thanks a lot.

**Tags:** python, fasta, bash, awk

---

## 1 answers

### Answer by username_1 on 2020-07-26. Score: 3 | Accepted answer

A simple Biopython solution- iterate over the sequence records, identify the species, open the file handle using append mode to ensure no data is overwritten, and write the record:
from Bio import SeqIO

for record in SeqIO.parse("myfile.fa", "fasta"):
    species = record.id.split('_')[0]
    with open(f"{species}.fa", "a") as f:
        SeqIO.write(record, f, "fasta")

> **Comments:**
> **username_0**: Thanks. I tried:   `from Bio import SeqIO  for record in SeqIO.parse("test.fasta", "fasta"):     species = record.id.split('Species-')[0]     with open(f"{species}.fasta", "a") as f:         SeqIO.write(record, f, "fasta") `   But I got as output every sample in a separate file and not combined all species one in one file.  
> **username_1**: @Julie your code on the line `species =` does not match my code, try what I suggested (if your input format is as described)  
> **username_0**: Sorry. I made a mistake when posting the question, my fasta headers actually look like:  >Species-1-samplenameA ... I edited my post.  
> **username_1**: @Julie `species = '-'.join(record.id.split('-', 2)[:2])`  
> **username_0**: Works perfectly! Thanks a lot.  

---

