-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15b_02_downstream_heritability_calc_control_heritability.R
199 lines (157 loc) · 7.82 KB
/
15b_02_downstream_heritability_calc_control_heritability.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
suppressMessages(library(Matrix))
suppressMessages(library(Seurat))
suppressMessages(library(data.table))
suppressMessages(library(dplyr))
suppressMessages(library(ggplot2))
suppressMessages(library(ggpubr))
suppressMessages(library(tidyverse))
suppressMessages(library(parallel))
suppressMessages(library(doParallel))
suppressMessages(library(lme4))
suppressMessages(library(lme4qtl))
suppressMessages(library(lattice))
suppressMessages(library(variancePartition))
suppressMessages(library(gtools))
suppressMessages(library(lmtest))
suppressMessages(library(org.Hs.eg.db))
# set relevent i/o paths
section_name <- "15b_downstream_heritability"
subsection_long_name <- "15b_02_downstream_heritability_calc_control_heritability"
# set relevent i/o paths
date <- "2023-10-18"
HomeFolder <- "/lustre/scratch123/hgi/teams/parts/cf14/crispr_scrnaseq_hipsci/"
ExperimentName <- "Pica"
# options for testing
io_path <- paste0("scripts/io/", ExperimentName, "_io.R")
utils_path <- "scripts/io/Magpie_Utils.R"
control_tag <- c("unassigned", "NonTarget") # use all unassigned cells as control
keep_singleton_lines <- FALSE
line_set_ind <- 7
start_ind <- 4100
no_genes2test <- 100
perm_seed <- 0
args <- commandArgs(trailingOnly = TRUE)
for (ind in 1:length(args)){
arg <- args[[ind]]
##needed arguments
if (arg == "--date"){ date <- args[[ind + 1]] }
if (arg == "--home_folder"){ HomeFolder <- args[[ind + 1]] }
if (arg == "--no_genes2test"){ no_genes2test <- as.numeric(args[[ind + 1]]) }
if (arg == "--start_ind"){ start_ind <- as.numeric(args[[ind + 1]]) }
## donors to include
if (arg == "--donors2include"){ donors2include <- args[[ind + 1]] }
if (arg == "--line_set_ind"){ line_set_ind <- as.numeric(args[[ind + 1]]) }
## not needed
if (arg == "--section_name"){ section_name <- args[[ind + 1]] }
if (arg == "--utils_path"){ utils_path <- args[[ind + 1]] }
if (arg == "--io_path"){ io_path <- args[[ind + 1]] }
## permuting
if (arg == "--permute_cells"){ permute_cells <- as.logical(args[[ind + 1]]) }
if (arg == "--permute_lines"){ permute_lines <- as.logical(args[[ind + 1]]) }
if (arg == "--rnd_seed_start"){ rnd_seed_start <- as.numeric(args[[ind + 1]]) }
if (arg == "--max_seeds2test"){ max_seeds2test <- as.numeric(args[[ind + 1]]) }
if (arg == "--n_perms"){ n_perms <- as.numeric(args[[ind + 1]]) }
if (arg == "--keep_singleton_lines"){ keep_singleton_lines <- as.logical(args[[ind + 1]]) }
}
print(paste0(section_name))
print(paste("date:", date))
print(paste("home folder:", HomeFolder))
if(exists("donors2include")){print('including donors: '); print(donors2include)}
setwd(HomeFolder)
source(io_path)
source(utils_path)
GuideMetadata <- fread(GuideMetadataPath)
LineMetadata <- fread(LineMetadataPath)
fnm <- sort(list.files(file.path(OutFolder, "15a_get_line_perms"), pattern = "_lines_per_target.tsv", full.names = T), decreasing = T)[1]
donor_sets <- fread(fnm )
if (exists("donors2include")){
d <- sort(unlist(strsplit(donors2include, '_')))
} else {
d <- sort(unlist(strsplit(donor_sets$lines[which(donor_sets$line_set_name == line_set_ind)], ";")))
}
outdir <- file.path(file.path(OutFolder, section_name, subsection_long_name, paste0('line_set_ind_', line_set_ind)))
if(!dir.exists(outdir)) dir.create(outdir, recursive = T)
plotsdir <- file.path(HTMLFolder, "pipeline", section_name, subsection_long_name, paste0('line_set_ind_', line_set_ind))
if(!dir.exists(plotsdir)) dir.create(plotsdir, recursive = T)
## ---- load data
gene_expression <- get_control_expression()
gene_expression$target <- NonTargetGeneName
gene_expression <- filter(gene_expression, donor %in% d)
n_cells <- dim(gene_expression)[1]
n_lines <- length(unique(gene_expression$cell_line))
n_donors <- length(unique(gene_expression$donor))
print("cells per line:")
table(gene_expression$cell_line)
## add guide
## cell metadata (for guides)
most_recent <- sort(gsub(
list.files(file.path(OutFolder, "5a_combine"), pattern = "control_cells_metadata.tsv", full.names = T),
pattern = paste0(".*5a_combine[/]|_.*"),
replacement = ""),
decreasing = T)[1]
cell_metadata_fnms <- grep(list.files(file.path(OutFolder, "5a_combine"), pattern = "control_cells_metadata.tsv", full.names = T),
pattern = most_recent, value = T)
cell_metadata <- lapply(cell_metadata_fnms, FUN = fread) %>%
bind_rows() %>% as.data.frame() %>%
#dplyr::filter(target_gene_name == target_gene) %>%
dplyr::select(c("V1", "guide" = 'feature_call'))
## add to gene expression df
gene_expression <- left_join(gene_expression,cell_metadata, by = c("id" = "V1"))
## ---- Throw out singleton lines
if (keep_singleton_lines == F){
print("removing singleton lines...")
lines2keep <- data.frame(
line = unique(gene_expression$cell_line),
donor = unlist(lapply(strsplit(unique(gene_expression$cell_line), "_"), "[[", 1)),
n_cells = as.numeric(table(gene_expression$cell_line))
) %>% filter(n_cells > min_cells_per_gene_per_donor)
donors2keep <- table(lines2keep$donor)
donors2keep <- names(donors2keep)[which(donors2keep == 2)]
lines2keep <- lines2keep %>% filter(donor %in% donors2keep)
print(paste0("keeping ", paste(lines2keep$line, collapse = ', ')))
gene_expression <- filter(gene_expression, cell_line %in% lines2keep$line)
print("cells per line:")
print(table(gene_expression$cell_line))
} else {
lines2keep <- unique(gene_expression$cell_line)
}
n_cells_used <- dim(gene_expression)[1]
n_lines_used <- length(unique(gene_expression$cell_line))
## ---- Calculate Heritability
### some functions
most_recent <- sort(gsub(list.files(paste0(OutFolder, "6a_run_control_lm")), pattern = "_.*", replace = ""), decreasing = T)[1]
downstream_gene_fnms <- list.files(paste0(OutFolder, "6a_run_control_lm"), pattern = most_recent )
downstream_gene_fnms <- downstream_gene_fnms[(start_ind+1):(start_ind + no_genes2test)]
fnm <- sort(list.files(file.path(OutFolder, '15a_get_line_perms'),
pattern = paste0("_line_set-", line_set_ind, "_", length(lines2keep$line)/2, "_perms_rnd-seed-", perm_seed, ".RDS"), full.names = T),
decreasing = T)[1]
all_permutations <- readRDS(fnm)
registerDoParallel(50)
res <- foreach(downstream_gene_fnm = downstream_gene_fnms) %dopar% {
downstream_gene_ind <- which(gsub(colnames(gene_expression), pattern = ":", replacement = '-') == gsub(downstream_gene_fnm, pattern = paste0(most_recent, "_gene-|[.]RDS"), replacement = ""))
downstream_gene_name <- unlist(lapply(strsplit(colnames(gene_expression)[downstream_gene_ind], ":"), "[[", 2))
df4lmm_control <- get_df4lmm(kd_expr = gene_expression %>% mutate(target = NonTargetGeneName),
downstream_gene_name = downstream_gene_name,
lines2include = lines2keep$line,
include_line_quality = F,
include_sex = T)
## with fixed effects
models_to_test <- gsub(var_expl_models, pattern = '[+] on_target_expr', replacement = '')
names(models_to_test) <- names(var_expl_models)
df4lmm = df4lmm_control
df2write <- run_perm(df4lmm = df4lmm_control,
val2calc = 'post_kd_expr',
models_to_test = models_to_test)
fwrite(df2write, paste0(outdir, '/', date, "_line-set-ind_", line_set_ind, '_', downstream_gene_name, "-", dim(df2write)[1] - 1, ".tsv.gz"), sep = '\t')
}
##LL_m2 <- logLik(lmer(formula = as.formula(paste0('post_kd_expr ', models_to_test['m2'])),
# data = df4lmm_control))
#LL_m1 <- logLik(lmer(formula = as.formula(paste0('post_kd_expr ', models_to_test['m1'])),
# data = df4lmm_control))
## ---- SessionInfo
sessionInfo()
## AARS had error at 840
## EXOSC2 had error at 2690
#fit1 <- lmer(lfc ~ on_target_expr + (1|cell_line) + (1|donor), df4lmm)
#fit2 <- lmer(lfc ~ on_target_expr + (1|cell_line), df4lmm)
#lrtest(fit1, fit2)