forked from eisenlab/SliceSeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountReadsPerGene.py
68 lines (58 loc) · 1.76 KB
/
CountReadsPerGene.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
62
63
64
65
66
from sys import stdin, stdout, argv
from collections import defaultdict, Counter
from os import path
import cPickle as pickle
import subprocess
import os
import re
chrs = defaultdict(lambda : defaultdict(list))
fbgn_finder = re.compile('FBgn[0-9]+')
fbtr_finder = re.compile('FBtr[0-9]+')
fbtr_to_fbgn = {}
for line in open(argv[1]):
try:
data = line.split('\t')
chr = data[0]
kind = data[2]
if fbgn_finder.findall(line) and fbtr_finder.findall(line):
fbtr_to_fbgn[fbtr_finder.findall(line)[0]] = \
fbgn_finder.findall(line)[0]
if kind != 'exon': continue
start = int(data[3])
stop = int(data[4])
try:
fbgn = fbtr_to_fbgn[fbtr_finder.findall(line)[0]]
except KeyError:
fbgn = 'ERR'
if chr not in chrs:
print chr
for i in range(start, stop+1):
if fbgn not in chrs[chr][i]:
chrs[chr][i].append(fbgn)
except Exception as err:
print err
genes = Counter()
for dir in os.listdir('.'):
if not path.isdir(dir): continue
print '-' * 30
print dir
print '-' * 30
samtools = subprocess.Popen(['samtools', 'view',
path.join(dir, 'accepted_hits.bam')],
stdout=subprocess.PIPE)
for i, line in enumerate(samtools.stdout):
data = line.split()
chr = data[2]
pos = int(data[3])
try:
for gene in chrs[chr][pos]:
genes[gene] += 1
except KeyError:
genes['OTHER'] += 1
if i % 1e6 == 0:
print '.',
stdout.flush()
outfh = open(dir + '_coverage.pkl', 'w')
pickle.dump(genes, outfh)
print
print 'Done Dumping'