It's also possible to extract subsets of dataframes or matrices in the same way by taking advantage of GRanges that are part of other objects. In the following example, we create a matrix of random data and use that to build a SummarizedExperiment object that uses a GRanges object to describe its rows:
set.seed(4321)
experiment_counts <- matrix( runif(4308 * 6, 1, 100), 4308)
sample_names <- c(rep("ctrl",3), rep("test",3) )
se <- SummarizedExperiment::SummarizedExperiment(rowRanges = gr_from_txt, assays = list(experiment_counts), colData = sample_names)
Then, we can subset in the same way as before and get back a subset of the data as well as a subset of the ranges. The assay() function returns the actual data matrix:
overlap_hits <- findOverlaps(region_of_interest_gr, se)
data_in_region <- se[subjectHits(overlap_hits) ]
assay(data_in_region)
This will give the resultant output:
## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 69.45349 90.44524 88.33501 60.87932 86.24007 45.64919