Skip to content

Instantly share code, notes, and snippets.

@drigodwin
Last active October 12, 2017 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drigodwin/07d8bbdb81ae2de6e35afa663098799f to your computer and use it in GitHub Desktop.
Save drigodwin/07d8bbdb81ae2de6e35afa663098799f to your computer and use it in GitHub Desktop.
tpm <- function(counts, lengths) {
rate <- counts / lengths
rate / sum(rate) * 1e6
}
ftr.cnt <- read.table("counts.txt", sep="\t", stringsAsFactors=FALSE,
header=TRUE)
library(dplyr)
library(tidyr)
ftr.tpm <- ftr.cnt %>%
gather(sample, cnt, 3:ncol(ftr.cnt)) %>%
mutate(tpm=tpm(cnt, Length)) %>%
select(-cnt) %>%
spread(sample, tpm)
write.table(ftr.tpm, file=tpm.esc.txt, sep="\t", row.names=FALSE, quote=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment