forked from ChrisLiu95/Cancer-prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_meta.py
199 lines (159 loc) · 5.28 KB
/
request_meta.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
import requests
import json
import pandas as pd
import os
file_fileds = []
case_fileds = []
def retrieveFileMeta(file_ids,outputfile):
'''
Get the tsv metadata for the list of case_ids
Args:
file_ids: numpy array of file_ids
outputfile: the output filename
'''
global file_fileds
fd = open(outputfile,'w')
cases_endpt = 'https://api.gdc.cancer.gov/files'
# The 'fields' parameter is passed as a comma-separated string of single names.
file_fileds = [
"file_id",
"file_name",
"cases.submitter_id",
"cases.demographic.ethnicity",
"cases.demographic.gender",
"cases.samples.tumor_descriptor",
"cases.samples.tissue_type",
"cases.samples.sample_type",
"cases.demographic.race",
"cases.demographic.state",
"cases.diagnoses.age_at_diagnosis",
"cases.exposures.alcohol_intensity",
"cases.exposures.cigarettes_per_day",
"cases.exposures.years_smoked",
"cases.project.primary_site"
]
filters = {
"op":"in",
"content":{
"field":"files.file_id",
"value": file_ids.tolist()
}
}
#print(filters)
file_fileds = ','.join(file_fileds)
params = {
"filters" : filters,
"fields": file_fileds,
"format": "TSV",
"pretty": "true",
"size": 3000
}
# print (params)
#print (filters)
#print (fields)
response = requests.post(cases_endpt, headers = {"Content-Type": "application/json"},json = params)
fd.write(response.content.decode("utf-8"))
fd.close()
# print(response.content)
def retrieveCaseMeta(file_ids,outputfile):
'''
Get the tsv metadata for the list of case_ids
Args:
file_ids: numpy array of file_ids
outputfile: the output filename
'''
fd = open(outputfile,'w')
cases_endpt = 'https://api.gdc.cancer.gov/cases'
filters = {
"op":"in",
"content":{
"field":"cases.case_id",
"value": file_ids.tolist()
}
}
# print (filters)
#expand group is diagnosis and demoragphic
params = {
"filters" : filters,
"expand" : "diagnoses,demographic,exposures",
"format": "TSV",
"pretty": "true",
"size": 1500
}
# print (params)
#print (filters)
#print (fields)
response = requests.post(cases_endpt, headers = {"Content-Type": "application/json"},json = params)
# print (response.content.decode("utf-8"))
fd.write(response.content.decode("utf-8"))
fd.close()
def genCasePayload(file_ids,payloadfile):
'''
Used for the curl method to generate the file payload.
'''
fd = open(payloadfile,"w")
filters = {
"filters":{
"op":"in",
"content":{
"field":"cases.case_id",
"value": file_ids.tolist()
}
},
"format":"TSV",
"expand" : "diagnoses,demographic,exposures",
"size": "1000",
"pretty": "true"
}
json_str = json.dumps(filters)
fd.write(json_str)
fd.close()
# return json_str
def genFilePayload(file_ids,payloadfile):
'''
Used for the curl method to generate the payload.
'''
fd = open(payloadfile,"w")
filters = {
"filters":{
"op":"in",
"content":{
"field":"files.file_id",
"value": file_ids.tolist()
}
},
"format":"TSV",
"fields":"file_id,file_name,cases.submitter_id,cases.case_id,data_category,data_type,cases.samples.tumor_descriptor,cases.samples.tissue_type,cases.samples.sample_type,cases.samples.submitter_id,cases.samples.sample_id,cases.samples.portions.analytes.aliquots.aliquot_id,cases.samples.portions.analytes.aliquots.submitter_id",
"pretty":"true",
"size": "1000"
}
json_str = json.dumps(filters)
fd.write(json_str)
fd.close()
def curlFileMeta(file_ids,payloadfile,outputfile):
genFilePayload(file_ids,payloadfile)
os.system("curl --request POST --header \"Content-Type: application/json\" --data @"+payloadfile+" 'https://api.gdc.cancer.gov/files' > "+outputfile)
def curlCaseMeta(case_ids,payloadfile,outputfile):
genCasePayload(case_ids,payloadfile)
os.system("curl --request POST --header \"Content-Type: application/json\" --data @"+payloadfile+" 'https://api.gdc.cancer.gov/cases' > "+outputfile)
if __name__ == '__main__':
data_dir = "/Users/Tony/Desktop/tmp/"
filename = data_dir+"file_case_OtherType.csv"
df = pd.read_csv(filename)
file_ids = df.file_id.values
case_ids = df.case_id.values
# print(case_ids)
fileids_meta_outfile = data_dir + "files_meta_OtherType.tsv"
# caseids_meta_outfile = data_dir + "cases_meta_Breast.tsv"
# python request method
retrieveFileMeta(file_ids,fileids_meta_outfile)
# retrieveCaseMeta(case_ids,caseids_meta_outfile)
# the curl method
'''
filepayload = "FilePayload"
casepayload = "CasePayload"
fileids_meta_outfile = "curl_files_meta.tsv"
caseids_meta_outfile = "curl_cases_meta.tsv"
curlFileMeta(file_ids,filepayload,fileids_meta_outfile)
curlCaseMeta(case_ids,casepayload,caseids_meta_outfile)
'''