distanceToNearest returns a Hits object. Is there a simpler way to extract the distance column from this rather than turning it into a data.frame and then selecting the column?
Example:
gr1 =GRanges(seqnames=c("chr1","chr2","chr2"),
ranges=IRanges(start=c(50,150,200),end=c(100,200,300)),
strand=c("+","-","-"))
gr2 =GRanges(seqnames=c("chr1","chr2","chr2"),
ranges=IRanges(start=c(175,250,400),end=c(225,375,500)),
strand=c("+","-","-"))
hits <- distanceToNearest(gr1,gr2)
hits$distance
## Error in hits$distance : $ operator not defined for this S4 class
as.data.frame(hits)$distance
## [1] 74 49 0
sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats4 parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] GenomicRanges_1.18.4 GenomeInfoDb_1.2.4 IRanges_2.0.1 S4Vectors_0.4.0 BiocGenerics_0.12.1
loaded via a namespace (and not attached):
[1] tools_3.1.2 XVector_0.6.0
queryHits and subjectHits can each be accessed with a single function. Is there a similar single operation I can do to extract the distance? I imagine most people want to access the distance when using distanceToNearest, or they'd just use nearest, so this would be a useful thing to have if it doesn't exist.
Thank you! Knowing that it's a metadata column it makes more sense now, and the new display will definitely make this clearer.