annotate spocc_occ.R @ 7:a85ff9b9cb09 draft

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