annotate spocc_occ.R @ 1:f9d76a46799a draft

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/spocc commit 8f11ecf7d465ef7b326bab46d8c8402c85cfed77
author ecology
date Fri, 14 Jun 2019 09:00:58 -0400
parents 724ad06de5a5
children f64b3c1a80a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
1 #!/usr/bin/Rscript
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
2
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
3 library(spocc)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
4
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
5
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
6 ##Def functions :
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
7 help<-function(){
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
8 cat("HELP\n")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
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")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
10 cat("Need 3 args :\n")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
11 cat(" - query : (character) One to many scientific names.\n")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
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")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
13 cat(" - limit : (numeric) Number of records to return. This is passed across all sources.\n")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
14 q("no")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
15 }
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
16
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
17 formatSpName <- function(spName) paste(strsplit(spName, split=' ')[[1]], collapse='_') ###Wallace function
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
18
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
19 ####################################
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
20
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
21 args = commandArgs(trailingOnly=TRUE)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
22
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
23 #Help display
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
24 if(args[1]=="-h" || args[1]=="--help" || length(args)<3){help()}
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
25
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
26 #Get args
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
27 sname<-args[1]
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
28 dbase_input<-args[2]
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
29 max<-as.integer(args[3])
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
30
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
31 #Get all databases
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
32 bases<-strsplit(dbase_input,",")
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
33 dbase<-c()
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
34 for (base in bases){
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
35 dbase<-c(dbase,base)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
36 }
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
37
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
38 #Get occurrences
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
39 results <- spocc::occ(query=sname, from=dbase, limit=max, has_coords=TRUE)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
40
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
41 #Dispay results
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
42 if(length(dbase)==1){
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
43 results_data <- results[[dbase[1]]]$data[[formatSpName(sname)]]
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
44 }else{
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
45 res <- occ2df(results)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
46 results_data <- res
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
47 }
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
48
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
49 results_data<-as.matrix(results_data)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
50
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
51 #If empty
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
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")}
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
53
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
54 #Write them
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
55 write.table(file="output.tab",results_data,sep="\t",row.names=FALSE)
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
56
724ad06de5a5 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tools/spocc_occ commit e37f100e1c3ea68ade708868f9de3f4c0dd7e720
ecology
parents:
diff changeset
57 q('no')