-
Notifications
You must be signed in to change notification settings - Fork 27
Speed up dot plots: integrate preprocessed data on frontend (SCP-5992) #2324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4eca48b
Draft integration of precomputed dot plot data API and model
eweitz a9ea5fb
Improve dot plot patched size, color alignment with original
eweitz 15f0c7a
Use raw pre-computed expression values while showing scaled colors
eweitz 560c28a
Merge branch 'development' of github.com:broadinstitute/single_cell_p…
eweitz 5ca402d
Add dot_plot_preprocessing_frontend feature flag
eweitz 863754b
Robustify migrations
eweitz 060c438
Refine variable name, remove debug
eweitz 57c82c0
Add tests for preprocessed dot plots
eweitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /** | ||
| * Monkeypatch for Morpheus to accept pre-computed dot plot data | ||
| * Data format: [mean_expression, percent_expressing] | ||
| * Mean expression values are normalized per-gene (per-row) to 0-1 range for proper color scaling | ||
| */ | ||
|
|
||
| (function() { | ||
| 'use strict' | ||
|
|
||
| /** | ||
| * Apply the dot plot patch to Morpheus once it's loaded | ||
| * Waits for window.morpheus to be available before patching | ||
| */ | ||
| function applyDotPlotPatch() { | ||
| if (typeof window.morpheus === 'undefined') { | ||
| // Morpheus not loaded yet, wait a bit and try again | ||
| setTimeout(applyDotPlotPatch, 100) | ||
| return | ||
| } | ||
|
|
||
| /** | ||
| * Convert your dot plot JSON format to a Morpheus dataset | ||
| */ | ||
| window.morpheus.DotPlotConverter = { | ||
|
|
||
| createDataset(data) { | ||
| const cellTypes = data.values | ||
| const geneNames = Object.keys(data.genes) | ||
| const nRows = geneNames.length | ||
| const nCols = cellTypes.length | ||
|
|
||
| // Create dataset with Float32 data type | ||
| // The dataset name becomes the first series name by default | ||
| const dataset = new window.morpheus.Dataset({ | ||
| name: 'Mean Expression', | ||
| rows: nRows, | ||
| columns: nCols, | ||
| dataType: 'Float32' | ||
| }) | ||
|
|
||
| // Add second series for the size metric (percent expressing) | ||
| // Morpheus uses 'percent' for sizing in dot plots | ||
| dataset.addSeries({ | ||
| name: 'percent', | ||
| dataType: 'Float32' | ||
| }) | ||
|
|
||
| // Set up row metadata (genes) | ||
| const rowIds = dataset.getRowMetadata().add('id') | ||
| geneNames.forEach((gene, i) => { | ||
| rowIds.setValue(i, gene) | ||
| }) | ||
|
|
||
| // Set up column metadata (cell types) | ||
| const colIds = dataset.getColumnMetadata().add('id') | ||
| const cellTypeMetadata = dataset.getColumnMetadata().add(data.annotation_name || 'Cell Type') | ||
| cellTypes.forEach((cellType, j) => { | ||
| colIds.setValue(j, cellType) | ||
| cellTypeMetadata.setValue(j, cellType) | ||
| }) | ||
|
|
||
| // Fill in the data | ||
| // Series 0: mean expression (for color) - will be normalized per-gene (row) | ||
| // Series 1: percent expressing (for size) - will be scaled to 0-100 | ||
| // Data format: values[0] = mean_expression, values[1] = percent_expressing | ||
| geneNames.forEach((gene, i) => { | ||
| const geneData = data.genes[gene] | ||
|
|
||
| geneData.forEach((values, j) => { | ||
| const meanExpression = values[0] | ||
| const percentExpressing = values[1] | ||
|
|
||
| // Use raw mean expression values, but convert zeros to NaN | ||
| // This excludes them from Morpheus color scaling while preserving actual values | ||
| const expressionValue = meanExpression === 0 ? NaN : meanExpression | ||
| dataset.setValue(i, j, expressionValue, 0) // Raw mean expression for color (zeros as NaN) | ||
| // Scale percent expressing to 0-100 range for better sizing | ||
| dataset.setValue(i, j, percentExpressing * 100, 1) // Percent expressing (0-100) for size | ||
| }) | ||
| }) | ||
|
|
||
| return dataset | ||
| }, | ||
|
|
||
| /** | ||
| * Add custom properties to enable dot plot mode | ||
| */ | ||
| configureDotPlot(dataset) { | ||
| // Add a property to indicate this is dot plot data | ||
| dataset._isDotPlot = true | ||
| dataset._dotPlotSizeSeries = 1 // Percent expressing | ||
| dataset._dotPlotColorSeries = 0 // Mean expression | ||
|
|
||
| return dataset | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Register a custom JSON reader for dot plot format | ||
| */ | ||
| const OriginalJsonReader = window.morpheus.JsonDatasetReader | ||
|
|
||
| window.morpheus.JsonDatasetReader = function() { | ||
| OriginalJsonReader.call(this) | ||
| } | ||
|
|
||
| window.morpheus.JsonDatasetReader.prototype = Object.create(OriginalJsonReader.prototype) | ||
|
|
||
| const originalRead = OriginalJsonReader.prototype.read | ||
| window.morpheus.JsonDatasetReader.prototype.read = function(fileOrUrl, callback) { | ||
| const self = this | ||
|
|
||
| // Check if it's our dot plot format | ||
| window.morpheus.Util.getText(fileOrUrl).then(text => { | ||
| try { | ||
| const data = JSON.parse(text) | ||
|
|
||
| // Check if it matches our dot plot format | ||
| if (data.annotation_name && data.values && data.genes) { | ||
| let dataset = window.morpheus.DotPlotConverter.createDataset(data) | ||
| dataset = window.morpheus.DotPlotConverter.configureDotPlot(dataset) | ||
| callback(null, dataset) | ||
| } else { | ||
| // Fall back to original reader | ||
| originalRead.call(self, fileOrUrl, callback) | ||
| } | ||
| } catch (err) { | ||
| callback(err) | ||
| } | ||
| }).catch(err => { | ||
| callback(err) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Helper to create dot plot directly from your data object | ||
| */ | ||
| window.createMorpheusDotPlot = function(data) { | ||
| const dataset = window.morpheus.DotPlotConverter.createDataset(data) | ||
| return window.morpheus.DotPlotConverter.configureDotPlot(dataset) | ||
| } | ||
|
|
||
| /** | ||
| * Patch the HeatMap to properly handle dot plot sizing with __count series | ||
| */ | ||
| const OriginalHeatMap = window.morpheus.HeatMap | ||
| window.morpheus.HeatMap = function(options) { | ||
| const heatmap = new OriginalHeatMap(options) | ||
|
|
||
| // Check if this is a precomputed dot plot dataset | ||
| if (options.dataset && options.dataset._isDotPlot) { | ||
| // Force the heatmap to use series 1 for sizing | ||
| if (heatmap.heatMapElementCanvas) { | ||
| heatmap.heatMapElementCanvas.sizeBySeriesIndex = 1 | ||
| } | ||
| } | ||
|
|
||
| return heatmap | ||
| } | ||
|
|
||
| // Copy static properties | ||
| Object.setPrototypeOf(window.morpheus.HeatMap, OriginalHeatMap) | ||
| window.morpheus.HeatMap.prototype = OriginalHeatMap.prototype | ||
| } | ||
|
|
||
| // Start trying to apply the patch | ||
| applyDotPlotPatch() | ||
| })() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
db/migrate/20251106100123_add_dot_plot_preprocessing_frontend_feature_flag.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class AddDotPlotPreprocessingFrontendFeatureFlag < Mongoid::Migration | ||
| def self.up | ||
| FeatureFlag.find_or_create_by(name: 'dot_plot_preprocessing_frontend') do |flag| | ||
| flag.default_value = false | ||
| flag.description = 'Enable pre-computed dot plot data from backend preprocessing' | ||
| end | ||
| end | ||
|
|
||
| def self.down | ||
| flag = FeatureFlag.find_by(name: 'dot_plot_preprocessing_frontend') | ||
| flag.destroy if flag.present? | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't ever be the case - all records are destroyed on study deletion, though clearly at some point something went sideways locally and you ended up with orphaned data. I did a quick check on staging & production and we don't have any of these (good), but leaving this check in place is harmless.