forked from ymulyo/cancerSurvivalEstimate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalDataLung.py
155 lines (124 loc) · 4.65 KB
/
finalDataLung.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
import json
survivalPlot = []
biospecimenCases = []
clinCases = []
#Survival plot data extraction
with open('lung-survival-plot.json') as f:
survivalPlot = json.load(f)
projectIDsurvival = survivalPlot[0]["donors"]
for i in projectIDsurvival:
i.pop("project_id", None)
i.pop("censored", None)
i.pop("submitter_id", None)
projectSurvivalDict = {}
for i in projectIDsurvival:
thisID = i["id"]
tempDict = {}
for key in i:
if key != "id":
tempDict[key] = i[key]
projectSurvivalDict[thisID] = tempDict
#Biospecimen Data Extraction
with open('lung-biospecimen.json') as f:
biospecimenCases = json.load(f)
biospecDict = {}
wantedValues = ["sample_type", "intermediate_dimension", "shortest_dimension", "state", "tumor_code", "portions"]
for i in biospecimenCases:
tempDict = {}
found = False
index = 0
while(not(found) and index < len(i["samples"])):
curSample = i["samples"][index]
if curSample["sample_type_id"] == "01" and curSample["state"] == "live":
for j in wantedValues:
if j in curSample:
if j == "portions":
if "slides" in curSample[j][0]:
tempDict["slides"] = curSample[j][0]["slides"]
else:
tempDict["slides"] = None
else:
tempDict[j] = curSample[j]
else:
tempDict[j] = None
found = True
index += 1
biospecDict[i["case_id"]] = tempDict
#Clinical cases data extraction
with open('lung_clinical_cases.json') as f:
clinCases = json.load(f)
clinicalCases = {}
cid = ["case_id"]
treatments = ["treatment_or_therapy","treatment_intent_type","therapeutic_agents"]
demographic = ["gender","year_of_birth","race","ethnicity"]
exposures = ["cigarettes_per_day","weight","alcohol_history","alcohol_intensity","bmi","years_smoked"]
diagnoses = ["classification_of_tumor","last_known_disease_status","primary_diagnosis","tumor_stage","age_at_diagnosis","morphology","tissue_or_organ_of_origin","days_to_birth","progression_or_recurrence","prior_malignancy","site_of_resection_or_biopsy","days_to_last_follow_up"]
for i in range(0, len(clinCases)):
thisCase = {}
for j in range(0,len(demographic)):
thisCase[demographic[j]] = clinCases[i]["demographic"][demographic[j]]
for j in range(0,len(exposures)):
thisCase[exposures[j]]= clinCases[i]["exposures"][0][exposures[j]]
for j in range(0,len(diagnoses)):
thisCase[diagnoses[j]]= clinCases[i]["diagnoses"][0][diagnoses[j]]
for j in range(0,len(treatments)):
thisCase[treatments[j]]= clinCases[i]["diagnoses"][0]["treatments"][0][treatments[j]]
thisCase["submitter_id"] = clinCases[i]["demographic"]["submitter_id"].split("_")[0]
clinicalCases[clinCases[i]["case_id"]]= thisCase
#Merge them base on patient ID
mergeDataID = biospecDict
popID = []
for key in mergeDataID:
if key in projectSurvivalDict:
mergeDataID[key].update(projectSurvivalDict[key])
else:
popID.append(key)
for i in popID:
mergeDataID.pop(i,None)
popID = []
for key in mergeDataID:
if key in clinicalCases:
mergeDataID[key].update(clinicalCases[key])
else:
popID.append(key)
for i in popID:
mergeDataID.pop(i,None)
test = list(mergeDataID["14313474-376f-4606-9ed9-25ed2acff411"].values())
test2 = list(mergeDataID["14313474-376f-4606-9ed9-25ed2acff411"].keys())
#Added Mutations and Genes
from pandas import DataFrame
df = DataFrame.from_csv("lungData.tsv", sep="\t")
dataDict = df.to_dict()
temp = dataDict["Mutations"]
mutDict = {k: {"simple_somatic_mutations" : int(''.join(v.split(',')))} for k, v in temp.items()}
temp = dataDict["Genes"]
genesDict = {k: {"genes_with_simple_somatic_mutations" : int(''.join(v.split(',')))} for k, v in temp.items()}
mutGenes = mutDict
for i in mutGenes:
mutGenes[i].update(genesDict[i])
for i in mergeDataID:
curSubID = mergeDataID[i]["submitter_id"]
if curSubID in mutGenes:
mergeDataID[i].update(mutGenes[curSubID])
print(mergeDataID)
csvArray = []
first = []
totalList = []
for i in mergeDataID:
totalList.append([list(mergeDataID[i].keys()), list(mergeDataID[i].values())])
string1 = ','.join(totalList[0][0])
f = open("lungFinalData.csv", "w")
f.write(string1 + "\n")
tl = []
for i in range(0,len(totalList)):
l1 = []
for j in range(0, len(totalList[i][1])):
if j == 5:
l1.append(str(None))
else:
l1.append(str(totalList[i][1][j]))
tl.append(l1)
for i in range(0, len(tl)):
string2 = ','.join(tl[i])
f.write(string2 + "\n")
f.close