cmatkhan commited on
Commit
9189222
·
1 Parent(s): 6f38c0a

re-doing chexmix peaks to annotate to nearest downstream orf

Browse files
README.md CHANGED
@@ -81,8 +81,7 @@ features:
81
  - rossi_2021_af_combined
82
  - rossi_2021_af_replicates_mindel
83
  - rossi_2021_af_combined_mindel
84
- - yep_filtered_peaks_yiming
85
- - yep_filtered_peaks_mindel
86
  fields:
87
  - name: target_locus_tag
88
  dtype: string
@@ -102,6 +101,22 @@ features:
102
  dtype: int64
103
  description: Promoter region end position (1-based, inclusive)
104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  - applies_to:
106
  - rossi_2021_af_replicates
107
  - rossi_2021_af_combined
@@ -156,8 +171,6 @@ features:
156
  - applies_to:
157
  - rossi_2021_af_replicates_mindel
158
  - rossi_2021_af_combined_mindel
159
- - yep_filtered_peaks_yiming
160
- - yep_filtered_peaks_mindel
161
  fields:
162
  - name: width
163
  dtype: int64
@@ -187,20 +200,28 @@ features:
187
  this is a feature from the Mindel data that I have not documented. It is a TODO.
188
 
189
  - applies_to:
190
- - yep_filtered_peaks_yiming
191
- - yep_filtered_peaks_mindel
192
  fields:
193
- - name: score
194
  dtype: float64
195
  description: >-
196
  The filtered bed file representing the high confidence ChExMix peaks from
197
- yeastepigenome.org score. Where multiple peaks intersect the same promoter,
198
- the median score is taken for that promoter. Replicates are then combined. When
199
- combining replicates, the median score across all replicates is taken. Note that
200
- when a replicate does not have a peak score for a promoter in the promoter set,
201
- that promoter is assigned a score of 0. So if replicate A as a peak score of
202
- 500 and replicate B did not have any peak intersecting with that promoter, then
203
- the combined score for that promoter would be 250.
 
 
 
 
 
 
 
 
 
204
 
205
  configs:
206
  - config_name: rossi_2021_metadata_replicate
@@ -231,7 +252,7 @@ configs:
231
  - config_name: rossi_2021_metadata_sample
232
  description: Sample-level metadata for combined ChIP-exo experiments including experimental conditions
233
  dataset_type: metadata
234
- applies_to: ["rossi_2021_af_combined", "rossi_2021_af_combined_mindel", "yep_filtered_peaks_yiming", "yep_filtered_peaks_mindel"]
235
  data_files:
236
  - split: train
237
  path: rossi_2021_metadata_sample.parquet
@@ -395,47 +416,17 @@ configs:
395
  description: Unique identifier combining regulator and replicates
396
  role: sample_id
397
 
398
- - config_name: yep_filtered_peaks_yiming
399
  description: >-
400
- The filtered high confidence peaks reported at yeastepigenome.org intersected
401
- with the Kang promoter regions. see `scripts/parse_rossi_filtered_peaks.R`
402
- for more details. NOTE: be sure to read the definition of the score column,
403
- which is at the top of this README in the common features section, as the
404
- score has been combined across peaks intersecting the same promoter in the same
405
- replicate, and then also across replicates.
406
- dataset_type: annotated_features
407
- genome_resources:
408
- region_sets:
409
- Kang:
410
- path: https://huggingface.co/datasets/BrentLab/yeast_genome_resources/blob/main/yiming_promoters.bed
411
- join_column: target_locus_tag
412
- data_files:
413
- - split: train
414
- path: yep_filtered_peaks_yiming.parquet
415
- dataset_info:
416
- features:
417
- - name: sample_id
418
- dtype: int64
419
- description: Unique identifier for the biological replicate
420
- role: sample_id
421
-
422
- - config_name: yep_filtered_peaks_mindel
423
- description: >-
424
- The filtered high confidence peaks reported at yeastepigenome.org intersected
425
- with the Mindel promoter regions. see `scripts/parse_rossi_filtered_peaks.R`
426
- for more details. NOTE: be sure to read the definition of the score column,
427
- which is at the top of this README in the common features section, as the
428
- score has been combined across peaks intersecting the same promoter in the same
429
- replicate, and then also across replicates.
430
- dataset_type: annotated_features
431
- genome_resources:
432
- region_sets:
433
- Mindel:
434
- path: https://huggingface.co/datasets/BrentLab/yeast_genome_resources/blob/main/mindel_promoters.csv.gz
435
- join_column: target_locus_tag
436
  data_files:
437
  - split: train
438
- path: yep_filtered_peaks_mindel.parquet
439
  dataset_info:
440
  features:
441
  - name: sample_id
 
81
  - rossi_2021_af_combined
82
  - rossi_2021_af_replicates_mindel
83
  - rossi_2021_af_combined_mindel
84
+ - yep_filtered_peaks_combined
 
85
  fields:
86
  - name: target_locus_tag
87
  dtype: string
 
101
  dtype: int64
102
  description: Promoter region end position (1-based, inclusive)
103
 
104
+ - applies_to:
105
+ - rossi_2021_af_replicates
106
+ - rossi_2021_af_combined
107
+ - rossi_2021_af_replicates_mindel
108
+ - rossi_2021_af_combined_mindel
109
+ fields:
110
+ - name: seqnames
111
+ dtype: string
112
+ description: Chromosome identifier (e.g., chrI, chrII, chrXVI)
113
+ - name: start
114
+ dtype: int64
115
+ description: Promoter region start position (1-based coordinate)
116
+ - name: end
117
+ dtype: int64
118
+ description: Promoter region end position (1-based, inclusive)
119
+
120
  - applies_to:
121
  - rossi_2021_af_replicates
122
  - rossi_2021_af_combined
 
171
  - applies_to:
172
  - rossi_2021_af_replicates_mindel
173
  - rossi_2021_af_combined_mindel
 
 
174
  fields:
175
  - name: width
176
  dtype: int64
 
200
  this is a feature from the Mindel data that I have not documented. It is a TODO.
201
 
202
  - applies_to:
203
+ - yep_filtered_peaks_combined
 
204
  fields:
205
+ - name: peak_score
206
  dtype: float64
207
  description: >-
208
  The filtered bed file representing the high confidence ChExMix peaks from
209
+ yeastepigenome.org score. Where multiple peaks are within 500bp upstream of a target
210
+ ORF, we take the median peak score. Replicates are then combined. When
211
+ combining replicates, the median score across all replicates is taken.
212
+ - name: n_peaks
213
+ dtype: float64
214
+ description: >-
215
+ The number of peaks annotated to the same target. This is both within replicates and
216
+ across (ie, there might be 3 peaks in 2 replicates for a total of 6 in this data)
217
+ - name: max_distance
218
+ dtype: float64
219
+ description: >-
220
+ The maximum distance a peak in the set (see n_peaks) is from the ORF
221
+ - name: peak_n_replicates
222
+ dtype: float64
223
+ description: >-
224
+ The number of replicates which have peaks for this target
225
 
226
  configs:
227
  - config_name: rossi_2021_metadata_replicate
 
252
  - config_name: rossi_2021_metadata_sample
253
  description: Sample-level metadata for combined ChIP-exo experiments including experimental conditions
254
  dataset_type: metadata
255
+ applies_to: ["rossi_2021_af_combined", "rossi_2021_af_combined_mindel", "yep_filtered_peaks_combined"]
256
  data_files:
257
  - split: train
258
  path: rossi_2021_metadata_sample.parquet
 
416
  description: Unique identifier combining regulator and replicates
417
  role: sample_id
418
 
419
+ - config_name: yep_filtered_peaks_combined
420
  description: >-
421
+ The filtered high confidence peaks reported at yeastepigenome.org. The peaks
422
+ are annotated to the nearest downstream gene (not dubious). Peaks within 500
423
+ bp of the ORF are keep. Within a given replicate, if a target has more than 1
424
+ peak, then the median score is taken. Replicates are combined in the same way,
425
+ with the median target score across replicates. see
426
+ `scripts/parse_rossi_filtered_peaks.R` for more details.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427
  data_files:
428
  - split: train
429
+ path: yep_filtered_peaks_combined.parquet
430
  dataset_info:
431
  features:
432
  - name: sample_id
scripts/CopyOfparse_rossi_filtered_peaks.R ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # fetch the zip files from the yeastepigenome.org server
2
+ # https://www.datacommons.psu.edu/download/eberly/pughlab/yeast-epigenome-project/
3
+ # and then extract them
4
+ # with
5
+ #!/usr/bin/env bash
6
+ # fetch_yep_peaks.sh
7
+ #
8
+ # Extracts *_chexmix_filtered_peaks.bed from each *_YEP.zip in ZIP_DIR.
9
+ # Output: yep_filtered_peaks/ in the current working directory.
10
+ #
11
+ # set -euo pipefail
12
+ #
13
+ # ZIP_DIR="${1:-/lts/mblab/downloaded_data/chipexo/yeastepigenome_data}"
14
+ # OUT_DIR="$(pwd)/yep_filtered_peaks"
15
+ #
16
+ # if [[ ! -d "$ZIP_DIR" ]]; then
17
+ # echo "[error] ZIP_DIR not found: $ZIP_DIR" >&2
18
+ # exit 1
19
+ # fi
20
+ #
21
+ # mkdir -p "$OUT_DIR"
22
+ #
23
+ # COUNT=0; SKIP=0; FAIL=0
24
+ #
25
+ # for zip_path in "$ZIP_DIR"/*_YEP.zip; do
26
+ # zip_name=$(basename "$zip_path")
27
+ # sample_id="${zip_name%_YEP.zip}"
28
+ # bed_name="${sample_id}_chexmix_filtered_peaks.bed"
29
+ # dest="$OUT_DIR/$bed_name"
30
+ #
31
+ # if [[ -f "$dest" ]]; then
32
+ # echo "[skip] $bed_name already exists"
33
+ # (( SKIP++ )) || true
34
+ # continue
35
+ # fi
36
+ #
37
+ # # file path inside the zip may vary — find it
38
+ # bed_in_zip=$(unzip -Z1 "$zip_path" 2>/dev/null | grep -m1 "${bed_name}$" || true)
39
+ #
40
+ # if [[ -z "$bed_in_zip" ]]; then
41
+ # echo "[warn] $bed_name not found in $zip_name — skipping" >&2
42
+ # (( FAIL++ )) || true
43
+ # continue
44
+ # fi
45
+ #
46
+ # echo "[ext ] $zip_name → $bed_name"
47
+ # unzip -p "$zip_path" "$bed_in_zip" > "$dest"
48
+ # (( COUNT++ )) || true
49
+ # done
50
+ #
51
+ # echo ""
52
+ # echo "[done] extracted=$COUNT skipped=$SKIP failed=$FAIL"
53
+ # echo "[done] output in: $OUT_DIR"
54
+
55
+ # the following can be run on the output
56
+
57
+ library(tidyverse)
58
+ library(arrow)
59
+ library(GenomicRanges)
60
+ library(here)
61
+
62
+ #' Convert BED format data frame to GRanges
63
+ #'
64
+ #' Handles coordinate system conversion from 0-indexed half-open BED format
65
+ #' to 1-indexed closed GenomicRanges format
66
+ #'
67
+ #' @param bed_df Data frame with chr, start, end columns in BED format (0-indexed, half-open)
68
+ #' @param zero_indexed Logical, whether input is 0-indexed (default: TRUE)
69
+ #' @return GRanges object
70
+ bed_to_granges <- function(bed_df, zero_indexed = TRUE) {
71
+
72
+ if (!all(c("chr", "start", "end") %in% names(bed_df))) {
73
+ stop("bed_df must have columns: chr, start, end")
74
+ }
75
+
76
+ # Convert from 0-indexed half-open [start, end) to 1-indexed closed [start, end]
77
+ if (zero_indexed) {
78
+ gr_start <- bed_df$start + 1
79
+ gr_end <- bed_df$end
80
+ } else {
81
+ gr_start <- bed_df$start
82
+ gr_end <- bed_df$end
83
+ }
84
+
85
+ # Create GRanges object (strand-agnostic for calling cards)
86
+ gr <- GenomicRanges::GRanges(
87
+ seqnames = bed_df$chr,
88
+ ranges = IRanges::IRanges(start = gr_start, end = gr_end),
89
+ strand = "*"
90
+ )
91
+
92
+ # Add any additional metadata columns
93
+ extra_cols <- setdiff(names(bed_df), c("chr", "start", "end", "strand"))
94
+ if (length(extra_cols) > 0) {
95
+ GenomicRanges::mcols(gr) <- bed_df[, extra_cols, drop = FALSE]
96
+ }
97
+
98
+ return(gr)
99
+ }
100
+
101
+ #' Annotate genomic regions with ChExMix peak scores
102
+ #'
103
+ #' Intersects a set of genomic regions (e.g. promoters) with ChExMix filtered
104
+ #' peaks and assigns each region a score. Regions overlapping a single peak
105
+ #' receive that peak's score; regions overlapping multiple peaks receive the
106
+ #' median score across all overlapping peaks. Non-overlapping regions are
107
+ #' scored 0.
108
+ #'
109
+ #' @param peaks_df Data frame with BED-format peak calls (0-indexed, half-open)
110
+ #' and a \code{score} column, as produced by \code{read_in_filtered_bed}.
111
+ #' @param regions_gr GRanges object of genomic regions to annotate
112
+ #' (e.g. an element of \code{regions_list}).
113
+ #'
114
+ #' @return A copy of \code{regions_gr} with an additional metadata column
115
+ #' \code{score}: the peak score for overlapping regions, 0 otherwise.
116
+ #'
117
+ #' @seealso \code{\link{bed_to_granges}}, \code{\link{read_in_filtered_bed}}
118
+ peaks_to_promoter_scores <- function(peaks_df, regions_gr) {
119
+
120
+ peaks_gr <- bed_to_granges(peaks_df)
121
+
122
+ hits <- GenomicRanges::findOverlaps(regions_gr, peaks_gr)
123
+
124
+ # for each hit, pull the peak score
125
+ scores_df <- tibble(
126
+ region_idx = S4Vectors::queryHits(hits),
127
+ score = peaks_df$score[S4Vectors::subjectHits(hits)]
128
+ ) |>
129
+ summarise(score = median(score), .by = region_idx)
130
+
131
+ # initialize all promoters to 0, fill in overlapping ones
132
+ result <- rep(0, length(regions_gr))
133
+ result[scores_df$region_idx] <- scores_df$score
134
+
135
+ regions_gr_tmp <- regions_gr
136
+ regions_gr_tmp$score <- result
137
+ regions_gr_tmp
138
+ }
139
+
140
+
141
+ rossi_meta = arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_metadata.parquet")
142
+ chrmap = read_csv("~/code/hf/yeast_genome_resources/chrmap.csv.gz")
143
+
144
+ brentlab_features = read_csv("~/code/hf/yeast_genome_resources/brentlab_features.csv.gz")
145
+
146
+ # Load regions
147
+ regions_list = list(
148
+ yiming = read_tsv(
149
+ "~/code/hf/yeast_genome_resources/yiming_promoters.bed",
150
+ col_names = c('chr', 'start', 'end', 'locus_tag', 'score', 'strand')) %>%
151
+ bed_to_granges(),
152
+ mindel = arrow::read_parquet("~/code/hf/yeast_genome_resources/mindel_promoters.parquet") |>
153
+ bed_to_granges()
154
+ )
155
+
156
+ filtered_bed = list.files(here("data/yep_filtered_peaks"), full.names = TRUE)
157
+ names(filtered_bed) = str_extract(basename(filtered_bed), "^\\d+")
158
+
159
+ read_in_filtered_bed = function(path){
160
+ read_tsv(path, col_names = c('chr', 'start', 'end', 'source', 'score')) |>
161
+ left_join(dplyr::select(chrmap, chr, ucsc)) |>
162
+ dplyr::select(-chr) |>
163
+ dplyr::rename(chr = ucsc) |>
164
+ dplyr::relocate(chr)
165
+ }
166
+
167
+ filtered_bed_df_list = map(filtered_bed, read_in_filtered_bed)
168
+
169
+ filtered_bed_df_list_no_null = filtered_bed_df_list[unlist(map(filtered_bed_df_list, ~nrow(.) > 0))]
170
+
171
+ filtered_bed_df_list_no_null_in_meta = filtered_bed_df_list_no_null[
172
+ as.integer(names(filtered_bed_df_list_no_null)) %in% rossi_meta$yeastepigenome_id]
173
+
174
+ af_list = list(
175
+ yiming = map(filtered_bed_df_list_no_null_in_meta, peaks_to_promoter_scores, regions_list$yiming),
176
+ mindel = map(filtered_bed_df_list_no_null_in_meta, peaks_to_promoter_scores, regions_list$mindel)
177
+ )
178
+
179
+ af_list_dfs = list(
180
+ yiming = bind_rows(map(af_list$yiming, as_tibble), .id = "yeastepigenome_id") |>
181
+ mutate(yeastepigenome_id = as.integer(yeastepigenome_id)) |>
182
+ left_join(dplyr::select(brentlab_features, locus_tag, symbol)) |>
183
+ dplyr::rename(target_locus_tag = locus_tag, target_symbol = symbol) |>
184
+ left_join(rossi_meta) |>
185
+ group_by(sample_id, seqnames, start, end, width, strand, target_locus_tag, target_symbol) |>
186
+ reframe(score = median(score)) |>
187
+ group_by(sample_id) |>
188
+ arrange(desc(score)),
189
+ mindel = bind_rows(map(af_list$mindel, as_tibble), .id = "yeastepigenome_id") |>
190
+ mutate(yeastepigenome_id = as.integer(yeastepigenome_id)) |>
191
+ left_join(rossi_meta) |>
192
+ group_by(sample_id, seqnames, start, end, width, strand, target_locus_tag, target_symbol) |>
193
+ reframe(score = median(score)) |>
194
+ group_by(sample_id) |>
195
+ arrange(desc(score))
196
+ )
197
+
198
+ # arrow::write_parquet(
199
+ # af_list_dfs$yiming,
200
+ # "~/code/hf/rossi_2021/yep_filtered_peaks_yiming.parquet",
201
+ # compression = "zstd",
202
+ # write_statistics = TRUE
203
+ # )
204
+ #
205
+ # arrow::write_parquet(
206
+ # af_list_dfs$mindel,
207
+ # "~/code/hf/rossi_2021/yep_filtered_peaks_mindel.parquet",
208
+ # compression = "zstd",
209
+ # write_statistics = TRUE
210
+ # )
scripts/parse_rossi_filtered_peaks.R CHANGED
@@ -98,60 +98,96 @@ bed_to_granges <- function(bed_df, zero_indexed = TRUE) {
98
  return(gr)
99
  }
100
 
101
- #' Annotate genomic regions with ChExMix peak scores
102
  #'
103
- #' Intersects a set of genomic regions (e.g. promoters) with ChExMix filtered
104
- #' peaks and assigns each region a score. Regions overlapping a single peak
105
- #' receive that peak's score; regions overlapping multiple peaks receive the
106
- #' median score across all overlapping peaks. Non-overlapping regions are
107
- #' scored 0.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  #'
109
  #' @param peaks_df Data frame with BED-format peak calls (0-indexed, half-open)
110
  #' and a \code{score} column, as produced by \code{read_in_filtered_bed}.
111
- #' @param regions_gr GRanges object of genomic regions to annotate
112
- #' (e.g. an element of \code{regions_list}).
 
 
113
  #'
114
- #' @return A copy of \code{regions_gr} with an additional metadata column
115
- #' \code{score}: the peak score for overlapping regions, 0 otherwise.
 
 
116
  #'
117
- #' @seealso \code{\link{bed_to_granges}}, \code{\link{read_in_filtered_bed}}
118
- peaks_to_promoter_scores <- function(peaks_df, regions_gr) {
119
 
120
  peaks_gr <- bed_to_granges(peaks_df)
121
 
122
- hits <- GenomicRanges::findOverlaps(regions_gr, peaks_gr)
123
-
124
- # for each hit, pull the peak score
125
- scores_df <- tibble(
126
- region_idx = S4Vectors::queryHits(hits),
127
- score = peaks_df$score[S4Vectors::subjectHits(hits)]
128
- ) |>
129
- summarise(score = median(score), .by = region_idx)
130
-
131
- # initialize all promoters to 0, fill in overlapping ones
132
- result <- rep(0, length(regions_gr))
133
- result[scores_df$region_idx] <- scores_df$score
134
-
135
- regions_gr_tmp <- regions_gr
136
- regions_gr_tmp$score <- result
137
- regions_gr_tmp
 
 
 
 
 
138
  }
139
 
140
 
141
  rossi_meta = arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_metadata.parquet")
142
  chrmap = read_csv("~/code/hf/yeast_genome_resources/chrmap.csv.gz")
143
 
144
- brentlab_features = read_csv("~/code/hf/yeast_genome_resources/brentlab_features.csv.gz")
145
 
146
- # Load regions
147
- regions_list = list(
148
- yiming = read_tsv(
149
- "~/code/hf/yeast_genome_resources/yiming_promoters.bed",
150
- col_names = c('chr', 'start', 'end', 'locus_tag', 'score', 'strand')) %>%
151
- bed_to_granges(),
152
- mindel = arrow::read_parquet("~/code/hf/yeast_genome_resources/mindel_promoters.parquet") |>
153
- bed_to_granges()
154
- )
155
 
156
  filtered_bed = list.files(here("data/yep_filtered_peaks"), full.names = TRUE)
157
  names(filtered_bed) = str_extract(basename(filtered_bed), "^\\d+")
@@ -171,40 +207,24 @@ filtered_bed_df_list_no_null = filtered_bed_df_list[unlist(map(filtered_bed_df_l
171
  filtered_bed_df_list_no_null_in_meta = filtered_bed_df_list_no_null[
172
  as.integer(names(filtered_bed_df_list_no_null)) %in% rossi_meta$yeastepigenome_id]
173
 
174
- af_list = list(
175
- yiming = map(filtered_bed_df_list_no_null_in_meta, peaks_to_promoter_scores, regions_list$yiming),
176
- mindel = map(filtered_bed_df_list_no_null_in_meta, peaks_to_promoter_scores, regions_list$mindel)
177
- )
178
-
179
- af_list_dfs = list(
180
- yiming = bind_rows(map(af_list$yiming, as_tibble), .id = "yeastepigenome_id") |>
181
- mutate(yeastepigenome_id = as.integer(yeastepigenome_id)) |>
182
- left_join(dplyr::select(brentlab_features, locus_tag, symbol)) |>
183
- dplyr::rename(target_locus_tag = locus_tag, target_symbol = symbol) |>
184
- left_join(rossi_meta) |>
185
- group_by(sample_id, seqnames, start, end, width, strand, target_locus_tag, target_symbol) |>
186
- reframe(score = median(score)) |>
187
- group_by(sample_id) |>
188
- arrange(desc(score)),
189
- mindel = bind_rows(map(af_list$mindel, as_tibble), .id = "yeastepigenome_id") |>
190
- mutate(yeastepigenome_id = as.integer(yeastepigenome_id)) |>
191
- left_join(rossi_meta) |>
192
- group_by(sample_id, seqnames, start, end, width, strand, target_locus_tag, target_symbol) |>
193
- reframe(score = median(score)) |>
194
- group_by(sample_id) |>
195
- arrange(desc(score))
196
- )
197
 
198
  # arrow::write_parquet(
199
- # af_list_dfs$yiming,
200
- # "~/code/hf/rossi_2021/yep_filtered_peaks_yiming.parquet",
201
- # compression = "zstd",
202
- # write_statistics = TRUE
203
- # )
204
- #
205
- # arrow::write_parquet(
206
- # af_list_dfs$mindel,
207
- # "~/code/hf/rossi_2021/yep_filtered_peaks_mindel.parquet",
208
  # compression = "zstd",
209
  # write_statistics = TRUE
210
  # )
 
98
  return(gr)
99
  }
100
 
101
+ #' Build a GRanges of protein-coding gene features for peak annotation
102
  #'
103
+ #' Restricts the brentlab feature set to protein-coding genes, excluding
104
+ #' dubious ORFs, and returns a strand-aware GRanges carrying the locus tag
105
+ #' and gene symbol as metadata. Coordinates in the feature table are 1-indexed
106
+ #' closed (SGD convention) and are used directly. Also removes mito genes
107
+ #'
108
+ #' @param features_df Data frame from \code{brentlab_features.parquet} with
109
+ #' columns chr, start, end, strand, type, locus_tag, symbol, note.
110
+ #' @return GRanges of retained gene features with metadata columns
111
+ #' \code{locus_tag} and \code{symbol}.
112
+ prepare_gene_features <- function(features_df) {
113
+
114
+ genes <- features_df |>
115
+ dplyr::filter(type == "gene",
116
+ stringr::str_detect(note, stringr::fixed("dubious", ignore_case = TRUE),
117
+ negate = TRUE),
118
+ chr != "chrM")
119
+
120
+ GenomicRanges::GRanges(
121
+ seqnames = genes$chr,
122
+ ranges = IRanges::IRanges(start = genes$start, end = genes$end),
123
+ strand = genes$strand,
124
+ locus_tag = genes$locus_tag,
125
+ symbol = genes$symbol
126
+ )
127
+ }
128
+
129
+ #' Annotate ChExMix peaks to the nearest downstream gene feature
130
+ #'
131
+ #' Assigns each peak to its nearest downstream feature by genomic coordinate
132
+ #' (strand-agnostic, since peaks carry no strand) and records the gap distance.
133
+ #' This proceeds in two rounds:
134
+ #' \enumerate{
135
+ #' \item Every peak is annotated to the nearest downstream gene and the
136
+ #' distance to that gene is computed. Peaks whose nearest downstream gene
137
+ #' lies farther than \code{nbp} (or have no downstream gene) are dropped.
138
+ #' \item Retained peak-to-gene assignments are aggregated by target gene,
139
+ #' and the median peak score across the set of peaks annotated to a gene is
140
+ #' reported as that gene's \code{peak_score}.
141
+ #' }
142
  #'
143
  #' @param peaks_df Data frame with BED-format peak calls (0-indexed, half-open)
144
  #' and a \code{score} column, as produced by \code{read_in_filtered_bed}.
145
+ #' @param features_gr GRanges of gene features, as produced by
146
+ #' \code{prepare_gene_features}.
147
+ #' @param nbp Maximum allowed distance (bp) between a peak and its nearest
148
+ #' downstream gene for the peak to be retained. Default 500.
149
  #'
150
+ #' @return A tibble with one row per annotated gene and columns
151
+ #' \code{target_locus_tag}, \code{target_symbol}, \code{peak_score} (median
152
+ #' peak score across annotated peaks) and \code{n_peaks} (number of peaks
153
+ #' annotated to the gene).
154
  #'
155
+ #' @seealso \code{\link{bed_to_granges}}, \code{\link{prepare_gene_features}}
156
+ annotate_peaks_to_features <- function(peaks_df, features_gr, nbp = 500) {
157
 
158
  peaks_gr <- bed_to_granges(peaks_df)
159
 
160
+ # round 1: nearest downstream feature (strand-agnostic) and its distance
161
+ ds_idx <- GenomicRanges::precede(peaks_gr, features_gr, ignore.strand = TRUE)
162
+ keep <- !is.na(ds_idx)
163
+
164
+ peaks_gr <- peaks_gr[keep]
165
+ feat_hits <- features_gr[ds_idx[keep]]
166
+
167
+ round1 <- tibble::tibble(
168
+ score = peaks_df$score[keep],
169
+ target_locus_tag = feat_hits$locus_tag,
170
+ target_symbol = feat_hits$symbol,
171
+ distance = GenomicRanges::distance(peaks_gr, feat_hits,
172
+ ignore.strand = TRUE)) |>
173
+ dplyr::filter(distance <= nbp)
174
+
175
+ # round 2: aggregate peaks per target gene, median score as the peak score
176
+ round1 |>
177
+ dplyr::group_by(target_locus_tag, target_symbol) |>
178
+ dplyr::reframe(peak_score = median(score),
179
+ n_peaks = dplyr::n(),
180
+ max_distance = max(distance))
181
  }
182
 
183
 
184
  rossi_meta = arrow::read_parquet("~/code/hf/rossi_2021/rossi_2021_metadata.parquet")
185
  chrmap = read_csv("~/code/hf/yeast_genome_resources/chrmap.csv.gz")
186
 
187
+ brentlab_features = arrow::read_parquet("~/code/hf/yeast_genome_resources/brentlab_features.parquet")
188
 
189
+ # protein-coding gene features used to annotate peaks (excludes dubious ORFs)
190
+ gene_features_gr = prepare_gene_features(brentlab_features)
 
 
 
 
 
 
 
191
 
192
  filtered_bed = list.files(here("data/yep_filtered_peaks"), full.names = TRUE)
193
  names(filtered_bed) = str_extract(basename(filtered_bed), "^\\d+")
 
207
  filtered_bed_df_list_no_null_in_meta = filtered_bed_df_list_no_null[
208
  as.integer(names(filtered_bed_df_list_no_null)) %in% rossi_meta$yeastepigenome_id]
209
 
210
+ # annotate each sample's peaks to nearest downstream genes within 500bp
211
+ annotated = map(filtered_bed_df_list_no_null_in_meta,
212
+ annotate_peaks_to_features, gene_features_gr, nbp = 500)
213
+
214
+ annotated_df = bind_rows(annotated, .id = "yeastepigenome_id") |>
215
+ mutate(yeastepigenome_id = as.integer(yeastepigenome_id)) |>
216
+ left_join(rossi_meta, by = "yeastepigenome_id") |>
217
+ group_by(sample_id, target_locus_tag, target_symbol) |>
218
+ reframe(
219
+ peak_score = median(peak_score),
220
+ n_peaks = sum(n_peaks),
221
+ max_distance = max(max_distance),
222
+ peak_n_replicates = n()) |>
223
+ arrange(desc(peak_score), .by_group = TRUE)
 
 
 
 
 
 
 
 
 
224
 
225
  # arrow::write_parquet(
226
+ # annotated_df,
227
+ # "~/code/hf/rossi_2021/yep_filtered_peaks_combined.parquet",
 
 
 
 
 
 
 
228
  # compression = "zstd",
229
  # write_statistics = TRUE
230
  # )
yep_filtered_peaks_mindel.parquet → yep_filtered_peaks_combined.parquet RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:1f8473129d802f994ddc01c9e2d5d1625da8b541dc3439b7a389397568a93662
3
- size 24383507
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:201410f57fe0368cf14ab1dce12192792293f885517d31f86a07494c9ef86d2a
3
+ size 527546
yep_filtered_peaks_yiming.parquet DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:cbd1506072d97198bce01b497cb93a33a79772843bd6fa22bfe5dd04e3037795
3
- size 30745732