forked from BioBam/scMaSigPro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryCoeff.R
257 lines (221 loc) · 8.41 KB
/
queryCoeff.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#' @title Get Features from scMaSigPro Object
#'
#' @description
#' This function extracts features from an object of class `scMaSigPro`
#' based on a specified query. The function query the coefficients matrix.
#'
#' @param scmpObj An object of class \code{\link{ScMaSigPro}}.
#' @param rsq Coefficient of determination or R-squared value threshold.
#' @param includeInflu Whether to include genes with influential observations.
#' @param p_value Overall model significance.
#' @param query A string specifying the type of query.
#' @param change The effective change at the end of the pseudotime.
#' (Default is NULL)
#' @param strictly If `change != NULL`, selects features if all coefficients
#' have similar change.
#' @param verbose Print detailed output in the console. (Default is TRUE)
#'
#' @return A subset of matrix of coefficients.
#'
#' @seealso `scMaSigPro::sc.filter()`
#'
#' @author Priyansh Srivastava \email{spriyansh29@@gmail.com}.
#'
#' @export
queryCoeff <- function(scmpObj,
rsq = 0.7,
p_value = scmpObj@Parameters@p_value,
includeInflu = TRUE,
query = "pseudotime_path",
change = NULL,
strictly = FALSE,
verbose = TRUE) {
# Check Validity of the object
assertthat::assert_that(is(scmpObj, "ScMaSigPro"),
msg = "Please provide object of class 'ScMaSigPro'"
)
# Check for group_vector
assertthat::assert_that(!isEmpty(scmpObj@[email protected]),
msg = "'scmpObj@[email protected]' is empty"
)
# Check for requested change
if (!is.null(change)) {
assertthat::assert_that(
all(
change %in% c("increasing", "decreasing")
),
msg = "Invalid change, please select on of 'increasing' or 'decreasing'"
)
}
# Check query
assertthat::assert_that(
all(
query %in% c("pseudotime", "pseudotime_path", "path", "path_pseudotime")
),
msg = "Invalid query, please select on of 'pseudotime', 'pseudotime_path' or 'path'"
)
# Check coefficents
coeff_matrix <- showCoeff(scmpObj = scmpObj)
# Apply R2 filter
sol_df <- showSol(scmpObj = scmpObj)[, c(1, 2)] %>% as.data.frame()
# Subset the sol
sol_df <- sol_df[sol_df[["R-squared"]] >= rsq, , drop = FALSE]
# Apply p-value filter
sig_genes <- rownames(sol_df[sol_df[["p-value"]] <= p_value, , drop = FALSE])
# Subset the coeff_matrix
coeff_matrix <- coeff_matrix[sig_genes, , drop = FALSE]
# All coeffients
coeff_vector <- colnames(coeff_matrix)
# Print Model formula
message(paste("Polynomial-GLM Formula:", showPoly(scmpObj)))
# Get groups
compare_groups_vector <- unique(scmpObj@[email protected])
# Generate group name vector
avail_groups_vector <- unique(unlist(stringr::str_split(compare_groups_vector, "vs")))
# Verbose
if (verbose) {
message(paste0(
"Number of available groups ", length(compare_groups_vector),
", i.e. ", paste(compare_groups_vector, collapse = ", ")
))
}
# Query Simplification
if (query == "pseudotime") {
# Get temporal variable
time_comp <- scmpObj@Parameters@bin_ptime_col
# Get all the columns with time variable
time_comp_vector <- grep(
pattern = time_comp,
x = coeff_vector,
ignore.case = FALSE,
value = TRUE
)
# Exclude any of the group effects
group_effects <- paste(avail_groups_vector, collapse = "|")
# Get all the columns with time only variable
time_comp_vector <- grep(
pattern = group_effects,
x = time_comp_vector,
ignore.case = FALSE,
value = TRUE, invert = TRUE
)
# Get the coefficients
time_coeff_matrix <- coeff_matrix[, time_comp_vector, drop = FALSE]
# Use the apply function to check each row
rows_with_all_nonzero <- apply(time_coeff_matrix, 1, function(row) all(row != 0))
# Subset the data to keep only the rows where all values are non-zero
time_coeff_matrix <- time_coeff_matrix[rows_with_all_nonzero, , drop = FALSE]
# Check for requested change
if (!is.null(change)) {
if (change == "increasing") {
if (strictly) {
rows_increasing <- apply(time_coeff_matrix, 1, function(row) all(row > 0))
time_coeff_matrix <- time_coeff_matrix[rows_increasing, , drop = FALSE]
} else {
time_coeff_matrix <- time_coeff_matrix[rowSums(time_coeff_matrix) > 0, , drop = FALSE]
}
} else if (change == "decreasing") {
if (strictly) {
rows_decreasing <- apply(time_coeff_matrix, 1, function(row) all(row < 0))
time_coeff_matrix <- time_coeff_matrix[rows_decreasing, , drop = FALSE]
} else {
time_coeff_matrix <- time_coeff_matrix[rowSums(time_coeff_matrix) < 0, , drop = FALSE]
}
}
}
# Return
return(time_coeff_matrix)
} else if (query == "pseudotime_path" || query == "path_pseudotime") {
# Get temporal variable
time_comp <- scmpObj@Parameters@bin_ptime_col
# Get all the columns with time variable
time_comp_vector <- grep(
pattern = time_comp,
x = coeff_vector,
ignore.case = FALSE,
value = TRUE
)
# Exclude any of the group effects
group_effects <- paste(avail_groups_vector, collapse = "|")
# Get all the columns with time only variable
time_comp_vector <- grep(
pattern = group_effects,
x = time_comp_vector,
ignore.case = FALSE,
value = TRUE, invert = FALSE
)
# Get the coeffients
time_coeff_matrix <- coeff_matrix[, time_comp_vector, drop = FALSE]
# Use the apply function to check each row
rows_with_all_nonzero <- apply(time_coeff_matrix, 1, function(row) all(row != 0))
# Subset the data to keep only the rows where all values are non-zero
time_coeff_matrix <- time_coeff_matrix[rows_with_all_nonzero, , drop = FALSE]
# If change is requested
if (!is.null(change)) {
if (change == "increasing") {
if (strictly) {
rows_increasing <- apply(time_coeff_matrix, 1, function(row) all(row > 0))
time_coeff_matrix <- time_coeff_matrix[rows_increasing, , drop = FALSE]
} else {
time_coeff_matrix <- time_coeff_matrix[rowSums(time_coeff_matrix) > 0, , drop = FALSE]
}
} else if (change == "decreasing") {
if (strictly) {
rows_decreasing <- apply(time_coeff_matrix, 1, function(row) all(row < 0))
time_coeff_matrix <- time_coeff_matrix[rows_decreasing, , drop = FALSE]
} else {
time_coeff_matrix <- time_coeff_matrix[rowSums(time_coeff_matrix) < 0, , drop = FALSE]
}
}
}
# Return
return(time_coeff_matrix)
}
# Query Simplification
else if (query == "path") {
# Get temporal variable
time_comp <- scmpObj@Parameters@bin_ptime_col
# Exclude any of the group effects
group_effects <- paste(avail_groups_vector, collapse = "|")
# Get all the columns with time variable
time_comp_vector <- grep(
pattern = group_effects,
x = coeff_vector,
ignore.case = FALSE,
value = TRUE
)
# Get all the columns with time only variable
time_comp_vector <- grep(
pattern = time_comp,
x = time_comp_vector,
ignore.case = FALSE,
value = TRUE, invert = TRUE
)
# Get the coeffients
time_coeff_matrix <- coeff_matrix[, time_comp_vector, drop = FALSE]
# Use the apply function to check each row
rows_with_all_nonzero <- apply(time_coeff_matrix, 1, function(row) all(row != 0))
# Subset the data to keep only the rows where all values are non-zero
time_coeff_matrix <- time_coeff_matrix[rows_with_all_nonzero, , drop = FALSE]
# Check for requested change
if (!is.null(change)) {
if (change == "increasing") {
if (strictly) {
rows_increasing <- apply(time_coeff_matrix, 1, function(row) all(row > 0))
time_coeff_matrix <- time_coeff_matrix[rows_increasing, , drop = FALSE]
} else {
time_coeff_matrix <- time_coeff_matrix[rowSums(time_coeff_matrix) > 0, , drop = FALSE]
}
} else if (change == "decreasing") {
if (strictly) {
rows_decreasing <- apply(time_coeff_matrix, 1, function(row) all(row < 0))
time_coeff_matrix <- time_coeff_matrix[rows_decreasing, , drop = FALSE]
} else {
time_coeff_matrix <- time_coeff_matrix[rowSums(time_coeff_matrix) < 0, , drop = FALSE]
}
}
}
# Return
return(time_coeff_matrix)
}
}