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=rtrt$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=datasameSample,,drop=F

> tmb=tmbsameSample,,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

学习交流

相关推荐
User_芊芊君子2 天前
RStudio 鸿蒙 PC 适配全记录:以 Qt 原生工作区承载嵌入式 R
qt·r语言·harmonyos
临床数据科学和人工智能兴趣组2 天前
399元现在超值!学R语言,订阅我们专栏就够了,包括了所有的内容,不断更新!
人工智能·数据挖掘·r语言·r语言-4.2.1·临床研究
babe小鑫3 天前
生物统计学专业校招:SAS、R、Python学习顺序实用指南
python·学习·r语言
大江东去浪淘尽千古风流人物3 天前
【LoMa】局部特征匹配重访:从LoMa-B到旋转不变LoMa-R的架构与工程实践
开发语言·深度学习·计算机视觉·r语言·视觉定位·sfm·局部特征匹配
babe小鑫4 天前
精算学专业秋招:Excel、SQL、R和Python应该先练什么
sql·r语言·excel
海盗12345 天前
微软技术日报 2026-09-10:Project Zenith 开发者设备亮相,.NET 11 RC1 可投产
microsoft·r语言·.net
统计学小王子6 天前
数学建模国赛倒计时1天——《统计模型精讲(岭回归R语言实战篇)》
数学建模·回归·r语言
统计学小王子6 天前
数学建模国赛倒计时 1 天 ——《统计模型精讲(逐步回归 R 语言实战篇)》
开发语言·数学建模·r语言
统计学小王子7 天前
数学建模国赛倒计时 2 天 ——《统计模型精讲(广义线性模型 R 语言实例 + 分析)》
开发语言·数学建模·r语言
统计学小王子7 天前
数学建模国赛倒计时 2 天 ——《缺失值处理一(R语言)》
开发语言·数学建模·r语言