annotate mytools/collapseBed.py @ 9:87eb5c5ddfe9

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