annotate collapseBed.py @ 22:869c7664e584

Uploaded
author xuebing
date Sat, 31 Mar 2012 11:52:14 -0400
parents 16ba480adf96
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
1 '''
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
2 collapse intervals
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
3 '''
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
4
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
5 def collapseInterval_strand(filename):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
6 uniqintv = {}
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
7 data = {}
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
8 f = open(filename)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
9 header = f.readline()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
10 if 'chr' in header:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
11 flds = header.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
12 key = '\t'.join([flds[0],flds[1],flds[2],flds[5]])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
13 uniqintv[key] = 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
14 data[key] = flds
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
15 for line in f:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
16 flds = line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
17 key = '\t'.join([flds[0],flds[1],flds[2],flds[5]])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
18 if uniqintv.has_key(key):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
19 uniqintv[key] = uniqintv[key] + 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
20 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
21 uniqintv[key] = 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
22 data[key] = flds
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
23 f.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
24 for key in uniqintv.keys():
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
25 print '\t'.join(data[key]+[str(uniqintv[key])])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
26 #flds = key.split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
27 #print '\t'.join([flds[0],flds[1],flds[2],'.',str(uniqintv[key]),flds[3]])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
28
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
29 def collapseInterval(filename):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
30 uniqintv = {}
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
31 data = {}
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
32 f = open(filename)
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
33 header = f.readline()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
34 if 'chr' in header:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
35 flds = header.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
36 key = '\t'.join([flds[0],flds[1],flds[2]])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
37 uniqintv[key] = 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
38 data[key] = flds
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
39 for line in f:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
40 flds = line.strip().split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
41 key = '\t'.join([flds[0],flds[1],flds[2]])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
42 if uniqintv.has_key(key):
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
43 uniqintv[key] = uniqintv[key] + 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
44 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
45 uniqintv[key] = 1
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
46 data[key] = flds
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
47 f.close()
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
48 for key in uniqintv.keys():
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
49 print '\t'.join(data[key]+[str(uniqintv[key])])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
50 #flds = key.split('\t')
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
51 #print '\t'.join([flds[0],flds[1],flds[2],'.',str(uniqintv[key])])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
52
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
53 import sys
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
54
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
55 if sys.argv[2] == 'strand':
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
56 collapseInterval_strand(sys.argv[1])
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
57 else:
16ba480adf96 Uploaded
xuebing
parents:
diff changeset
58 collapseInterval(sys.argv[1])