knitr::opts_chunk$set( fig.path = "man/figures/")
Brings transcriptomics to the tidyverse!
The code is released under the version 3 of the GNU General Public License.
knitr::include_graphics("man/figures/logo.png")
website: stemangiola.github.io/tidybulk/ Third party tutorials Please have a look also to
knitr::include_graphics("man/figures/new_SE_usage-01.png")
Function | Description
------------ | -------------
aggregate_duplicates
| Aggregate abundance and annotation of duplicated transcripts in a robust way
identify_abundant
keep_abundant
| identify or keep the abundant genes
keep_variable
| Filter for top variable features
scale_abundance
| Scale (normalise) abundance for RNA sequencing depth
reduce_dimensions
| Perform dimensionality reduction (PCA, MDS, tSNE, UMAP)
cluster_elements
| Labels elements with cluster identity (kmeans, SNN)
remove_redundancy
| Filter out elements with highly correlated features
adjust_abundance
| Remove known unwanted variation (Combat)
test_differential_abundance
| Differential transcript abundance testing (DESeq2, edgeR, voom)
deconvolve_cellularity
| Estimated tissue composition (Cibersort, llsr, epic, xCell, mcp_counter, quantiseq
test_differential_cellularity
| Differential cell-type abundance testing
test_stratification_cellularity
| Estimate Kaplan-Meier survival differences
test_gene_enrichment
| Gene enrichment analyses (EGSEA)
test_gene_overrepresentation
| Gene enrichment on list of transcript names (no rank)
test_gene_rank
| Gene enrichment on list of transcript (GSEA)
impute_missing_abundance
| Impute abundance for missing data points using sample groupings
Utilities | Description
------------ | -------------
get_bibliography
| Get the bibliography of your workflow
tidybulk
| add tidybulk attributes to a tibble object
tidybulk_SAM_BAM
| Convert SAM BAM files into tidybulk tibble
pivot_sample
| Select sample-wise columns/information
pivot_transcript
| Select transcript-wise columns/information
rotate_dimensions
| Rotate two dimensions of a degree
ensembl_to_symbol
| Add gene symbol from ensembl IDs
symbol_to_entrez
| Add entrez ID from gene symbol
describe_transcript
| Add gene description from gene symbol
All functions are directly compatibble with SummarizedExperiment
object.
library(dplyr) library(tidyr) library(tibble) library(magrittr) library(ggplot2) library(ggrepel) library(tidybulk) library(tidySummarizedExperiment) library(here) my_theme = theme_bw() + theme( panel.border = element_blank(), axis.line = element_line(), panel.grid.major = element_line(size = 0.2), panel.grid.minor = element_line(size = 0.1), text = element_text(size=12), legend.position="bottom", aspect.ratio=1, strip.background = element_blank(), axis.title.x = element_text(margin = margin(t = 10, r = 10, b = 10, l = 10)), axis.title.y = element_text(margin = margin(t = 10, r = 10, b = 10, l = 10)) ) utils::download.file("https://zenodo.org/records/11201167/files/counts_SE.rda?download=1", destfile = "counts_SE.rda") here("counts_SE.rda") |> load() tibble_counts = counts_SE |> as_tibble()
From Bioconductor
BiocManager::install("tidybulk")
From Github
devtools::install_github("stemangiola/tidybulk")
We will use a SummarizedExperiment
object
counts_SE
Loading tidySummarizedExperiment
will automatically abstract this object as tibble
, so we can display it and manipulate it with tidy tools. Although it looks different, and more tools (tidyverse) are available to us, this object is in fact a SummarizedExperiment
object.
class(counts_SE)
First of all, you can cite all articles utilised within your workflow automatically from any tidybulk tibble
counts_SE |> get_bibliography()
transcripts
tidybulk provide the aggregate_duplicates
function to aggregate duplicated transcripts (e.g., isoforms, ensembl). For example, we often have to convert ensembl symbols to gene/transcript symbol, but in doing so we have to deal with duplicates. aggregate_duplicates
takes a tibble and column names (as symbols; for sample
, transcript
and count
) as arguments and returns a tibble with transcripts with the same name aggregated. All the rest of the columns are appended, and factors and boolean are appended as characters.
counts
We may want to compensate for sequencing depth, scaling the transcript abundance (e.g., with TMM algorithm, Robinson and Oshlack doi.org/10.1186/gb-2010-11-3-r25). scale_abundance
takes a tibble, column names (as symbols; for sample
, transcript
and count
) and a method as arguments and returns a tibble with additional columns with scaled data as <NAME OF COUNT COLUMN>_scaled
.
counts_SE.norm |> select(`count`, count_scaled, .abundant, everything())
We can easily plot the scaled density to check the scaling outcome. On the x axis we have the log scaled counts, on the y axes we have the density, data is grouped by sample and coloured by cell type.
counts_SE.norm |> ggplot(aes(count_scaled + 1, group=.sample, color=`Cell.type`)) + geom_density() + scale_x_log10() + my_theme
variable transcripts
We may want to identify and filter variable transcripts.
dimensions
We may want to reduce the dimensions of our data, for example using PCA or MDS algorithms. reduce_dimensions
takes a tibble, column names (as symbols; for sample
, transcript
and count
) and a method (e.g., MDS or PCA) as arguments and returns a tibble with additional columns for the reduced dimensions.
MDS (Robinson et al., 10.1093/bioinformatics/btp616)
On the x and y axes axis we have the reduced dimensions 1 to 3, data is coloured by cell type.
counts_SE.norm.MDS |> pivot_sample() |> select(contains("Dim"), everything()) counts_SE.norm.MDS |> pivot_sample() |> GGally::ggpairs(columns = 6:(6+5), ggplot2::aes(colour=`Cell.type`))
PCA
On the x and y axes axis we have the reduced dimensions 1 to 3, data is coloured by cell type.
counts_SE.norm.PCA |> pivot_sample() |> select(contains("PC"), everything()) counts_SE.norm.PCA |> pivot_sample() |> GGally::ggpairs(columns = 11:13, ggplot2::aes(colour=`Cell.type`))
tSNE
counts_SE.norm.tSNE = breast_tcga_mini_SE |> identify_abundant() |> reduce_dimensions( method = "tSNE", perplexity=10, pca_scale =TRUE )
count_m_log = log(count_m + 1) tsne = Rtsne::Rtsne( t(count_m_log), perplexity=10, pca_scale =TRUE )$Y tsne$cell_type = tibble_counts[ match(tibble_counts$sample, rownames(tsne)), "Cell.type" ]
Plot
counts_SE.norm.tSNE |> pivot_sample() |> select(contains("tSNE"), everything()) counts_SE.norm.tSNE |> pivot_sample() |> ggplot(aes(x = `tSNE1`, y = `tSNE2`, color=Call)) + geom_point() + my_theme
dimensions
We may want to rotate the reduced dimensions (or any two numeric columns really) of our data, of a set angle. rotate_dimensions
takes a tibble, column names (as symbols; for sample
, transcript
and count
) and an angle as arguments and returns a tibble with additional columns for the rotated dimensions. The rotated dimensions will be added to the original data set as <NAME OF DIMENSION> rotated <ANGLE>
by default, or as specified in the input arguments.
counts_SE.norm.MDS.rotated = counts_SE.norm.MDS |> rotate_dimensions(`Dim1`, `Dim2`, rotation_degrees = 45, action="get")
rotation = function(m, d) { r = d * pi / 180 ((bind_rows( c(`1` = cos(r), `2` = -sin(r)), c(`1` = sin(r), `2` = cos(r)) ) |> as_matrix()) %*% m) } mds_r = pca |> rotation(rotation_degrees) mds_r$cell_type = counts[ match(counts$sample, rownames(mds_r)), "Cell.type" ]
Original On the x and y axes axis we have the first two reduced dimensions, data is coloured by cell type.
counts_SE.norm.MDS.rotated |> ggplot(aes(x=`Dim1`, y=`Dim2`, color=`Cell.type` )) + geom_point() + my_theme
Rotated On the x and y axes axis we have the first two reduced dimensions rotated of 45 degrees, data is coloured by cell type.
counts_SE.norm.MDS.rotated |> pivot_sample() |> ggplot(aes(x=`Dim1_rotated_45`, y=`Dim2_rotated_45`, color=`Cell.type` )) + geom_point() + my_theme
differential abundance
We may want to test for differential transcription between sample-wise factors of interest (e.g., with edgeR). test_differential_abundance
takes a tibble, column names (as symbols; for sample
, transcript
and count
) and a formula representing the desired linear model as arguments and returns a tibble with additional columns for the statistics from the hypothesis test (e.g., log fold change, p-value and false discovery rate).
counts_SE.de = counts_SE |> test_differential_abundance( ~ condition, action="get") counts_SE.de
library(edgeR) dgList <- DGEList(counts=counts_m,group=group) keep <- filterByExpr(dgList) dgList <- dgList[keep,,keep.lib.sizes=FALSE] dgList <- calcNormFactors(dgList) design <- model.matrix(~group) dgList <- estimateDisp(dgList,design) fit <- glmQLFit(dgList,design) qlf <- glmQLFTest(fit,coef=2) topTags(qlf, n=Inf)
The functon test_differential_abundance
operated with contrasts too. The constrasts hve the name of the design matrix (generally
counts_SE.de = counts_SE |> identify_abundant(factor_of_interest = condition) |> test_differential_abundance( ~ 0 + condition, .contrasts = c( "conditionTRUE - conditionFALSE"), action="get" )
counts
We may want to adjust counts
for (known) unwanted variation. adjust_abundance
takes as arguments a tibble, column names (as symbols; for sample
, transcript
and count
) and a formula representing the desired linear model where the first covariate is the factor of interest and the second covariate is the unwanted variation, and returns a tibble with additional columns for the adjusted counts as <COUNT COLUMN>_adjusted
. At the moment just an unwanted covariated is allowed at a time.
Cell type composition
We may want to infer the cell type composition of our samples (with the algorithm Cibersort; Newman et al., 10.1038/nmeth.3337). deconvolve_cellularity
takes as arguments a tibble, column names (as symbols; for sample
, transcript
and count
) and returns a tibble with additional columns for the adjusted cell type proportions.
With the new annotated data frame, we can plot the distributions of cell types across samples, and compare them with the nominal cell type labels to check for the purity of isolation. On the x axis we have the cell types inferred by Cibersort, on the y axis we have the inferred proportions. The data is facetted and coloured by nominal cell types (annotation given by the researcher after FACS sorting).
counts_SE.cibersort |> pivot_longer( names_to= "Cell_type_inferred", values_to = "proportion", names_prefix ="cibersort__", cols=contains("cibersort__") ) |> ggplot(aes(x=`Cell_type_inferred`, y=proportion, fill=`Cell.type`)) + geom_boxplot() + facet_wrap(~`Cell.type`) + my_theme + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), aspect.ratio=1/5)
We can also perform a statistical test on the differential cell-type abundance across conditions
counts_SE |> test_differential_cellularity(. ~ condition )
We can also perform regression analysis with censored data (coxph).
# Add survival data counts_SE_survival = counts_SE |> nest(data = -sample) |> mutate( days = sample(1:1000, size = n()), dead = sample(c(0,1), size = n(), replace = TRUE) ) |> unnest(data) # Test counts_SE_survival |> test_differential_cellularity(survival::Surv(days, dead) ~ .)
We can also perform test of Kaplan-Meier curves.
counts_stratified = counts_SE_survival |> # Test test_stratification_cellularity( survival::Surv(days, dead) ~ ., sample, transcript, count ) counts_stratified
Plot Kaplan-Meier curves
counts_stratified$plot[[1]]
samples
We may want to cluster our data (e.g., using k-means sample-wise). cluster_elements
takes as arguments a tibble, column names (as symbols; for sample
, transcript
and count
) and returns a tibble with additional columns for the cluster annotation. At the moment only k-means clustering is supported, the plan is to introduce more clustering methods.
k-means
We can add cluster annotation to the MDS dimension reduced data set and plot.
counts_SE.norm.cluster |> ggplot(aes(x=`Dim1`, y=`Dim2`, color=`cluster_kmeans`)) + geom_point() + my_theme
SNN
Matrix package (v1.3-3) causes an error with Seurat::FindNeighbors used in this method. We are trying to solve this issue. At the moment this option in unaviable.
counts_SE.norm.SNN |> pivot_sample() |> select(contains("tSNE"), everything()) counts_SE.norm.SNN |> pivot_sample() |> gather(source, Call, c("cluster_SNN", "Call")) |> distinct() |> ggplot(aes(x = `tSNE1`, y = `tSNE2`, color=Call)) + geom_point() + facet_grid(~source) + my_theme # Do differential transcription between clusters counts_SE.norm.SNN |> mutate(factor_of_interest = `cluster_SNN` == 3) |> test_differential_abundance( ~ factor_of_interest, action="get" )
redundant
transcriptsWe may want to remove redundant elements from the original data set (e.g., samples or transcripts), for example if we want to define cell-type specific signatures with low sample redundancy. remove_redundancy
takes as arguments a tibble, column names (as symbols; for sample
, transcript
and count
) and returns a tibble with redundant elements removed (e.g., samples). Two redundancy estimation approaches are supported:
Approach 1
We can visualise how the reduced redundancy with the reduced dimentions look like
counts_SE.norm.non_redundant |> pivot_sample() |> ggplot(aes(x=`Dim1`, y=`Dim2`, color=`Cell.type`)) + geom_point() + my_theme
Approach 2
counts_SE.norm.non_redundant = counts_SE.norm.MDS |> remove_redundancy( method = "reduced_dimensions", Dim_a_column = `Dim1`, Dim_b_column = `Dim2` )
We can visualise MDS reduced dimensions of the samples with the closest pair removed.
counts_SE.norm.non_redundant |> pivot_sample() |> ggplot(aes(x=`Dim1`, y=`Dim2`, color=`Cell.type`)) + geom_point() + my_theme
The above wrapper streamline the most common processing of bulk RNA sequencing data. Other useful wrappers are listed above.
We can calculate gene counts (using FeatureCounts; Liao Y et al., 10.1093/nar/gkz114) from a list of BAM/SAM files and format them into a tidy structure (similar to counts).
counts = tidybulk_SAM_BAM( file_names, genome = "hg38", isPairedEnd = TRUE, requireBothEndsMapped = TRUE, checkFragLength = FALSE, useMetaFeatures = TRUE )
We can add gene symbols from ensembl identifiers. This is useful since different resources use ensembl IDs while others use gene symbol IDs. This currently works for human and mouse.
counts_ensembl |> ensembl_to_symbol(ens)
We can add gene full name (and in future description) from symbol identifiers. This currently works for human and mouse.
counts_SE |> describe_transcript() |> select(feature, description, everything())
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.