TCGA臨床數(shù)據(jù)的提取

TCGA臨床數(shù)據(jù)的下載這里不做介紹。下載后解壓數(shù)據(jù)會得到包含多個子文件夾的的文件,如下所示:


TCGA文件.png

每個子文件夾下通常包含XML文件,有的也會包含多個文件。但我們需要用到的就是XML文件,從中提取得到臨床信息。XML文件內(nèi)容如下圖的例子所示:

xml.png

我們需要從每個子文件下面中的XML文件中提取臨床信息,并合并到一起形成完整的臨床數(shù)據(jù)。這里使用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.對于缺失的臨床數(shù)據(jù),替換為unknow。
3.對于多次隨訪的臨床數(shù)據(jù),只取最后一次的隨訪數(shù)據(jù)。
注:XML數(shù)據(jù)中可以提取的信息很多,除了基本的性別、年齡、生存時間等,還包括手術(shù)信息,放、化療信息,甚至還包括放療的劑量等。然而這完全取決與數(shù)據(jù)的完整性,提取太多信息會存在很多缺失值,因此這里提取的信息就是基本的臨床信息。

值得注意的是,TCGA的臨床數(shù)據(jù)中包含初次診斷的信息以及隨訪信息。
通過觀察XML文件,奧利給發(fā)現(xiàn)目前的臨床數(shù)據(jù)有由初診數(shù)據(jù)和兩次隨訪數(shù)據(jù)構(gòu)成,TCGA是一個不斷更新的數(shù)據(jù)庫,因此新的隨訪數(shù)據(jù)會不斷被更新并加入到臨床信息中。但是這里只提取最后一次的隨訪信息。

使用方法:

將代碼復(fù)制保存為 .py結(jié)尾的文件,比如為get_clindata.py。將get_clindata.py文件復(fù)制到解壓后的臨床數(shù)據(jù)的子文件夾目錄,如下圖:


復(fù)制代碼文件.png

在命令行(windows就是命令提示符模式)進(jìn)入代碼所在的文件路徑
輸入:python get_clindata.py 你的當(dāng)前文件路徑(即代碼所在的文件路徑)

比如:

python get_clindata.py /Users/mac/test_raw/py_code/py_project/gdc_download_20190703_022829.964065

回車即可在當(dāng)前文件路徑找到result.txt文件,打開如下圖所示:


臨床數(shù)據(jù)文件.png

至此,就完成了臨床數(shù)據(jù)的提取。

但使龍城飛將在
不教胡馬度陰山
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容