File size: 7,446 Bytes
d0ca76f
 
 
 
 
 
e85efd8
d0ca76f
e85efd8
 
 
 
d0ca76f
e85efd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0ca76f
e85efd8
 
 
d0ca76f
e85efd8
 
 
 
 
 
d0ca76f
e85efd8
 
d0ca76f
e85efd8
 
 
d0ca76f
e85efd8
 
 
 
 
 
d0ca76f
 
e85efd8
 
 
d0ca76f
 
 
 
e85efd8
 
d0ca76f
e85efd8
 
 
 
d0ca76f
 
e85efd8
 
 
 
d0ca76f
e85efd8
d0ca76f
e85efd8
d0ca76f
 
e85efd8
 
 
 
 
d0ca76f
e85efd8
d0ca76f
 
e85efd8
d0ca76f
e85efd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0ca76f
e85efd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0ca76f
e85efd8
 
 
 
 
 
 
db330d7
e85efd8
 
 
 
 
 
 
 
 
d0ca76f
db330d7
 
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
## NOTE: The data is currently on /lts/mblab/downloaded_data/barkai_checseq
## and the parquet dataset is on the brentlab-strides aws at s3://yeast-binding-perturbation-data/barkai_checkseq

library(tidyverse)
library(here)
library(arrow)
library(GEOquery)

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

# sacCer3_genome = rtracklayer::import("~/ref/sacCer3/ucsc/sacCer3.fa.gz", format="fasta")
#
# sacCer3_seqnames = unlist(map(str_split(names(sacCer3_genome), " "), ~.[[1]]))
#
# sacCer3_genome_df = tibble(
#   seqnames = rep(sacCer3_seqnames, Biostrings::width(sacCer3_genome))
# ) %>%
#   group_by(seqnames) %>%
#   mutate(start = row_number()-1,
#          end = row_number()) %>%
#   ungroup()
#
# retrieve_series_paths = function(series_id){
#   sra_meta_path = file.path("data/barkai_checseq", series_id, "SraRunTable.csv")
#   stopifnot(file.exists(sra_meta_path))
#   df = read_csv(sra_meta_path)
#
#   data_files = list.files(here("data/barkai_checseq", series_id), "*.txt.gz", full.names = TRUE)
#
#   stopifnot(nrow(df) == length(data_files))
#
#   names(data_files) = str_extract(basename(data_files), "GSM\\d+")
#
#   list(
#     meta = sra_meta_path,
#     files = data_files
#   )
# }
#
#
# add_genomic_coordinate = function(checseqpath){
#
#   bind_cols(sacCer3_genome_df,
#             data.table::fread(checseqpath, sep = "\t", col.names='pileup'))
#
# }
#
# process_checseq_files = function(file){
#
#   add_genomic_coordinate(file) %>%
#     filter(pileup != 0)
# }
#
# series_list = map(set_names(c("GSE179430", "GSE209631", "GSE222268")), retrieve_series_paths)
#
# dataset_basepath = here("data/barkai_checseq/hf/genome_map")
#
# # Create output directory
# dir.create(dataset_basepath, recursive = TRUE, showWarnings = FALSE)
#
# for (series_id in names(series_list)) {
#
#   message(glue::glue("Processing series {series_id}"))
#
#   for (accession_id in names(series_list[[series_id]]$files)) {
#
#     message(glue::glue("  Processing {accession_id}"))
#
#     df <- process_checseq_files(
#       series_list[[series_id]]$files[[accession_id]]
#     ) %>%
#       mutate(accession = accession_id, series = series_id)
#
#     df %>%
#       group_by(seqnames) %>%
#     write_dataset(
#       path = dataset_basepath,
#       format = "parquet",
#       partitioning = c("series", "accession"),
#       existing_data_behavior = "overwrite",
#       compression = "zstd",
#       write_statistics = TRUE,
#       use_dictionary = c(
#         seqnames = TRUE
#       )
#     )
#
#     gc()
#   }
# }

# the following code was used to parse an entire series to DF and then save
# to a parquet dataset. that was too large and I chose the dataset partitioning
# instead.

split_manipulation <- function(manipulation_str) {
  parts <- str_split(manipulation_str, "::")[[1]]

  if (length(parts) != 2) {
    stop("Unexpected format. Expected 'LOCUS::TAGGED_CONSTRUCT'")
  }

  tagged_locus <- parts[1]
  rhs <- parts[2]

  # default
  dbd_donor_symbol_str <- "none"
  ortholog <- "none"

  # Check for paralog DBD
  if (str_detect(rhs, "-[A-Za-z0-9]+DBD-Mnase$")) {
    dbd_donor_symbol_str <- toupper(str_remove(str_split(rhs, "-", simplify = TRUE)[[2]], "DBD"))
  } else if (str_detect(rhs, "^K\\.lactis .*?-Mnase$")) {
    ortholog <- rhs
  }

  list(
    mnase_tagged_symbol = tagged_locus,
    dbd_donor_symbol = dbd_donor_symbol_str,
    ortholog_donor = ortholog
  )
}


split_deletion <- function(deletion_str) {
  parts <- str_split(deletion_str, "::", simplify = TRUE)

  list(
    paralog_deletion_symbol = parts[1],
    paralog_resistance_cassette = if (ncol(parts) >= 2) parts[2] else "none"
  )
}

split_construct_to_tibble = function(split_list){
  background = list(background=split_list[[1]])
  manipulation_list = split_manipulation(split_list[[2]])
  deletion_list = split_deletion(tryCatch(split_list[[3]], error = function(e) "none"))

  bind_cols(map(list(background, manipulation_list, deletion_list), as_tibble))

}


split_constructs <- function(s) {
  s <- str_trim(s)
  if (s == "" || is.na(s)) return(character(0))
  # split on spaces ONLY when the next token starts a new locus "XYZ::"
  split_geno = str_split(s, "\\s+(?=[A-Za-z0-9_.()\\-]+::)")[[1]]

  bind_cols(tibble(genotype = s), split_construct_to_tibble(split_geno))


}

gse178430_meta = read_csv("data/barkai_checseq/GSE179430/SraRunTable.csv") %>%
    mutate(genotype = str_replace(genotype, "Yap2", "Cad1")) %>%
    mutate(genotype = str_replace(genotype, "Yap4", "Cin5"))

gse178430_parsed_meta = bind_cols(
  select(gse178430_meta, `Sample Name`, strainid, Instrument) %>%
    dplyr::rename(accession = `Sample Name`,
                  instrument = Instrument),
  bind_rows(map(gse178430_meta$genotype, split_constructs))) %>%
  left_join(select(genomicfeatures, locus_tag, symbol) %>%
              dplyr::rename(mnase_tagged_symbol = symbol)) %>%
  dplyr::rename(regulator_locus_tag = locus_tag,
                regulator_symbol = mnase_tagged_symbol) %>%
  select(accession, regulator_locus_tag, regulator_symbol, strainid,
         instrument, genotype, dbd_donor_symbol, ortholog_donor,
         paralog_deletion_symbol, paralog_resistance_cassette)

gse178430_parsed_meta %>%
  write_parquet(here("/home/chase/code/hf/barkai_compendium/GSE178430_metadata.parquet"),
                compression = "zstd",
                write_statistics = TRUE,
                use_dictionary = c(
                  accession = TRUE,
                  regulator_locus_tag = TRUE,
                  regulator_symbol = TRUE
                )
  )

gse209631_meta = read_csv("data/barkai_checseq/GSE209631/SraRunTable.csv")

gse209631_parsed_meta = gse209631_meta %>%
  select(`Sample Name`, tagged_tf, Instrument, `variant-type`) %>%
  janitor::clean_names() %>%
  dplyr::rename(accession = sample_name) %>%
  arrange(tagged_tf, variant_type) %>%
  left_join(select(genomicfeatures, locus_tag, symbol) %>% dplyr::rename(tagged_tf = symbol)) %>%
  dplyr::rename(regulator_symbol = tagged_tf, regulator_locus_tag = locus_tag) %>%
  select(accession, regulator_locus_tag, regulator_symbol, variant_type)

gse209631_parsed_meta %>%
  write_parquet(here("/home/chase/code/hf/barkai_compendium/GSE209631_metadata.parquet"),
                compression = "zstd",
                write_statistics = TRUE,
                use_dictionary = c(
                  accession = TRUE,
                  regulator_locus_tag = TRUE,
                  regulator_symbol = TRUE,
                  variant_type = TRUE
                )
  )

gse=GEOquery::getGEO(filename=here("data/barkai_checseq/GSE222268_series_matrix.txt"))

gse222268_meta = Biobase::pData(gse@phenoData) %>% as_tibble() %>%
  select(title, geo_accession, extract_protocol_ch1, description,
         instrument_model, library_selection) %>%
  mutate(description = ifelse(description == "", library_selection, description)) %>%
  dplyr::rename(accession = geo_accession) %>%
  select(-library_selection, -instrument_model, -extract_protocol_ch1)

gse222268_meta %>%
  write_parquet(here("/home/chase/code/hf/barkai_compendium/GSE222268_metadata.parquet"),
                compression = "zstd",
                write_statistics = TRUE,
                use_dictionary = c(
                  accession = TRUE
                )
  )