File size: 6,738 Bytes
0381f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee0e998
0381f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
library(tidyverse)
library(furrr)
library(here)

dto_results_lookup = expand_grid(
    pr_source = c("hackett", "hughes_oe", "hughes_ko", "hu_reimand", "kemmeren", "mahendrawada_rnaseq"),
    binding_source = c("cc", "chipexo", "harbison", "mahendrawada_chec"),
    pr_ranking_column = c("log2fc", "pvalue")) %>%
    mutate(
        result_file = map(file.path(here("results/dto"),
                                    pr_source,
                                    binding_source, "results",
                                    pr_ranking_column),
                          ~list.files(.x, full.names = TRUE))) %>%
    unnest(result_file) %>%
    mutate(tmp = str_remove(basename(result_file), ".json")) %>%
    separate_wider_delim(tmp,
                         names = c('binding_id', 'perturbation_id'),
                         delim = "-_-")

plan(multisession, workers = 20)

# Function with retry logic
read_json_with_retry = function(filepath, max_retries = 1) {
    for (attempt in 1:max_retries) {
        result = tryCatch({
            pivot_wider(enframe(as_vector(jsonlite::read_json(filepath))))
        }, error = function(e) {
            if (attempt < max_retries) {
                message(sprintf("Attempt %d/%d failed for %s: %s. Retrying...",
                                attempt, max_retries, filepath, e$message))
                Sys.sleep(2^(attempt - 1))  # Exponential backoff: 1s, 2s, 4s, 8s, 16s
                NULL
            } else {
                stop(sprintf("Failed after %d attempts for file: %s\nError: %s",
                             max_retries, filepath, e$message))
            }
        })

        if (!is.null(result)) {
            return(result)
        }
    }
}

dto_results_list = future_imap(dto_results_lookup$result_file, ~{
    tryCatch({
        # Check if file is empty
        if (file.size(.x) == 0) {
            return(tibble(
                empirical_pvalue = NA_real_,
                fdr = NA_real_,
                population_size = NA_real_,
                rank1 = NA_real_,
                rank2 = NA_real_,
                set1_len = NA_real_,
                set2_len = NA_real_,
                unpermuted_intersection_size = NA_real_,
                unpermuted_pvalue = NA_real_
            ))
        }
        read_json_with_retry(.x)
    }, error = function(e) {
        stop(sprintf("Index %d failed:\nFile: %s\nError: %s",
                     .y, .x, e$message))
    })
}, .progress = TRUE, .options = furrr_options(seed = TRUE))

dto_results_df = bind_cols(dto_results_lookup, bind_rows(dto_results_list))

hf_results_df = dto_results_df %>%
    mutate(
        binding_id = case_when(
            binding_source == "cc" & str_detect(binding_id, "-")
            ~ paste0("BrentLab/callingcards;annotated_features_combined;",
                     binding_id),
            binding_source == "cc" & str_detect(binding_id, "-", negate=TRUE)
            ~ paste0("BrentLab/callingcards;annotated_features;",
                     binding_id),
            binding_source == "chipexo"
            ~ paste0("BrentLab/rossi_2021;rossi_2021_af_combined;",
                     binding_id),
            binding_source == "harbison"
            ~ paste0("BrentLab/harbison_2004;harbison_2004;",
                     binding_id),
            binding_source == "mahendrawada_chec"
            ~ paste0("BrentLab/mahendrawada_2025;chec_mahendrawada_m2025_af_combined;",
                     binding_id)),
        perturbation_id = case_when(
            pr_source == "hackett" ~ paste0("BrentLab/hackett_2020;hackett_2020;", perturbation_id),
            pr_source == "hughes_oe" ~ paste0("BrentLab/hughes_2006;overexpression;", perturbation_id),
            pr_source == "hughes_ko" ~ paste0("BrentLab/hughes_2006;knockout;", perturbation_id),
            pr_source == "hu_reimand" ~ paste0("BrentLab/hu_2007_reimand_2010;hu_2007_reimand_2010;", perturbation_id),
            pr_source == "kemmeren" ~ paste0("BrentLab/kemmeren_2014;kemmeren_2014;", perturbation_id),
            pr_source == "mahendrawada_rnaseq" ~ paste0("BrentLab/mahendrawada_2025;rnaseq_reprocessed;", perturbation_id),
            .default = "ERROR"),
        binding_repo_dataset = case_when(
            binding_source == "cc" & str_detect(binding_id, "-")
            ~ "callingcards-annotated_features_combined",
            binding_source == "cc" & str_detect(binding_id, "-", 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 = case_when(
            pr_source == "hackett" ~ paste0("hackett_2020-hackett_2020"),
            pr_source == "hughes_oe" ~ paste0("hughes_2006-overexpression"),
            pr_source == "hughes_ko" ~ paste0("hughes_2006-knockout"),
            pr_source == "hu_reimand" ~ paste0("hu_2007_reimand_2010-hu_2007_reimand_2010"),
            pr_source == "kemmeren" ~ paste0("kemmeren_2014-kemmeren_2014"),
            pr_source == "mahendrawada_rnaseq" ~ paste0("mahendrawada_2025-rnaseq_reprocessed"),
            .default = "ERROR")) %>%
    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,
           pr_ranking_column,
           binding_repo_dataset, perturbation_repo_dataset)


# note that the deprecated directory is ignored in yeast_comparative_analysis.
# this exists on my computer and could exist on yours, too, if you wanted to
# put old results in a subdir called deprecated that would be ignored by huggingface/git
# old_db_dto_res = arrow::open_dataset("~/code/hf/yeast_comparative_analysis/deprecated/dto") %>%
#     collect()

# arrow::write_dataset(
#     hf_results_df,
#     path = "/home/chase/code/hf/yeast_comparative_analysis/dto",
#     format = "parquet",
#     partitioning = c("binding_repo_dataset", "perturbation_repo_dataset"),
#     existing_data_behavior = "overwrite",
#     compression = "zstd",
#     write_statistics = TRUE,
#     use_dictionary = c(
#         binding_id = TRUE,
#         perturbation_id = TRUE
#     )
# )