MIP / src /02.1_assemble_datasets.R
maom's picture
add curaction scripts
2071122
#' Assemble a Rosetta models dataset
#'
#' @param data_path character directory .pdb.gz files are located
#' @param output_path character output .parquet path
#'
#' Write output_path .parquet file with columns
#' <id> <pdb> [<scores>]
#' where [<scores>] are key-value entries following
#' the TER line in each .pdb.gz file
assemble_rosetta_models <- function(
data_path,
output_path) {
cat(
"data path: ", data_path, "\n",
"output path: ", output_path, "\n",
sep = "")
file_index <- 1
models <- list.files(
path = data_path,
full.names = TRUE,
pattern = "*.pdb.gz",
recursive = TRUE) |>
purrr::map_dfr(.f = function(path) {
file_handle <- path |>
file(open = "rb") |>
gzcon()
if( file_index %% 1000 == 0) {
cat("Reading '", path, "' ", file_index, "\n", sep = "")
}
file_index <<- file_index + 1
lines <- file_handle |> readLines()
file_handle |> close()
ter_line_index <- which(
lines |> stringr::str_detect("^TER"),
arr.ind = TRUE)
lines[(ter_line_index + 1) : (length(lines) - 1)] |>
paste0(collapse = "\n") |>
readr::read_delim(
delim = " ",
col_names = c("key", "value"),
show_col_types = FALSE) |>
dplyr::mutate(
id = path |>
basename() |>
stringr::str_replace_all(".pdb.gz", ""),
.before = 1) |>
dplyr::mutate(
pdb = lines[1:ter_line_index] |> paste0(collapse = "\n"))
})
models <- arrow::arrow_table(models)
models$pdb <- models$pdb$cast(arrow::string())
models |> arrow::write_parquet(output_path)
}
# call assemble_rosetta_models for the high_quality and low_quality datasets
dataset_tag <- "rosetta_high_quality_models"
assemble_rosetta_models(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
dataset_tag <- "rosetta_low_quality_models"
assemble_rosetta_models(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
#' Assemble a DMP-Fold models dataset
#'
#' @param data_path character directory .pdb.gz files are located
#' @param output_path character output .parquet path
#'
#' Write output_path .parquet file with columns
#' <id> <pdb>
#'
#' Note that dmpfold doesn't write out score lines like Rosetta
assemble_dmpfold_models <- function(
data_path,
output_path) {
cat(
"data path: ", data_path, "\n",
"output path: ", output_path, "\n",
sep = "")
file_index <- 1
models <- list.files(
path = data_path,
full.names = TRUE,
pattern = "*.pdb.gz",
recursive = TRUE) |>
purrr::map_dfr(.f = function(path) {
file_handle <- path |>
file(open = "rb") |>
gzcon()
if (file_index %% 1000 == 0) {
cat("Reading '", path, "' ", file_index, "\n", sep = "")
}
file_index <<- file_index + 1
lines <- file_handle |> readLines()
file_handle |> close()
ter_line_index <- which(
lines |> stringr::str_detect("^TER"),
arr.ind = TRUE)
data.frame(
id = path |> basename() |> stringr::str_replace_all(".pdb.gz", ""),
pdb = lines[1:ter_line_index] |> paste0(collapse = "\n"))
})
models |>
arrow::write_parquet(output_path)
}
# call assemble_rosetta_models for the high_quality and low_quality datasets
dataset_tag <- "dmpfold_high_quality_models"
assemble_dmpfold_models(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
dataset_tag <- "dmpfold_low_quality_models"
assemble_dmpfold_models(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
####################################
## ##
## Assemble Function Predictions ##
## ##
####################################
#' Assemble DeepFRI Function Prediction dataset
#'
#' @param data_path character directory where *_pred_scores.json.gz files are located
#' @param output_path character output .parquet path
#'
#' Write output_path .parquet file with columns
#' <id> <term_id> <term_name> <Y_hat>
#'
#' <id>: Structure identifier like `MIP_00004873`
#' <term_ontology>: term onlogy, one of [BP, CC, EC, or MF]
#' <term_id>: GO or EC term identifiers like `GO:0009225`
#' <term_name> is the description of the term
assemble_DeepFRI_function_predictions <- function(
data_path,
output_path) {
cat(
"data path: ", data_path, "\n",
"output path: ", output_path, "\n",
sep = "")
file_index <- 1
scores <- c("BP", "CC", "EC", "MF") |>
purrr::map_dfr(.f = function(ontology) {
cat("Reading predictions cores for ontology ", ontology, "\n", sep = "")
list.files(
path = data_path,
full.names = TRUE,
pattern = paste0("*_", ontology, "_pred_scores.json.gz"),
recursive = TRUE) |>
purrr::map_dfr(.f = function(path) {
cat("Reading '", path, "' ", file_index, "\n", sep = "")
file_index <<- file_index + 1
data <- jsonlite::fromJSON(txt = path)
scores <- as.data.frame(data$Y_hat)
names(scores) <- data$goterms
scores <- scores |>
dplyr::mutate(
id = data$pdb_chains,
.before = 1) |>
tidyr::pivot_longer(
cols = -"id",
names_to = "term_id",
values_to = "Y_hat") |>
dplyr::left_join(
data.frame(
term_ontology = ontology,
term_id = data$goterms,
term_name = data$gonames),
by = "term_id") |>
dplyr::select(
id,
term_ontology,
term_id,
term_name,
Y_hat)
})
})
scores |>
arrow::write_parquet(output_path)
}
# call assemble_rosetta_models for all the datasets
dataset_tag <- "rosetta_high_quality_function_predictions"
assemble_DeepFRI_function_predictions(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
dataset_tag <- "rosetta_low_quality_function_predictions"
assemble_DeepFRI_function_predictions(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
dataset_tag <- "dmpfold_high_quality_function_predictions"
assemble_DeepFRI_function_predictions(
data_path = paste0(
"data/mxoicrobiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))
dataset_tag <- "dmpfold_low_quality_function_predictions"
assemble_DeepFRI_function_predictions(
data_path = paste0(
"data/microbiome_immunity_project_dataset/dataset/",
dataset_tag),
output_path = paste0("intermediate/", dataset_tag, ".parquet"))