TCGA数据下载与整理-TCGAbiolinks包

写在前面,如果不是R语言高手,建议利用网站下载
个人爱好——1、官网:https://portal.gdc.cancer.gov
方法见从TCGA数据库下载并整合清洗高通量肿瘤表达谱-临床性状数据。
2、UCSC—xena:UCSC Xena,强推,压缩版,为tsv文件。

1、下载临床信息

以下代码参考单基因生信分析流程(1)一文解决TCGA数据下载整理问题 -

1.1、加载包,rm(list = ls())清空环境,日常操作,以免出什么幺蛾子,好习惯得养成
rm(list = ls())
setwd("D:\\生\\结")
library(TCGAbiolinks)
library(SummarizedExperiment)
library(dplyr)
library(DT)
library(rtracklayer)
library(readr)

我遇到的问题:记得先安装,如果install.packages()安装出现安装不成功的情况,建议用官网的安装代码,或者BiocManager::install(),可以解决。或者更改镜像路径!

下载整理TCGA数据,

以结肠癌(COAD)为例,下载其他类型,请参考TCGA / 癌症简称 / 缩写 / TCGA癌症中英文对照 -

query=GDCquery(project = "TCGA-COAD",
              data.category = "Clinical",#数据类型
              file.type = "xml")
GDCdownload(query)
clinical=GDCprepare_clinic(query,clinical.info = "patient")
1.2、筛选主要信息

由于我本人用R下载数据的时候经常有问题,所以我在xena下载,为tsv文件,以下均以该文件为例。
首先筛选自己所需要的数据

dat=read_tsv("TCGA-COAD.GDC_phenotype.tsv",col_names = TRUE,col_types = NULL)
clinicaldata=dat%>%
  dplyr::select(submitter_id.samples,age_at_initial_pathologic_diagnosis,gender.demographic,submitter_id,
                days_to_death.demographic,days_to_last_follow_up.diagnoses,race.demographic,
                age_at_diagnosis.diagnoses,person_neoplasm_cancer_status,tumor_stage.diagnoses,
                pathologic_T,pathologic_N,pathologic_M,vital_status.demographic,disease_type,
                sample_type.samples)%>%
  distinct(submitter_id.samples,.keep_all = TRUE)#选择所有行

利用dplyr::select选择所需要列,建议先view或者$命令查看下载的列名根据需要选择
distinct选择所需要的行, %>%为管道符号,表示然后意思,能够加快对代码的阅读理解,不加载dplyr包无法使用,偶尔会报错。

1.3、整理死亡患者信息
dead_patient=clinicaldata%>%
  dplyr::filter(vital_status.demographic=='Dead')%>%
  dplyr::select(-days_to_last_follow_up.diagnoses)%>%
  reshape::rename(c(submitter_id='Barcode',
                    submitter_id.samples='sampleID',
                    gender.demographic='Gender',
                    days_to_death.demographic='OS_time',
                    vital_status.demographic='OS',
                    race.demographic='Race',
                    age_at_initial_pathologic_diagnosis='Age',
                    person_neoplasm_cancer_status='cancer_status',
                    tumor_stage.diagnoses='TNM_stage'))%>%
  mutate(OS=ifelse(OS=="Dead",1,0))

备注
filter过滤函数;reshape::rename重新命名函数
此处应注意选择的列,选择死亡患者,则不要days_to_last_followup这一列,然后days_to_death作为生存时间,挑选生存患者的时候则相反,切记切记!
mutate(),转换数值函数

1.4、整理生存患者信息,合并
alive_patient=clinicaldata%>%
  dplyr::filter(vital_status.demographic=='Alive')%>%
  dplyr::select(-days_to_death.demographic)%>%
  reshape::rename(c(submitter_id='Barcode',
                    submitter_id.samples='sampleID',
                    gender.demographic='Gender',
                    days_to_last_follow_up.diagnoses='OS_time',
                    vital_status.demographic='OS',
                    race.demographic='Race',
                    age_at_initial_pathologic_diagnosis='Age',
                    person_neoplasm_cancer_status='cancer_status',
                    tumor_stage.diagnoses='TNM_stage'))%>%
  mutate(OS=ifelse(OS=="Dead",1,0))
surdata=rbind(dead_patient,alive_patient)
write.csv(surdata,file = "COAD_phenotype.csv")

二、下载表达谱与整理

COAD为例

exp=GDCquery(project = "TCGA-COAD",           
             data.category = "Transcriptome Profiling",
             data.type = "Gene Expression Quantification", 
             workflow.type = "HTSeq - FPKM")
GDCdownload(exp)
expdat=GDCprepare(query = exp)
count_matrix=as.data.frame(assay(expdat))

运行时家里网络报:Error in value[3L] :**** GDC server down, try to use this package later,实验室网络也偶尔遇到。主要是官网api问题,所以建议在线下载安全,个人愚见。以下操作以xena下载数据展示,tsv文件。

library(TCGAbiolinks)
library(SummarizedExperiment)
library(dplyr)
library(DT)
library(readr)
library(rtracklayer)
exp=read_tsv("TCGA-COAD.htseq_fpkm.tsv",col_names = T,col_types = NULL,na=c("","NA"))
exp=exp%>%
  reshape::rename(c(Ensembl_ID="gene_id"))
#按装rtracklayer包,不仅可用于gtf文件的导入导出,还可以操作其他多种数据格式(BED,WIG,bigWig......)
#BiocManager::install("rtracklayer")
#加载注释文件,仅仅所需要得id,name,type列,其他信息可不需要,感兴趣得可以view函数看一下到底还有啥
gtf=rtracklayer::import('gencode.v35.long_noncoding_RNAs.gff3')
gff_df=as.data.frame(gtf)
genegtf=gff_df%>%
  dplyr::select(gene_id,gene_type,gene_name)
#提取表达谱内的lncRNA,记得先把表达谱内的Ensembl_ID改成"gene_id")
lncRNA_exp=gff_df%>%
  dplyr::filter(type=="gene",gene_type=="lncRNA")%>%
  dplyr::select(gene_id,gene_type,gene_name)%>%
  dplyr::inner_join(exp,by="gene_id")%>%
  tidyr::unite(gene_id,gene_type,gene_id,gene_name,sep="|")
#做到这一步时,记得留意一下目前第一列得名称得顺序,接下来是把他们分开,顺序需要跟后面删除得对应起来。我把name放在第三列,所有后面删除第一第二列。
lncRNA_exp=lncRNA_exp%>%
  tidyr::separate(gene_id,c("gene_type","gene_id","gene_name"),
                  sep="\\|")
lncRNA_exp=lncRNA_exp[,-(1:2)]#删除第一第二列,保留gene_name列
index=duplicated(lncRNA_exp$gene_name)
lncRNA_exp=lncRNA_exp[!index,]
row.names(lncRNA_exp)=lncRNA_exp$gene_name
lncRNA_exp$gene_name=NULL

提取完毕,可以保存,接下来删除癌旁样本和二次测序的样本,根据TCGA样本命名规则,第14、15个数字表示样本,其中编号0109表示肿瘤,1019表示正常对照,(https://gdc.cancer.gov/resources-tcga-users/tcga-code-tables/sample-type-codes)。

mete=data.frame(colnames(lncRNA_exp))#取第一行,即样本ID
for (i in 1:length(mete[,1])) {
  num=as.numeric(as.character(substring(mete[i,1],14,15)))#提取14、15列
  if(num==1) {mete[i,2]="T"}
  if(num!=1){mete[i,2]="N"}
}
names(mete)=c("id","group")
mete$group=as.factor(mete$group)
mete=subset(mete,mete$group=="T")
lncRNA_exp1=lncRNA_exp[,which(colnames(lncRNA_exp)%in% mete$id)]
#讲FPKM转为TPM,看自己需求。
fpkmToTpm=function(fpkm)
{
  exp(log(fpkm)-log(sum(fpkm))+log(1e6))
}
expr=as.data.frame(apply(lncRNA_exp1,2,fpkmToTpm))
#数据导出,TXT文档
write.table(expr, file = "COAD_lncRNA_exp_TPM.txt", append = FALSE, 
quote = TRUE, sep = "\t",eol = "\n", na = "NA", dec = ".", 
row.names = TRUE,col.names = TRUE, qmethod = c("escape", "double"),fileEncoding = "")

至此,得到肿瘤样本(01)的表达谱(TPM或者FPKM),其实01-09均是肿瘤样本,本次仅仅删选了01的肿瘤样本。由于xena下载的表达谱数据不包含二代测序数据,所以以下代码仅供参考。

mete1=data.frame(colnames(lncRNA_exp1))
for (i in 1:length(mete1[,1])) {
  chr=as.character(substring(mete1[i,1],22,25))#第22,25列
  if(chr=="A277") {mete1[i,2]="Rep"}
  if(num!="A277"){mete1[,2]="T"}
}
names(mete1)=c("id","group")
mete1$group=as.factor(mete1$group)
mete1=subset(mete1,mete1$group=="T")
lncRNA_exp2=lncRNA_exp1[,which(colnames(lncRNA_exp1) %in% mete1$id)]
write.csv(lncRNA_exp2,file = "COAD_lncRNA_exp.csv")#以csv文件保存

你可能感兴趣的:(TCGA数据下载与整理-TCGAbiolinks包)