hughes_2006 / scripts /parse_hughes_2006.R
cmatkhan's picture
Upload folder using huggingface_hub
51ae7ad verified
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)))
df_oe_harmonized = df_oe %>%
left_join(
select(hughes_2006_meta,
regulator_locus_tag,
regulator_symbol)) %>%
left_join(genome_map) %>%
select(regulator_locus_tag, regulator_symbol,
target_locus_tag, target_symbol,
dye_plus, dye_minus, mean_norm_log2fc)
df_oe_harmonized %>%
write_parquet("~/code/hf/hughes_2006/overexpression.parquet",
compression = "zstd",
write_statistics = TRUE,
use_dictionary = c(
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,
regulator_locus_tag,
regulator_symbol)) %>%
left_join(genome_map) %>%
select(regulator_locus_tag, regulator_symbol,
target_locus_tag, target_symbol,
dye_plus, dye_minus, mean_norm_log2fc)
df_ko_harmonized %>%
write_parquet("~/code/hf/hughes_2006/knockout.parquet",
compression = "zstd",
write_statistics = TRUE,
use_dictionary = c(
regulator_locus_tag = TRUE,
regulator_symbol = TRUE,
target_locus_tag = TRUE,
target_symbol = TRUE
)
)
hughes_2006_meta %>%
janitor::clean_names() %>%
write_parquet("~/code/hf/hughes_2006/metadata.parquet",
compression = "zstd",
write_statistics = TRUE,
use_dictionary = c(
regulator_locus_tag = TRUE,
regulator_symbol = TRUE,
oe_passed_qc = TRUE,
del_passed_qc = TRUE
)
)