This is a test version of Biostars. For the public version, visit https://www.biostars.org.
FASTA file split

Hii, I have a merged fasta file of 1500 sequences.I want to split it into only 2 files ,one having 1000 fasta sequnces and other having 500 fasta sequences with headers intact.Can anyone suggest me the way with proper command to do it easily by awk or grep?

alignment sequence

for simple fasta format, sequence in one line:

$ head -n 2000 in.fa > first_1k.fa 
$ tail -n 1000 in.fa > last_500.fa

for fasta with multiple line sequence format, using bioawk: https://github.com/lh3/bioawk

$ bioawk -cfastx 'NR<=1000{print ">"$name"\n"$seq}' in.fa > first_1k.fa
$ bioawk -cfastx 'NR>1000{print ">"$name"\n"$seq}' in.fa > last_500.fa

If each raw sequence is in and only one line, then including the header it will be two lines, so you can use:

head  -n 2000 file.fasta > file1.fasta
tail  +2001 file.fasta > file2.fasta

+ before 2001 is necessary as it will output line 2001 and anything after that line.

Hii, This is my fasta file header

>hCoV-19/Country_name/1-27/2020|EPI_ISL_413522|2020-01-27

I have a merged fasta file.Can you suggest me a script or way to split my merged fasta file based on country_name? I want all the fasta sequences from one country in one separate fasta and same for others.Is it possible?

Try this:

 awk '{ if( $0 ~ /^>/){print prevID"\t"seq; prevID=$0; seq=""} else {gsub(/\W/, "", $0) ; seq=seq$0} } END {print prevID"\t"seq}' YOUR_FASTA_FILE |awk 'FNR >1'|head -1000|sed 's/\t/\n/g' > first1000seqs.fas

for the last 500 sequences substitute the head -1000 to tail -500.

Are all the Country names right after the first / ???

yes .All country names are after first "/"

this is a solution using bioawk to process fasta file: https://github.com/lh3/bioawk

$ bioawk -cfastx '{split($name, a, "/"); print ">"$name"\n"$seq >a[2]}' in.fa

Try this:

  awk '{ if( $0 ~ /^>/){print prevID"\t"seq; prevID=$0; seq=""} else {gsub(/\W/, "", $0) ; seq=seq$0} } END {print prevID"\t"seq}' YOUR_FASTA_FILE |awk 'FNR >1'|head -1000|sed 's/\t/\n/g' > first1000seqs.fas

for the last 500 sequences substitute the head -1000 to tail -500.

2 answers

Priya, you can use seqkit

seqkit split2 your_fast_file.fa -s 1000 -f

-s, --by-size int split sequences into multi parts with N sequences

You can also use SEDA. To achieve the desired split, you may use the Split operation (under Choose operation / General) and configure Fixed number of sequences per file with 1000 sequences.

Log in to answer this question.