Mercurial > repos > ecology > claraguess
comparison cluster_ceamarc.R @ 0:adeb719a267f draft
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/Ecoregionalization_workflow commit 1f5e22a210b8a395f1c7b48f54e03e781a1b34c4
| author | ecology |
|---|---|
| date | Wed, 14 May 2025 13:49:38 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:adeb719a267f |
|---|---|
| 1 ##13/04/2023 | |
| 2 ##Seguineau Pauline | |
| 3 ### Clustering with Clara algorithm | |
| 4 | |
| 5 #load library | |
| 6 library(cluster) | |
| 7 library(dplyr) | |
| 8 library(tidyverse) | |
| 9 | |
| 10 #load arguments | |
| 11 args = commandArgs(trailingOnly=TRUE) | |
| 12 if (length(args)==0) | |
| 13 { | |
| 14 stop("This tool needs at least one argument") | |
| 15 }else{ | |
| 16 data <- args[1] | |
| 17 enviro <- args[2] | |
| 18 data.bio <- args[3] | |
| 19 k <- as.numeric(args[4]) | |
| 20 metric <- args[5] | |
| 21 sample <- as.numeric(args[6]) | |
| 22 } | |
| 23 | |
| 24 #load data | |
| 25 env.data <- read.table(enviro, header=TRUE, sep="\t",dec = ".", na.strings = "-9999") | |
| 26 data.bio <- read.table(data.bio, header=TRUE, sep="\t") | |
| 27 test3 <- read.table(data, header = TRUE, sep="\t") | |
| 28 | |
| 29 ###################################################################################################### | |
| 30 #Make clustering | |
| 31 | |
| 32 k <- k #number of clusters | |
| 33 test5 <- clara(test3, k, metric = metric, samples = sample, sampsize = min(nrow(test3), (nrow(data.bio)/nrow(test3))+2*k)) | |
| 34 | |
| 35 ####################################################################################################### | |
| 36 #save results | |
| 37 | |
| 38 png("sih.png") | |
| 39 plot(silhouette(test5)) | |
| 40 dev.off() | |
| 41 | |
| 42 clus <- cbind(data.bio[1:nrow(test3), 1:2],test5$clustering) | |
| 43 names(clus) <- c("lat", "long", "cluster") | |
| 44 clus <- cbind(clus,test3,env.data[,3:ncol(env.data)]) | |
| 45 | |
| 46 write.table(clus[1:3], file = "points_clus.tsv", quote = FALSE, sep="\t", row.names = FALSE) | |
| 47 write.table(clus, file = "clus.tsv", quote = FALSE, sep="\t", row.names = FALSE) | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 |
