-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxi_to_counts.R
37 lines (31 loc) · 1.44 KB
/
txi_to_counts.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# CardioPipeLine (c) 2020
# Author: Rohit Suratekar, ZDG Lab, IIMCB
#
# Generates count files for every sample
library("tximport")
suppressPackageStartupMessages(library("AnnotationDbi"))
tx <- GenomicFeatures::makeTxDbFromGFF(file = snakemake@input$gtf,
format = "gtf",
organism = snakemake@params$animal)
k <- AnnotationDbi::keys(tx, keytype = "GENEID")
tx_df <- AnnotationDbi::select(tx,
keys = k,
keytype = "GENEID",
columns = "TXNAME")
tx2gene <- tx_df[, 2:1]
txi <- tximport::tximport(snakemake@input$file,
type = snakemake@params$method,
tx2gene = tx2gene,
ignoreTxVersion = TRUE)
colnames(txi$counts) <- snakemake@wildcards$SRR_ID
# Tximport will generate fractions. We need to convert them to integers by
# just rounding them. This is what the original class does
# https://github.com/mikelove/DESeq2/blob/master/R/AllClasses.R
txi$counts <- round(txi$counts)
df <- data.frame(txi$counts)
data.table::setDT(df, keep.rownames = "gene_id")
fname <- paste(snakemake@wildcards$BASE, "deseq2", "counts",
snakemake@wildcards$SRR_ID, sep = "/")
fname <- paste0(fname, "/", snakemake@wildcards$SRR_ID, ".",
snakemake@params$method, ".counts")
write.csv(df, file = fname, row.names = FALSE)