annotate mytools/collapseBed.py @ 0:39217fa39ff2

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