参考

简书 | 脚本 | R | 转录组 counts 2 tpm


  1. # 导入包
  2. library(tidyverse)
  3. # 导入数据
  4. countsMatrix <- read_tsv("Counts.Length.tsv")
  5. # 宽变长,按样本分组,根据counts值和length计算tpm,长变宽
  6. tpmMatrix <- countsMatrix %>%
  7. pivot_longer(c(-Geneids, -Length),
  8. names_to = "Group",
  9. values_to = "SampleCounts") %>%
  10. group_by(Group) %>%
  11. mutate(SampleTPM = (((SampleCounts/Length)*1e6)/sum(SampleCounts/Length))) %>%
  12. pivot_wider(id_cols = "Geneids",
  13. names_from = "Group",
  14. values_from = "SampleTPM")
  15. # 导出tpm矩阵
  16. write_tsv(tpmMatrix, "TPM.tsv")