R/NITUMID.R

Defines functions Nitumid Signature_Match

Documented in Nitumid

Signature_Match <- function(gene_expression,
                            signature_genes=as.character(NITUMID::signature_marker_melanoma$gene_symbol),
                            signature_genes_alias=NULL){

  #check if the gene expression matrix and signature_genes are correctly formatted
  if (!is.matrix(gene_expression) ){
    stop("input gene_expression should be a numeric matrix!")
  }
  if ( !is.character(signature_genes)){
    stop("input signature_genes should be a character vector")
  }
  if (!is.null(signature_genes_alias)){
    if ( !is.character(signature_genes_alias)){
      stop("input signature_genes_alias should be a character vector")
    }
  }

  #match signature genes' position
  primary_match <- match(as.character(NITUMID::signature_marker_melanoma$gene_symbol),rownames(gene_expression))
  if (!is.null(signature_genes_alias)){
    second_match <- match(final_marker_v5_geneSymbol,rownames(gene_expression))
    primary_match[which(is.na(primary_match))] <- second_match[which(is.na(primary_match))]
  }
  missing_row_index <- which(is.na(primary_match) )
  #need to put a random number for missing signatures otherwise the NMF function will fail
  primary_match[which(is.na(primary_match) )] <- 1221
  #return
  return(list(matched_index = primary_match, missing_row_index=missing_row_index))
}


#' Deconvolution Methods NITUMID
#' NITUMID helps users to Estimate of cellular fractions.
#' @param bulkdata A matrix with genes in rows and samples in columns.
#' @param signature A data-frame containing signatures of different cell types.
#' @param allmarkers some of gene factors
#' @param if.bulk Default is Ture
#' @import NITUMID
#' @return A data frame of Mixed cellular fractions.
#' @export
#'
#' @examples
#'
#'
#' Bulk <- Bulk_GSE60424
#' res <- Nitumid(bulkdata = Bulk)



Nitumid <- function(bulkdata,
                    signature = NULL,
                    allmarkers = NITUMID::signature_marker_melanoma$gene_symbol,
                    if.bulk = TRUE) {

  Y <- bulkdata
  match_outcome <- Signature_Match(gene_expression = as.matrix(Y),
                                   signature_genes = as.character(allmarkers))
  NITUMID_out <- NITUMID::NITUMID(Y = Y[match_outcome$matched_index, ],
                                  if.bulk = if.bulk,
                                  A = NITUMID::A_melanoma_v5,
                                  row_index = setdiff(seq(1,53), match_outcome$missing_row_index),
                                  row_mean = NITUMID::row_mean_v5)


  best_index <- which.max(NITUMID_out$consistency_table)
  consistency_round <- round(NITUMID_out$consistency_table,
                             4)
  if (length(which(consistency_round == max(consistency_round))) >
      1) {
    message("There are multiple 'best' consistency, please look at all results manually")
    return(composite_result = NITUMID_out)
  }
  res <- list(W = NITUMID_out$result[[best_index]]$W,
              H = NITUMID_out$result[[best_index]]$H)

  res_Nitumid <- res$H

  return(res_Nitumid)
}
libcell/deconvBench documentation built on Sept. 24, 2022, 12:36 p.m.