Title: | Visualization of Clonal Expansion for Single Cell Immune Profiles |
---|---|
Description: | Visualize clonal expansion via circle-packing. 'APackOfTheClones' extends 'scRepertoire' to produce a publication-ready visualization of clonal expansion at a single cell resolution, by representing expanded clones as differently sized circles. The method was originally implemented by Murray Christian and Ben Murrell in the following immunology study: Ma et al. (2021) <doi:10.1126/sciimmunol.abg6356>. |
Authors: | Qile Yang [cre, aut, cph] |
Maintainer: | Qile Yang <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.2.4 |
Built: | 2024-11-18 23:50:25 UTC |
Source: | https://github.com/qile0317/apackoftheclones |
If the user is unsatisfied with the clonal expansion plot that
was generated from RunAPOTC()
and APOTCPlot()
, this function has a range of
arguments to modify the data and/or parameters of the visualization. Note
that some of the arguments may conflict with each other.
AdjustAPOTC( seurat_obj, reduction_base = NULL, clonecall = NULL, ..., extra_filter = NULL, run_id = NULL, new_rad_scale_factor = NULL, new_clone_scale_factor = NULL, repulse = FALSE, repulsion_threshold = 1, repulsion_strength = 1, max_repulsion_iter = 10L, relocate_cluster = NULL, relocation_coord = NULL, nudge_cluster = NULL, nudge_vector = NULL, recolor_cluster = NULL, new_color = NULL, rename_label = NULL, new_label = NULL, relocate_label = NULL, label_relocation_coord = NULL, nudge_label = NULL, label_nudge_vector = NULL, verbose = TRUE )
AdjustAPOTC( seurat_obj, reduction_base = NULL, clonecall = NULL, ..., extra_filter = NULL, run_id = NULL, new_rad_scale_factor = NULL, new_clone_scale_factor = NULL, repulse = FALSE, repulsion_threshold = 1, repulsion_strength = 1, max_repulsion_iter = 10L, relocate_cluster = NULL, relocation_coord = NULL, nudge_cluster = NULL, nudge_vector = NULL, recolor_cluster = NULL, new_color = NULL, rename_label = NULL, new_label = NULL, relocate_label = NULL, label_relocation_coord = NULL, nudge_label = NULL, label_nudge_vector = NULL, verbose = TRUE )
seurat_obj |
The seurat object to be adjusted. |
reduction_base |
character. The seurat reduction to base the clonal
expansion plotting on. Defaults to |
clonecall |
character. The column name in the seurat object metadata to
use. See |
... |
additional "subsetting" keyword arguments indicating the rows
corresponding to elements in the seurat object metadata that should be
filtered by. E.g., |
extra_filter |
character. An additional string that should be formatted
exactly like a statement one would pass into dplyr::filter that does
additional filtering to cells in the seurat object - on top of the other
keyword arguments - based on the metadata. This means that it will be
logically AND'ed with any keyword argument filters. This is a more flexible
alternative / addition to the filtering keyword arguments. For example, if
one wanted to filter by the length of the amino acid sequence of TCRs, one
could pass in something like |
run_id |
character. This will be the ID associated with the data of a
run, and will be used by other important functions like
where if keyword arguments and extra_filter are underscore characters if
there was no input for the |
new_rad_scale_factor |
a single numeric in (0, 1]. changes the radius scaling factor of all circles. |
new_clone_scale_factor |
a single numeric in (0, 1]. changes the clone_scale_factor |
repulse |
If |
repulsion_threshold |
numeric. The radius that clonal circle clusters overlap is acceptable when repulsing. |
repulsion_strength |
numeric. The smaller the value the less the clusters repulse each other per iteration, and vice versa. |
max_repulsion_iter |
integer. The number of repulsion iterations. |
relocate_cluster |
Numeric or Character. Indicates which cluster(s) based on the index or label to relocate to new coordinates. |
relocation_coord |
numeric of length two or a list of numerics of length
two of length of |
nudge_cluster |
Numeric or Character. Indicates which cluster(s) based on the index or label to "nudge"/translate their coordinate(s) by. |
nudge_vector |
numeric of length two or a list of numerics of length
two of length of |
recolor_cluster |
Numeric or Character. Indicates which cluster(s) based on the index or label to change their color by. |
new_color |
character of arbitrary length. Indicates the corresponding
new colors that selected clusters in |
rename_label |
Numeric or character. Indicates the index or name of label(s) to be renamed. |
new_label |
Character. Indicates the corresponding new label(s) that
selected label(s) in |
relocate_label |
Numeric or character. Indicates the index or name of label(s) to be relocated. |
label_relocation_coord |
Numeric of length two or a list of numerics of
length two of length of |
nudge_label |
Numeric or character. Indicates the index or name of label(s) to be "nudged"/translated. |
label_nudge_vector |
Numeric of length two or a list of numerics of
length two of length of |
verbose |
logical. Decides if visual cues are displayed to the R console of the progress. |
The adjusted seurat_obj
# do an APackOfTheClones run pbmc <- RunAPOTC(get(data("combined_pbmc")), verbose = FALSE) # adjust the rad_scale_factor, and nudge cluster 1 by x = 1, y = 1 pbmc <- AdjustAPOTC( pbmc, new_rad_scale_factor = 0.9, nudge_cluster = 1, nudge_vector = c(1, 1), verbose = FALSE ) # plot the result APOTCPlot(pbmc) # rename some labels pbmc <- AdjustAPOTC( pbmc, rename_label = c(2, 5), new_label = c("Foo", "Bar") ) # perhaps multiple clusters need to be relocated and repulsed pbmc <- AdjustAPOTC( pbmc, relocate_cluster = c("Foo", "C10"), # using labels works too relocation_coord = list(c(2, 3.5), c(0, 5)), repulse = TRUE, verbose = FALSE ) # plot again to check results APOTCPlot(pbmc, show_labels = TRUE, verbose = FALSE)
# do an APackOfTheClones run pbmc <- RunAPOTC(get(data("combined_pbmc")), verbose = FALSE) # adjust the rad_scale_factor, and nudge cluster 1 by x = 1, y = 1 pbmc <- AdjustAPOTC( pbmc, new_rad_scale_factor = 0.9, nudge_cluster = 1, nudge_vector = c(1, 1), verbose = FALSE ) # plot the result APOTCPlot(pbmc) # rename some labels pbmc <- AdjustAPOTC( pbmc, rename_label = c(2, 5), new_label = c("Foo", "Bar") ) # perhaps multiple clusters need to be relocated and repulsed pbmc <- AdjustAPOTC( pbmc, relocate_cluster = c("Foo", "C10"), # using labels works too relocation_coord = list(c(2, 3.5), c(0, 5)), repulse = TRUE, verbose = FALSE ) # plot again to check results APOTCPlot(pbmc, show_labels = TRUE, verbose = FALSE)
Given a seurat object with an 'apotc' (APackOfTheClones) object
from running RunAPOTC()
, this function will read the information and return
a customizable ggplot2 object of the clonal expansion with a circle size
legend. If the user is unhappy about certain aspects of the plot, many
parameters can be adjusted with the AdjustAPOTC function.
The specific APackOfTheClones run to be plotted can be identified in two
ways: either by inputting the run_id
associated with the run that was
either defined / auto-generated during RunAPOTC()
, or by inputting the
reduction_base
, clonecall
, extra_filter
and any other keyword arguments
that corresponded to the run. Its heavily recommended to use the run_id
.
If none of these parameters are inputted, the function defaults to returning
the plot of the latest run.
APOTCPlot( seurat_obj, reduction_base = NULL, clonecall = NULL, ..., extra_filter = NULL, run_id = NULL, show_shared = NULL, only_link = NULL, clone_link_width = "auto", clone_link_color = "black", clone_link_alpha = 0.5, res = 360L, linetype = "blank", use_default_theme = TRUE, retain_axis_scales = FALSE, alpha = 1, show_labels = FALSE, label_size = 5, add_size_legend = TRUE, legend_sizes = "auto", legend_position = "auto", legend_buffer = 0.2, legend_color = "#808080", legend_spacing = "auto", legend_label = "Clone sizes", legend_text_size = 5, add_legend_background = TRUE, add_legend_centerspace = 0, detail = TRUE, verbose = TRUE )
APOTCPlot( seurat_obj, reduction_base = NULL, clonecall = NULL, ..., extra_filter = NULL, run_id = NULL, show_shared = NULL, only_link = NULL, clone_link_width = "auto", clone_link_color = "black", clone_link_alpha = 0.5, res = 360L, linetype = "blank", use_default_theme = TRUE, retain_axis_scales = FALSE, alpha = 1, show_labels = FALSE, label_size = 5, add_size_legend = TRUE, legend_sizes = "auto", legend_position = "auto", legend_buffer = 0.2, legend_color = "#808080", legend_spacing = "auto", legend_label = "Clone sizes", legend_text_size = 5, add_legend_background = TRUE, add_legend_centerspace = 0, detail = TRUE, verbose = TRUE )
seurat_obj |
A seurat object that has been integrated with clonotype
data and has had a valid run of |
reduction_base |
character. The seurat reduction to base the clonal
expansion plotting on. Defaults to |
clonecall |
character. The column name in the seurat object metadata to
use. See |
... |
additional "subsetting" keyword arguments indicating the rows
corresponding to elements in the seurat object metadata that should be
filtered by. E.g., |
extra_filter |
character. An additional string that should be formatted
exactly like a statement one would pass into dplyr::filter that does
additional filtering to cells in the seurat object - on top of the other
keyword arguments - based on the metadata. This means that it will be
logically AND'ed with any keyword argument filters. This is a more flexible
alternative / addition to the filtering keyword arguments. For example, if
one wanted to filter by the length of the amino acid sequence of TCRs, one
could pass in something like |
run_id |
character. This will be the ID associated with the data of a
run, and will be used by other important functions like
where if keyword arguments and extra_filter are underscore characters if
there was no input for the |
show_shared |
The output of getSharedClones can be inputted here,
and the resulting plot will overlay lines between clone circles if that
clonotype is common between clusters. Note that the input must be
generated from data in the correct |
only_link |
Optional integer indicating to only display clone links originating from this cluster if showing shared clones. |
clone_link_width |
numeric. The width of the lines that connect shared
clones. Defaults to |
clone_link_color |
character. The color of the lines that connect shared
clones. Defaults to |
clone_link_alpha |
numeric. The alpha of the lines that connect shared clones. |
res |
The number of points on the generated path per full circle. From
plot viewers, if circles seem slightly too pixelated, it is recommended to
first try to export the plot as an |
linetype |
The type of outline each circle should have. defaults to
|
use_default_theme |
logical that defaults to |
retain_axis_scales |
If |
alpha |
numeric. The alpha of the circles in (0, 1]. Defaults to 1. |
show_labels |
If |
label_size |
The text size of labels if shown. Defaults to 5. |
add_size_legend |
If |
legend_sizes |
numeric vector. Indicates the circle sizes to be
displayed on the legend, and will always be sorted from smallest to greatest.
Defaults to |
legend_position |
character or numeric. Can be set to either
|
legend_buffer |
numeric. Indicates how much to "push" the legend towards the center of the plot from the selected corner. If negative, will push away |
legend_color |
character. Indicates the hex color of the circles displayed on the legend. Defaults to the hex code for a gray tone |
legend_spacing |
numeric. Indicates the horizontal distance between each
stacked circle on the size legend. Defaults to |
legend_label |
character. The title of the legend, which defaults to
|
legend_text_size |
numeric. The text size of the letters and numbers on the legend |
add_legend_background |
logical. If |
add_legend_centerspace |
numeric. An additional amount of distance changed between the circle sizes on the left side of the legend and the numbers on the right. Useful to set to around 0.5 (or more / less) when there are particularly large clone sizes that may cover the numbers. |
detail |
logical. If |
verbose |
logical. Decides if visual cues are displayed to the R console of the progress. |
A ggplot object of the APackOfTheClones clonal expansion plot of the
seurat object. There is an additional 10th element in the object named
"APackOfTheClones"
used by other functions in this package and shouldn't
interfere with any other ggplot functionality. (As far as currently known)
data("combined_pbmc") combined_pbmc <- RunAPOTC( combined_pbmc, run_id = "run1", verbose = FALSE ) # plotting with default arguments will plot the latest "run1" clonal_packing_plot <- APOTCPlot(combined_pbmc)
data("combined_pbmc") combined_pbmc <- RunAPOTC( combined_pbmc, run_id = "run1", verbose = FALSE ) # plotting with default arguments will plot the latest "run1" clonal_packing_plot <- APOTCPlot(combined_pbmc)
Generated with scRepertoire::combineExpression
. To construct this object
from scratch, try:
scRepertoire::combineExpression(scRepertoire::combineTCR(get(data("contig_list", , package = "scRepertoire")), samples = c("P17B", "P17L", "P18B", , "P18L", "P19B", "P19L", "P20B", "P20L"), removeNA = FALSE, , removeMulti = FALSE, filterMulti = FALSE), get(data("scRep_example", , package = "scRepertoire")), cloneCall = "gene", proportion = TRUE)
data("combined_pbmc")
data("combined_pbmc")
A Seurat object with the following slots filled
Currently only contains one assay ("RNA" - scRNA-seq expression data)
counts - Raw expression data
data - Normalized expression data
scale.data - Scaled expression data
var.features - names of the current features selected as variable
meta.features - Assay level metadata such as mean and variance
Cell level metadata with a combined TCR contig list from scRepertoire
Current default assay
Current default idents
Neighbor graphs computed, currently stores the SNN
Dimensional reductions: UMAP
Seurat version used to create the object
Command history, including the one used to create this object "combineExpression"
A convenience function to check for the existence of an APackOfTheClones run with its run id, regardless of if any run has been made
containsApotcRun(seurat_obj, run_id)
containsApotcRun(seurat_obj, run_id)
seurat_obj |
a seurat object |
run_id |
character. The id of the associated ApotcRun. |
A logical indicating whether the run exists.
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", run_id = "run1", verbose = FALSE ) containsApotcRun(pbmc, "run1") #> [1] TRUE containsApotcRun(pbmc, "run2") #> [1] FALSE
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", run_id = "run1", verbose = FALSE ) containsApotcRun(pbmc, "run1") #> [1] TRUE containsApotcRun(pbmc, "run2") #> [1] FALSE
Get clonotype frequencies from a seurat object's metadata, either as one
whole table, or in a list of tables, based on the current / some custom
ident of each cell. Note that depending on the ident (indicated by the
by_cluster
argument) there may be more or less clonotypes counted based
on the number of rows containing NA for that column of that ident if it
isn't the active ident.
countCloneSizes( seurat_obj, clonecall = "strict", extra_filter = NULL, ..., by_cluster = TRUE, sort_decreasing = NULL )
countCloneSizes( seurat_obj, clonecall = "strict", extra_filter = NULL, ..., by_cluster = TRUE, sort_decreasing = NULL )
seurat_obj |
a seurat object combined with a VDJ library with
|
clonecall |
character. The column name in the seurat object metadata to
use. See |
extra_filter |
character. An additional string that should be formatted
exactly like a statement one would pass into dplyr::filter that does
additional filtering to cells in the seurat object - on top of the other
keyword arguments - based on the metadata. This means that it will be
logically AND'ed with any keyword argument filters. This is a more flexible
alternative / addition to the filtering keyword arguments. For example, if
one wanted to filter by the length of the amino acid sequence of TCRs, one
could pass in something like |
... |
additional "subsetting" keyword arguments indicating the rows
corresponding to elements in the seurat object metadata that should be
filtered by. E.g., |
by_cluster |
Logical or Character. If |
sort_decreasing |
a logical or NULL. If |
A list of tables or a single table depending on by_cluster
data("combined_pbmc") countCloneSizes(combined_pbmc) countCloneSizes(combined_pbmc, "aa") countCloneSizes(combined_pbmc, "nt", orig.ident = c("P17B", "P17L"))
data("combined_pbmc") countCloneSizes(combined_pbmc) countCloneSizes(combined_pbmc, "aa") countCloneSizes(combined_pbmc, "nt", orig.ident = c("P17B", "P17L"))
A convenience function to erase all data associated with a particular run, including the ApotcData and command in seurat_obj@command. The run_id would be no longer accessible afterwards.
deleteApotcData(seurat_obj, run_id)
deleteApotcData(seurat_obj, run_id)
seurat_obj |
a seurat object that has had RunAPOTC ran on it before in order of the functions being called. |
run_id |
character. The id of the associated ApotcRun. |
The modified input seurat object
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", run_id = "run1", verbose = FALSE ) getApotcDataIds(pbmc) #> [1] "run1" # delete the data pbmc <- deleteApotcData(pbmc, "run1") getApotcDataIds(pbmc) #> NULL
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", run_id = "run1", verbose = FALSE ) getApotcDataIds(pbmc) #> [1] "run1" # delete the data pbmc <- deleteApotcData(pbmc, "run1") getApotcDataIds(pbmc) #> NULL
A convenience function to get all run ids of previous RunAPOTC run IDs
getApotcDataIds(seurat_obj)
getApotcDataIds(seurat_obj)
seurat_obj |
a seurat object that has had RunAPOTC ran on it before in order of the functions being called. |
a character vector of all run ids of previous RunAPOTC runs, in
the order they were ran in. If there are no runs on the object, it returns
NULL
.
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", verbose = FALSE ) getApotcDataIds(pbmc) #> [1] "umap;CTstrict;_;_" pbmc <- RunAPOTC( seurat_obj = pbmc, reduction_base = "umap", clonecall = "gene", verbose = FALSE ) getApotcDataIds(pbmc) #> [1] "umap;CTstrict;_;_" "umap;CTgene;_;_"
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", verbose = FALSE ) getApotcDataIds(pbmc) #> [1] "umap;CTstrict;_;_" pbmc <- RunAPOTC( seurat_obj = pbmc, reduction_base = "umap", clonecall = "gene", verbose = FALSE ) getApotcDataIds(pbmc) #> [1] "umap;CTstrict;_;_" "umap;CTgene;_;_"
A convenience function to get the object id of the most recent valid
RunAPOTC()
run, to be used by APOTCPlot()
and AdjustAPOTC
getLastApotcDataId(seurat_obj)
getLastApotcDataId(seurat_obj)
seurat_obj |
a seurat object that has had RunAPOTC ran on it before in order of the functions being called. |
a character of the object id of the last RunAPOTC()
call
# first run pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", verbose = FALSE ) getLastApotcDataId(pbmc) #> [1] "umap;CTstrict;_;_" # second run with a different clonecall pbmc <- RunAPOTC( seurat_obj = pbmc, reduction_base = "umap", clonecall = "gene", verbose = FALSE ) getLastApotcDataId(pbmc) #> [1] "umap;CTgene;_;_"
# first run pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", verbose = FALSE ) getLastApotcDataId(pbmc) #> [1] "umap;CTstrict;_;_" # second run with a different clonecall pbmc <- RunAPOTC( seurat_obj = pbmc, reduction_base = "umap", clonecall = "gene", verbose = FALSE ) getLastApotcDataId(pbmc) #> [1] "umap;CTgene;_;_"
Utility function to calculate the physical xy coordinates of each seurat
cluster based on a dimensional reduction already present in the object.
The results are returned in a list with the length of the number of distinct
seurat clusters based on the seurat_obj meta.data
.
getReductionCentroids(seurat_obj, reduction)
getReductionCentroids(seurat_obj, reduction)
seurat_obj |
input seurat object with the dimensional reduction of choice already present, and seurat clusters computed. |
reduction |
character. The reduction that the centroid calculation should be based on. |
A list of the length of the number of distinct clusters in the seurat object metadata, where each element of the list is a numeric vector of length 2, with the numbers corresponding to the x and y coordinate respectively of the seurat cluster with the corresponding index.
data("combined_pbmc") getReductionCentroids(combined_pbmc, reduction = "umap")
data("combined_pbmc") getReductionCentroids(combined_pbmc, reduction = "umap")
The list of clustered clonotype frequencies from countCloneSizes can be merged by this function to a frequency table of all clonotypes similar to the data that can be seen in the seurat object metadata. By default, this function sorts the table with largest clonotypes first, and this may be useful for quickly gauging which clonotypes are the most expanded overall.
mergeCloneSizes(clustered_clone_sizes, sort_decreasing = TRUE)
mergeCloneSizes(clustered_clone_sizes, sort_decreasing = TRUE)
clustered_clone_sizes |
the output of countCloneSizes. |
sort_decreasing |
a logical or NULL. If |
a table object
clustered_clone_sizes <- countCloneSizes(get(data("combined_pbmc"))) mergeCloneSizes(clustered_clone_sizes)
clustered_clone_sizes <- countCloneSizes(get(data("combined_pbmc"))) mergeCloneSizes(clustered_clone_sizes)
This function has most of the parameters related to legend in
APOTCPlot()
, and can plot a new / override the current legend.
However, it is very important that the input plot to the function
is a plot generated solely by APOTCPlot()
or vizAPOTC()
due to it
being a custom ggplot object. It will not override or erase any
additional layers that the user/other functions have added. To just
remove the legend, see removeLegend.
overlayLegend( apotc_ggplot, legend_sizes = "auto", legend_position = "auto", legend_buffer = 0.2, legend_color = "#808080", legend_spacing = "auto", legend_label = "Clone sizes", legend_text_size = 5, add_legend_background = TRUE, add_legend_centerspace = 0, linetype = "blank", res = 360L )
overlayLegend( apotc_ggplot, legend_sizes = "auto", legend_position = "auto", legend_buffer = 0.2, legend_color = "#808080", legend_spacing = "auto", legend_label = "Clone sizes", legend_text_size = 5, add_legend_background = TRUE, add_legend_centerspace = 0, linetype = "blank", res = 360L )
apotc_ggplot |
a ggplot object that is the output of |
legend_sizes |
numeric vector. Indicates the circle sizes to be
displayed on the legend, and will always be sorted from smallest to greatest.
Defaults to |
legend_position |
character or numeric. Can be set to either
|
legend_buffer |
numeric. Indicates how much to "push" the legend towards the center of the plot from the selected corner. If negative, will push away |
legend_color |
character. Indicates the hex color of the circles displayed on the legend. Defaults to the hex code for a gray tone |
legend_spacing |
numeric. Indicates the horizontal distance between each
stacked circle on the size legend. Defaults to |
legend_label |
character. The title of the legend, which defaults to
|
legend_text_size |
numeric. The text size of the letters and numbers on the legend |
add_legend_background |
logical. If |
add_legend_centerspace |
numeric. An additional amount of distance changed between the circle sizes on the left side of the legend and the numbers on the right. Useful to set to around 0.5 (or more / less) when there are particularly large clone sizes that may cover the numbers. |
linetype |
The type of outline each circle should have. defaults to
|
res |
The number of points on the generated path per full circle. From
plot viewers, if circles seem slightly too pixelated, it is recommended to
first try to export the plot as an |
The size legend on APackOfTheClones plots are simply a collection of
annotation layers of the rect
, text
, circle
geoms. Therefore it
isn't quite a ggplot legend. In the actual ggplot object, the $layers
element should all be named with an empty character ""
, and those
that comprise the annotation layers of the legend should be named whatever
APackOfTheClones:::.ApotcLegendLayerName
is. Note that this is simply
an implementation detail that the user should not interfere with.
A ggplot object of the APackOfTheClones clonal expansion plot of the
seurat object. There is an additional 10th element in the object named
"APackOfTheClones"
used by other functions in this package and shouldn't
interfere with any other ggplot functionality. (As far as currently known)
library(dplyr) # create a plot with a legend apotc_plot <- vizAPOTC(get(data("combined_pbmc")), verbose = FALSE) # reposition the legend to top right overlayLegend(apotc_plot, legend_position = "top right") # use different sizes and label - may be nice to use the pipe apotc_plot %>% overlayLegend( legend_sizes = c(1, 3, 7, 9), legend_label = "odd sizes" )
library(dplyr) # create a plot with a legend apotc_plot <- vizAPOTC(get(data("combined_pbmc")), verbose = FALSE) # reposition the legend to top right overlayLegend(apotc_plot, legend_position = "top right") # use different sizes and label - may be nice to use the pipe apotc_plot %>% overlayLegend( legend_sizes = c(1, 3, 7, 9), legend_label = "odd sizes" )
Removes the clone size legend on an APackOfTheClones plot, if one is present. Will preserve any additional ggplot layers.
removeLegend(apotc_ggplot)
removeLegend(apotc_ggplot)
apotc_ggplot |
a ggplot object that is the output of |
A ggplot object of the APackOfTheClones clonal expansion plot of the
seurat object. There is an additional 10th element in the object named
"APackOfTheClones"
used by other functions in this package and shouldn't
interfere with any other ggplot functionality. (As far as currently known)
# create an APackOfTheClones plot with a legend apotc_plot <- vizAPOTC( get(data("combined_pbmc")), add_size_legend = TRUE, verbose = FALSE ) # remove the legend apotc_plot <- removeLegend(apotc_plot) apotc_plot
# create an APackOfTheClones plot with a legend apotc_plot <- vizAPOTC( get(data("combined_pbmc")), add_size_legend = TRUE, verbose = FALSE ) # remove the legend apotc_plot <- removeLegend(apotc_plot) apotc_plot
A function to rename an APackOfTheClones run identified by its run id in a Seurat object.
renameApotcRun(seurat_obj, old_run_id, new_run_id)
renameApotcRun(seurat_obj, old_run_id, new_run_id)
seurat_obj |
A Seurat object containing APackOfTheClones data - the
output of |
old_run_id |
Character. The current id of the APackOfTheClones run to be renamed. |
new_run_id |
Character. The new id to assign to the APackOfTheClones run. |
A Seurat object with the APackOfTheClones run renamed.
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", run_id = "run1", verbose = FALSE ) pbmc <- renameApotcRun(pbmc, "run1", "new_run") # Now "run1" has been renamed to "new_run"
pbmc <- RunAPOTC( seurat_obj = get(data("combined_pbmc")), reduction_base = "umap", clonecall = "strict", run_id = "run1", verbose = FALSE ) pbmc <- renameApotcRun(pbmc, "run1", "new_run") # Now "run1" has been renamed to "new_run"
Computes necessary information for an APackOfTheClones
clonal expansion plot (APOTCPlot()
) and stores it in the seurat object.
Gets sizes of unique clones and utilizes a circle-packing algorithm to
pack circles representing individual clones in approximately the same
dimensional reduction (reduction_base
) coordinates based on some cell
ident (defaults to the active ident).
The parameter extra_filter
along with an unlimited number of additional
keyword arguments can be used to filter the cells by certain conditions
in the metadata, and new results will be stored in addition to other runs
the users may have done.
Each APackOfTheClones run is uniquely identified by the parameters
reduction_base
, clonecall
, extra_filter
, and any additional keywords
passed to filter the metadata. Each distinct run result is stored in the
seurat object and has an associated Id generated from the aforementioned
parameters. To view the id of the latest run, call getLastApotcDataId.
To view all the ids of previous runs, call getApotcDataIds. To work further
with a specific run (most importantly, plotting), the user can use this id
in the arguments with is slightly more convenient than passing in the
original RunAPOTC parameters again but both ways work.
If the user wishes to manually customize/fix the expansion plot generated, the circular packing information can be modified with the AdjustAPOTC function.
RunAPOTC( seurat_obj, reduction_base = "umap", clonecall = "strict", ..., extra_filter = NULL, alt_ident = NULL, run_id = NULL, clone_scale_factor = "auto", rad_scale_factor = 0.95, order_clones = TRUE, try_place = FALSE, repulse = TRUE, repulsion_threshold = 1, repulsion_strength = 1, max_repulsion_iter = 20L, override = FALSE, verbose = TRUE )
RunAPOTC( seurat_obj, reduction_base = "umap", clonecall = "strict", ..., extra_filter = NULL, alt_ident = NULL, run_id = NULL, clone_scale_factor = "auto", rad_scale_factor = 0.95, order_clones = TRUE, try_place = FALSE, repulse = TRUE, repulsion_threshold = 1, repulsion_strength = 1, max_repulsion_iter = 20L, override = FALSE, verbose = TRUE )
seurat_obj |
Seurat object with one or more dimension reductions and
already have been integrated with a TCR/BCR library with
|
reduction_base |
character. The seurat reduction to base the clonal
expansion plotting on. Defaults to |
clonecall |
character. The column name in the seurat object metadata to
use. See |
... |
additional "subsetting" keyword arguments indicating the rows
corresponding to elements in the seurat object metadata that should be
filtered by. E.g., |
extra_filter |
character. An additional string that should be formatted
exactly like a statement one would pass into dplyr::filter that does
additional filtering to cells in the seurat object - on top of the other
keyword arguments - based on the metadata. This means that it will be
logically AND'ed with any keyword argument filters. This is a more flexible
alternative / addition to the filtering keyword arguments. For example, if
one wanted to filter by the length of the amino acid sequence of TCRs, one
could pass in something like |
alt_ident |
character. By default, cluster identity is assumed to be
whatever is in |
run_id |
character. This will be the ID associated with the data of a
run, and will be used by other important functions like
where if keyword arguments and extra_filter are underscore characters if
there was no input for the |
clone_scale_factor |
Dictates how much to scale each circle(between 0,1)
radius when converting from clonotype counts into circles that represent
individual clonotypes. The argument defaults to the character |
rad_scale_factor |
numeric between 0 and 1. This value decreases the radius of the smallest clones by this scale factor. And the absolute value of this decrease will be applied to all packed circles, effectively shrinking all circles on the spot, and introduce more constant spacing in between. |
order_clones |
logical. Decides if the largest clone circles should be
near cluster centroids. This is highly recommended to be set to TRUE for
increased intuitiveness of the visualization, as resulting plots tend to
give an improved impression of the proportion of expanded clones. If
|
try_place |
If |
repulse |
If |
repulsion_threshold |
numeric. The radius that clonal circle clusters overlap is acceptable when repulsing. |
repulsion_strength |
numeric. The smaller the value the less the clusters repulse each other per iteration, and vice versa. |
max_repulsion_iter |
integer. The number of repulsion iterations. |
override |
logical. If |
verbose |
logical. Decides if visual cues are displayed to the R console of the progress. |
Note that the subsetting arguments ...
and extra_filter
are only a
quick convenience to subset based on metadata, and the subset
S3 method
defined in Seurat
is much more mature are has more features. Additionally,
users need to work with data subsets are recommended to and likely already
are working with seurat objects subsetted/split with Seurat::SplitObject
.
All APackOfTheClones run data is stored in the Seurat object under
seurat_object@misc$APackOfTheClones
, which is a list of S4 objects of the
type "ApotcData", with each element corresponding to a unique run. The id of
each run is the name of each element in the list. The user
really shouldn't manually modify anything in the list as it may cause
unexpected behavior with many other functions.
Additionally, it logs a seurat command associated with the run in the
seurat_object@commands
slot as a "SeuratCommand" object (from Seurat),
where the name of the object in the list is formatted as RunAPOTC.run_id
.
A modified version of the input seurat object, which harbors data
necessary for visualizing the clonal expansion of the cells with
APOTCPlot()
and has a friendly user interface to modify certain attributes with
AdjustAPOTC.
For the ident that was used to cluster the clones, labels for each cluster
are inferred and stored in the run so that they can be used by other
functions and optionally overlaid on the plot over clusters. If the levels
of the ident used is a naturally ordered integer sequence, then the labels
generated would be "C1", "C2", "C3" ...
, else they would be the actual
ident levels themselves.
APOTCPlot()
, AdjustAPOTC, getApotcDataIds
data("combined_pbmc") # this is the recommended approach to use a custom run_id with default params combined_pbmc <- RunAPOTC(combined_pbmc, run_id = "default", verbose = FALSE) # here's a seperate run with some filters to the meta data, where # `orig.ident` is a custom column in the example data. Notice that it is not # a `RunAPOTC()` parameter but a user keyword argument combined_pbmc <- RunAPOTC( combined_pbmc, run_id = "sample17", orig.ident = c("P17B", "P17L"), verbose = FALSE ) # the exact same thing can be achieved with the `extra_filter` parameter combined_pbmc <- RunAPOTC( combined_pbmc, run_id = "sample17", extra_filter = "substr(orig.ident, 2, 3) == '17'", override = TRUE, verbose = FALSE )
data("combined_pbmc") # this is the recommended approach to use a custom run_id with default params combined_pbmc <- RunAPOTC(combined_pbmc, run_id = "default", verbose = FALSE) # here's a seperate run with some filters to the meta data, where # `orig.ident` is a custom column in the example data. Notice that it is not # a `RunAPOTC()` parameter but a user keyword argument combined_pbmc <- RunAPOTC( combined_pbmc, run_id = "sample17", orig.ident = c("P17B", "P17L"), verbose = FALSE ) # the exact same thing can be achieved with the `extra_filter` parameter combined_pbmc <- RunAPOTC( combined_pbmc, run_id = "sample17", extra_filter = "substr(orig.ident, 2, 3) == '17'", override = TRUE, verbose = FALSE )
This is an analogue for scRepertoire::highlightClones
that can highlight
certain clonotypes on an APackOfTheClones clonal expansion plot. For most
combinations of the arguments, there will be a ggplot fill legend on the
right side that correspond to each (existing) clonotype.
showCloneHighlight( apotc_ggplot, clonotype, color_each = TRUE, default_color = "#808080", scale_bg = 1, fill_legend = TRUE )
showCloneHighlight( apotc_ggplot, clonotype, color_each = TRUE, default_color = "#808080", scale_bg = 1, fill_legend = TRUE )
apotc_ggplot |
A ggplot object that is the output of |
clonotype |
character vector of the sequence(s) to highlight. Note that it must be of the clonecall of the code that created the plot. A warning will be shown if any of the sequences are not present. |
color_each |
Either a logical of length 1, or a character(s). It is
|
default_color |
A character of length 1 or |
scale_bg |
A positive numeric. Scales the brightness value of each color
of the non-highlighted clones by itself as a scaling factor. Defaults to 1
which will not alter the current brightness. Note that if
|
fill_legend |
logical indicating whether a ggplot legend of the "fill" of each clonotype should be displayed. |
Under the hood, this function simply mutates the plotting data.frame under
$data
in the ggplot object, and operates on a column named color
.
Note that if color_each = FALSE
and default_color = NULL
, this is
equivalent to simply not highlighting anything and a warning will be shown.
A ggplot object with the data modified to the highlighted colors
library(dplyr) data("combined_pbmc") # piping the plot can be nice to read syntactically - # By default, assigns unique colors to highlights and everything else is gray vizAPOTC(combined_pbmc, clonecall = "aa", verbose = FALSE) %>% showCloneHighlight("CASLSGSARQLTF_CASSSTVAGEQYF") # one useful application is to highlight shared clones - beware that the # clonotype sequences may get extremely long in the legend shared_aa_clones <- names(getSharedClones(combined_pbmc, clonecall = "aa")) vizAPOTC(combined_pbmc, clonecall = "aa", verbose = FALSE) %>% showCloneHighlight(shared_aa_clones)
library(dplyr) data("combined_pbmc") # piping the plot can be nice to read syntactically - # By default, assigns unique colors to highlights and everything else is gray vizAPOTC(combined_pbmc, clonecall = "aa", verbose = FALSE) %>% showCloneHighlight("CASLSGSARQLTF_CASSSTVAGEQYF") # one useful application is to highlight shared clones - beware that the # clonotype sequences may get extremely long in the legend shared_aa_clones <- names(getSharedClones(combined_pbmc, clonecall = "aa")) vizAPOTC(combined_pbmc, clonecall = "aa", verbose = FALSE) %>% showCloneHighlight(shared_aa_clones)
This function combines the functionality of both RunAPOTC()
and
APOTCPlot()
. Given a Seurat object, it first runs the APackOfTheClones
method (RunAPOTC()
) to compute clonal expansion information, and then
generates a ggplot2 object of the clonal expansion plot with
a circle size legend. (APOTCPlot()
)
vizAPOTC( seurat_obj, reduction_base = "umap", clonecall = "strict", ..., extra_filter = NULL, alt_ident = NULL, clone_scale_factor = "auto", rad_scale_factor = 0.95, order_clones = TRUE, try_place = FALSE, repulse = TRUE, repulsion_threshold = 1, repulsion_strength = 1, max_repulsion_iter = 20L, show_shared = NULL, only_link = NULL, clone_link_width = "auto", clone_link_color = "black", clone_link_alpha = 0.5, res = 360L, linetype = "blank", use_default_theme = TRUE, retain_axis_scales = FALSE, alpha = 1, show_labels = FALSE, label_size = 5, add_size_legend = TRUE, legend_sizes = "auto", legend_position = "auto", legend_buffer = 0.2, legend_color = "#808080", legend_spacing = "auto", legend_label = "Clone sizes", legend_text_size = 5, add_legend_background = TRUE, add_legend_centerspace = 0, detail = TRUE, verbose = TRUE )
vizAPOTC( seurat_obj, reduction_base = "umap", clonecall = "strict", ..., extra_filter = NULL, alt_ident = NULL, clone_scale_factor = "auto", rad_scale_factor = 0.95, order_clones = TRUE, try_place = FALSE, repulse = TRUE, repulsion_threshold = 1, repulsion_strength = 1, max_repulsion_iter = 20L, show_shared = NULL, only_link = NULL, clone_link_width = "auto", clone_link_color = "black", clone_link_alpha = 0.5, res = 360L, linetype = "blank", use_default_theme = TRUE, retain_axis_scales = FALSE, alpha = 1, show_labels = FALSE, label_size = 5, add_size_legend = TRUE, legend_sizes = "auto", legend_position = "auto", legend_buffer = 0.2, legend_color = "#808080", legend_spacing = "auto", legend_label = "Clone sizes", legend_text_size = 5, add_legend_background = TRUE, add_legend_centerspace = 0, detail = TRUE, verbose = TRUE )
seurat_obj |
A seurat object that has been integrated with clonotype
data with |
reduction_base |
character. The seurat reduction to base the clonal
expansion plotting on. Defaults to |
clonecall |
character. The column name in the seurat object metadata to
use. See |
... |
additional "subsetting" keyword arguments indicating the rows
corresponding to elements in the seurat object metadata that should be
filtered by. E.g., |
extra_filter |
character. An additional string that should be formatted
exactly like a statement one would pass into dplyr::filter that does
additional filtering to cells in the seurat object - on top of the other
keyword arguments - based on the metadata. This means that it will be
logically AND'ed with any keyword argument filters. This is a more flexible
alternative / addition to the filtering keyword arguments. For example, if
one wanted to filter by the length of the amino acid sequence of TCRs, one
could pass in something like |
alt_ident |
character. By default, cluster identity is assumed to be
whatever is in |
clone_scale_factor |
Dictates how much to scale each circle(between 0,1)
radius when converting from clonotype counts into circles that represent
individual clonotypes. The argument defaults to the character |
rad_scale_factor |
numeric between 0 and 1. This value decreases the radius of the smallest clones by this scale factor. And the absolute value of this decrease will be applied to all packed circles, effectively shrinking all circles on the spot, and introduce more constant spacing in between. |
order_clones |
logical. Decides if the largest clone circles should be
near cluster centroids. This is highly recommended to be set to TRUE for
increased intuitiveness of the visualization, as resulting plots tend to
give an improved impression of the proportion of expanded clones. If
|
try_place |
If |
repulse |
If |
repulsion_threshold |
numeric. The radius that clonal circle clusters overlap is acceptable when repulsing. |
repulsion_strength |
numeric. The smaller the value the less the clusters repulse each other per iteration, and vice versa. |
max_repulsion_iter |
integer. The number of repulsion iterations. |
show_shared |
The output of getSharedClones can be inputted here,
and the resulting plot will overlay lines between clone circles if that
clonotype is common between clusters. Note that the input must be
generated from data in the correct |
only_link |
Optional integer indicating to only display clone links originating from this cluster if showing shared clones. |
clone_link_width |
numeric. The width of the lines that connect shared
clones. Defaults to |
clone_link_color |
character. The color of the lines that connect shared
clones. Defaults to |
clone_link_alpha |
numeric. The alpha of the lines that connect shared clones. |
res |
The number of points on the generated path per full circle. From
plot viewers, if circles seem slightly too pixelated, it is recommended to
first try to export the plot as an |
linetype |
The type of outline each circle should have. defaults to
|
use_default_theme |
logical that defaults to |
retain_axis_scales |
If |
alpha |
numeric. The alpha of the circles in (0, 1]. Defaults to 1. |
show_labels |
If |
label_size |
The text size of labels if shown. Defaults to 5. |
add_size_legend |
If |
legend_sizes |
numeric vector. Indicates the circle sizes to be
displayed on the legend, and will always be sorted from smallest to greatest.
Defaults to |
legend_position |
character or numeric. Can be set to either
|
legend_buffer |
numeric. Indicates how much to "push" the legend towards the center of the plot from the selected corner. If negative, will push away |
legend_color |
character. Indicates the hex color of the circles displayed on the legend. Defaults to the hex code for a gray tone |
legend_spacing |
numeric. Indicates the horizontal distance between each
stacked circle on the size legend. Defaults to |
legend_label |
character. The title of the legend, which defaults to
|
legend_text_size |
numeric. The text size of the letters and numbers on the legend |
add_legend_background |
logical. If |
add_legend_centerspace |
numeric. An additional amount of distance changed between the circle sizes on the left side of the legend and the numbers on the right. Useful to set to around 0.5 (or more / less) when there are particularly large clone sizes that may cover the numbers. |
detail |
logical. If |
verbose |
logical. Decides if visual cues are displayed to the R console of the progress. |
Note that the subsetting arguments ...
and extra_filter
are only a
quick convenience to subset based on metadata, and the subset
S3 method
defined in Seurat
is much more mature are has more features. Additionally,
users need to work with data subsets are recommended to and likely already
are working with seurat objects subsetted/split with Seurat::SplitObject
.
A ggplot object of the APackOfTheClones clonal expansion plot of the
seurat object. There is an additional 10th element in the object named
"APackOfTheClones"
used by other functions in this package and shouldn't
interfere with any other ggplot functionality. (As far as currently known)
For the ident that was used to cluster the clones, labels for each cluster
are inferred and stored in the run so that they can be used by other
functions and optionally overlaid on the plot over clusters. If the levels
of the ident used is a naturally ordered integer sequence, then the labels
generated would be "C1", "C2", "C3" ...
, else they would be the actual
ident levels themselves.
data("combined_pbmc") # plot with default parameters vizAPOTC(combined_pbmc, verbose = FALSE) # use arguments from RunAPOTC and APOTCPlot vizAPOTC( combined_pbmc, try_place = TRUE, show_labels = TRUE, verbose = FALSE )
data("combined_pbmc") # plot with default parameters vizAPOTC(combined_pbmc, verbose = FALSE) # use arguments from RunAPOTC and APOTCPlot vizAPOTC( combined_pbmc, try_place = TRUE, show_labels = TRUE, verbose = FALSE )