Skip to content

From sample peaks to consensus regions

Differential accessibility requires one region definition for every sample. Independent sample peak lists cannot serve as columns of a count matrix because the intervals differ. This workflow merges their overlapping or adjacent intervals and retains a merged region when at least two donors support it within either condition.

Separate a peak call from a differential result

Section titled “Separate a peak call from a differential result”

MACS2 calls narrow peaks on each cleaned sample BAM with a q-value threshold of 0.01. Paired-end mode uses fragment information. The workflow does not apply a second Tn5 shift as a fragment correction. A sample peak identifies local enrichment under the caller’s model; it is not a test of stimulated versus resting accessibility.

For paired records, the MACS2 BAMPE parser requires the SAM proper pair flag. The orphan helper checks chromosome, strand, and coordinate order without requiring that flag. Its retained pair total can therefore exceed MACS2’s parsed fragment total.

The consensus rule admits regions detected in only one condition when that condition has support from at least two donors. Requiring support in both conditions would remove condition-specific regions before their counts could be compared. Pooling the eight samples into one apparent replicate would also conceal donor support.

Merged regions can become wider than their constituent peaks. Chained overlaps can join nearby signal into one interval, so a consensus count describes the whole merged interval rather than a single summit. The width distribution helps identify that consequence of the chosen region definition.

Recalculate support from the original peak intervals

Section titled “Recalculate support from the original peak intervals”

The pipeline’s boolean table has a detail that affects interpretation. It records a sample as present only when that sample’s condition meets the replicate threshold in the merged region. A single peak in the other condition can therefore have a false entry despite a real overlap.

Both examples first calculate raw sample support from the peak intervals. They then apply the within-condition threshold and compare the result with the pipeline table. The preparation also marks autosomal regions and any overlap with hg19 blacklist v2. Those region flags define downstream eligibility in addition to the pipeline’s alignment filters.

library(GenomicRanges)
library(jsonlite)
metadata <- read.delim("/fixtures/metadata.tsv", colClasses = "character")
metadata <- metadata[order(metadata$donor, metadata$condition), ]
regions <- read.delim("/fixtures/regions.tsv")
sample_peaks <- read.delim(gzfile("/fixtures/sample_peaks.tsv.gz"))
pipeline_support <- read.delim("/fixtures/pipeline_support.tsv",
row.names = 1, check.names = FALSE)
stopifnot(identical(rownames(pipeline_support), regions$region_id))
stopifnot(setequal(names(pipeline_support), metadata$sample_id))
stopifnot(all(regions$start >= 0L), all(regions$end > regions$start))
stopifnot(all(sample_peaks$start >= 0L))
stopifnot(all(sample_peaks$end > sample_peaks$start))
consensus_ranges <- GRanges(regions$chrom,
IRanges(regions$start + 1L, regions$end))
sample_ranges <- GRanges(sample_peaks$chrom,
IRanges(sample_peaks$start + 1L, sample_peaks$end))
overlaps <- findOverlaps(consensus_ranges, sample_ranges,
ignore.strand = TRUE)
support <- matrix(FALSE, nrow(regions), nrow(metadata),
dimnames = list(regions$region_id, metadata$sample_id))
sample_index <- match(sample_peaks$sample_id[subjectHits(overlaps)],
metadata$sample_id)
support[cbind(queryHits(overlaps), sample_index)] <- TRUE
resting <- metadata$condition == "resting"
stimulated <- metadata$condition == "stimulated"
resting_support <- rowSums(support[, resting, drop = FALSE])
stimulated_support <- rowSums(support[, stimulated, drop = FALSE])
stopifnot(all(resting_support >= 2L | stimulated_support >= 2L))
qualified <- support
qualified[, resting] <- support[, resting] & resting_support >= 2L
qualified[, stimulated] <- support[, stimulated] & stimulated_support >= 2L
stopifnot(identical(unname(qualified),
unname(as.matrix(pipeline_support[, metadata$sample_id]))))
support_summary <- data.frame(region_id = regions$region_id,
resting = resting_support,
stimulated = stimulated_support)
eligible <- regions$autosomal & !regions$blacklist_overlap
widths <- regions$end - regions$start
cat(sprintf("Sample peaks: %d; consensus regions: %d\n",
nrow(sample_peaks), nrow(regions)))
cat(sprintf("Eligible autosomal regions: %d; median consensus width: %.1f bp\n",
sum(eligible), median(widths)))
cat(sprintf("Raw overlaps masked by the condition threshold: %d\n",
sum(support & !qualified)))
for (sample_id in metadata$sample_id) {
cat(sprintf("%s called_peaks=%d consensus_overlaps=%d\n", sample_id,
sum(sample_peaks$sample_id == sample_id),
sum(support[, sample_id])))
}
dir.create("outputs", showWarnings = FALSE)
write.table(data.frame(region_id = regions$region_id, support,
check.names = FALSE), "outputs/support_r.tsv",
sep = "\t", quote = FALSE, row.names = FALSE)
write.table(support_summary, "outputs/support_summary_r.tsv", sep = "\t",
quote = FALSE, row.names = FALSE)
write_json(list(sample_peaks = nrow(sample_peaks), regions = nrow(regions),
eligible = sum(eligible), median_width = median(widths),
masked_overlaps = sum(support & !qualified)),
"outputs/metrics_r.json", auto_unbox = TRUE, digits = 16)
png("outputs/peak_consensus_r.png", width = 1400, height = 650, res = 160)
par(mfrow = c(1, 2), mar = c(4.5, 4.5, 1.5, 1))
hist(log10(widths), breaks = 40, col = "#87b9c1", border = "white",
main = "", xlab = "log10 consensus width in bases", ylab = "Regions")
support_table <- table(factor(resting_support, levels = 0:4),
factor(stimulated_support, levels = 0:4))
image(0:4, 0:4, log10(support_table + 1),
col = hcl.colors(30, "Blues 3", rev = TRUE),
xlab = "Resting donors with a peak",
ylab = "Stimulated donors with a peak")
for (i in 0:4) {
for (j in 0:4) {
text(i, j, support_table[i + 1, j + 1], cex = 0.75)
}
}
invisible(dev.off())
Sample peaks: 298076; consensus regions: 52518
Eligible autosomal regions: 50599; median consensus width: 566.0 bp
Raw overlaps masked by the condition threshold: 7188
resting_REP1 called_peaks=30997 consensus_overlaps=28237
stimulated_REP1 called_peaks=54569 consensus_overlaps=46961
resting_REP2 called_peaks=11595 consensus_overlaps=10762
stimulated_REP2 called_peaks=49633 consensus_overlaps=43865
resting_REP3 called_peaks=40601 consensus_overlaps=32039
stimulated_REP3 called_peaks=43797 consensus_overlaps=39806
resting_REP4 called_peaks=25668 consensus_overlaps=23496
stimulated_REP4 called_peaks=41216 consensus_overlaps=34268

The figure shows consensus widths and raw donor support.

Carry the region definition into interpretation

Section titled “Carry the region definition into interpretation”

The completed consensus contains 52,518 regions, of which 50,599 pass the autosomal and blacklist criteria. Every stimulated profile has more called peaks than its paired resting profile. That asymmetry depends on enrichment and detection under the peak model; it does not establish an absolute increase in accessible DNA. Resting REP2 contributes fewer peaks alongside the weak enrichment documented in the QC lesson.

Two donors provide the minimum support for admission, but that rule is not an estimate of reproducibility across a larger population. The count model still needs the eight sample measurements and the donor terms. A region supported by only one condition can have nonzero counts in every sample, because a peak-calling threshold does not define the absence of fragments.

The count lesson applies the declared autosomal and count filters to this consensus. The annotation lesson retains its full intervals when it calculates promoter overlap and nearest-TSS distance.

The GenomicRanges documentation provides the interval and overlap methods used to reconstruct sample support. Read the GRanges and findOverlaps help when you compare inclusive R intervals with BED coordinates. The MACS2 v2.2.7.1 paired-end parser shows the proper-pair requirement behind the fragment-count comparison.