-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcheck_fasta.py
More file actions
61 lines (51 loc) · 1.94 KB
/
check_fasta.py
File metadata and controls
61 lines (51 loc) · 1.94 KB
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
"""
Check a fasta file to make sure it is correct
"""
import os
import sys
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Check the integrity of a fasta file")
parser.add_argument('-f', help='fasta file to check', required=True)
parser.add_argument('-o', help='output file to write', required=True)
parser.add_argument('-s', help='strip new lines from sequence (default: keep new lines)', action="store_true")
parser.add_argument('-v', help='verbose output', action="store_true")
args = parser.parse_args()
seqid = None
seq = ""
with open(args.f, 'r') as fin:
with open(args.o, 'w') as out:
for l in fin:
l = l.strip()
if not l:
continue
if l.startswith(">"):
if seqid:
if seq:
out.write(seqid)
out.write(seq)
elif args.v:
sys.stderr.write("There is no sequence for {}. Skipped\n".format(seqid))
seqid = l + "\n"
seq = ""
continue
if ">" in l:
# line contains some sequence and then a new header line
tmpseq = l[:l.index('>')]
tmpid = l[l.index('>'):]
seq += tmpseq + "\n"
out.write(seqid)
out.write(seq)
seqid = tmpid + "\n"
seq = ""
continue
if args.s:
seq += l
else:
seq += l + "\n"
if seqid:
if seq:
out.write(seqid)
out.write(seq)
elif args.v:
sys.stderr.write("There is no sequence for {}. Skipped\n".format(seqid))