A quick awk for linearizing the sequences:
awk '$0~/^>/{if(NR>1){print sequence;sequence=""}print $0}$0!~/^>/{sequence=sequence""$0}END{print sequence}' "$1"
I have a file contain multiple gene >sequences. I want to separate into a file with gene ID perl or python script
As others have mentioned this is answered a lot on the forum. But I can't help myself when it comes to trying to make bash do this sort of thing (sequences will have to be linearised).
#!/bin/bash
i=1;
while read line ; do
if [ ${line:0:1} == ">" ] ; then
echo "$line" >> seq"${i}".fasta
else
echo "$line" >> seq"${i}".fasta
((i++))
fi
done < $1
Usage:
$ bash splitfasta.sh multifasta.fasta
Disclaimer:
You should always use a proper parser though (like biopython) as it'll catch many of the special cases. My code just has the bonus of not requiring anything to be installed to run.
A quick awk for linearizing the sequences:
awk '$0~/^>/{if(NR>1){print sequence;sequence=""}print $0}$0!~/^>/{sequence=sequence""$0}END{print sequence}' "$1"
This is a python script for splitting FASTA file into an individual file.
from Bio import SeqIO
import argparse
parser = argparse.ArgumentParser(description="Split the fasta file into individual file with each gene seq")
parser.add_argument('-f', action='store', dest='fasta_file', help='Input fasta file')
result = parser.parse_args()
f_open = open(result.fasta_file, "rU")
for rec in SeqIO.parse(f_open, "fasta"):
id = rec.id
seq = rec.seq
id_file = open(id, "w")
id_file.write(">"+str(id)+"\n"+str(seq))
id_file.close()
f_open.close()
To run above code, (save the above code in code.py file)
python code.py -f fasta_file
Note: You need to install Biopython module SeqIO to run this code.
you should add a note: this script requires biopython module installed
Hello,
I tried to use this code, but PYZO kept giving me error message. Any resolution? Thanks!
Running script: "C:\Users\14805\Desktop\python test\New folder\Splitthefastafile.py"
C:\Users\14805\Desktop\python test\New folder\Splitthefastafile.py:9: DeprecationWarning: 'U' mode is deprecated
f_open = open('result.fasta_file', "rU")
Traceback (most recent call last):
File "C:\Users\14805\Desktop\python test\New folder\Splitthefastafile.py", line 9, in <module>
f_open = open('result.fasta_file', "rU")
FileNotFoundError: [Errno 2] No such file or directory: 'result.fasta_file'
You haven't told it where the target fasta file is or have gotten the path/filename wrong, so it cannot find it.
Also, don't use spaces in file/folder names.
Log in to answer this question.
Can you show an example?
Search this forum for any regular question. These were answered many times. (You'll get used to this if you are a new user)
Here is one of many solutions on this forum: How To Split A Multiple Fasta
BTW, it is a question, not a tutorial.
Ok, I will put a new post.
Thanks
Please do not ask questions in existing threads. I suggest you delete this comment and ask a new questions, providing representative input and desired output data so that people can reproduce the problem.