ropensci / traits

R package for accessing species trait data from multiple databases
Other
41 stars 13 forks source link

betydb_traits function does not use genus, species arguments #21

Closed dlebauer closed 7 years ago

dlebauer commented 9 years ago

When I pass different genus and species arguments to the betydb_traits function, the results should be different.

Specifically, there should be no overlap in the result sets since the results will be associated with one species or the other but not both.

The functions betydb_traits and betydb_yields are currently commented out in the source code (end of R/betydb.R) because they don't work, but the following should work:

## betydb_yields() works for genus, not for genus + species

betydb_yields <- function(genus = NULL, species = NULL, fmt = "json", key=NULL, user=NULL, pwd=NULL, ...){
  args <- traitsc(list(genus = genus, species = species))
  url <- makeurl("yields", fmt)
  betydb_GET(url = url, args, key, user, pwd, "trait", ...)
}

## works
betydb_yields(genus = "Miscanthus")
## doesn't work: Server Error 500
betydb_yields(genus = "Miscanthus", species = 'giganteus')

## betydb_traits does not work
betydb_traits <- function(genus = NULL, species = NULL, trait = NULL, author = NULL, fmt = "json", key=NULL, user=NULL, pwd=NULL, ...){
  args <- traitsc(list(species.genus = genus, species.species = species, variables.name = trait))
  url <- makeurl("traits", fmt)
  betydb_GET(url = url, args, key, user, pwd, "trait", ...)
}

betydb_traits(genus = "Miscanthus")

Errors presumably related to composing valid url's (see traceback, below). The Url-based API is defined in section 3 of the "Data Access" documentation (https://www.authorea.com/users/5574/articles/7062/_show_article). But the following should are valid urls:

These API calls work:

and can also return contents of site, species, citations, treatments tables by appending &include[]=specie&include[]=site

I've added a test that should pass (currently wrapped in expect_error, as they give errors): if the genus and species arguments are used in the query, the results should be different. The test doesn't currently pass because the results are not filtered by genus / species, so I have commented it out.

  mxg <- betydb_traits(genus = 'Miscanthus', species = "giganteus", 
                                user = "ropensci-traits", pwd = "ropensci")
  pavi <- betydb_traits(genus = 'Panicum', species = "virgatum",
                                user = "ropensci-traits", pwd = "ropensci")
  expect_false(mxg == pavi)

... the current error / traceback is goes to constructing the url

> betydb_traits(genus = "Acer", species = 'rubrum')
Error in betydb_http(url, args, key, user, pwd, ...) : 
  server error: (500) Internal Server Error
> traceback()
5: stop(http_condition(x, "error", call = sys.call(-1)))
4: stop_for_status(res) at betydb.R#106
3: betydb_http(url, args, key, user, pwd, ...) at betydb.R#69
2: betydb_GET(url = url, args, key, user, pwd, "trait", ...) at #4
1: betydb_traits(genus = "Acer", species = "rubrum")

However, it would be faster to pull meta-data records and do joins on the client side, e.g.

acru <- betydb_search("Acer Rubrum")
acru_sites <- betydb_site(id = acru$site_id)
acru_citations <- betydb_citations(id = acru$citation_id)

acru %>% left_join(acru_sites, by = 'site_id') %>% left_join(acru_citations, by = 'citation_id')

> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

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] pander_0.5.2      dplyr_0.4.3       traits_0.1.1.9000 testthat_0.10.0  
[5] devtools_1.8.0   

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.6      git2r_0.10.1     plyr_1.8.3       iterators_1.0.7 
 [5] tools_3.2.1      digest_0.6.8     jsonlite_0.9.17  memoise_0.2.1   
 [9] nlme_3.1-121     lattice_0.20-33  foreach_1.4.2    rstudioapi_0.3.1
[13] DBI_0.3.1        curl_0.9.1       parallel_3.2.1   httr_1.0.0      
[17] stringr_1.0.0    roxygen2_4.1.1   xml2_0.1.1       knitr_1.10.5    
[21] rversions_1.0.2  grid_3.2.1       reshape_0.8.5    data.table_1.9.4
[25] R6_2.1.1         XML_3.98-1.3     reshape2_1.4.1   magrittr_1.5    
[29] codetools_0.2-14 assertthat_0.1   bold_0.2.6       taxize_0.6.2    
[33] ape_3.3          stringi_0.5-5    openssl_0.4      lazyeval_0.1.10 
[37] chron_2.3-47     crayon_1.3.1   
sckott commented 9 years ago

@dlebauer i don't quite understand what you mean, can you clarify?

dlebauer commented 9 years ago

The expect_false(mxg == pavi) test fails.

I would expect different results when querying data for different species. I assume the command

betydb_traits(genus = 'Panicum', species = "virgatum")

To be (roughly) equivalent to the sql query:

select * from traits where genus = 'Panicum' and species = 'virgatum';
dlebauer commented 9 years ago

Seems to be the API, so I submitted the bug PecanProject/bety#224

Next time I will be more careful :boom:

sckott commented 9 years ago

was just testing, seems to be the API gives the same results

sckott commented 7 years ago

@dlebauer can this be closed?

dlebauer commented 7 years ago

Yes it can be closed On Tue, Mar 21, 2017 at 3:34 PM Scott Chamberlain notifications@github.com wrote:

@dlebauer https://github.com/dlebauer can this be closed?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ropensci/traits/issues/21#issuecomment-288193483, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcX5yYwxmcmeX_sewqOfL6zkItIbtTWks5roCZOgaJpZM4Dd_NG .

sckott commented 7 years ago

thanks @dlebauer