-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter-seq-len.py
executable file
·57 lines (48 loc) · 1.41 KB
/
filter-seq-len.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
#!/usr/bin/env python3
# Author: Arkadiy Garber
from collections import defaultdict
import statistics
import os
import re
import textwrap
import argparse
import sys
parser = argparse.ArgumentParser(
prog="filter-seq-len.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
Script to filter out sequences below a certain length
Developed by Arkadiy Garber: [email protected]
'''))
parser.add_argument('-f', help='input FASTA sequence file')
parser.add_argument('-m', help="minimum sequence length")
parser.add_argument('-o', help="name output file")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
file = open(args.f, "r")
seq = ''
header = ''
if args.o != args.f:
out = open(args.o, "w")
for i in file:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) >= int(args.m):
print(header)
out.write(">" + header + "\n")
out.write(seq + "\n")
header = i[1:]
header = header.split(" ")[0]
seq = ''
else:
header = i[1:]
header = header.split(" ")[0]
seq = ''
else:
seq += i
file.close()
out.close()
else:
print("Output file name same as the fasta file. You want your assembly overwritten!?")