|
|
library(tidyverse) |
|
|
library(arrow) |
|
|
library(here) |
|
|
|
|
|
|
|
|
mahendrawada_features = arrow::read_parquet("~/code/hf/mahendrawada_2025/features_mahendrawada_2025.parquet") |
|
|
|
|
|
|
|
|
|
|
|
perturbation_response_data = list( |
|
|
mahendrawada_rnaseq = arrow::read_parquet("~/code/hf/mahendrawada_2025/rnaseq_reprocessed.parquet") %>% |
|
|
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>% |
|
|
replace_na(list(log2FoldChange = 0, pvalue = 1)) %>% |
|
|
mutate(abs_log2fc = abs(log2FoldChange)), |
|
|
|
|
|
|
|
|
kemmeren = arrow::open_dataset("~/code/hf/kemmeren_2014/kemmeren_2014.parquet") %>% |
|
|
filter(target_locus_tag %in% mahendrawada_features$locus_tag, |
|
|
str_detect(regulator_locus_tag, "WT-", negate=TRUE)) %>% |
|
|
select(sample_id, regulator_locus_tag, target_locus_tag, Madj, pval) %>% |
|
|
arrow::to_duckdb() %>% |
|
|
group_by(sample_id, target_locus_tag) %>% |
|
|
mutate(rn = row_number(desc(abs(Madj)))) %>% |
|
|
filter(rn == 1) %>% |
|
|
select(-rn) %>% |
|
|
ungroup() %>% |
|
|
collect(), |
|
|
hackett = arrow::read_parquet("~/code/hf/hackett_2020/hackett_2020.parquet") %>% |
|
|
filter(target_locus_tag %in% mahendrawada_features$locus_tag, |
|
|
str_detect(regulator_locus_tag, "WT-", negate=TRUE)) %>% |
|
|
select(sample_id, regulator_locus_tag, target_locus_tag, log2_shrunken_timecourses) %>% |
|
|
arrow::to_duckdb() %>% |
|
|
group_by(sample_id, target_locus_tag) %>% |
|
|
mutate(rn = row_number(desc(abs(log2_shrunken_timecourses)))) %>% |
|
|
filter(rn == 1) %>% |
|
|
select(-rn) %>% |
|
|
ungroup() %>% |
|
|
collect() %>% |
|
|
|
|
|
mutate(pvalue = 0), |
|
|
hu_reimand = arrow::read_parquet("~/code/hf/hu_2007_reimand_2010/hu_2007_reimand_2010.parquet") %>% |
|
|
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>% |
|
|
select(sample_id, regulator_locus_tag, target_locus_tag, effect, pval) %>% |
|
|
arrow::to_duckdb() %>% |
|
|
group_by(sample_id, target_locus_tag) %>% |
|
|
mutate(rn = row_number(desc(abs(effect)))) %>% |
|
|
filter(rn == 1) %>% |
|
|
select(-rn) %>% |
|
|
ungroup() %>% |
|
|
collect(), |
|
|
hughes_ko = arrow::read_parquet("~/code/hf/hughes_2006/knockout.parquet") %>% |
|
|
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>% |
|
|
select(sample_id, regulator_locus_tag, target_locus_tag, mean_norm_log2fc) %>% |
|
|
arrow::to_duckdb() %>% |
|
|
group_by(sample_id, target_locus_tag) %>% |
|
|
mutate(rn = row_number(desc(abs(mean_norm_log2fc)))) %>% |
|
|
filter(rn == 1) %>% |
|
|
select(-rn) %>% |
|
|
ungroup() %>% |
|
|
collect() %>% |
|
|
|
|
|
mutate(pvalue = 0), |
|
|
hughes_oe = arrow::read_parquet("~/code/hf/hughes_2006/overexpression.parquet") %>% |
|
|
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>% |
|
|
select(sample_id, regulator_locus_tag, target_locus_tag, mean_norm_log2fc) %>% |
|
|
arrow::to_duckdb() %>% |
|
|
group_by(sample_id, target_locus_tag) %>% |
|
|
mutate(rn = row_number(desc(abs(mean_norm_log2fc)))) %>% |
|
|
filter(rn == 1) %>% |
|
|
select(-rn) %>% |
|
|
ungroup() %>% |
|
|
collect() %>% |
|
|
|
|
|
mutate(pvalue = 0) |
|
|
) |
|
|
|
|
|
composite_cc = arrow::open_dataset("~/code/hf/callingcards/annotated_features_combined") %>% |
|
|
collect() %>% |
|
|
left_join(arrow::read_parquet("~/code/hf/callingcards/annotated_features_combined_meta.parquet")) %>% |
|
|
dplyr::rename(id = genome_map_id_set) |
|
|
|
|
|
single_cc_meta = arrow::read_parquet("~/code/hf/callingcards/annotated_features_meta.parquet") %>% |
|
|
filter(batch != "composite") |
|
|
|
|
|
single_cc = arrow::open_dataset("~/code/hf/callingcards/annotated_features") %>% |
|
|
filter(id %in% single_cc_meta$id) %>% |
|
|
collect() %>% |
|
|
left_join(single_cc_meta) %>% |
|
|
mutate(id = as.character(id)) |
|
|
|
|
|
binding_data = list( |
|
|
cc = single_cc %>% |
|
|
select(intersect(colnames(.), colnames(composite_cc))) %>% |
|
|
bind_rows(composite_cc %>% |
|
|
select(intersect(colnames(.), colnames(single_cc)))), |
|
|
harbison = arrow::read_parquet("~/code/hf/harbison_2004/harbison_2004.parquet") %>% |
|
|
replace_na(list(effect = 0, pvalue = 1)) %>% |
|
|
group_by(sample_id, target_locus_tag) %>% |
|
|
slice_max(abs(effect), n = 1, with_ties = FALSE) %>% |
|
|
ungroup(), |
|
|
chipexo = arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_af_combined.parquet") %>% |
|
|
left_join(arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_metadata_sample.parquet")), |
|
|
mahendrawada_chec = arrow::read_parquet("~/code/hf/mahendrawada_2025/chec_mahendrawada_m2025_af_combined.parquet") %>% |
|
|
left_join(arrow::read_parquet("~/code/hf/mahendrawada_2025/chec_mahendrawada_m2025_af_combined_meta.parquet")) |
|
|
) |
|
|
|
|
|
mahendrawada_rnaseq_dto_background = map(binding_data, ~{ |
|
|
.x %>% |
|
|
ungroup() %>% |
|
|
select(target_locus_tag) %>% |
|
|
distinct() %>% |
|
|
filter(target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) |
|
|
}) |
|
|
|
|
|
mahendrawada_rnaseq_dto = list( |
|
|
cc = list( |
|
|
binding = binding_data$cc %>% |
|
|
filter(poisson_pval <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>% |
|
|
group_by(id) %>% |
|
|
arrange(desc(callingcards_enrichment)) %>% |
|
|
mutate(pvalue_rank = rank(poisson_pval, ties.method = 'min')) %>% |
|
|
dplyr::rename(sample_id = id) %>% |
|
|
group_by(sample_id), |
|
|
pr = perturbation_response_data$mahendrawada_rnaseq %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data$cc$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data$cc$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)), |
|
|
harbison = list( |
|
|
binding = binding_data$harbison %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
arrange(desc(effect)) %>% |
|
|
mutate(pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id), |
|
|
pr = perturbation_response_data$mahendrawada_rnaseq %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data$harbison$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data$harbison$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)), |
|
|
chipexo = list( |
|
|
binding = binding_data$chipexo %>% |
|
|
filter(log_poisson_pval <= log(0.1)) %>% |
|
|
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
arrange(desc(enrichment)) %>% |
|
|
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>% |
|
|
group_by(sample_id), |
|
|
pr = perturbation_response_data$mahendrawada_rnaseq %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data$chipexo$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data$chipexo$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)), |
|
|
mahendrawada_chec = list( |
|
|
binding = binding_data$mahendrawada_chec %>% |
|
|
filter(log_poisson_pval <= log(0.1)) %>% |
|
|
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
arrange(desc(enrichment)) %>% |
|
|
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>% |
|
|
group_by(sample_id), |
|
|
pr = perturbation_response_data$mahendrawada_rnaseq %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data$mahendrawada_chec$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data$mahendrawada_chec$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)) |
|
|
) |
|
|
|
|
|
|
|
|
create_pr_dto = function(pr_data, pr_effect_col, pr_pval_col, binding_data_list) { |
|
|
|
|
|
|
|
|
pr_standardized = pr_data %>% |
|
|
ungroup() %>% |
|
|
|
|
|
|
|
|
|
|
|
filter(regulator_locus_tag != target_locus_tag) |
|
|
|
|
|
|
|
|
if (pr_effect_col != "effect") { |
|
|
pr_standardized = pr_standardized %>% |
|
|
rename(effect = !!sym(pr_effect_col)) |
|
|
} |
|
|
|
|
|
|
|
|
if (pr_pval_col != "pvalue") { |
|
|
|
|
|
if ("pvalue" %in% colnames(pr_standardized)) { |
|
|
pr_standardized = pr_standardized %>% |
|
|
select(-pvalue) |
|
|
} |
|
|
pr_standardized = pr_standardized %>% |
|
|
rename(pvalue = !!sym(pr_pval_col)) |
|
|
} |
|
|
|
|
|
|
|
|
dto_list = list( |
|
|
cc = list( |
|
|
binding = binding_data_list$cc %>% |
|
|
filter(regulator_locus_tag != target_locus_tag) %>% |
|
|
filter(poisson_pval <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>% |
|
|
group_by(id) %>% |
|
|
arrange(desc(callingcards_enrichment)) %>% |
|
|
mutate(pvalue_rank = rank(poisson_pval, ties.method = 'min')) %>% |
|
|
dplyr::rename(sample_id = id) %>% |
|
|
group_by(sample_id), |
|
|
pr = pr_standardized %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data_list$cc$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data_list$cc$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)), |
|
|
|
|
|
harbison = list( |
|
|
binding = binding_data_list$harbison %>% |
|
|
filter(regulator_locus_tag != target_locus_tag) %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
arrange(desc(effect)) %>% |
|
|
mutate(pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id), |
|
|
pr = pr_standardized %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data_list$harbison$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data_list$harbison$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)), |
|
|
|
|
|
chipexo = list( |
|
|
binding = binding_data_list$chipexo %>% |
|
|
filter(regulator_locus_tag != target_locus_tag) %>% |
|
|
filter(log_poisson_pval <= log(0.1)) %>% |
|
|
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
arrange(desc(enrichment)) %>% |
|
|
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>% |
|
|
group_by(sample_id), |
|
|
pr = pr_standardized %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data_list$chipexo$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data_list$chipexo$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)), |
|
|
|
|
|
mahendrawada_chec = list( |
|
|
binding = binding_data_list$mahendrawada_chec %>% |
|
|
filter(regulator_locus_tag != target_locus_tag) %>% |
|
|
filter(log_poisson_pval <= log(0.1)) %>% |
|
|
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
arrange(desc(enrichment)) %>% |
|
|
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>% |
|
|
group_by(sample_id), |
|
|
pr = pr_standardized %>% |
|
|
filter(pvalue <= 0.1) %>% |
|
|
filter(regulator_locus_tag %in% unique(binding_data_list$mahendrawada_chec$regulator_locus_tag), |
|
|
target_locus_tag %in% unique(binding_data_list$mahendrawada_chec$target_locus_tag)) %>% |
|
|
group_by(sample_id) %>% |
|
|
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'), |
|
|
pvalue_rank = rank(pvalue, ties.method = 'min')) %>% |
|
|
group_by(sample_id)) |
|
|
) |
|
|
|
|
|
return(dto_list) |
|
|
} |
|
|
|
|
|
|
|
|
all_pr_dtos = list( |
|
|
mahendrawada_rnaseq = create_pr_dto( |
|
|
perturbation_response_data$mahendrawada_rnaseq, |
|
|
pr_effect_col = "log2FoldChange", |
|
|
pr_pval_col = "padj", |
|
|
binding_data_list = binding_data |
|
|
), |
|
|
|
|
|
kemmeren = create_pr_dto( |
|
|
perturbation_response_data$kemmeren, |
|
|
pr_effect_col = "Madj", |
|
|
pr_pval_col = "pval", |
|
|
binding_data_list = binding_data |
|
|
), |
|
|
|
|
|
hackett = create_pr_dto( |
|
|
perturbation_response_data$hackett, |
|
|
pr_effect_col = "log2_shrunken_timecourses", |
|
|
pr_pval_col = "pvalue", |
|
|
binding_data_list = binding_data |
|
|
), |
|
|
|
|
|
hu_reimand = create_pr_dto( |
|
|
perturbation_response_data$hu_reimand, |
|
|
pr_effect_col = "effect", |
|
|
pr_pval_col = "pval", |
|
|
binding_data_list = binding_data |
|
|
), |
|
|
|
|
|
hughes_ko = create_pr_dto( |
|
|
perturbation_response_data$hughes_ko, |
|
|
pr_effect_col = "mean_norm_log2fc", |
|
|
pr_pval_col = "pvalue", |
|
|
binding_data_list = binding_data |
|
|
), |
|
|
|
|
|
hughes_oe = create_pr_dto( |
|
|
perturbation_response_data$hughes_oe, |
|
|
pr_effect_col = "mean_norm_log2fc", |
|
|
pr_pval_col = "pvalue", |
|
|
binding_data_list = binding_data |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
write_out_pr_dto_lists = function(pr_dataset_name, |
|
|
binding_pr_set_name, |
|
|
all_pr_dtos_list, |
|
|
base_outdir=here("results/dto")) { |
|
|
|
|
|
output_path = file.path(base_outdir, pr_dataset_name) |
|
|
|
|
|
binding_pr_set = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]] |
|
|
|
|
|
binding_split = binding_pr_set$binding %>% |
|
|
group_split() |
|
|
names(binding_split) = pull(group_keys(binding_pr_set$binding), sample_id) |
|
|
|
|
|
pr_split = binding_pr_set$pr %>% |
|
|
group_split() |
|
|
names(pr_split) = pull(group_keys(binding_pr_set$pr), sample_id) |
|
|
|
|
|
curr_output_path = list( |
|
|
binding = file.path(output_path, binding_pr_set_name, "binding"), |
|
|
pr_effect = file.path(output_path, binding_pr_set_name, "pr", "effect"), |
|
|
pr_pvalue = file.path(output_path, binding_pr_set_name, "pr", "pvalue") |
|
|
) |
|
|
|
|
|
map(curr_output_path, dir.create, recursive = TRUE, showWarnings = FALSE) |
|
|
|
|
|
|
|
|
map(names(binding_split), ~{ |
|
|
binding_split[[.x]] %>% |
|
|
select(target_locus_tag, pvalue_rank) %>% |
|
|
arrange(pvalue_rank) %>% |
|
|
write_csv(file.path(curr_output_path$binding, paste0(.x, ".csv")), |
|
|
col_names = FALSE) |
|
|
}) |
|
|
|
|
|
|
|
|
map(names(pr_split), ~{ |
|
|
pr_split[[.x]] %>% |
|
|
select(target_locus_tag, abs_effect_rank) %>% |
|
|
arrange(abs_effect_rank) %>% |
|
|
write_csv(file.path(curr_output_path$pr_effect, paste0(.x, ".csv")), |
|
|
col_names = FALSE) |
|
|
}) |
|
|
|
|
|
|
|
|
map(names(pr_split), ~{ |
|
|
pr_split[[.x]] %>% |
|
|
select(target_locus_tag, pvalue_rank) %>% |
|
|
arrange(pvalue_rank) %>% |
|
|
write_csv(file.path(curr_output_path$pr_pvalue, paste0(.x, ".csv")), |
|
|
col_names = FALSE) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
write_pr_background = function(pr_dataset_name, |
|
|
binding_pr_set_name, |
|
|
background_list, |
|
|
base_outdir = here("results/dto")) { |
|
|
output_path = file.path(base_outdir, pr_dataset_name) |
|
|
|
|
|
background_list[[binding_pr_set_name]] %>% |
|
|
write_csv(file.path(output_path, binding_pr_set_name, "background.csv"), |
|
|
col_names = FALSE) |
|
|
} |
|
|
|
|
|
|
|
|
create_pr_lookups = function(pr_dataset_name, binding_pr_set_name, |
|
|
all_pr_dtos_list, |
|
|
scratch_path = "/scratch/mblab/chasem/dto") { |
|
|
|
|
|
binding_samples = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$binding %>% |
|
|
ungroup() %>% |
|
|
dplyr::select(sample_id, regulator_locus_tag) %>% |
|
|
distinct() %>% |
|
|
dplyr::rename(binding_id = sample_id) |
|
|
|
|
|
pr_samples = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$pr %>% |
|
|
ungroup() %>% |
|
|
dplyr::select(sample_id, regulator_locus_tag) %>% |
|
|
distinct() %>% |
|
|
dplyr::rename(pr_id = sample_id) |
|
|
|
|
|
|
|
|
lookup_df = binding_samples %>% |
|
|
full_join(pr_samples, by = "regulator_locus_tag", relationship = "many-to-many") %>% |
|
|
mutate(binding = if_else(!is.na(binding_id), |
|
|
file.path(scratch_path, pr_dataset_name, |
|
|
binding_pr_set_name, "binding", |
|
|
paste0(binding_id, ".csv")), |
|
|
NA_character_), |
|
|
pr_effect = if_else(!is.na(pr_id), |
|
|
file.path(scratch_path, pr_dataset_name, |
|
|
binding_pr_set_name, "pr", "effect", |
|
|
paste0(pr_id, ".csv")), |
|
|
NA_character_), |
|
|
pr_pvalue = if_else(!is.na(pr_id), |
|
|
file.path(scratch_path, pr_dataset_name, |
|
|
binding_pr_set_name, "pr", "pvalue", |
|
|
paste0(pr_id, ".csv")), |
|
|
NA_character_)) |
|
|
|
|
|
|
|
|
complete_lookup = lookup_df %>% |
|
|
filter(!is.na(binding_id) & !is.na(pr_id)) %>% |
|
|
select(binding, pr_effect, pr_pvalue) |
|
|
|
|
|
incomplete_after_filtering = lookup_df %>% |
|
|
filter(is.na(binding_id) | is.na(pr_id)) %>% |
|
|
mutate(missing_type = case_when( |
|
|
is.na(binding_id) & is.na(pr_id) ~ "both", |
|
|
is.na(binding_id) ~ "binding", |
|
|
is.na(pr_id) ~ "pr", |
|
|
TRUE ~ "unknown" |
|
|
)) %>% |
|
|
select(regulator_locus_tag, binding_id, pr_id, missing_type) %>% |
|
|
distinct() |
|
|
|
|
|
return(list( |
|
|
lookup = complete_lookup, |
|
|
incomplete_after_filtering = incomplete_after_filtering |
|
|
)) |
|
|
} |
|
|
|
|
|
all_pr_backgrounds = map(names(all_pr_dtos), ~{ |
|
|
map(binding_data, function(bd) { |
|
|
bd %>% |
|
|
ungroup() %>% |
|
|
select(target_locus_tag) %>% |
|
|
distinct() %>% |
|
|
filter(target_locus_tag %in% unique(all_pr_dtos[[.x]][[1]]$pr$target_locus_tag)) |
|
|
}) |
|
|
}) |
|
|
names(all_pr_backgrounds) = names(all_pr_dtos) |
|
|
|
|
|
|
|
|
lookup_results = list() |
|
|
|
|
|
dto_input_outdir = here("results/dto") |
|
|
for (pr_name in names(all_pr_dtos)) { |
|
|
lookup_results[[pr_name]] = list() |
|
|
|
|
|
for (binding_name in names(all_pr_dtos[[pr_name]])) { |
|
|
write_out_pr_dto_lists(pr_name, binding_name, all_pr_dtos) |
|
|
write_pr_background(pr_name, binding_name, all_pr_backgrounds[[pr_name]]) |
|
|
|
|
|
lookup_result = create_pr_lookups(pr_name, binding_name, all_pr_dtos) |
|
|
lookup_results[[pr_name]][[binding_name]] = lookup_result |
|
|
|
|
|
|
|
|
lookup_result$lookup %>% |
|
|
write_tsv(file.path(dto_input_outdir, pr_name, binding_name, "lookup.txt"), |
|
|
col_names = FALSE) |
|
|
|
|
|
|
|
|
if (nrow(lookup_result$incomplete_after_filtering) > 0) { |
|
|
lookup_result$incomplete_after_filtering %>% |
|
|
write_csv(file.path(dto_input_outdir, pr_name, binding_name, "incomplete.csv")) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
incomplete_summary = map_dfr(names(lookup_results), ~{ |
|
|
map_dfr(names(lookup_results[[.x]]), function(binding_name) { |
|
|
lookup_results[[.x]][[binding_name]]$incomplete_after_filtering %>% |
|
|
mutate(pr_dataset = .x, binding_dataset = binding_name) |
|
|
}) |
|
|
}) |
|
|
|
|
|
print(incomplete_summary %>% count(pr_dataset, binding_dataset, missing_type)) |
|
|
|
|
|
dto_results_path_list = list.files(base_outdir, |
|
|
"*.json", |
|
|
recursive = TRUE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dto_results_frames_list = map(file.path(base_outdir, dto_results_path_list), |
|
|
~as_tibble(jsonlite::read_json(.x))) |
|
|
names(dto_results_frames_list) = dto_results_path_list |
|
|
dto_results_frame = bind_rows(dto_results_frames_list, .id = 'path') |
|
|
|
|
|
curr_dto_res = arrow::open_dataset("~/code/hf/yeast_comparative_analysis/dto") %>% |
|
|
collect() |
|
|
|
|
|
results_df = tibble( |
|
|
combined_id = str_remove(basename(dto_results_list), ".json"), |
|
|
binding_source = dirname(dirname(dirname(dirname(dto_results_list)))), |
|
|
pr_scoring = basename(dirname(dto_results_list)), |
|
|
path = dto_results_path_list) %>% |
|
|
separate_wider_delim(combined_id, |
|
|
names = c('binding_sampleid', 'pr_sampleid'), |
|
|
delim = '-_-') %>% |
|
|
left_join(dto_results_frame) %>% |
|
|
select(-path) %>% |
|
|
mutate( |
|
|
binding_id = case_when( |
|
|
binding_source == "cc" & str_detect(binding_sampleid, "-") |
|
|
~ paste0("BrentLab/callingcards;annotated_features_combined", |
|
|
binding_sampleid), |
|
|
binding_source == "cc" & str_detect(binding_sampleid, "-", negate=TRUE) |
|
|
~ paste0("BrentLab/callingcards;annotated_features", |
|
|
binding_sampleid), |
|
|
binding_source == "chipexo" |
|
|
~ paste0("BrentLab/rossi_2021;rossi_2021_af_combined", |
|
|
binding_sampleid), |
|
|
binding_source == "harbison" |
|
|
~ paste0("BrentLab/harbison_2004;harbison_2004", |
|
|
binding_sampleid), |
|
|
binding_source == "mahendrawada_chec" |
|
|
~ paste0("BrentLab/mahendrawada_2025;chec_mahendrawada_m2025_af_combined", |
|
|
binding_sampleid)), |
|
|
perturbation_id = paste0("BrentLab/mahendrawada_2025/rnaseq_reprocessed", pr_sampleid), |
|
|
binding_repo_dataset = case_when( |
|
|
binding_source == "cc" & str_detect(binding_sampleid, "-") |
|
|
~ "callingcards-annotated_features_combined", |
|
|
binding_source == "cc" & str_detect(binding_sampleid, "-", negate=TRUE) |
|
|
~ "callingcards-annotated_features", |
|
|
binding_source == "chipexo" |
|
|
~ "rossi_2021-rossi_2021_af_combined", |
|
|
binding_source == "harbison" |
|
|
~ paste0("harbison_2004-harbison_2004"), |
|
|
binding_source == "mahendrawada_chec" |
|
|
~ "mahendrawada_2025-chec_mahendrawada_m2025_af_combined"), |
|
|
perturbation_repo_dataset = "mahendrawada_2025-rnaseq_reprocessed") %>% |
|
|
dplyr::rename(binding_rank_threshold = rank1, |
|
|
perturbation_rank_threshold = rank2, |
|
|
binding_set_size = set1_len, |
|
|
perturbation_set_size = set2_len, |
|
|
dto_fdr = fdr, |
|
|
dto_empirical_pvalue = empirical_pvalue) %>% |
|
|
select(binding_id, perturbation_id, |
|
|
binding_rank_threshold, perturbation_rank_threshold, |
|
|
binding_set_size, perturbation_set_size, |
|
|
dto_fdr, dto_empirical_pvalue, |
|
|
binding_repo_dataset, perturbation_repo_dataset) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|