-
Notifications
You must be signed in to change notification settings - Fork 0
/
platjam-neuro.R
183 lines (177 loc) · 5.59 KB
/
platjam-neuro.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#' Convert frequency matrix to normalizedMatrix format
#'
#' Convert frequency matrix to normalizedMatrix format
#'
#' This function takes input data with frequency represented
#' as columns, and observations as rows. It will optionally
#' scale each row to have fixed minimum-to-maximum value
#' range, given a range of frequencies to use for scaling.
#'
#' @return `normalizedMatrix` object, a subclass of `matrix`,
#' as defined in `EnrichedHeatmap`.
#'
#' @param mat numeric matrix with frequency represented as
#' columns, and whose numeric frequency is stored in
#' `colnames(mat)` as character values.
#' @param target_frequency numeric vector representing a
#' range of frequencies to be considered the "target",
#' and thus highlighted by `EnrichedHeatmap::EnrichedHeatmap()`.
#' By default the target range is used to order rows from
#' high to low signal, unless the order is specified
#' otherwise. When `target_frequency` is `NULL`, there
#' is no target indicated in the normalizedMatrix.
#' @param ... additional arguments are ignored.
#'
#' @family jam import functions
#'
#' @export
frequency_matrix2nmat <- function
(mat,
target_frequency=NULL,
target_name="target",
signal_name="Frequency",
do_scale=TRUE,
scale_from=0,
scale_to=1,
scale_frequency=NULL,
apply_floor=TRUE,
verbose=FALSE,
...)
{
## Purpose is to convert a frequency matrix to normalizedMatrix
x_freq <- as.numeric(colnames(x));
if (any(is.na(x_freq))) {
stop("colnames(x) must represent numeric frequency values.");
}
#
freq_index <- seq_len(ncol(x));
target_index <- which(x_freq >= min(target_frequency) &
x_freq <= max(target_frequency));
if (length(target_index) > 0) {
upstream_index <- which(freq_index < min(target_index));
} else {
upstream_index <- NULL;
}
downstream_index <- which(freq_index > max(c(0, target_index)));
if (verbose) {
jamba::printDebug("frequency_matrix2nmat(): ",
"upstream_index:",
upstream_index,
", target_index:",
target_index,
", downstream_index:",
downstream_index);
}
## Optionally run normScale() on each row
if (do_scale) {
if (length(scale_frequency) > 0) {
scale_index <- which(x_freq >= min(scale_frequency) &
x_freq <= max(scale_frequency));
} else {
scale_index <- freq_index;
}
if (verbose) {
jamba::printDebug("frequency_matrix2nmat(): ",
"scale_index:",
scale_index);
}
mat <- rowNormScale(mat,
from=scale_from,
to=scale_to,
col_range=scale_index);
attr(mat, "scale_index") <- scale_index;
attr(mat, "scale_range") <- c(scale_from,
scale_to);
if (apply_floor) {
if (verbose) {
scale_diff <- (scale_to - scale_from) / 2;
jamba::printDebug("frequency_matrix2nmat(): ",
"scale_diff:",
scale_diff);
}
attr(mat, "scale_floor") <- c(scale_from - scale_diff,
scale_to + scale_diff);
mat <- noiseFloor(mat,
minimum=scale_from - scale_diff,
ceiling=scale_to + scale_diff);
}
}
attr(mat, "upstream_index") <- upstream_index;
attr(mat, "target_index") <- target_index;
attr(mat, "downstream_index") <- downstream_index;
attr(mat, "extend") <- c(0, max(x_freq));
attr(mat, "smooth") <- FALSE;
attr(mat, "signal_name") <- signal_name;
attr(mat, "target_name") <- target_name;
attr(mat, "target_is_single_point") <- FALSE;
attr(mat, "background") <- 0;
attr(mat, "signal_is_categorical") <- FALSE;
class(mat) <- c("normalizedMatrix", "matrix");
return(mat);
}
#' Normalize and scale data per row
#'
#' Normalize and scale data per row
#'
#' This function essentially calls `jamba::normScale()`
#' on each row of a numeric matrix. By default, it scales
#' values to a fixed numeric range from 0 to 1, where
#' the minimum value is set to 0 and the maximum value is
#' set to 1. It is much more configurable, see `jamba::normScale()`
#' help docs.
#'
#' @param x,from,to,naValue,singletMethod arguments passed to
#' `jamba::normScale()`. Note that the default `low,high` values
#' use the column range defined by `col_range`.
#' @param low numeric value or `NULL`, passed to `jamba::normScale()`
#' for each row in `x`. When `low` is `NULL`, it uses the
#' minimum value in `col_range` for each row.
#' @param high numeric value or `NULL`, passed to `jamba::normScale()`
#' for each row in `x`. When `high` is `NULL`, it uses the
#' maximum value in `col_range` for each row.
#' @param col_range integer vector referring to column numbers in
#' the input `x` matrix to use in `jamba::normScale()`. When
#' `col_range` is `NULL`, it uses all columns in `x`.
#' @param ... additional arguments are passed to `jamba::normScale()`.
#'
#' @family jam utility functions
#'
#' @examples
#' m <- matrix(1:9, ncol=3);
#' m;
#'
#' rowNormScale(m);
#' rowNormScale(m, from=0, to=10);
#'
#' @export
rowNormScale <- function
(x,
from=0,
to=1,
naValue=NA,
low=NULL,
high=NULL,
singletMethod="min",
col_range=NULL,
...)
{
if (length(col_range) == 0) {
col_range <- seq_len(ncol(x));
}
t(apply(x, 1, function(i){
if (length(low) == 0) {
low <- min(i[col_range], na.rm=TRUE);
}
if (length(high) == 0) {
high <- max(i[col_range], na.rm=TRUE);
}
jamba::normScale(i,
low=low,
high=high,
from=from,
to=to,
singletMethod=singletMethod,
naValue=naValue,
...);
}));
}