File size: 4,303 Bytes
25daa63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff386e
25daa63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ff386e
 
25daa63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
library(tidyverse)
library(DESeq2)
library(arrow)
library(here)


run_degron_deseq2 <- function(regulator_sym, env_cond, timepoint_val,
                              degron_data = degron,
                              baseline_data = baseline_control) {

    # Filter degron data for the specified regulator, condition, and timepoint
    degron_meta_filtered <- degron_data$meta %>%
        filter(
            regulator_symbol == regulator_sym,
            env_condition == env_cond,
            timepoint == timepoint_val) %>%
        mutate(degron_treatment = factor(degron_treatment,
                                         levels = c("DMSO", "IAA")))



    if (nrow(degron_meta_filtered) != 6) {
        stop(sprintf("We expect exactly 3 replicates in each conditition for a total of 6 samples. These parameters result in %s", nrow(degron_meta_filtered)))
    }

    # Get the IAA and DMSO samples
    iaa_samples <- degron_meta_filtered %>%
        filter(degron_treatment == "IAA") %>%
        pull(sra_accession)

    dmso_samples <- degron_meta_filtered %>%
        filter(degron_treatment == "DMSO") %>%
        pull(sra_accession)

    if (length(iaa_samples) != 3 || length(dmso_samples) != 3) {
        stop("Missing IAA or DMSO samples for the specified parameters")
    }

    # Get counts for these samples
    counts_filtered <- degron_data$counts_long %>%
        filter(sra_accession %in% c(iaa_samples, dmso_samples))

    # Convert to wide format (genes x samples)
    counts_wide <- counts_filtered %>%
        select(target_locus_tag, sra_accession, count) %>%
        pivot_wider(
            names_from = sra_accession,
            values_from = count) %>%
        column_to_rownames("target_locus_tag")

    # Create sample metadata
    sample_meta <- degron_meta_filtered %>%
        select(sra_accession, degron_treatment) %>%
        column_to_rownames("sra_accession")

    # Ensure counts columns match metadata rows
    counts_wide <- counts_wide[, rownames(sample_meta)]

    # Create DESeq2 dataset
    dds <- DESeqDataSetFromMatrix(
        countData = counts_wide,
        colData = sample_meta,
        design = ~ degron_treatment
    )

    # Run DESeq2
    dds <- DESeq(dds)

    # Get results
    res <- results(dds)

    # Return both the DESeq object and results
    return(list(
        dds = dds,
        results = res %>%
            as_tibble(rownames="target_locus_tag") %>%
            mutate(sample_id = paste(unique(degron_meta_filtered$sample_id), collapse="_")) %>%
            left_join(distinct(degron$counts_long %>% select(target_locus_tag, target_symbol))) %>%
            mutate(regulator_locus_tag = unique(degron_meta_filtered$regulator_locus_tag),
                   regulator_symbol = unique(degron_meta_filtered$regulator_symbol),
                   env_condition = unique(degron_meta_filtered$env_condition),
                   timepoint = unique(degron_meta_filtered$timepoint)) %>%
            dplyr::relocate(sample_id, regulator_locus_tag, regulator_symbol,
                            env_condition, timepoint),
        metadata = degron_meta_filtered
    ))
}


baseline_control = list(
    counts_long = arrow::read_parquet(
        "~/code/hf/mahendrawada_2025/wt_baseline_counts.parquet"),
    meta = arrow::read_parquet(
        "~/code/hf/mahendrawada_2025/wt_baseline_counts_meta.parquet")
)

degron = list(
    counts_long = arrow::read_parquet("~/code/hf/mahendrawada_2025/degron_counts.parquet"),
    meta = arrow::read_parquet("~/code/hf/mahendrawada_2025/degron_counts_meta.parquet")
)

z = run_degron_deseq2("CYC8", 'standard_30C', 30, degron, baseline_control)

x = map(unique(degron$meta$regulator_symbol),
        ~run_degron_deseq2(., 'standard_30C', 30, degron, baseline_control))

results_df <- map_dfr(x, ~.$results)

# results_df %>%
#   write_parquet("~/code/hf/mahendrawada_2025/rnaseq_reprocessed.parquet",
#                 compression = "zstd",
#                 write_statistics = TRUE,
#                 chunk_size = 6708,
#                 use_dictionary = c(
#                   sample_id = TRUE,
#                   regulator_locus_tag = TRUE,
#                   regulator_symbol = TRUE,
#                   target_locus_tag = TRUE,
#                   target_symbol = TRUE
#                 )
#   )