-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_sequence_from_fasta.py
61 lines (44 loc) · 1.58 KB
/
delete_sequence_from_fasta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
#Delete a specific sequence from fasta file
#Help message: python3 delete_sequence_from_fasta -h
import argparse
from collections import defaultdict
import re
#################################################
def parse_fasta(filename):
seqs_heads=defaultdict(lambda:"")
header=""
with open (filename, "r") as fh:
for line in fh:
line=line.rstrip("\n")
if ">" in line:
header=line
else:
seqs_heads[re.sub(">","",header)]+=line
return seqs_heads
#############################################################
def write_fasta(dict_fasta, filename):
fh_out=open(filename, "w")
for i,j in dict_fasta.items():
fh_out.write(">"+i+"\n"+j+"\n")
###########################################################
def filter_fasta(dict_fasta,species_name):
try:
del dict_fasta[species_name]
except KeyError:
print(species_name+" not found in fasta!")
finally:
return dict_fasta
##################################################
def main():
parser=argparse.ArgumentParser(description="Remove specific sequence from fasta. If species not in fasta the output is the same unfiltered fasta")
parser.add_argument("in_fasta", help="Input fasta file")
parser.add_argument("out_fasta", help="Output fasta file (filtered)")
parser.add_argument("species", help="Species to remove from fasta")
args=parser.parse_args()
seqs_headers=parse_fasta(args.in_fasta)
seqs_headers_new=filter_fasta(seqs_headers,args.species)
write_fasta(seqs_headers_new, args.out_fasta)
###################################################################
if __name__=='__main__':
main()