Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Depends:
R (>= 3.6)
Suggests:
curatedTCGAData,
TCGAutils,
rmarkdown,
testthat,
knitr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(convertProfileToNetworks)
export(countIntType)
export(countIntType_batch)
export(countPatientsInNet)
export(createNetFuncFromSimList)
export(createPSN_MultiData)
export(dataList2List)
export(enrichLabelNets)
Expand Down Expand Up @@ -64,6 +65,7 @@ export(sparsify2)
export(sparsify3)
export(splitTestTrain)
export(splitTestTrain_resampling)
export(subsampleValidationData)
export(tSNEPlotter)
export(thresholdSmoothedMutations)
export(updateNets)
Expand Down
31 changes: 23 additions & 8 deletions R/buildPredictor.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#' So keys(groupList[["rna"]]) would have pathway names, generating one PSN
#' per pathways, and values(groupList[["rna"]]) would be genes that would be
#' grouped for the corresponding pathwayList.
#' @param sims (list) rules to create similarity networks from input data. Keys are names of
#' data layers and should be identical to names(groupList). Values is either a character
#' for built-in similarity functions; call allowedSims() to see full list; or a custom function.
#' @param makeNetFunc (function) user-defined function for creating the set
#' of input PSN provided to netDx. See createPSN_MultiData()::customFunc.
#' @param outDir (char) directory where results will be stored. If this
Expand Down Expand Up @@ -171,7 +174,8 @@
#' # makeNetFunc=makeNets, ### custom network creation function
#' # outDir=paste(tempdir(),"pred_output",sep=getFileSep()), ## absolute path
#' # numCores=16L,featScoreMax=2L, featSelCutoff=1L,numSplits=2L)
buildPredictor <- function(dataList,groupList,outDir=tempdir(),makeNetFunc,
buildPredictor <- function(dataList,groupList,outDir=tempdir(),
makeNetFunc=NULL,sims=NULL,
featScoreMax=10L,trainProp=0.8,numSplits=10L,numCores,JavaMemory=4L,
featSelCutoff=9L,keepAllData=FALSE,startAt=1L, preFilter=FALSE,
impute=FALSE,preFilterGroups=NULL, imputeGroups=NULL,logging="default",
Expand All @@ -196,7 +200,7 @@ if (logging == "all") {
verbose_predict <- FALSE
}

# Check input
# Check input - error handling
if (missing(dataList)) stop("dataList must be supplied.\n")
if (missing(groupList)) stop("groupList must be supplied.\n")
if (length(groupList)<1) stop("groupList must be of length 1+\n")
Expand All @@ -213,13 +217,18 @@ if (!is(groupList,"list") || not_list || names_nomatch ) {
stop(paste(msg,sep=""))
}

# checks either/or provided, sets missing var to NULL
x <- checkMakeNetFuncSims(makeNetFunc=makeNetFunc, sims=sims,groupList=groupList)

if (!is(dataList,"MultiAssayExperiment"))
stop("dataList must be a MultiAssayExperiment")

if (trainProp <= 0 | trainProp >= 1)
stop("trainProp must be greater than 0 and less than 1")
if (startAt > numSplits) stop("startAt should be between 1 and numSplits")

# end check input error handling

megaDir <- outDir
if (file.exists(megaDir)) {
stop(paste("outDir seems to already exist!",
Expand Down Expand Up @@ -274,7 +283,6 @@ if (verbose_default){
}
}


outList <- list()

# create master list of possible networks
Expand All @@ -289,8 +297,14 @@ colnames(tmp) <- c("NetType","NetName")
outList[["inputNets"]] <- tmp

if (verbose_default) {
message("\n\nCustom function to generate input nets:")
print(makeNetFunc)
if (!is.null(makeNetFunc)){
message("\n\nCustom function to generate input nets:")
print(makeNetFunc)

} else {
message("Similarity metrics provided:")
print(sims)
}
message(sprintf("-------------------------------\n"))
}

Expand Down Expand Up @@ -386,8 +400,8 @@ for (rngNum in startAt:numSplits) {

if (verbose_default) message("** Creating features")
createPSN_MultiData(dataList=dats_train,groupList=groupList,
pheno=pheno_id,
netDir=netDir,customFunc=makeNetFunc,numCores=numCores,
pheno=pheno_id,
netDir=netDir,makeNetFunc=makeNetFunc,sims=sims, numCores=numCores,
verbose=verbose_makeFeatures)
if (verbose_default) message("** Compiling features")
dbDir <- compileFeatures(netDir,outDir, numCores=numCores,
Expand Down Expand Up @@ -512,7 +526,8 @@ for (rngNum in startAt:numSplits) {
pheno_id <- setupFeatureDB(pheno,netDir)
createPSN_MultiData(dataList=dats_tmp,groupList=groupList,
pheno=pheno_id,
netDir=netDir,customFunc=makeNetFunc,numCores=numCores,
netDir=netDir,makeNetFunc=makeNetFunc,sims=sims,
numCores=numCores,
filterSet=pTally,verbose=verbose_default)
dbDir <- compileFeatures(netDir,outDir=pDir,numCores=numCores,
verbose=verbose_compileNets,debugMode=debugMode)
Expand Down
2 changes: 1 addition & 1 deletion R/compileFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#'
#' pheno_id <- setupFeatureDB(pheno,netDir)
#' netList <- createPSN_MultiData(dataList=dataList, groupList=groupList,
#' pheno=pheno_id,netDir=netDir,customFunc=makeNets,verbose=TRUE)
#' pheno=pheno_id,netDir=netDir,makeNetFunc=makeNets,verbose=TRUE)
#'
#' outDir <- paste(tmpDir,'dbdir',sep=getFileSep());
#' dir.create(outDir)
Expand Down
30 changes: 22 additions & 8 deletions R/createPSN_MultiData.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
#' with internally-generated identifiers.
#' @param netDir (char) path to directory where networks will be stored
#' @param filterSet (char) vector of networks to include
#' @param customFunc (function) custom user-function to create PSN.
#' @param makeNetFunc (function) custom user-function to create PSN.
#' Must take dataList,groupList,netDir as parameters. Must
#' check if a given groupList is empty (no networks to create) before
#' the makePSN call for it. This is to avoid trying to make nets for datatypes
#' that did not pass feature selection
#' @param sims (list) Similarity metric settings for patient data.
#' Keys must be identical to those of groupList.
#' Values are either of type character, used for built-in similarity functions,
#' or are functions, when a custom function is provided.
#' @param verbose (logical) print messages
#' @param ... other parameters to makePSN_NamedMatrix() or makePSN_RangedSets()
#' @return (char) vector of network names. Side effect of creating the nets
Expand Down Expand Up @@ -95,33 +99,33 @@
#' pheno_id <- setupFeatureDB(colData(brca),netDir)
#' createPSN_MultiData(dataList=datList2,groupList=groupList,
#' pheno=pheno_id,
#' netDir=netDir,customFunc=makeNets,numCores=1)
#' netDir=netDir,makeNetFunc=makeNets,numCores=1)
#' @export
createPSN_MultiData <- function(dataList, groupList, pheno, netDir=tempdir(),
filterSet = NULL,
verbose = TRUE, customFunc, ...) {
verbose = TRUE, makeNetFunc=NULL, sims=NULL, ...) {

if (missing(dataList))
stop("dataList must be supplied.\n")
if (missing(groupList))
stop("groupList must be supplied.\n")

# resolve user-provided IDs with internal IDs
dataList <- lapply(dataList, function(x) {
midx <- match(colnames(x), pheno$ID)
colnames(x) <- pheno$INTERNAL_ID[midx]
x
})

if (!is.null(filterSet)) {
if (length(filterSet) < 1) {
s1 <- "filterSet is empty."
s2 <- "It needs to have at least one net to proceed."
stop(paste(s1, s2, sep = " "))
}
}
if (missing(customFunc))
stop("customFunc must be suppled.\n")



# Filter for nets (potentially feature-selected ones)
if (!is.null(filterSet)) {
Expand All @@ -139,12 +143,22 @@ createPSN_MultiData <- function(dataList, groupList, pheno, netDir=tempdir(),
}
}
groupList <- groupList2
sims <- sims[which(names(sims) %in% names(groupList))]
rm(groupList2)
}

if (!is.null(makeNetFunc)){
# call user-defined function for making PSN
netList <- customFunc(dataList = dataList, groupList = groupList,
netList <- makeNetFunc(dataList = dataList, groupList = groupList,
netDir = netDir, ...)
} else {
netList <- createNetFuncFromSimList(dataList=dataList,
groupList = groupList,
netDir = netDir,
sims = sims,
...
)
}

if (length(netList) < 1)
stop("\n\nNo features created! Filters may be too stringent.\n")
Expand Down
30 changes: 23 additions & 7 deletions R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ return(list(
#' @param EMapPctPass (numeric between 0 and 1) percent of splits for which feature must have score in range
#' [EMapMinScore,EMapMaxScore] to be included for EnrichmentMap visualization
#' @param outDir (char) directory where files should be written
#' @return
#' @export
#' @return (list) 1) GMTfiles (char): GMT files used to create EnrichmentMap in Cytoscape.
#' 2) NodeStyles (char): .txt files used to assign node attributes in Cytoscape. Importantly,
#' attributes include node fill, which indicates the highest consistent score for a given
#' feature.
#' @export
makeInputForEnrichmentMap <- function(model,results,pathwayList,
EMapMinScore=0L, EMapMaxScore=1L,
EMapPctPass=0.5,outDir)
Expand Down Expand Up @@ -214,8 +217,10 @@ return(list(GMTfiles=gmtFiles,NodeStyles=nodeAttrFiles))
#' same class, relative to those of other classes, using Dijkstra distance (calcShortestPath flag).
#' @param dat (MultiAssayExperiment) input data
#' @param groupList (list) feature groups, identical to groupList provided for buildPredictor()
#' @param makeNets (function) Function used to create patient similarity networks. Identical to
#' @param makeNetFunc (function) Function used to create patient similarity networks. Identical to
#' makeNets provided to buildPredictor()
#' @param sims (list) rules for creating PSN. Preferred over makeNetFunc. See buildPredictor()
#' for details.
#' @param selectedFeatures (list) selected features for each class (key of list). This object is returned as
#' part of a call to getResults(), after running buildPredictor().
#' @param plotCytoscape (logical) If TRUE, plots network in Cytoscape.
Expand Down Expand Up @@ -245,9 +250,18 @@ return(list(GMTfiles=gmtFiles,NodeStyles=nodeAttrFiles))
#' colours (colour)
#' 6) outDir (char) value of outDir parameter
#' @export
getPSN <- function(dat, groupList, makeNets, selectedFeatures, plotCytoscape=FALSE,
aggFun="MEAN", prune_pctX=0.30, prune_useTop=TRUE,numCores=1L,calcShortestPath=FALSE
getPSN <- function(dat, groupList,
makeNetFunc=NULL, sims=NULL,
selectedFeatures, plotCytoscape=FALSE,
aggFun="MEAN", prune_pctX=0.30, prune_useTop=TRUE,
numCores=1L,calcShortestPath=FALSE
){


# checks either/or provided, sets missing var to NULL
x <- checkMakeNetFuncSims(makeNetFunc=makeNetFunc,
sims=sims,groupList=groupList)

topPath <- gsub(".profile","", unique(unlist(selectedFeatures)))
topPath <- gsub("_cont.txt","",topPath)

Expand All @@ -262,8 +276,10 @@ for (nm in names(groupList)) {

message("* Making integrated PSN")
psn <-
plotIntegratedPatientNetwork(dat,
groupList=g2, makeNetFunc=makeNets,
plotIntegratedPatientNetwork(
dataList=dat,
groupList=g2, makeNetFunc=makeNetFunc,
sims=sims,
aggFun=aggFun,
prune_pctX=prune_pctX,
prune_useTop=prune_useTop,
Expand Down
4 changes: 2 additions & 2 deletions R/plotEmap.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ plotEmap <- function(gmtFile, nodeAttrFile, netName = "generic",
}

####################################### create EM using given parameters
if (netName %in% getNetworkList()) {
if (netName %in% RCy3::getNetworkList()) {
RCy3::deleteNetwork(netName)
}
em_command <- paste("enrichmentmap build analysisType=\"generic\"",
"gmtFile=", gmtFile, "pvalue=", 1, "qvalue=", 1,
"similaritycutoff=", 0.05, "coefficients=", "JACCARD")
response <- RCy3::commandsGET(em_command)
renameNetwork(netName, getNetworkSuid())
RCy3::renameNetwork(netName, RCy3::getNetworkSuid())

### #annotate the network using AutoAnnotate app
aa_command <- paste("autoannotate annotate-clusterBoosted",
Expand Down
13 changes: 11 additions & 2 deletions R/plotIntegratedPatientNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#' list of lists, where the outer list corresponds to assay (e.g. mRNA,
#' clinical) and inner list to features to generate from that datatype.
#' @param makeNetFunc (function) function to create features
#' @param sims (list) rules for creating PSN. Preferred over makeNetFunc
#' @param setName (char) name to assign the network in Cytoscape
#' @param numCores (integer) number of cores for parallel processing
#' @param prune_pctX (numeric between 0 and 1) fraction of most/least
Expand Down Expand Up @@ -59,14 +60,20 @@
#' @importFrom RColorBrewer brewer.pal
#' @importFrom stats wilcox.test qexp density
#' @export
plotIntegratedPatientNetwork <- function(dataList,groupList,makeNetFunc,
plotIntegratedPatientNetwork <- function(dataList,groupList,
makeNetFunc=NULL,sims=NULL,
setName="predictor",prune_pctX=0.05, prune_useTop=TRUE,
aggFun="MAX",calcShortestPath=FALSE,
showStats=FALSE,
outDir=tempdir(),numCores=1L,nodeSize=50L,edgeTransparency=40L,
nodeTransparency=155L,plotCytoscape=FALSE,
verbose=FALSE) {


# checks either/or provided, sets missing var to NULL
checkMakeNetFuncSims(makeNetFunc=makeNetFunc,
sims=sims,groupList=groupList)

if (missing(dataList)) stop("dataList is missing.")

dat <- dataList2List(dataList, groupList)
Expand All @@ -81,7 +88,9 @@ pheno_id <- setupFeatureDB(pheno,outDir)

createPSN_MultiData(dataList=dat$assays,groupList=groupList,
pheno=pheno_id,
netDir=outDir,customFunc=makeNetFunc,numCores=numCores,
netDir=outDir,
makeNetFunc=makeNetFunc,sims=sims,
numCores=numCores,
verbose=FALSE)
convertProfileToNetworks(
netDir=profDir,
Expand Down
Loading