-
Notifications
You must be signed in to change notification settings - Fork 9
/
simplify_microbial.R
73 lines (68 loc) · 2 KB
/
simplify_microbial.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
suppressPackageStartupMessages({
library(dplyr)
library(readr)
})
collapse_conditional_direction <- function(direction) {
if (length(direction) == 1) {
return(direction)
} else if (all(direction == direction[[1]])) {
return(paste0(direction[[1]], '*'))
} else {
return(paste(direction, collapse=','))
}
}
args <- commandArgs(trailingOnly = TRUE)
feature_files <- args[1:2]
microbial_features <- read_tsv(args[[3]], col_types = cols())
results <- vector("list", length(feature_files))
for (k in seq_along(feature_files)) {
hits <-
read_tsv(feature_files[[k]], col_types = cols()) %>%
inner_join(
microbial_features,
by = c("cancer", "what", "versus", "features", "genera")
)
results[[k]] <- hits %>%
mutate(
direction = ifelse(
p_value > 0.05, NA, ifelse(p_greater > .1, "Negative", "Positive")
),
univariate_direction =
ifelse(
is.na(univariate_fdr) | univariate_fdr > 0.05,
"",
ifelse(
univariate_direction == -1, "Negative", "Positive"
)
),
univariate_fdr = ifelse(is.na(univariate_fdr) | univariate_fdr > 0.05,
"n.s.",
sprintf("%.3g", univariate_fdr)
)
) %>%
select(
Cancer = cancer,
Versus = versus,
Genus = genera,
How = how,
`Models Present` = seen,
`Conditional Direction` = direction,
`Univariate FDR` = univariate_fdr,
`Univariate Direction` = univariate_direction
)
}
features <- bind_rows(results)
features <- features %>%
group_by(Cancer, Versus, Genus) %>%
summarize(
How = paste(How, collapse = ","),
`Models Present` = paste0(sum(`Models Present`), '/', n() * 100),
`Conditional Direction` = collapse_conditional_direction(`Conditional Direction`),
`Univariate FDR` = `Univariate FDR`[1],
`Univariate Direction` = `Univariate Direction`[1],
.groups = "drop"
)
features %>%
arrange(Cancer, Versus, desc(`Models Present`)) %>%
format_tsv() %>%
cat()