Mercurial > repos > iuc > virannot_otu
comparison seek_otu.R @ 0:e924ab4ab00a draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/virAnnot commit ab5e1189217b6ed5f1c5d7c5ff6b79b6a4c18cff
| author | iuc |
|---|---|
| date | Wed, 21 Aug 2024 13:13:10 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e924ab4ab00a |
|---|---|
| 1 #!/usr/bin/env Rscript | |
| 2 | |
| 3 ## Redirect R error handling to stderr. | |
| 4 options(show.error.messages = FALSE, error = function() { | |
| 5 cat(geterrmessage(), file = stderr()) | |
| 6 q("no", 1, FALSE) | |
| 7 }) | |
| 8 | |
| 9 ## Avoid crashing Galaxy with a UTF8 error on German LC settings | |
| 10 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | |
| 11 | |
| 12 args <- commandArgs(trailingOnly = TRUE) | |
| 13 if (length(args) == 0) { | |
| 14 stop("Arguments missing for Rscrpit", call. = FALSE) | |
| 15 } else { | |
| 16 # percentage of identity | |
| 17 id_threshold <- as.numeric(args[3]) | |
| 18 # get input data (matrix) | |
| 19 data <- read.csv(args[1], header = FALSE, sep = ",", row.names = 1) | |
| 20 # remove last 2 columns | |
| 21 data_length <- length(data) | |
| 22 # create matrix | |
| 23 mat <- as.matrix(data[, 1:data_length], fill = TRUE) | |
| 24 # create coordinate matrix | |
| 25 d <- as.dist(1 - mat) | |
| 26 # create tree | |
| 27 hc <- hclust(d, method = "single") | |
| 28 # assign otu based on identity value | |
| 29 otu <- cutree(hc, h = -id_threshold) | |
| 30 # group contigs by otu | |
| 31 # Print results to output file | |
| 32 output <- args[2] | |
| 33 # unique is used to know the number of different otu | |
| 34 for (i in unique(otu)) { | |
| 35 # retrieve contigs belonging to the same otu | |
| 36 clust <- which(otu == i) | |
| 37 # write otu number and number of contigs in this otu | |
| 38 cat( | |
| 39 paste("OTU_", i, ",", length(clust), ",", sep = ""), | |
| 40 file = output, append = TRUE | |
| 41 ) | |
| 42 for (n in names(clust)) { | |
| 43 # write contigs name | |
| 44 cat(paste(gsub(" ", "", n), ",", sep = ""), file = output, append = TRUE) | |
| 45 } | |
| 46 cat("\n", sep = "", file = output, append = TRUE) | |
| 47 } | |
| 48 } |
