Mercurial > repos > iuc > deseq2
annotate get_deseq_dataset.R @ 18:3bf1b3ec1ddf draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit 448dccb0c02aba00d8301247b0f0f406ab3d4fa2
author | iuc |
---|---|
date | Fri, 16 Nov 2018 14:47:19 -0500 |
parents | d9e5cadc7f0b |
children | c56e0689e46e |
rev | line source |
---|---|
17
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
1 get_deseq_dataset <- function(sampleTable, header, designFormula, tximport, txtype, tx2gene) { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
2 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
3 dir <- "" |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
4 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
5 if (!is.null(header)) { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
6 hasHeader <- TRUE |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
7 } else { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
8 hasHeader <- FALSE |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
9 } |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
10 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
11 if (!is.null(tximport)) { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
12 if (is.null(tx2gene)) stop("A transcript-to-gene map or a GTF file is required for tximport") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
13 if (tolower(file_ext(opt$tx2gene)) == "gtf") { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
14 gtfFile <-tx2gene |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
15 } else { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
16 gtfFile <- NULL |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
17 tx2gene <- read.table(tx2gene, header=FALSE) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
18 } |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
19 useTXI <- TRUE |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
20 } else { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
21 useTXI <- FALSE |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
22 } |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
23 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
24 if (!useTXI & hasHeader) { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
25 countfiles <- lapply(as.character(sampleTable$filename), function(x){read.delim(x, row.names=1)}) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
26 tbl <- do.call("cbind", countfiles) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
27 colnames(tbl) <- rownames(sampleTable) # take sample ids from header |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
28 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
29 # check for htseq report lines (from DESeqDataSetFromHTSeqCount function) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
30 oldSpecialNames <- c("no_feature", "ambiguous", "too_low_aQual", |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
31 "not_aligned", "alignment_not_unique") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
32 specialRows <- (substr(rownames(tbl), 1, 1) == "_") | rownames(tbl) %in% oldSpecialNames |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
33 tbl <- tbl[!specialRows, , drop = FALSE] |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
34 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
35 dds <- DESeqDataSetFromMatrix(countData = tbl, |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
36 colData = subset(sampleTable, select=-(filename)), |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
37 design = designFormula) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
38 } else if (!useTXI & !hasHeader) { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
39 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
40 # construct the object from HTSeq files |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
41 dds <- DESeqDataSetFromHTSeqCount(sampleTable = sampleTable, |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
42 directory = dir, |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
43 design = designFormula) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
44 colnames(dds) <- row.names(sampleTable) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
45 |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
46 } else { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
47 # construct the object using tximport |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
48 # first need to make the tx2gene table |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
49 # this takes ~2-3 minutes using Bioconductor functions |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
50 if (!is.null(gtfFile)) { |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
51 suppressPackageStartupMessages({ |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
52 library("GenomicFeatures") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
53 }) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
54 txdb <- makeTxDbFromGFF(gtfFile, format="gtf") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
55 k <- keys(txdb, keytype = "GENEID") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
56 df <- select(txdb, keys = k, keytype = "GENEID", columns = "TXNAME") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
57 tx2gene <- df[, 2:1] # tx ID, then gene ID |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
58 } |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
59 library("tximport") |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
60 txiFiles <- as.character(sampleTable$filename) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
61 labs <- row.names(sampleTable) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
62 names(txiFiles) <- labs |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
63 txi <- tximport(txiFiles, type=txtype, tx2gene=tx2gene) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
64 dds <- DESeqDataSetFromTximport(txi, |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
65 subset(sampleTable, select=-c(filename)), |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
66 designFormula) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
67 } |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
68 return(dds) |
d9e5cadc7f0b
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/deseq2 commit b95582cea8320d5488056a9576474f79cec53be8
iuc
parents:
diff
changeset
|
69 } |