-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
186 lines (163 loc) · 4.61 KB
/
util.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import operator
import random
import gzip
def printSortedMap(regularMap):
sortedMap = sorted(regularMap.iteritems(), key=operator.itemgetter(1))
for s in sortedMap:
print s[0] + '\t' + str(s[1])
def printSortedMapToFile(regularMap, fileName):
sortedMap = sorted(regularMap.iteritems(), key=operator.itemgetter(1))
f = open(fileName, 'w')
for s in sortedMap:
f.write(s[0] + '\t' + str(s[1]) + '\n')
f.close()
def printMapToFile(regularMap, fileName):
f = open(fileName, 'w')
for k, v in regularMap.iteritems():
f.write(k + '\t' + str(v) + '\n')
f.close()
def printMapWithList(mapWithList):
for key, lst in mapWithList.iteritems():
print key + '\t' + '\t'.join(str(x) for x in lst)
def printMapWithListToFile(mapWithList, fileName):
f = open(fileName, 'w')
for key, lst in mapWithList.iteritems():
# print key, len(lst)
f.write(key + '\t' + '\t'.join(str(x) for x in lst) + '\n')
f.close()
def print2DList(tdList):
for r in tdList:
cs = ''
for c in r:
cs += str(c) + '\t'
print cs
def printSnpAlleleCounts(s):
for snpId in s:
cs = ''
for j, v in s[snpId].iteritems():
if v > 0:
cs += j + ':' + str(v) + '\t'
print snpId, cs
def readMapWithListFromFile(fileName):
mapWithList = {}
for line in open(fileName, 'r'):
parts = line.replace('\n', '').split('\t')
key = parts[0]
elements = parts[1:]
mapWithList[key] = elements
return mapWithList
def readMapWithOneFloat(fileName, ignoreInvalid = True):
mapWithFloat = {}
for line in open(fileName, 'r'):
if len(line) == 0:
continue
parts = line.replace('\n', '').split('\t')
if len(parts) < 2:
print line
continue
if (parts[1] == 'NA' or parts[1] == 'NaN') and ignoreInvalid:
parts[1] = 0.0
#continue
mapWithFloat[parts[0].strip()] = float(parts[1])
return mapWithFloat
def readMapWithOneFloatGzip(fileName, ignoreInvalid = True):
mapWithFloat = {}
for line in gzip.open(fileName, 'r'):
if len(line) == 0:
continue
parts = line.replace('\n', '').split('\t')
if len(parts) < 2:
print line
continue
if (parts[1] == 'NA' or parts[1] == 'NaN') and ignoreInvalid:
parts[1] = 0.0
#continue
mapWithFloat[parts[0].strip()] = float(parts[1])
return mapWithFloat
def readListAsMapFromFile(fileName):
lst = {}
for line in open(fileName, 'r'):
lst[line.replace('\n', '')] = 1
return lst
def combineCounts(allSnpInfos):
combinedSnpInfo = {}
for snpInfo in allSnpInfos:
for snpId, counts in snpInfo.iteritems():
if snpId not in combinedSnpInfo:
combinedSnpInfo[snpId] = counts
else:
for c in counts:
combinedSnpInfo[snpId][c] += counts[c]
return combinedSnpInfo
def invertMap(originalMapWithList):
invertedMap = {}
for k, lst in originalMapWithList.iteritems():
for l in lst:
if l not in invertedMap:
invertedMap[l] = [k]
else:
invertedMap[l].append(k)
return invertedMap
def sortAndReturnIndex(lst):
newMap = {}
for i in range(len(lst)):
newMap[i] = lst[i]
sortedMap = sorted(newMap.iteritems(), key=operator.itemgetter(1))
sortedMapIndex = {}
for s in range(len(sortedMap)):
furthestIndex = s
for s2 in range(s, len(sortedMap)):
if sortedMap[s][1] == sortedMap[s2][1]:
furthestIndex = s2
else:
break
sortedMapIndex[sortedMap[s][0]] = furthestIndex
return sortedMapIndex
def sortAndReturnIndexBig(lst):
newMap = {}
for i in range(len(lst)):
newMap[i] = lst[i]
sortedMap = sorted(newMap.iteritems(), key=operator.itemgetter(1))
sortedMapIndex = {}
for s in range(len(sortedMap)):
furthestIndex = s
for s2 in reversed(range(0, s + 1)):
if sortedMap[s][1] == sortedMap[s2][1]:
furthestIndex = s2
else:
break
sortedMapIndex[sortedMap[s][0]] = furthestIndex
return sortedMapIndex
def getFriendlyPathwayName(pFull):
parts = pFull.split('%')
return (parts[0], parts[1], parts[2])
def getGroupNames(groupIds, expr):
res = []
eps = expr.split('.')
for g in groupIds:
gps = g.split('.')
assert(len(gps) == len(eps))
dontInclude = False
for i in range(len(gps)):
hasMatch = False
if eps[i] == 'X':
hasMatch = True
epsp = eps[i].split(',')
for epspi in epsp:
if epspi == gps[i]:
hasMatch = True
if not hasMatch:
dontInclude = True
break
if not dontInclude:
res.append(g)
return res
def shuffleValues(m):
vs = m.values()
random.shuffle(vs)
i = 0
rndM = {}
for k in m:
rndM[k] = vs[i]
i += 1
return rndM