-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget16S
executable file
·66 lines (48 loc) · 1.71 KB
/
get16S
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
62
63
64
65
66
#!/usr/bin/pypy
import sys
import os
import re
from Bio import SeqIO
if len(sys.argv) < 2:
print('USAGE: get16S GBK_DIR')
sys.exit(65)
# rRNA magic regex
regex = '(.*)16[sS]'
prog = re.compile(regex)
print('Using /%s/ regex search'%regex)
print('')
gbkdir = sys.argv[1]
notf = open('notfound.txt', 'w')
for infile in os.listdir(gbkdir):
if os.path.isdir(os.path.join(gbkdir,infile)):continue
i = 0
rseqs = []
print('Reading %s'%infile)
org = infile.split('.')[0]
for s in SeqIO.parse(os.path.join(gbkdir, infile), 'genbank'):
rRNA = sorted(filter(lambda x: x.type == 'rRNA', s.features),
key=lambda x: int(x.location.start))
for f in rRNA:
# Is it 16S?
if not prog.match(f.qualifiers['product'][0]):
continue
#print f.qualifiers['product'][0], f1.qualifiers['product'][0]
i += 1
rid = '%s_%d'%(org, i)
rseq = s[int(f.location.start): int(f.location.end)+1]
if f.strand < 0:
rseq = rseq.reverse_complement()
rseq.description = '(%s, %d, %d, -)'%(s.id,
int(f.location.start), int(f.location.end)+1)
else:
rseq.description = '(%s, %d, %d, +)'%(s.id,
int(f.location.start), int(f.location.end)+1)
rseq.id = rid
rseqs.append(rseq)
if len(rseqs) == 0:
print ('No 16S found for %s'%org)
notf.write('%s\n'%org)
continue
print('Found %d 16S in %s'%(len(rseqs), org))
SeqIO.write(rseqs, '%s.rna.fna'%org, 'fasta')
notf.close()