Mercurial > repos > ieguinoa > tximport
comparison tximport.R @ 0:2f5e9c0fe367 draft default tip
"planemo upload for repository https://github.com/ieguinoa/tximport-galaxy-wrapper commit 2bb25471c1320fb1206afa2c4daf536b6d6e275f-dirty"
author | ieguinoa |
---|---|
date | Wed, 09 Oct 2019 15:38:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2f5e9c0fe367 |
---|---|
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 ) } ) | |
3 | |
4 # 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") | |
6 | |
7 library("getopt") | |
8 #library("tools") | |
9 options(stringAsFactors = FALSE, useFancyQuotes = FALSE) | |
10 args <- commandArgs(trailingOnly = TRUE) | |
11 | |
12 # get options, using the spec as defined by the enclosed list. | |
13 # we read the options from the default: commandArgs(TRUE). | |
14 spec <- matrix(c( | |
15 "help", "h", 0, "logical", | |
16 "base_dir", "w", 1, "character", | |
17 "out_file", "o", 1, "character", | |
18 "countsFiles", "n", 1, "character", | |
19 "countsFromAbundance", "r", 1, "character", | |
20 "format", "v", 1, "character", | |
21 "gff_file", "H", 0, "character", | |
22 "tx2gene", "f", 0, "character", | |
23 "geneIdCol", "l", 0, "character", | |
24 "txIdCol" , "p", 1, "character", | |
25 "abundanceCol", "i", 0, "character", | |
26 "countsCol", "y", 1, "character", | |
27 "lengthCol", "x", 1, "character"), | |
28 byrow=TRUE, ncol=4) | |
29 | |
30 opt <- getopt(spec) | |
31 | |
32 | |
33 | |
34 | |
35 # if help was asked for print a friendly message | |
36 # and exit with a non-zero error code | |
37 if (!is.null(opt$help)) { | |
38 cat(getopt(spec, usage=TRUE)) | |
39 q(status=1) | |
40 } | |
41 | |
42 if (is.null(opt$gff_file) & is.null(opt$tx2gene)) { | |
43 cat("A GFF/GTF file or a tx2gene table is required\n") | |
44 q(status=1) | |
45 } | |
46 | |
47 if (opt$format == 'none'){ #custom format | |
48 if (is.null(opt$txIdCol) | is.null(opt$abundanceCol) | is.null(opt$countsCol) | is.null(opt$lengthCol)) { | |
49 cat("If you select a custom format for the input files you need to specify the column names\n") | |
50 q(status=1) | |
51 } | |
52 } | |
53 | |
54 if (is.null(opt$countsFiles)) { | |
55 cat("'countsFiles' is required\n") | |
56 q(status=1) | |
57 } | |
58 | |
59 ## parse counts files | |
60 library(rjson) | |
61 dat <- fromJSON(opt$countsFiles) | |
62 samples_df <- lapply(dat, function(samples) # Loop through each "sample" | |
63 { | |
64 # Convert each group to a data frame. | |
65 # This assumes you have 6 elements each time | |
66 data.frame(matrix(unlist(samples), ncol=2, byrow=T)) | |
67 }) | |
68 samples_df <- do.call(rbind, samples_df) | |
69 colnames(samples_df) <- c("path","id") | |
70 rownames(samples_df) <- NULL | |
71 | |
72 # Prepare char vector with files and sample names | |
73 files <- file.path(samples_df[,"path"]) | |
74 names(files) <- samples_df[,"id"] | |
75 #files | |
76 #all(file.exists(files)) | |
77 | |
78 | |
79 | |
80 library(tximport) | |
81 | |
82 | |
83 | |
84 | |
85 ### if the input is a gff/gtf file first need to create the tx2gene table | |
86 if (!is.null(opt$gff_file)) { | |
87 suppressPackageStartupMessages({ | |
88 library("GenomicFeatures") | |
89 }) | |
90 txdb <- makeTxDbFromGFF(opt$gff_file) | |
91 k <- keys(txdb, keytype = "TXNAME") | |
92 tx2gene <- select(txdb, keys=k, columns="GENEID", keytype="TXNAME") | |
93 # Remove 'transcript:' from transcript IDs (when gffFile is a GFF3 from Ensembl and the transcript does not have a Name) | |
94 tx2gene$TXNAME <- sub('^transcript:', '', tx2gene$TXNAME) | |
95 | |
96 } else { | |
97 tx2gene <- read.table(opt$tx2gene,header=FALSE) | |
98 } | |
99 | |
100 | |
101 | |
102 ## | |
103 if (is.null(opt$geneIdCol)) { ## there is a tx2gene table | |
104 if (opt$format == 'none'){ #predefined format | |
105 cat("here i am too\n") | |
106 txi_out <- tximport(files, type="none",txIdCol=opt$txIdCol,abundanceCol=opt$abundanceCol,countsCol=opt$countsCol,lengthCol=opt$lengthCol,tx2gene=tx2gene,countsFromAbundance=opt$countsFromAbundance) | |
107 } else { | |
108 txi_out <- tximport(files, type=opt$format, tx2gene=tx2gene,countsFromAbundance=opt$countsFromAbundance) | |
109 } | |
110 } else { # the gene_ID is a column in the counts table | |
111 if (opt$format == 'none'){ #predefined format | |
112 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) | |
113 } else { | |
114 txi_out <- tximport(files, type=opt$format, geneIdCol=opt$geneIdCol,countsFromAbundance=opt$countsFromAbundance) | |
115 } | |
116 | |
117 } | |
118 # write count as table | |
119 write.table(txi_out$counts, file=opt$out_file, row.names = TRUE, col.names = TRUE, quote = FALSE, sep = "\t") |