RNA-seq流程学习笔记(18)- Heatmap图

2023-05-16

1. 准备感兴趣基因集(genelist)并进行适当格式转换

# 对基因list进行整理
# 设置工作目录
setwd("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap")
# 将基因导入当前工作环境
list <- read.csv("zonghe.csv")
# 对特定感兴趣基因list进行提取
genes_V1<- as.vector(list[,1])
View(genes_V1)

# 对基因进行必要的格式转换
# 鼠源gene_symbol转化为gene_id
# 加载R包
# BiocManager::install("clusterProfiler")
library("clusterProfiler")
# 加载注释包
# BiocManager::install("org.Mm.eg.db")
# 小鼠 org.Mm.eg.db  # 人org.Hs.eg.db    # 猪org.Ss.eg.db  # 鸡 org.Gg.eg.db   # 酵母 org.Sc.sgd.db
# E coli strain K12 org.EcK12.eg.db      # E coli strain Sakai  org.EcSakai.eg.db
# 犬 org.Cf.eg.db    # 牛org.Bt.eg.db
library("org.Mm.eg.db")
# 查看该R包可提供转化的数据类型
# keytypes(org.Hs.eg.db) 
## 以下是R包org.Hs.eg.db拥有的ID类型,可供选择,对应原来的ids里面的类型
## ID的格式,你挑一个出来和下面的是对应的
# [1] "ACCNUM"       "ALIAS"        "ENSEMBL"      "ENSEMBLPROT"  "ENSEMBLTRANS" "ENTREZID"    
# [7] "ENZYME"       "EVIDENCE"     "EVIDENCEALL"  "GENENAME"     "GO"           "GOALL"       
# [13] "IPI"          "MAP"          "OMIM"         "ONTOLOGY"     "ONTOLOGYALL"  "PATH"        
# [19] "PFAM"         "PMID"         "PROSITE"      "REFSEQ"       "SYMBOL"       "UCSCKG" 

# 由SYMBOL转换为ENSEMBL
genes_V2 <- bitr(genes_V1,               # 输入待处理的gene_id
                 fromType = "SYMBOL",    # fromType是指你的数据ID类型是属于哪一类的
                 toType = "ENSEMBL",     # toType是指你要转换成哪种ID类型,可以写多种,也可以只写一种
                 OrgDb = org.Mm.eg.db)   # Orgdb是指对应的注释包是哪个

# 由鼠源ENSEMBL编号转换为鸡源ENSEMBL编号
# https://cloud.tencent.com/developer/article/1708373
# https://www.jianshu.com/p/78a64d2d998a
# 加载biomaRt包
# BiocManager::install("biomaRt")
library("biomaRt")
# 选择目标数据库和数据集,这里选择人和小鼠的
# useMart一般后面跟两个参数
# 第一个参数是借助ensemble数据库
# 第二个参数是告诉选择哪个物种的数据集
chicken = useMart("ensembl", dataset = "ggallus_gene_ensembl", host = "https://dec2021.archive.ensembl.org/")
mouse = useMart("ensembl", dataset = "mmusculus_gene_ensembl", host = "https://dec2021.archive.ensembl.org/")
human = useMart("ensembl", dataset = "hsapiens_gene_ensembl", host = "https://dec2021.archive.ensembl.org/")
genes_V3 = getLDS(attributes = c("ensembl_gene_id"),      # 输入数据集的属性参数,鼠源symbol为mgi_symbol,人源symbol为hgnc_symbol
                  filters = "ensembl_gene_id",            # 输入数据集在查询中使用的参数过滤器,同上为鼠源symbol
                  values = genes_V2$ENSEMBL,              # 输入的数据集,即待转换的gene_ENSEMBL向量集
                  mart = mouse,                           # 输入数据对应的数据库,鼠源即上面定义的“mouse”
                  attributesL = c("ensembl_gene_id"),     # 输出数据集的属性参数,此处为人源symbol
                  martL = chicken,                        # 输出数据对应的数据库,鸡源即上面定义的“chicken”
                  uniqueRows=T)                           # 单独一行进行输出
# 通过listAttributes(mouse)查询mouse数据库中可使用的属性参数如下:
# 1                                    ensembl_gene_id
# 2                            ensembl_gene_id_version
# 3                              ensembl_transcript_id
# 4                      ensembl_transcript_id_version
# 5                                 ensembl_peptide_id
# 6                         ensembl_peptide_id_version
# 7                                    ensembl_exon_id
# 8                                        description
# 9                                    chromosome_name
# 10                                    start_position
# 11                                      end_position
# 12                                            strand
# 13                                              band
# 14                                  transcript_start
# 15                                    transcript_end
# 16                          transcription_start_site
# 17                                 transcript_length
# 通过listAttributes(mouse)查询mouse数据库中可使用的属性参数如下:
# 1                      chromosome_name
# 2                                start
# 3                                  end
# 4                               strand
# 5                   chromosomal_region
# 6                         with_biogrid
# 7                            with_ccds
# 8                          with_chembl
# 9           with_entrezgene_trans_name
# 10                           with_embl

# 由ENSEMBL转换为SYMBOL
library("org.Gg.eg.db")
genes_V4 <- bitr(genes_V3$Gene.stable.ID.1,   # 输入待处理的gene_id
                 fromType = "ENSEMBL",        # fromType是指你的数据ID类型是属于哪一类的
                 toType = "SYMBOL",           # toType是指你要转换成哪种ID类型,可以写多种,也可以只写一种
                 OrgDb = org.Gg.eg.db)        # Orgdb是指对应的注释包是哪个

# 查看转化后的结果
View(genes_V2)
View(genes_V3)
View(genes_V4)

#将数据保存至heatmap文件中,进行查重处理
write.table(genes_V4, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/T_cell_pathway_genes.txt", row.names = FALSE)

2. 对各样本的FPKM值进行整理

#对FPKM数据进行整理
#清空环境变量
rm(list=ls())
#获取当前工作目录
getwd()

##将StringTie分析得到的含有FPKM数据的TAB文件导入当前工作环境中
#设置工作目录
setwd("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/")

A1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/A1_FRAS220122137.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
A2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/A2_FRAS220122138.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
A3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/A3_FRAS220122139.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
B1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/B1_FRAS220122140.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
B2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/B2_FRAS220122141.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
B3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/B3_FRAS220122142.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
H1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/H1_FRAS220122143.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
H2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/H2_FRAS220122144.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
H3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/H3_FRAS220122145.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
I1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/I1_FRAS220122146.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
I2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/I2_FRAS220122147.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
I3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/I3_FRAS220122148.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
J1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/J1_FRAS220122149.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
J2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/J2_FRAS220122150.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
J3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/J3_FRAS220122151.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
K1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/K1_FRAS220122152.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
K2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/K2_FRAS220122153.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
K3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/K3_FRAS220122154.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
L1.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/L1_FRAS220122155.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
L2.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/L2_FRAS220122156.gene.tab", header = TRUE, sep = "\t" , quote = "\"")
L3.gene.tab <- read.table("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/gene_tab/L3_FRAS220122157.gene.tab", header = TRUE, sep = "\t" , quote = "\"")

##提取指定列的内容
###对数据中的Gene.ID和FPKM两列数据进行提取
A1.FPKM <- A1.gene.tab[,c(1,8)]
A2.FPKM <- A2.gene.tab[,c(1,8)]
A3.FPKM <- A3.gene.tab[,c(1,8)]
B1.FPKM <- B1.gene.tab[,c(1,8)]
B2.FPKM <- B2.gene.tab[,c(1,8)]
B3.FPKM <- B3.gene.tab[,c(1,8)]
H1.FPKM <- H1.gene.tab[,c(1,8)]
H2.FPKM <- H2.gene.tab[,c(1,8)]
H3.FPKM <- H3.gene.tab[,c(1,8)]
I1.FPKM <- I1.gene.tab[,c(1,8)]
I2.FPKM <- I2.gene.tab[,c(1,8)]
I3.FPKM <- I3.gene.tab[,c(1,8)]
J1.FPKM <- J1.gene.tab[,c(1,8)]
J2.FPKM <- J2.gene.tab[,c(1,8)]
J3.FPKM <- J3.gene.tab[,c(1,8)]
K1.FPKM <- K1.gene.tab[,c(1,8)]
K2.FPKM <- K2.gene.tab[,c(1,8)]
K3.FPKM <- K3.gene.tab[,c(1,8)]
L1.FPKM <- L1.gene.tab[,c(1,8)]
L2.FPKM <- L2.gene.tab[,c(1,8)]
L3.FPKM <- L3.gene.tab[,c(1,8)]

###重命名指定列为各样品名称
###重命名全部的列是name(data) <- c("NO","name")
###重命名单个列是colnames(data)[2] <- 'newname'
colnames(A1.FPKM)[2] <-"A1"
colnames(A2.FPKM)[2] <-"A2"
colnames(A3.FPKM)[2] <-"A3"
colnames(B1.FPKM)[2] <-"B1"
colnames(B2.FPKM)[2] <-"B2"
colnames(B3.FPKM)[2] <-"B3"
colnames(H1.FPKM)[2] <-"H1"
colnames(H2.FPKM)[2] <-"H2"
colnames(H3.FPKM)[2] <-"H3"
colnames(I1.FPKM)[2] <-"I1"
colnames(I2.FPKM)[2] <-"I2"
colnames(I3.FPKM)[2] <-"I3"
colnames(J1.FPKM)[2] <-"J1"
colnames(J2.FPKM)[2] <-"J2"
colnames(J3.FPKM)[2] <-"J3"
colnames(K1.FPKM)[2] <-"K1"
colnames(K2.FPKM)[2] <-"K2"
colnames(K3.FPKM)[2] <-"K3"
colnames(L1.FPKM)[2] <-"L1"
colnames(L2.FPKM)[2] <-"L2"
colnames(L3.FPKM)[2] <-"L3"

#将各基因对应的FPKM数据保存至heatmap文件中
write.table(A1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/A1.FPKM.txt", row.names = FALSE)
write.table(A2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/A2.FPKM.txt", row.names = FALSE)
write.table(A3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/A3.FPKM.txt", row.names = FALSE)
write.table(B1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/B1.FPKM.txt", row.names = FALSE)
write.table(B2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/B2.FPKM.txt", row.names = FALSE)
write.table(B3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/B3.FPKM.txt", row.names = FALSE)
write.table(H1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/H1.FPKM.txt", row.names = FALSE)
write.table(H2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/H2.FPKM.txt", row.names = FALSE)
write.table(H3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/H3.FPKM.txt", row.names = FALSE)
write.table(I1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/I1.FPKM.txt", row.names = FALSE)
write.table(I2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/I2.FPKM.txt", row.names = FALSE)
write.table(I3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/I3.FPKM.txt", row.names = FALSE)
write.table(J1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/J1.FPKM.txt", row.names = FALSE)
write.table(J2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/J2.FPKM.txt", row.names = FALSE)
write.table(J3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/J3.FPKM.txt", row.names = FALSE)
write.table(K1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/K1.FPKM.txt", row.names = FALSE)
write.table(K2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/K2.FPKM.txt", row.names = FALSE)
write.table(K3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/K3.FPKM.txt", row.names = FALSE)
write.table(L1.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/L1.FPKM.txt", row.names = FALSE)
write.table(L2.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/L2.FPKM.txt", row.names = FALSE)
write.table(L3.FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/L3.FPKM.txt", row.names = FALSE)

##提取感兴趣的基因集所对应的FPKM
##设置工作目录
setwd("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap")
genes_V5 <- read.csv("group_zonghe.txt")
Interesting_pathway <- group_6_zonghe


#利用match函数对差异基因List信息(小文件)和FPKM值信息(大文件)进行提取
##利用match函数提取各样品中差异基因所在的行数并重新命名为row.NO文件
##match(x,y)函数输出结果:x向量在y向量中所处的位置,x向量元素不存在y向量中的返回NA
##match(x, table$i)函数输出结果:返回x向量在table中$i列中所处的位置
##对Interesting_pathway各样品进行处理
A1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, A1.FPKM$Gene.ID))
A2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, A2.FPKM$Gene.ID))
A3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, A3.FPKM$Gene.ID))
B1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, B1.FPKM$Gene.ID))
B2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, B2.FPKM$Gene.ID))
B3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, B3.FPKM$Gene.ID))
H1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, H1.FPKM$Gene.ID))
H2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, H2.FPKM$Gene.ID))
H3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, H3.FPKM$Gene.ID))
I1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, I1.FPKM$Gene.ID))
I2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, I2.FPKM$Gene.ID))
I3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, I3.FPKM$Gene.ID))
J1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, J1.FPKM$Gene.ID))
J2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, J2.FPKM$Gene.ID))
J3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, J3.FPKM$Gene.ID))
K1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, K1.FPKM$Gene.ID))
K2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, K2.FPKM$Gene.ID))
K3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, K3.FPKM$Gene.ID))
L1_Interesting_pathway_row.NO <- c(match(Interesting_pathway, L1.FPKM$Gene.ID))
L2_Interesting_pathway_row.NO <- c(match(Interesting_pathway, L2.FPKM$Gene.ID))
L3_Interesting_pathway_row.NO <- c(match(Interesting_pathway, L3.FPKM$Gene.ID))


#根据以上行数,对各样品的FPKM值进行提取
##对Interesting_pathway各样品的FPKM值进行提取
A1_Interesting_pathway_gene_FPKM <- na.omit(A1.FPKM[A1_Interesting_pathway_row.NO ,])
A2_Interesting_pathway_gene_FPKM <- na.omit(A2.FPKM[A2_Interesting_pathway_row.NO ,])
A3_Interesting_pathway_gene_FPKM <- na.omit(A3.FPKM[A3_Interesting_pathway_row.NO ,])
B1_Interesting_pathway_gene_FPKM <- na.omit(B1.FPKM[B1_Interesting_pathway_row.NO ,])
B2_Interesting_pathway_gene_FPKM <- na.omit(B2.FPKM[B2_Interesting_pathway_row.NO ,])
B3_Interesting_pathway_gene_FPKM <- na.omit(B3.FPKM[B3_Interesting_pathway_row.NO ,])
H1_Interesting_pathway_gene_FPKM <- na.omit(H1.FPKM[H1_Interesting_pathway_row.NO ,])
H2_Interesting_pathway_gene_FPKM <- na.omit(H2.FPKM[H2_Interesting_pathway_row.NO ,])
H3_Interesting_pathway_gene_FPKM <- na.omit(H3.FPKM[H3_Interesting_pathway_row.NO ,])
I1_Interesting_pathway_gene_FPKM <- na.omit(I1.FPKM[I1_Interesting_pathway_row.NO ,])
I2_Interesting_pathway_gene_FPKM <- na.omit(I2.FPKM[I2_Interesting_pathway_row.NO ,])
I3_Interesting_pathway_gene_FPKM <- na.omit(I3.FPKM[I3_Interesting_pathway_row.NO ,])
J1_Interesting_pathway_gene_FPKM <- na.omit(J1.FPKM[J1_Interesting_pathway_row.NO ,])
J2_Interesting_pathway_gene_FPKM <- na.omit(J2.FPKM[J2_Interesting_pathway_row.NO ,])
J3_Interesting_pathway_gene_FPKM <- na.omit(J3.FPKM[J3_Interesting_pathway_row.NO ,])
K1_Interesting_pathway_gene_FPKM <- na.omit(K1.FPKM[K1_Interesting_pathway_row.NO ,])
K2_Interesting_pathway_gene_FPKM <- na.omit(K2.FPKM[K2_Interesting_pathway_row.NO ,])
K3_Interesting_pathway_gene_FPKM <- na.omit(K3.FPKM[K3_Interesting_pathway_row.NO ,])
L1_Interesting_pathway_gene_FPKM <- na.omit(L1.FPKM[L1_Interesting_pathway_row.NO ,])
L2_Interesting_pathway_gene_FPKM <- na.omit(L2.FPKM[L2_Interesting_pathway_row.NO ,])
L3_Interesting_pathway_gene_FPKM <- na.omit(L3.FPKM[L3_Interesting_pathway_row.NO ,])


#利用merge函数对各组实验的FPKM值进行合并
##merge(x,y, by="")
##对Interesting_pathway各样品的FPKM值进行合并
Interesting_pathway_gene_FPKM_A <- merge(A1_Interesting_pathway_gene_FPKM, merge(A2_Interesting_pathway_gene_FPKM, A3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")
Interesting_pathway_gene_FPKM_B <- merge(B1_Interesting_pathway_gene_FPKM, merge(B2_Interesting_pathway_gene_FPKM, B3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")
Interesting_pathway_gene_FPKM_H <- merge(H1_Interesting_pathway_gene_FPKM, merge(H2_Interesting_pathway_gene_FPKM, H3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")
Interesting_pathway_gene_FPKM_I <- merge(I1_Interesting_pathway_gene_FPKM, merge(I2_Interesting_pathway_gene_FPKM, I3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")
Interesting_pathway_gene_FPKM_J <- merge(J1_Interesting_pathway_gene_FPKM, merge(J2_Interesting_pathway_gene_FPKM, J3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")
Interesting_pathway_gene_FPKM_K <- merge(K1_Interesting_pathway_gene_FPKM, merge(K2_Interesting_pathway_gene_FPKM, K3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")
Interesting_pathway_gene_FPKM_L <- merge(L1_Interesting_pathway_gene_FPKM, merge(L2_Interesting_pathway_gene_FPKM, L3_Interesting_pathway_gene_FPKM,by="Gene.ID"),by="Gene.ID")

Interesting_pathway_gene_FPKM <- merge(merge(merge(Interesting_pathway_gene_FPKM_A, merge(Interesting_pathway_gene_FPKM_B, Interesting_pathway_gene_FPKM_H,by="Gene.ID"),by="Gene.ID"), merge(Interesting_pathway_gene_FPKM_I, merge(Interesting_pathway_gene_FPKM_J, Interesting_pathway_gene_FPKM_K,by="Gene.ID"),by="Gene.ID"),by="Gene.ID"), Interesting_pathway_gene_FPKM_L,by="Gene.ID")
  

##查看合并结果,确认
View(Interesting_pathway_gene_FPKM)

#将对应的FPKM数据保存至heatmap文件中
write.table(Interesting_pathway_gene_FPKM, file = "E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/Interesting_FPKM.txt", row.names = FALSE)

3. 利用R包pheatmap对各样本的FPKM值进行绘图

#开始绘制heatmap图啦啦啦啦啦啦啦啦啦
#代码参考网站:https://www.jianshu.com/p/d86e4afe1065

#安装包(作者说这种方式下载的pheatmap包版本更新一些)
#install.packages('devtools')
#library(devtools)
#install_github("raivokolde/pheatmap")

#清空环境变量
rm(list=ls())
#获取当前工作目录
getwd()
#设置工作目录
setwd("E:/Rstudio/xieruyu/RNA-seq/2022-07-28/Rtreatment/heatmap/")

#加载包
library(RColorBrewer)#设置颜色用的
#BiocManager::install("pheatmap")
library(pheatmap)
#设置配色方案
cc = colorRampPalette(rev(brewer.pal(n=7, name="RdYlBu"))) #Rd=red Yl=yellow Bu=blue
#读入文件,如果确实过多,会无法聚类,最好保证没有缺失,或将缺失替换为0
T_cell_pathway<-read.table(file = "T_cell_pathway_FPKM.txt",row.names = 1,header = T,check.names = F) 

#如果矩阵内容是fpkm表达量,一般取log10(fpkm+1)绘图
T_cell_pathway=log2(T_cell_pathway+1)

#pheatmap参数解释:
#第一个参数是需要用pheatmap画图的数据
#color: 设置颜色。如果想画得更精细一些,可以取cc(1000)
#main: 标题,会显示在最上面
#fontsize: row的字体大小
#scale: 是否归一化为正态分布,可选row,column,none。一般对row进行归一化的情况比较多,column较少。
#border_color: 是否显示边框及边框的颜色,NA不显示, red显示红色。支持简单的颜色单词
#na_col: 设置缺失值的颜色,支持简单颜色单词,一般设置为灰色就满好识别的。
#cluster_rows & cluster_cols: 设置是否对行进行聚类,这个就见仁见智,看你的实际需求了。当缺失值较多的时候是无法进行聚类的。一个解决办法是读取数据的时候不设置缺失值。
#show_rownames & show_colnames: 是否显示行/列的名称
#treeheight_row & treeheight_col: 当前面设置了聚类之后,两边会出现聚类的树,这个参数是设置树的高度的。
#cellheight & cellwidth: 设置每个各自的宽度和高度。有的时候不设置这两个值画出来的树容易放飞自我????
#cutree_row & cutree_col: 是否根据聚类情况把树切开,可以设置切开的份数。
#display_numbers: 设置是否显示每个单元格的值。这个也是个人喜好及文章需求。
#legend: 设置是否显示旁边的bar状图例,emmmm好像还没碰到说不要那个玩意儿的情况。。
#filename: 设置输出文件的名字。可以设置的文件类型有:pdf,png,jpg,tiff,bmp

#绘图group_up
heatmap=pheatmap(T_cell_pathway,color = cc(1000),
                 main=" ",
                 fontsize = 15,
                 scale="row",
                 border_color = NA,
                 na_col = "grey",
                 cluster_rows = F,cluster_cols = F,
                 show_rownames = T,show_colnames = T,
                 treeheight_row = 30,treeheight_col = 30,
                 cellheight = 15,cellwidth = 30,
                 cutree_row=2,cutree_col=2,
                 display_numbers = F,legend = T,
                 filename = "T_cell_pathway.tiff")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RNA-seq流程学习笔记(18)- Heatmap图 的相关文章

  • Vertex 中的 R iGraph 热图

    我对 R 很陌生 有一个问题被困住了 是否可以在顶点上打印热图iGraph 我知道我可以做一个彩色的正方形或圆形 但是小型热图可能吗 这是绘制我当前图表的代码 create graph graph lt graph data frame n
  • d3heatmap包错误

    当我输入到 2022 年尺寸为 634 的矩阵 M 时 d3heatmap M 会输出一个错误 all vapply s is integer NA 不为 TRUE 我调试了它并导致了这一行 调试 colClust 为什么是这样 我传入的矩
  • 如何用Python绘制时间序列热图? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想绘制一个图表 其中 x 轴作为时间轴 y 轴作为其值 颜色将指示其频率 频率越高 颜色越深 我认为您正在寻找二维直方图 impor
  • Canvas 或 ImageView 上的 Android 热图

    I want to generate dynamic heatmap just as below on either canvas or imageview I looked into google map heat map API but
  • 在 Julia-lang 中生成热图的子图

    我正在尝试生成一个具有多个热图 根据单元格值具有颜色阴影的矩阵 的图形 图 眼下using Plots pyplot and heatmap mat 足以生成热图 我不清楚如何用更多的东西来制作一个图形 看完这个页面后示例子图 https
  • R 中的 pheatmap 格式:图例大小并创建方形图

    Pheatmap 仅在 legend FALSE 时创建方形图 我尝试使用 par 来允许更多 oma 和 mar 空间 但运气不佳 图例也很大 我找不到任何有关减少此图例或更改其位置的文档 第一个图没有安装树状图 但这与尺寸问题无关 无论
  • Google Maps API v2 热图无法可靠显示

    好的 我正在努力根据从服务器提取的数据实现热图 我基于官方的 Google heatmap api 并且我的代码基于他们的代码演示 但是 我的代码不起作用 有时它会显示热图 但更多时候它什么也不显示 我已经仔细检查以确保数据传入 因此缺少数
  • 来自 Pivot 的 Seaborn 热图中的数据顺序

    所以我有一个使用seaborn创建的热图 revels rd pivot Flavour Packet number Contents ax sns heatmap revels annot True fmt d linewidths 0
  • 将项目添加到不可变的 Seq

    假设我有一个字符串序列作为输入 我想获得一个新的不可变的Seq它由输入的元素和一个项目组成 c 以下是我发现有效的两种方法 assert Seq a b c Seq a b Seq c 这个的问题是 似乎实例化了一个临时序列 Seq c 只
  • 使用 Seaborn 和 Matplotlib 对齐热图和线图的共享子图中的 x 轴刻度

    绘制一个热图和线图使用具有共享 x 轴的 Seaborn 热图的刻度被放置在热图条的中间 因此 底部线图将继承热图刻度位置和标签 而不反映真实数据 因为线图刻度应从零开始 换句话说 我需要将两个图的刻度移动到从 x 轴原点开始 最佳 或者将
  • 如何更改 heatmap.2 中的颜色键值?

    如上面的截图所示 我使用了该功能heatmap 2 here 我怎样才能改变 Value 在颜色编码栏中任何其他名称 人们可以只使用 gplots 包中的数据 library gplots data mtcars x lt as matri
  • 对热图的刻度线进行分组

    I have a heatmap that looks like this from Plotting a 2D heatmap with Matplotlib https stackoverflow com questions 33282
  • 使用seaborn 热图突出显示每行的最小值

    我试图使用相同的颜色突出显示每行的最小值 例如 第一行最小值为 0 3 我想用蓝色突出显示它 同样 对于第二行 0 042 等等 这是代码 import numpy as np import seaborn as sns import ma
  • 如何在 geom_tile ggplot 中移动图块右/左端的刻度线和标签?

    我无法将 geom tile 中的 x 轴标签 包括刻度线 移动到每个图块的右端 我还想在左端添加零 我尝试过休息和标签 但没有运气 使用中断和标签也不起作用 我试图实现这个答案中所做的事情 但建议的解决方案不起作用 如何强制 x 轴刻度线
  • seq 和 list 之间的区别

    Clojure 语言中的 seq 和列表有什么区别 list 1 2 3 gt 1 2 3 seq 1 2 3 gt 1 2 3 这两种形式似乎被评估为相同的结果 首先 它们可能看起来相同 但实际上并非如此 class list 1 2 3
  • 仅根据邮政编码在 R 中绘制热图

    我想在 R 中绘制热图 但我的数据文件是这样的 Lat Long Zip Zvalue 我基本上需要在纬度和经度值之间进行插值 并根据 z 值绘制颜色 我怎样才能在 R 中做到这一点 我最终想要得到这样的东西 套餐spatstat是你的朋友
  • 根据要求在 Python/Matplotlib 中为热图着色

    我正在尝试制作具有指定颜色要求的热图 我想为数据设置一个间隔并判断为ok并将其着色为绿色 其余结果应着色为红色 有谁知道如何做到这一点吗 我附上了一个使用 pandas 和 matplotlib 的简单示例 以便更好地理解 import n
  • 了解用于处理色边距的scale_fill_continuous_divergingx参数输入

    这个问题是我上一个问题的延续here https stackoverflow com questions 58718527 setting midpoint for continuous diverging color scale on a
  • 如何在seaborn热图标签中使用科学计数法?

    我正在尝试在 python 中使用seaborn 获取热图 不幸的是 即使数字非常大 它也没有使用科学记数法 我想知道是否有任何简单的方法可以转换为科学记数法或任何其他合理的格式 这是显示问题的一段代码 import seaborn as
  • 如何在 Seaborn 中的热图轴上表达类

    我使用 Seaborn 创建了一个非常简单的热图 显示相似性方阵 这是我使用的一行代码 sns heatmap sim mat linewidths 0 square True robust True sns plt show 这是我得到的

随机推荐