forked from andysaurin/tpm_rpkm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpm_rpkm.R
executable file
·65 lines (45 loc) · 1.68 KB
/
tpm_rpkm.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env Rscript
# Author: Andy Saurin ([email protected])
#
# Simple RScript to calculate RPKMs and TPMs
# based on method for RPKM/TPM calculations shown in http://www.rna-seqblog.com/rpkm-fpkm-and-tpm-clearly-explained/
#
# The input file is the output of featureCounts
#
rpkm <- function(counts, lengths) {
pm <- sum(counts) /1e6
rpm <- counts/pm
rpm/(lengths/1000)
}
tpm <- function(counts, lengths) {
rpk <- counts/(lengths/1000)
coef <- sum(rpk) / 1e6
rpk/coef
}
## read table from featureCounts output
args <- commandArgs(T)
tag <- tools::file_path_sans_ext(args[1])
cat('Reading in featureCounts data...')
ftr.cnt <- read.table(args[1], sep="\t", header=T, quote="") #Important to disable default quote behaviour or else genes with apostrophes will be taken as strings
cat(' Done\n')
if ( ncol(ftr.cnt) < 7 ) {
cat(' The input file is not the raw output of featureCounts (number of columns > 6) \n')
quit('no')
}
lengths = ftr.cnt[,6]
counts <- ftr.cnt[,7:ncol(ftr.cnt)]
cat('Performing RPKM calculations...')
rpkms <- apply(counts, 2, function(x) rpkm(x, lengths) )
ftr.rpkm <- cbind(ftr.cnt[,1:6], rpkms)
rpkms <- apply(counts, 2, function(x) rpkm(x, lengths) )
ftr.rpkm <- cbind(ftr.cnt[,1:6], rpkms)
write.table(ftr.rpkm, file=paste0(tag, "_rpkm.txt"), sep="\t", row.names=FALSE, quote=FALSE)
cat(' Done.\n\tSaved as ')
cat ( paste0(tag, "_rpkm.txt", '\n') )
cat('Performing TPM calculations...')
tpms <- apply(counts, 2, function(x) tpm(x, lengths) )
ftr.tpm <- cbind(ftr.cnt[,1:6], tpms)
write.table(ftr.tpm, file=paste0(tag, "_tpm.txt"), sep="\t", row.names=FALSE, quote=FALSE)
cat(' Done.\n\tSaved as ')
cat ( paste0(tag, "_tpm.txt", '\n') )
quit('no')