diff --git a/src/render/insight_data.py b/src/render/insight_data.py index 00e5fe48..a41dc032 100644 --- a/src/render/insight_data.py +++ b/src/render/insight_data.py @@ -44,6 +44,43 @@ def heatmap_levels(file_manager, dataset_id, name_tag): return names + +# Scores of a deconvolved mass the deconvolved heatmaps can be colored by +# (toolbar "Color"), as label to column of `with_mass_scores` +MASS_SCORE_COLUMNS = {'QScore': 'qscore', 'Cosine score': 'cosine', 'SNR': 'snr'} +# Column whose values the toolbar's "Layers" shows or hides: in how many charge +# states a mass was found +CHARGE_STATES_COLUMN = 'charge_states' + + +def with_mass_scores(file_manager, dataset_id, heatmap): + """A deconvolved heatmap frame (or one of its levels) with each mass's + QScore, cosine score, SNR and the number of charge states it was found + in (1, 2 or "3 or more"), taken from the mass table by scan and mass.""" + scores = ( + scan(file_manager, dataset_id, 'mass_table') + .select( + pl.col('index').cast(pl.UInt32).alias('scan_idx'), + 'QScore', 'CosineScore', 'SNR', 'MinCharges', 'MaxCharges', + ) + .explode(['QScore', 'CosineScore', 'SNR', 'MinCharges', 'MaxCharges']) + .with_columns(pl.int_range(pl.len()).over('scan_idx').cast(pl.Int64).alias('mass_idx')) + .select( + 'scan_idx', 'mass_idx', + pl.col('QScore').cast(pl.Float64).alias('qscore'), + pl.col('CosineScore').cast(pl.Float64).alias('cosine'), + pl.col('SNR').cast(pl.Float64).alias('snr'), + (pl.col('MaxCharges') - pl.col('MinCharges') + 1) + .clip(upper_bound=3).cast(pl.Utf8) + .replace({'1': '1 charge', '2': '2 charges', '3': '3 or more'}) + .alias(CHARGE_STATES_COLUMN), + ) + ) + return ( + heatmap.with_columns(pl.col('scan_idx').cast(pl.UInt32), pl.col('mass_idx').cast(pl.Int64)) + .join(scores, on=['scan_idx', 'mass_idx'], how='left') + ) + # Peaks per MS1 spectrum the 3D MS1 map draws: the strongest of each MS1_MAP_PEAKS_PER_SPECTRUM = 40 diff --git a/src/render/render.py b/src/render/render.py index e0ee32f2..75f6a6d9 100644 --- a/src/render/render.py +++ b/src/render/render.py @@ -544,13 +544,24 @@ def _heatmap(comp_name, file_manager, dataset_id): raise MissingResult(result) # The levels the parse downsampled; without them Insight builds its own levels = insight_data.heatmap_levels(file_manager, dataset_id, result) + # A deconvolved map can be colored by each mass's scores and show or hide + # masses by the number of charge states they were found in (toolbar) + scored = sets_mass and insight_data.result_path(file_manager, dataset_id, 'mass_table') is not None + + def frame(name): + data = insight_data.scan(file_manager, dataset_id, name) + return insight_data.with_mass_scores(file_manager, dataset_id, data) if scored else data + + options = dict( + color_columns=insight_data.MASS_SCORE_COLUMNS, + layer_column=insight_data.CHARGE_STATES_COLUMN, + ) if scored else {} return _component( - Heatmap, comp_name, file_manager, dataset_id, [result, *(levels or [])], + Heatmap, comp_name, file_manager, dataset_id, + [result, *(levels or []), *(['mass_table'] if scored else [])], lambda: dict( - data=insight_data.scan(file_manager, dataset_id, result), - levels=None if levels is None else [ - insight_data.scan(file_manager, dataset_id, name) for name in levels - ], + data=frame(result), + levels=None if levels is None else [frame(name) for name in levels], x_column='rt', y_column='mass', intensity_column='intensity', interactivity=interactivity, title=title, x_label='Retention Time', y_label=y_label, @@ -562,6 +573,7 @@ def _heatmap(comp_name, file_manager, dataset_id): # FLASHViewer's export name; its wheel scrolled the page svg_filename='FLASHViewer-heatmap', scroll_zoom=False, ), + render=options, ) diff --git a/tests/test_insight_data.py b/tests/test_insight_data.py index f316cc78..305670d3 100644 --- a/tests/test_insight_data.py +++ b/tests/test_insight_data.py @@ -168,3 +168,22 @@ def test_ms1_peak_map_keeps_the_strongest_peaks_of_each_spectrum(fd): strongest = raw.filter(pl.col('scan_idx') == first)['intensity'].sort(descending=True).head(5) kept = peaks.filter(pl.col('scan_idx') == first)['intensity'].sort(descending=True) assert kept.to_list() == pytest.approx(strongest.cast(pl.Float64).to_list()) + + +@pytest.mark.parametrize('name_tag', ['ms1_deconv_heatmap', 'ms2_deconv_heatmap']) +def test_mass_scores_join_the_heatmap_by_spectrum_and_mass(fd, name_tag): + heatmap = insight_data.scan(fd, FD_DATASET, name_tag) + scored = insight_data.with_mass_scores(fd, FD_DATASET, heatmap).collect() + assert scored.height == heatmap.collect().height + for column in [*insight_data.MASS_SCORE_COLUMNS.values(), insight_data.CHARGE_STATES_COLUMN]: + assert scored[column].null_count() == 0 + assert set(scored[insight_data.CHARGE_STATES_COLUMN]) <= {'1 charge', '2 charges', '3 or more'} + + # A point gets the scores of its own mass in the mass table + masses = insight_data.mass_table(fd, FD_DATASET).collect() + point = scored.row(0, named=True) + mass = masses.filter( + (pl.col('scan_idx') == point['scan_idx']) & (pl.col('mass_idx') == point['mass_idx']) + ).row(0, named=True) + assert point['qscore'] == pytest.approx(mass['QScore']) + assert point['mass'] == pytest.approx(mass['MonoMass'])