tpm rpkm
# countMatrix is a matrix of read counts from featureCounts or other similar tools# geneLength is a data frame with first column as gene id and second column as gene length# TPM ---------------------------------------------------------------------# 20190505 | update on 20200817TPM <- function(countMatrix, geneLength) {library(tidyverse)library(data.table)geneLength <- geneLength %>%as.data.table() %>%set_names(c("geneID","gLength"))# sort gene ordergeneLength <- geneLength[match(rownames(countMatrix), geneLength$geneID),]# calculate tpmtmp <- countMatrix/(geneLength$gLength / 1000)col_sum_perM <- colSums(tmp) / 1e6tmp1 <- sweep(tmp, 2, col_sum_perM, `/`)}# RPKM --------------------------------------------------------------------# 20190604RPKM <- function(countMatrix, geneLength) {library(tidyverse)library(data.table)geneLength <- geneLength %>%as.data.table() %>%set_names(c("geneID","gLength"))# sort gene ordergeneLength <- geneLength[match(rownames(countMatrix), geneLength$geneID),]# calculate rpkmcol_sum_perM <- colSums(countMatrix) / 1e6tmp <- sweep(countMatrix, 2, col_sum_perM, `/`)tmp1 <- tmp/(geneLength$gLength / 1000)}
