rnabioco / valr

Genome Interval Arithmetic in R
http://rnabioco.github.io/valr/
Other
88 stars 25 forks source link

bed_intersect function convert factor columns to numeric #360

Closed frequena closed 4 years ago

frequena commented 4 years ago

When I use the function "bed_intersect", any factor column in the input is converted automatically to an integer column. The same column as a character type, it does not change.

Example:

library(valr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

tibble(chrom = '1', start = 1, end = 10, pathogenic = factor(c('Yes'))) %>%
  bed_intersect(tibble(chrom = '1', start = 1, end = 5))
#> # A tibble: 1 x 8
#>   chrom start.x end.x pathogenic.x start.y end.y .source .overlap
#>   <chr>   <dbl> <dbl>        <int>   <dbl> <dbl> <chr>      <int>
#> 1 1           1    10            1       1     5 1              4

tibble(chrom = '1', start = 1, end = 10, pathogenic = c('Yes')) %>%
  bed_intersect(tibble(chrom = '1', start = 1, end = 5))
#> # A tibble: 1 x 8
#>   chrom start.x end.x pathogenic.x start.y end.y .source .overlap
#>   <chr>   <dbl> <dbl> <chr>          <dbl> <dbl> <chr>      <int>
#> 1 1           1    10 Yes                1     5 1              4

Created on 2020-04-23 by the reprex package (v0.3.0)

jayhesselberth commented 4 years ago

Thanks for the clear example, this looks like a bug in how factors are handled internally.

kriemo commented 4 years ago

Yes, this is a bug, thanks for bringing this issue to our attention. I see how to fix this but will need to do some testing prior to pushing as it affects some c++ internals.

kriemo commented 4 years ago

This should be fixed on master now. Please reinstall from github to fix this issue. Thanks.

devtools::install_github("rnabioco/valr")
frequena commented 4 years ago

It works! Perfect, thank you very much for your work, valr is really useful!