PoisonAlien / maftools

Summarize, Analyze and Visualize MAF files from TCGA or in-house studies.
http://bioconductor.org/packages/release/bioc/html/maftools.html
MIT License
442 stars 218 forks source link

Scoping for custom query in `subsetMaf` #578

Closed harrig12 closed 4 years ago

harrig12 commented 4 years ago

Enjoying maftools. Excellent package, thank you!

I have an issue that I think may have more to do with the defaults in eval/parse than subsetMaf, but it is in using subsetMaf that I noticed this, and it is a bit inconvenient.

Seems that there is something odd about the behavior of the environment that a custom query is evaluated in. Using subsetMaf in the global environment, there is no problem, but from within a function it can't "see" an object that should be available to it, passed through a function.

MWE

library(maftools)
laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools")
laml <- read.maf(maf = laml.maf)

# In global env this works fine
classes = 'Splice_Site'
subsetMaf(maf = laml, query = "Variant_Classification %in% classes")

# But within my own function, I get an error...
myFun <- function(maf, myClasses){
    subsetted_maf <- subsetMaf(maf, query = "Variant_Classification %in% myClasses")
    return(subsetted_maf)
}

myFun(laml, classes)

Here's the output

> myFun(laml, classes)
Error in eval(stub[[3L]], x, enclos) : object 'myClasses' not found

Session info

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-conda_cos6-linux-gnu (64-bit)
Running under: Ubuntu 16.04.6 LTS

Matrix products: default
BLAS/LAPACK: /home/cait/miniconda3/envs/mtordash/lib/libmkl_rt.so

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
 [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] maftools_2.4.05

loaded via a namespace (and not attached):
 [1] compiler_4.0.2     Matrix_1.2-18      RColorBrewer_1.1-2 survival_3.2-3
 [5] R.methodsS3_1.8.0  splines_4.0.2      grid_4.0.2         data.table_1.13.0
 [9] R.utils_2.9.2      R.oo_1.23.0        lattice_0.20-41
PoisonAlien commented 4 years ago

Hi, Thanks for using maftools and I am glad that you find it useful. Regarding the issue I can not think of any workarounds for it. It seems to be an issue with cascading environments and scope of variables. I will read more and see if I can find any solution.

ShixiangWang commented 4 years ago

@harrig12 Here I put a workaound, hope it helps

library(maftools)
laml.maf <- system.file("extdata", "tcga_laml.maf.gz", package = "maftools")
laml <- read.maf(maf = laml.maf)

# In global env this works fine
classes = c('Splice_Site', "Frame_Shift_Ins")
subsetMaf(maf = laml, query = "Variant_Classification %in% classes")

myFun <- function(maf, myClasses){
  myClasses <- deparse(dput(myClasses))
  subsetted_maf <- subsetMaf(maf, query = paste0("Variant_Classification %in% ", myClasses))
  return(subsetted_maf)
}

z = myFun(laml, classes)
table(z@data$Variant_Classification)
PoisonAlien commented 4 years ago

That's a pretty neat trick!

PoisonAlien commented 4 years ago

I am closing this issue for now since @ShixiangWang answer seems to work. Please feel free to reopen if necessary.