forked from bdo311/chirpseq-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountMappedReads.py
50 lines (40 loc) · 1.28 KB
/
countMappedReads.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
# countMappedReads.py
# 12/13/14
import csv, os, sys, multiprocessing, glob
csv.register_dialect("textdialect", delimiter='\t')
if len(sys.argv)<3:
print "Usage: python countMappedReads.py <config file> <output folder>"
exit()
conf = sys.argv[1]
ofolder = sys.argv[2]
if not glob.glob(ofolder + '/'): os.mkdir(ofolder)
def getStats(fn):
basename = ofolder + '/' + os.path.basename(fn).split('.')[0] + '_stat.txt'
print "samtools flagstat " + fn + " > " + basename
os.system("samtools flagstat " + fn + " > " + basename)
def main():
# read list of files that need to be flagstatted
files = []
ifile = open(conf, 'r')
reader = csv.reader(ifile, 'textdialect')
for row in reader:
files.append(row[0])
ifile.close()
# samtools
p = multiprocessing.Pool(len(files))
p.map(getStats, files)
# read into output file
ofile = open("allstats.txt", 'w')
writer = csv.writer(ofile, 'textdialect')
statfiles = glob.glob("*_stat.txt")
for fn in statfiles:
ifile = open(fn, 'r')
total = ifile.readline().split(' ')[0]
ifile.readline()
mapped = ifile.readline().split(' ')[0]
ifile.close()
name = fn[:-9]
writer.writerow([name, total, mapped, float(mapped)/float(total) * 100])
ofile.close()
if __name__ == "__main__":
main()