This won't work if the non-blank-space separated words are too long. Given that the 40 char limit is the primary concern, the cut should be something like -c 1-40 and it should come after the sed operations.
I have FASTA headers with long annotation names, but the program it will be run through for proteomics has a limit of roughly 40 characters or else it will crash.
The file starts off like this:
>TRINITY_DN0_c0_g1_i1.p1 - RecName: Full=E3 ubiquitin-protein ligase CIP8; AltName: Full=COP1-interacting protein 8; AltName: Full=RING-type E3 ubiquitin transferase CIP8
SEQUENCE
>TRINITY_DN10003_c0_g1_i12.p1 - RecName: Full=Polycomb group protein FIE1; AltName: Full=Protein FERTILIZATION-INDEPENDENT ENDOSPERM 2; Short=OsFIE2; AltName: Full=WD40 repeat-containing protein 153; Short=OsWD40-153
SEQUENCE
Ideally I want the FASTA to look like this:
>DN0_c0_g1_i1.p1 - E3 ubiquitin-protein
SEQUENCE
>DN10003_c0_g1_i12.p1 - Polycomb group
SEQUENCE
I used sed and seqkit to cut out the repetitive parts
sed 's/>.*Y_/>/' proteome.fasta
seqkit replace -p " RecName: Full=" -r ' ' proteome.fasta > proteome2.fasta
The fasta looks like this now:
>DN0_c0_g1_i1.p1 - E3 ubiquitin-protein ligase CIP8; AltName: Full=COP1-interacting protein 8; AltName: Full=RING-type E3 ubiquitin transferase CIP8
SEQUENCE
>DN10003_c0_g1_i12.p1 - Polycomb group protein FIE1; AltName: Full=Protein FERTILIZATION-INDEPENDENT ENDOSPERM 2; Short=OsFIE2; AltName: Full=WD40 repeat-containing protein 153; Short=OsWD40-153
SEQUENCE
What can I do to limit the header length? Can I do it with seqkit?
3 answers
You can do sth like this-
cat proteome.fasta
>TRINITY_DN0_c0_g1_i1.p1 - RecName: Full=E3 ubiquitin-protein ligase CIP8; AltName: Full=COP1-interacting protein 8; AltName: Full=RING-type E3 ubiquitin transferase CIP8
SEQUENCE
>TRINITY_DN10003_c0_g1_i12.p1 - RecName: Full=Polycomb group protein FIE1; AltName: Full=Protein FERTILIZATION-INDEPENDENT ENDOSPERM 2; Short=OsFIE2; AltName: Full=WD40 repeat-containing protein 153; Short=OsWD40-153
SEQUENCE
cut -d ' ' -f1-5 proteome.fasta |sed 's/RecName: Full=//g' |sed 's/TRINITY_//g'
>DN0_c0_g1_i1.p1 - E3 ubiquitin-protein
SEQUENCE
>DN10003_c0_g1_i12.p1 - Polycomb group
SEQUENCE
Please check this too if it works.
sed 's/TRINITY_//g' proteome.fasta |sed 's/RecName: Full=//g' |awk '/^>/{print substr($0, 1, 39)} !/^>/{print}'
This is the most complete and concise answer. The two seds instead of using seqkit is great.
The awk for the character limit also works like a charm. If you make this the main post, I will accept it as the answer. Thanks for the nice solution!
Ok. I am making it as the main post. Please go ahead and accept it as answer. Thanks!
Please check this, it will work-
sed 's/TRINITY_//g' proteome.fasta |sed 's/RecName: Full=//g' |awk '/^>/{print substr($0, 1, 39)} !/^>/{print}'
Looks like a job for bioawk. For example, if you just need the IDs (like in the seqkit answer above), you can do this:
bioawk -c fastx '{print ">"$name; print $seq}' test.fa
>DN0_c0_g1_i1.p1
SEQUENCE
>DN10003_c0_g1_i12.p1
SEQUENCE
This works because bioawk -c fastx parses FASTA/FASTQ into a table of 3 or 4 fields for you (FASTA is missing $qual of course). From bioawk -c help you can see the column names:
...
fastx:
1:name 2:seq 3:qual 4:comment
You can also use $comment in substr or gsub functions to extract other info and add it after your ID ($name). Comments are separated from names by the first whitespace in the header.
Log in to answer this question.
How about this (starting with your last example file)
OP also wants
TRINITY_removed so asedmight be required before the awk. However, given that OP has already figured out the sed, maybe useproteome.fastainstead offasta_filein your code so OP knows not to replace their entire code with your awk.I used the last example that OP showed above.
fasta_fileis just a place holder for file name.Only keep the sequence identifiers:
Where,
Or
But the number of the characters may exceed the limit.
This is a neat use of seqkit, I will definitely keep this in mind for future projects.
Perhaps you should also check that after trimming the names you don't get duplicate IDs. These two commands should give the same output (not checked):