annotate FunctExeCalcCommIndexesGalaxy.r @ 7:c67ece97d4b1 draft default tip

"planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 65ab5b6fe84871db0fe18244d805cea19a44e830"
author ecology
date Sat, 26 Jun 2021 07:19:51 +0000
parents 2cd0a5a321c2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
1 #Rscript
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
2
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
3 #####################################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
4 #####################################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
5 ################################# Calculate community indexes from observation data #################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
6 #####################################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
7 #####################################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
8
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
9 ###################### Packages R
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
10
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
11 suppressMessages(library(tidyr))
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
12
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
13 ###################### Load arguments and declaring variables
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
14
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
15 args <- commandArgs(trailingOnly = TRUE)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
16
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
17 if (length(args) < 4) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
18 stop("At least one argument must be supplied, an input dataset file (.tabular).", call. = FALSE) # if no args -> error and exit1
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
19
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
20 } else {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
21 import_data <- args[1] ###### Nom du fichier importé avec son extension / file name imported with the file type ".filetype"
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
22 index <- args[2] ###### List of selected metrics to calculate
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
23 source(args[3]) ###### Import functions
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
24 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
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")
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
26
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
27
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
28 #Import des données / Import data
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
29 obs <- read.table(import_data, sep = "\t", dec = ".", header = TRUE, encoding = "UTF-8") #
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
30 obs[obs == -999] <- NA
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
31 factors <- fact_det_f(obs = obs)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
32 obs_type <- def_typeobs_f(obs = obs)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
33 obs <- create_unitobs(data = obs)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
34
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
35 vars_data <- c("observation.unit", "species.code", "number")
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
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"
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
37 check_file(obs, err_msg_data, vars_data, 3)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
38
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
39
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
40
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
41 ####################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
42 ################# create community metrics table ## Function : calc_biodiv_f #######################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
43 ####################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
44
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
45 ########################################################################################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
46 calc_biodiv_f <- function(d_ata, unitobs = "observation.unit", code_species = "species.code", nombres = "number",
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
47 indices = index) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
48 ## Purpose: compute biodiversity indexes
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
49 ## ----------------------------------------------------------------------
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
50 ## Arguments: d_ata : input observation file
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
51 ## unitobs : name of column observation unit
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
52 ## code_species : name of species column
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
53 ## nombres : name of abundance column
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
54 ## indices : list of indexes to compute
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
55 ## ----------------------------------------------------------------------
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
56 ## Author: Yves Reecht, Date: 29 oct. 2010, 08:58 modified by Coline ROYAUX in june 2020
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
57
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
58 ## Supress lines that doesn't represent a species :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
59
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
60 notspline <- grep("(sp\\.)$|([1-9])$|^(Absencemacrofaune)$|^(NoID)$|^(Acrobranc)$|^(Acrodigit)$|^(Acroencr)$|^(Acrosubm)$|^(Acrotabu)$|^(Adredure)$|^(Adremoll)$|^(Algaturf)$|^(Balimona)$|^(Corablan)$|^(CoradurV)$|^(Coraenal)$|^(Coramor1)$|^(Coramor2)$|^(Coramou)$|^( Dallcora)$|^(Debrcora)$|^(Debris)$|^(Hare)$|^(HexaChar)$|^(MuraCong)$|^(Nacrbran)$|^(Nacrcham)$|^(Nacrencr)$|^(Nacrfoli)$|^(Nacrmass)$|^(Nacrsubm)$|^(Recrcora)$|^(Roche)$|^(Sable)$|^(Vase)$", d_ata[, code_species], value = FALSE)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
61 if (length(notspline) != 0) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
62 d_ata <- d_ata[-notspline, ]
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
63 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
64
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
65 ## Suppress unused factor levels :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
66 d_ata <- .GlobalEnv$drop_levels_f(df = d_ata)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
67
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
68
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
69 ## aggregation of data if not already done :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
70 if (nrow(d_ata) > nrow(expand.grid(unique(d_ata[, unitobs]), unique(d_ata[, code_species])))) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
71 d_ata <- agregations_generic_f(d_ata = d_ata, metrics = nombres,
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
72 factors = c(unitobs, code_species),
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
73 list_fact = NULL)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
74 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
75
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
76 df_biodiv <- as.data.frame(as.table(tapply(d_ata[, nombres],
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
77 d_ata[, unitobs],
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
78 sum, na.rm = TRUE)))
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
79
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
80 colnames(df_biodiv) <- c(unitobs, nombres)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
81
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
82 ## ##################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
83 ## species richness :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
84 d_ata$presence_absence <- .GlobalEnv$pres_abs_f(nombres = d_ata[, nombres], logical = FALSE)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
85
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
86 df_biodiv$species_richness <- as.vector(tapply(d_ata$presence_absence,
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
87 d_ata[, unitobs], sum, na.rm = TRUE),
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
88 "integer")
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
89 ## ... as.vector to avoid the class "array".
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
90
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
91 ## ##################################################
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
92 ## Simpson, Shannon indexes and derivatives :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
93
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
94 mat_nb <- tapply(d_ata[, nombres], # Matrix of individual count /species/unitobs.
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
95 list(d_ata[, unitobs], d_ata[, code_species]),
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
96 sum, na.rm = TRUE)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
97
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
98 mat_nb[is.na(mat_nb)] <- 0 # Vrais zéros
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
99
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
100 ## each species individual proportion in the dataset :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
101 prop_indiv <- sweep(mat_nb, 1,
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
102 apply(mat_nb, 1, sum, na.rm = TRUE), # individual count / unitobs ; equiv df_biodiv$nombre.
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
103 FUN = "/")
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
104
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
105 ## Simpson indexes :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
106 df_biodiv$simpson <- apply(prop_indiv^2, 1, sum, na.rm = TRUE)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
107
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
108 if (any(is.element(c("all", "simpson.l"), indices))) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
109 df_biodiv$simpson_l <- 1 - df_biodiv$simpson
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
110 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
111
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
112 ## Shannon index :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
113 df_biodiv$shannon <- -1 * apply(prop_indiv * log(prop_indiv), 1, sum, na.rm = TRUE)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
114
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
115 ## Pielou index :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
116 if (any(is.element(c("all", "pielou"), indices))) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
117 df_biodiv$pielou <- df_biodiv$shannon / log(df_biodiv$species_richness)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
118 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
119
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
120 ## Hill index :
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
121 if (any(is.element(c("all", "hill"), indices))) {
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
122 df_biodiv$hill <- (1 - df_biodiv$simpson) / exp(df_biodiv$shannon) # equiv df_biodiv$l.simpson / exp(df_biodiv$shannon)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
123 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
124
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
125
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
126 return(df_biodiv)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
127 }
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
128
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
129 ################# Analysis
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
130
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
131 res <- calc_numbers_f(obs, obs_type = obs_type, factors = factors, nb_name = "number")
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
132
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
133 table_comm_indexes <- calc_biodiv_f(res, unitobs = "observation.unit", code_species = "species.code", nombres = "number",
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
134 indices = index)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
135 table_comm_indexes <- create_year_location(table_comm_indexes)
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
136 #Save dataframe in a tabular format
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
137
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
138 filename_comm <- "TabCommunityIndexes.tabular"
6
2cd0a5a321c2 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit cd938a05cb066b16f2518159fa0ff5e3ed8a2b2b"
ecology
parents: 5
diff changeset
139 write.table(table_comm_indexes, filename_comm, row.names = FALSE, quote = FALSE, sep = "\t", dec = ".", fileEncoding = "UTF-8")
5
5bd7ddd7601f "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
140 cat(paste("\nWrite table with Community indexes. \n--> \"", filename_comm, "\"\n", sep = ""))