R语言:肿瘤突变负荷分析

> merge_maf <- function(metadata, path){

#通过合并path,还有sample sheet前两列得到每一个文件的完整路径

filenames <- file.path(path, metadatafile_id, metadatafile_name,

fsep = .Platform$file.sep)

message ('############### Merging maf data ################\n',

'### This step may take a few minutes ###\n')

#通过lapply循环去读每一个样本的maf,然后通过rbind合并成矩阵,按行来合并

#colClasses指定所有列为字符串

mafMatrix <- do.call("rbind", lapply(filenames, function(fl)

read.table(gzfile(fl),header=T,sep="\t",quote="",fill=T,colClasses="character")))

return (mafMatrix)

}

#定义去除重复样本的函数FilterDuplicate

> FilterDuplicate <- function(metadata) {

filter <- which(duplicated(metadata[,'sample']))

if (length(filter) != 0) {

metadata <- metadata[-filter,]

}

message (paste('Removed', length(filter), 'samples', sep=' '))

return (metadata)

}

#读入maf的sample sheet文件

> metaMatrix.maf=read.table("maf_sample_sheet.tsv",sep="\t",header=T)

#替换.为下划线,转换成小写,sample_id替换成sample

>names(metaMatrix.maf)=gsub("sample_id","sample",gsub("\\.","_",tolower(names(metaMatrix.maf))))

#删掉最后一列sample_type中的空格

> metaMatrix.mafsample_type=gsub(" ","",metaMatrix.mafsample_type)

#删掉重复的样本

> metaMatrix.maf <- FilterDuplicate(metaMatrix.maf)

#调用merge_maf函数合并maf的矩阵

> maf_value=merge_maf(metadata=metaMatrix.maf,

path="maf_data"

)

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| > #查看前三行前十列 > maf_value[1:3,1:10] Hugo_Symbol Entrez_Gene_Id Center NCBI_Build Chromosome Start_Position End_Position Strand Variant_Classification Variant_Type 1 PLOD1 5351 WUGSC GRCh38 chr1 11957858 11957858 + Missense_Mutation SNP 2 IVL 3713 WUGSC GRCh38 chr1 152910652 152910652 + Nonsense_Mutation SNP 3 OBSCN 84033 WUGSC GRCh38 chr1 228256740 228256740 + Missense_Mutation SNP |

#保存合并后的maf文件

> write.table(file="combined_maf_value.txt",maf_value,row.names=F,quote=F,sep="\t")

#TMB打分

> BiocManager::install("maftools")

> library(maftools)

> laml <- read.maf(maf = "combined_maf_value.txt")

> tmb_table_wt_log = tmb(maf = laml)

#tmb_table_wt_log = tmb(maf = laml): 这行代码调用了 tmb() 函数,计算了基于 MAF 数据的肿瘤突变负荷(TMB)。参数 maf 接受了之前读取的 laml 数据框作为输入,然后将结果赋值给了 tmb_table_wt_log 变量。

write.table(tmb_table_wt_log,file="TMB_log.txt",sep="\t",row.names=F)

#突变负荷分析

> library(limma)

> library(ggplot2)

#install.packages("ggpubr")

> library(ggpubr)

#install.packages("ggExtra")

> library(ggExtra)

> expFile="geneExp.txt"

> tmbFile="TMB.txt"

> rt=read.table(expFile, header=T, sep="\t", check.names=F, row.names=1)

> gene=colnames(rt)[1]

> tumorData=rt[rt$Type=="Tumor",1,drop=F]

> tumorData=as.matrix(tumorData)

> rownames(tumorData)=gsub("(.*?)\\-(.*?)\\-(.*?)\\-.*", "\\1\\-\\2\\-\\3\\", rownames(tumorData))

> data=avereps(tumorData)

> tmb=read.table(tmbFile, header=T, sep="\t", check.names=F, row.names=1)

> rownames(tmb)=gsub("(.*?)\\-(.*?)\\-(.*?)\\-.*", "\\1\\-\\2\\-\\3\\", rownames(tmb))

> tmb=avereps(tmb)

> sameSample=intersect(row.names(data), row.names(tmb))

> data=data[sameSample,,drop=F]

> tmb=tmb[sameSample,,drop=F]

> rt=cbind(data, tmb)

> x=as.numeric(rt[,gene])

> y=log2(as.numeric(rt[,"total_perMB_log"])+1)

> df1=as.data.frame(cbind(x,y))

> corT=cor.test(x, y, method="spearman")

> p1=ggplot(df1, aes(x, y)) +

xlab(paste0(gene, " expression"))+ylab("Tumor mutation burden")+

geom_point()+ geom_smooth(method="lm",formula = y ~ x) + theme_bw()+

stat_cor(method = 'spearman', aes(x =x, y =y))

p1

> p2=ggMarginal(p1, type = "density", xparams = list(fill = "orange"),yparams = list(fill = "blue"))

> p2

学习交流

相关推荐
PhyliciaFelicia1 天前
基于R语言机器学习遥感数据处理与模型空间预测
开发语言·深度学习·随机森林·机器学习·数据分析·r语言
长安不及十里2 天前
PaddleDetection 自定义训练目标检测
人工智能·目标检测·r语言
WangYan20223 天前
ChatGPT+R语言强强联合,数据分析不再难!回归与混合效应模型、多元统计分析、结构方程模型(SEM)(lavaan)、Meta分析、贝叶斯回归等应用
chatgpt·数据分析·r语言·结构方程模型·多元统计分析·回归与混合效应模型
hakesashou4 天前
python和r语言的区别是什么
开发语言·python·r语言
青春不流名4 天前
centos72009源码编译R语言
开发语言·r语言
SofterICer4 天前
pysim-4-1.1.17 eUICC ISD-R commands
开发语言·r语言
让学习成为一种生活方式4 天前
基因共表达分析-R-脚本04
java·windows·r语言
卡卡_R-Python5 天前
简单线性回归分析-基于R语言
算法·r语言·线性回归
纪伊路上盛名在5 天前
如何初步部署自己的服务器,达到生信分析的及格线2(待更新)
linux·运维·服务器·python·学习·r语言
桂渊泉树6 天前
R整理数据技巧
r语言