参考
简书 | 脚本 | R | 转录组 counts 2 tpm
# 导入包
library(tidyverse)
# 导入数据
countsMatrix <- read_tsv("Counts.Length.tsv")
# 宽变长,按样本分组,根据counts值和length计算tpm,长变宽
tpmMatrix <- countsMatrix %>%
pivot_longer(c(-Geneids, -Length),
names_to = "Group",
values_to = "SampleCounts") %>%
group_by(Group) %>%
mutate(SampleTPM = (((SampleCounts/Length)*1e6)/sum(SampleCounts/Length))) %>%
pivot_wider(id_cols = "Geneids",
names_from = "Group",
values_from = "SampleTPM")
# 导出tpm矩阵
write_tsv(tpmMatrix, "TPM.tsv")