Python FASTA scripting
Hi all, I finally got that illusive Bioinformatician job and am now undergoing training. My first project is to write a FASTA parsing script that will take a FASTA file and split it into multiple files each containing a gene and its sequence. The task is to be done in pure Python 3.7 so no Biopython for me.
…
TestFile = "/Users/me/Desktop/PythonScrip/testanimals.fasta"
f1 = open(TestFile, "r")
titlelist = []
seqlist=[]
line = f1.readline()
if not line.startswith('>'):
raise TypeError("Not a Fasta, Check file and start again.")
for line in f1:
if line.startswith(">"):
titlelist.append(line)
if not line.startswith(">"):
seqlist.append(line)
if line.startswith('n'):
seqlist.remove('n')
else:
print("Carrying on...")
print(titlelist)
print(seqlist)
for name in titlelist:
names=name.strip(">").strip()
for seq in seqlist:
seqs=seq.strip(">").strip()
file = open("/Users/me/Desktop/PythonScrip/gene{}.fasta".format(names),'w')
file.write(StringToWrite)
StringToWrite = "{}n{}".format(names, seqs)
print(StringToWrite)
print(names)
print(seqs)
My input file is very basic:
>duck
quackquackquack
>dinosaur
roarroarroarroar
>dog
woofwoofwoof
>fish
glubglubglub
>frog
ribbetribbetribbet
…
I am currently having a bit of an issue at this point. The script works great, multiple files are created with different names but they all contain the same sequence, eg all of the animals go ribbetribbetribbet. although funny its also annoying. I can’t quite get my head around what I’ve done though I think it has something to do with the 1st for being a priority and essentially ignoring the second.
I know it’s ugly code but I am just getting started 🙂
Thanks for the help!
• 4.0k views
One approach I’d suggest uses standard input. This makes it very easy to test out different inputs:
#!/usr/bin/env python
import sys
import re
import os
import errno
sequence = ""
for line in sys.stdin:
line = line.rstrip()
if line.startswith('>'):
if len(sequence) > 0:
clean_header = re.sub('[^A-Za-z0-9]+', '', header)
out_fn = '{}.fa'.format(clean_header)
if os.path.exists(out_fn):
raise OSError(errno.EEXIST)
with open(out_fn, 'w') as o:
o.write('>{}n{}n'.format(header, sequence))
header = line.lstrip('>')
sequence = ""
else:
sequence += line
# print out last element
if len(sequence) > 0:
clean_header = re.sub('[^A-Za-z0-9]+', '', header)
out_fn = '{}.fa'.format(clean_header)
if os.path.exists(out_fn):
raise OSError(errno.EEXIST)
with open(out_fn, 'w') as o:
o.write('>{}n{}n'.format(header, sequence))
To use:
$ python splitter.py < input.fa
This does a few things that I think make for a decent Python script:
- It should work with Python 2 or 3.
- It handles multiline Fasta input.
- It does not require slow dependencies like Biopython.
- If does some rudimentary filename sanitation, in case the Fasta record’s header has characters in it that would cause problems with a filesystem (slashes, some punctuation, etc.)
- It does some rudimentary error checking, so that you don’t overwrite files with the same file name.
- It uses Unix standard input and error streams.
- It uses the
with
motif to automatically close file handles, once the work is done.
Hello,
for name in titlelist:
names=name.strip(">").strip()
here you are overwrite names
with each iteration. The result is, that names
contains the last name in the list.
for seq in seqlist:
seqs=seq.strip(">").strip()
file = open("/Users/me/Desktop/PythonScrip/gene{}.fasta".format(names),'w')
file.write(StringToWrite)
StringToWrite = "{}n{}".format(names, seqs)
Now names
is taken as a file name in each iteration. Because names
doesn’t change anymore, you are always overwrite the same file. The fasta header stays the same, only the sequence is changing with each iteration.
The script works great, multiple files are created with different names
This cannot be true (see above). Furthermore you should get a NameError: name 'StringToWrite' is not defined
, because you are using the variable before assign it.
Some more tips:
- You don’t take care of the case, where the sequence is longer then one line.
- Get familiar with the
with
statement for opening/closing files - Also the concept of dictionaries will be useful for this usecase
You can do the following:
import sys
def fasta_parser(fasta):
"""
Iterates through fasta
Args:
fasta (file): fasta_file
Returns:
seqs (list): list of seqs
headers (list): list of headers
"""
seqs = []
headers = []
with open(fasta) as fa:
sequence = ""
header = None
for line in fa:
if line.startswith('>'):
headers.append(line[1:-1])
if header:
seqs.append([sequence])
sequence = ""
header = line[1:]
else:
sequence += line.rstrip()
seqs.append([sequence])
return headers, seqs
def main():
myfa = sys.argv[1]
headers, seqs = fasta_parser(myfa)
flat_seqs = []
for seq in seqs:
for item in seq:
flat_seqs.append(item)
for header, seq in zip(headers, flat_seqs):
with open('%s.fa' %header, 'w') as new_fa:
new_fa.write('>'+header+'n'+str(seq))
if __name__ == "__main__":
main()
This is dealing with all the problems listed in other answers such as multiline fastas and multi-fastas. It stores the headers and seqs in a list when you unpack the function. It is then iterating through both the headers and seqs lists (which are the same length because I have flattened the multiline sequences) and naming the file based on the header and adding a .fa
extension to it, then writing the header to the first line, and the seq as a flat line under the file.
EDIT: To run the code just python3 split_fa.py /path/to/fun.fa
Hi All, incase anyone wanted to know this is the function i went with in the end. With some help from people on here and on my course we managed:
def FastaSplitter(FileInput, FileOutPut):
Name = ""
Seq = []
FILE = open(FileInput, 'r')
for line in FILE:
if line[0] == '>':
if Name == "":
pass
else:
FILE.close()
print(line)
else:
print(line)
It looks like I may have originally overcomplicating things.
Thanks for the all the help everyone.
Traffic: 2315 users visited in the last hour
Read more here: Source link