Skip to content

Instantly share code, notes, and snippets.

@Puriney
Created July 30, 2014 17:03
Show Gist options
  • Save Puriney/db8d6e0d91bcd95e355d to your computer and use it in GitHub Desktop.
Save Puriney/db8d6e0d91bcd95e355d to your computer and use it in GitHub Desktop.
R: GenomicRanges elementMetadata add column
library(GenomicRanges)
sampleChr <- paste0("Chr", 1:3)
sampleStarts <- sample(1:100, size = 3, replace = F)
sampleEnds <- sampleStarts + 10
sampleStrand <- sample(c("+", "-"), size = 3, replace = T)
sampleValue <- rnorm(3)
sampleGR1 <- GRanges(seqnames = Rle(sampleChr), ranges = IRanges(sampleStarts, sampleEnds), strand = Rle(sampleStrand), value = sampleValue)
sampleGR2 <- GRanges(seqnames = Rle(sampleChr), ranges = IRanges(sampleStarts + 10 , sampleEnds + 100), strand = Rle(sampleStrand), value = sampleValue)
sampleGR <- c(sampleGR1, sampleGR2)
testGR1 <- GRanges(seqnames = Rle(sampleChr), ranges = IRanges(sampleStarts, sampleEnds), strand = Rle(sampleStrand), value1 = sampleValue * 100, value2 = sampleValue + 100)
testGR2 <- GRanges(seqnames = Rle(sampleChr), ranges = IRanges(sampleStarts, sampleEnds), strand = Rle(sampleStrand), value3 = sampleValue / 100)
id <- na.omit(match(sampleGR, testGR1))
values(sampleGR) <- cbind(values(sampleGR), elementMetadata(testGR1)[id, ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment