vitessce / vitessceR

R API and htmlwidget for Vitessce
https://r-docs.vitessce.io
Other
38 stars 9 forks source link

Views that do not share a dataset are coordinated together by default #67

Closed lee-t closed 2 years ago

lee-t commented 2 years ago

Goal User asks if it is possible to see multiple PCA or UMAP scatterplots of the same data but with different cell set labels. So for a given dataset with 2 cell set metas, ie Tier 1, and Tier 2, create a Vitessce widget with a PCA plot Tier 1 colors and a separate PCA plot with tier 2 colors.

image

To Reproduce A toy example is provided as the original data is unpublished. The app downloads and processes the SingleCellExperiment and wraps it into 2 separate objects, each with different cell set labels. the layout has 2 scatterplots and 2 cell set components. The resulting vitessce widget shows only tier2 label coloring and cannot render tier1 coloring. In fact it looks like both PCA plots are controlled with the second CellSet component.

image

I've also tried another app with 2 completely different datasets, a seurat object and and spatialexperiment object. The resulting vitessce widget shows the same behavior with the 2nd cellset component controlling both PCA scatterplots.

Expected behavior One PCA plot controlled by one CellSet component, and a seperate PCA plot with a seperate CellSet component controlled independently in the same vitessce widget

keller-mark commented 2 years ago

Hi @lee-t, this is the expected behavior, since by default, everything that can be coordinated will be coordinated, with the following exceptions: https://github.com/vitessce/vitessce/blob/b28ae1abbc5e08d296f979f0f3606c6e75365e47/src/app/state/coordination.js#L60

However you raise a good point that this does not make much sense when the dataset IDs are different.

In the meantime, you should be able to achieve the desired behavior using vc$link_views, which will explicitly link the pairs (tier1, cell_sets1) and (tier2, cell_sets2) together, separately. In the c_values argument, we can supply the default values for those coordination types, where NA in R will become null in JS, causing the cellSetColor and cellSetSelection coordination values to be automatically initialized as expected.

vc <- VitessceConfig$new("My config")
dataset1 <- vc$add_dataset("level1")
dataset1 <- dataset1$add_object(w)
dataset2 <- vc$add_dataset("level2")
dataset2 <- dataset2$add_object(l)

tier1 <- vc$add_view(dataset1, Component$SCATTERPLOT, mapping = "PCA")
tier2 <- vc$add_view(dataset2, Component$SCATTERPLOT, mapping = "PCA")
cell_sets1 <- vc$add_view(dataset1, Component$CELL_SETS)   
cell_sets2 <- vc$add_view(dataset2, Component$CELL_SETS)

+vc$link_views(
+  c(tier1, cell_sets1),
+  c(CoordinationType$CELL_SET_SELECTION, CoordinationType$CELL_SET_COLOR, CoordinationType$CELL_COLOR_ENCODING),
+  c_values = c(NA, NA, "cellSetSelection")
+)
+vc$link_views(
+  c(tier2, cell_sets2),
+  c(CoordinationType$CELL_SET_SELECTION, CoordinationType$CELL_SET_COLOR, CoordinationType$CELL_COLOR_ENCODING),
+  c_values = c(NA, NA, "cellSetSelection")
+)

vc$layout(hconcat(vconcat(tier1, tier2), vconcat(cell_sets1, cell_sets2)))
vc$widget()
lee-t commented 2 years ago

Thanks for the explanation mark! I see other issues for more granular control over link_views and scatterplots, but for now this solution works for me.