File size: 6,841 Bytes
3336357
 
 
 
 
 
11f503b
 
 
 
 
 
 
 
 
 
 
 
 
 
3336357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11f503b
 
3336357
 
 
 
11f503b
3336357
 
11f503b
 
3336357
 
 
 
11f503b
3336357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11f503b
 
3336357
61bb81f
 
 
 
 
 
 
 
 
11f503b
 
 
 
 
 
 
 
 
 
 
 
3336357
 
11f503b
3336357
 
 
 
61bb81f
11f503b
61bb81f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
library(tidyverse)
library(httr)
library(here)
library(arrow)
library(readxl)

## NOTES:
##
## Hu had a heat shock and tetracycline condition. Reimand says they
## use:
## Further analysis was performed separately for the following
## three cases: A) same-strain replicates (BY4741);
## B) replicates of different strains (BY4741 and S288C);
## c) replicates of the induced promoter strain (R1158+TET)
##
## which i interpreted to mean (given some inspection from the original Hu
## metadata), that it should be !heat_shock and !tetracycline except
## for the regulators which did not have those conditions, in which
## case I kept the tetracyclie condition.

# genomic feature harmonization table ----
# see https://huggingface.co/datasets/BrentLab/yeast_genome_resources
gene_table = arrow::open_dataset(here("data/genome_files/hf/features")) %>%
  as_tibble()


hu_data = list(
  de = here("data/hu/gkq232_SuppTable1C_KOTargetGenes_Matrix_LogFoldChange.dat.xz"),
  pval = here("data/hu/gkq232_SuppTable1B_KOTargetGenes_Matrix_PValue.dat.xz")
)

hu_de_target_genes = as.character(read_delim(hu_data$de,
                             delim=' ',
                             col_names = FALSE,
                             n_max = 1)[1,])
hu_de_target_genes = str_replace(hu_de_target_genes, "\\.", "-")

hu_de_colnames = c('hu_regulator', hu_de_target_genes)

hu_pval_target_genes = as.character(read_delim(hu_data$pval,
                                             delim=' ',
                                             col_names = FALSE,
                                             n_max = 1)[1,])
hu_pval_target_genes = str_replace(hu_pval_target_genes, "\\.", "-")

hu_pval_colnames = c('hu_regulator', hu_pval_target_genes)

stopifnot(setequal(hu_pval_colnames, hu_de_colnames))

# Note: The warning is expected. this is being parsed
# correctly despite the warning
de_df = read.delim(
  hu_data$de,
  sep=' ',
  col.names = hu_de_colnames,
  check.names=FALSE) %>%
  as_tibble()

# Note: The warning is expected. this is being parsed
# correctly despite the warning
pval_df = read.delim(
  hu_data$pval,
  sep=' ',
  col.names = hu_pval_colnames,
  check.names=FALSE) %>%
  as_tibble()

hu_df = de_df %>%
  pivot_longer(-hu_regulator,
               names_to='hu_target',
               values_to='effect') %>%
  left_join(pval_df %>%
              pivot_longer(-hu_regulator,
                           names_to='hu_target',
                           values_to='pval')) %>%
  mutate(hu_regulator = toupper(hu_regulator)) %>%
  # remove deleted orfs
  filter(!hu_target %in% c('YER187W-A', 'YCR103C', 'YCL006C', "YIL080W"))

tf_map = list()

tf_map$init = hu_df %>%
  distinct(hu_regulator) %>%
  mutate(regulator_symbol = hu_regulator) %>%
  # these are labeled with aliases in the hu data
  mutate(regulator_symbol = case_when(
    regulator_symbol == "CAF17" ~ "IBA57",
    regulator_symbol == "RCS1" ~ "AFT1",
    regulator_symbol == "RLR1" ~ "THO2",
    regulator_symbol == "ZMS1" ~ "RSF2",
    regulator_symbol == "RIS1" ~ "ULS1",
    .default = regulator_symbol
  )) %>%
  left_join(gene_table %>%
              select(symbol,locus_tag) %>%
              dplyr::rename(regulator_locus_tag = locus_tag,
                            regulator_symbol = symbol)) %>%
  mutate(regulator_symbol = ifelse(is.na(regulator_locus_tag),
                                   NA,
                                   regulator_symbol))

tf_map$locus_tags = tf_map$init %>%
  filter(!complete.cases(.)) %>%
  select(hu_regulator) %>%
  mutate(regulator_locus_tag = hu_regulator) %>%
  left_join(gene_table %>%
              select(symbol,locus_tag) %>%
              dplyr::rename(regulator_locus_tag = locus_tag,
                            regulator_symbol = symbol))

tf_map_df = bind_rows(
  tf_map$init %>% filter(complete.cases(.)),
  tf_map$locus_tags
)

stopifnot(setequal(tf_map$init$hu_regulator, tf_map_df$hu_regulator))

target_map = list()

target_map$init = hu_df %>%
  distinct(hu_target) %>%
  mutate(target_locus_tag = hu_target) %>%
  left_join(select(gene_table, locus_tag, symbol)
            %>% dplyr::rename(target_locus_tag = locus_tag,
                              target_symbol = symbol))

target_map$alias_df = gene_table %>%
  mutate(hu_target = map_chr(alias, ~ {
    alias_i <- .x
    match <- keep(target_map$init %>%
                    filter(!complete.cases(.)) %>%
                    pull(hu_target),
                  ~ str_detect(alias_i, .x))
    if (length(match) > 0) match[[1]] else NA_character_
  })) %>%
  filter(!is.na(hu_target)) %>%
  select(hu_target, locus_tag, symbol) %>%
  dplyr::rename(target_locus_tag = locus_tag,
                target_symbol = symbol)

target_map_df = bind_rows(target_map$init %>% filter(complete.cases(.)),
                          target_map$alias_df)

stopifnot(setequal(target_map_df$hu_target,
                   unique(hu_df$hu_target)))

final_df = hu_df %>%
  left_join(tf_map_df) %>%
  left_join(target_map_df)

# never used -- don't bother
# hu_reimand_from_db = read_csv("data/hu/hu_2007_reimand_db_20251127.csv")

# from https://www.nature.com/articles/ng2012#Sec10
supplement_table_1 = readxl::read_excel("data/hu/41588_2007_BFng2012_MOESM20_ESM.xlsx") %>%
  janitor::clean_names() %>%
  dplyr::rename(regulator_locus_tag = `tf_systematic_name`) %>%
  select(-tf_gene_name) %>%
  mutate(heat_shock = str_detect(growth_condition, "heat-shock"),
         tetracycline_treatment = str_detect(growth_condition, "tetracycline treatment")) %>%
  select(-growth_condition)

supplement_table_1_filt = supplement_table_1 %>%
  filter(!heat_shock, !tetracycline_treatment)

supplement_table_1_metadata = supplement_table_1_filt %>%
  bind_rows(
    supplement_table_1 %>%
      filter(
        !heat_shock,
        regulator_locus_tag %in%
               setdiff(final_df$regulator_locus_tag,
                       supplement_table_1_filt$regulator_locus_tag)))

final_df_for_parquet = final_df %>%
  mutate(sample_id = cur_group_id()) %>%
  select(sample_id,
         regulator_locus_tag, regulator_symbol,
         target_locus_tag, target_symbol,
         effect, pval) %>%
  ungroup() %>%
  arrange(regulator_locus_tag) %>%
  left_join(supplement_table_1_metadata)


final_df_for_parquet %>%
  write_parquet(here("~/code/hf/hu_2007_reimand_2010/hu_2007_reimand_2010.parquet"),
                compression = "zstd",
                chunk_size = 6249,
                write_statistics = TRUE,
                use_dictionary = c(
                  sample_id = TRUE,
                  regulator_locus_tag = TRUE,
                  regulator_symbol = TRUE,
                  target_locus_tag = TRUE,
                  target_symbol = TRUE,
                  heat_shock = TRUE,
                  tetracycline_treatment = TRUE
                ))