distanceToNearest
1
0
Entering edit mode
@f0d6de0f
Last seen 3 months ago
United Kingdom

Hi,

I invoked the following command:

dist <- distanceToNearest(rt.gr, mop, ignore.strand = TRUE)

Note: rt.gr and mop are granges

I found out that NROW(dist) is one row shorter than NROW(rt.gr), meaning thate is one range in rt.gr that does not have a corresponding nearest range in mop. How do I find the missing row equivalent in rt.gr? Possibly did not return a value? I have checked ranges(rt.gr) for non-numeric values and did not find any.

Thanks

rtracklayer ranges distanceToNearest • 493 views
ADD COMMENT
0
Entering edit mode

Will you provide a self-contained example that shows the issue? It's hard to respond otherwise.

ADD REPLY
0
Entering edit mode

After issuing this cmd:

dist <- distanceToNearest(rt.gr, mop, ignore.strand = TRUE)

I proceeded to do the following:

rt.gr$dat_dist <- mcols(dist)$distance

Then I got the following error:

Error in `[[<-`(`*tmp*`, name, value = c(3344L, 2127L, 24L, 183L, 255L,  : 
158611 elements in value to replace 158612 elements
ADD REPLY
0
Entering edit mode
@james-w-macdonald-5106
Last seen 1 day ago
United States

You shouldn't expect that the length of dist will be the same as the length as your rt.gr object. Also, by 'self-contained' example, I meant something that other people could run. Just showing your code is not self-contained because I don't have your data. But here is an example.

## run this first
> example(distanceToNearest) 
> query
GRanges object with 2 ranges and 0 metadata columns:
      seqnames    ranges strand
         <Rle> <IRanges>  <Rle>
  [1]        A         1      *
  [2]        B         5      *
  -------
  seqinfo: 2 sequences from an unspecified genome; no seqlengths
> subject
GRanges object with 3 ranges and 0 metadata columns:
      seqnames    ranges strand
         <Rle> <IRanges>  <Rle>
  [1]        A      6-10      *
  [2]        A      5-10      *
  [3]        A     13-15      *
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths

## this will return a Hits object that conforms to the 'subject' GRanges object
> distanceToNearest(subject, query)
Hits object with 3 hits and 1 metadata column:
      queryHits subjectHits |  distance
      <integer>   <integer> | <integer>
  [1]         1           1 |         4
  [2]         2           1 |         3
  [3]         3           1 |        11
  -------
  queryLength: 3 / subjectLength: 2

## this will return a Hits object that DOES NOT conform to the 'query' GRanges object.
> distanceToNearest(query,subject)
Hits object with 1 hit and 1 metadata column:
      queryHits subjectHits |  distance
      <integer>   <integer> | <integer>
  [1]         1           2 |         3
  -------
  queryLength: 2 / subjectLength: 3
1
Entering edit mode

But do note that

> z <- distanceToNearest(query,subject)
> query$distance <- NA
> query$distance[queryHits(z)] <- mcols(z)$distance
> query
GRanges object with 2 ranges and 1 metadata column:
      seqnames    ranges strand |  distance
         <Rle> <IRanges>  <Rle> | <integer>
  [1]        A         1      * |         3
  [2]        B         5      * |      <NA>
  -------
  seqinfo: 2 sequences from an unspecified genome; no seqlengths
>
ADD REPLY
1
Entering edit mode

Also with plyranges:

> query %>% add_nearest_distance(subject)
GRanges object with 2 ranges and 1 metadata column:
      seqnames    ranges strand |  distance
         <Rle> <IRanges>  <Rle> | <integer>
  [1]        A         1      * |         3
  [2]        B         5      * |      <NA>
  -------
  seqinfo: 2 sequences from an unspecified genome; no seqlengths
ADD REPLY
0
Entering edit mode

Thanks Michael. It's good to know this alternative way.

ADD REPLY
0
Entering edit mode

Thanks James. I really appreciate your explanations.

ADD REPLY

Login before adding your answer.

Traffic: 568 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6