File size: 4,427 Bytes
c329a72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
library(httr)
library(jsonlite)
library(tidyverse)

## TODO: this uses the rossi metadata that already existed. That will eventually
## be removed. This needs to be created from the yeastepigenome pull below,
## and the data from the getGEO directly

# Fetch all samples from the API
response <- GET("https://odin.cac.cornell.edu/yep_api/reviewSamples")

# Parse the JSON response
samples_data <- content(response, "text", encoding = "UTF-8") %>%
    fromJSON(flatten = TRUE)

# Convert to tibble
# The data comes as a named list with numeric indices as names
yeastepigenome_sample_df <- samples_data %>%
    map_df(~as_tibble(.), .id = "index") %>%
    select(sampleId, assayType, treatments, growthMedia, antibody) %>%
    dplyr::rename(yeastepigenome_id = sampleId,
                  assay_type = assayType,
                  treatment = treatments,
                  growth_media = growthMedia)

rossi_meta = arrow::read_parquet("~/code/hf/rossi_2021/deprecated_rossi_2021_metadata.parquet")

rossi_meta_with_addtl = rossi_meta %>%
    left_join(yeastepigenome_sample_df) %>%
    filter(!run_accession %in% c('SRR11466887', 'SRR11466891')) %>%
    bind_rows(
        tibble(
            regulator_locus_tag = c("YNL076W", "YGL244W"),
            regulator_symbol = c("MKS1", "RTF1"),
            run_accession = c("SRR11466887", "SRR11466891"),
            yeastepigenome_id = c(14846, 12031),
            assay_type = "ChIP-exo",
            treatment = "Normal",
            growth_media = "YPD",
            antibody = c("HA-tag: Santa Cruz sc-7392", "TAP-tag: Sigma i5006"))) %>%
    arrange(regulator_locus_tag) %>%
    select(-assay_type) %>%
    group_by(regulator_locus_tag, treatment, growth_media) %>%
    mutate(sample_id = cur_group_id()) %>%
    ungroup()

# arrow::write_parquet(
#     rossi_meta_with_addtl,
#     "~/code/hf/rossi_2021/rossi_2021_metadata.parquet",
#     compression = "zstd",
#     write_statistics = TRUE,
#     use_dictionary = c(
#         sample_id = TRUE,
#         regulator_locus_tag=TRUE,
#         regulator_symbol = TRUE,
#         treatment = TRUE,
#         growth_media = TRUE))


# NOTE: the following works, but is currently unused
#
# library(GEOquery)
# library(tidyverse)
#
# # Get the GEO series data
# gse <- getGEO("GSE147927", GSEMatrix = FALSE)
# sample_list <- GSMList(gse)
#
# # Helper function for NULL coalescing
# `%||%` <- function(x, y) if (is.null(x)) y else x
#
# # Extract sample metadata
# extract_sample_metadata_robust <- function(gsm) {
#     meta <- Meta(gsm)
#
#     # Start with basic info
#     result <- tibble(
#         gsm_id = meta$geo_accession %||% NA,
#         title = meta$title %||% NA,
#         source_name = meta$source_name_ch1 %||% NA,
#         organism = meta$organism_ch1 %||% NA
#     )
#
#     # Extract characteristics from the 'characteristics_ch1' field
#     if (!is.null(meta$characteristics_ch1)) {
#         for (char in meta$characteristics_ch1) {
#             # Split on first colon
#             parts <- str_split(char, ":\\s*", n = 2)[[1]]
#             if (length(parts) == 2) {
#                 char_name <- parts[1]
#                 char_value <- parts[2]
#                 result[[char_name]] <- char_value
#             }
#         }
#     }
#
#     # Add protocols
#     result$treatment_protocol <- paste(meta$treatment_protocol_ch1, collapse = " ") %||% NA
#     result$growth_protocol <- paste(meta$growth_protocol_ch1, collapse = " ") %||% NA
#     result$extract_protocol <- paste(meta$extract_protocol_ch1, collapse = " ") %||% NA
#
#     # Add library info
#     result$library_strategy <- meta$library_strategy %||% NA
#     result$library_source <- meta$library_source %||% NA
#     result$library_selection <- meta$library_selection %||% NA
#     result$instrument_model <- meta$instrument_model %||% NA
#
#     # Add data processing
#     result$data_processing <- paste(meta$data_processing, collapse = " | ") %||% NA
#
#     # Extract SRA accession
#     relations <- meta$relation
#     sra_relation <- relations[grepl("SRA", relations)]
#     if (length(sra_relation) > 0) {
#         result$sra_accession <- str_extract(sra_relation, "SR[XR]\\d+")
#     } else {
#         result$sra_accession <- NA
#     }
#
#     return(result)
# }
#
# # Apply to all samples
# all_samples_metadata <- map_df(sample_list, extract_sample_metadata_robust)
#
# # View results
# glimpse(all_samples_metadata)