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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
library(tidyverse)
|
| 2 |
+
library(arrow)
|
| 3 |
+
library(here)
|
| 4 |
+
|
| 5 |
+
# these are the protein coding non dubious loci
|
| 6 |
+
mahendrawada_features = arrow::read_parquet("~/code/hf/mahendrawada_2025/features_mahendrawada_2025.parquet")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# read in and prepare the perturbation response data
|
| 10 |
+
perturbation_response_data = list(
|
| 11 |
+
mahendrawada_rnaseq = arrow::read_parquet("~/code/hf/mahendrawada_2025/rnaseq_reprocessed.parquet") %>%
|
| 12 |
+
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>%
|
| 13 |
+
replace_na(list(log2FoldChange = 0, pvalue = 1)) %>%
|
| 14 |
+
mutate(abs_log2fc = abs(log2FoldChange)),
|
| 15 |
+
# kemmeren requires deduplicating instances where there are multiple probes
|
| 16 |
+
# to the same locus_tag. Take the max
|
| 17 |
+
kemmeren = arrow::open_dataset("~/code/hf/kemmeren_2014/kemmeren_2014.parquet") %>%
|
| 18 |
+
filter(target_locus_tag %in% mahendrawada_features$locus_tag,
|
| 19 |
+
str_detect(regulator_locus_tag, "WT-", negate=TRUE)) %>%
|
| 20 |
+
select(sample_id, regulator_locus_tag, target_locus_tag, Madj, pval) %>%
|
| 21 |
+
arrow::to_duckdb() %>%
|
| 22 |
+
group_by(sample_id, target_locus_tag) %>%
|
| 23 |
+
mutate(rn = row_number(desc(abs(Madj)))) %>%
|
| 24 |
+
filter(rn == 1) %>%
|
| 25 |
+
select(-rn) %>%
|
| 26 |
+
ungroup() %>%
|
| 27 |
+
collect(),
|
| 28 |
+
hackett = arrow::read_parquet("~/code/hf/hackett_2020/hackett_2020.parquet") %>%
|
| 29 |
+
filter(target_locus_tag %in% mahendrawada_features$locus_tag,
|
| 30 |
+
str_detect(regulator_locus_tag, "WT-", negate=TRUE)) %>%
|
| 31 |
+
select(sample_id, regulator_locus_tag, target_locus_tag, log2_shrunken_timecourses) %>%
|
| 32 |
+
arrow::to_duckdb() %>%
|
| 33 |
+
group_by(sample_id, target_locus_tag) %>%
|
| 34 |
+
mutate(rn = row_number(desc(abs(log2_shrunken_timecourses)))) %>%
|
| 35 |
+
filter(rn == 1) %>%
|
| 36 |
+
select(-rn) %>%
|
| 37 |
+
ungroup() %>%
|
| 38 |
+
collect() %>%
|
| 39 |
+
# add this for consistency with the other datasets
|
| 40 |
+
mutate(pvalue = 0),
|
| 41 |
+
hu_reimand = arrow::read_parquet("~/code/hf/hu_2007_reimand_2010/hu_2007_reimand_2010.parquet") %>%
|
| 42 |
+
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>%
|
| 43 |
+
select(sample_id, regulator_locus_tag, target_locus_tag, effect, pval) %>%
|
| 44 |
+
arrow::to_duckdb() %>%
|
| 45 |
+
group_by(sample_id, target_locus_tag) %>%
|
| 46 |
+
mutate(rn = row_number(desc(abs(effect)))) %>%
|
| 47 |
+
filter(rn == 1) %>%
|
| 48 |
+
select(-rn) %>%
|
| 49 |
+
ungroup() %>%
|
| 50 |
+
collect(),
|
| 51 |
+
hughes_ko = arrow::read_parquet("~/code/hf/hughes_2006/knockout.parquet") %>%
|
| 52 |
+
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>%
|
| 53 |
+
select(sample_id, regulator_locus_tag, target_locus_tag, mean_norm_log2fc) %>%
|
| 54 |
+
arrow::to_duckdb() %>%
|
| 55 |
+
group_by(sample_id, target_locus_tag) %>%
|
| 56 |
+
mutate(rn = row_number(desc(abs(mean_norm_log2fc)))) %>%
|
| 57 |
+
filter(rn == 1) %>%
|
| 58 |
+
select(-rn) %>%
|
| 59 |
+
ungroup() %>%
|
| 60 |
+
collect() %>%
|
| 61 |
+
# add this for consistency with the other datasets
|
| 62 |
+
mutate(pvalue = 0),
|
| 63 |
+
hughes_oe = arrow::read_parquet("~/code/hf/hughes_2006/overexpression.parquet") %>%
|
| 64 |
+
filter(target_locus_tag %in% mahendrawada_features$locus_tag) %>%
|
| 65 |
+
select(sample_id, regulator_locus_tag, target_locus_tag, mean_norm_log2fc) %>%
|
| 66 |
+
arrow::to_duckdb() %>%
|
| 67 |
+
group_by(sample_id, target_locus_tag) %>%
|
| 68 |
+
mutate(rn = row_number(desc(abs(mean_norm_log2fc)))) %>%
|
| 69 |
+
filter(rn == 1) %>%
|
| 70 |
+
select(-rn) %>%
|
| 71 |
+
ungroup() %>%
|
| 72 |
+
collect() %>%
|
| 73 |
+
# add this for consistency with the other datasets
|
| 74 |
+
mutate(pvalue = 0)
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
composite_cc = arrow::open_dataset("~/code/hf/callingcards/annotated_features_combined") %>%
|
| 78 |
+
collect() %>%
|
| 79 |
+
left_join(arrow::read_parquet("~/code/hf/callingcards/annotated_features_combined_meta.parquet")) %>%
|
| 80 |
+
dplyr::rename(id = genome_map_id_set)
|
| 81 |
+
|
| 82 |
+
single_cc_meta = arrow::read_parquet("~/code/hf/callingcards/annotated_features_meta.parquet") %>%
|
| 83 |
+
filter(batch != "composite")
|
| 84 |
+
|
| 85 |
+
single_cc = arrow::open_dataset("~/code/hf/callingcards/annotated_features") %>%
|
| 86 |
+
filter(id %in% single_cc_meta$id) %>%
|
| 87 |
+
collect() %>%
|
| 88 |
+
left_join(single_cc_meta) %>%
|
| 89 |
+
mutate(id = as.character(id))
|
| 90 |
+
|
| 91 |
+
binding_data = list(
|
| 92 |
+
cc = single_cc %>%
|
| 93 |
+
select(intersect(colnames(.), colnames(composite_cc))) %>%
|
| 94 |
+
bind_rows(composite_cc %>%
|
| 95 |
+
select(intersect(colnames(.), colnames(single_cc)))),
|
| 96 |
+
harbison = arrow::read_parquet("~/code/hf/harbison_2004/harbison_2004.parquet") %>%
|
| 97 |
+
replace_na(list(effect = 0, pvalue = 1)) %>%
|
| 98 |
+
group_by(sample_id, target_locus_tag) %>%
|
| 99 |
+
slice_max(abs(effect), n = 1, with_ties = FALSE) %>%
|
| 100 |
+
ungroup(),
|
| 101 |
+
chipexo = arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_af_combined.parquet") %>%
|
| 102 |
+
left_join(arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_metadata_sample.parquet")),
|
| 103 |
+
mahendrawada_chec = arrow::read_parquet("~/code/hf/mahendrawada_2025/chec_mahendrawada_m2025_af_combined.parquet") %>%
|
| 104 |
+
left_join(arrow::read_parquet("~/code/hf/mahendrawada_2025/chec_mahendrawada_m2025_af_combined_meta.parquet"))
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
mahendrawada_rnaseq_dto_background = map(binding_data, ~{
|
| 108 |
+
.x %>%
|
| 109 |
+
ungroup() %>%
|
| 110 |
+
select(target_locus_tag) %>%
|
| 111 |
+
distinct() %>%
|
| 112 |
+
filter(target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag))
|
| 113 |
+
})
|
| 114 |
+
|
| 115 |
+
mahendrawada_rnaseq_dto = list(
|
| 116 |
+
cc = list(
|
| 117 |
+
binding = binding_data$cc %>%
|
| 118 |
+
filter(poisson_pval <= 0.1) %>%
|
| 119 |
+
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag),
|
| 120 |
+
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>%
|
| 121 |
+
group_by(id) %>%
|
| 122 |
+
arrange(desc(callingcards_enrichment)) %>%
|
| 123 |
+
mutate(pvalue_rank = rank(poisson_pval, ties.method = 'min')) %>%
|
| 124 |
+
dplyr::rename(sample_id = id) %>%
|
| 125 |
+
group_by(sample_id),
|
| 126 |
+
pr = perturbation_response_data$mahendrawada_rnaseq %>%
|
| 127 |
+
filter(pvalue <= 0.1) %>%
|
| 128 |
+
filter(regulator_locus_tag %in% unique(binding_data$cc$regulator_locus_tag),
|
| 129 |
+
target_locus_tag %in% unique(binding_data$cc$target_locus_tag)) %>%
|
| 130 |
+
group_by(sample_id) %>%
|
| 131 |
+
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'),
|
| 132 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 133 |
+
group_by(sample_id)),
|
| 134 |
+
harbison = list(
|
| 135 |
+
binding = binding_data$harbison %>%
|
| 136 |
+
filter(pvalue <= 0.1) %>%
|
| 137 |
+
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag),
|
| 138 |
+
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>%
|
| 139 |
+
group_by(sample_id) %>%
|
| 140 |
+
arrange(desc(effect)) %>%
|
| 141 |
+
mutate(pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 142 |
+
group_by(sample_id),
|
| 143 |
+
pr = perturbation_response_data$mahendrawada_rnaseq %>%
|
| 144 |
+
filter(pvalue <= 0.1) %>%
|
| 145 |
+
filter(regulator_locus_tag %in% unique(binding_data$harbison$regulator_locus_tag),
|
| 146 |
+
target_locus_tag %in% unique(binding_data$harbison$target_locus_tag)) %>%
|
| 147 |
+
group_by(sample_id) %>%
|
| 148 |
+
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'),
|
| 149 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 150 |
+
group_by(sample_id)),
|
| 151 |
+
chipexo = list(
|
| 152 |
+
binding = binding_data$chipexo %>%
|
| 153 |
+
filter(log_poisson_pval <= log(0.1)) %>%
|
| 154 |
+
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag),
|
| 155 |
+
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>%
|
| 156 |
+
group_by(sample_id) %>%
|
| 157 |
+
arrange(desc(enrichment)) %>%
|
| 158 |
+
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>%
|
| 159 |
+
group_by(sample_id),
|
| 160 |
+
pr = perturbation_response_data$mahendrawada_rnaseq %>%
|
| 161 |
+
filter(pvalue <= 0.1) %>%
|
| 162 |
+
filter(regulator_locus_tag %in% unique(binding_data$chipexo$regulator_locus_tag),
|
| 163 |
+
target_locus_tag %in% unique(binding_data$chipexo$target_locus_tag)) %>%
|
| 164 |
+
group_by(sample_id) %>%
|
| 165 |
+
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'),
|
| 166 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 167 |
+
group_by(sample_id)),
|
| 168 |
+
mahendrawada_chec = list(
|
| 169 |
+
binding = binding_data$mahendrawada_chec %>%
|
| 170 |
+
filter(log_poisson_pval <= log(0.1)) %>%
|
| 171 |
+
filter(regulator_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$regulator_locus_tag),
|
| 172 |
+
target_locus_tag %in% unique(perturbation_response_data$mahendrawada_rnaseq$target_locus_tag)) %>%
|
| 173 |
+
group_by(sample_id) %>%
|
| 174 |
+
arrange(desc(enrichment)) %>%
|
| 175 |
+
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>%
|
| 176 |
+
group_by(sample_id),
|
| 177 |
+
pr = perturbation_response_data$mahendrawada_rnaseq %>%
|
| 178 |
+
filter(pvalue <= 0.1) %>%
|
| 179 |
+
filter(regulator_locus_tag %in% unique(binding_data$mahendrawada_chec$regulator_locus_tag),
|
| 180 |
+
target_locus_tag %in% unique(binding_data$mahendrawada_chec$target_locus_tag)) %>%
|
| 181 |
+
group_by(sample_id) %>%
|
| 182 |
+
mutate(abs_log2fc_rank = rank(-abs_log2fc, ties.method = 'min'),
|
| 183 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 184 |
+
group_by(sample_id))
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# Function to create DTO for a given PR dataset
|
| 188 |
+
create_pr_dto = function(pr_data, pr_effect_col, pr_pval_col, binding_data_list) {
|
| 189 |
+
|
| 190 |
+
# Standardize column names for the PR data
|
| 191 |
+
pr_standardized = pr_data %>%
|
| 192 |
+
ungroup() %>%
|
| 193 |
+
# remove the target observation for the perturbed locus
|
| 194 |
+
# NOTE: this is also done for the binding data, though i don't
|
| 195 |
+
# remove it from the background
|
| 196 |
+
filter(regulator_locus_tag != target_locus_tag)
|
| 197 |
+
|
| 198 |
+
# Handle effect column renaming
|
| 199 |
+
if (pr_effect_col != "effect") {
|
| 200 |
+
pr_standardized = pr_standardized %>%
|
| 201 |
+
rename(effect = !!sym(pr_effect_col))
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
# Handle pvalue column renaming
|
| 205 |
+
if (pr_pval_col != "pvalue") {
|
| 206 |
+
# If renaming a different column to pvalue, drop existing pvalue column first
|
| 207 |
+
if ("pvalue" %in% colnames(pr_standardized)) {
|
| 208 |
+
pr_standardized = pr_standardized %>%
|
| 209 |
+
select(-pvalue)
|
| 210 |
+
}
|
| 211 |
+
pr_standardized = pr_standardized %>%
|
| 212 |
+
rename(pvalue = !!sym(pr_pval_col))
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
# Create DTOs for each binding dataset
|
| 216 |
+
dto_list = list(
|
| 217 |
+
cc = list(
|
| 218 |
+
binding = binding_data_list$cc %>%
|
| 219 |
+
filter(regulator_locus_tag != target_locus_tag) %>%
|
| 220 |
+
filter(poisson_pval <= 0.1) %>%
|
| 221 |
+
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag),
|
| 222 |
+
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>%
|
| 223 |
+
group_by(id) %>%
|
| 224 |
+
arrange(desc(callingcards_enrichment)) %>%
|
| 225 |
+
mutate(pvalue_rank = rank(poisson_pval, ties.method = 'min')) %>%
|
| 226 |
+
dplyr::rename(sample_id = id) %>%
|
| 227 |
+
group_by(sample_id),
|
| 228 |
+
pr = pr_standardized %>%
|
| 229 |
+
filter(pvalue <= 0.1) %>%
|
| 230 |
+
filter(regulator_locus_tag %in% unique(binding_data_list$cc$regulator_locus_tag),
|
| 231 |
+
target_locus_tag %in% unique(binding_data_list$cc$target_locus_tag)) %>%
|
| 232 |
+
group_by(sample_id) %>%
|
| 233 |
+
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'),
|
| 234 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 235 |
+
group_by(sample_id)),
|
| 236 |
+
|
| 237 |
+
harbison = list(
|
| 238 |
+
binding = binding_data_list$harbison %>%
|
| 239 |
+
filter(regulator_locus_tag != target_locus_tag) %>%
|
| 240 |
+
filter(pvalue <= 0.1) %>%
|
| 241 |
+
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag),
|
| 242 |
+
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>%
|
| 243 |
+
group_by(sample_id) %>%
|
| 244 |
+
arrange(desc(effect)) %>%
|
| 245 |
+
mutate(pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 246 |
+
group_by(sample_id),
|
| 247 |
+
pr = pr_standardized %>%
|
| 248 |
+
filter(pvalue <= 0.1) %>%
|
| 249 |
+
filter(regulator_locus_tag %in% unique(binding_data_list$harbison$regulator_locus_tag),
|
| 250 |
+
target_locus_tag %in% unique(binding_data_list$harbison$target_locus_tag)) %>%
|
| 251 |
+
group_by(sample_id) %>%
|
| 252 |
+
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'),
|
| 253 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 254 |
+
group_by(sample_id)),
|
| 255 |
+
|
| 256 |
+
chipexo = list(
|
| 257 |
+
binding = binding_data_list$chipexo %>%
|
| 258 |
+
filter(regulator_locus_tag != target_locus_tag) %>%
|
| 259 |
+
filter(log_poisson_pval <= log(0.1)) %>%
|
| 260 |
+
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag),
|
| 261 |
+
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>%
|
| 262 |
+
group_by(sample_id) %>%
|
| 263 |
+
arrange(desc(enrichment)) %>%
|
| 264 |
+
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>%
|
| 265 |
+
group_by(sample_id),
|
| 266 |
+
pr = pr_standardized %>%
|
| 267 |
+
filter(pvalue <= 0.1) %>%
|
| 268 |
+
filter(regulator_locus_tag %in% unique(binding_data_list$chipexo$regulator_locus_tag),
|
| 269 |
+
target_locus_tag %in% unique(binding_data_list$chipexo$target_locus_tag)) %>%
|
| 270 |
+
group_by(sample_id) %>%
|
| 271 |
+
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'),
|
| 272 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 273 |
+
group_by(sample_id)),
|
| 274 |
+
|
| 275 |
+
mahendrawada_chec = list(
|
| 276 |
+
binding = binding_data_list$mahendrawada_chec %>%
|
| 277 |
+
filter(regulator_locus_tag != target_locus_tag) %>%
|
| 278 |
+
filter(log_poisson_pval <= log(0.1)) %>%
|
| 279 |
+
filter(regulator_locus_tag %in% unique(pr_standardized$regulator_locus_tag),
|
| 280 |
+
target_locus_tag %in% unique(pr_standardized$target_locus_tag)) %>%
|
| 281 |
+
group_by(sample_id) %>%
|
| 282 |
+
arrange(desc(enrichment)) %>%
|
| 283 |
+
mutate(pvalue_rank = rank(log_poisson_pval, ties.method = 'min')) %>%
|
| 284 |
+
group_by(sample_id),
|
| 285 |
+
pr = pr_standardized %>%
|
| 286 |
+
filter(pvalue <= 0.1) %>%
|
| 287 |
+
filter(regulator_locus_tag %in% unique(binding_data_list$mahendrawada_chec$regulator_locus_tag),
|
| 288 |
+
target_locus_tag %in% unique(binding_data_list$mahendrawada_chec$target_locus_tag)) %>%
|
| 289 |
+
group_by(sample_id) %>%
|
| 290 |
+
mutate(abs_effect_rank = rank(-abs(effect), ties.method = 'min'),
|
| 291 |
+
pvalue_rank = rank(pvalue, ties.method = 'min')) %>%
|
| 292 |
+
group_by(sample_id))
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
return(dto_list)
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
# Create all DTOs
|
| 299 |
+
all_pr_dtos = list(
|
| 300 |
+
mahendrawada_rnaseq = create_pr_dto(
|
| 301 |
+
perturbation_response_data$mahendrawada_rnaseq,
|
| 302 |
+
pr_effect_col = "log2FoldChange",
|
| 303 |
+
pr_pval_col = "padj",
|
| 304 |
+
binding_data_list = binding_data
|
| 305 |
+
),
|
| 306 |
+
|
| 307 |
+
kemmeren = create_pr_dto(
|
| 308 |
+
perturbation_response_data$kemmeren,
|
| 309 |
+
pr_effect_col = "Madj",
|
| 310 |
+
pr_pval_col = "pval",
|
| 311 |
+
binding_data_list = binding_data
|
| 312 |
+
),
|
| 313 |
+
|
| 314 |
+
hackett = create_pr_dto(
|
| 315 |
+
perturbation_response_data$hackett,
|
| 316 |
+
pr_effect_col = "log2_shrunken_timecourses",
|
| 317 |
+
pr_pval_col = "pvalue",
|
| 318 |
+
binding_data_list = binding_data
|
| 319 |
+
),
|
| 320 |
+
|
| 321 |
+
hu_reimand = create_pr_dto(
|
| 322 |
+
perturbation_response_data$hu_reimand,
|
| 323 |
+
pr_effect_col = "effect",
|
| 324 |
+
pr_pval_col = "pval",
|
| 325 |
+
binding_data_list = binding_data
|
| 326 |
+
),
|
| 327 |
+
|
| 328 |
+
hughes_ko = create_pr_dto(
|
| 329 |
+
perturbation_response_data$hughes_ko,
|
| 330 |
+
pr_effect_col = "mean_norm_log2fc",
|
| 331 |
+
pr_pval_col = "pvalue",
|
| 332 |
+
binding_data_list = binding_data
|
| 333 |
+
),
|
| 334 |
+
|
| 335 |
+
hughes_oe = create_pr_dto(
|
| 336 |
+
perturbation_response_data$hughes_oe,
|
| 337 |
+
pr_effect_col = "mean_norm_log2fc",
|
| 338 |
+
pr_pval_col = "pvalue",
|
| 339 |
+
binding_data_list = binding_data
|
| 340 |
+
)
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
# Write out DTO ranked lists
|
| 344 |
+
|
| 345 |
+
results_basedir = "~/htcf_local/dto"
|
| 346 |
+
|
| 347 |
+
write_out_pr_dto_lists = function(pr_dataset_name, binding_pr_set_name, all_pr_dtos_list) {
|
| 348 |
+
|
| 349 |
+
output_path = file.path(results_basedir, pr_dataset_name)
|
| 350 |
+
|
| 351 |
+
binding_pr_set = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]
|
| 352 |
+
|
| 353 |
+
binding_split = binding_pr_set$binding %>%
|
| 354 |
+
group_split()
|
| 355 |
+
names(binding_split) = pull(group_keys(binding_pr_set$binding), sample_id)
|
| 356 |
+
|
| 357 |
+
pr_split = binding_pr_set$pr %>%
|
| 358 |
+
group_split()
|
| 359 |
+
names(pr_split) = pull(group_keys(binding_pr_set$pr), sample_id)
|
| 360 |
+
|
| 361 |
+
curr_output_path = list(
|
| 362 |
+
binding = file.path(output_path, binding_pr_set_name, "binding"),
|
| 363 |
+
pr_effect = file.path(output_path, binding_pr_set_name, "pr", "effect"),
|
| 364 |
+
pr_pvalue = file.path(output_path, binding_pr_set_name, "pr", "pvalue")
|
| 365 |
+
)
|
| 366 |
+
|
| 367 |
+
map(curr_output_path, dir.create, recursive = TRUE, showWarnings = FALSE)
|
| 368 |
+
|
| 369 |
+
# Write out binding lists
|
| 370 |
+
map(names(binding_split), ~{
|
| 371 |
+
binding_split[[.x]] %>%
|
| 372 |
+
select(target_locus_tag, pvalue_rank) %>%
|
| 373 |
+
arrange(pvalue_rank) %>%
|
| 374 |
+
write_csv(file.path(curr_output_path$binding, paste0(.x, ".csv")),
|
| 375 |
+
col_names = FALSE)
|
| 376 |
+
})
|
| 377 |
+
|
| 378 |
+
# Write out effect-ranked pr lists
|
| 379 |
+
map(names(pr_split), ~{
|
| 380 |
+
pr_split[[.x]] %>%
|
| 381 |
+
select(target_locus_tag, abs_effect_rank) %>%
|
| 382 |
+
arrange(abs_effect_rank) %>%
|
| 383 |
+
write_csv(file.path(curr_output_path$pr_effect, paste0(.x, ".csv")),
|
| 384 |
+
col_names = FALSE)
|
| 385 |
+
})
|
| 386 |
+
|
| 387 |
+
# Write out pvalue pr lists
|
| 388 |
+
map(names(pr_split), ~{
|
| 389 |
+
pr_split[[.x]] %>%
|
| 390 |
+
select(target_locus_tag, pvalue_rank) %>%
|
| 391 |
+
arrange(pvalue_rank) %>%
|
| 392 |
+
write_csv(file.path(curr_output_path$pr_pvalue, paste0(.x, ".csv")),
|
| 393 |
+
col_names = FALSE)
|
| 394 |
+
})
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
# Generalized function to write background
|
| 398 |
+
write_pr_background = function(pr_dataset_name, binding_pr_set_name, background_list) {
|
| 399 |
+
output_path = file.path(results_basedir, pr_dataset_name)
|
| 400 |
+
|
| 401 |
+
background_list[[binding_pr_set_name]] %>%
|
| 402 |
+
write_csv(file.path(output_path, binding_pr_set_name, "background.csv"),
|
| 403 |
+
col_names = FALSE)
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
# Generalized function to create lookups
|
| 407 |
+
create_pr_lookups = function(pr_dataset_name, binding_pr_set_name, all_pr_dtos_list, scratch_path = "/scratch/mblab/chasem/dto") {
|
| 408 |
+
|
| 409 |
+
lookup_df = all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$binding %>%
|
| 410 |
+
ungroup() %>%
|
| 411 |
+
dplyr::select(sample_id, regulator_locus_tag) %>%
|
| 412 |
+
distinct() %>%
|
| 413 |
+
dplyr::rename(binding_id = sample_id) %>%
|
| 414 |
+
left_join(all_pr_dtos_list[[pr_dataset_name]][[binding_pr_set_name]]$pr %>%
|
| 415 |
+
ungroup() %>%
|
| 416 |
+
dplyr::select(sample_id, regulator_locus_tag) %>%
|
| 417 |
+
distinct() %>%
|
| 418 |
+
dplyr::rename(pr_id = sample_id)) %>%
|
| 419 |
+
mutate(binding = file.path(scratch_path, pr_dataset_name,
|
| 420 |
+
binding_pr_set_name, "binding",
|
| 421 |
+
paste0(binding_id, ".csv")),
|
| 422 |
+
pr_effect = file.path(scratch_path, pr_dataset_name,
|
| 423 |
+
binding_pr_set_name, "pr", "effect",
|
| 424 |
+
paste0(pr_id, ".csv")),
|
| 425 |
+
pr_pvalue = file.path(scratch_path, pr_dataset_name,
|
| 426 |
+
binding_pr_set_name, "pr", "pvalue",
|
| 427 |
+
paste0(pr_id, ".csv"))) %>%
|
| 428 |
+
select(binding, pr_effect, pr_pvalue)
|
| 429 |
+
|
| 430 |
+
return(lookup_df)
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
# Create background lists for all PR datasets (if not already created)
|
| 434 |
+
all_pr_backgrounds = map(names(all_pr_dtos), ~{
|
| 435 |
+
map(binding_data, function(bd) {
|
| 436 |
+
bd %>%
|
| 437 |
+
ungroup() %>%
|
| 438 |
+
select(target_locus_tag) %>%
|
| 439 |
+
distinct() %>%
|
| 440 |
+
filter(target_locus_tag %in% unique(all_pr_dtos[[.x]][[1]]$pr$target_locus_tag))
|
| 441 |
+
})
|
| 442 |
+
})
|
| 443 |
+
names(all_pr_backgrounds) = names(all_pr_dtos)
|
| 444 |
+
|
| 445 |
+
# Write out all DTOs for all PR datasets
|
| 446 |
+
for (pr_name in names(all_pr_dtos)) {
|
| 447 |
+
for (binding_name in names(all_pr_dtos[[pr_name]])) {
|
| 448 |
+
write_out_pr_dto_lists(pr_name, binding_name, all_pr_dtos)
|
| 449 |
+
write_pr_background(pr_name, binding_name, all_pr_backgrounds[[pr_name]])
|
| 450 |
+
|
| 451 |
+
create_pr_lookups(pr_name, binding_name, all_pr_dtos) %>%
|
| 452 |
+
write_tsv(file.path(results_basedir, pr_name, binding_name, "lookup.txt"),
|
| 453 |
+
col_names = FALSE)
|
| 454 |
+
}
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
dto_results_path_list = list.files(results_basedir,
|
| 458 |
+
"*.json",
|
| 459 |
+
recursive = TRUE)
|
| 460 |
+
|
| 461 |
+
## Parse the results -- note that this needs to be adjusted for the
|
| 462 |
+
## expanded set of perturbation response
|
| 463 |
+
|
| 464 |
+
dto_results_frames_list = map(file.path(results_basedir, dto_results_path_list),
|
| 465 |
+
~as_tibble(jsonlite::read_json(.x)))
|
| 466 |
+
names(dto_results_frames_list) = dto_results_path_list
|
| 467 |
+
dto_results_frame = bind_rows(dto_results_frames_list, .id = 'path')
|
| 468 |
+
|
| 469 |
+
curr_dto_res = arrow::open_dataset("~/code/hf/yeast_comparative_analysis/dto") %>%
|
| 470 |
+
collect()
|
| 471 |
+
|
| 472 |
+
results_df = tibble(
|
| 473 |
+
combined_id = str_remove(basename(dto_results_list), ".json"),
|
| 474 |
+
binding_source = dirname(dirname(dirname(dirname(dto_results_list)))),
|
| 475 |
+
pr_scoring = basename(dirname(dto_results_list)),
|
| 476 |
+
path = dto_results_path_list) %>%
|
| 477 |
+
separate_wider_delim(combined_id,
|
| 478 |
+
names = c('binding_sampleid', 'pr_sampleid'),
|
| 479 |
+
delim = '-_-') %>%
|
| 480 |
+
left_join(dto_results_frame) %>%
|
| 481 |
+
select(-path) %>%
|
| 482 |
+
mutate(
|
| 483 |
+
binding_id = case_when(
|
| 484 |
+
binding_source == "cc" & str_detect(binding_sampleid, "-")
|
| 485 |
+
~ paste0("BrentLab/callingcards;annotated_features_combined",
|
| 486 |
+
binding_sampleid),
|
| 487 |
+
binding_source == "cc" & str_detect(binding_sampleid, "-", negate=TRUE)
|
| 488 |
+
~ paste0("BrentLab/callingcards;annotated_features",
|
| 489 |
+
binding_sampleid),
|
| 490 |
+
binding_source == "chipexo"
|
| 491 |
+
~ paste0("BrentLab/rossi_2021;rossi_2021_af_combined",
|
| 492 |
+
binding_sampleid),
|
| 493 |
+
binding_source == "harbison"
|
| 494 |
+
~ paste0("BrentLab/harbison_2004;harbison_2004",
|
| 495 |
+
binding_sampleid),
|
| 496 |
+
binding_source == "mahendrawada_chec"
|
| 497 |
+
~ paste0("BrentLab/mahendrawada_2025;chec_mahendrawada_m2025_af_combined",
|
| 498 |
+
binding_sampleid)),
|
| 499 |
+
perturbation_id = paste0("BrentLab/mahendrawada_2025/rnaseq_reprocessed", pr_sampleid),
|
| 500 |
+
binding_repo_dataset = case_when(
|
| 501 |
+
binding_source == "cc" & str_detect(binding_sampleid, "-")
|
| 502 |
+
~ "callingcards-annotated_features_combined",
|
| 503 |
+
binding_source == "cc" & str_detect(binding_sampleid, "-", negate=TRUE)
|
| 504 |
+
~ "callingcards-annotated_features",
|
| 505 |
+
binding_source == "chipexo"
|
| 506 |
+
~ "rossi_2021-rossi_2021_af_combined",
|
| 507 |
+
binding_source == "harbison"
|
| 508 |
+
~ paste0("harbison_2004-harbison_2004"),
|
| 509 |
+
binding_source == "mahendrawada_chec"
|
| 510 |
+
~ "mahendrawada_2025-chec_mahendrawada_m2025_af_combined"),
|
| 511 |
+
perturbation_repo_dataset = "mahendrawada_2025-rnaseq_reprocessed") %>%
|
| 512 |
+
dplyr::rename(binding_rank_threshold = rank1,
|
| 513 |
+
perturbation_rank_threshold = rank2,
|
| 514 |
+
binding_set_size = set1_len,
|
| 515 |
+
perturbation_set_size = set2_len,
|
| 516 |
+
dto_fdr = fdr,
|
| 517 |
+
dto_empirical_pvalue = empirical_pvalue) %>%
|
| 518 |
+
select(binding_id, perturbation_id,
|
| 519 |
+
binding_rank_threshold, perturbation_rank_threshold,
|
| 520 |
+
binding_set_size, perturbation_set_size,
|
| 521 |
+
dto_fdr, dto_empirical_pvalue,
|
| 522 |
+
binding_repo_dataset, perturbation_repo_dataset)
|
| 523 |
+
|
| 524 |
+
# arrow::write_dataset(
|
| 525 |
+
# results_df,
|
| 526 |
+
# path = "/home/chase/code/hf/yeast_comparative_analysis/dto",
|
| 527 |
+
# format = "parquet",
|
| 528 |
+
# partitioning = c("binding_repo_dataset", "perturbation_repo_dataset"),
|
| 529 |
+
# existing_data_behavior = "overwrite",
|
| 530 |
+
# compression = "zstd",
|
| 531 |
+
# write_statistics = TRUE,
|
| 532 |
+
# use_dictionary = c(
|
| 533 |
+
# binding_id = TRUE,
|
| 534 |
+
# perturbation_id = TRUE
|
| 535 |
+
# )
|
| 536 |
+
# )
|