Chase Mateusiak commited on
Commit
ea4e6f3
·
1 Parent(s): 95d9bd6

adding peaks vs promoter sets

Browse files
README.md CHANGED
@@ -71,7 +71,6 @@ features:
71
  - rossi_2021_af_combined_start_codon_500bp
72
  - rossi_2021_af_replicates_intergenic_replicates
73
  - rossi_2021_af_combined_intergenic
74
- - macs2_annotated_peaks_combined
75
  fields:
76
  - name: regulator_locus_tag
77
  dtype: string
@@ -80,6 +79,24 @@ features:
80
  dtype: string
81
  description: Standard gene symbol of the transcription factor
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  - applies_to:
85
  - rossi_2021_af_replicates
@@ -91,7 +108,10 @@ features:
91
  - rossi_2021_af_combined_start_codon_500bp
92
  - rossi_2021_af_replicates_intergenic_replicates
93
  - rossi_2021_af_combined_intergenic
94
- - macs2_annotated_peaks_combined
 
 
 
95
  fields:
96
  - name: target_locus_tag
97
  dtype: string
@@ -133,11 +153,11 @@ features:
133
  - rossi_2021_af_combined_intergenic
134
  fields:
135
  - name: background_counts
136
- dtype: int64
137
  description: Read counts in the background/control sample for this peak region
138
  role: quantitative_measure
139
  - name: experiment_counts
140
- dtype: int64
141
  description: Read counts in the ChIP-exo experiment sample for this peak region
142
  role: quantitative_measure
143
  - name: total_background_counts
@@ -227,7 +247,7 @@ features:
227
  ORF, we take the median peak score. Replicates are then combined. When
228
  combining replicates, the median score across all replicates is taken.
229
  - name: n_peaks
230
- dtype: float64
231
  description: >-
232
  The number of peaks annotated to the same target. This is both within replicates and
233
  across (ie, there might be 3 peaks in 2 replicates for a total of 6 in this data)
@@ -236,7 +256,7 @@ features:
236
  description: >-
237
  The maximum distance a peak in the set (see n_peaks) is from the ORF
238
  - name: peak_n_replicates
239
- dtype: float64
240
  description: >-
241
  The number of replicates which have peaks for this target
242
 
@@ -269,7 +289,7 @@ configs:
269
  - config_name: rossi_2021_metadata_sample
270
  description: Sample-level metadata for combined ChIP-exo experiments including experimental conditions
271
  dataset_type: metadata
272
- applies_to: ["rossi_2021_af_combined", "rossi_2021_af_combined_mindel", "yep_filtered_peaks_combined", "rossi_2021_af_combined_start_codon_500bp", "rossi_2021_af_combined_intergenic", "macs2_annotated_peaks_combined"]
273
  data_files:
274
  - split: train
275
  path: rossi_2021_metadata_sample.parquet
@@ -304,7 +324,8 @@ configs:
304
  description: Sample identifier used by yeastepigenome.org
305
 
306
  - config_name: genome_map
307
- description: "ChIP-exo 5' tag coverage data partitioned by sample accession"
 
308
  dataset_type: genome_map
309
  data_files:
310
  - split: train
@@ -368,40 +389,90 @@ configs:
368
  dtype: string
369
  description: Chromosome name (e.g., chrI, chrII, etc.)
370
  - name: start
371
- dtype: int32
372
  description: 0-based start position of the peak region
373
  - name: end
374
- dtype: int32
375
  description: 0-based, half open end position of a single base resolution peak
376
  - name: score
377
- dtype: int32
378
  description: "Score assigned by ChExMix as reported by yeastepigenome.org"
379
 
380
- - config_name: macs2_annotated_peaks_combined
381
- description: "peaks called with macs2. see scripts/rossi_peak_analysis.R for details. peaks filtered to -log10(q) > 0.1, and to only those within 700 bp of the 5' end of the most 5' exon. max, median and nearest scores are assigned to the feature."
 
 
382
  dataset_type: annotated_features
383
  data_files:
384
  - split: train
385
- path: macs2_annotated_peaks_combined.parquet
 
 
 
 
 
386
  dataset_info:
387
  features:
388
  - name: sample_id
389
- dtype: string
390
  description: sample identifier. use with rossi_2021_metadata_sample
391
- - name: n_peaks
392
- dtype: int32
393
- description: number of peaks that are annotated to within 700 bp of the target. Note that a peak may be annotated to multiple targets
394
- - name: nearest_score
395
- dtype: float64
396
- description: -log10(qvalue) of the peak nearest to the target
397
- - name: median_score
398
- dtype: float64
399
- description: median -log10(qvalue) of the peaks annotated to the target
400
- - name: max_score
401
- dtype: float64
402
- description: max -log10(qvalue) of the peaks annotated to the target
403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
 
406
  - config_name: rossi_2021_af_replicates
407
  description: ChIP-exo annotated features at biological replicate level with binding peaks and statistical significance metrics
 
71
  - rossi_2021_af_combined_start_codon_500bp
72
  - rossi_2021_af_replicates_intergenic_replicates
73
  - rossi_2021_af_combined_intergenic
 
74
  fields:
75
  - name: regulator_locus_tag
76
  dtype: string
 
79
  dtype: string
80
  description: Standard gene symbol of the transcription factor
81
 
82
+ - applies_to:
83
+ - macs_bp500
84
+ - macs_intergenic
85
+ - macs_kang
86
+ - macs_mindel
87
+ fields:
88
+ - name: n_peaks
89
+ dtype: int32
90
+ description: number of peaks that are annotated to within 700 bp of the target. Note that a peak may be annotated to multiple targets
91
+ - name: nearest_score
92
+ dtype: float64
93
+ description: -log10(qvalue) of the peak nearest to the target
94
+ - name: median_score
95
+ dtype: float64
96
+ description: median -log10(qvalue) of the peaks annotated to the target
97
+ - name: max_score
98
+ dtype: float64
99
+ description: max -log10(qvalue) of the peaks annotated to the target
100
 
101
  - applies_to:
102
  - rossi_2021_af_replicates
 
108
  - rossi_2021_af_combined_start_codon_500bp
109
  - rossi_2021_af_replicates_intergenic_replicates
110
  - rossi_2021_af_combined_intergenic
111
+ - macs_bp500
112
+ - macs_intergenic
113
+ - macs_kang
114
+ - macs_mindel
115
  fields:
116
  - name: target_locus_tag
117
  dtype: string
 
153
  - rossi_2021_af_combined_intergenic
154
  fields:
155
  - name: background_counts
156
+ dtype: float64
157
  description: Read counts in the background/control sample for this peak region
158
  role: quantitative_measure
159
  - name: experiment_counts
160
+ dtype: float64
161
  description: Read counts in the ChIP-exo experiment sample for this peak region
162
  role: quantitative_measure
163
  - name: total_background_counts
 
247
  ORF, we take the median peak score. Replicates are then combined. When
248
  combining replicates, the median score across all replicates is taken.
249
  - name: n_peaks
250
+ dtype: int32
251
  description: >-
252
  The number of peaks annotated to the same target. This is both within replicates and
253
  across (ie, there might be 3 peaks in 2 replicates for a total of 6 in this data)
 
256
  description: >-
257
  The maximum distance a peak in the set (see n_peaks) is from the ORF
258
  - name: peak_n_replicates
259
+ dtype: int32
260
  description: >-
261
  The number of replicates which have peaks for this target
262
 
 
289
  - config_name: rossi_2021_metadata_sample
290
  description: Sample-level metadata for combined ChIP-exo experiments including experimental conditions
291
  dataset_type: metadata
292
+ applies_to: ["rossi_2021_af_combined", "rossi_2021_af_combined_mindel", "yep_filtered_peaks_combined", "rossi_2021_af_combined_start_codon_500bp", "rossi_2021_af_combined_intergenic", "macs_bp500", "macs_intergenic", "macs_kang", "macs_mindel"]
293
  data_files:
294
  - split: train
295
  path: rossi_2021_metadata_sample.parquet
 
324
  description: Sample identifier used by yeastepigenome.org
325
 
326
  - config_name: genome_map
327
+ description: >-
328
+ ChIP-exo 5' tag coverage data partitioned by sample accession. See https://github.com/BrentLab/checseq_promoter_enrichment_slurm_pipeline/tree/main/promoter_enrichment for how these are created from alignments.
329
  dataset_type: genome_map
330
  data_files:
331
  - split: train
 
389
  dtype: string
390
  description: Chromosome name (e.g., chrI, chrII, etc.)
391
  - name: start
392
+ dtype: float64
393
  description: 0-based start position of the peak region
394
  - name: end
395
+ dtype: float64
396
  description: 0-based, half open end position of a single base resolution peak
397
  - name: score
398
+ dtype: float64
399
  description: "Score assigned by ChExMix as reported by yeastepigenome.org"
400
 
401
+ - config_name: macs_bp500
402
+ description: >-
403
+ peaks called with macs. see scripts/rossi_peak_analysis.R for details. then intersected with the promoter set defined
404
+ as 500 bp upstream of the start codon.
405
  dataset_type: annotated_features
406
  data_files:
407
  - split: train
408
+ path: macs_bp500.parquet
409
+ genome_resources:
410
+ region_sets:
411
+ start_codon_500bp:
412
+ path: https://huggingface.co/datasets/BrentLab/yeast_genome_resources/blob/main/start_codon_500bp_upstream_promoters.bed
413
+ join_column: target_locus_tag
414
  dataset_info:
415
  features:
416
  - name: sample_id
417
+ dtype: int64
418
  description: sample identifier. use with rossi_2021_metadata_sample
 
 
 
 
 
 
 
 
 
 
 
 
419
 
420
+ - config_name: macs_intergenic
421
+ description: >-
422
+ peaks called with macs. see scripts/rossi_peak_analysis.R for details. then intersected with the promoter set defined
423
+ as the intergenic regions from SGD 5-1.
424
+ dataset_type: annotated_features
425
+ data_files:
426
+ - split: train
427
+ path: macs_intergenic.parquet
428
+ genome_resources:
429
+ region_sets:
430
+ intergenic:
431
+ path: https://huggingface.co/datasets/BrentLab/yeast_genome_resources/blob/main/intergenic_regions_metadata_5_1.csv
432
+ join_column: target_locus_tag
433
+ dataset_info:
434
+ features:
435
+ - name: sample_id
436
+ dtype: int64
437
+ description: sample identifier. use with rossi_2021_metadata_sample
438
 
439
+ - config_name: macs_kang
440
+ description: >-
441
+ peaks called with macs. see scripts/rossi_peak_analysis.R for details. then intersected with the promoter set defined
442
+ as 700 bp upstream, truncated by upstream features.
443
+ dataset_type: annotated_features
444
+ data_files:
445
+ - split: train
446
+ path: macs_kang.parquet
447
+ genome_resources:
448
+ region_sets:
449
+ Kang:
450
+ path: https://huggingface.co/datasets/BrentLab/yeast_genome_resources/blob/main/yiming_promoters.bed
451
+ join_column: target_locus_tag
452
+ dataset_info:
453
+ features:
454
+ - name: sample_id
455
+ dtype: int64
456
+ description: sample identifier. use with rossi_2021_metadata_sample
457
+
458
+ - config_name: macs_mindel
459
+ description: >-
460
+ peaks called with macs. see scripts/rossi_peak_analysis.R for details. then intersected with the promoter set defined
461
+ by Mindel. See huggingface/BrentLab/yeast_genome_resources.
462
+ dataset_type: annotated_features
463
+ data_files:
464
+ - split: train
465
+ path: macs_mindel.parquet
466
+ genome_resources:
467
+ region_sets:
468
+ Mindel:
469
+ path: https://huggingface.co/datasets/BrentLab/yeast_genome_resources/blob/main/mindel_promoters.csv.gz
470
+ join_column: target_locus_tag
471
+ dataset_info:
472
+ features:
473
+ - name: sample_id
474
+ dtype: int64
475
+ description: sample identifier. use with rossi_2021_metadata_sample
476
 
477
  - config_name: rossi_2021_af_replicates
478
  description: ChIP-exo annotated features at biological replicate level with binding peaks and statistical significance metrics
macs_bp500.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:704ef9d019695cf30d4bbdb89159f6006300b61c7de662e320d086022dc03032
3
+ size 7021500
macs_intergenic.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd2eb9b04fb6077047106431a515973e5e5ae778312400eb65a2f026791d20c7
3
+ size 9323332
macs_kang.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7e65e9bc8f3630b990dcc86937832407dc4f2e36439d2d0ff1c139c64a79120c
3
+ size 6998849
macs_mindel.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4b3ba76d80089378f8cfb121238d98300788a0111283310b00b505a612269dbf
3
+ size 8626913
scripts/rossi_peak_analysis.R CHANGED
@@ -5,7 +5,20 @@ library(here)
5
 
6
  exclude_regions <- rtracklayer::import(here("data/ChExMix_Peak_Filter_List_190612.bed"))
7
  seqlevels(exclude_regions)[which(seqlevels(exclude_regions) == "chr2-micron")] <- "2-micron"
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
9
 
10
  read_in_annotated_peaks <- function(peak_path) {
11
  df <- read_tsv(peak_path, show_col_types = FALSE)
@@ -96,230 +109,374 @@ peaks_df <- bind_rows(annotated_peaks$target_score, .id = "tmp") |>
96
  )
97
 
98
 
99
- rossi_sample_meta <- arrow::read_parquet("~/projects/huggingface/rossi_2021/rossi_2021_metadata_sample.parquet")
 
 
 
 
100
 
101
- brentlab_features <- read_csv("~/projects/huggingface/yeast_genome_resources/brentlab_features.csv.gz")
102
 
103
- peaks_df_to_hf <- peaks_df |>
104
- left_join(rossi_sample_meta) |>
105
- dplyr::select(sample_id, regulator_locus_tag,
106
- regulator_symbol,
107
- target_locus_tag = entrez_id,
108
- n_peaks, nearest_score, median_score, max_score
109
- ) |>
110
- left_join(dplyr::select(brentlab_features,
111
- target_locus_tag = locus_tag,
112
- target_symbol = symbol
113
- )) |>
114
- dplyr::relocate(
115
- sample_id, regulator_locus_tag, regulator_symbol,
116
- target_locus_tag, target_symbol
117
- )
118
 
119
- peaks_df_to_hf |>
120
- arrow::write_parquet("~/projects/huggingface/rossi_2021/macs2_annotated_peaks.parquet")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
- mcisaac_responsive <- arrow::read_parquet("~/projects/huggingface/hackett_2020/hackett_2020_analysis_set.parquet")
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- peaks_with_mcisaac <- peaks_df |>
125
- dplyr::rename(target_locus_tag = entrez_id) |>
126
- filter(
127
- treatment == "Normal",
128
- growth_media == "YPD",
129
- median_score >= -log10(0.1)
130
- ) |>
131
- left_join(dplyr::select(
132
- mcisaac_responsive,
133
- regulator_locus_tag,
134
- target_locus_tag,
135
- time,
136
- responsive
137
- )) |>
138
- filter(!is.na(responsive))
139
 
140
- peaks_with_mcisaac |>
141
- filter(time == 30) |>
142
- group_by(regulator_locus_tag) |>
143
- nest() |>
144
- mutate(
145
- rr_nearest = map_dbl(data, ~ {
146
- .x |>
147
- arrange(desc(nearest_score)) |>
148
- slice_head(n = 25) |>
149
- summarise(sum(responsive) / n()) |>
150
- pull()
151
- }),
152
- rr_max = map_dbl(data, ~ {
153
- .x |>
154
- arrange(desc(max_score)) |>
155
- slice_head(n = 25) |>
156
- summarise(sum(responsive) / n()) |>
157
- pull()
158
- }),
159
- rr_median = map_dbl(data, ~ {
160
- .x |>
161
- arrange(desc(median_score)) |>
162
- slice_head(n = 25) |>
163
- summarise(sum(responsive) / n()) |>
164
- pull()
165
- })
166
- ) |>
167
- dplyr::select(-data) |>
168
- pivot_longer(
169
- cols = starts_with("rr_"),
170
- names_to = "score_type",
171
- values_to = "rr"
172
- ) |>
173
- ggplot(aes(x = score_type, y = rr)) +
174
- geom_boxplot()
175
-
176
- library(patchwork)
177
- library(gridExtra)
178
-
179
- score_summary <- peaks_with_mcisaac |>
180
- filter(time == 30) |>
181
- group_by(regulator_locus_tag) |>
182
- reframe(
183
- score_type = c("nearest", "max", "median"),
184
- n_targets = c(
185
- n_distinct(target_locus_tag),
186
- n_distinct(target_locus_tag),
187
- n_distinct(target_locus_tag)
188
- ),
189
- n_peaks = c(n(), n(), n()),
190
- min = c(min(nearest_score), min(max_score), min(median_score)),
191
- max = c(max(nearest_score), max(max_score), max(median_score)),
192
- median = c(median(nearest_score), median(max_score), median(median_score)),
193
- mean = c(mean(nearest_score), mean(max_score), mean(median_score))
194
- )
195
 
196
 
197
- p1 <- score_summary |>
198
- ggplot(aes(x = score_type, y = mean, fill = score_type)) +
199
- geom_boxplot(alpha = 0.7) +
200
- labs(title = "Mean Score Distribution", y = "Mean Score", x = "") +
201
- theme_minimal() +
202
- theme(legend.position = "none")
203
-
204
- p3 <- score_summary |>
205
- ggplot(aes(x = n_targets, y = mean, color = score_type)) +
206
- geom_point(alpha = 0.6) +
207
- facet_wrap(~score_type) +
208
- scale_x_log10() +
209
- labs(title = "Number of Targets vs Mean Score", x = "N Targets (log10)", y = "Mean") +
210
- theme_minimal() +
211
- theme(legend.position = "none")
212
-
213
- # Calculate stats for table
214
- n_targets_summary <- score_summary |>
215
- dplyr::select(n_targets) |>
216
- distinct() |>
217
- pull(n_targets) %>%
218
- {
219
- tibble(
220
- Min = round(quantile(., probs = 0), 2),
221
- Q25 = round(quantile(., probs = 0.25), 2),
222
- Median = round(quantile(., probs = 0.5), 2),
223
- Q75 = round(quantile(., probs = 0.75), 2),
224
- Max = round(quantile(., probs = 1), 2)
225
- )
226
- }
227
 
228
- # Vertical boxplot
229
- p4_plot <- score_summary |>
230
- dplyr::select(regulator_locus_tag, n_targets) |>
231
- distinct() |>
232
- ggplot(aes(x = "", y = n_targets)) +
233
- geom_boxplot(width = 0.3) +
234
- scale_y_log10() +
235
- labs(title = "Distribution of Targets per Regulator", y = "N Targets (log10)", x = "") +
236
- theme_minimal() +
237
- theme(legend.position = "none")
238
-
239
- # Summary table
240
- p4_table <- gridExtra::tableGrob(n_targets_summary,
241
- rows = NULL,
242
- theme = ttheme_minimal(base_size = 10)
243
  )
244
 
245
- (p1 + p3) / (p4_plot + p4_table)
246
-
247
- authors_orig_peaks <- arrow::read_parquet("~/projects/huggingface/rossi_2021/yep_filtered_peaks.parquet") |>
248
- mutate(yeastepigenome_id = as.integer(yeastepigenome_id))
249
- authors_orig_peaks_meta <- arrow::read_parquet("~/projects/huggingface/rossi_2021/rossi_2021_metadata.parquet")
250
-
251
- authors_orig_peaks_normal_conds <- authors_orig_peaks |>
252
- left_join(authors_orig_peaks_meta) |>
253
- filter(treatment == "Normal", growth_media == "YPD")
254
-
255
- find_nearest_peaks <- function(macs_peaks, yep_chexmix_peaks, chrmap) {
256
- library(GenomicRanges)
257
- # Prepare MACS peaks (convert chr names)
258
- macs_gr <- macs_peaks |>
259
- left_join(chrmap |> dplyr::select(ucsc, chr)) |>
260
- dplyr::select(-chr) |>
261
- dplyr::rename(seqnames = ucsc) |>
262
- filter(!is.na(seqnames)) |>
263
- dplyr::select(seqnames, start, end, macs_score = peak_score) |>
264
- GRanges()
265
-
266
- # Prepare YEP ChExMix peaks (convert chr names)
267
- yep_gr <- yep_chexmix_peaks |>
268
- dplyr::select(seqnames = chr, start, end, yeastepigenome_id, yep_score = score) |>
269
- GRanges()
270
-
271
- # Find nearest neighbors
272
- hits <- distanceToNearest(yep_gr, macs_gr)
273
-
274
- # Add results back to YEP peaks
275
- yep_with_nearest <- yep_chexmix_peaks |>
276
- mutate(
277
- query_idx = 1:n(),
278
- subject_idx = subjectHits(hits),
279
- distance = mcols(hits)$distance
280
  ) |>
281
- left_join(
282
- macs_peaks |>
283
- mutate(subject_idx = 1:n()) |>
284
- dplyr::select(subject_idx, macs_score = peak_score, nearest_promoter_id),
285
- by = "subject_idx"
 
 
 
 
 
 
 
 
 
 
 
 
286
  ) |>
287
  mutate(
288
- macs_score_percentile = percent_rank(macs_score)
289
- )
290
-
291
- return(yep_with_nearest)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  }
293
 
294
- chrmap <- read_csv("~/projects/huggingface/yeast_genome_resources/chrmap.csv.gz")
295
 
296
- # Usage:
297
- authors_with_nearest <- find_nearest_peaks(
298
- annotated_peaks$df$YJR060W_CBF1_Normal_YPD,
299
- authors_orig_peaks_normal_conds |> filter(regulator_symbol == "CBF1"),
300
- chrmap
301
- )
 
 
 
302
 
303
- norm_cond_reg_syms <- intersect(
304
- str_extract(names(compact(annotated_peaks$df))[str_detect(names(compact(annotated_peaks$df)), "Normal")], "(?<=_)[^_]+(?=_)"),
305
- unique(authors_orig_peaks_normal_conds$regulator_symbol)
306
- )
307
 
 
308
 
309
- results <- map(norm_cond_reg_syms, ~ {
310
- yep_df <- filter(authors_orig_peaks_normal_conds, regulator_symbol == .x)
311
- rlt <- unique(yep_df$regulator_locus_tag)
312
- macs_df <- annotated_peaks$df[[paste(rlt, .x, "Normal_YPD", sep = "_")]] |>
313
- filter(peak_score > -log10(0.05))
314
 
315
- find_nearest_peaks(
316
- macs_df,
317
- yep_df,
318
- chrmap
 
 
 
 
 
 
 
 
 
 
319
  )
320
- })
321
-
322
- names(results) <- norm_cond_reg_syms
323
- results_df <- bind_rows(results)
324
 
325
- summary(results_df$macs_score_percentile)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  exclude_regions <- rtracklayer::import(here("data/ChExMix_Peak_Filter_List_190612.bed"))
7
  seqlevels(exclude_regions)[which(seqlevels(exclude_regions) == "chr2-micron")] <- "2-micron"
8
+ brentlab_features <- read_csv("~/projects/huggingface/yeast_genome_resources/brentlab_features.csv.gz")
9
+
10
+
11
+ promoters <- list(
12
+ bp500 = rtracklayer::import("~/projects/huggingface/yeast_genome_resources/start_codon_500bp_upstream_promoters.bed"),
13
+ mindel = GenomicRanges::GRanges(read_csv("~/projects/huggingface/yeast_genome_resources/mindel_promoters.csv.gz")),
14
+ kang = rtracklayer::import("~/projects/huggingface/yeast_genome_resources/yiming_promoters.bed"),
15
+ intergenic = rtracklayer::import("~/projects/huggingface/yeast_genome_resources/intergenic_regions_5_1.bed")
16
+ )
17
 
18
+ GenomicRanges::mcols(promoters$mindel) <- GenomicRanges::mcols(promoters$mindel) |>
19
+ as.data.frame() |>
20
+ dplyr::transmute(name = target_locus_tag) |>
21
+ S4Vectors::DataFrame()
22
 
23
  read_in_annotated_peaks <- function(peak_path) {
24
  df <- read_tsv(peak_path, show_col_types = FALSE)
 
109
  )
110
 
111
 
112
+ annotate_peaks_to_promoters <- function(peaks_df, promoters_gr) {
113
+ peaks_gr <- GenomicRanges::GRanges(
114
+ seqnames = peaks_df$chr,
115
+ ranges = IRanges::IRanges(start = peaks_df$start, end = peaks_df$end)
116
+ )
117
 
118
+ hits <- GenomicRanges::findOverlaps(peaks_gr, promoters_gr, ignore.strand = TRUE)
119
 
120
+ if (length(hits) == 0) {
121
+ return(peaks_df |> dplyr::slice(0) |> dplyr::mutate(promoter_id = character(), distance_to_tss = numeric()))
122
+ }
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
+ promoter_strand <- as.character(GenomicRanges::strand(promoters_gr))
125
+ promoter_start <- GenomicRanges::start(promoters_gr)
126
+ promoter_end <- GenomicRanges::end(promoters_gr)
127
+ promoter_name <- promoters_gr$name
128
+
129
+ peaks_df[S4Vectors::queryHits(hits), ] |>
130
+ dplyr::mutate(
131
+ promoter_id = promoter_name[S4Vectors::subjectHits(hits)],
132
+ .promoter_strand = promoter_strand[S4Vectors::subjectHits(hits)],
133
+ .promoter_start = promoter_start[S4Vectors::subjectHits(hits)],
134
+ .promoter_end = promoter_end[S4Vectors::subjectHits(hits)],
135
+ .peak_mid = (start + end) / 2,
136
+ # TSS-proximal edge of the promoter interval
137
+ .tss_pos = dplyr::if_else(.promoter_strand == "+", .promoter_end, .promoter_start),
138
+ distance_to_tss = abs(.peak_mid - .tss_pos)
139
+ ) |>
140
+ dplyr::select(-dplyr::starts_with("."), distance_to_tss, promoter_id)
141
+ }
142
 
143
+ score_targets_promoters <- function(df, promoters_gr, peak_score_thresh = -log10(0.1)) {
144
+ df |>
145
+ dplyr::filter(!in_exclude_region) |>
146
+ annotate_peaks_to_promoters(promoters_gr) |>
147
+ dplyr::filter(peak_score > peak_score_thresh) |>
148
+ dplyr::group_by(promoter_id) |>
149
+ dplyr::reframe(
150
+ n_peaks = n(),
151
+ nearest_score = peak_score[which.min(distance_to_tss)],
152
+ median_score = median(peak_score),
153
+ max_score = max(peak_score)
154
+ )
155
+ }
156
 
157
+ chrmap <- read_csv("~/projects/huggingface/yeast_genome_resources/chrmap.csv.gz")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
+ reduce_peak_cols <- function(df) {
160
+ df |>
161
+ dplyr::rename(peak_id = 1) |>
162
+ dplyr::select(peak_id, chr, start, end, strand, peak_score, in_exclude_region) |>
163
+ left_join(dplyr::select(chrmap, chr, ucsc)) |>
164
+ mutate(chr = ucsc) |>
165
+ dplyr::select(-ucsc)
166
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
 
169
+ annotated_peaks$df_reduced <- map(
170
+ compact(annotated_peaks$df),
171
+ reduce_peak_cols
172
+ )
173
+
174
+ annotated_peaks_by_promoter <- map(promoters, ~ {
175
+ map(
176
+ annotated_peaks$df_reduced,
177
+ score_targets_promoters,
178
+ promoters_gr = .
179
+ )
180
+ })
181
+
182
+ intergenic_meta <- read_csv("~/projects/huggingface/yeast_genome_resources/intergenic_regions_metadata_5_1.csv")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
 
184
+ annotated_peaks_by_promoter_df <- map(annotated_peaks_by_promoter,
185
+ bind_rows,
186
+ .id = "tmp"
 
 
 
 
 
 
 
 
 
 
 
 
187
  )
188
 
189
+ annotated_peaks_by_promoter_df$intergenic <- annotated_peaks_by_promoter_df$intergenic |>
190
+ left_join(intergenic_meta |>
191
+ dplyr::select(
192
+ promoter_id = ir_name,
193
+ feature_left,
194
+ feature_right
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  ) |>
196
+ pivot_longer(-promoter_id, values_to = "target_locus_tag") |>
197
+ dplyr::select(-name), relationship = "many-to-many") |>
198
+ dplyr::select(-promoter_id) |>
199
+ mutate(promoter_id = target_locus_tag)
200
+
201
+ chec_meta <- arrow::read_parquet("~/projects/huggingface/rossi_2021/rossi_2021_metadata_sample.parquet")
202
+
203
+ reformat_tmp <- function(df) {
204
+ df |>
205
+ separate_wider_delim(tmp,
206
+ delim = "_", names = c(
207
+ "regulator_locus_tag",
208
+ "regulator_symbol",
209
+ "treatment",
210
+ "growth_media"
211
+ ),
212
+ too_few = "align_start"
213
  ) |>
214
  mutate(
215
+ treatment = ifelse(treatment == "Heat", "Heat Shock", treatment),
216
+ growth_media = ifelse(is.na(growth_media), "YPD", growth_media)
217
+ ) |>
218
+ mutate(target_locus_tag = promoter_id) |>
219
+ dplyr::select(-promoter_id) |>
220
+ left_join(dplyr::select(brentlab_features,
221
+ target_locus_tag = locus_tag,
222
+ target_symbol = symbol
223
+ )) |>
224
+ dplyr::relocate(regulator_locus_tag, regulator_symbol, treatment, growth_media, target_locus_tag, target_symbol) |>
225
+ group_by(regulator_locus_tag, treatment, growth_media) |>
226
+ arrange(desc(max_score)) |>
227
+ ungroup() |>
228
+ filter(
229
+ !is.na(target_locus_tag),
230
+ !is.na(target_symbol)
231
+ ) |>
232
+ left_join(dplyr::select(
233
+ chec_meta,
234
+ sample_id, regulator_locus_tag,
235
+ treatment, growth_media
236
+ )) |>
237
+ dplyr::relocate(sample_id)
238
  }
239
 
240
+ annotated_peaks_by_promoter_df_out <- map(annotated_peaks_by_promoter_df, reformat_tmp)
241
 
242
+ write_out_promoter_intersect_peaks <- function(name, df) {
243
+ output_path <- file.path(
244
+ "~/projects/huggingface/rossi_2021",
245
+ paste0("macs_", name, ".parquet")
246
+ )
247
+ df |>
248
+ dplyr::select(-c(regulator_locus_tag, regulator_symbol, treatment, growth_media)) |>
249
+ arrow::write_parquet(output_path)
250
+ }
251
 
252
+ # map2(names(annotated_peaks_by_promoter_df_out),
253
+ # annotated_peaks_by_promoter_df_out,
254
+ # write_out_promoter_intersect_peaks)
 
255
 
256
+ rossi_sample_meta <- arrow::read_parquet("~/projects/huggingface/rossi_2021/rossi_2021_metadata_sample.parquet")
257
 
258
+ brentlab_features <- read_csv("~/projects/huggingface/yeast_genome_resources/brentlab_features.csv.gz")
 
 
 
 
259
 
260
+ peaks_df_to_hf <- peaks_df |>
261
+ left_join(rossi_sample_meta) |>
262
+ dplyr::select(sample_id, regulator_locus_tag,
263
+ regulator_symbol,
264
+ target_locus_tag = entrez_id,
265
+ n_peaks, nearest_score, median_score, max_score
266
+ ) |>
267
+ left_join(dplyr::select(brentlab_features,
268
+ target_locus_tag = locus_tag,
269
+ target_symbol = symbol
270
+ )) |>
271
+ dplyr::relocate(
272
+ sample_id, regulator_locus_tag, regulator_symbol,
273
+ target_locus_tag, target_symbol
274
  )
 
 
 
 
275
 
276
+ # peaks_df_to_hf |>
277
+ # arrow::write_parquet("~/projects/huggingface/rossi_2021/macs2_annotated_peaks.parquet")
278
+
279
+ # mcisaac_responsive <- arrow::read_parquet("~/projects/huggingface/hackett_2020/hackett_2020_analysis_set.parquet")
280
+ #
281
+ # peaks_with_mcisaac <- peaks_df |>
282
+ # dplyr::rename(target_locus_tag = entrez_id) |>
283
+ # filter(
284
+ # treatment == "Normal",
285
+ # growth_media == "YPD",
286
+ # median_score >= -log10(0.1)
287
+ # ) |>
288
+ # left_join(dplyr::select(
289
+ # mcisaac_responsive,
290
+ # regulator_locus_tag,
291
+ # target_locus_tag,
292
+ # time,
293
+ # responsive
294
+ # )) |>
295
+ # filter(!is.na(responsive))
296
+ #
297
+ # peaks_with_mcisaac |>
298
+ # filter(time == 30) |>
299
+ # group_by(regulator_locus_tag) |>
300
+ # nest() |>
301
+ # mutate(
302
+ # rr_nearest = map_dbl(data, ~ {
303
+ # .x |>
304
+ # arrange(desc(nearest_score)) |>
305
+ # slice_head(n = 25) |>
306
+ # summarise(sum(responsive) / n()) |>
307
+ # pull()
308
+ # }),
309
+ # rr_max = map_dbl(data, ~ {
310
+ # .x |>
311
+ # arrange(desc(max_score)) |>
312
+ # slice_head(n = 25) |>
313
+ # summarise(sum(responsive) / n()) |>
314
+ # pull()
315
+ # }),
316
+ # rr_median = map_dbl(data, ~ {
317
+ # .x |>
318
+ # arrange(desc(median_score)) |>
319
+ # slice_head(n = 25) |>
320
+ # summarise(sum(responsive) / n()) |>
321
+ # pull()
322
+ # })
323
+ # ) |>
324
+ # dplyr::select(-data) |>
325
+ # pivot_longer(
326
+ # cols = starts_with("rr_"),
327
+ # names_to = "score_type",
328
+ # values_to = "rr"
329
+ # ) |>
330
+ # ggplot(aes(x = score_type, y = rr)) +
331
+ # geom_boxplot()
332
+ #
333
+ # library(patchwork)
334
+ # library(gridExtra)
335
+ #
336
+ # score_summary <- peaks_with_mcisaac |>
337
+ # filter(time == 30) |>
338
+ # group_by(regulator_locus_tag) |>
339
+ # reframe(
340
+ # score_type = c("nearest", "max", "median"),
341
+ # n_targets = c(
342
+ # n_distinct(target_locus_tag),
343
+ # n_distinct(target_locus_tag),
344
+ # n_distinct(target_locus_tag)
345
+ # ),
346
+ # n_peaks = c(n(), n(), n()),
347
+ # min = c(min(nearest_score), min(max_score), min(median_score)),
348
+ # max = c(max(nearest_score), max(max_score), max(median_score)),
349
+ # median = c(median(nearest_score), median(max_score), median(median_score)),
350
+ # mean = c(mean(nearest_score), mean(max_score), mean(median_score))
351
+ # )
352
+ #
353
+ #
354
+ # p1 <- score_summary |>
355
+ # ggplot(aes(x = score_type, y = mean, fill = score_type)) +
356
+ # geom_boxplot(alpha = 0.7) +
357
+ # labs(title = "Mean Score Distribution", y = "Mean Score", x = "") +
358
+ # theme_minimal() +
359
+ # theme(legend.position = "none")
360
+ #
361
+ # p3 <- score_summary |>
362
+ # ggplot(aes(x = n_targets, y = mean, color = score_type)) +
363
+ # geom_point(alpha = 0.6) +
364
+ # facet_wrap(~score_type) +
365
+ # scale_x_log10() +
366
+ # labs(title = "Number of Targets vs Mean Score", x = "N Targets (log10)", y = "Mean") +
367
+ # theme_minimal() +
368
+ # theme(legend.position = "none")
369
+ #
370
+ # # Calculate stats for table
371
+ # n_targets_summary <- score_summary |>
372
+ # dplyr::select(n_targets) |>
373
+ # distinct() |>
374
+ # pull(n_targets) %>%
375
+ # {
376
+ # tibble(
377
+ # Min = round(quantile(., probs = 0), 2),
378
+ # Q25 = round(quantile(., probs = 0.25), 2),
379
+ # Median = round(quantile(., probs = 0.5), 2),
380
+ # Q75 = round(quantile(., probs = 0.75), 2),
381
+ # Max = round(quantile(., probs = 1), 2)
382
+ # )
383
+ # }
384
+ #
385
+ # # Vertical boxplot
386
+ # p4_plot <- score_summary |>
387
+ # dplyr::select(regulator_locus_tag, n_targets) |>
388
+ # distinct() |>
389
+ # ggplot(aes(x = "", y = n_targets)) +
390
+ # geom_boxplot(width = 0.3) +
391
+ # scale_y_log10() +
392
+ # labs(title = "Distribution of Targets per Regulator", y = "N Targets (log10)", x = "") +
393
+ # theme_minimal() +
394
+ # theme(legend.position = "none")
395
+ #
396
+ # # Summary table
397
+ # p4_table <- gridExtra::tableGrob(n_targets_summary,
398
+ # rows = NULL,
399
+ # theme = ttheme_minimal(base_size = 10)
400
+ # )
401
+ #
402
+ # (p1 + p3) / (p4_plot + p4_table)
403
+ #
404
+ # authors_orig_peaks <- arrow::read_parquet("~/projects/huggingface/rossi_2021/yep_filtered_peaks.parquet") |>
405
+ # mutate(yeastepigenome_id = as.integer(yeastepigenome_id))
406
+ # authors_orig_peaks_meta <- arrow::read_parquet("~/projects/huggingface/rossi_2021/rossi_2021_metadata.parquet")
407
+ #
408
+ # authors_orig_peaks_normal_conds <- authors_orig_peaks |>
409
+ # left_join(authors_orig_peaks_meta) |>
410
+ # filter(treatment == "Normal", growth_media == "YPD")
411
+ #
412
+ # find_nearest_peaks <- function(macs_peaks, yep_chexmix_peaks, chrmap) {
413
+ # library(GenomicRanges)
414
+ # # Prepare MACS peaks (convert chr names)
415
+ # macs_gr <- macs_peaks |>
416
+ # left_join(chrmap |> dplyr::select(ucsc, chr)) |>
417
+ # dplyr::select(-chr) |>
418
+ # dplyr::rename(seqnames = ucsc) |>
419
+ # filter(!is.na(seqnames)) |>
420
+ # dplyr::select(seqnames, start, end, macs_score = peak_score) |>
421
+ # GRanges()
422
+ #
423
+ # # Prepare YEP ChExMix peaks (convert chr names)
424
+ # yep_gr <- yep_chexmix_peaks |>
425
+ # dplyr::select(seqnames = chr, start, end, yeastepigenome_id, yep_score = score) |>
426
+ # GRanges()
427
+ #
428
+ # # Find nearest neighbors
429
+ # hits <- distanceToNearest(yep_gr, macs_gr)
430
+ #
431
+ # # Add results back to YEP peaks
432
+ # yep_with_nearest <- yep_chexmix_peaks |>
433
+ # mutate(
434
+ # query_idx = 1:n(),
435
+ # subject_idx = subjectHits(hits),
436
+ # distance = mcols(hits)$distance
437
+ # ) |>
438
+ # left_join(
439
+ # macs_peaks |>
440
+ # mutate(subject_idx = 1:n()) |>
441
+ # dplyr::select(subject_idx, macs_score = peak_score, nearest_promoter_id),
442
+ # by = "subject_idx"
443
+ # ) |>
444
+ # mutate(
445
+ # macs_score_percentile = percent_rank(macs_score)
446
+ # )
447
+ #
448
+ # return(yep_with_nearest)
449
+ # }
450
+ #
451
+ # chrmap <- read_csv("~/projects/huggingface/yeast_genome_resources/chrmap.csv.gz")
452
+ #
453
+ # # Usage:
454
+ # authors_with_nearest <- find_nearest_peaks(
455
+ # annotated_peaks$df$YJR060W_CBF1_Normal_YPD,
456
+ # authors_orig_peaks_normal_conds |> filter(regulator_symbol == "CBF1"),
457
+ # chrmap
458
+ # )
459
+ #
460
+ # norm_cond_reg_syms <- intersect(
461
+ # str_extract(names(compact(annotated_peaks$df))[str_detect(names(compact(annotated_peaks$df)), "Normal")], "(?<=_)[^_]+(?=_)"),
462
+ # unique(authors_orig_peaks_normal_conds$regulator_symbol)
463
+ # )
464
+ #
465
+ #
466
+ # results <- map(norm_cond_reg_syms, ~ {
467
+ # yep_df <- filter(authors_orig_peaks_normal_conds, regulator_symbol == .x)
468
+ # rlt <- unique(yep_df$regulator_locus_tag)
469
+ # macs_df <- annotated_peaks$df[[paste(rlt, .x, "Normal_YPD", sep = "_")]] |>
470
+ # filter(peak_score > -log10(0.05))
471
+ #
472
+ # find_nearest_peaks(
473
+ # macs_df,
474
+ # yep_df,
475
+ # chrmap
476
+ # )
477
+ # })
478
+ #
479
+ # names(results) <- norm_cond_reg_syms
480
+ # results_df <- bind_rows(results)
481
+ #
482
+ # summary(results_df$macs_score_percentile)