Mercurial > repos > iuc > tximport
comparison tximport.R @ 3:915a9dd57e1a draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/tximport commit 5f8e6e70a9d82331367f7a052a76a04c47c5144e
| author | iuc |
|---|---|
| date | Sat, 28 Sep 2024 16:33:10 +0000 |
| parents | 3e2f1aef1aac |
| children |
comparison
equal
deleted
inserted
replaced
| 2:865d5bc8f531 | 3:915a9dd57e1a |
|---|---|
| 1 # setup R error handling to go to stderr | 1 # setup R error handling to go to stderr |
| 2 options(show.error.messages = F, error = function() { | 2 options(show.error.messages = F, error = function() { |
| 3 cat(geterrmessage(), file = stderr()); q("no", 1, F) | 3 cat(geterrmessage(), file = stderr()) |
| 4 q("no", 1, F) | |
| 4 }) | 5 }) |
| 5 | 6 |
| 6 # we need that to not crash galaxy with an UTF8 error on German LC settings. | 7 # we need that to not crash galaxy with an UTF8 error on German LC settings. |
| 7 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") | 8 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") |
| 8 | 9 |
| 10 options(stringAsFactors = FALSE, useFancyQuotes = FALSE) | 11 options(stringAsFactors = FALSE, useFancyQuotes = FALSE) |
| 11 args <- commandArgs(trailingOnly = TRUE) | 12 args <- commandArgs(trailingOnly = TRUE) |
| 12 | 13 |
| 13 # get options, using the spec as defined by the enclosed list. | 14 # get options, using the spec as defined by the enclosed list. |
| 14 # we read the options from the default: commandArgs(TRUE). | 15 # we read the options from the default: commandArgs(TRUE). |
| 15 spec <- matrix(c( | 16 spec <- matrix( |
| 16 "help", "h", 0, "logical", | 17 c( |
| 17 "out_file", "o", 1, "character", | 18 "help", "h", 0, "logical", |
| 18 "countsFiles", "n", 1, "character", | 19 "out_file", "o", 1, "character", |
| 19 "countsFromAbundance", "r", 1, "character", | 20 "countsFiles", "n", 1, "character", |
| 20 "format", "v", 1, "character", | 21 "countsFromAbundance", "r", 1, "character", |
| 21 "gff_file", "H", 0, "character", | 22 "format", "v", 1, "character", |
| 22 "tx2gene", "f", 0, "character", | 23 "gff_file", "H", 0, "character", |
| 23 "geneIdCol", "l", 0, "character", | 24 "tx2gene", "f", 0, "character", |
| 24 "txIdCol", "p", 1, "character", | 25 "geneIdCol", "l", 0, "character", |
| 25 "abundanceCol", "i", 0, "character", | 26 "txIdCol", "p", 1, "character", |
| 26 "countsCol", "y", 1, "character", | 27 "abundanceCol", "i", 0, "character", |
| 27 "lengthCol", "x", 1, "character"), | 28 "countsCol", "y", 1, "character", |
| 28 byrow = TRUE, ncol = 4) | 29 "lengthCol", "x", 1, "character" |
| 30 ), | |
| 31 byrow = TRUE, ncol = 4 | |
| 32 ) | |
| 29 | 33 |
| 30 opt <- getopt(spec) | 34 opt <- getopt(spec) |
| 31 | 35 |
| 32 # if help was asked for print a friendly message | 36 # if help was asked for print a friendly message |
| 33 # and exit with a non-zero error code | 37 # and exit with a non-zero error code |
| 34 if (!is.null(opt$help)) { | 38 if (!is.null(opt$help)) { |
| 35 cat(getopt(spec, usage = TRUE)) | 39 cat(getopt(spec, usage = TRUE)) |
| 36 q(status = 1) | 40 q(status = 1) |
| 37 } | |
| 38 | |
| 39 if (is.null(opt$gff_file) & is.null(opt$tx2gene)) { | |
| 40 cat("A GFF/GTF file or a tx2gene table is required\n") | |
| 41 q(status = 1) | |
| 42 } | 41 } |
| 43 | 42 |
| 44 if (opt$format == "none") { #custom format | 43 if (is.null(opt$gff_file) & is.null(opt$tx2gene)) { |
| 44 cat("A GFF/GTF file or a tx2gene table is required\n") | |
| 45 q(status = 1) | |
| 46 } | |
| 47 | |
| 48 if (opt$format == "none") { # custom format | |
| 45 if (is.null(opt$txIdCol) | is.null(opt$abundanceCol) | is.null(opt$countsCol) | is.null(opt$lengthCol)) { | 49 if (is.null(opt$txIdCol) | is.null(opt$abundanceCol) | is.null(opt$countsCol) | is.null(opt$lengthCol)) { |
| 46 cat("If you select a custom format for the input files you need to specify the column names\n") | 50 cat("If you select a custom format for the input files you need to specify the column names\n") |
| 47 q(status = 1) | 51 q(status = 1) |
| 48 } | 52 } |
| 49 } | 53 } |
| 50 | 54 |
| 51 if (is.null(opt$countsFiles)) { | 55 if (is.null(opt$countsFiles)) { |
| 52 cat("'countsFiles' is required\n") | 56 cat("'countsFiles' is required\n") |
| 53 q(status = 1) | 57 q(status = 1) |
| 54 } | 58 } |
| 55 | 59 |
| 56 | 60 |
| 57 # load samples from tab file | 61 # load samples from tab file |
| 58 samples_df <- read.table(opt$countsFiles, sep = "\t", header = TRUE) | 62 samples_df <- read.table(opt$countsFiles, sep = "\t", header = TRUE) |
| 73 }) | 77 }) |
| 74 txdb <- makeTxDbFromGFF(opt$gff_file) | 78 txdb <- makeTxDbFromGFF(opt$gff_file) |
| 75 k <- keys(txdb, keytype = "TXNAME") | 79 k <- keys(txdb, keytype = "TXNAME") |
| 76 tx2gene <- select(txdb, keys = k, columns = "GENEID", keytype = "TXNAME") | 80 tx2gene <- select(txdb, keys = k, columns = "GENEID", keytype = "TXNAME") |
| 77 # Remove 'transcript:' from transcript IDs (when gffFile is a GFF3 from Ensembl and the transcript does not have a Name) | 81 # Remove 'transcript:' from transcript IDs (when gffFile is a GFF3 from Ensembl and the transcript does not have a Name) |
| 78 tx2gene$TXNAME <- sub("^transcript:", "", tx2gene$TXNAME) # nolint | 82 tx2gene$TXNAME <- sub("^transcript:", "", tx2gene$TXNAME) # nolint |
| 79 | |
| 80 } else { | 83 } else { |
| 81 tx2gene <- read.table(opt$tx2gene, header = FALSE) | 84 tx2gene <- read.table(opt$tx2gene, header = FALSE) |
| 82 } | 85 } |
| 83 | 86 |
| 84 | 87 |
| 85 | 88 |
| 86 ## | 89 ## |
| 87 if (is.null(opt$geneIdCol)) { ## there is a tx2gene table | 90 if (is.null(opt$geneIdCol)) { ## there is a tx2gene table |
| 88 if (opt$format == "none") { #predefined format | 91 if (opt$format == "none") { # predefined format |
| 89 txi_out <- tximport(files, type = "none", txIdCol = opt$txIdCol, abundanceCol = opt$abundanceCol, countsCol = opt$countsCol, lengthCol = opt$lengthCol, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance) | 92 txi_out <- tximport(files, type = "none", txIdCol = opt$txIdCol, abundanceCol = opt$abundanceCol, countsCol = opt$countsCol, lengthCol = opt$lengthCol, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance) |
| 90 } else { | 93 } else { |
| 91 txi_out <- tximport(files, type = opt$format, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance) | 94 txi_out <- tximport(files, type = opt$format, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance) |
| 92 } | 95 } |
| 93 } else { # the gene_ID is a column in the counts table | 96 } else { # the gene_ID is a column in the counts table |
| 94 if (opt$format == "none") { #predefined format | 97 if (opt$format == "none") { # predefined format |
| 95 txi_out <- tximport(files, type = "none", geneIdCol = opt$geneIdCol, txIdCol = opt$txIdCol, abundanceCol = opt$abundanceCol, countsCol = opt$countsCol, lengthCol = opt$lengthCol, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance) | 98 txi_out <- tximport(files, type = "none", geneIdCol = opt$geneIdCol, txIdCol = opt$txIdCol, abundanceCol = opt$abundanceCol, countsCol = opt$countsCol, lengthCol = opt$lengthCol, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance) |
| 96 } else { | 99 } else { |
| 97 txi_out <- tximport(files, type = opt$format, geneIdCol = opt$geneIdCol, countsFromAbundance = opt$countsFromAbundance) | 100 txi_out <- tximport(files, type = opt$format, geneIdCol = opt$geneIdCol, countsFromAbundance = opt$countsFromAbundance) |
| 98 } | 101 } |
| 99 | |
| 100 } | 102 } |
| 101 # write count as table | 103 # write count as table |
| 102 write.table(txi_out$counts, file = opt$out_file, row.names = TRUE, col.names = TRUE, quote = FALSE, sep = "\t") | 104 write.table(txi_out$counts, file = opt$out_file, row.names = TRUE, col.names = TRUE, quote = FALSE, sep = "\t") |
