@datagrok/hit-triage 1.9.2 → 1.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/CLAUDE.md +199 -0
- package/css/hit-triage.css +0 -41
- package/dist/package-test.js +1 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/files/pepTriage/campaigns/DMT-1/campaign.json +1 -0
- package/files/pepTriage/campaigns/DMT-1/enriched_table.csv +2262 -0
- package/files/pepTriage/templates/Demo Template.json +1 -0
- package/package.json +6 -4
- package/src/app/accordeons/new-campaign-accordeon.ts +24 -6
- package/src/app/accordeons/new-pep-triage-template-accordeon.ts +208 -0
- package/src/app/accordeons/new-template-accordeon.ts +15 -9
- package/src/app/consts.ts +23 -10
- package/src/app/dialogs/functions-dialog.ts +14 -221
- package/src/app/dialogs/merge-table-dialog.ts +360 -0
- package/src/app/hit-app-base.ts +4 -7
- package/src/app/hit-design-app.ts +19 -42
- package/src/app/hit-design-views/info-view.ts +17 -15
- package/src/app/hit-triage-app.ts +44 -17
- package/src/app/hit-triage-views/info-view.ts +268 -49
- package/src/app/pep-triage-app.ts +87 -0
- package/src/app/pep-triage-views/info-view.ts +123 -0
- package/src/app/types.ts +40 -66
- package/src/app/utils/calculate-single-cell.ts +20 -219
- package/src/app/utils/merge-table.ts +297 -0
- package/src/app/utils.ts +100 -123
- package/src/package-api.ts +12 -0
- package/src/package.g.ts +24 -0
- package/src/package.ts +62 -0
package/src/app/types.ts
CHANGED
|
@@ -1,61 +1,30 @@
|
|
|
1
1
|
import * as DG from 'datagrok-api/dg';
|
|
2
2
|
|
|
3
|
+
// Re-export shared compute-function types from the statistics library
|
|
4
|
+
export type {TemplateFunction, TemplateScript, ComputeQuery, TemplateCompute,
|
|
5
|
+
ComputeFunctions, IComputeDialogResult, IFunctionArgs,
|
|
6
|
+
IDescriptorTree, Descriptor, IChemFunctionsDialogResult} from '@datagrok-libraries/statistics/src/compute-functions/types';
|
|
7
|
+
import type {TemplateFunction, TemplateScript, TemplateCompute} from '@datagrok-libraries/statistics/src/compute-functions/types';
|
|
8
|
+
|
|
9
|
+
// Backward-compatible aliases
|
|
10
|
+
export type HitTriageTemplateFunction = TemplateFunction;
|
|
11
|
+
export type HitTriageTemplateScript = TemplateScript;
|
|
12
|
+
export type HitTriageComputeQuery = TemplateScript & { inputName: string };
|
|
13
|
+
export type HitTriageTemplateCompute = TemplateCompute;
|
|
14
|
+
|
|
3
15
|
export type AppName = keyof CampaignsType;
|
|
4
16
|
|
|
5
17
|
export type CampaignsType = {
|
|
6
18
|
'Hit Triage': HitTriageCampaign,
|
|
7
19
|
'Hit Design': HitDesignCampaign,
|
|
8
20
|
'PeptiHit': HitDesignCampaign,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export type IDescriptorTree = {
|
|
12
|
-
[key: string]: {
|
|
13
|
-
descriptors: Array<Descriptor>,
|
|
14
|
-
} & Descriptor;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type Descriptor = {
|
|
18
|
-
name: string,
|
|
19
|
-
description: string,
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export type IFunctionArgs<T = any> = {
|
|
23
|
-
[key: string]: T,
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type HitTriageTemplateFunction = {
|
|
27
|
-
package: string,
|
|
28
|
-
name: string,
|
|
29
|
-
args: IFunctionArgs,
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type HitTriageTemplateScript = {
|
|
33
|
-
name: string,
|
|
34
|
-
args: IFunctionArgs,
|
|
35
|
-
id: string,
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export type HitTriageComputeQuery = HitTriageTemplateScript & {
|
|
39
|
-
inputName: string,
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type IComputeDialogResult = {
|
|
43
|
-
descriptors: string[],
|
|
44
|
-
externals: {
|
|
45
|
-
[_: string]: IFunctionArgs
|
|
46
|
-
},
|
|
47
|
-
scripts?: {
|
|
48
|
-
[_: string]: IFunctionArgs
|
|
49
|
-
}
|
|
50
|
-
queries?: {
|
|
51
|
-
[_: string]: IFunctionArgs
|
|
52
|
-
}
|
|
21
|
+
'PepTriage': HitTriageCampaign,
|
|
53
22
|
}
|
|
54
23
|
|
|
55
24
|
export type HitTriageTemplate = {
|
|
56
25
|
name: string,
|
|
57
26
|
key: string,
|
|
58
|
-
compute:
|
|
27
|
+
compute: TemplateCompute,
|
|
59
28
|
submit?: HitTriageTemplateSubmit,
|
|
60
29
|
campaignFields: HitTriageCampaignField[],
|
|
61
30
|
dataSourceType: IngestType,
|
|
@@ -86,16 +55,6 @@ export type HitTriageTemplateIngest = {
|
|
|
86
55
|
molColName: string,
|
|
87
56
|
};
|
|
88
57
|
|
|
89
|
-
export type HitTriageTemplateCompute = {
|
|
90
|
-
descriptors: {
|
|
91
|
-
enabled: boolean,
|
|
92
|
-
args: string[],
|
|
93
|
-
}
|
|
94
|
-
functions: HitTriageTemplateFunction[],
|
|
95
|
-
scripts?: HitTriageTemplateScript[],
|
|
96
|
-
queries?: HitTriageTemplateScript[],
|
|
97
|
-
};
|
|
98
|
-
|
|
99
58
|
export type HitTriageTemplateSubmit = {
|
|
100
59
|
fName: string,
|
|
101
60
|
package?: string | undefined
|
|
@@ -132,11 +91,6 @@ export type TriagePermissions = {
|
|
|
132
91
|
view: string[],
|
|
133
92
|
};
|
|
134
93
|
|
|
135
|
-
export type IChemFunctionsDialogResult = {
|
|
136
|
-
okProxy: () => void,
|
|
137
|
-
root: HTMLElement,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
94
|
export type INewTemplateResult<T> = {
|
|
141
95
|
template: Promise<T>,
|
|
142
96
|
root: HTMLElement,
|
|
@@ -148,13 +102,33 @@ export type INewTemplateResult<T> = {
|
|
|
148
102
|
export type HitDesignTemplate = Omit<HitTriageTemplate, 'dataSourceType' | 'queryFunctionName'> &
|
|
149
103
|
{stages: string[]};
|
|
150
104
|
|
|
151
|
-
export type
|
|
105
|
+
export type HitDesignMergeMode = 'smiles' | 'vid';
|
|
106
|
+
export type HitDesignMergeClashStrategy = 'skip' | 'overwrite';
|
|
107
|
+
|
|
108
|
+
export type HitDesignMergeConfig = {
|
|
109
|
+
mode: HitDesignMergeMode,
|
|
110
|
+
// Only set when the source file lives on the Datagrok file share, so it can be reopened later
|
|
111
|
+
filePath?: string,
|
|
112
|
+
// Incoming column name to use as molecule source (required for SMILES mode, optional for VID mode)
|
|
113
|
+
molColName?: string,
|
|
114
|
+
// Incoming column name carrying V-iD values (only used in VID mode)
|
|
115
|
+
vidColName?: string,
|
|
116
|
+
addNewRows: boolean,
|
|
117
|
+
runComputeOnNewRows: boolean,
|
|
118
|
+
clashStrategy: HitDesignMergeClashStrategy,
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type HitDesignCampaign = Omit<HitTriageCampaign, 'filters' | 'ingest'> & {
|
|
122
|
+
tilesViewerFormSketch?: string,
|
|
123
|
+
mergeConfig?: HitDesignMergeConfig,
|
|
124
|
+
};
|
|
152
125
|
|
|
153
126
|
// todo: probably add some more stuff
|
|
154
127
|
export type PeptiHitTemplate = HitDesignTemplate & {toAtomiLevelProps?: {[key: string]: any}}
|
|
155
128
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
129
|
+
// ##################### PEP TRIAGE TYPES #####################
|
|
130
|
+
|
|
131
|
+
export type PepTriageTemplate = HitTriageTemplate & {
|
|
132
|
+
sequenceColumnName: string,
|
|
133
|
+
moleculeColumnName?: string,
|
|
134
|
+
}
|
|
@@ -1,228 +1,29 @@
|
|
|
1
1
|
/* eslint-disable max-len */
|
|
2
|
-
import * as grok from 'datagrok-api/grok';
|
|
3
|
-
import * as ui from 'datagrok-api/ui';
|
|
4
2
|
import * as DG from 'datagrok-api/dg';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import {
|
|
4
|
+
calculateCellValues as _calculateCellValues,
|
|
5
|
+
calculateColumns as _calculateColumns,
|
|
6
|
+
} from '@datagrok-libraries/statistics/src/compute-functions/execution';
|
|
7
|
+
import type {ComputeExecutionOptions} from '@datagrok-libraries/statistics/src/compute-functions/execution';
|
|
8
|
+
import {TemplateFunction, TemplateScript, IComputeDialogResult} from '../types';
|
|
9
|
+
import {HitDesignMolColName} from '../consts';
|
|
8
10
|
import {_package} from '../../package';
|
|
9
11
|
|
|
12
|
+
function getOptions(): ComputeExecutionOptions {
|
|
13
|
+
return {
|
|
14
|
+
convertToSmiles: (v: string) => _package.convertToSmiles(v),
|
|
15
|
+
logger: _package.logger,
|
|
16
|
+
molColName: HitDesignMolColName,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
10
20
|
export async function calculateCellValues(
|
|
11
|
-
values: string[], descriptors: string[], functions:
|
|
12
|
-
queries:
|
|
21
|
+
values: string[], descriptors: string[], functions: TemplateFunction[], scripts: TemplateScript[] = [],
|
|
22
|
+
queries: TemplateScript[] = [],
|
|
13
23
|
): Promise<DG.DataFrame> {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const canonicalSmiles = values.map((v) => _package.convertToSmiles(v));
|
|
17
|
-
const col = DG.Column.fromStrings(HitDesignMolColName, canonicalSmiles);
|
|
18
|
-
const table = DG.DataFrame.fromColumns([col]);
|
|
19
|
-
table.name = 'HD cell values';
|
|
20
|
-
await table.meta.detectSemanticTypes();
|
|
21
|
-
const promises: Promise<void>[] = [];
|
|
22
|
-
promises.push(new Promise(async (resolve) => {
|
|
23
|
-
try {
|
|
24
|
-
if (descriptors.length)
|
|
25
|
-
await grok.chem.descriptors(table, col.name, descriptors);
|
|
26
|
-
} catch (e) {
|
|
27
|
-
_package.logger.error('Descriptors calculation error');
|
|
28
|
-
_package.logger.error(e);
|
|
29
|
-
}
|
|
30
|
-
resolve();
|
|
31
|
-
}));
|
|
32
|
-
|
|
33
|
-
for (const func of functions) {
|
|
34
|
-
promises.push(new Promise(async (resolve) => {
|
|
35
|
-
try {
|
|
36
|
-
const props = func.args;
|
|
37
|
-
const fs = DG.Func.find({package: func.package, name: func.name});
|
|
38
|
-
if (!fs.length || !fs[0]) {
|
|
39
|
-
console.warn(`Function ${func.name} from package ${func.package} is not found`);
|
|
40
|
-
resolve();
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const f = fs[0];
|
|
44
|
-
const tablePropName = f.inputs[0].name;
|
|
45
|
-
const colPropName = f.inputs[1].name;
|
|
46
|
-
await f.apply({...props, [tablePropName]: table, [colPropName]: col.name});
|
|
47
|
-
} catch (e) {
|
|
48
|
-
_package.logger.error(e);
|
|
49
|
-
}
|
|
50
|
-
resolve();
|
|
51
|
-
}));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
for (const script of scripts) {
|
|
55
|
-
const props = script.args;
|
|
56
|
-
promises.push(new Promise(async (resolve) => {
|
|
57
|
-
try {
|
|
58
|
-
const scriptFunc = DG.Func.find({name: script.name})
|
|
59
|
-
.filter((s) => s.type === funcTypeNames.script && s.id === script.id)[0] as DG.Script | undefined;
|
|
60
|
-
if (scriptFunc) {
|
|
61
|
-
const tablePropName = scriptFunc.inputs[0].name;
|
|
62
|
-
const colPropName = scriptFunc.inputs[1].name;
|
|
63
|
-
|
|
64
|
-
const r: DG.DataFrame = await scriptFunc.apply({...props, [tablePropName]: table, [colPropName]: col.name});
|
|
65
|
-
if (r && r.rowCount === table.rowCount && scriptFunc.language === 'python') {
|
|
66
|
-
for (const c of r.columns) {
|
|
67
|
-
c.name = table.columns.getUnusedName(c.name);
|
|
68
|
-
table.columns.add(c);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
} catch (e) {
|
|
73
|
-
_package.logger.error(e);
|
|
74
|
-
}
|
|
75
|
-
resolve();
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
for (const query of queries) {
|
|
80
|
-
const props = query.args;
|
|
81
|
-
promises.push(new Promise(async (resolve) => {
|
|
82
|
-
try {
|
|
83
|
-
const queryFunc = DG.Func.find({name: query.name})
|
|
84
|
-
.filter((s) => s.type === funcTypeNames.query && s.id === query.id)[0];
|
|
85
|
-
if (queryFunc) {
|
|
86
|
-
const listPropName = queryFunc.inputs[0].name;
|
|
87
|
-
const valueList = [canonicalSmiles];
|
|
88
|
-
const qRes = await queryFunc.apply({...props, [listPropName]: valueList});
|
|
89
|
-
if (!qRes || qRes.rowCount === 0) {
|
|
90
|
-
resolve();
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
await joinQueryResults(table, HitDesignMolColName, qRes);
|
|
94
|
-
}
|
|
95
|
-
} catch (e) {
|
|
96
|
-
_package.logger.error(e);
|
|
97
|
-
}
|
|
98
|
-
resolve();
|
|
99
|
-
}));
|
|
100
|
-
}
|
|
101
|
-
await Promise.all(promises);
|
|
102
|
-
pg.close();
|
|
103
|
-
const molCol = table.col(HitDesignMolColName)!;
|
|
104
|
-
for (let i = 0; i < molCol.length; i++) {
|
|
105
|
-
// restore previous values
|
|
106
|
-
molCol.set(i, values[i], false);
|
|
107
|
-
}
|
|
108
|
-
return table;
|
|
109
|
-
};
|
|
24
|
+
return _calculateCellValues(values, descriptors, functions, scripts, queries, getOptions());
|
|
25
|
+
}
|
|
110
26
|
|
|
111
27
|
export async function calculateColumns(resultMap: IComputeDialogResult, dataFrame: DG.DataFrame, molColName: string) {
|
|
112
|
-
|
|
113
|
-
// first step: convert all values to canonical smiles.
|
|
114
|
-
const molCol = dataFrame.col(molColName);
|
|
115
|
-
if (!molCol)
|
|
116
|
-
throw new Error('There is no molecule column in dataframe');
|
|
117
|
-
|
|
118
|
-
const molSmilesColName = `~${molColName}.canonicalSmiles`; // chem package might have already created that, but we update it just in any case
|
|
119
|
-
const molSmilesCol = dataFrame.columns.getOrCreate(molSmilesColName, DG.TYPE.STRING);
|
|
120
|
-
// we don't want to overwrite existing values, as they can contain important conformation information
|
|
121
|
-
molSmilesCol.semType = DG.SEMTYPE.MOLECULE;
|
|
122
|
-
|
|
123
|
-
const molColList = molCol.toList();
|
|
124
|
-
for (let i = 0; i < molColList.length; i++) {
|
|
125
|
-
if (molCol.isNone(i))
|
|
126
|
-
continue;
|
|
127
|
-
const value: string | null = molColList[i];
|
|
128
|
-
if (!value)
|
|
129
|
-
continue;
|
|
130
|
-
const newVal = _package.convertToSmiles(value);
|
|
131
|
-
molSmilesCol.set(i, newVal, false);
|
|
132
|
-
}
|
|
133
|
-
const promises: Promise<void>[] = [];
|
|
134
|
-
promises.push(new Promise(async (resolve) => {
|
|
135
|
-
try {
|
|
136
|
-
if (resultMap.descriptors && resultMap.descriptors.length > 0)
|
|
137
|
-
await grok.chem.descriptors(dataFrame!, molSmilesColName!, resultMap.descriptors);
|
|
138
|
-
} catch (e) {
|
|
139
|
-
_package.logger.error('Descriptors calculation error');
|
|
140
|
-
_package.logger.error(e);
|
|
141
|
-
}
|
|
142
|
-
resolve();
|
|
143
|
-
}));
|
|
144
|
-
|
|
145
|
-
for (const funcName of Object.keys(resultMap.externals)) {
|
|
146
|
-
promises.push(new Promise(async (resolve) => {
|
|
147
|
-
try {
|
|
148
|
-
const props = resultMap.externals[funcName];
|
|
149
|
-
const f = DG.Func.find({package: funcName.split(':')[0], name: funcName.split(':')[1]})[0];
|
|
150
|
-
const tablePropName = f.inputs[0].name;
|
|
151
|
-
const colPropName = f.inputs[1].name;
|
|
152
|
-
if (props)
|
|
153
|
-
await f.apply({...props, [tablePropName]: dataFrame!, [colPropName]: molSmilesColName});
|
|
154
|
-
} catch (e) {
|
|
155
|
-
_package.logger.error(e);
|
|
156
|
-
}
|
|
157
|
-
resolve();
|
|
158
|
-
}));
|
|
159
|
-
};
|
|
160
|
-
// handling scripts
|
|
161
|
-
for (const scriptName of Object.keys(resultMap.scripts ?? {})) {
|
|
162
|
-
const props = resultMap.scripts![scriptName];
|
|
163
|
-
promises.push(new Promise(async (resolve) => {
|
|
164
|
-
if (props) {
|
|
165
|
-
const scriptParts = scriptName.split(':');
|
|
166
|
-
const scriptId = scriptParts[2];
|
|
167
|
-
if (!scriptId) {
|
|
168
|
-
resolve();
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
const sn = scriptParts[1]; // script name
|
|
173
|
-
const s = DG.Func.find({name: sn})
|
|
174
|
-
.filter((s) => s.type === funcTypeNames.script && s.id === scriptId)[0] as DG.Script | undefined;
|
|
175
|
-
if (!s) {
|
|
176
|
-
resolve();
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
const tablePropName = s!.inputs[0].name;
|
|
180
|
-
const colPropName = s!.inputs[1].name;
|
|
181
|
-
const r: DG.DataFrame = await s.apply({...props, [tablePropName]: dataFrame, [colPropName]: molSmilesColName});
|
|
182
|
-
if (r && r.rowCount === dataFrame.rowCount && s.language === 'python') {
|
|
183
|
-
for (const c of r.columns) {
|
|
184
|
-
c.name = dataFrame.columns.getUnusedName(c.name);
|
|
185
|
-
dataFrame.columns.add(c);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
} catch (e) {
|
|
189
|
-
_package.logger.error(e);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
resolve();
|
|
193
|
-
}));
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
// handling queries
|
|
197
|
-
for (const queryName of Object.keys(resultMap.queries ?? {})) {
|
|
198
|
-
const props = resultMap.queries![queryName];
|
|
199
|
-
promises.push(new Promise(async (resolve) => {
|
|
200
|
-
if (props) {
|
|
201
|
-
const queryParts = queryName.split(':');
|
|
202
|
-
const queryId = queryParts[2];
|
|
203
|
-
if (!queryId) {
|
|
204
|
-
resolve();
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
try {
|
|
208
|
-
const qn = queryParts[1]; // query name
|
|
209
|
-
const s = DG.Func.find({name: qn})
|
|
210
|
-
.filter((s) => s.type === funcTypeNames.query && s.id === queryId)[0];
|
|
211
|
-
if (!s) {
|
|
212
|
-
resolve();
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
const listPropName = s.inputs[0].name;
|
|
216
|
-
const resDf: DG.DataFrame = await s.apply({...props, [listPropName]: molSmilesCol.toList()});
|
|
217
|
-
if (resDf)
|
|
218
|
-
await joinQueryResults(dataFrame!, molSmilesColName!, resDf);
|
|
219
|
-
} catch (e) {
|
|
220
|
-
console.error(e);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
resolve();
|
|
224
|
-
}));
|
|
225
|
-
};
|
|
226
|
-
await Promise.all(promises);
|
|
227
|
-
pg.close();
|
|
28
|
+
return _calculateColumns(resultMap, dataFrame, molColName, getOptions());
|
|
228
29
|
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
import * as DG from 'datagrok-api/dg';
|
|
3
|
+
import * as grok from 'datagrok-api/grok';
|
|
4
|
+
import {HitDesignApp} from '../hit-design-app';
|
|
5
|
+
import {HitDesignMergeConfig, HitDesignMergeMode, HitDesignTemplate} from '../types';
|
|
6
|
+
import {HitDesignMolColName, TileCategoriesColName, ViDColName, ViDSemType} from '../consts';
|
|
7
|
+
import {calculateCellValues} from './calculate-single-cell';
|
|
8
|
+
import {registerMolsBatch} from './molreg';
|
|
9
|
+
import {_package} from '../../package';
|
|
10
|
+
|
|
11
|
+
const MOL_NAME_HINTS = ['molecule', 'smiles', 'mol', 'structure', 'canonical_smiles', 'canonicalsmiles'];
|
|
12
|
+
const VID_NAME_HINTS = ['v-id', 'vid', 'v_id'];
|
|
13
|
+
|
|
14
|
+
export function guessMoleculeColumn(df: DG.DataFrame): DG.Column | null {
|
|
15
|
+
const cols = df.columns.toList();
|
|
16
|
+
const semCols = cols.filter((c) => c.semType === DG.SEMTYPE.MOLECULE);
|
|
17
|
+
if (semCols.length > 0) {
|
|
18
|
+
const named = semCols.find((c) => MOL_NAME_HINTS.includes(c.name.toLowerCase()));
|
|
19
|
+
return named ?? semCols[0];
|
|
20
|
+
}
|
|
21
|
+
const named = cols.find((c) => MOL_NAME_HINTS.includes(c.name.toLowerCase()));
|
|
22
|
+
if (named) return named;
|
|
23
|
+
return cols.find((c) => c.type === DG.COLUMN_TYPE.STRING) ?? null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function guessVidColumn(df: DG.DataFrame): DG.Column | null {
|
|
27
|
+
const cols = df.columns.toList();
|
|
28
|
+
const named = cols.find((c) => VID_NAME_HINTS.includes(c.name.toLowerCase()));
|
|
29
|
+
if (named) return named;
|
|
30
|
+
const semCol = cols.find((c) => c.semType === ViDSemType);
|
|
31
|
+
if (semCol) return semCol;
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isLocalUploadFileInfo(fi: DG.FileInfo): boolean {
|
|
36
|
+
const fullPath = (fi.fullPath ?? '').toLowerCase();
|
|
37
|
+
const name = (fi.name ?? '').toLowerCase();
|
|
38
|
+
return !fullPath || fullPath === name;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type SourceMapping = {col: DG.Column; target: string; isComputed: boolean};
|
|
42
|
+
|
|
43
|
+
function getSourceMappings(
|
|
44
|
+
incomingDf: DG.DataFrame,
|
|
45
|
+
computedDf: DG.DataFrame | null,
|
|
46
|
+
campMolColName: string,
|
|
47
|
+
cfg: HitDesignMergeConfig,
|
|
48
|
+
): SourceMapping[] {
|
|
49
|
+
const sources: SourceMapping[] = [];
|
|
50
|
+
for (const col of incomingDf.columns.toList()) {
|
|
51
|
+
// In SMILES mode, never carry over a V-iD-like column from the incoming table:
|
|
52
|
+
// campaign V-iDs are authoritative.
|
|
53
|
+
if (cfg.mode === 'smiles' && col.name === ViDColName) continue;
|
|
54
|
+
|
|
55
|
+
let target = col.name;
|
|
56
|
+
if (cfg.molColName && col.name === cfg.molColName) target = campMolColName;
|
|
57
|
+
if (cfg.mode === 'vid' && cfg.vidColName && col.name === cfg.vidColName) target = ViDColName;
|
|
58
|
+
|
|
59
|
+
sources.push({col, target, isComputed: false});
|
|
60
|
+
}
|
|
61
|
+
if (computedDf) {
|
|
62
|
+
for (const col of computedDf.columns.toList()) {
|
|
63
|
+
if (col.name === HitDesignMolColName || col.name === campMolColName) continue;
|
|
64
|
+
if (sources.some((s) => s.target === col.name)) continue;
|
|
65
|
+
sources.push({col, target: col.name, isComputed: true});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return sources;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function setCorrectedValue(toCol: DG.Column, fromCol: DG.Column, toIdx: number, fromIdx: number): void {
|
|
72
|
+
try {
|
|
73
|
+
if (toCol.type === fromCol.type) {
|
|
74
|
+
toCol.set(toIdx, fromCol.get(fromIdx), false);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (toCol.type === DG.COLUMN_TYPE.STRING) {
|
|
78
|
+
toCol.set(toIdx, fromCol.getString(fromIdx), false);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const v = fromCol.get(fromIdx);
|
|
82
|
+
if (toCol.type === DG.COLUMN_TYPE.INT) {
|
|
83
|
+
if (typeof v === 'number') toCol.set(toIdx, Math.round(v), false);
|
|
84
|
+
else if (typeof v === 'string' && v.length > 0 && !isNaN(Number(v))) toCol.set(toIdx, Math.round(Number(v)), false);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (toCol.type === DG.COLUMN_TYPE.FLOAT) {
|
|
88
|
+
if (typeof v === 'number') toCol.set(toIdx, v, false);
|
|
89
|
+
else if (typeof v === 'string' && v.length > 0 && !isNaN(Number(v))) toCol.set(toIdx, Number(v), false);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
toCol.set(toIdx, v, false);
|
|
93
|
+
} catch (e) {
|
|
94
|
+
_package.logger.error(`Merge: failed to set ${toCol.name}[${toIdx}] from ${fromCol.name}[${fromIdx}]: ${e}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function isCellEmpty(col: DG.Column, idx: number): boolean {
|
|
99
|
+
if (col.isNone(idx)) return true;
|
|
100
|
+
if (col.type === DG.COLUMN_TYPE.STRING) {
|
|
101
|
+
const v = col.get(idx);
|
|
102
|
+
return v === '' || v == null;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Computes the campaign-side join keys, returns the per-row key array and a key → row map.
|
|
109
|
+
* In SMILES mode the key is the canonical SMILES; in VID mode it's the V-iD string.
|
|
110
|
+
*/
|
|
111
|
+
function buildCampaignKeys(
|
|
112
|
+
campDf: DG.DataFrame,
|
|
113
|
+
campMolColName: string,
|
|
114
|
+
mode: HitDesignMergeMode,
|
|
115
|
+
): {keys: (string | null)[]; map: Map<string, number>} {
|
|
116
|
+
const keys: (string | null)[] = new Array(campDf.rowCount).fill(null);
|
|
117
|
+
const map = new Map<string, number>();
|
|
118
|
+
if (mode === 'smiles') {
|
|
119
|
+
const col = campDf.col(campMolColName);
|
|
120
|
+
if (!col) throw new Error(`Campaign has no molecule column "${campMolColName}"`);
|
|
121
|
+
for (let i = 0; i < campDf.rowCount; i++) {
|
|
122
|
+
const v = col.get(i);
|
|
123
|
+
if (!v) continue;
|
|
124
|
+
const canon = _package.convertToSmiles(v);
|
|
125
|
+
if (canon && canon !== 'MALFORMED_INPUT_VALUE') {
|
|
126
|
+
keys[i] = canon;
|
|
127
|
+
if (!map.has(canon)) map.set(canon, i);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} else {
|
|
131
|
+
const col = campDf.col(ViDColName);
|
|
132
|
+
if (!col) throw new Error(`Campaign has no "${ViDColName}" column`);
|
|
133
|
+
for (let i = 0; i < campDf.rowCount; i++) {
|
|
134
|
+
const v = col.get(i);
|
|
135
|
+
if (!v) continue;
|
|
136
|
+
keys[i] = v;
|
|
137
|
+
if (!map.has(v)) map.set(v, i);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return {keys, map};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function buildIncomingKeys(incomingDf: DG.DataFrame, cfg: HitDesignMergeConfig): (string | null)[] {
|
|
144
|
+
const keys: (string | null)[] = new Array(incomingDf.rowCount).fill(null);
|
|
145
|
+
if (cfg.mode === 'smiles') {
|
|
146
|
+
const col = incomingDf.col(cfg.molColName!);
|
|
147
|
+
if (!col) throw new Error(`Incoming table has no column "${cfg.molColName}"`);
|
|
148
|
+
for (let i = 0; i < incomingDf.rowCount; i++) {
|
|
149
|
+
const v = col.get(i);
|
|
150
|
+
if (!v) continue;
|
|
151
|
+
const canon = _package.convertToSmiles(v);
|
|
152
|
+
if (canon && canon !== 'MALFORMED_INPUT_VALUE') keys[i] = canon;
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
const col = incomingDf.col(cfg.vidColName!);
|
|
156
|
+
if (!col) throw new Error(`Incoming table has no column "${cfg.vidColName}"`);
|
|
157
|
+
for (let i = 0; i < incomingDf.rowCount; i++) {
|
|
158
|
+
const v = col.get(i);
|
|
159
|
+
if (v) keys[i] = String(v);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return keys;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Merges `incomingDf` into the current campaign of `app` based on the supplied config.
|
|
167
|
+
* Supports both SMILES-based and V-iD-based joins, optional addition of new rows,
|
|
168
|
+
* and optional execution of the campaign compute pipeline on the incoming molecules.
|
|
169
|
+
*/
|
|
170
|
+
export async function mergeIntoCampaign<T extends HitDesignTemplate>(
|
|
171
|
+
app: HitDesignApp<T>,
|
|
172
|
+
incomingDf: DG.DataFrame,
|
|
173
|
+
cfg: HitDesignMergeConfig,
|
|
174
|
+
): Promise<void> {
|
|
175
|
+
if (!app.dataFrame) throw new Error('Campaign has no data');
|
|
176
|
+
const campDf = app.dataFrame;
|
|
177
|
+
const campMolColName = app.molColName;
|
|
178
|
+
|
|
179
|
+
// Effective flags: silently strip combinations that the dialog should already have prevented.
|
|
180
|
+
const canAddNewRows = cfg.addNewRows && !!cfg.molColName;
|
|
181
|
+
const willRunCompute = cfg.runComputeOnNewRows && canAddNewRows;
|
|
182
|
+
|
|
183
|
+
const {map: campKeyMap} = buildCampaignKeys(campDf, campMolColName, cfg.mode);
|
|
184
|
+
const incKeys = buildIncomingKeys(incomingDf, cfg);
|
|
185
|
+
|
|
186
|
+
// Decide which incoming rows match the campaign and which are net-new BEFORE running
|
|
187
|
+
// compute, so the compute pipeline only runs on the rows that will actually be added.
|
|
188
|
+
const matched: {campRow: number; incRow: number}[] = [];
|
|
189
|
+
const unmatched: number[] = [];
|
|
190
|
+
for (let i = 0; i < incomingDf.rowCount; i++) {
|
|
191
|
+
const k = incKeys[i];
|
|
192
|
+
if (!k) continue;
|
|
193
|
+
const campRow = campKeyMap.get(k);
|
|
194
|
+
if (campRow !== undefined) matched.push({campRow, incRow: i});
|
|
195
|
+
else if (canAddNewRows) unmatched.push(i);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Compute is only run for the unmatched (newly added) molecules, not for matched rows.
|
|
199
|
+
// The resulting DataFrame is row-aligned with `unmatched` (index `u`), not with the
|
|
200
|
+
// original incoming rows.
|
|
201
|
+
let computedDf: DG.DataFrame | null = null;
|
|
202
|
+
if (willRunCompute && cfg.molColName && unmatched.length > 0) {
|
|
203
|
+
const compute = app.campaign?.template?.compute ?? app.template?.compute;
|
|
204
|
+
const incMolCol = incomingDf.col(cfg.molColName);
|
|
205
|
+
if (compute && incMolCol) {
|
|
206
|
+
const newMols = unmatched.map((i) => incMolCol.get(i));
|
|
207
|
+
const descriptors = compute.descriptors.enabled ? compute.descriptors.args ?? [] : [];
|
|
208
|
+
try {
|
|
209
|
+
computedDf = await calculateCellValues(newMols, descriptors, compute.functions, compute.scripts ?? [], compute.queries ?? []);
|
|
210
|
+
} catch (e) {
|
|
211
|
+
_package.logger.error(e);
|
|
212
|
+
grok.shell.warning('Merge: compute pipeline failed; merging without computed columns.');
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const sources = getSourceMappings(incomingDf, computedDf, campMolColName, cfg);
|
|
218
|
+
|
|
219
|
+
// Add columns missing in the campaign
|
|
220
|
+
const newlyAdded = new Set<string>();
|
|
221
|
+
for (const s of sources) {
|
|
222
|
+
if (!campDf.columns.contains(s.target)) {
|
|
223
|
+
const nc = campDf.columns.addNew(s.target, s.col.type);
|
|
224
|
+
if (s.col.semType) nc.semType = s.col.semType;
|
|
225
|
+
newlyAdded.add(s.target);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
app.isJoining = true;
|
|
230
|
+
try {
|
|
231
|
+
// Fill matched rows. Computed columns are skipped here — they were only computed for
|
|
232
|
+
// the newly added subset and do not have values aligned with matched incoming rows.
|
|
233
|
+
for (const {campRow, incRow} of matched) {
|
|
234
|
+
for (const s of sources) {
|
|
235
|
+
if (s.isComputed) continue;
|
|
236
|
+
// Don't overwrite the join-key column on matched rows: it already matches by definition.
|
|
237
|
+
if (cfg.mode === 'smiles' && s.target === campMolColName) continue;
|
|
238
|
+
if (cfg.mode === 'vid' && s.target === ViDColName) continue;
|
|
239
|
+
|
|
240
|
+
const targetCol = campDf.col(s.target);
|
|
241
|
+
if (!targetCol) continue;
|
|
242
|
+
const fillEmpty = isCellEmpty(targetCol, campRow);
|
|
243
|
+
if (newlyAdded.has(s.target) || fillEmpty || cfg.clashStrategy === 'overwrite')
|
|
244
|
+
setCorrectedValue(targetCol, s.col, campRow, incRow);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Add unmatched rows.
|
|
249
|
+
const newRowStartIdx = campDf.rowCount;
|
|
250
|
+
if (unmatched.length > 0) {
|
|
251
|
+
const hasStages = (app.stages?.length ?? 0) > 0 && campDf.columns.contains(TileCategoriesColName);
|
|
252
|
+
for (let u = 0; u < unmatched.length; u++) {
|
|
253
|
+
const incRow = unmatched[u];
|
|
254
|
+
campDf.rows.addNew(null, true);
|
|
255
|
+
const newCampRow = campDf.rowCount - 1;
|
|
256
|
+
for (const s of sources) {
|
|
257
|
+
// Computed columns are aligned with the unmatched array (index `u`); incoming
|
|
258
|
+
// columns stay aligned with the original incoming row index.
|
|
259
|
+
const fromIdx = s.isComputed ? u : incRow;
|
|
260
|
+
setCorrectedValue(campDf.col(s.target)!, s.col, newCampRow, fromIdx);
|
|
261
|
+
}
|
|
262
|
+
if (hasStages) {
|
|
263
|
+
const stageCol = campDf.col(TileCategoriesColName)!;
|
|
264
|
+
if (isCellEmpty(stageCol, newCampRow))
|
|
265
|
+
stageCol.set(newCampRow, app.stages[0], false);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Re-register new molecules so they get fresh, dictionary-backed V-iDs.
|
|
270
|
+
// This deliberately overwrites any V-iD value carried over from the incoming table
|
|
271
|
+
// (incoming V-iDs are not authoritative for the campaign).
|
|
272
|
+
if (cfg.molColName) {
|
|
273
|
+
const incMolCol = incomingDf.col(cfg.molColName);
|
|
274
|
+
const campVidCol = campDf.col(ViDColName);
|
|
275
|
+
if (incMolCol && campVidCol && app.campaignId) {
|
|
276
|
+
const newMols = unmatched.map((i) => incMolCol.get(i)).filter((m) => !!m && m !== '');
|
|
277
|
+
if (newMols.length > 0) {
|
|
278
|
+
try {
|
|
279
|
+
const vidMap = await registerMolsBatch(newMols, app.campaignId, app.appName);
|
|
280
|
+
for (let j = 0; j < unmatched.length; j++) {
|
|
281
|
+
const mol = incMolCol.get(unmatched[j]);
|
|
282
|
+
const vid = mol ? vidMap.get(mol) : undefined;
|
|
283
|
+
if (vid) campVidCol.set(newRowStartIdx + j, vid, false);
|
|
284
|
+
}
|
|
285
|
+
} catch (e) {
|
|
286
|
+
_package.logger.error(e);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
} finally {
|
|
293
|
+
setTimeout(() => {app.isJoining = false;}, 500);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
campDf.fireValuesChanged();
|
|
297
|
+
}
|