Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safely execute functions like sci2comm and comm2sci #778

Closed
sastoudt opened this issue Oct 4, 2019 · 6 comments
Closed

safely execute functions like sci2comm and comm2sci #778

sastoudt opened this issue Oct 4, 2019 · 6 comments
Milestone

Comments

@sastoudt
Copy link

sastoudt commented Oct 4, 2019

Session Info
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] purrr_0.2.5       taxize_0.9.6      data.table_1.12.2

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2       xml2_1.2.0       magrittr_1.5     ape_5.3          lattice_0.20-35 
 [6] R6_2.4.0         rlang_0.3.4      foreach_1.4.4    stringr_1.4.0    plyr_1.8.4      
[11] tools_3.5.1      parallel_3.5.1   bold_0.8.6       grid_3.5.1       nlme_3.1-137    
[16] iterators_1.0.10 httpcode_0.2.0   reshape2_1.4.3   codetools_0.2-15 triebeard_0.3.0 
[21] curl_4.0         crul_0.7.4       stringi_1.4.3    compiler_3.5.1   urltools_1.7.2  
[26] jsonlite_1.6     reshape_0.8.8    zoo_1.8-5       

When trying to get scientific names for a vector of common names (or vice versa), one bad request ruins the whole function call.

birdNames = c( "stellers jay" ,    "common raven" ,        "american robin",       "red-tailed hawk" )
test <- comm2sci(unique(birdNames))

No ENTREZ API key provided
 Get one via taxize::use_entrez()
See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/

Retrieving data for taxon 'stellers jay'

Not found. Consider checking the spelling or alternate classification
No ENTREZ API key provided
 Get one via taxize::use_entrez()
See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
Error: Bad Request (HTTP 400)

Could the purrr::safely function be wrapped into the function calls to avoid this behavior? Or if you don't want to mess with the core functionality, providing an example of this work around in the documentation could be helpful.

library(purrr)
safe_sciN <- safely(comm2sci)

birdScientific = birdNames %>%  map(safe_sciN)
birdScientificNice = lapply(birdScientific, function(x){x$result[[1]]}) ## grab the result instead of the error, this step could be streamlined with purrr too
birdScientificNice = lapply(birdScientificNice, function(x){ifelse(length(x)==0, NA, x)}) ## add an NA if empty so it keeps the vector size the same, this step could be streamlined with purrr too
birdScientificNice = unlist(birdScientificNice)
birdScientificNice 
## [1] NA                   "Corvus corax"       "Turdus migratorius" "Buteo jamaicensis" 
@sckott
Copy link
Contributor

sckott commented Oct 5, 2019

thanks for this @sastoudt having a look

@sckott sckott added this to the v0.9.9 milestone Oct 5, 2019
@sckott
Copy link
Contributor

sckott commented Oct 5, 2019

Those were good suggestions, but i swapped out the stop on nothing found line of code for just returning an empty result. Can you reinstall and try again?

@sastoudt
Copy link
Author

sastoudt commented Oct 5, 2019

Thanks for taking a look so quickly! I re-installed and am getting an error (but a new one):

birdNames = c( "stellers jay" ,    "common raven" ,        "american robin",       "red-tailed hawk" )
test <- comm2sci(unique(birdNames))

No ENTREZ API key provided
 Get one via taxize::use_entrez()
See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
══  1 queries  ═══════════════

Retrieving data for taxon 'stellers jay'

Not found. Consider checking the spelling or alternate classification
══  Results  ═════════════════

● Total: 1Found: 0Not Found: 0
No ENTREZ API key provided
 Get one via taxize::use_entrez()
See https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/
Error: Bad Request (HTTP 400)

@sckott
Copy link
Contributor

sckott commented Oct 9, 2019

First, it's a good idea to get your own Entrez key. I dont think that's the issue here though.

Did you make sure to restart your R session after installing the new version? Sorry to ask, but many people do not think to do this.

@sastoudt
Copy link
Author

sastoudt commented Oct 9, 2019

Oops, you called it. I did forget to restart my R session. Now it works. Thanks! Can the same fix be made for sci2comm?

@sckott
Copy link
Contributor

sckott commented Oct 9, 2019

great, glad it worked!

i'll have a look at sci2comm

@sckott sckott closed this as completed in d3762bf Oct 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants