Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functions implementation and update of the code #6

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ Imports: limma, stats, methods, RColorBrewer, clusterProfiler,GGally,
Suggests: RUnit, BiocGenerics, knitr (>= 1.6), rmarkdown, BiocStyle (>=
1.3), roxygen2, doSNOW
VignetteBuilder: knitr
RoxygenNote: 6.1.1
RoxygenNote: 7.2.3
NeedsCompilation: no
Packaged: 2020-05-05 11:56:29 UTC; ewoewi
147 changes: 93 additions & 54 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,54 +1,93 @@
# Generated by roxygen2: do not edit by hand

export(ClusterGeneSets)
export(CombineGeneSets)
export(GenesPerGeneSet)
export(GetSTRINGdbPerGeneSets)
export(HighlightGeneSets)
export(LoadGeneSets)
export(ManageGeneSets)
export(MergeObjects)
export(ORAGeneSets)
export(ObjectCreator)
export(OptimalGeneSets)
export(PlotGeneDendrogram)
export(PlotGeneNetworks)
export(PlotGeneSets)
export(ShowExperimentdata)
export(ShowGeneSets)
export(ShowMeta)
export(VennDiagram)
export(WriteGeneSets)
export(plotSTRINGdbPerGeneSets)
exportClasses(PathwayObject)
exportMethods(GenesPerGeneSet)
exportMethods(PlotGeneDendrogram)
exportMethods(PlotGeneNetworks)
exportMethods(PlotGeneSets)
exportMethods(ShowExperimentdata)
exportMethods(ShowGeneSets)
exportMethods(ShowMeta)
exportMethods(WriteGeneSets)
import(GGally)
import(RColorBrewer)
import(STRINGdb)
import(WebGestaltR)
import(cluster)
import(clusterProfiler)
import(clustree)
import(factoextra)
import(limma)
import(methods)
import(network)
import(org.Hs.eg.db)
import(org.Mm.eg.db)
import(pheatmap)
import(readxl)
import(stats)
import(utils)
importFrom(grDevices,colorRampPalette)
importFrom(stats,dist)
importFrom(stats,hclust)
importFrom(stats,kmeans)
importFrom(utils,read.csv)
importFrom(utils,write.table)
# Generated by roxygen2: do not edit by hand

export(BreakUpCluster)
export(ClusterGeneSets)
export(CombineGeneSets)
export(GenesPerGeneSet)
export(GetOptimalCluster)
export(GetSTRINGdbPerGeneSets)
export(HighlightGeneSets)
export(LoadGeneSets)
export(ManageGeneSets)
export(MeanClusterDifferences)
export(MergeObjects)
export(ORAGeneSets)
export(ObjectCreator)
export(OptimalGeneSets)
export(PathwayObject)
export(PlotClusterTissueExpression)
export(PlotGeneDendrogram)
export(PlotGeneNetworks)
export(PlotGeneSets)
export(PlotLD)
export(PlotPathwayCluster)
export(PlotTissueExpression)
export(PlotTree)
export(PlotTreePathway)
export(ShowExperimentdata)
export(ShowGeneSets)
export(ShowMeta)
export(ShowPathwayCluster)
export(UmapGeneSets)
export(VennDiagram)
export(WriteGeneSets)
export(plotSTRINGdbPerGeneSets)
exportClasses(PathwayObject)
exportMethods(GenesPerGeneSet)
exportMethods(PlotGeneDendrogram)
exportMethods(PlotGeneNetworks)
exportMethods(PlotGeneSets)
exportMethods(ShowExperimentdata)
exportMethods(ShowGeneSets)
exportMethods(ShowMeta)
exportMethods(TissueExpressionPerGeneSet)
exportMethods(WriteGeneSets)
import(AnnotationDbi)
import(ComplexHeatmap)
import(GGally)
import(GO.db)
import(GetoptLong)
import(RColorBrewer)
import(STRINGdb)
import(WebGestaltR)
import(bigstatsr)
import(cluster)
import(clusterProfiler)
import(clustree)
import(cowplot)
import(doParallel)
import(dplyr)
import(factoextra)
import(foreach)
import(ggdendro)
import(ggnewscale)
import(ggplot2)
import(ggtree)
import(ggwordcloud)
import(grid)
import(httr)
import(jsonlite)
import(limma)
import(methods)
import(network)
import(org.Hs.eg.db)
import(org.Mm.eg.db)
import(parallel)
import(patchwork)
import(pheatmap)
import(readxl)
import(reshape2)
import(rgl)
import(simplifyEnrichment)
import(slam)
import(stats)
import(stringr)
import(tidyverse)
import(umap)
import(utils)
importFrom(grDevices,colorRampPalette)
importFrom(stats,dist)
importFrom(stats,hclust)
importFrom(stats,kmeans)
importFrom(utils,read.csv)
importFrom(utils,write.table)
132 changes: 111 additions & 21 deletions R/CombineGeneSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#' @import limma
#' @import org.Hs.eg.db
#' @import org.Mm.eg.db
#' @import bigstatsr
#' @import parallel
#' @import doParallel
#' @import foreach
#' @import limma
#' @param Object A PathwayObject.
#' @param combineMethod lets the functions know if the standard RR, a Jaccard index or Cohens Kappa.
#' @param combineMethod.supplied a function which parameter A and parameter B.
Expand Down Expand Up @@ -45,7 +50,7 @@
#'


CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supplied, display="Condensed")
CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supplied, display="Condensed", threads=1)
{
message("[=========================================================]")
message("[<<<< CombineGeneSets START >>>>>]")
Expand Down Expand Up @@ -126,6 +131,7 @@ CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supp
pathways.i <- do.call(rbind, Object@Data)

}
pathways.i2 <- pathways.i[!duplicated(pathways.i$Pathways), ]
pathways.i$GeneSets <- paste("GeneSet",1:nrow(pathways.i),sep="")

################################################
Expand Down Expand Up @@ -155,20 +161,20 @@ CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supp

}


#####################################
##---------Calculate RR------------##
#####################################
if(combineMethod == "Standard" | combineMethod == "RR")
{
message("calulating RR")

}else{
message(paste("calulating",combineMethod))
}
pathways.mtx <- matrix(data = 0, nrow = nrow(pathways.i), ncol = length(molecules))
colnames(pathways.mtx) <- molecules
rownames(pathways.mtx) <- as.character(pathways.i$Pathways)
#


for(list.i in 1:nrow(pathways.i))
{
Expand All @@ -178,11 +184,37 @@ CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supp

##
pathways.i$RR_name <- paste(pathways.i[,"Groups"],rownames(pathways.mtx),sep="_")

##

RR <- matrix(data = 0, nrow = nrow(pathways.mtx), ncol = nrow(pathways.mtx))
rownames(RR) <- pathways.i$RR_name
colnames(RR) <- pathways.i$RR_name
if (threads == 1) {
RR <- matrix(data = 0, nrow = nrow(pathways.mtx), ncol = nrow(pathways.mtx))
rownames(RR) <- pathways.i$RR_name
colnames(RR) <- pathways.i$RR_name
} else {
RR <- as_FBM(matrix(data = 0, nrow = nrow(pathways.mtx), ncol = nrow(pathways.mtx)))
}

## For RR with unique pathways
pathways.mtx2 <- matrix(data = 0, nrow = nrow(pathways.i2), ncol = length(molecules))
colnames(pathways.mtx2) <- molecules
rownames(pathways.mtx2) <- as.character(pathways.i2$Pathways)

for(list.i2 in 1:nrow(pathways.i2))
{
molecules.i2 <- toupper(as.vector(strsplit2(pathways.i2[list.i2, "Molecules"], split=Object@metadata[1,"seperator"])))
pathways.mtx2[list.i2, molecules.i2] <- 1
}

pathways.i2$RR_name <- rownames(pathways.mtx2)

if (threads == 1) {
RR2 <- matrix(data = 0, nrow = nrow(pathways.mtx2), ncol = nrow(pathways.mtx2))
rownames(RR2) <- pathways.i2$RR_name
colnames(RR2) <- pathways.i2$RR_name
} else {
RR2 <- as_FBM(matrix(data = 0, nrow = nrow(pathways.mtx2), ncol = nrow(pathways.mtx2)))
}

############################################
#---------User supplied function-----------#
Expand Down Expand Up @@ -254,27 +286,85 @@ CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supp
#######################################
#---------standard function-----------#
#######################################
RR.function <- function(x)
{
J <- x
I <- pathways.mtx[Disease1,]
N <- (ncol(pathways.mtx))#number of molecules

Pi <- sum(I)#number of molecules in pathway 1
Pj <- sum(J)#number of molecules in pathway 2

Cij <- sum(names(J)[J==1] %in% names(I)[I==1])

if(Cij == 1){Cij <- 0.1}#Adjusted for pathways with 1 molecules
RRij <- (Cij*N)/((Pi*Pj) - Cij)
return(RRij)
}

RR.function2 <- function(x)
{
J <- x
I <- pathways.mtx2[Disease1,]
N <- (ncol(pathways.mtx2))#number of molecules

Pi <- sum(I)#number of molecules in pathway 1
Pj <- sum(J)#number of molecules in pathway 2

Cij <- sum(names(J)[J==1] %in% names(I)[I==1])

if(Cij == 1){Cij <- 0.1}#Adjusted for pathways with 1 molecules
RRij <- (Cij*N)/((Pi*Pj) - Cij)
return(RRij)
}

if(combineMethod=="Standard")
{
for(Disease1 in 1:nrow(RR))
{
RR.function <- function(x)
if (threads == 1) {
for(Disease1 in 1:nrow(RR))
{
J <- x
I <- pathways.mtx[Disease1,]
N <- (ncol(pathways.mtx))#number of molecules

Pi <- sum(I)#number of molecules in pathway 1
Pj <- sum(J)#number of molecules in pathway 2
RR[Disease1,] <- apply(X = pathways.mtx, MARGIN = 1, FUN = RR.function )
}

Cij <- sum(names(J)[J==1] %in% names(I)[I==1])
if(Cij == 1){Cij <- 0.1}#Adjusted for pathways with 1 molecules
RRij <- (Cij*N)/((Pi*Pj) - Cij)
return(RRij)
for(Disease1 in 1:nrow(RR2))
{
RR2[Disease1,] <- apply(X = pathways.mtx2, MARGIN = 1, FUN = RR.function2 )
}
RR[Disease1,] <- apply(X = pathways.mtx, MARGIN = 1, FUN = RR.function )

} else {#parallelization
message(paste0("Parallelizing processes, ", threads, " cores have been selected. "), sep="")

if (threads > detectCores()) {
message("More cores have been selected than detected.")
threads <- detectCores()
message(paste0("The maximum number of cores will be used:", threads, " cores."))
}

cl <- makeCluster(threads)
registerDoParallel(cl)
foreach(Disease1=1:nrow(RR[])) %dopar% {

RR[Disease1,] <- apply(X = pathways.mtx, MARGIN = 1, FUN = RR.function )

}
stopCluster(cl)

cl <- makeCluster(threads)
registerDoParallel(cl)
foreach(Disease1=1:nrow(RR2[])) %dopar% {

RR2[Disease1,] <- apply(X = pathways.mtx2, MARGIN = 1, FUN = RR.function2 )

}
stopCluster(cl)

RR <- RR[]
rownames(RR) <- pathways.i$RR_name
colnames(RR) <- pathways.i$RR_name

RR2 <- RR2[]
rownames(RR2) <- pathways.i2$RR_name
colnames(RR2) <- pathways.i2$RR_name
}
}
######################################
Expand All @@ -283,7 +373,7 @@ CombineGeneSets <- function(Object, combineMethod="Standard", combineMethod.supp

Object@Data <- list(pathways.i)
[email protected] <- as.data.frame(RR)

[email protected] <- as.data.frame(RR2)


message("-----------------------------------------------------------")
Expand Down
3 changes: 2 additions & 1 deletion R/LoadGeneSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ LoadGeneSets <- function(file_location = canonical.files,
metadata = metadata,
Data.RR = data.frame(),
plot = list(aka2 = data.frame(),
aka3 = data.frame()))
aka3 = data.frame()),
dfTissue = data.frame())


message("-----------------------------------------------------------")
Expand Down
8 changes: 5 additions & 3 deletions R/ObjectCreator.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@
#'
#'
#'

#'
ObjectCreator <- function(Pathways, Molecules, Groups, Source, Type, structure, sep, organism = NA)
ObjectCreator <- function(Pathways, Molecules, Groups, Source, Type, structure, sep, organism = NA, Pvalues = NA, enrichmentScore = NA)
{
message("[=========================================================]")
message("[<<<< ObjectCreator START >>>>>]")
Expand All @@ -67,7 +66,9 @@ ObjectCreator <- function(Pathways, Molecules, Groups, Source, Type, structure,
Data$Pathways <- Pathways
Data$Molecules <- Molecules
Data$Groups <- Groups
Data$Pval <- Pvalues
Data$Type <- rep(Type, times = nrow(Data))
Data$enrichScore <- enrichmentScore

for(can.i in 1:nrow(Data))
{
Expand Down Expand Up @@ -113,7 +114,8 @@ ObjectCreator <- function(Pathways, Molecules, Groups, Source, Type, structure,
metadata = metadata,
Data.RR = data.frame(),
plot = list(aka2 = data.frame(),
aka3 = data.frame()))
aka3 = data.frame()),
dfTissue = data.frame())


message("-----------------------------------------------------------")
Expand Down
Loading