comparison tximport.R @ 1:2c338211927c draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/tximport commit b2e8dce6a2cebcc338b0f1b3acefd6ea01fbb418"
author iuc
date Thu, 23 Sep 2021 00:18:22 +0000
parents 206a71a69161
children
comparison
equal deleted inserted replaced
0:206a71a69161 1:2c338211927c
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 () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } ) 2 options(show.error.messages = F, error = function() {
3 cat(geterrmessage(), file = stderr()); q("no", 1, F)
4 })
3 5
4 # we need that to not crash galaxy with an UTF8 error on German LC settings. 6 # we need that to not crash galaxy with an UTF8 error on German LC settings.
5 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8") 7 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
6 8
7 library("getopt") 9 library("getopt")
17 "countsFromAbundance", "r", 1, "character", 19 "countsFromAbundance", "r", 1, "character",
18 "format", "v", 1, "character", 20 "format", "v", 1, "character",
19 "gff_file", "H", 0, "character", 21 "gff_file", "H", 0, "character",
20 "tx2gene", "f", 0, "character", 22 "tx2gene", "f", 0, "character",
21 "geneIdCol", "l", 0, "character", 23 "geneIdCol", "l", 0, "character",
22 "txIdCol" , "p", 1, "character", 24 "txIdCol", "p", 1, "character",
23 "abundanceCol", "i", 0, "character", 25 "abundanceCol", "i", 0, "character",
24 "countsCol", "y", 1, "character", 26 "countsCol", "y", 1, "character",
25 "lengthCol", "x", 1, "character"), 27 "lengthCol", "x", 1, "character"),
26 byrow=TRUE, ncol=4) 28 byrow = TRUE, ncol = 4)
27 29
28 opt <- getopt(spec) 30 opt <- getopt(spec)
29 31
30 # if help was asked for print a friendly message 32 # if help was asked for print a friendly message
31 # and exit with a non-zero error code 33 # and exit with a non-zero error code
32 if (!is.null(opt$help)) { 34 if (!is.null(opt$help)) {
33 cat(getopt(spec, usage=TRUE)) 35 cat(getopt(spec, usage = TRUE))
34 q(status=1) 36 q(status = 1)
35 } 37 }
36 38
37 if (is.null(opt$gff_file) & is.null(opt$tx2gene)) { 39 if (is.null(opt$gff_file) & is.null(opt$tx2gene)) {
38 cat("A GFF/GTF file or a tx2gene table is required\n") 40 cat("A GFF/GTF file or a tx2gene table is required\n")
39 q(status=1) 41 q(status = 1)
40 } 42 }
41 43
42 if (opt$format == 'none'){ #custom format 44 if (opt$format == "none") { #custom format
43 if (is.null(opt$txIdCol) | is.null(opt$abundanceCol) | is.null(opt$countsCol) | is.null(opt$lengthCol)) { 45 if (is.null(opt$txIdCol) | is.null(opt$abundanceCol) | is.null(opt$countsCol) | is.null(opt$lengthCol)) {
44 cat("If you select a custom format for the input files you need to specify the column names\n") 46 cat("If you select a custom format for the input files you need to specify the column names\n")
45 q(status=1) 47 q(status = 1)
46 } 48 }
47 } 49 }
48 50
49 if (is.null(opt$countsFiles)) { 51 if (is.null(opt$countsFiles)) {
50 cat("'countsFiles' is required\n") 52 cat("'countsFiles' is required\n")
51 q(status=1) 53 q(status = 1)
52 } 54 }
53 55
54 56
55 # load samples from tab file 57 # load samples from tab file
56 samples_df <- read.table(opt$countsFiles, sep="\t", header=TRUE) 58 samples_df <- read.table(opt$countsFiles, sep = "\t", header = TRUE)
57 colnames(samples_df) <- c("id","path") 59 colnames(samples_df) <- c("id", "path")
58 rownames(samples_df) <- NULL 60 rownames(samples_df) <- NULL
59 # Prepare char vector with files and sample names 61 # Prepare char vector with files and sample names
60 files <- file.path(samples_df[,"path"]) 62 files <- file.path(samples_df[, "path"])
61 names(files) <- samples_df[,"id"] 63 names(files) <- samples_df[, "id"]
62 64
63 65
64 66
65 library(tximport) 67 library(tximport)
66 68
69 suppressPackageStartupMessages({ 71 suppressPackageStartupMessages({
70 library("GenomicFeatures") 72 library("GenomicFeatures")
71 }) 73 })
72 txdb <- makeTxDbFromGFF(opt$gff_file) 74 txdb <- makeTxDbFromGFF(opt$gff_file)
73 k <- keys(txdb, keytype = "TXNAME") 75 k <- keys(txdb, keytype = "TXNAME")
74 tx2gene <- select(txdb, keys=k, columns="GENEID", keytype="TXNAME") 76 tx2gene <- select(txdb, keys = k, columns = "GENEID", keytype = "TXNAME")
75 # Remove 'transcript:' from transcript IDs (when gffFile is a GFF3 from Ensembl and the transcript does not have a Name) 77 # Remove 'transcript:' from transcript IDs (when gffFile is a GFF3 from Ensembl and the transcript does not have a Name)
76 tx2gene$TXNAME <- sub('^transcript:', '', tx2gene$TXNAME) 78 tx2gene$TXNAME <- sub("^transcript:", "", tx2gene$TXNAME) # nolint
77 79
78 } else { 80 } else {
79 tx2gene <- read.table(opt$tx2gene,header=FALSE) 81 tx2gene <- read.table(opt$tx2gene, header = FALSE)
80 } 82 }
81 83
82 84
83 85
84 ## 86 ##
85 if (is.null(opt$geneIdCol)) { ## there is a tx2gene table 87 if (is.null(opt$geneIdCol)) { ## there is a tx2gene table
86 if (opt$format == 'none'){ #predefined format 88 if (opt$format == "none") { #predefined format
87 txi_out <- tximport(files, type="none",txIdCol=opt$txIdCol,abundanceCol=opt$abundanceCol,countsCol=opt$countsCol,lengthCol=opt$lengthCol,tx2gene=tx2gene,countsFromAbundance=opt$countsFromAbundance) 89 txi_out <- tximport(files, type = "none", txIdCol = opt$txIdCol, abundanceCol = opt$abundanceCol, countsCol = opt$countsCol, lengthCol = opt$lengthCol, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance)
88 } else { 90 } else {
89 txi_out <- tximport(files, type=opt$format, tx2gene=tx2gene,countsFromAbundance=opt$countsFromAbundance) 91 txi_out <- tximport(files, type = opt$format, tx2gene = tx2gene, countsFromAbundance = opt$countsFromAbundance)
90 } 92 }
91 } else { # the gene_ID is a column in the counts table 93 } else { # the gene_ID is a column in the counts table
92 if (opt$format == 'none'){ #predefined format 94 if (opt$format == "none") { #predefined format
93 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) 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)
94 } else { 96 } else {
95 txi_out <- tximport(files, type=opt$format, geneIdCol=opt$geneIdCol,countsFromAbundance=opt$countsFromAbundance) 97 txi_out <- tximport(files, type = opt$format, geneIdCol = opt$geneIdCol, countsFromAbundance = opt$countsFromAbundance)
96 } 98 }
97 99
98 } 100 }
99 # write count as table 101 # write count as table
100 write.table(txi_out$counts, file=opt$out_file, row.names = TRUE, col.names = TRUE, quote = FALSE, sep = "\t") 102 write.table(txi_out$counts, file = opt$out_file, row.names = TRUE, col.names = TRUE, quote = FALSE, sep = "\t")