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
20 changes: 16 additions & 4 deletions R/extractSignatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
}
}
100 changes: 53 additions & 47 deletions man/extractSignatures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.