waldronlab / cBioPortalData

Integrate the cancer genomics portal, cBioPortal, using MultiAssayExperiment
https://waldronlab.io/cBioPortalData/
30 stars 12 forks source link

cBioPortalData did not work in custom function #58

Closed hufanglq closed 2 years ago

hufanglq commented 2 years ago

Thanks for good job! I have problem with cBioPortalData, following code works well, but if put in a function, it won't work.

gene1='TP53'; cancer='gbm'
cbio <- cBioPortal()
mycancerstudy <- paste0(tolower(cancer),"_tcga")
mycaselist <- as.data.frame(sampleLists(cbio, mycancerstudy))[2,4]
mygeneticprofile <- paste0(tolower(cancer),"_tcga_rna_seq_v2_mrna")
datax <- cBioPortalData(api = cbio,
                                        studyId=mycancerstudy,genes=gene1,
                                        molecularProfileIds=mygeneticprofile, 
                                        sampleListId = mycaselist,
                                        check_build = F,
                                        by = "hugoGeneSymbol")

like this:

mytfun <- function(gene1='TP53', cancer='gbm'){
cbio <- cBioPortal()
mycancerstudy <- paste0(tolower(cancer),"_tcga")
mycaselist <- as.data.frame(sampleLists(cbio, mycancerstudy))[2,4]
mygeneticprofile <- paste0(tolower(cancer),"_tcga_rna_seq_v2_mrna")
datax <- cBioPortalData(api = cbio,
                                        studyId=mycancerstudy,genes=gene1,
                                        molecularProfileIds=mygeneticprofile, 
                                        sampleListId = mycaselist,
                                        check_build = F,
                                        by = "hugoGeneSymbol")

}
mytfun()

The error message was " Error in FUN(X[[i]], ...) : object 'mygeneticprofile' not found "

LiNk-NY commented 2 years ago

Hi @hufanglq

cBioPortalData is really meant for end-users and not developers. The error is due to the system that allows caching of arguments. The function is meant to run in the .GlobalEnv and not in a function env.

The wrapper function provided here restricts the flexibility in the arguments and I do not recommend this approach.

For advanced low-level usage, see our developer vignette.

Best regards, Marcel