find the more recently updated reactive array

Get latest updated reactive array

I have a notebook where I define a set of genes using two different methods: 1) clicking on a dendrogram to select a cluster of genes or 2) zooming in or out to select the list of currently visible genes. These two types of interactions with Clustergrammer-GL reactively define two sets of arrays: dendro_gene_list and viz_gene_list. However, I would like to have a new array (gene_list) that is defined by whichever list (dendro_gene_list or viz_gene_list) has been updated more recently.

This example seems to do something similar https://observablehq.com/@mbostock/linked-inputs but I’m unsure if this approach will work for my example.

For the time being, I might set up a selector for the user to choose between the two list types. This set of genes will be used for enrichment analysis via API calls.

Background on Purpose of Notebook

For reference, the purpose of the notebook is to help researchers understand the biological functions of differentially regulated genes after COVID-19 infection - e.g. we can use enrichment analysis to find pathways that are associated with a set of genes or transcription factors that might be responsible for up-regulation of these genes. We can for instance reorder genes based on their log2-fold-change and then zoom into the most up-regulated genes to selectively run enrichment analysis on these genes.

Hi @cornhundred,

For better or for worse, to me this feels like a natural application for a mutable (https://observablehq.com/@observablehq/introduction-to-mutable-state).

Initialize gene_list to null:

mutable gene_list = null

… and then, in each of your dendro_gene_list and viz_gene_list cells, when they have finished computation, and are about to return the final value, assign it to the mutable:

dendro_gene_list = {
  ... compute
  mutable gene_list = value;
  return value;
}

Now, any cell that uses gene_list will have a reactive reference to the more recently completed value from either dendro_gene_list or viz_gene_list. For example:

first_element_of_gene_list = gene_list[0]
1 Like

Thanks @jashkenas , I’ll give that a try and let you know how it goes.

I currently implemented the following: a user has to click on the heatmap in order to re-run enrichment analysis. Clicking in the heatmap area will set gene_list to viz_gene_list and clicking the row dendrogram will set gene_list to dendro_gene_list. However, it would be cool to have an auto-updating enrichment (although it might overtax the Enrichr API) and it looks like the mutable approach should work for that - I’ll update on this post.