R/constructCandidateNetwork.R

Defines functions .select_unique_genes constructCandidateNetwork

Documented in constructCandidateNetwork

#' \code{constructCandidateNetwork}
#'
#' Create a \code{PrixFixeNetwork} by randomly sampling genes from loci. Called within
#' \code{initializePopulation} and not intended to be called by user.
#'
#' @param pf_data a \code{PFData} object generated by \code{PFDataLoader}
#' @param member a character string of either \code{true_members} or \code{null_members}.
#' @param binary boolean for whether to get binary (TRUE) or weighted (FALSE) adjacency matrix.
#' @return A \code{PrixFixeNetwork}
#'
constructCandidateNetwork <- function(pf_data,
                                      member=c("null_members", "true_members"),
                                      binary) {

  # Note: slot = member overrides the synthesize_null_members function because
  # null_members and true_members have their own slots inside a Locus class object.
  candidate_network <- new(
    "PrixFixeNetwork",
    "loci" = sampleLoci(pf_data, slot = "locus_id"),
    "genes" = .select_unique_genes(pf_data, member))

  return(calculateAdjacencyMatrix(candidate_network, pf_data, binary))
}

.select_unique_genes <- function(pf_data, member) {
  # Ensure genes returned are unique for each locus
  #
  # worker function for constructCandidateNetwork
  while (TRUE) {
    genes <- sampleLoci(synthesizeNullMembers(pf_data),
                        slot = member)
    if (length(unique(genes)) == length(genes)) {
      return(genes)
    }
  }
}
princeew/PFFindR documentation built on Dec. 31, 2020, 2:06 a.m.