R/mle.getPatientBitstrings.r

Defines functions mle.getPatientBitstrings

Documented in mle.getPatientBitstrings

#' Generate patient-specific bitstrings from adaptive network walk.
#'
#' This function calculates the bitstrings (1 is a hit; 0 is a miss) associated with the adaptive network walk made by the diffusion algorithm trying to find the variables in the encoded subset, against the background knowledge graph.
#' @param ig - An igraph object of the background knowledge graph.
#' @param path - The filepath to a directory you will store patient-specific RData files in.
#' @param data.pvals - The matrix that gives the perturbation strenght significance for all variables (columns) for each patient (rows)
#' @param patient - The row number (integer) in data.pvals associated with the patient being processed.
#' @export mle.getPatientBitstrings
#' @examples
#' mle.getPatientBitstrings(ig, path, data.pvals, patient)
mle.getPatientBitstrings <- function(ig, path, data.pvals, patient) {
  optimalBitString <- list()
  print(sprintf("Processing patient (%s) %d / %d to generate permutations...", rownames(data.pvals)[patient], patient, nrow(data.pvals)))
  # Top k perturbed metabolites for patient p
  for (k in 1:kmax) {
    patient.sig.nodes <- names(sort(data.pvals[patient,], decreasing = FALSE))[1:k]
    permutationByStartNode <- mle.getPermutations(patient.sig.nodes, patient, ig)
    # Phase 2: Find optimal information content after compression.
    bitStrings.pt <- list()
    for (sn in 1:length(patient.sig.nodes)) {
      startNode <- patient.sig.nodes[sn]
      current_node_set <- permutationByStartNode[[startNode]]
      bitStrings.pt[[sn]] <- as.numeric(current_node_set %in% patient.sig.nodes)
      names(bitStrings.pt[[sn]]) <- current_node_set
    }
    tmp <- unlist(lapply(bitStrings.pt, function(i) sum(i)))
    bitStrings.pt <- bitStrings.pt[which(tmp==max(tmp))]
    optimalBitString[[k]] <- paste(bitStrings.pt[[which.min(unlist(lapply(bitStrings.pt, function(i) sum(which(i==1)))))]], collapse="")
    tmp <- bitStrings.pt[[which.min(unlist(lapply(bitStrings.pt, function(i) sum(which(i==1)))))]]
    directly <- patient.sig.nodes[-which(patient.sig.nodes %in% names(which(tmp==1)))]
    if (length(directly)>0) {
      names(optimalBitString[[k]]) <- sprintf("%s{+%s}", paste(names(which(tmp==1)), collapse="/"), paste(directly, collapse="/"))
    } else {
      names(optimalBitString[[k]]) <- paste(names(which(tmp==1)), collapse="/")
    }
  }
  save(optimalBitString, file=sprintf("%s/%s-Bitstrings.RData", path, rownames(data.pvals)[patient]))
}
lashmore/MolEndoMatch documentation built on May 5, 2019, 8:02 p.m.