annotate collapseTab.py @ 12:2f4ea569f048

Uploaded
author xuebing
date Sat, 10 Mar 2012 08:10:44 -0500
parents b7f1d9f8f3bc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
1 '''
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
2 collapse tabular files, with key columns, and max columns
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
3 '''
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
4
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
5 def collapseTab(filename,c_key,c_max):
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
6 # keeping rows with max value in column c_max
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
7 nCol = max(max(c_key),c_max)
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
8 c_max = c_max - 1
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
9 for i in range(len(c_key)):
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
10 c_key[i] = c_key[i] - 1
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
11 uniqintv = {}
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
12 data = {}
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
13 f = open(filename)
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
14 for line in f:
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
15 flds = line.strip().split('\t')
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
16 if len(flds) < nCol:
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
17 continue
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
18 key = ''
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
19 for i in c_key:
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
20 key = key + flds[i-1] # i is 1-based, python is 0-based
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
21 if not uniqintv.has_key(key):
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
22 uniqintv[key] = float(flds[c_max])
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
23 data[key] = flds
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
24 elif uniqintv[key] < float(flds[c_max]):
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
25 uniqintv[key] = float(flds[c_max])
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
26 data[key] = flds
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
27
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
28 f.close()
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
29 for key in uniqintv.keys():
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
30 print '\t'.join(data[key])
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
31
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
32 import sys
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
33
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
34 # convert string to number list
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
35 c_key = map(int,sys.argv[2].split(','))
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
36 c_max = int(sys.argv[3])
b7f1d9f8f3bc Uploaded
xuebing
parents:
diff changeset
37 collapseTab(sys.argv[1],c_key,c_max)