-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrrna.py
More file actions
49 lines (44 loc) · 1.47 KB
/
rrna.py
File metadata and controls
49 lines (44 loc) · 1.47 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
import subprocess as sp
import gzip
import csv
from helpers import find_mean, timestamp
def rrna_search(input, input_path, out_path, write_path, db_path, err_file):
try:
n, frag = input
input_file = f'{input_path}/{n}'
out_file = f"{out_path}/{n}.rrna.cmscan.tsv"
db_file = f"{db_path}/rRNA"
cmd = [
'cmscan',
'--noali',
'--cut_tc',
'--cpu', '1',
'--tblout', str(out_file),
db_file,
input_file,
]
proc = sp.run(
cmd,
stdout=sp.PIPE,
stderr=sp.PIPE,
universal_newlines=True
)
tsvfile = open(out_file)
fh = csv.reader(tsvfile, delimiter="\t")
lengths = []
bitscores = []
count = 0
for line in fh:
if(line[0][0] != '#'):
cols = line[0].strip().split()
lengths.append(abs(int(cols[8])- int(cols[7])))
bitscores.append(float(cols[14]))
count+=1
tsvfile.close()
bitscore_mean = find_mean(bitscores)
length_mean = find_mean(lengths)
with gzip.open(f"{write_path}/{n}.gz", mode='wt') as fout:
fout.write(f"{bitscore_mean}\t{length_mean}\t{count}\n")
except Exception as err:
with open(err_file, 'a') as fout:
fout.write(f"{timestamp()} Error calculating rrna factor for file:{n} {err}\n")