美文网首页TCGA学习Tcga
TCGA数据库挖掘(1)TCGA准备

TCGA数据库挖掘(1)TCGA准备

作者: 呆呱呱 | 来源:发表于2021-03-23 19:50 被阅读0次

    TCGA workfolw

    image.png image.png

    RMD格式

    image.png
    image.png

    image.png image.png

    R包安装

    options("repos" = c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
    if(!require("BiocManager")) install.packages("BiocManager",update = F,ask = F)
    
    options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
    
    cran_packages <- c('tidyr',
                       'tibble',
                       'dplyr',
                       'stringr',
                       'ggplot2',
                       'ggpubr',
                       'factoextra',
                       'FactoMineR',
                       'pheatmap',
                       "survival",
                       "survminer",
                       "patchwork",
                       "ggstatsplot",
                       "ggplotify",
                       "cowplot",
                       "glmnet",
                       "ROCR",
                       "caret",
                       "randomForest",
                       "survminer",
                       "Hmisc",
                       "e1071",
                       "deconstructSigs",
                       "timeROC"
    ) 
    Biocductor_packages <- c("KEGG.db",
                             "limma",
                             "clusterProfiler",
                             "org.Hs.eg.db",
                             "SummarizedExperiment",
                             "DESeq2",
                             "edgeR",
                             "ggpubr",
                             "rtracklayer",
                             "genefilter",
                             "maftools",
                             "ComplexHeatmap",
                             "GDCRNATools"
    )
    
    
    for (pkg in cran_packages){
      if (! require(pkg,character.only=T) ) {
        install.packages(pkg,ask = F,update = F)
        require(pkg,character.only=T) 
      }
    }
    
    
    # use BiocManager to install
    for (pkg in Biocductor_packages){
      if (! require(pkg,character.only=T) ) {
        BiocManager::install(pkg,ask = F,update = F)
        require(pkg,character.only=T) 
      }
    }
    
    
    #前面的报错都先不要管。主要看这里
    for (pkg in c(Biocductor_packages,cran_packages)){
      require(pkg,character.only=T) 
    }
    
    #哪个报错,就回去安装哪个。如果你没有安装xx包,却提示你xx包不存在,这也正常,是因为复杂的依赖关系,缺啥补啥。
    if(!require(AnnoProbe))devtools::install_local("AnnoProbe-master.zip",upgrade = F,dependencies = T)
    if(!require(tinyarray))devtools::install_local("tinyarray-master.zip",upgrade = F,dependencies = T)
    library(tinyarray)
    
    

    数据下载

    组学数据是按照样本来组织的【正常组织也可以从病人身上取下来】组学数据是以counts来结尾的,临床信息是按照病人来组织的
    image.png

    文件类型介绍

    manifest
    gdcclient是派出去买菜的机器人,manifest就是买菜的清单
    xml
    image.png
    counts
    RNA测序数据计数的结果。counts:把样本中所有的序列打碎后匹配到参考基因组上,意思就是每个基因都匹配到了多少reads【用于差异分析的数据不需要进行标准化】
    ---
    title: "gdc-client数据下载"
    author: "Sun Xiaojie"
    output: html_document
    editor_options: 
      chunk_output_type: console
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(
      collapse = TRUE,
      comment = "#>"
    )
    knitr::opts_chunk$set(fig.width = 6,fig.height = 6,collapse = TRUE)
    knitr::opts_chunk$set(message = FALSE)
    

    本文的内容是用GDC下载并整理表达矩阵和临床信息数据。

    1.从网页选择数据,下载manifest文件

    数据存放网站:https://portal.gdc.cancer.gov/

    在Repository勾选自己需要的case和file类型。以CHOL为例:
    case-Project选择TCGA-CHOL。

    file-选择如图:

    左右分别是expdata 和clinical的样本选择截图。选好后,点击右侧manifest键下载对应的清单文件txt格式。【先下载临床信息后下载样本,要记得改名】

    2.使用gdc-client工具下载

    注意

    将gdc-client(mac)或gdc-client.exe(windows)放在工作目录下;

    将manifest文件放在工作目录下。

    options(stringsAsFactors = F)
    library(stringr)
    cancer_type="TCGA-CHOL"
    if(!dir.exists("clinical"))dir.create("clinical")
    if(!dir.exists("expdata"))dir.create("expdata")
    dir()
    #下面两行命令在terminal完成
    #./gdc-client download -m gdc_manifest_cl.2020-03-23.txt -d clinical
    #./gdc-client download -m gdc_manifest_expdata.2020-03-23.txt -d expdata
    
    length(dir("./clinical/"))
    length(dir("./expdata/"))
    

    可以看到,下载的文件是按样本存放的,我们需要得到的是表格,需要将他们批量读入R语言并整理。

    3.整理临床信息

    library(XML)
    result <- xmlParse("./clinical/142aea0e-7a7b-4ac4-9dbb-0f62e2379599/nationwidechildrens.org_clinical.TCGA-W5-AA2O.xml")
    
    rootnode <- xmlRoot(result)
    xmlSize(rootnode)
    
    print(rootnode[1])
    #print(rootnode[2])
    xmldataframe <- xmlToDataFrame(rootnode[2])
    head(t(xmlToDataFrame(rootnode[2])))
    
    xmls = dir("clinical/",pattern = "*.xml$",recursive = T)
    cl = list()
    for(i in 1:length(xmls)){
      result <- xmlParse(paste0("clinical/",xmls[[i]]))
      rootnode <- xmlRoot(result)
      cl[[i]] = xmlToDataFrame(rootnode[2])
    }
    clinical <- do.call(rbind,cl)
    clinical[1:3,1:3]
    
    有48个单行数据框组成的

    4.整理表达矩阵

    探索数据:先任选两个counts文件读取,并观察geneid的顺序是否一致。

    options(stringsAsFactors = F)
    x = read.table("expdata/03aee74e-4e37-4a58-a720-c90e807d2f40/be5bc6a0-9720-47ac-953e-fa8d0c32cd82.htseq.counts.gz",
                   row.names = 1,sep = "\t")
    x2 = read.table("expdata/10d08172-48d2-49e7-b760-721163492cc1/c1071bcd-5a0c-4e09-a578-fc4b6dbe26ad.htseq.counts.gz",
                    row.names = 1,sep = "\t")
    identical(rownames(x),rownames(x2))
    

    由此可知,他们的geneid顺序是一致的,可以直接cbind,不会导致顺序错乱。

    批量读取所有的counts.gz文件。

    count_files = dir("expdata/",pattern = "*.htseq.counts.gz$",recursive = T)
    
    exp = list()
    for(i in 1:length(count_files)){
      exp[[i]] <- read.table(paste0("expdata/",count_files[[i]]),row.names = 1,sep = "\t")
    }
      
    exp <- do.call(cbind,exp)
    dim(exp)
    exp[1:4,1:4]
    

    发现问题:这样产生出来的表达矩阵没有列名。

    解决办法:找到一个文件名与样本ID一一对应的文件。cart-json文件。

    meta <- jsonlite::fromJSON("metadata.cart.2020-03-23.json")
    colnames(meta)
    ids <- meta$associated_entities;class(ids)
    ids[[1]]
    ids[[1]][,1]
    

    可以看到,meta$associated_entities是个列表,这个列表里包含数据框,数据框的第一列内容就是tcga样本id。

    注意,换了数据需要自己探索存放在哪一列。不一定是完全一样的,需要确认清楚。

    ID = sapply(ids,function(x){x[,1]})
    file2id = data.frame(file_name = meta$file_name,
                         ID = ID)
    

    文件名与TCGA样本ID的对应关系已经得到,接下来是将其添加到表达矩阵中,成为行名。需要找到读取文件的顺序,一一对应修改。

    head(file2id$file_name,2)
    head(count_files,2)
    count_files2 = stringr::str_split(count_files,"/",simplify = T)[,2]
    table(count_files2 %in% file2id$file_name)
    

    count_files2的顺序就是列名的顺序,根据它来调整file2id的顺序。此处需要再次理解一下match函数。

    file2id = file2id[match(count_files2,file2id$file_name),]
    colnames(exp) = file2id$ID
    exp[1:4,1:4]
    

    表达矩阵整理完成,需要过滤一下那些在很多样本里表达量都为0的基因。过滤标准不唯一。

    dim(exp)
    #exp = exp[rowSums(exp)>0,]
    exp = exp[apply(exp, 1, function(x) sum(x > 1) > 9), ]
    dim(exp)
    exp[1:4,1:4]
    

    分组信息

    根据样本ID的第14-15位,给样本分组(tumor和normal)


    image.png
    table(str_sub(colnames(exp),14,15))
    Group = ifelse(as.numeric(str_sub(colnames(exp),14,15)) < 10,'tumor','normal')
    Group = factor(Group,levels = c("normal","tumor"))
    table(Group)
    save(exp,clinical,Group,cancer_type,file = paste0(cancer_type,"_gdc.Rdata"))
    

    相关文章

      网友评论

        本文标题:TCGA数据库挖掘(1)TCGA准备

        本文链接:https://www.haomeiwen.com/subject/odnqcltx.html