Reads RSEM *.genes.results (or *.isoforms.results) files and returns
one value per gene symbol and sample: the expected count, the TPM, the
effective length or, for transcripts, the isoform percentage.
Usage
rsem_read(
data = "data",
metadata = "metadata.csv",
species,
expr,
level = "gene",
id = "symbol",
sample_from = "filename",
validate_reference = TRUE,
allow_reorder = FALSE,
annotation = NULL
)Arguments
- data
Directory that contains the RSEM files.
- metadata
Path to the metadata CSV, or
NULLto find the files indata(see Input).- species
"human"or"mouse". Required withid = "symbol"; the function stops if the gene IDs are of the other species. Not used withid = "original", which works for any organism.- expr
"count"(expected_count),"tpm"(TPM),"fpkm"(FPKM),"length"(effective_length) or, for transcripts,"isopct"(IsoPct); with--calc-pmeor--calc-cialso the posterior values"pme_count"(posterior_mean_count),"pme_tpm"(pme_TPM),"pme_fpkm"(pme_FPKM),"count_sd"(posterior_standard_deviation_of_count) and, for transcripts,"isopct_pme"(IsoPct_from_pme_TPM); with--calc-cithe credibility interval"tpm_ci_lower","tpm_ci_upper","fpkm_ci_lower","fpkm_ci_upper"and the coefficient of quartile variation"tpm_cqv","fpkm_cqv"((Q3 - Q1) / (Q3 + Q1)of the posterior samples; the level of the interval is set by--ci-credibility-level, 95% by default, and is not recorded in the file). See Posterior estimates. Required.- level
"gene"(*.genes.results, the default) or"isoform"(*.isoforms.results).- id
"symbol"(the default),"original"(the IDs of the file) or"annotation"(names fromannotation).- sample_from
With
metadata = NULL:"filename"(the default) or"parent_dir".- validate_reference
Check that every file lists the same transcripts for each gene (the column
transcript_id(s)), as set; files made with different references stop the function. DefaultTRUE; skipped when the column is missing.- allow_reorder
Accept files that have the same IDs in a different order, and put them in the order of the first file. Default
FALSE: a different order stops the function, because it usually means the files do not belong together.- annotation
With
id = "annotation": a GTF file or a data frame with the columnsgene_idandgene_name, as inrsem_biotype_profile(); for a RefSeq GFF3 reference, the GTF thatrsem-prepare-referencewrites.
Value
A data frame with one row per gene symbol (or gene ID, or transcript; as row names) and one column per sample, in the order of the metadata. A message reports how many genes received a symbol.
Input
metadata is a CSV with the columns sample_id and ref; other columns
are ignored. ref is the name of the sample's RSEM file inside data
(that is, the file read is file.path(data, ref)).
With metadata = NULL the files are found instead: every
*.genes.results (or *.isoforms.results, with or without .gz) file under data, searched
recursively. The sample ID is the file name without the suffix
(sample_from = "filename") or the name of the folder that holds the file
(sample_from = "parent_dir", for layouts such as S1/rsem.genes.results).
The files and the sample IDs found are printed, sorted by sample ID, and
duplicated sample IDs stop the function rather than overwrite each other.
Groups are never guessed from file names; give them in the sample table of
rsem_long() or rsem_fit_reference().
Transcript files must give every transcript the same gene in all samples; a file from a different reference stops the function.
Every file needs the column gene_id (transcript_id for transcripts) and
the column expr selects; other columns are ignored. All files must
contain the same rows in the same order, without duplicates. A listed file
that is missing stops the function; no sample is dropped silently.
Gene symbols
With id = "symbol" (the default), symbols come from org.Hs.eg.db
(human) or org.Mm.eg.db (mouse). The version suffix of an Ensembl gene ID
(ENSG00000141510.18) is removed before the lookup, because the OrgDb
keys have no version. The rules are:
An Ensembl ID with several symbols gets the symbol of its lowest Entrez ID.
Rows that end up with the same symbol are summed (counts and TPM are both additive), never reduced to their maximum. Effective lengths are not additive: they are averaged with the TPM of each row as weight, the way RSEM averages the lengths of a gene's transcripts (a plain mean where the rows have no TPM in that sample).
A gene without a symbol (not in the OrgDb, or not an Ensembl ID, such as a spike-in or an ID ending in
_PAR_Y) keeps itsgene_idfrom the file, so no expression is lost. If such an ID equals a gene symbol, the function stops instead of merging the two.
The original IDs behind every row are kept in the attribute "gene_ids"
(a data frame with the columns gene and gene_id); rsem_write() can
write them next to the values. Summing happens on the original scale,
before any log transformation: log2(1 + TPM_a + TPM_b) is not the sum of
log2(1 + TPM_a) and log2(1 + TPM_b).
With id = "original", rows keep the gene_id of the file and no OrgDb or
species is needed, so results for any organism or a de novo transcriptome
can be read.
Files made with rsem-calculate-expression --append-names have IDs such
as ENSG00000141510.18_TP53; the version and the appended name are removed
before the lookup, and rows without a symbol keep the full ID of the file.
Files may be gzip-compressed (*.genes.results.gz).
Rows appear in the order in which they first occur in the files. Values are not rounded and TPM is not renormalized.
Names from an annotation file
RefSeq-based references have IDs such as gene1000, which say nothing
about the gene; rsem-calculate-expression --append-names appends the name
(gene1000_TP53). With id = "annotation" and the GTF of the reference
(or a data frame), each row is named by the gene_name of its gene_id.
A file ID is matched to the annotation as it is, or without its Ensembl
version, or as <gene_id>_<gene_name> (the form --append-names writes):
the part before an underscore must be a gene_id of the annotation and the
part after it exactly that gene's gene_name. An ID such as gene1_2 is
therefore not taken for gene1, and underscores inside IDs or names are
not cut at the wrong place. Rows that match nothing, or
have no name, keep their ID. Rows with the same name are summed, as for
symbols (this includes the _PAR_Y copies of pseudoautosomal genes in
GENCODE, which the OrgDb route keeps under their IDs), and
attr(x, "gene_ids") keeps the original IDs. Names from an annotation and
from the OrgDb can differ for some genes.
Checks on the files
Every file must list the same genes (transcripts) as the first, in the same
order, and, with validate_reference, the same transcripts for each gene
(gene-level files) or the same gene for each transcript (isoform files):
the same gene ID with a different set of transcripts means the files were
quantified against different references, and their values are not
comparable. rsem_check() reports such problems for all files at once
instead of stopping at the first.
Posterior estimates
With --calc-pme or --calc-ci, RSEM adds posterior means, their standard
deviation and credibility intervals. "pme_count", "pme_tpm" and
"pme_fpkm" are posterior means and add up over rows like counts. The
standard deviation, the interval bounds, the coefficient of quartile
variation and "isopct_pme" do not add up (the interval of a sum is not the sum
of the intervals), so they need id = "original" or level = "isoform"
and are never merged. A file without the column stops with the option to
use. A coefficient of quartile variation that RSEM could not compute (both
quartiles are 0, written as nan) is NA.
Effective length
expr = "length" reads effective_length. RSEM sets it to 0 for a
transcript shorter than the mean fragment length, and the gene-level value
is an average over transcripts weighted by their abundance, so a gene can
have length 0 in some samples and a positive length in others.
tximport and DESeq2::DESeqDataSetFromTximport() stop on such zeros. The
function reports how many genes have a zero length and keeps them as 0
(attribute "zero_length": the number of zero genes per sample). Common
choices are to remove those genes or to import the transcript-level
results; replacing 0 by 1 changes the length offset of those genes. See
rsem_length_profile().
Transcripts
With level = "isoform", the *.isoforms.results files are read; rows are
transcript IDs as in the file and are never merged. expr can also be
"isopct" (the transcript's share of its gene's abundance, in percent) and
"isopct_pme" (the same from the posterior mean TPM).
The attribute "tx2gene" holds the transcript-to-gene table
(transcript_id, gene_id, and gene: the symbol with id = "symbol",
else the gene_id), which rsem_select_isoforms() uses.
Examples
if (requireNamespace("org.Hs.eg.db", quietly = TRUE)) {
ex <- system.file("extdata", "example", package = "rsemverse")
tpm <- rsem_read(
data = file.path(ex, "data"),
metadata = file.path(ex, "metadata.csv"),
species = "human",
expr = "tpm"
)
head(tpm)
# find the files instead of listing them
tpm2 <- rsem_read(file.path(ex, "data"), metadata = NULL,
species = "human", expr = "tpm")
}
#> rsem_read: 3 samples, 44 genes -> 43 rows (species = human, expr = tpm, org.Hs.eg.db 3.23.1)
#> with symbol: 42 genes (1 symbols merged from several IDs); kept as original IDs: 2 genes
#> rsem_read: found 3 files under '/home/runner/work/_temp/Library/rsemverse/extdata/example/data' (sample ID from the file name):
#> S1 <- S1.genes.results
#> S2 <- S2.genes.results
#> S3 <- S3.genes.results
#> rsem_read: 3 samples, 44 genes -> 43 rows (species = human, expr = tpm, org.Hs.eg.db 3.23.1)
#> with symbol: 42 genes (1 symbols merged from several IDs); kept as original IDs: 2 genes