It shouldn't, but in case the . doesn't play nicely with your terminal, just enclose the string to look for in quotes: ".4"
I have a combined fasta file with all my sequences. I want to print the ID lines and their DNA sequence that end in .4
So far I have awk /'>*.4'/ {getline;print} combined.fasta Which prints the sequences that I want, how do I get the ID lines as well?
4 answers
Since you requested bash only:
#!/bin/bash
string="$2"
while read line ; do
if [[ ${line:0:1} == ">" ]] ; then
header="$line"
else
seq="$line"
if [[ "$header" == *"$string" ]]; then
echo -e "$header""\n""$seq"
fi
fi
done < $1
Put it in a script file and run it like:
$ bash parseheaders.sh seqs.fasta .4
Some of my own test data:
$ cat seqs.fasta
>tpg|Magnaporthiopsis_incrustans|JF414846
ACTGTAGTAGCTACGATCGATCAGATGATCACGTAGCATCGATCGATCATCGACTAGTAGATCACTCGACATAGATCCACATCAATAGATCATCATCATCATAATCGATCACTAGCAGC
>tpg|Pyricularia_pennisetigena|AB818016
GCAAGNTTCATGACGATGTAGAATGGCTTATCGAAGGGAGCAGGCCAGGGATTGAGGTCCGTCTCACGGGTTGGCTTCACTCCCCCACTGCCAGCCCTCTTGCTGCAACTCCACCAGAA
>tpg|Inocybe_sororia|EU525947
AACCANGCCGCGACGGCGGTGCGATCGGGAAACGCGGCGGTGGCGGAGGAATCGGCCATCCTTCACCATATCGGCCAAGGATTGTGGTTCCTGTAGGGCTCGCGCAGCCCAGGACGCGC
$ bash parseheaders.sh seqs.fasta 947
>tpg|Inocybe_sororia|EU525947
AACCANGCCGCGACGGCGGTGCGATCGGGAAACGCGGCGGTGGCGGAGGAATCGGCCATCCTTCACCATATCGGCCAAGGATTGTGGTTCCTGTAGGGCTCGCGCAGCCCAGGACGCGC
This works very well, many thanks! PS. also didn't need to put quotes around the .4.
You can accept multiple answers (green check mark). So you should accept this one one as well.
Just as a final comment though, I'd advise following some of the other suggestions here and use proper parsers like bioperl, biopython, bioawk and so on. My own personal go-to script for pulling out sequences is here, using Biopython (though it finds the key of interest anywhere in the header, not just at the end, so it wasn't directly applicable to this case.
grep '^>.*4$' -A 1 --no-group-separator in.fa
How about:
awk '/>*.4/ {print $0; getline; print}' combined.fasta
Log in to answer this question.
Can you show an example of your input data and ideal output?
Thanks for responding, I have ID's that end in .1, .2 and so on up until .8. I just want the .4's and their sequence.
Input:
Output: