-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpull_coefficients.R
146 lines (131 loc) · 3.53 KB
/
pull_coefficients.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
suppressMessages({
library(dplyr)
library(optparse)
library(readr)
library(tibble)
})
ROW_MARGIN <- 1
COL_MARGIN <- 2
CLINICAL_FEATURES <- c("age_at_diagnosis", "gender_male", "tumor_stage")
parse_filename <- function(filename) {
ss <- as.list(strsplit(basename(filename), "_")[[1]])
# ignore the first element
names(ss) <- c("ignore1", "cancer", "what", "versus", "features", "how")
if (ss$how == "counts") {
names(ss) <- c(
"ignore1", "cancer", "what", "versus", "features", "ignore2", "how"
)
}
ss$cancer <- toupper(ss$cancer)
ss$versus <- versus <- ifelse(
ss$what == "surv",
toupper(ss$versus),
tools::toTitleCase(ss$versus) # Drugs are title case
)
ss$how <- toupper(ss$how)
ss
}
wilcox_feature_not_zero <- function(trials) {
apply(
trials,
ROW_MARGIN,
function(x) {
tryCatch(
{
wilcox.test(x)$p.value
},
error = function(e) as.numeric(NA)
)
}
)
}
wilcox_feature_gt_zero <- function(trials) {
apply(
trials,
ROW_MARGIN,
function(x) {
tryCatch(
{
wilcox.test(x, alt = "gr")$p.value
},
error = function(e) as.numeric(NA)
)
}
)
}
option_list <- list(
make_option("--rank-cutoff",
action = "store",
default = 50,
type = "integer",
help = "Only consider args of lower rank"
),
make_option("--seen-cutoff",
action = "store",
default = 0.2,
type = "double",
help = "Fraction that must contain the feature"
),
make_option("--p-cutoff",
action = "store",
default = 0.01,
type = "double",
help = "P-value that feature is away from zero"
)
)
parser <- OptionParser(
usage = "%prog [options] file",
option_list = option_list
)
arguments <- parse_args(parser, positional_arguments = TRUE)
opt <- arguments$options
rank_cutoff <- opt[["rank-cutoff"]]
seen_cutoff <- opt[["seen-cutoff"]]
p_cutoff <- opt[["p-cutoff"]]
filenames <- arguments$args
results <- vector("list", length(filenames))
for (i in seq_along(filenames)) {
filename <- filenames[i]
metadata <- parse_filename(filename)
trials <- readRDS(filename) %>% as.matrix()
features_wilcox <- p.adjust(wilcox_feature_not_zero(trials), method = "holm")
# Not adjusted. Just checking direction
features_greater <- wilcox_feature_gt_zero(trials)
feature_ranks <- apply(-abs(trials), COL_MARGIN, rank)
feature_ranks_used <- ifelse(feature_ranks <= rank_cutoff, feature_ranks, NA)
feature_seen <- apply(
feature_ranks_used, ROW_MARGIN, function(x) sum(!is.na(x))
)
selected <- (feature_seen >= seen_cutoff * ncol(trials)) &
(!is.na(features_wilcox) & features_wilcox <= p_cutoff) &
!(names(feature_seen) %in% CLINICAL_FEATURES)
results[[i]] <- tibble(
cancer = metadata$cancer,
what = metadata$what,
versus = metadata$versus,
features = metadata$features,
how = metadata$how,
genera = rownames(trials)[selected],
seen = feature_seen[selected],
mean = apply(
trials[selected, , drop = FALSE], ROW_MARGIN, mean,
na.rm = TRUE
),
median_rank = apply(
feature_ranks_used[selected, , drop = FALSE], ROW_MARGIN, median,
na.rm = TRUE
),
p_value = features_wilcox[selected],
p_greater = features_greater[selected]
)
}
do.call(rbind, results) %>%
arrange(cancer, what, how, desc(mean)) %>%
mutate(
p_value = sprintf("%.3g", p_value),
p_greater = sprintf("%.3g", p_greater),
mean = sprintf("%.3g", mean),
median_rank = sprintf("%.1f", median_rank)
) %>%
format_tsv() %>%
cat()