File size: 9,914 Bytes
6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 51ae7ad 6bc931b 08f5bf1 b11a8f7 08f5bf1 51ae7ad 6bc931b 08f5bf1 6bc931b 51ae7ad 6bc931b 08f5bf1 6bc931b b11a8f7 6bc931b 51ae7ad 08f5bf1 51ae7ad 08f5bf1 51ae7ad b11a8f7 |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
library(tidyverse)
library(here)
library(arrow)
library(readxl)
# Constants ----
genomicfeatures_PATH <- here("data/genome_files/hf/features")
HUGHES_DATA_DIR <- here("data/hughes_2006")
# Load genomic features harmonization table ----
# See https://huggingface.co/datasets/BrentLab/yeast_genome_resources
genomicfeatures <- arrow::open_dataset(genomicfeatures_PATH) %>%
as_tibble()
#' Read and process Hughes normalized data
#'
#' @param path Path to Excel file
#' @param ko Logical indicating if this is knockout data (TRUE) or overexpression (FALSE)
#' @return Tibble with processed data
read_hughes_normalized_data <- function(path, ko = TRUE) {
if (!file.exists(path)) {
stop("File not found: ", path)
}
df <- read_excel(path)
# Standardize first column name
colnames(df)[1] <- "hughes_target"
# Pivot to long format
df_long <- df %>%
pivot_longer(
cols = -hughes_target,
names_to = "sample",
values_to = "norm_log2fc"
) %>%
mutate(norm_log2fc = as.numeric(norm_log2fc)) %>%
# remove deleted ORFs
filter(!hughes_target %in% c(
"YCL006C",
"YCR103C",
"YER187W-A"
)) %>%
# these are transposons/retrotransposons and just have
# typos
mutate(hughes_target = case_when(
hughes_target == "YDR034CD01" ~ "YDR034C-D",
hughes_target == "YDR210WD01" ~ "YDR210W-D",
hughes_target == "YDR261CD01" ~ "YDR261C-D",
hughes_target == "YGR161CD01" ~ "YGR161C-D",
hughes_target == "YPR158CD01" ~ "YPR158C-D",
.default = hughes_target
))
# Process based on data type
if (ko) {
df_long = df_long %>%
separate(sample, into = c("prefix", "suffix"), sep = "/", remove = FALSE) %>%
mutate(
dye_orientation = str_extract(suffix, "[+-]$"),
regulator_symbol = str_to_upper(str_remove(suffix, "[+-]"))
) %>%
select(-prefix, -suffix)
} else {
df_long = df_long %>%
mutate(
dye_orientation = str_extract(sample, "[+-]$"),
regulator_symbol = str_to_upper(str_remove(sample, "(-A)?OE[+-]"))
)
}
df_long %>%
select(-sample) %>%
pivot_wider(id_cols = c(regulator_symbol, hughes_target),
names_from = dye_orientation,
values_from = norm_log2fc) %>%
rename(dye_plus = `+`, dye_minus = `-`) %>%
rowwise() %>%
mutate(
mean_norm_log2fc = case_when(
# Both values are NA
is.na(dye_plus) & is.na(dye_minus) ~ NA_real_,
# Only one value is available - use that value
is.na(dye_plus) & !is.na(dye_minus) ~ dye_minus,
!is.na(dye_plus) & is.na(dye_minus) ~ dye_plus,
# Both values available but have opposite signs - average to zero
!is.na(dye_plus) & !is.na(dye_minus) & (sign(dye_plus) != sign(dye_minus)) ~ 0,
# Both values available with same sign - take the mean
TRUE ~ mean(c(dye_plus, dye_minus), na.rm = TRUE)
)
) %>%
ungroup()
}
# Load and process data ----
message("Loading knockout data...")
df_ko <- read_hughes_normalized_data(
file.path(HUGHES_DATA_DIR, "Del_NormalizedRatios.xls"),
ko = TRUE
)
message("Loading overexpression data...")
df_oe <- read_hughes_normalized_data(
file.path(HUGHES_DATA_DIR, "OE_NormalizedRatios.xls"),
ko = FALSE
)
message("Loading Z-scores...")
zscore_df <- read_excel(file.path(HUGHES_DATA_DIR, "Z_SCORES_FOR_106_EXPERIMENTS.xls")) %>%
rename(hughes_target = `...1`) %>%
pivot_longer(
cols = -hughes_target,
names_to = "sample",
values_to = "zscore"
) %>%
mutate(
perturbation = case_when(
str_detect(sample, "-D$") ~ "deletion",
str_detect(sample, "^OE") ~ "overexpression",
TRUE ~ "unknown"
),
hughes_regulator = str_to_upper(str_remove(sample, "-D$|^OE"))
)
# Load metadata ----
message("Loading metadata...")
hughes_2006_meta <- read_excel(file.path(HUGHES_DATA_DIR, "TRANSCRIPTION_FACTOR_LIST.xls")) %>%
rename(
regulator_locus_tag = Id_001,
regulator_symbol = Id_002
) %>%
mutate(
essential = `Essential/Nonessential` == "essential",
oe_passed_qc = `OE passed QC` == "yes",
del_passed_qc = `DEL passed QC` == "yes"
) %>%
select(-`Essential/Nonessential`, -ends_with("passed QC"))
# Data validation ----
message("Validating data consistency...")
validate_data_consistency <- function() {
tests <- list(
# Check regulator locus tags match genomic features
regulator_locus_consistency = setequal(
intersect(genomicfeatures$locus_tag, hughes_2006_meta$regulator_locus_tag),
hughes_2006_meta$regulator_locus_tag
),
# Check regulator symbols match genomic features
regulator_symbol_consistency = setequal(
intersect(genomicfeatures$symbol, hughes_2006_meta$regulator_symbol),
hughes_2006_meta$regulator_symbol
),
# Check OE regulators match metadata
oe_regulator_consistency = setequal(
intersect(hughes_2006_meta$regulator_symbol, unique(df_oe$regulator_symbol)),
unique(df_oe$regulator_symbol)
),
# Check KO regulators match metadata
ko_regulator_consistency = setequal(
intersect(hughes_2006_meta$regulator_symbol, unique(df_ko$regulator_symbol)),
unique(df_ko$regulator_symbol)
),
# Check target consistency between KO and OE
target_consistency = setequal(
unique(df_ko$hughes_target),
unique(df_oe$hughes_target)
),
# Check targets match genomic features
target_genomic_consistency = setequal(
unique(df_oe$hughes_target),
intersect(unique(df_oe$hughes_target), genomicfeatures$locus_tag)
)
)
# Report results
failed_tests <- names(tests)[!unlist(tests)]
if (length(failed_tests) == 0) {
message("✓ All validation tests passed")
} else {
stop("✗ Validation failed for: ", paste(failed_tests, collapse = ", "))
}
invisible(tests)
}
validate_data_consistency()
# Summary statistics ----
message("Data loading complete. Summary:")
message("- Knockout experiments: ", length(unique(df_ko$regulator_symbol)), " regulators")
message("- Overexpression experiments: ", length(unique(df_oe$regulator_symbol)), " regulators")
message("- Target genes: ", length(unique(df_ko$hughes_target)))
message("- Z-score experiments: ", length(unique(zscore_df$sample)))
# Note about missing targets in Z-score data
missing_targets_count <- length(setdiff(unique(df_ko$hughes_target),
unique(zscore_df$hughes_target)))
if (missing_targets_count > 0) {
message("- Z-score data missing ", missing_targets_count, " targets present in KO/OE data")
}
missing_locus_tags <- setdiff(
unique(df_oe$hughes_target),
intersect(unique(df_oe$hughes_target), genomicfeatures$locus_tag)
)
genome_map = tibble(
locus_tag = intersect(unique(df_oe$hughes_target),
genomicfeatures$locus_tag)) %>%
left_join(genomicfeatures %>% select(locus_tag, symbol)) %>%
mutate(hughes_target = locus_tag) %>%
bind_rows(
genomicfeatures %>%
filter(str_detect(alias, paste(missing_locus_tags, collapse = "|"))) %>%
mutate(alias_match = str_extract(alias, paste(missing_locus_tags, collapse = "|"))) %>%
dplyr::rename(hughes_target = alias_match) %>%
select(hughes_target, locus_tag, symbol)
) %>%
dplyr::rename(target_locus_tag = locus_tag, target_symbol = symbol)
stopifnot(setequal(genome_map$hughes_target, unique(df_oe$hughes_target)))
hughes_2006_meta_with_id = hughes_2006_meta %>%
arrange(regulator_locus_tag) %>%
group_by(regulator_locus_tag) %>%
mutate(sample_id = cur_group_id()) %>%
ungroup() %>%
select(sample_id, all_of(colnames(hughes_2006_meta))) %>%
arrange(sample_id)
stopifnot(
hughes_2006_meta_with_id %>% group_by(sample_id) %>% filter(n()>1) %>% nrow() == 0
)
df_oe_harmonized = df_oe %>%
left_join(
select(hughes_2006_meta_with_id,
sample_id,
regulator_locus_tag,
regulator_symbol)) %>%
left_join(genome_map) %>%
select(sample_id,
regulator_locus_tag, regulator_symbol,
target_locus_tag, target_symbol,
dye_plus, dye_minus, mean_norm_log2fc) %>%
arrange(sample_id, target_locus_tag)
df_oe_harmonized %>%
write_parquet("~/code/hf/hughes_2006/overexpression.parquet",
compression = "zstd",
write_statistics = TRUE,
use_dictionary = c(
sample_id = TRUE,
regulator_locus_tag = TRUE,
regulator_symbol = TRUE,
target_locus_tag = TRUE,
target_symbol = TRUE
)
)
df_ko_harmonized = df_ko %>%
left_join(
select(hughes_2006_meta_with_id,
sample_id,
regulator_locus_tag,
regulator_symbol)) %>%
left_join(genome_map) %>%
select(sample_id,
regulator_locus_tag, regulator_symbol,
target_locus_tag, target_symbol,
dye_plus, dye_minus, mean_norm_log2fc) %>%
arrange(sample_id, target_locus_tag)
df_ko_harmonized %>%
write_parquet("~/code/hf/hughes_2006/knockout.parquet",
compression = "zstd",
write_statistics = TRUE,
use_dictionary = c(
sample_id = TRUE,
regulator_locus_tag = TRUE,
regulator_symbol = TRUE,
target_locus_tag = TRUE,
target_symbol = TRUE
)
)
hughes_2006_meta_with_id %>%
janitor::clean_names() %>%
write_parquet("~/code/hf/hughes_2006/metadata.parquet",
compression = "zstd",
write_statistics = TRUE,
use_dictionary = c(
sample_id = TRUE,
regulator_locus_tag = TRUE,
regulator_symbol = TRUE,
oe_passed_qc = TRUE,
del_passed_qc = TRUE
)
)
|