-
Notifications
You must be signed in to change notification settings - Fork 0
/
geneExpressionValidation.py
358 lines (319 loc) · 13.1 KB
/
geneExpressionValidation.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import os
import logging
import hashlib
import requests
import json
import subprocess
import pandas
import numpy
from concurrent.futures import ThreadPoolExecutor
import warnings
warnings.filterwarnings("ignore")
logger = logging.getLogger(__name__)
def md5sum(filePath):
md5_hash = hashlib.md5()
with open(filePath, "rb") as file:
for chunk in iter(lambda: file.read(4096), b""):
md5_hash.update(chunk)
return md5_hash.hexdigest()
def existing_md5sums(logger, projectName, dataType, sampleDict):
existingFiles = [file for file in os.listdir(f"gdcFiles/{projectName}/STAR") if not file.startswith(".")]
existingMd5sumFileDict = {md5sum(f"gdcFiles/{projectName}/STAR/{file}"): file for file in existingFiles}
gdcMd5sumFileDict = {sampleDict[sample][fileID]["md5sum"]: fileID for sample in sampleDict for fileID in sampleDict[sample]}
logger.info(f"{len(gdcMd5sumFileDict)} files found from the GDC for {dataType} data for {projectName}")
logger.info(f"{len(existingMd5sumFileDict)} files found at gdcFiles/{projectName}/STAR")
fileIdDict = {innerKey: value
for outerDict in sampleDict.values()
for innerKey, value in outerDict.items()}
filesNeededToUpdate = {gdcMd5sumFileDict[md5sum]: fileIdDict[gdcMd5sumFileDict[md5sum]]["fileName"] for md5sum in gdcMd5sumFileDict if md5sum not in existingMd5sumFileDict}
logger.info(f"{len(filesNeededToUpdate)} files needed to update")
return filesNeededToUpdate
def custom_round(chunk):
for col in chunk:
chunk[col] = chunk[col].apply(lambda x: numpy.format_float_scientific(x, precision=8) if pandas.notna(x) else numpy.nan)
return chunk
def round_ForNans(x):
if( pandas.notna(x) ):
return numpy.format_float_scientific(x, precision=8)
else:
return numpy.nan
def downloadFiles(fileList, projectName):
if isinstance(fileList, list):
ids = fileList
elif isinstance(fileList, dict):
ids = list(fileList.keys())
jsonPayload = {
"ids": ids
}
with open("payload.txt", "w") as payloadFile:
payloadFile.write(str(jsonPayload).replace("\'", "\""))
logger.info("Downloading from GDC: ")
outputDir = f"gdcFiles/{projectName}/STAR"
os.makedirs(outputDir, exist_ok=True)
curlCommand = [
"curl", "--request", "POST", "--header", "Content-Type: application/json",
"--data", "@payload.txt", "https://api.gdc.cancer.gov/data"
]
if len(fileList) != 1:
outputFile = "gdcFiles.tar.gz"
curlCommand.extend(["-o", outputFile])
subprocess.run(curlCommand)
os.system(f"tar --strip-components=1 -xzf gdcFiles.tar.gz -C {outputDir}")
else:
outputFile = f"{outputDir}/{list(fileList.values())[0]}"
curlCommand.extend(["-o", outputFile])
subprocess.run(curlCommand)
def getXenaSamples(xenaFile): # get all samples from the xena matrix
with open(xenaFile, "r") as xenaData:
header = xenaData.readline() # get column labels from xena matrix
sampleList = header.split("\t") # split tsv file into list
sampleList.pop(0) # remove unnecessary label
sampleList = [sample.strip() for sample in sampleList] # make sure there isn't extra whitespace
return sampleList
def getAllSamples(projectName):
casesEndpt = "https://api.gdc.cancer.gov/cases"
allSamplesFilter = {
"op": "and",
"content": [
{
"op": "in",
"content": {
"field": "cases.project.project_id",
"value": [
projectName
]
}
},
{
"op": "in",
"content": {
"field": "files.analysis.workflow_type",
"value": [
"STAR - Counts"
]
}
},
{
"op": "in",
"content": {
"field": "files.data_category",
"value": [
"Transcriptome Profiling"
]
}
},
{
"op": "in",
"content": {
"field": "files.data_type",
"value": [
"Gene Expression Quantification"
]
}
},
{
"op": "in",
"content": {
"field": "files.experimental_strategy",
"value": [
"RNA-Seq"
]
}
}
]
}
params = {
"filters": json.dumps(allSamplesFilter),
"fields": "submitter_sample_ids",
"format": "json",
"size": 20000
}
response = requests.post(casesEndpt, json=params, headers={"Content-Type": "application/json"})
responseJson = unpeelJson(response.json())
allSamples = []
for caseDict in responseJson:
for sample in caseDict["submitter_sample_ids"]:
allSamples.append(sample)
return allSamples
def unpeelJson(jsonObj):
jsonObj = jsonObj.get("data").get("hits")
return jsonObj
def dataTypeSamples(samples, projectName):
filesEndpt = "https://api.gdc.cancer.gov/files"
dataTypeFilter = {
"op": "and",
"content": [
{
"op": "in",
"content": {
"field": "cases.project.project_id",
"value": [
projectName
]
}
},
{
"op": "in",
"content": {
"field": "analysis.workflow_type",
"value": [
'STAR - Counts'
]
}
},
{
"op": "in",
"content": {
"field": "data_category",
"value": "Transcriptome Profiling"
}
},
{
"op": "in",
"content": {
"field": "data_type",
"value": [
"Gene Expression Quantification"
]
}
},
{
"op": "in",
"content": {
"field": "experimental_strategy",
"value": [
"RNA-Seq"
]
}
},
{
"op": "in",
"content": {
"field": "cases.samples.submitter_id",
"value": samples
}
}
]
}
params = {
"filters": json.dumps(dataTypeFilter),
"fields": "cases.samples.submitter_id,file_id,file_name,cases.samples.tissue_type,md5sum",
"format": "json",
"size": 20000
}
response = requests.post(filesEndpt, json=params, headers={"Content-Type": "application/json"})
responseJson = unpeelJson(response.json())
dataTypeDict = {}
uniqueSamples = []
for caseDict in responseJson:
for sample in caseDict["cases"][0]["samples"]:
sampleName = sample["submitter_id"]
if sampleName in dataTypeDict:
dataTypeDict[sampleName][caseDict["file_id"]] = {"fileName": caseDict["file_name"],
"md5sum": caseDict["md5sum"]
}
else:
dataTypeDict[sampleName] = {}
dataTypeDict[sampleName][caseDict["file_id"]] = {"fileName": caseDict["file_name"],
"md5sum": caseDict["md5sum"]
}
uniqueSamples.append(sampleName)
return dataTypeDict, uniqueSamples
def xenaDataframe(xenaFile):
xenaDF = pandas.read_csv(xenaFile, sep="\t", index_col=0)
splitDF = numpy.array_split(xenaDF.columns, 32)
tasks = [xenaDF[i] for i in splitDF]
with ThreadPoolExecutor() as executor:
result = executor.map(custom_round, tasks)
resultDF = pandas.concat(result, axis=1)
return resultDF
def compare(logger, dataColumn, sampleDict, xenaDF, projectName):
samplesCorrect = 0
failed = []
sampleNum = 1
total = len(sampleDict)
for sample in sampleDict:
fileCount = 0
for fileID in sampleDict[sample]:
fileName = sampleDict[sample][fileID]["fileName"]
sampleFile = "gdcFiles/{}/STAR/{}".format(projectName, fileName)
if fileCount == 0:
sampleDataDF = pandas.read_csv(sampleFile, sep="\t", skiprows=1)
sampleDataDF = sampleDataDF.drop(sampleDataDF.index[:4])
sampleDataDF.reset_index(inplace=True, drop=True)
sampleDataDF["nonNanCount"] = 0
sampleDataDF["nonNanCount"] = sampleDataDF.apply(
lambda x: 1 if (not (pandas.isna(x[dataColumn]))) else 0, axis=1)
sampleDataDF[dataColumn] = sampleDataDF[dataColumn].fillna(0)
else:
tempDF = pandas.read_csv(sampleFile, sep="\t", skiprows=1)
tempDF = tempDF.drop(tempDF.index[:4])
tempDF.reset_index(inplace=True, drop=True)
tempDF["nonNanCount"] = 0
tempDF["nonNanCount"] = tempDF.apply(lambda x: 1 if (not (pandas.isna(x[dataColumn]))) else 0,
axis=1)
tempDF[dataColumn] = tempDF[dataColumn].fillna(0)
sampleDataDF["nonNanCount"] += tempDF["nonNanCount"]
sampleDataDF[dataColumn] += tempDF[dataColumn]
fileCount += 1
sampleDataDF[dataColumn] = sampleDataDF[dataColumn].astype(float)
sampleDataDF[dataColumn] = sampleDataDF.apply(lambda x: x[dataColumn]/x["nonNanCount"] if x["nonNanCount"] != 0 else numpy.nan, axis=1)
sampleDataDF[dataColumn] = numpy.log2(sampleDataDF[dataColumn] + 1)
vectorRound = numpy.vectorize(round_ForNans)
roundedSeries = vectorRound(sampleDataDF[dataColumn])
sampleDataDF[dataColumn] = roundedSeries
#print(sampleDataDF[dataType])
#print(xenaDF[sample])
# pool = multiprocessing.Pool()
# sampleDataDF[dataType] = pool.map(round_ForNans, sampleDataDF[dataType])
sampleDataDF[dataColumn].reset_index(drop=True, inplace=True)
xenaDF[sample].reset_index(drop=True, inplace=True)
xenaColumn = xenaDF[sample]
sampleColumn = sampleDataDF[dataColumn]
if( xenaColumn.equals(sampleColumn)):
status = "[{:d}/{:d}] Sample: {} - Passed"
logger.info(status.format(sampleNum, total, sample))
samplesCorrect += 1
else:
status = "[{:d}/{:d}] Sample: {} - Failed"
logger.info(status.format(sampleNum, total, sample))
failed.append('{} ({})'.format(sample, sampleNum))
sampleNum += 1
return failed
def main(dataType, xenaFilePath, projectName):
logger.info("Testing [{}] data for [{}].".format(dataType, projectName))
dataTypeDict = {
"star_fpkm": "fpkm_unstranded",
"star_fpkm-uq": "fpkm_uq_unstranded",
"star_tpm": "tpm_unstranded",
"star_counts": "unstranded"
}
dataColumn = dataTypeDict[dataType]
xenaSamples = getXenaSamples(xenaFilePath)
allSamples = getAllSamples(projectName)
sampleDict, uniqueSamples = dataTypeSamples(allSamples, projectName)
xenaDF = xenaDataframe(xenaFilePath)
if sorted(uniqueSamples) != sorted(xenaSamples):
logger.info("ERROR: Samples retrieved from the GDC do not match those found in Xena matrix.")
logger.info(f"Number of samples from the GDC: {len(uniqueSamples)}")
logger.info(f"Number of samples in Xena matrix: {len(xenaSamples)}")
logger.info(f"Samples from GDC and not in Xena: {[x for x in uniqueSamples if x not in xenaSamples]}")
logger.info(f"Samples from Xena and not in GDC: {[x for x in xenaSamples if x not in uniqueSamples]}")
exit(1)
if os.path.isdir(f"gdcFiles/{projectName}/STAR"):
fileIDs = existing_md5sums(logger, projectName, dataType, sampleDict)
else:
fileIDs = [fileID for sample in sampleDict for fileID in sampleDict[sample]]
logger.info(f"{len(fileIDs)} files found from the GDC for {dataType} data for {projectName}")
logger.info(f"0 files found at gdcFiles/{projectName}/STAR")
logger.info(f"{len(fileIDs)} files needed to download")
if len(fileIDs) != 0:
downloadFiles(fileIDs, projectName)
result = compare(logger, dataColumn, sampleDict, xenaDF, projectName)
if len(result) == 0:
logger.info("[{}] test passed for [{}].".format(dataType, projectName))
return 'PASSED'
else:
logger.info("[{}] test failed for [{}].".format(dataType, projectName))
logger.info("Samples failed: {}".format(result))
return 'FAILED'