Mercurial > repos > mnhn65mo > spocc_occ
comparison spocc_occ.R @ 0:8852afebb8f6 draft
Uploaded
author | mnhn65mo |
---|---|
date | Thu, 02 Aug 2018 04:33:51 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8852afebb8f6 |
---|---|
1 #!/usr/bin/Rscript | |
2 | |
3 library(spocc) | |
4 | |
5 | |
6 ##Def functions : | |
7 help<-function(){ | |
8 cat("HELP\n") | |
9 cat("Spocc::occ, Search on a single species name, or many. And search across a single or many data sources.\n\n") | |
10 cat("Need 3 args :\n") | |
11 cat(" - query : (character) One to many scientific names.\n") | |
12 cat(" - from : (character) Data source to get data from, any combination of gbif, bison, inat,ebird, ecoengine and/or vertnet.\n") | |
13 cat(" - limit : (numeric) Number of records to return. This is passed across all sources.\n") | |
14 q("no") | |
15 } | |
16 | |
17 formatSpName <- function(spName) paste(strsplit(spName, split=' ')[[1]], collapse='_') ###Wallace function | |
18 | |
19 #################################### | |
20 | |
21 args = commandArgs(trailingOnly=TRUE) | |
22 | |
23 #Help display | |
24 if(args[1]=="-h" || args[1]=="--help" || length(args)<3){help()} | |
25 | |
26 #Get args | |
27 sname<-args[1] | |
28 dbase_input<-args[2] | |
29 max<-as.integer(args[3]) | |
30 | |
31 #Get all databases | |
32 bases<-strsplit(dbase_input,",") | |
33 dbase<-c() | |
34 for (base in bases){ | |
35 dbase<-c(dbase,base) | |
36 } | |
37 | |
38 #Get occurrences | |
39 results <- spocc::occ(query=sname, from=dbase, limit=max, has_coords=TRUE) | |
40 | |
41 #Dispay results | |
42 if(length(dbase)==1){ | |
43 results_data <- results[[dbase[1]]]$data[[formatSpName(sname)]] | |
44 }else{ | |
45 res <- occ2df(results) | |
46 results_data <- res | |
47 } | |
48 | |
49 results_data<-as.matrix(results_data) | |
50 | |
51 #If empty | |
52 if(length(results_data)==0){cat("\nNo occurrences found.\nLittle tip : Check your input typo, some databases are case sensitive : Genus species.\n")} | |
53 | |
54 #Write them | |
55 write.table(file="output.tab",results_data,sep="\t",row.names=FALSE) | |
56 | |
57 q('no') |