annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
1 #Rscript
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
2
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
3 #####################################################################################################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
4 #####################################################################################################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
5 ############################ Calculate presence absence table from observation data #################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
6 #####################################################################################################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
7 #####################################################################################################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
8
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
9 ###################### Packages
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
10 suppressMessages(library(tidyr))
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
11
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
12 ###################### Load arguments and declaring variables
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
13
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
14 args <- commandArgs(trailingOnly = TRUE)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
15
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
16
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
17 if (length(args) < 2) {
8d8aec182fb1 "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
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
19
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
20 } else {
8d8aec182fb1 "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"
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
22 source(args[2]) ###### Import functions
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
23
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
24 }
8d8aec182fb1 "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")
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
26
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
27
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
28 #Import des données / Import data
8d8aec182fb1 "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") #
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
30 obs[obs == -999] <- NA
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
31 factors <- fact_det_f(obs = obs)
8d8aec182fb1 "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)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
33 obs <- create_unitobs(data = obs)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
34
8d8aec182fb1 "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")
8d8aec182fb1 "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"
8d8aec182fb1 "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)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
38
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
39
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
40 ####################################################################################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
41 #################### Create presence/absence table ## Function : calc_pres_abs_f ####################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
42 ####################################################################################################
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
43
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
44 calc_pres_abs_f <- function(d_ata,
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
45 nb_name = "number") {
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
46 ## Purpose: Compute presence absence
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
47 ## ----------------------------------------------------------------------
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
48 ## Arguments: d_ata : temporary metrics table
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
49 ## nb_name : name of abundance column
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
50 ##
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
51 ## Output: presence absence vector
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
52 ## ----------------------------------------------------------------------
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
53 ## Author: Yves Reecht, Date: 20 déc. 2011, 12:04 modified by Coline ROYAUX 04 june 2020
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
54
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
55 ## Presence - absence :
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
56 pres_abs <- integer(nrow(d_ata))
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
57 pres_abs[d_ata[, nb_name] > 0] <- as.integer(1)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
58 pres_abs[d_ata[, nb_name] == 0] <- as.integer(0)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
59
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
60 return(pres_abs)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
61 }
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
62
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
63
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
64 ################# Analysis
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
65
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
66 res <- calc_numbers_f(obs, obs_type = obs_type, factors = factors, nb_name = "number")
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
67 res$presence_absence <- calc_pres_abs_f(res, nb_name = "number")
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
68 res <- create_year_location(res)
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
69
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
70 #Save dataframe in a tabular format
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
71 filename_pres_abs <- "TabPresAbs.tabular"
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
72 write.table(res, filename_pres_abs, row.names = FALSE, sep = "\t", dec = ".", fileEncoding = "UTF-8")
8d8aec182fb1 "planemo upload for repository https://github.com/ColineRoyaux/PAMPA-Galaxy commit 04381ca7162ec3ec68419e308194b91d11cacb04"
ecology
parents: 0
diff changeset
73 cat(paste("\nWrite table with presence/absence. \n--> \"", filename_pres_abs, "\"\n", sep = ""))