Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/eighty-coats-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@platforma-open/milaboratories.cdr3-spectratype.model": patch
"@platforma-open/milaboratories.cdr3-spectratype.ui": patch
---

Add AA abundance per position plot
74 changes: 69 additions & 5 deletions model/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type UiState = {
weightedFlag: boolean;
vStackedBarPlotState: GraphMakerState;
cdr3StackedBarPlotState: GraphMakerState;
aaPositionPlotState: GraphMakerState;
};

export const model = BlockModel.create()
Expand Down Expand Up @@ -47,6 +48,11 @@ export const model = BlockModel.create()
template: 'stackedBar',
currentTab: null,
},
aaPositionPlotState: {
title: 'CDR3 AA Position',
template: 'stackedBar',
currentTab: null,
},
})

.argsValid((ctx) => {
Expand Down Expand Up @@ -120,17 +126,75 @@ export const model = BlockModel.create()
);
})

.outputWithStatus('aaPositionPf', (ctx) => {
const datasetRef = ctx.args.datasetRef;
if (datasetRef === undefined) return undefined;

const pCols = ctx.resultPool.getAnchoredPColumns(
{ main: datasetRef },
[{
axes: [
{ anchor: 'main', idx: 0 },
{ anchor: 'main', idx: 1 },
{ name: 'pl7.app/vdj/chain' },
{ name: 'pl7.app/vdj/numberingPosition' },
],
}],
);

if (!pCols || pCols.length === 0) return undefined;
return ctx.createPFrame(pCols);
})

.output('aaPositionPCols', (ctx) => {
const datasetRef = ctx.args.datasetRef;
if (datasetRef === undefined) return undefined;

const pCols = ctx.resultPool.getAnchoredPColumns(
{ main: datasetRef },
[{
axes: [
{ anchor: 'main', idx: 0 },
{ anchor: 'main', idx: 1 },
{ name: 'pl7.app/vdj/chain' },
{ name: 'pl7.app/vdj/numberingPosition' },
],
}],
);

if (!pCols || pCols.length === 0) return undefined;
return pCols.map((c) => ({ columnId: c.id, spec: c.spec } satisfies PColumnIdAndSpec));
})

.output('isRunning', (ctx) => ctx.outputs?.getIsReadyOrError() === false)

.title(() => 'CDR3 Spectratype')

.subtitle((ctx) => ctx.args.customBlockLabel || ctx.args.defaultBlockLabel)

.sections((_) => [
{ type: 'link', href: '/', label: 'Bubble Plot' },
{ type: 'link', href: '/vStackedBarPlot', label: 'V Spectratype' },
{ type: 'link', href: '/cdr3StackedBarPlot', label: 'CDR3 Spectratype' },
])
.sections((ctx) => {
const datasetRef = ctx.args?.datasetRef;
const hasAaPositions = datasetRef
? (ctx.resultPool.getAnchoredPColumns(
{ main: datasetRef },
[{
axes: [
{ anchor: 'main', idx: 0 },
{ anchor: 'main', idx: 1 },
{ name: 'pl7.app/vdj/chain' },
{ name: 'pl7.app/vdj/numberingPosition' },
],
}],
) ?? []).length > 0
: false;

return [
{ type: 'link', href: '/', label: 'Bubble Plot' },
{ type: 'link', href: '/vStackedBarPlot', label: 'V Spectratype' },
{ type: 'link', href: '/cdr3StackedBarPlot', label: 'CDR3 Spectratype' },
...(hasAaPositions ? [{ type: 'link' as const, href: '/aaPositionPlot' as const, label: 'AA Position' }] : []),
];
})
Comment on lines +129 to +197

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The call to ctx.resultPool.getAnchoredPColumns with identical arguments is repeated three times in outputWithStatus('aaPositionPf', ...), output('aaPositionPCols', ...), and sections(...). This is inefficient, especially if getAnchoredPColumns is an expensive operation, and it also leads to code duplication, which can make maintenance more difficult.

A better approach would be to create a single intermediate output to compute the columns once. The result can then be reused in the other outputs and sections, which will improve performance and maintainability.

  .output('internalAaPositionPCols', (ctx) => {
    const datasetRef = ctx.args.datasetRef;
    if (datasetRef === undefined) return undefined;

    const pCols = ctx.resultPool.getAnchoredPColumns(
      { main: datasetRef },
      [{
        axes: [
          { anchor: 'main', idx: 0 },
          { anchor: 'main', idx: 1 },
          { name: 'pl7.app/vdj/chain' },
          { name: 'pl7.app/vdj/numberingPosition' },
        ],
      }],
    );

    return (pCols && pCols.length > 0) ? pCols : undefined;
  })

  .outputWithStatus('aaPositionPf', (ctx) => {
    const pCols = ctx.outputs.internalAaPositionPCols;
    if (pCols === undefined) return undefined;
    return ctx.createPFrame(pCols);
  })

  .output('aaPositionPCols', (ctx) => {
    const pCols = ctx.outputs.internalAaPositionPCols;
    if (pCols === undefined) return undefined;
    return pCols.map((c) => ({ columnId: c.id, spec: c.spec } satisfies PColumnIdAndSpec));
  })

  .output('isRunning', (ctx) => ctx.outputs?.getIsReadyOrError() === false)

  .title(() => 'CDR3 Spectratype')

  .subtitle((ctx) => ctx.args.customBlockLabel || ctx.args.defaultBlockLabel)

  .sections((ctx) => {
    const hasAaPositions = ctx.outputs.internalAaPositionPCols !== undefined;

    return [
      { type: 'link', href: '/', label: 'Bubble Plot' },
      { type: 'link', href: '/vStackedBarPlot', label: 'V Spectratype' },
      { type: 'link', href: '/cdr3StackedBarPlot', label: 'CDR3 Spectratype' },
      ...(hasAaPositions ? [{ type: 'link' as const, href: '/aaPositionPlot' as const, label: 'AA Position' }] : []),
    ];
  })


.done(2);

Expand Down
Loading
Loading