-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path015_prepare-dempap-raw-data.R
167 lines (133 loc) · 5.08 KB
/
015_prepare-dempap-raw-data.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
# Tidy and clean DepMap raw data.
if (basename(getwd()) == "munge") {
setwd("..")
}
source(".Rprofile")
library(tidyverse)
source("munge/munge_functions.R")
# ---- Data tidying functions ----
make_known_essentials_and_nonessentials <- function(essentials_file,
nonessentials_file,
out_file) {
essentials <- readr::read_csv(essentials_file) %>%
extract_hugo_gene_name() %>%
add_column(is_essential = TRUE)
nonessentials <- readr::read_csv(nonessentials_file) %>%
extract_hugo_gene_name() %>%
add_column(is_essential = FALSE)
bind_rows(essentials, nonessentials) %>%
readr::write_csv(out_file)
}
check_all_pass_qc <- function(df) {
if (!all(df$passes_qc)) {
stop("Not all batches passes QC.")
}
}
tidy_log_fold_change <- function(lfc_file,
replicate_map,
dropped_guides,
guide_map,
out_file) {
guide_map <- read_achilles_guide_map(guide_map)
rep_map <- read_achilles_replicate_map(replicate_map)
dropped_reps <- get_dropped_replicates(rep_map)
print(paste("Number of dropped batches:", length(dropped_reps)))
dropped_guides <- readr::read_csv(dropped_guides)$X1
print(paste("Number of dropped guides:", length(dropped_guides)))
lfc_df <- readr::read_csv(lfc_file) %>%
rename(sgrna = `Construct Barcode`) %>%
filter(!sgrna %in% !!dropped_guides) %>%
remove_columns(dropped_reps) %>%
pivot_longer(-sgrna, names_to = "replicate_id", values_to = "lfc") %>%
inner_join(rep_map, by = "replicate_id") %>%
inner_join(guide_map, by = "sgrna") %>%
rename(n_sgrna_alignments = n_alignments)
check_all_pass_qc(lfc_df)
readr::write_csv(lfc_df, out_file)
}
tidy_read_counts <- function(rc_file,
replicate_map,
dropped_guides,
guide_map,
out_file) {
guide_map <- read_achilles_guide_map(guide_map)
rep_map <- read_achilles_replicate_map(replicate_map)
dropped_reps <- get_dropped_replicates(rep_map)
print(paste("Number of dropped batches:", length(dropped_reps)))
dropped_guides <- readr::read_csv(dropped_guides)$X1
print(paste("Number of dropped guides:", length(dropped_guides)))
read_counts_df <- readr::read_csv(rc_file) %>%
rename(sgrna = `Construct Barcode`) %>%
filter(!sgrna %in% !!dropped_guides) %>%
remove_columns(dropped_reps) %>%
pivot_longer(-sgrna, names_to = "replicate_id", values_to = "read_counts") %>%
inner_join(rep_map, by = "replicate_id") %>%
inner_join(guide_map, by = "sgrna") %>%
rename(n_sgrna_alignments = n_alignments)
check_all_pass_qc(read_counts_df)
readr::write_csv(read_counts_df, out_file)
}
tidy_crispr_gene_effect <- function(chronos_ge_file,
ceres_ge_file,
out_file) {
chronos_ge <- readr::read_csv(chronos_ge_file) %>%
flatten_wide_df_by_gene(
values_to = "chronos_gene_effect",
id_col_name = "DepMap_ID",
rename_id_col_to = depmap_id,
col_names_to = hugo_symbol
)
ceres_ge <- readr::read_csv(ceres_ge_file) %>%
flatten_wide_df_by_gene(
values_to = "ceres_gene_effect",
id_col_name = "DepMap_ID",
rename_id_col_to = depmap_id,
col_names_to = hugo_symbol
)
ge_combined <- full_join(
chronos_ge,
ceres_ge,
by = c("depmap_id", "hugo_symbol")
)
message(
"Note: some data may be missing from the Chronos gene effect because Chronos does
not model guides that target multiple genes."
)
readr::write_csv(ge_combined, out_file)
}
# ---- Function calls ----
done <- function() {
print("\nDONE\n\n")
}
print("---- Tidying known essential and non-essential genes. ----")
make_known_essentials_and_nonessentials(
essentials_file = snakemake@input[["common_essentials"]],
nonessentials_file = snakemake@input[["nonessentials"]],
out_file = snakemake@output[["known_essentials"]]
)
done()
print("---- Tidying log fold change data. ----")
tidy_log_fold_change(
lfc_file = snakemake@input[["achilles_logfold_change"]],
replicate_map = snakemake@input[["achilles_replicate_map"]],
dropped_guides = snakemake@input[["achilles_dropped_guides"]],
guide_map = snakemake@input[["achilles_guide_map"]],
out_file = snakemake@output[["achilles_log_fold_change"]]
)
done()
print("---- Tidying read count data. ----")
tidy_read_counts(
rc_file = snakemake@input[["achilles_raw_readcounts"]],
replicate_map = snakemake@input[["achilles_replicate_map"]],
dropped_guides = snakemake@input[["achilles_dropped_guides"]],
guide_map = snakemake@input[["achilles_guide_map"]],
out_file = snakemake@output[["achilles_read_counts"]]
)
done()
print("---- Tidying CRISPR gene effect. ----")
tidy_crispr_gene_effect(
chronos_ge_file = snakemake@input[["crispr_gene_effect_chronos"]],
ceres_ge_file = snakemake@input[["crispr_gene_effect_ceres"]],
out_file = snakemake@output[["crispr_gene_effect"]]
)
done()