How can I extract the longest N isoforms per gene from a fasta file? The question of how one can extract the longest isoform per gene from a multi-fasta file has been answered expertly in this thread: 
How can longest isoforms (per gene) be extracted from a FASTA file?

I wonder: how can the python script "filter.py" by Devon Ryan, that is marked as best answer, be modified so that it extracts the N longest isoform sequences per gene instead of only the single longest isoform? 

Any help on this to a python novice is very much appreciated. 

Here is an example of how the data looks (two genes with 5 isoformes each, and I need the 3 longest isoforms for each gene):

&gt;Animal1|gene000001_1  
TTLQLLNELVLLLHYTTLLLHTKKTKESTINNYFKQKEQK  
&gt;Animal1|gene000001_2  
CYYYTTQHYYYIQKKQKNLQSTITSNKKNKNKHAK  
&gt;Animal1|gene000001_3  
ARDMLDYTTTTKRVSATTTLHNTTTTYKKNKRIYNQQLLQTKRTKISMQNKNLNQINYKK  
LQETTEKHAK  
&gt;Animal1|gene000001_4  
HHAVNFQPMSEATGIYLNSTEIRNSLLQYSSWSWFTFHTCKLWNVTF  
&gt;Animal1|gene000001_5  
EILCYSIPPGVGLLFTPVNFGMLRSKLIKVRILDIINFNCSLLICLPPYLFQIDVGEDKD  
DPVVEIVLYLACSI  
&gt;Animal1|gene000002_1  
TTLQLLNELVLLLHYTTLLLHTKKTKESTINNYFKQKEQK  
&gt;Animal1|gene000002_2  
CYYYTTQHYYYIQKKQKNLQSTITSNKKNKNKHAK  
&gt;Animal1|gene000002_3  
ARDMLDYTTTTKRVSATTTLHNTTTTYKKNKRIYNQQLLQTKRTKISMQNKNLNQINYKK  
LQETTEKHAK  
&gt;Animal1|gene000002_4  
LCSAVARQKTKSRKYDATVDTCFSNIKININVLSFSLIVNPKYSKVSYQYGTNDAFRKFF  
ITSRSLR  
&gt;Animal1|gene000002_5  
ETLEYFGLTINEKESTLILILMLLKQVSTVASYFLDLVFCLATALHS  


As output, I would like to have a fasta file like the following: 

&gt;Animal1|gene000001_5  
EILCYSIPPGVGLLFTPVNFGMLRSKLIKVRILDIINFNCSLLICLPPYLFQIDVGEDKD  
DPVVEIVLYLACSI  
&gt;Animal1|gene000001_3  
ARDMLDYTTTTKRVSATTTLHNTTTTYKKNKRIYNQQLLQTKRTKISMQNKNLNQINYKK  
LQETTEKHAK  
&gt;Animal1|gene000001_4  
HHAVNFQPMSEATGIYLNSTEIRNSLLQYSSWSWFTFHTCKLWNVTF  
&gt;Animal1|gene000002_3  
ARDMLDYTTTTKRVSATTTLHNTTTTYKKNKRIYNQQLLQTKRTKISMQNKNLNQINYKK  
LQETTEKHAK  
&gt;Animal1|gene000002_4  
LCSAVARQKTKSRKYDATVDTCFSNIKININVLSFSLIVNPKYSKVSYQYGTNDAFRKFF  
ITSRSLR  
&gt;Animal1|gene000002_5  
ETLEYFGLTINEKESTLILILMLLKQVSTVASYFLDLVFCLATALHS
I know you are asking for a python solution. But maybe you are interested in other ways.

You can use samtools faidx to index your fasta file. The resulting file contains the information about the length of every sequence. We can sort by length and extract the first n transcripts per gene.

1. Index fasta

samtools faidx input.fa


2. Extract IDs of the n longest transcripts per gene

sed 's/_.\+//g' input.fa.fai | sort -u | 
    parallel -k 'grep {} -m 3 &lt;(sort -k2,2 -r --sort g input.fa.fai) |
        cut -f1' &gt; longestID.txt


The sed command replaces everything from _ onwards in each line, leaving behind only the gene name. sort -u is used to get a list of distinct gene names. For each of theses gene names we grep for the first 3 matches in the descending sorted by length index file. As we just want the sequence ids, we select only the first column with cut. Adopt the -m parameter if you want more or fewer transcripts.

3. Get sequences

samtools faidx input.fa -r longestID.txt &gt; output.fa