comparison FunctExeCalcPresAbsGalaxy.r @ 0:c9dfe4e20a45 draft

"planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 07f1028cc764f920b1e6419c151f04ab4e3600fa"
author ecology
date Tue, 21 Jul 2020 05:59:49 -0400
parents
children 8d8aec182fb1
comparison
equal deleted inserted replaced
-1:000000000000 0:c9dfe4e20a45
1 #Rscript
2
3 #####################################################################################################################
4 #####################################################################################################################
5 ############################ Calculate presence absence table from observation data #################################
6 #####################################################################################################################
7 #####################################################################################################################
8
9 ###################### Packages
10 suppressMessages(library(tidyr))
11
12 ###################### Load arguments and declaring variables
13
14 args = commandArgs(trailingOnly=TRUE)
15 #options(encoding = "UTF-8")
16
17 if (length(args) < 2) {
18 stop("At least one argument must be supplied, an input dataset file (.tabular).", call.=FALSE) #si pas d'arguments -> affiche erreur et quitte / if no args -> error and exit1
19
20 } else {
21 Importdata<-args[1] ###### Nom du fichier importé avec son extension / file name imported with the file type ".filetype"
22 source(args[2]) ###### Import functions
23
24 }
25 #### Data must be a dataframe with at least 3 variables : unitobs representing location and year ("observation.unit"), species code ("species.code") and abundance ("number")
26
27
28 #Import des données / Import data
29 obs<- read.table(Importdata,sep="\t",dec=".",header=TRUE,encoding="UTF-8") #
30 obs[obs == -999] <- NA
31 factors <- fact.det.f(Obs=obs)
32 ObsType <- def.typeobs.f(Obs=obs)
33 obs <- create.unitobs(data=obs)
34
35 vars_data<-c("observation.unit","species.code","number")
36 err_msg_data<-"The input dataset doesn't have the right format. It need to have at least the following 3 variables :\n- observation.unit (or point and year)\n- species.code\n- number\n"
37 check_file(obs,err_msg_data,vars_data,3)
38
39
40 ####################################################################################################
41 #################### Create presence/absence table ## Function : calc.presAbs.f ####################
42 ####################################################################################################
43
44 calc.presAbs.f <- function(Data,
45 nbName="number")
46 {
47 ## Purpose: Compute presence absence
48 ## ----------------------------------------------------------------------
49 ## Arguments: Data : temporary metrics table
50 ## nbName : name of abundance column
51 ##
52 ## Output: presence absence vector
53 ## ----------------------------------------------------------------------
54 ## Author: Yves Reecht, Date: 20 déc. 2011, 12:04 modified by Coline ROYAUX 04 june 2020
55
56 ## Presence - absence :
57 presAbs <- integer(nrow(Data))
58 presAbs[Data[ , nbName] > 0] <- as.integer(1)
59 presAbs[Data[ , nbName] == 0] <- as.integer(0)
60
61 return(presAbs)
62 }
63
64
65 ################# Analysis
66
67 res <- calc.numbers.f(obs, ObsType=ObsType , factors=factors, nbName="number")
68 res$pres.abs <- calc.presAbs.f(res, nbName="number")
69 res <- create.year.point(res)
70
71 #Save dataframe in a tabular format
72 filenamePresAbs <- "TabPresAbs.tabular"
73 write.table(res, filenamePresAbs, row.names=FALSE, sep="\t", dec=".",fileEncoding="UTF-8")
74 cat(paste("\nWrite table with presence/absence. \n--> \"",filenamePresAbs,"\"\n",sep=""))
75