forked from bdo311/chirpseq-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeBedgraphSmaller.py
executable file
·59 lines (50 loc) · 1.5 KB
/
makeBedgraphSmaller.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
# makeBedgraphSmaller.py
# 8/5/14
# compresses a bedgraph by making the step size >= a certain threshold
import csv, sys
csv.register_dialect("textdialect", delimiter='\t')
def main():
ifn, ofn = sys.argv[1], sys.argv[2]
thresh = int(sys.argv[3])
ifile = open(ifn, 'r')
reader = csv.reader(ifile, 'textdialect')
ofile = open(ofn, 'w')
writer = csv.writer(ofile, 'textdialect')
# variables that carry forward
currStart = 0
currLength = 0
currTotal = 0
# fencepost first row
row = reader.next()
newTranscript = True
regionStart, regionEnd = int(row[1]), int(row[2])
regionLength = regionEnd - regionStart
regionTotal = float(row[3]) * regionLength
if regionLength >= thresh: writer.writerow(row)
else:
currStart = regionStart
currLength = regionLength
currTotal = regionTotal
newTranscript = False
# all other rows
for row in reader:
regionStart, regionEnd = int(row[1]), int(row[2])
regionLength = regionEnd - regionStart
regionTotal = float(row[3]) * regionLength
if newTranscript:
if regionLength >= thresh: writer.writerow(row)
else:
currStart = regionStart
currLength = regionLength
currTotal = regionTotal
newTranscript = False
else:
currLength = regionEnd - currStart
currTotal = currTotal + regionTotal
if currLength >= thresh:
writer.writerow([row[0], currStart, regionEnd, currTotal/currLength])
newTranscript = True
ifile.close()
ofile.close()
if __name__ == '__main__':
main()