|
a |
|
b/R/runNTP.R |
|
|
1 |
#' @name runNTP |
|
|
2 |
#' @title Run nearest template prediction |
|
|
3 |
#' @description Using Nearest Template Prediction (NTP) based on predefined templates derived from current identified subtypes to assign potential subtype label on external cohort. |
|
|
4 |
#' @param expr A numeric matrix with row features and sample columns; data is recommended to be z-scored. |
|
|
5 |
#' @param templates A data frame with at least two columns; class (coerced to factor) and probe (coerced to character). |
|
|
6 |
#' @param scaleFlag A logic value to indicate if the expression data should be further scaled. TRUE by default. |
|
|
7 |
#' @param centerFlag A logic value to indicate if the expression data should be further centered. TRUE by default. |
|
|
8 |
#' @param nPerm An integer value to indicate the permutations for p-value estimation. |
|
|
9 |
#' @param distance A string value to indicate the distance measurement. Allowed values contain c('cosine', 'pearson', 'spearman', 'kendall'); "cosine" by default. |
|
|
10 |
#' @param seed An integer value for p-value reproducibility. |
|
|
11 |
#' @param verbose A logic value to indicate whether console messages are to be displayed; TRUE by default. |
|
|
12 |
#' @param doPlot A logic value to indicate whether to produce prediction heatmap; FALSE by default. |
|
|
13 |
#' @param fig.path A string value to indicate the output path for storing the nearest template prediction heatmap. |
|
|
14 |
#' @param fig.name A string value to indicate the name of the nearest template prediction heatmap. |
|
|
15 |
#' @param width A numeric value to indicate the width of output figure. |
|
|
16 |
#' @param height A numeric value to indicate the height of output figure. |
|
|
17 |
#' @return A figure of predictive heatmap by NTP (.pdf) and a list with the following components: |
|
|
18 |
#' |
|
|
19 |
#' \code{ntp.res} a data.frame storing the results of nearest template prediction (see \link[CMScaller]{ntp}). |
|
|
20 |
#' |
|
|
21 |
#' \code{clust.res} similar to `clust.res` returned by `getMOIC()` or `get%algorithm_name%` or `getConsensusMOIC()`. |
|
|
22 |
#' |
|
|
23 |
#' \code{mo.method} a string value indicating the method used for prediction. |
|
|
24 |
#' @export |
|
|
25 |
#' @importFrom CMScaller ntp subHeatmap |
|
|
26 |
#' @importFrom grDevices dev.copy2pdf |
|
|
27 |
#' @examples # There is no example and please refer to vignette. |
|
|
28 |
#' @references Hoshida, Y. (2010). Nearest Template Prediction: A Single-Sample-Based Flexible Class Prediction with Confidence Assessment. PLoS ONE 5, e15543. |
|
|
29 |
runNTP <- function(expr = NULL, |
|
|
30 |
templates = NULL, |
|
|
31 |
scaleFlag = TRUE, |
|
|
32 |
centerFlag = TRUE, |
|
|
33 |
nPerm = 1000, |
|
|
34 |
distance = "cosine", |
|
|
35 |
seed = 123456, |
|
|
36 |
verbose = TRUE, |
|
|
37 |
doPlot = FALSE, |
|
|
38 |
fig.path = getwd(), |
|
|
39 |
fig.name = "ntpheatmap", |
|
|
40 |
width = 5, |
|
|
41 |
height = 5) { |
|
|
42 |
|
|
|
43 |
# message("Using up- or down-regulated biomarkers (templates) are highly recommended.\n") |
|
|
44 |
|
|
|
45 |
if(!is.element(distance, c("cosine", "pearson", "spearman", "kendall"))) { |
|
|
46 |
stop("the argument of distance should be one of cosine, pearson, spearman, or kendall.") |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
com_feat <- intersect(rownames(expr), templates$probe) |
|
|
50 |
message(paste0("--original template has ",nrow(templates), " biomarkers and ", length(com_feat)," are matched in external expression profile.")) |
|
|
51 |
expr <- expr[com_feat, , drop = FALSE] |
|
|
52 |
templates <- templates[which(templates$probe %in% com_feat), , drop = FALSE] |
|
|
53 |
|
|
|
54 |
if(is.element(0,as.numeric(table(templates$class)))) { |
|
|
55 |
stop("at least one class has no probes/genes matched in template file!") |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
emat <- t(scale(t(expr), scale = scaleFlag, center = centerFlag)) |
|
|
59 |
if(doPlot) { |
|
|
60 |
outFig <- paste0(fig.name,".pdf") |
|
|
61 |
ntp.res <- ntp(emat = emat, |
|
|
62 |
templates = templates, |
|
|
63 |
doPlot = doPlot, |
|
|
64 |
nPerm = nPerm, |
|
|
65 |
distance = distance, |
|
|
66 |
nCores = 1, |
|
|
67 |
seed = seed, |
|
|
68 |
verbose = verbose) |
|
|
69 |
invisible(dev.copy2pdf(file = file.path(fig.path, outFig), width = width, height = height)) |
|
|
70 |
|
|
|
71 |
} else { |
|
|
72 |
ntp.res <- ntp(emat = emat, |
|
|
73 |
templates = templates, |
|
|
74 |
doPlot = doPlot, |
|
|
75 |
nPerm = nPerm, |
|
|
76 |
distance = distance, |
|
|
77 |
nCores = 1, |
|
|
78 |
seed = seed, |
|
|
79 |
verbose = verbose) |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
ntp.res[,setdiff(colnames(ntp.res),"prediction")] <- round(ntp.res[,setdiff(colnames(ntp.res),"prediction")], 4) |
|
|
83 |
ex.moic.res <- data.frame(samID = rownames(ntp.res), |
|
|
84 |
clust = gsub("CS","",ntp.res$prediction), |
|
|
85 |
row.names = rownames(ntp.res), |
|
|
86 |
stringsAsFactors = FALSE) |
|
|
87 |
|
|
|
88 |
return(list(ntp.res = ntp.res, clust.res = ex.moic.res, mo.method = "NTP")) |
|
|
89 |
} |