解释R码函数
我想进行路径富集分析。 我有21个重要基因列表,我想检查多种类型的途径(即检查KEGG途径、GOterms、复合物等的富集情况) 我在一篇旧的生物化学文章中找到了这个代码示例。然而,我很难适应它为自己 首先,, 1-这意味着什么?我不知道这个多冒号语法解释R码函数,r,R,我想进行路径富集分析。 我有21个重要基因列表,我想检查多种类型的途径(即检查KEGG途径、GOterms、复合物等的富集情况) 我在一篇旧的生物化学文章中找到了这个代码示例。然而,我很难适应它为自己 首先,, 1-这意味着什么?我不知道这个多冒号语法 hyperg <- Category:::.doHyperGInternal hyperg出现错误的原因是,您似乎没有从bioconductor安装类别软件包。我怀疑这是因为三重冒号操作符:。此运算符非常类似于双冒号运算符:。使用::可以
hyperg <- Category:::.doHyperGInternal
hyperg出现错误的原因是,您似乎没有从bioconductor安装类别
软件包。我怀疑这是因为三重冒号操作符:
。此运算符非常类似于双冒号运算符:
。使用::
可以从包中访问导出的对象而无需加载,而::
允许访问非导出对象(在本例中,使用类别中的hyperg
函数)。如果安装了类别
软件包,则代码运行时不会出错
关于sapply
声明:
pVals.by.pathway<-t(sapply(genes.by.pathway, hyperg.test, significant.genes, all.geneIDs))
此循环完成后,sapply
函数将输出简化为矩阵。然而,通过采用转置t
,输出更加用户友好
一般来说,当试图理解复杂的apply
语句时,我发现最好将它们分成更小的部分,看看对象本身是什么样子。代码的行为是否与您预期的不一样,或者您只是想更好地理解它?不清楚您是否有问题或只是需要信息。@cdeterman-两者,我都不太理解,尤其是最后一行,这使得很难修改它,我得到了这个错误>pVals.by.pathway谢谢,这很有帮助,我不知道您可以通过这种方式将参数传递给函数
library(KEGGREST)
library(org.Hs.eg.db)
# created named list, length 449, eg:
# path:hsa00010: "Glycolysis / Gluconeogenesis"
pathways <- keggList("pathway", "hsa")
# make them into KEGG-style human pathway identifiers
human.pathways <- sub("path:", "", names(pathways))
# for demonstration, just use the first ten pathways
demo.pathway.ids <- head(human.pathways, 10)
demo.pathways <- setNames(keggGet(demo.pathway.ids), demo.pathway.ids)
genes.by.pathway <- lapply(demo.pathways, function(demo.pathway) {
demo.pathway$GENE[c(TRUE, FALSE)]
})
all.geneIDs <- keys(org.Hs.eg.db)
# chose one of these for demonstration. the first (a whole genome random
# set of 100 genes) has very little enrichment, the second, a random set
# from the pathways themselves, has very good enrichment in some pathways
set.seed(123)
significant.genes <- sample(all.geneIDs, size=100)
#significant.genes <- sample(unique(unlist(genes.by.pathway)), size=10)
# the hypergeometric distribution is traditionally explained in terms of
# drawing a sample of balls from an urn containing black and white balls.
# to keep the arguments straight (in my mind at least), I use these terms
# here also
hyperg <- Category:::.doHyperGInternal
hyperg.test <-
function(pathway.genes, significant.genes, all.genes, over=TRUE)
{
white.balls.drawn <- length(intersect(significant.genes, pathway.genes))
white.balls.in.urn <- length(pathway.genes)
total.balls.in.urn <- length(all.genes)
black.balls.in.urn <- total.balls.in.urn - white.balls.in.urn
balls.pulled.from.urn <- length(significant.genes)
hyperg(white.balls.in.urn, black.balls.in.urn,
balls.pulled.from.urn, white.balls.drawn, over)
}
pVals.by.pathway <-
t(sapply(genes.by.pathway, hyperg.test, significant.genes, all.geneIDs))
print(pVals.by.pathway)
pVals.by.pathway<-t(sapply(genes.by.pathway, hyperg.test, significant.genes, all.geneIDs))
pVals.by.pathway <-
t(sapply(genes.by.pathway, hyperg.test, significant.genes=significant.genes, all.genes=all.geneIDs))