tximport imports transcript-level quantifications and summarizes them to the gene level for differential expression analysis with DESeq2 or edgeR.
if (!require('BiocManager', quietly = TRUE))
install.packages('BiocManager')
BiocManager::install('tximport')
BiocManager::install('tximeta') # Optional: metadata-aware import
BiocManager::install('txdbmaker') # For makeTxDbFromGFF (Bioconductor >= 3.19; older: GenomicFeatures)
Tell your AI agent what you want to do: - "Import my Salmon quantification results into R" - "Summarize transcript counts to gene level for DESeq2" - "Create a tx2gene mapping from my GTF file"
"Import Salmon quant.sf files from my samples and create a gene-level count matrix"
"Load my kallisto results into R for differential expression analysis"
"Create a transcript-to-gene mapping from my Ensembl GTF file"
"Build tx2gene using biomaRt for human transcripts"
"Import Salmon results and create a DESeqDataSet ready for analysis"
"Use tximeta to automatically link my Salmon results to annotations"
The tx2gene data frame must have exactly two columns:
- TXNAME - Transcript identifiers matching your quant files
- GENEID - Gene identifiers for summarization
library(txdbmaker) # makeTxDbFromGFF moved here in Bioconductor >= 3.19 (older Bioc: GenomicFeatures)
txdb <- makeTxDbFromGFF('Homo_sapiens.GRCh38.110.gtf.gz')
k <- keys(txdb, keytype = 'TXNAME')
tx2gene <- AnnotationDbi::select(txdb, k, c('TXNAME', 'GENEID'), 'TXNAME')
write.csv(tx2gene, 'tx2gene.csv', row.names = FALSE)
library(biomaRt)
mart <- useEnsembl(biomart = 'genes', dataset = 'hsapiens_gene_ensembl') # useMart is deprecated
tx2gene <- getBM(
attributes = c('ensembl_transcript_id_version', 'ensembl_gene_id_version'),
mart = mart
)
colnames(tx2gene) <- c('TXNAME', 'GENEID')
# If transcript IDs are like ENST00000456328.2|ENSG00000223972.5
quant <- read.table('sample1_quant/quant.sf', header = TRUE)
tx2gene <- data.frame(
TXNAME = quant$Name,
GENEID = sapply(strsplit(quant$Name, '\\|'), '[', 2)
)
library(tximport)
library(DESeq2)
# 1. Set up files
sample_names <- c('ctrl1', 'ctrl2', 'treat1', 'treat2')
files <- file.path(paste0(sample_names, '_quant'), 'quant.sf')
names(files) <- sample_names
# 2. Load tx2gene
tx2gene <- read.csv('tx2gene.csv')
# 3. Import
txi <- tximport(files, type = 'salmon', tx2gene = tx2gene)
# 4. Create metadata
coldata <- data.frame(
condition = factor(c('control', 'control', 'treated', 'treated')),
row.names = sample_names
)
# 5. Create DESeqDataSet
dds <- DESeqDataSetFromTximport(txi, colData = coldata, design = ~ condition)
# 6. Filter and analyze
dds <- dds[rowSums(counts(dds)) >= 10, ]
dds <- DESeq(dds)
res <- results(dds)
tximeta automatically links to annotation databases:
library(tximeta)
coldata <- data.frame(
files = files,
names = sample_names,
condition = c('control', 'control', 'treated', 'treated')
)
# Import with automatic annotation
se <- tximeta(coldata)
# Summarize to gene level
gse <- summarizeToGene(se)
# Get gene symbols
library(org.Hs.eg.db)
gse <- addIds(gse, 'SYMBOL', gene = TRUE)
ignoreTxVersion = TRUE.'no' (default) for full-length + DESeq2/edgeR; 'lengthScaledTPM' for limma-voom; 'scaledTPM' with txOut=TRUE for DTU; for 3'-tag build the DDS via DESeqDataSetFromMatrix(round(txi$counts), ...) so the length offset is not applied.