From 4343bbc96ded4e888fbb74642687b37f262eef5c Mon Sep 17 00:00:00 2001 From: TescoOriginal Date: Mon, 21 Dec 2020 17:04:29 +0000 Subject: [PATCH] extractSignatures new arguments Added absolute contribution argument and an option to increase nruns from default 1 for extractSignatures function. Modified R documentation accordingly. --- R/extractSignatures.R | 20 ++++++-- man/extractSignatures.Rd | 100 +++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 51 deletions(-) diff --git a/R/extractSignatures.R b/R/extractSignatures.R index e5dd92f6..f41e9ba8 100644 --- a/R/extractSignatures.R +++ b/R/extractSignatures.R @@ -6,6 +6,8 @@ #' #' @param mat Input matrix of diemnsion nx96 generated by \code{\link{trinucleotideMatrix}} #' @param n decompose matrix into n signatures. Default NULL. Tries to predict best value for \code{n} by running NMF on a range of values and chooses based on cophenetic correlation coefficient. +#' @param nrun Default 1. Number of runs to perform +#' @param absolute Default FALSE. Returns absolute contribution per sample. #' @param plotBestFitRes plots consensus heatmap for range of values tried. Default FALSE #' @param parallel Default 4. Number of cores to use. #' @param pConstant A small positive value to add to the matrix. Use it ONLY if the functions throws an \code{non-conformable arrays} error @@ -23,7 +25,7 @@ #' @export -extractSignatures = function(mat, n = NULL, plotBestFitRes = FALSE, parallel = 4, pConstant = NULL){ +extractSignatures = function(mat, n = NULL, nrun = 1, absolute = FALSE, plotBestFitRes = FALSE, parallel = 4, pConstant = NULL){ #suppressPackageStartupMessages(require(NMF, quietly = TRUE)) #transpose matrix @@ -57,9 +59,9 @@ extractSignatures = function(mat, n = NULL, plotBestFitRes = FALSE, parallel = 4 message(paste0('-Running NMF for factorization rank: ', n)) if(!is.null(parallel)){ - conv.mat.nmf = NMF::nmf(x = mat, rank = n, .opt = paste0('P', parallel), seed = 123456) + conv.mat.nmf = NMF::nmf(x = mat, rank = n, nrun = nrun, .opt = paste0('P', parallel), seed = 123456) }else{ - conv.mat.nmf = NMF::nmf(x = mat, rank = n, seed = 123456) + conv.mat.nmf = NMF::nmf(x = mat, rank = n, nrun = nrun, seed = 123456) } #Signatures @@ -70,6 +72,11 @@ extractSignatures = function(mat, n = NULL, plotBestFitRes = FALSE, parallel = 4 #Contribution h = NMF::coef(conv.mat.nmf) colnames(h) = colnames(mat) #correct colnames (seems to be mssing with low mutation load) + #Absolute contributions per sample, exposures are not scaled + if (absolute == TRUE) { + h_absolute = h + rownames(h_absolute) = paste("Signature", 1:nrow(h_absolute), sep = "_") + } #For single signature, contribution will be 100% per sample if(n == 1){ h = h/h @@ -80,5 +87,10 @@ extractSignatures = function(mat, n = NULL, plotBestFitRes = FALSE, parallel = 4 } message("-Finished in",data.table::timetaken(start_time)) - return(list(signatures = w, contributions = h, nmfObj = conv.mat.nmf)) + if (absolute == TRUE) { + return(list(signatures = w, contributions = h, absolute_contributions = h_absolute, nmfObj = conv.mat.nmf)) + } + else { + return(list(signatures = w, contributions = h, nmfObj = conv.mat.nmf)) + } } diff --git a/man/extractSignatures.Rd b/man/extractSignatures.Rd index 62fbcfee..4f6e5b32 100644 --- a/man/extractSignatures.Rd +++ b/man/extractSignatures.Rd @@ -1,47 +1,53 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/extractSignatures.R -\name{extractSignatures} -\alias{extractSignatures} -\title{Extract mutational signatures from trinucleotide context.} -\usage{ -extractSignatures( - mat, - n = NULL, - plotBestFitRes = FALSE, - parallel = 4, - pConstant = NULL -) -} -\arguments{ -\item{mat}{Input matrix of diemnsion nx96 generated by \code{\link{trinucleotideMatrix}}} - -\item{n}{decompose matrix into n signatures. Default NULL. Tries to predict best value for \code{n} by running NMF on a range of values and chooses based on cophenetic correlation coefficient.} - -\item{plotBestFitRes}{plots consensus heatmap for range of values tried. Default FALSE} - -\item{parallel}{Default 4. Number of cores to use.} - -\item{pConstant}{A small positive value to add to the matrix. Use it ONLY if the functions throws an \code{non-conformable arrays} error} -} -\value{ -a list with decomposed scaled signatures, signature contributions in each sample and NMF object. -} -\description{ -Decompose a matrix of 96 substitution classes into \code{n} signatures. -} -\details{ -This function decomposes a non-negative matrix into n signatures. -} -\examples{ -\dontrun{ -laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools") -laml <- read.maf(maf = laml.maf) -laml.tnm <- trinucleotideMatrix(maf = laml, ref_genome = 'BSgenome.Hsapiens.UCSC.hg19', prefix = 'chr', -add = TRUE, useSyn = TRUE) -library("NMF") -laml.sign <- extractSignatures(mat = laml.tnm, plotBestFitRes = FALSE, n = 2, pConstant = 0.01) -} -} -\seealso{ -\code{\link{trinucleotideMatrix}} \code{\link{plotSignatures}} \code{\link{compareSignatures}} -} +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/extractSignatures.R +\name{extractSignatures} +\alias{extractSignatures} +\title{Extract mutational signatures from trinucleotide context.} +\usage{ +extractSignatures( + mat, + n = NULL, + nrun = 1, + absolute = FALSE, + plotBestFitRes = FALSE, + parallel = 4, + pConstant = NULL +) +} +\arguments{ +\item{mat}{Input matrix of diemnsion nx96 generated by \code{\link{trinucleotideMatrix}}} + +\item{n}{decompose matrix into n signatures. Default NULL. Tries to predict best value for \code{n} by running NMF on a range of values and chooses based on cophenetic correlation coefficient.} + +\item{nrun}{Default 1. Number of runs to perform} + +\item{absolute}{Default FALSE. Returns absolute contribution per sample.} + +\item{plotBestFitRes}{plots consensus heatmap for range of values tried. Default FALSE} + +\item{parallel}{Default 4. Number of cores to use.} + +\item{pConstant}{A small positive value to add to the matrix. Use it ONLY if the functions throws an \code{non-conformable arrays} error} +} +\value{ +a list with decomposed scaled signatures, signature contributions in each sample and NMF object. +} +\description{ +Decompose a matrix of 96 substitution classes into \code{n} signatures. +} +\details{ +This function decomposes a non-negative matrix into n signatures. +} +\examples{ +\dontrun{ +laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools") +laml <- read.maf(maf = laml.maf) +laml.tnm <- trinucleotideMatrix(maf = laml, ref_genome = 'BSgenome.Hsapiens.UCSC.hg19', prefix = 'chr', +add = TRUE, useSyn = TRUE) +library("NMF") +laml.sign <- extractSignatures(mat = laml.tnm, plotBestFitRes = FALSE, n = 2, pConstant = 0.01) +} +} +\seealso{ +\code{\link{trinucleotideMatrix}} \code{\link{plotSignatures}} \code{\link{compareSignatures}} +}