R语言-Bioconductor依赖管理&&KEGG富集分析&&通路图&&pathview报错解决

win10-R语言3.6.2-Bioconductor依赖管理&&KEGG富集分析&&通路图&&pathview报错解决

一、环境准备

下载安装包(64位),(如果没有翻墙,选择国内的源进行下载)链接如下:

https://mirrors.tuna.tsinghua.edu.cn/CRAN/bin/windows/base/R-3.6.2-win.exe

其他版本可以打开https://mirrors.tuna.tsinghua.edu.cn/CRAN,再根据自己物理机的配置自行选择,如下图所示:
在这里插入图片描述

或者选择本人的百度云链接进行下载,链接如下:

链接:https://pan.baidu.com/s/1_FEnaqRfSY1HpoQW9nfWbA 
提取码:727u

安装

  • 双击下载好的R-3.6.2-win.exe文件,按提示安装即可。

配置环境变量,在系统环境变量的Path中写入安装的R路径F:\R\R-3.6.2\bin
在这里插入图片描述
在这里插入图片描述
点击快捷方式即可运行程序
在这里插入图片描述

二、运行程序

代码:

#install.packages("colorspace")
#install.packages("stringi")
#options(BioC_mirror=”https://mirrors.tuna.tsinghua.edu.cn/bioc/“)
#if (!requireNamespace("BiocManager", quietly = TRUE))
#   install.packages("BiocManager")
#BiocManager::install(version = "3.10")
#BiocManager::install(c("DOSE", "clusterProfiler","pathview"))

# setwd设置工作路径,文件的读入和写出都是相对与该路径
setwd("C:\\Users\\urmsone\\Desktop\\Li")
library("clusterProfiler")
rt=read.table("id.txt",sep="\t",header=T,check.names=F)
rt=rt[is.na(rt[,"entrezID"])==F,]

geneFC=rt$logFC
gene=rt$entrezID
names(geneFC)=gene

#kegg富集分析
kk <- enrichKEGG(gene = gene, organism = "hsa", pvalueCutoff =0.05, qvalueCutoff =0.05)
write.table(kk,file="KEGG.txt",sep="\t",quote=F,row.names = F)

#柱状图
tiff(file="barplot.tiff",width = 30,height = 20,units ="cm",compression="lzw",bg="white",res=600)
barplot(kk, drop = TRUE, showCategory = 20)
dev.off()

#点图
tiff(file="dotplot.tiff",width = 30,height = 20,units ="cm",compression="lzw",bg="white",res=600)
dotplot(kk, showCategory = 20)
dev.off()

#通路图
library("pathview")
source("./pathview-u.R")
keggxls=read.table("KEGG.txt",sep="\t",header=T)
for(i in keggxls$ID){
  pv.out <- pathview(gene.data = geneFC, pathway.id = i, species = "hsa", out.suffix = "pathview")
}

PS:首次运行时才需要安装依赖
安装依赖:

# 该程序需要手动安装的依赖有colorspace,stringi,DOSE,clusterProfiler,pathview
install.packages("colorspace")
install.packages("stringi")
# 换源
options(BioC_mirror=”https://mirrors.tuna.tsinghua.edu.cn/bioc/“)
if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install(version = "3.10")
BiocManager::install(c("DOSE", "clusterProfiler","pathview"))

三、运行程序

打开R客户端
在这里插入图片描述
将代码复制进去执行

setwd("C:\\Users\\urmsone\\Desktop\\Li")
library("clusterProfiler")
rt=read.table("id.txt",sep="\t",header=T,check.names=F)
rt=rt[is.na(rt[,"entrezID"])==F,]

geneFC=rt$logFC
gene=rt$entrezID
names(geneFC)=gene

#kegg富集分析
kk <- enrichKEGG(gene = gene, organism = "hsa", pvalueCutoff =0.05, qvalueCutoff =0.05)
write.table(kk,file="KEGG.txt",sep="\t",quote=F,row.names = F)

#通路图
library("pathview")
#source("./pathview-u.R")
keggxls=read.table("KEGG.txt",sep="\t",header=T)
for(i in keggxls$ID){
  pv.out <- pathview(gene.data = geneFC, pathway.id = i, species = "hsa", out.suffix = "pathview")
}

PS: 如果输出的KEGG.txt文件中含有ID为hsa04251的数据,可能会报错。报错在下节讲解如何解决

四、报错解决

报错: With R version 3.5 or greater, install Bioconductor packages using BiocManager

  • 执行source("http://bioconductor.org/biocLite.R")报错

    错误: With R version 3.5 or greater, install Bioconductor packages using BiocManager; see https://bioconductor.org/install
    

    在这里插入图片描述

  • 原因:R 3.6以后使用BoicManager进行依赖管理

  • 解决

    # R 3.6以后使用BoicManager进行依赖管理
    options(BioC_mirror=”https://mirrors.tuna.tsinghua.edu.cn/bioc/“)
    if (!requireNamespace("BiocManager", quietly = TRUE))
        install.packages("BiocManager")
    BiocManager::install(version = "3.10")
    # 要安装什么依赖直接BiocManager::install("xxx")即可
    

报错:只有负下标里才能有零

  • 执行pv.out <- pathview(gene.data = geneFC, pathway.id = i, species = “hsa”, out.suffix = “pathview”)报错

    Info: Writing image file hsa04215.pathview.png Info: some node width is different from others, and hence adjusted! Error in img[pidx[i, 3]:pidx[i, 4], sel.px, 1:3] : 只有负下标里才能有零
    

    在这里插入图片描述

  • 原因:拉下来的hsa04215.xml文件中部分entry为type="rectangle" x="1" y="1" width="1" height="1",node.info函数在解析该xml文件的时候会生成x=1 y=1的记录,导致生成png图片的时候报错
    在这里插入图片描述

  • 解决: 在pathview函数中把entry为type="rectangle" x="1" y="1" width="1" height="1"的数据过滤掉
    在这里插入图片描述

    • 首先,导入pathview
      > library("pathview")
      载入需要的程辑包:org.Hs.eg.db
      载入需要的程辑包:AnnotationDbi
      载入需要的程辑包:stats4
      载入需要的程辑包:BiocGenerics
      载入需要的程辑包:parallel
      ...
      
    • 然后获取pathview的源码
      > pathview
      function (gene.data = NULL, cpd.data = NULL, pathway.id, species = "hsa", 
          kegg.dir = ".", cpd.idtype = "kegg", gene.idtype = "entrez", 
          gene.annotpkg = NULL, min.nnodes = 3, kegg.native = TRUE, 
          map.null = TRUE, expand.node = FALSE, split.group = FALSE, 
          map.symbol = TRUE, map.cpdname = TRUE, node.sum = "sum", 
          discrete = list(gene = FALSE, cpd = FALSE), limit = list(gene = 1, 
              cpd = 1), bins = list(gene = 10, cpd = 10), both.dirs = list(gene = T, 
              cpd = T), trans.fun = list(gene = NULL, cpd = NULL), 
          low = list(gene = "green", cpd = "blue"), mid = list(gene = "gray", 
              cpd = "gray"), high = list(gene = "red", 
              cpd = "yellow"), na.col = "transparent", 
          ...) 
      
      
    • 新建一个pathview-u.R文件,将pathview函数的源码写入,并修改函数名为pathview-u,在plot.data.gene=node.map..后加入plot.data.gene<-plot.data.gene[rowSums(plot.data.gene[,c("x","y","width","height")])!=4,]
      在这里插入图片描述
      在这里插入图片描述
  • 导入修改后的函数,并使用:

    > source("./pathview-u.R")
    > for(i in keggxls$ID){
    +   pv.out <- pathview_u(gene.data = geneFC, pathway.id = i, species = "hsa", out.suffix = "pathview")
    + }
    Info: Downloading xml files for hsa04657, 1/1 pathways..
    Info: Downloading png files for hsa04657, 1/1 pathways..
    'select()' returned 1:1 mapping between keys and columns
    Info: Working in directory C:/Users/urmsone/Desktop/Li
    Info: Writing image file hsa04657.pathview.png
    Info: Downloading xml files for hsa04668, 1/1 pathways..
    Info: Downloading png files for hsa04668, 1/1 pathways..
    'select()' returned 1:1 mapping between keys and columns
    Info: Working in directory C:/Users/urmsone/Desktop/Li
    Info: Writing image file hsa04668.pathview.png
    Info: Downloading xml files for hsa04066, 1/1 pathways..
    Info: Downloading png files for hsa04066, 1/1 pathways..
    'select()' returned 1:1 mapping between keys and columns
    Info: Working in directory C:/Users/urmsone/Desktop/Li
    Info: Writing image file hsa04066.pathview.png
    ...
    

报错:Error in readPNG(paste(kegg.dir, "/", pathway.name, ".png", sep = "")) : libpng error: Read Error

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

五、Demo获取链接

完整Demo和使用说明请参考https://github.com/UrmsOne/KEGG-demo

  • 10
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
KEGG富集分析是一种常用的生物信息学分析方法,用于研究基因或蛋白质的功能和通路富集情况。在R语言中,可以使用Bioconductor中的包来进行KEGG富集分析,其中包括KEGGREST和clusterProfiler。 首先,你需要安装并加载这些包。可以使用下面的代码来完成这一步骤: install.packages("BiocManager") BiocManager::install("KEGGREST") BiocManager::install("clusterProfiler") library(KEGGREST) library(clusterProfiler) 接下来,你需要准备好你的基因列表,并使用KEGGREST包中的函数获取基因对应的KEGG通路信息。下面是一个示例代码,你可以根据你的需要进行修改: gene_list <- c("gene1", "gene2", "gene3") # 替换为你的基因列表 kegg_pathways <- keggGet("pathway", "hsa", "list") # 获取KEGG通路列表 gene_pathway <- enrichKEGG(gene = gene_list, organism = "hsa", pvalueCutoff = 0.05) # 进行KEGG富集分析 最后,你可以使用clusterProfiler包中的函数来可视化KEGG富集分析结果,比如绘制富集通路的柱状、网络等。以下是一个绘制柱状的示例代码: barplot(gene_pathway, showCategory = 10) # 显示前10个富集通路 通过以上步骤,你就可以在R语言中进行KEGG富集分析了。请注意,根据你的具体需求,你可能还需要进行一些参数的调整和结果的解释。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [R语言clusProfiler进行GO与KEGG富集分析](https://blog.csdn.net/Joey_Liu666/article/details/124988292)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [生信分析论文套路R语言代码](https://download.csdn.net/download/thtfhtfhvyyy/87244940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值