forked from MaayanLab/milestones-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-ml-consolidated.py
261 lines (219 loc) · 8.28 KB
/
db-ml-consolidated.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
# coding=utf-8
import csv
from pymongo import MongoClient
from datetime import datetime
from dateutil.parser import parse
client = MongoClient('mongodb://username:[email protected]/Milestones')
db = client['Milestones']
md = db['data']
md.drop()
dInit = {}
mongoArr = []
with open('milestones-consolidated.txt', 'rU') as data:
reader = csv.reader(data, skipinitialspace=False, delimiter="\t")
mcArr = []
for row in reader:
if any(row):
mcArr.append(row)
for inp in mcArr:
if inp[0] and not (inp[0] == 'center'):
# print(inp)
dictTot = dInit.copy()
dictTot['center'] = inp[0]
assayDict = {}
assayDict['name'] = inp[1]
assayDict['info'] = inp[2]
dictTot['assay'] = assayDict
# cell-lines
allCellLines = []
clDictInit = {}
# Format of cell line cell (from Excel):
# name1,type1(cell line or iPSC differentiated),
# class1(normal,cancer line,…)\control-or-disease,
# tissue1;...
# Split by ; then , to build arr of objs
cellLineArr = inp[3].split(";")
for cLine in cellLineArr:
clDict = clDictInit.copy()
cLineData = cLine.split(",")
if cLineData[0]:
clDict['name'] = cLineData[0]
if len(cLineData) > 1:
if cLineData[1]:
clDict['type'] = cLineData[1]
if cLineData[2]:
# Check if \ in class
# ---> For diseased (not cancer) cell lines
if "\\" in cLineData[2]:
classArr = cLineData[2].split("\\")
clDict['class'] = classArr[0]
clDict['controlOrDisease'] = classArr[1]
else:
clDict['class'] = cLineData[2]
if cLineData[3]:
clDict['tissue'] = cLineData[3]
if clDict:
allCellLines.append(clDict)
if allCellLines:
dictTot['cellLines'] = allCellLines
# cell-lines-meta: count1,type1;count2,type2
# Split by ; then \ to build arr of objs
cLineMetaArr = []
cLineMetaDictInit = {}
cLineMetaData = inp[4].split(";")
for meta in cLineMetaData:
# print(meta)
cLineMetaDict = cLineMetaDictInit.copy()
cLineMeta = meta.split("\\")
if cLineMeta[0]:
cnt = cLineMeta[0]
cLineMetaDict['count'] = float(cnt) if '.' in cnt else int(cnt)
if cLineMeta[1]:
cLineMetaDict['type'] = cLineMeta[1]
if cLineMetaDict:
cLineMetaArr.append(cLineMetaDict)
if cLineMetaArr:
dictTot['cellLinesMeta'] = cLineMetaArr
# perturbagens: name1,type1(,perturbagens1);name2,type2(,purturbagens2)
# Split by ; then , to build arr of objs
pertArr = []
pertDictInit = {}
pertsAll = inp[5].split(";")
for perts in pertsAll:
pertDict = pertDictInit.copy()
pertData = perts.split(",")
if pertData[0]:
pertDict['name'] = pertData[0]
if len(pertData) > 1:
pertDict['type'] = pertData[1]
# Check if has perturbagens value
if len(pertData) == 3:
pertDict['perturbagens'] = pertData[2]
if pertDict:
pertArr.append(pertDict)
if pertArr:
dictTot['perturbagens'] = pertArr
# perturbagens-meta
pertMetaArr = []
pertMetaDict = {}
# pair:type1,count1;type2,count2
# Split by \ and get pair
# Then split [1] by ; and then , to build arr of objs
getPair = inp[6].split("\\")
if getPair[0] == "true":
pertMetaDict['pair'] = True
elif getPair[0] == "false":
pertMetaDict['pair'] = False
pertCountArr = []
pertCountDictInit = {}
if getPair[1]:
pertCountMeta = getPair[1].split(";")
print(pertCountMeta)
for countMeta in pertCountMeta:
print(countMeta)
countData = countMeta.split(",")
pertCountDict = pertCountDictInit.copy()
if countData[0]:
pertCountDict['type'] = countData[0]
if len(countData) > 1:
if countData[1]:
pertCountDict['count'] = int(countData[1])
if pertCountDict:
pertCountArr.append(pertCountDict)
if pertCountArr:
pertMetaDict['countType'] = pertCountArr
# dose1(,dose2,...);dose-count
doseAll = inp[7].split(";")
pertMetaDict['dose'] = doseAll[0].split(",")
if len(doseAll) > 1:
if doseAll[1]:
pertMetaDict['doseCount'] = int(doseAll[1])
# time1(,time2,...);time-unit;time-points
pertMetaTime = inp[8].split(";")
timeArr = pertMetaTime[0].split(",")
if timeArr:
for time in timeArr:
if time:
time = int(time)
pertMetaDict['time'] = timeArr
if len(pertMetaTime) > 1:
pertMetaDict['timeUnit'] = pertMetaTime[1]
pertMetaDict['timePoints'] = int(pertMetaTime[2])
if pertMetaDict:
dictTot['perturbagensMeta'] = pertMetaDict
# instance-meta: reps,tech-reps
instanceMetaDict = {}
reps = inp[9].split(",")
if reps[0]:
instanceMetaDict['reps'] = int(reps[0])
if len(reps) == 2:
instanceMetaDict['techReps'] = int(reps[1])
# instance-meta map: pert1,cell-line1;pert2,cell-line2
mapArr = []
mapDictInit = {}
# pprint(inp[10])
if inp[10] == 'one-all':
# print("TRUE")
# print(pertArr)
for pertObj in pertArr:
# pprint(pertObj)
if 'name' in pertObj:
for cLineObj in allCellLines:
mapDict = mapDictInit.copy()
mapDict['perturbagen'] = pertObj['name']
if 'name' in cLineObj:
mapDict['cellLine'] = cLineObj['name']
mapArr.append(mapDict)
else:
allMaps = inp[10].split(";")
for mapData in allMaps:
mapDict = mapDictInit.copy()
maps = mapData.split(",")
if len(maps) == 2:
mapDict['perturbagen'] = maps[0]
mapDict['cellLine'] = maps[1]
if mapDict:
mapArr.append(mapDict)
if mapArr:
instanceMetaDict['map'] = mapArr
if instanceMetaDict:
dictTot['instanceMeta'] = instanceMetaDict
# readouts -> name1\datatype1;name2\datatype2
readoutsArr = []
readoutsDictInit = {}
allReadouts = inp[11].split(";")
for readout in allReadouts:
readoutDict = readoutsDictInit.copy()
readoutData = readout.split("\\")
if readoutData[0]:
readoutDict['name'] = readoutData[0]
if len(readoutData) == 2:
readoutDict['datatype'] = readoutData[1]
if readoutDict:
readoutsArr.append(readoutDict)
if readoutsArr:
dictTot['readouts'] = readoutsArr
# readout-count
if inp[12]:
dictTot['readoutCount'] = inp[12]
# Data release dates
dateDict = {}
if inp[13]:
lvlOneDate = parse(inp[13] + ' -0400')
dateDict['levelOne'] = lvlOneDate
if inp[14]:
lvlTwoDate = parse(inp[14] + ' -0400')
dateDict['levelTwo'] = lvlTwoDate
if inp[15]:
lvlThreeDate = parse(inp[15] + ' -0400')
dateDict['levelThree'] = lvlThreeDate
if inp[16]:
lvlFourDate = parse(inp[16] + ' -0400')
dateDict['levelFour'] = lvlFourDate
if dateDict:
dictTot['releaseDates'] = dateDict
if inp[17]:
dictTot['releaseLink'] = inp[17]
md.insert(dictTot)
print(dictTot)
# print(dictTot)