annotate r-sleuth.R @ 2:a049d0d5a05e draft

Uploaded
author youyuh48
date Fri, 08 Dec 2017 22:15:41 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
1 #!/usr/bin/env Rscript
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
2
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
3 # A command-line interface to sleuth for use with Galaxy
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
4 # This script modified from
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
5 # https://github.com/pachterlab/bears_analyses/blob/master/sleuth.R
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
6
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
7 # setup R error handling to go to stderr
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
8 options( show.error.messages=F, error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
9
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
10 # we need that to not crash galaxy with an UTF8 error on German LC settings.
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
11 loc <- Sys.setlocale("LC_MESSAGES", "en_US.UTF-8")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
12
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
13 library("getopt")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
14 library("tools")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
15 options(stringAsFactors = FALSE, useFancyQuotes = FALSE)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
16 args <- commandArgs(trailingOnly = TRUE)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
17
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
18 # get options, using the spec as defined by the enclosed list.
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
19 spec <- matrix(c(
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
20 "quiet", "q", 0, "logical",
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
21 "help", "h", 0, "logical",
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
22 "indir", "i", 1, "character",
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
23 "metadata", "m", 1, "character",
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
24 "full_model", "f", 1, "character",
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
25 "reduced_model", "r", 1, "character",
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
26 "gene_anno_name", "a", 2, "character"
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
27 ), byrow=TRUE, ncol=4)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
28 opt <- getopt(spec)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
29
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
30 # if help was asked for print a friendly message
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
31 # and exit with a non-zero error code
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
32 if (!is.null(opt$help)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
33 cat(getopt(spec, usage=TRUE))
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
34 q(status=1)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
35 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
36
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
37 # enforce the following required arguments
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
38 if (is.null(opt$indir)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
39 cat("'indir' is required\n")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
40 q(status=1)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
41 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
42 if (is.null(opt$metadata)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
43 cat("'metadata' is required\n")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
44 q(status=1)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
45 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
46 if (is.null(opt$full_model)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
47 cat("'full_model' is required\n")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
48 q(status=1)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
49 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
50 if (is.null(opt$reduced_model)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
51 cat("'reduced_model' is required\n")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
52 q(status=1)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
53 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
54
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
55 verbose <- if (is.null(opt$quiet)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
56 TRUE
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
57 } else {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
58 FALSE
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
59 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
60
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
61 suppressPackageStartupMessages({
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
62 library("sleuth")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
63 library("biomaRt")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
64 })
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
65
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
66 s2c <- read.table(file.path(opt$metadata), header = TRUE, stringsAsFactors=FALSE)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
67 run_dirs <- s2c$sample
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
68 kal_dirs <- c()
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
69
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
70 for (dir in run_dirs) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
71 kal_dirs <- c(kal_dirs, file.path(opt$indir, dir, "kallisto"))
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
72 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
73
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
74 s2c <- dplyr::mutate(s2c, path = kal_dirs)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
75
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
76 if (!is.null(opt$gene_anno_name)) {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
77 mart <- biomaRt::useMart(biomart = "ensembl", dataset = opt$gene_anno_name)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
78 t2g <- biomaRt::getBM(attributes = c("ensembl_transcript_id", "ensembl_gene_id", "external_gene_name"), mart = mart)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
79 t2g <- dplyr::rename(t2g, target_id = ensembl_transcript_id, ens_gene = ensembl_gene_id, ext_gene = external_gene_name)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
80 so <- sleuth_prep(s2c, as.formula(opt$full_model), target_mapping = t2g, read_bootstrap_tpm=TRUE, extra_bootstrap_summary=TRUE)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
81 } else {
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
82 so <- sleuth_prep(s2c, as.formula(opt$full_model), read_bootstrap_tpm=TRUE, extra_bootstrap_summary=TRUE)
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
83 }
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
84 so <- sleuth_fit(so, as.formula(opt$full_model), "full")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
85 so <- sleuth_fit(so, as.formula(opt$reduced_model), "reduced")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
86 so <- sleuth_lrt(so, "reduced", "full")
a049d0d5a05e Uploaded
youyuh48
parents:
diff changeset
87 sleuth_deploy(so, opt$indir)