-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCdata.py
294 lines (268 loc) · 10.3 KB
/
TCdata.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# TCdata Class
import csv
import numpy as np
import matplotlib.pyplot as plt
class TCdata:
data = []
def __init__(self, f, n, challenge):
self.challenge = challenge
self.numGs = 0
self.t = []
self.numTPs = 0
self.numPTs = 0
self.numPos = 0
self.numNeg = 0
self.TC = self.loadData(f)
self.network = self.loadNetwork(n)
# INTERNAL CLASS FUNCTIONS
def loadData(self, f):
# assumes time pt is first number in row remaining columns are gene data
t = []
firstRow = True
firstPT = True
if (self.challenge == 'D3'):
lastTP = '200'
if (self.challenge == 'D4'):
lastTP = '1000'
numGs = 0
numTPs = 0
numPTs = 0
data = {}
with open(f, 'r') as file:
reader = csv.reader(file, delimiter='\t')
for row in reader:
if row[0]!='':
if firstRow:
numGs = len(row[1:])
Gs = row[1:]
data = dict((el, []) for el in Gs)
keys = list(data.keys())
currData = [[] for k in range(numGs)]
firstRow = False
else:
if firstPT:
t.append(int(row[0]))
numTPs = numTPs + 1
if (row[0]=='0'):
numPTs = numPTs + 1
currData = [[] for k in range(numGs)]
for i, v in enumerate(row[1:]):
currData[i].append(float(v))
else:
if (row[0] != lastTP):
for i, v in enumerate(row[1:]):
currData[i].append(float(v))
else:
firstPT = False
for i, v in enumerate(row[1:]):
currData[i].append(float(v))
for i, v in enumerate(currData):
data[keys[i]].append(v)
else:
if (row[0]=='0'):
numPTs = numPTs + 1
currData = [[] for k in range(numGs)]
for i, v in enumerate(row[1:]):
currData[i].append(float(v))
else:
if (row[0] != lastTP):
for i, v in enumerate(row[1:]):
currData[i].append(float(v))
else:
for i, v in enumerate(row[1:]):
currData[i].append(float(v))
for i, v in enumerate(currData):
data[keys[i]].append(v)
self.setTime(t)
self.setGs(numGs)
self.setPTs(numPTs)
self.setTPs(numTPs)
self.setTime(t)
return data
def loadNetwork(self,n):
labels = {'+': 1, '-': 2}
nwk = np.zeros((self.numGs,self.numGs),dtype='int')
with open(n, 'r') as file:
reader = csv.reader(file, delimiter='\t')
for row in reader:
r = int(row[0].split("G")[1])-1
c = int(row[1].split("G")[1])-1
if row[2].isdigit():
l = row[2]
else:
l = labels[row[2]]
nwk[r, c] = l
unique, counts = np.unique(nwk, return_counts=True)
edgeCnts = dict(zip(unique, counts))
self.setnumPos(edgeCnts[1])
if len(edgeCnts) > 2:
self.setnumNeg(edgeCnts[2])
return nwk
def setTime(self, t):
self.t = t
return
def setTPs(self, TPs):
self.numTPs = TPs
return
def setPTs(self, PTs):
# numPTs = int(len(self.TC)/self.numTPs)
self.numPTs = PTs
return
def setGs(self, G):
self.numGs = G
return
def setnumPos(self, num):
self.numPos = num
return
def setnumNeg(self, num):
self.numNeg = num
return
def getTPstart(self, PT):
return self.numTPs * (PT-1)
def getTPstop(self, PT):
start = self.getTPstart(PT)
return (start + self.numTPs) - 1
def getPT(self, keys, R, T, PT,dim):
# initialize variables
info = []
timec = []
label = 0
rec = []
# generate list of info
info.append(keys[R])
info.append(keys[T])
info.append(PT+1)
# create 1-D timecourse (numpy array)
Rtc = np.array(self.TC[keys[R]][PT],dtype='float')
Ttc = np.array(self.TC[keys[T]][PT],dtype='float')
if dim ==1:
timec = np.concatenate((Rtc, Ttc))
else:
timec = np.vstack((Rtc, Ttc))
# timec.extend(self.TC[keys[T]][PT])
# print('TC: {}'.format(timec))
label = [0 if self.network[R][T]==0 else 1]
rec = [info, timec, label]
return rec
def flipPT(self, keys, R, T, PT, dim):
info = []
timec = []
label = 0
rec = []
info.append(keys[T])
info.append(keys[R])
info.append(PT+1)
Rtc = np.array(self.TC[keys[R]][PT],dtype='float')
Ttc = np.array(self.TC[keys[T]][PT],dtype='float')
if dim == 1:
timec = np.concatenate((Ttc, Rtc))
else:
timec = np.vstack((Rtc, Ttc))
# timec.extend(self.TC[keys[T]][PT])
# print('TC: {}'.format(timec))
label = [0 if self.network[R][T]==0 else 2]
rec = [info, timec, label]
return rec
# EXTERNAL FUNCTIONS
def get2TCwLabels(self, dim, bidir, PTs):
# using network and TC output file for all gene pairs
# dim = 1, then 1 x (#tps*2) vector created with label
# dim = 3, then 2 x #tps vector created with label
# bidir = False then only R-T recs created and labels are 0/1
# bidir = True then R-T and T-R recs are created and labels are 0/1/2
# PTs is a vector that indicates what perturbations (integers) to
# extract. if empty, then all PTs will be extracted
# generate R-T and T-R recs labels = 0, 1, 2
# Get data from dataframe
keys = list(self.TC.keys())
data = []
if (dim==1):
if bidir:
# print('Dim 1 T-R & R-T')
for R in range(self.numGs):
for T in range(self.numGs):
if R!=T:
if PTs:
for PT in PTs:
data.append(self.getPT(keys,R,T,PT-1,dim))
data.append(self.flipPT(keys,R,T,PT-1,dim))
else:
for PT in range(self.numPTs):
data.append(self.getPT(keys,R,T,PT,dim))
data.append(self.flipPT(keys,R,T,PT,dim))
return data
else:
# print('Dim 1 T-R only')
for R in range(self.numGs):
for T in range(self.numGs):
if R!=T:
if PTs:
for PT in PTs:
data.append(self.getPT(keys,R,T,PT-1,dim))
else:
for PT in range(self.numPTs):
data.append(self.getPT(keys,R,T,PT,dim))
return data
elif (dim ==2):
if bidir:
# print('Dim 2 T-R & R-T')
for R in range(self.numGs):
for T in range(self.numGs):
if R!=T:
if PTs:
for PT in PTs:
data.append(self.getPT(keys,R,T,PT-1,dim))
data.append(self.flipPT(keys,R,T,PT-1,dim))
else:
for PT in range(self.numPTs):
data.append(self.getPT(keys,R,T,PT,dim))
data.append(self.flipPT(keys,R,T,PT,dim))
else:
# print('Dim 2 T-R only')
for R in range(self.numGs):
for T in range(self.numGs):
if R!=T:
if PTs:
for PT in PTs:
data.append(self.getPT(keys,R,T,PT-1,dim))
else:
for PT in range(self.numPTs):
data.append(self.getPT(keys,R,T,PT,dim))
return data
else:
print("Invalid value for dim: choose only 1 or 2")
def extractData(self, Genes):
if not Genes:
data = self.TC
else:
data = {x:self.TC[x] for x in Genes}
return data
def extractData2(self, PTs):
# extractData2 returns a dictionary with 1 record / gene / PT
# keys in dictionary are of the form G[gene #]-[perturbation #]
data = self.extractData([])
newdata = {}
if not PTs:
for k in data:
for i in range(self.numPTs):
newkey = k + '-' + str(i+1)
newdata[newkey] = data[k][i]
else:
for k in data:
for p in PTs:
newkey = k + '-' + str(p)
newdata[newkey] = data[k][p-1]
return newdata
def plotData(self, TC):
leg = []
for d in TC:
tc = np.array(TC[d],dtype=float)
for i, e in enumerate(tc):
leg.append(d+'-P'+str(i+1))
plt.plot(self.t,e)
plt.title('Gene Expression Patterns')
plt.xlabel('Time (units)')
plt.ylabel('Normalized Expression')
if len(leg) < 21:
plt.legend(leg)
plt.show()