Question: Raw counts to TPM in R
0
gravatar for rmf
5 months ago by
rmf540
rmf540 wrote:

Can someone verify if this R code for converting raw counts to TPM is correct?

#' @title Compute TPM for a read count matrix
#' @param dfr A numeric data.frame of read counts with samples (columns) and genes (rows).
#' @param len A vector of gene cds length equal to number of rows of dfr.
#' 
r_tpm <- function(dfr,len)
{
  dfr1 <- sweep(dfr,MARGIN=1,(len/10^4),`/`)
  scf <- colSums(dfr1)/(10^6)
  return(sweep(dfr1,2,scf,`/`))
}
rna-seq R • 1.4k views
ADD COMMENTlink written 5 months ago by rmf540

Do you have a reason for suspecting it's not? Have you tested it? What do your tests reveal?

ADD REPLYlink written 5 months ago by RamRS20k
6
gravatar for ATpoint
5 months ago by
ATpoint13k
Germany
ATpoint13k wrote:

Use this code snippet from Michael Love.

ADD COMMENTlink written 5 months ago by ATpoint13k
2

Cool! I get the same result.

# michael's version
# https://support.bioconductor.org/p/91218/

tpm3 <- function(counts,len) {
  x <- counts/len
  return(t(t(x)*1e6/colSums(x)))
}

Michael's version is much faster despite all the transposes.

enter image description here

ADD REPLYlink modified 5 months ago by RamRS20k • written 5 months ago by rmf540

If an answer was helpful you should upvote it, if the answer resolved your question you should mark it as accepted.
Upvote|Bookmark|Accept

ADD REPLYlink written 5 months ago by Kevin Blighe37k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1903 users visited in the last hour