Mercurial > repos > ecology > estimate_endem
comparison EstimEndem.R @ 0:f7a55ccf2a3e draft
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Phylodiversity_workflow commit 0de557d919c26eb0b5ab61504bc597d551503ac3
author | ecology |
---|---|
date | Tue, 20 May 2025 09:52:29 +0000 |
parents | |
children | f3a977826375 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f7a55ccf2a3e |
---|---|
1 #!/bin/Rscript | |
2 # phyloregions | |
3 | |
4 args = commandArgs(trailingOnly=TRUE) | |
5 #args = c("input/matrix_file", "input/tree_file.txt", "input/grid_aq_3_PF.shp") | |
6 | |
7 # library | |
8 library(phyloregion) | |
9 library(ape) | |
10 library(Matrix) | |
11 library(SparseArray) | |
12 library(sf) | |
13 library(sp) | |
14 library(raster) | |
15 library(dplyr) | |
16 | |
17 | |
18 save_sf <- function(){ | |
19 #st_write(phyloreg_sf[,-(3:5)], paste0(tempdir(), "/", "output.shp"), delete_layer = TRUE) | |
20 write_sf(phyloreg_sf, "output.shp") | |
21 } | |
22 | |
23 | |
24 if (length(args)<5){stop('Usage : sparseMatrix.csv tree.txt grid.shp nb_clust clust_method') | |
25 }else{ | |
26 # read enter files | |
27 comm_tree <- read.tree(args[1]) | |
28 | |
29 comm_matrix <- readSparseCSV(args[2], sep = "\t") | |
30 comm_matrix <- as(comm_matrix,"dgCMatrix") | |
31 | |
32 grid <- read_sf(args[3]) | |
33 | |
34 nb_clust <- as.integer(args[4]) | |
35 | |
36 clust_method <- toString(args[5]) | |
37 | |
38 # calculate phylogenetic Beta diversity - a phylogenetic distance matrix between grid cells | |
39 phylo_beta <- phylobeta(comm_matrix, comm_tree, index.family = "sorensen") | |
40 | |
41 #select the less distorting clustering method, best fitting between phylogenetic distances in phylobeta matrix | |
42 # and raw distances from branch lengths of the tree | |
43 select_linkage(phylo_beta[[1]]) | |
44 | |
45 #select optimal number of clusters with selected method | |
46 optim <- optimal_phyloregion(phylo_beta[[3]], k = 30) | |
47 print(paste("the best number of cluster is :", optim$optimal$k)) | |
48 #plot(optim$df$k, optim$df$ev, pch = 20) # k - nbr of clusters VS explained variance given k | |
49 # k has to be selected by a user | |
50 | |
51 # pass the grid cell to spatial format | |
52 grid_sp <- as_Spatial(grid) | |
53 #proj4string(grid_sp) | |
54 | |
55 # calculate phyloregions clusters | |
56 y <- phyloregion(phylo_beta[[3]], pol = grid_sp, k = nb_clust, method = clust_method) | |
57 #summary(y) | |
58 #phylo_nmds <- y$NMDS | |
59 | |
60 # take an shp spatial file for phyloregions and put it to sf format | |
61 phyloreg_sf <- y$pol | |
62 # print(st_crs(phyloreg_sf)) | |
63 #plot(phyloreg_sf) | |
64 phyloreg_sf <- st_as_sf(phyloreg_sf, crs = st_crs(grid)) | |
65 # print(st_crs(phyloreg_sf)) | |
66 | |
67 names(phyloreg_sf)[3:8] <- c("R_val", "G_val", "B_val", "r_code", "g_code", "b_code") | |
68 | |
69 | |
70 save_sf() | |
71 } | |
72 | |
73 | |
74 # sf_recup <- read_sf("output.shp") | |
75 |