Skip to content

Commit

Permalink
modified: R/General_functions.R
Browse files Browse the repository at this point in the history
	new file:   man/Fun.enrich_withFC.Rd
  • Loading branch information
cweng committed Dec 17, 2023
1 parent 6248974 commit 6519c74
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(DepthSummary)
export(DistObjects)
export(DoDE)
export(FromDist2Graph)
export(Fun.enrich_withFC)
export(GEM_Wrapper)
export(GTSummary)
export(Get_Clonal_Variants)
Expand Down
90 changes: 89 additions & 1 deletion R/General_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,92 @@ pvalues<-c(pvalues,binom.md$p.value)
fulllist<-cbind(query.peaks.motif.summary,FC=query.peaks.motif.summary$ratio/(sum(query.peaks.motif.summary$Freq.y)/sum(query.peaks.motif.summary$Freq.x)),pvalues=pvalues)
fulllist$qvalue<-qvalue(fulllist$pvalues)$qvalues
return(fulllist)
}
}

#' Fun.enrich_withFC
#'
#' This function is to do GSEA enrichment analysis
#' @param markergenesList is a list containing several elements each of which is a vector of gene names
#' @param All.genes the background genes data(all.genes.refer)
#' @param db =msig.db the term database to use, default is the msig.db; data(msig.db)
#' @param qcutoff=0.05 the term to show with q value lower than 0.05
#' @param top=NULL pass a number to only show top # terms
#' @return this will return a simple object with two elements, one is the table to show the significant terms , the other one shows the gene names involved significant terms
#' @export
#' @examples
#' S7G.GSEA<-Fun.enrich_withFC(markergenesList=list(S7G=topS7G.genes),All.genes=all.genes.refer)
Fun.enrich_withFC<-function(markergenesList,All.genes=all.genes.refer
,db=msig.db,qcutoff=0.05,top=NULL)
{
require(qvalue)
binomial.test.resultsList<-list()
resultname<-c()
for (markname in names(markergenesList))
{
if(length((markergenesList[[markname]]))==0)
{
next
}
print(markname)
binomialtest.msig.result<-c()
for (name in names(db))
{
binomialtest.msig.result<-rbind(binomialtest.msig.result,binomialtest.msig.enrch_deplet(as.character(markergenesList[[markname]]),All.genes,name,thedatabase=db))
}
fdr.enrich<-qvalue(binomialtest.msig.result[,2])$qvalues
fdr.deplete<-qvalue(binomialtest.msig.result[,3])$qvalues
FDR.combined<-c()
FDR.combined[which(is.na(binomialtest.msig.result$ratio))]<-NA
FDR.combined[which(binomialtest.msig.result$ratio>1)]<-fdr.enrich[which(binomialtest.msig.result$ratio>1)]
FDR.combined[which(binomialtest.msig.result$ratio<=1)]<-fdr.deplete[which(binomialtest.msig.result$ratio<=1)]
if(is.null(FDR.combined))
{
next
}
binomialtest.msig.result<-cbind(binomialtest.msig.result,FDR=FDR.combined)
binomialtest.msig.result<-binomialtest.msig.result[order(binomialtest.msig.result$FDR),]
binomial.test.resultsList<-c(binomial.test.resultsList,list(binomialtest.msig.result))
resultname<-c(resultname,markname)

}

names(binomial.test.resultsList)=resultname
DRscombo<-data.frame()
for (markname in names(binomial.test.resultsList))
{
tmp<-subset(binomial.test.resultsList[[markname]],FDR<qcutoff)
row.names(tmp)<-tmp[,"name"]
colnames(tmp)[c(4,5)]<-paste(markname,colnames(tmp)[c(4,5)],sep="_")
for (othermarkname in setdiff(names(binomial.test.resultsList),markname))
{
tmp2<-binomial.test.resultsList[[othermarkname]]
row.names(tmp2)<-tmp2[,"name"]
tmp2<-tmp2[row.names(tmp),c("ratio","FDR")]
colnames(tmp2)<-paste(othermarkname,colnames(tmp2),sep="_")
tmp<-cbind(tmp,tmp2)
}
tmp<-tmp[,c(-1,-2,-3)]
if (!is.null(top))
{
tmp<-head(tmp,n=top)
}
DRscombo<-Tomerge.col(DRscombo,tmp)
}
all.list<-c()
for (term in row.names(DRscombo))
{
cur.genelist<-c()
for (i in 1:length(markergenesList))
{
cur.genes<-markergenesList[[i]][markergenesList[[i]] %in% as.character(db[[term]])]
cur.genelist<-c(cur.genelist,list(cur.genes))
}
names(cur.genelist)<-names(markergenesList)
all.list<-c(all.list,list(cur.genelist))

}
names(all.list)<-row.names(DRscombo)
#)
#DRscombo<-DRscombo[tmpcondition,]
return(list(DRscombo,all.list=all.list))
}
34 changes: 34 additions & 0 deletions man/Fun.enrich_withFC.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6519c74

Please sign in to comment.