Skip to content
Open
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
65 changes: 34 additions & 31 deletions R/comparisonMatrix.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Create a matrix of which pairwise comparisons to make
#'
#'
#' Construct a matrix of pairwise comparisons (pairs can be either samples or groups)
#'
#'
#' @param ftmsObj ftmsData object
#' @param comparisons dictates which pairwise comparisons to make. 'all' will create a matrix for all pairwise comparisons, 'control' will create a matrix for all comparisons against a specified control group, 'one-factor' will create a matrix of pairwise comparisons to be made where only one 'main_effect' changes between the two groups, or a list of specific comparisons to be made (e.g., list(c("Group1","Group2"),c("Group3","Group4"))) can be given.
#' @param control if wanting to only compare against a control, must specify which group or sample is the control
Expand All @@ -10,29 +10,32 @@ comparisonMatrix <- function(ftmsObj, comparisons, control=NULL) {
if (missing(ftmsObj)) stop("ftmsObj is missing")
if (missing(comparisons)) stop("comparisons is missing")
if (!inherits(ftmsObj, "ftmsData")) stop("ftmsObj must be of type ftmsData")

groupDF <- attr(ftmsObj, "group_DF")

if (is.null(groupDF)) { ## this means each sample is its own group so construct a dummy groupDF

if(all(tolower(comparisons) == "one-factor")){ stop("The 'one-factor' specification cannot be used without a group data frame. Please use 'group_designation' first.") }

samp.names <- unique(ftmsObj$f_data[, getFDataColName(ftmsObj)])
groupDF <- data.frame(Sample=samp.names, Group=samp.names)
colnames(groupDF)[1] <- getFDataColName(ftmsObj)
} else {
if(is.factor(groupDF$Group)){groupDF$Group <- droplevels(groupDF$Group)}
}

if (!missing(control) & !is.null(control)) {
if (!(control %in% groupDF$Group)) {
stop(paste0("control column '", control, "' not found in the data"))
}
}

# determine which pairwise comparisons to make
if(is.list(comparisons)){
if (!all(unlist(comparisons) %in% groupDF$Group)) {
stop("not all groups specified in pairs parameter were found in the data")
}

pairs <- do.call(cbind, comparisons)
} else if(tolower(comparisons) == "all"){
pairs <- combn(levels(as.factor(groupDF$Group)),2)
Expand All @@ -52,62 +55,62 @@ comparisonMatrix <- function(ftmsObj, comparisons, control=NULL) {
}else{
stop("check that comparisons argument is either 'all', 'control', 'one-factor', or a list of specific comparisons")
}

return(pairs)

}

#' Create a list of which pairwise comparisons to make
#'
#'
#' Construct a list of pairwise comparisons, where only
#' one main effect changes between each group
#'
#'
#' @param ftmsObj ftmsData object
#' @param groupDF group data frame from ftmsData object
#' @return a list of which pairwise comparisons to make

one_factor_change <- function(ftmsObj, groupDF){

# Get only uniuqe groups
df <- groupDF[!duplicated(groupDF$Group),]

# Remove Sample and Group columns
if(ncol(df) > 2){
df <- df[,-which(colnames(df) %in% c(getFDataColName(ftmsObj), "Group"))]
}else{
df <- as.data.frame(df[,-which(colnames(df) %in% getFDataColName(ftmsObj))])
colnames(df) <- "Group"
}

# Make sure it's character, not factor
df <- apply(df, 2, as.character)

# Compare each row to every other row
res <- lapply(c(1:(nrow(df)-1)), function(i){

res <- lapply(c((i+1):nrow(df)), function(j){

temp <- apply(as.data.frame(df[c(i,j),]), 2, function(k){

k[1] == k[2]

})
data.frame(Grp1=paste(df[i,], collapse="_"),
Grp2=paste(df[j,], collapse="_"),

data.frame(Grp1=paste(df[i,], collapse="_"),
Grp2=paste(df[j,], collapse="_"),
Compare = (sum(temp) == (ncol(df)-1)))

})

return(do.call(rbind, res))

})

res <- do.call(rbind, res)
fin <- subset(res, Compare)

ret <- lapply(c(1:nrow(fin)), function(x) c(as.character(fin$Grp1)[x], as.character(fin$Grp2)[x]))

return(ret)
}

}
63 changes: 33 additions & 30 deletions R/mapCompoundsToModules.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' Map compound level data to modules in MetaCyc
#'
#'
#' Map compound data to MetaCyc modules The database used
#' is determined by the database previously used to map peaks to compounds.
#' For MetaCyc, modules are pathways that are not super-pathways
#' @param compoundObj an object of type compoundData
#' @return moduleData object
#'
#'
#' @author Amanda White
#'
#'
#' @export
mapCompoundsToModules <- function(compoundObj) {
if (!inherits(compoundObj, "compoundData")) {
Expand All @@ -25,17 +25,17 @@ mapCompoundsToModules <- function(compoundObj) {
compounds <- mc_compounds
data("mc_compound_reaction_map")
comp_rxn_map <- mc_compound_reaction_map

# mapping from compound --> reaction --> module node
data("mc_reaction_module_node_map")
reaction_module_node_map <- mc_reaction_module_node_map

}

if (getDataScale(compoundObj) != "pres") {
compoundObj <- edata_transform(compoundObj, data_scale="pres")
}

comp_rxn <- comp_rxn_map
ind <- unlist(lapply(comp_rxn, function(x) return(all(is.null(x)) || all(is.na(x)))))
comp_rxn <- comp_rxn[!ind]
Expand All @@ -47,44 +47,47 @@ mapCompoundsToModules <- function(compoundObj) {
comp_rxn_mod <- dplyr::inner_join(comp_rxn, reaction_module_node_map, by=c('Reaction'='REACTION'))
comp_rxn_mod <- dplyr::rename(comp_rxn_mod, Module = MODULE, Module_Node = MODULE_NODE) %>%
dplyr::mutate(Module_Node_Comb=paste0(Module, ": ", Module_Node))

# add compounds to e_data
e_data <- dplyr::select_(compoundObj$e_meta, getCompoundColName(compoundObj), getEDataColName(compoundObj)) %>%

# add compounds to e_data
e_data <- compoundObj$e_meta %>%
dplyr::select(dplyr::all_of(c(
getCompoundColName(compoundObj),
getEDataColName(compoundObj)
))) %>%
dplyr::left_join(compoundObj$e_data, by=getEDataColName(compoundObj)) %>%
dplyr::select(-dplyr::matches(getEDataColName(compoundObj)))

# join e_data to compound/reaction/module mapping
join_by <- c("Compound")
names(join_by) <- getCompoundColName(compoundObj)
e_data <- dplyr::inner_join(comp_rxn_mod, e_data, by=join_by)
e_data <- dplyr::inner_join(comp_rxn_mod, e_data, by=join_by)

# get compounds observed in this dataset for each reaction (for e_meta)
observed_comp_per_rxn <- e_data %>%
dplyr::mutate(n_samples_848234=rowSums(dplyr::select(., -dplyr::one_of(c(getCompoundColName(compoundObj)), "Reaction", "Module_Node", "Module", "Module_Node_Comb")))) %>%
dplyr::select_(getCompoundColName(compoundObj), "Module_Node_Comb", "n_samples_848234") %>%
observed_comp_per_rxn <- e_data %>%
dplyr::mutate(n_samples_848234 = rowSums(dplyr::select(.,-dplyr::any_of(c(getCompoundColName(compoundObj),"Reaction", "Module_Node", "Module", "Module_Node_Comb"))))) %>%
dplyr::select(dplyr::all_of(c(getCompoundColName(compoundObj), "Module_Node_Comb", "n_samples_848234"))) %>%
dplyr::filter(n_samples_848234 > 0) %>%
dplyr::select(-n_samples_848234) %>%
dplyr::group_by(Module_Node_Comb) %>%
dplyr::rename_(Compound = getCompoundColName(compoundObj)) %>%
dplyr::rename(Compound = getCompoundColName(compoundObj)) %>%
dplyr::summarise(Compounds_in_Dataset=paste(Compound, collapse=";"))

e_meta <- dplyr::select(e_data, Module_Node, Module, Module_Node_Comb) %>%
dplyr::left_join(observed_comp_per_rxn, by="Module_Node_Comb")

## Calculate number of UNIQUE compounds per node
e_data <- e_data %>%
dplyr::group_by_("Module_Node_Comb", getCompoundColName(compoundObj)) %>%
dplyr::summarise_if(is.numeric, dplyr::funs(sum)) %>%
dplyr::ungroup()

dplyr::group_by(dplyr::across(dplyr::all_of(c("Module_Node_Comb",getCompoundColName(compoundObj))))) %>%
dplyr::summarise(dplyr::across(dplyr::where(is.numeric), sum),.groups = "drop")

e_data <- e_data %>%
dplyr::mutate_if(is.numeric, dplyr::funs(ifelse(.>0, 1, 0))) %>%
dplyr::select(-dplyr::matches(getCompoundColName(compoundObj))) %>%
dplyr::group_by(Module_Node_Comb) %>%
dplyr::summarise_all(dplyr::funs(sum)) %>%
dplyr::summarise_all(dplyr::funs(sum)) %>%
dplyr::ungroup() %>%
as.data.frame()

# get # OBSERVABLE compounds per reaction subject to mass filter applied to compoundObj
obs_comp <- dplyr::inner_join(comp_rxn_mod, compounds, by=c('Compound'='COMPOUND'))
if (!is.null(attr(compoundObj, "filters")) && !is.null(attr(compoundObj, "filters")$massFilt)) {
Expand All @@ -96,15 +99,15 @@ mapCompoundsToModules <- function(compoundObj) {
stop(paste("Unknown database:", db))
}
}

obs_comp <- dplyr::group_by(obs_comp, Module_Node_Comb) %>%
dplyr::summarise(`N_Observable_Compounds`=dplyr::n_distinct(Compound))
e_meta <- dplyr::left_join(e_meta, obs_comp, by="Module_Node_Comb") %>% as.data.frame()
e_meta <- dplyr::arrange(e_meta, Module, Module_Node_Comb)
result <- as.moduleData(e_data, compoundObj$f_data, e_meta, edata_cname = "Module_Node_Comb",
fdata_cname=getFDataColName(compoundObj), module_cname="Module",
module_node_cname = "Module_Node", instrument_type = getInstrumentType(compoundObj),

result <- as.moduleData(e_data, compoundObj$f_data, e_meta, edata_cname = "Module_Node_Comb",
fdata_cname=getFDataColName(compoundObj), module_cname="Module",
module_node_cname = "Module_Node", instrument_type = getInstrumentType(compoundObj),
db=getDatabase(compoundObj))

#attributes from compoundData to carry forward:
Expand All @@ -113,7 +116,7 @@ mapCompoundsToModules <- function(compoundObj) {
# result <- ftmsRanalysis:::setInstrumentType(result, getInstrumentType(compoundData))
result <- ftmsRanalysis:::setDataScale(result, "count")
attr(result, "filters") <- attr(compoundObj, "filters")

## TODO: are there any other cnames that need to be carried through??
if (!is.null(attr(compoundObj, "cnames")$extraction_cname)) {
attr(result, "cnames")$extaction_cname <- attr(compoundObj, "cnames")$extraction_cname
Expand Down
Loading
Loading