Last active
October 21, 2020 09:27
-
-
Save stephaniehicks/7bbb1955e58de3e1f0ea1597ec833a79 to your computer and use it in GitHub Desktop.
Demo for the Introduction to GenomicRanges and plyranges by Stephanie Hicks (2020-10-06)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(GenomicRanges) | |
library(plyranges) | |
## Create a GRanges object | |
gr <- GRanges(seqnames = "chr1", | |
strand = c("+", "-"), | |
ranges = IRanges(start = c(102012,520211), | |
end=c(120303, 526211)), | |
gene_id = c(1001,2151), | |
score = c(10, 25)) | |
gr | |
## Create a new IRanges object | |
ir_new <- IRanges(start = c(1:3),end=c(5,2,8)) | |
ir_new | |
## Modify (or mutate) a genomic interval by altering the width | |
## Use the mutate() verb with anchor_*() adverbs | |
mutate(ir_new, width = 10) | |
mutate(anchor_start(ir_new), width = 10) | |
mutate(anchor_end(ir_new), width = 10) | |
## Anchoring fixes either the start, end, center coordinates | |
## of the GRange object. | |
## Can be used in combination with either mutate or stretch | |
## Stretch a genomic interval by an intger amount | |
ir_new | |
stretch(anchor_center(ir_new), extend = 10) | |
## Shift (left and right) | |
ir_new | |
shift_left(ir_new, shift = 10) | |
shift_right(ir_new, shift = 10) | |
## Create a GRanges object | |
gr <- | |
data.frame(seqnames = sample(c("chr1", "chr2"), 7, replace = TRUE), | |
strand = sample(c("+", "-"), 7, replace = TRUE), | |
score = runif(7), | |
start = 1:7, | |
width = 10) %>% | |
as_granges() | |
gr | |
## Summarizing by group | |
gr %>% | |
group_by(strand) %>% | |
summarize(mean_score = mean(score)) | |
## Filtering by group | |
gr %>% | |
group_by(strand) %>% | |
filter(score == max(score)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment