Skip to contents

Returns one row per gene and sample, with the columns gene, sample_id and value, followed by every column of the sample table. This is the layout that ggplot2 and dplyr work with: a group or a continuous variable in the sample table can go straight to colour, facets or the x axis.

Usage

rsem_long(x, metadata = NULL)

Arguments

x

The gene by sample table, as returned by rsem_read() or the other functions of this package.

metadata

The sample table: a path to a CSV file, or a data frame, with a sample_id column. Optional; without it only gene, sample_id and value are returned.

Value

A data frame with nrow(x) * ncol(x) rows.

Sample table

The sample table is the same metadata.csv that rsem_read() uses (or a data frame). It needs a sample_id column; every other column is added as it is, with no setting per column: columns of numbers stay numeric (for example age or dose) and other columns stay text (for example group or batch). sample_id is always read as text, so 001 stays 001.

Every sample in x must be in the sample table. Rows of the sample table whose sample is not in x (for example samples removed earlier) are left out, and a message says so.

sample_id is a factor in the column order of x, so plots show the samples in that order. Each gene's rows are next to each other.

Examples

if (requireNamespace("org.Hs.eg.db", quietly = TRUE)) {
  ex <- system.file("extdata", "example", package = "rsemverse")
  meta <- file.path(ex, "metadata.csv")
  tpm <- rsem_read(file.path(ex, "data"), meta, species = "human", expr = "tpm")
  long <- rsem_long(rsem_transform(tpm, "log2"), metadata = meta)
  head(long)
  # ggplot2::ggplot(subset(long, gene == "TP53"),
  #                 ggplot2::aes(sample_id, value)) + ggplot2::geom_col()
}
#> 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_transform: 43 genes x 3 samples; log2(value + 1)
#> rsem_long: 129 rows (43 genes x 3 samples) with 1 sample column(s): ref (text)
#>    gene sample_id    value              ref
#> 1  TP53        S1 13.73136 S1.genes.results
#> 2  TP53        S2 14.03399 S2.genes.results
#> 3  TP53        S3 13.64539 S3.genes.results
#> 4 GAPDH        S1 14.21372 S1.genes.results
#> 5 GAPDH        S2 13.56937 S2.genes.results
#> 6 GAPDH        S3 13.63571 S3.genes.results