TCGA临床数据的下载这里不做介绍。下载后解压数据会得到包含多个子文件夹的的文件,如下所示:
TCGA文件.png
每个子文件夹下通常包含XML文件,有的也会包含多个文件。但我们需要用到的就是XML文件,从中提取得到临床信息。XML文件内容如下图的例子所示:
xml.png
我们需要从每个子文件下面中的XML文件中提取临床信息,并合并到一起形成完整的临床数据。这里使用Python,代码如下:
import xml.etree.cElementTree as ET
import os
import re
import sys
sourceDir = sys.argv[1]
filepath = []
for dirName, subfolders, filenames in os.walk(sourceDir):
if len(filenames) > 1:
file_xml = [i for i in filenames if r'.xml' in i]
if file_xml:
filepath.append(os.path.join(dirName, *file_xml))
else:
filepath.append(os.path.join(dirName, *filenames))
def None2unknow(str):
if dict_clin[str]:
dict_clin[str][-1] = "unknow" if dict_clin[str][-1] is None else dict_clin[str][-1]
else:
dict_clin[str].append("unknow")
with open('result.txt', 'w') as F:
F.write("ID\tSurvival_time\tStatus\tRace\tGender\tAge\tGrade\tStage\tT\tN\tM\n")
for path in filepath:
tree = ET.ElementTree(file=path)
root = tree.getroot()
dict_clin = {"ID": [],"Survival_time": [],"Status": [],
"Race": [],"Gender": [],"Age": [],"Grade": [],
"Stage": [],"T": [],"N": [],"M": []}
for n in root.iter():
# basic information
if n.tag.endswith("bcr_patient_barcode"):
dict_clin["ID"].append(n.text)
if n.tag.endswith("days_to_last_followup"):
dict_clin["Survival_time"].append(n.text)
if n.tag.endswith("vital_status"):
dict_clin["Status"].append(n.text)
if n.tag.endswith("race"):
dict_clin["Race"].append(n.text)
if n.tag.endswith("gender"):
dict_clin["Gender"].append(n.text)
if n.tag.endswith("age_at_initial_pathologic_diagnosis"):
dict_clin["Age"].append(n.text)
if n.tag.endswith("neoplasm_histologic_grade"):
dict_clin["Grade"].append(n.text)
if n.tag.endswith("pathologic_stage"):
dict_clin["Stage"].append(n.text)
if n.tag.endswith("pathologic_T"):
dict_clin["T"].append(n.text)
if n.tag.endswith("pathologic_N"):
dict_clin["N"].append(n.text)
if n.tag.endswith("pathologic_M"):
dict_clin["M"].append(n.text)
for key in dict_clin.keys():
None2unknow(key)
F.write(dict_clin["ID"][-1]+"\t"+
dict_clin["Survival_time"][-1]+"\t"+
dict_clin["Status"][-1]+"\t"+
dict_clin["Race"][-1]+"\t"+
dict_clin["Gender"][-1]+"\t"+
dict_clin["Age"][-1]+"\t"+
dict_clin["Grade"][-1]+"\t"+
dict_clin["Stage"][-1]+"\t"+
dict_clin["T"][-1]+"\t"+
dict_clin["N"][-1]+"\t"+
dict_clin["M"][-1]+"\n")
代码说明:
1.代码考虑到可能存在空的子文件夹的情况。
2.对于缺失的临床数据,替换为unknow。
3.对于多次随访的临床数据,只取最后一次的随访数据。
注:XML数据中可以提取的信息很多,除了基本的性别、年龄、生存时间等,还包括手术信息,放、化疗信息,甚至还包括放疗的剂量等。然而这完全取决与数据的完整性,提取太多信息会存在很多缺失值,因此这里提取的信息就是基本的临床信息。
值得注意的是,TCGA的临床数据中包含初次诊断的信息以及随访信息。
通过观察XML文件,奥利给发现目前的临床数据有由初诊数据和两次随访数据构成,TCGA是一个不断更新的数据库,因此新的随访数据会不断被更新并加入到临床信息中。但是这里只提取最后一次的随访信息。
使用方法:
将代码复制保存为 .py结尾的文件,比如为get_clindata.py。将get_clindata.py文件复制到解压后的临床数据的子文件夹目录,如下图:
复制代码文件.png
在命令行(windows就是命令提示符模式)进入代码所在的文件路径
输入:python get_clindata.py 你的当前文件路径(即代码所在的文件路径)
比如:
python get_clindata.py /Users/mac/test_raw/py_code/py_project/gdc_download_20190703_022829.964065
回车即可在当前文件路径找到result.txt文件,打开如下图所示:
临床数据文件.png
至此,就完成了临床数据的提取。
但使龙城飞将在
不教胡马度阴山