comparison FunctExeCalcPresAbsGalaxy.r @ 3:8d8aec182fb1 draft

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