@gcszhn/mol4agent-core 0.2.0 → 0.4.0
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/README.md +200 -19
- package/README_zh.md +181 -16
- package/lib/color.d.ts +33 -0
- package/lib/color.js +288 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/inspect-mol.d.ts +28 -0
- package/lib/inspect-mol.js +103 -0
- package/lib/inspect-plugin.d.ts +22 -0
- package/lib/inspect-plugin.js +339 -0
- package/lib/inspect.d.ts +88 -0
- package/lib/inspect.js +189 -0
- package/lib/protocol.d.ts +283 -16
- package/lib/protocol.js +36 -0
- package/lib/represent.d.ts +25 -0
- package/lib/represent.js +191 -0
- package/lib/select.d.ts +12 -0
- package/lib/select.js +41 -1
- package/lib/viewer.d.ts +41 -4
- package/lib/viewer.js +502 -181
- package/package.json +4 -2
package/lib/color.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour parsing for the harness-neutral protocol.
|
|
3
|
+
*
|
|
4
|
+
* Mol*'s uniform theme and canvas renderer take a 24-bit `0xRRGGBB` integer.
|
|
5
|
+
* Agents naturally write CSS colours instead (`"red"`, `"#ff0000"`,
|
|
6
|
+
* `"rgb(255, 0, 0)"`), so normalize every accepted form here. The parser is
|
|
7
|
+
* pure and Node-safe: core tests and adapter tool handlers share it without
|
|
8
|
+
* pulling in Mol* or a DOM.
|
|
9
|
+
*
|
|
10
|
+
* @module @gcszhn/mol4agent-core/color
|
|
11
|
+
*/
|
|
12
|
+
/** Syntax accepted by {@link parseColor}; also used in model-facing errors. */
|
|
13
|
+
export declare const COLOR_SYNTAX: string;
|
|
14
|
+
/**
|
|
15
|
+
* Parse one agent-supplied colour into a 24-bit `0xRRGGBB` integer.
|
|
16
|
+
*
|
|
17
|
+
* Alpha is accepted syntactically (`#rrggbbaa`, 4-digit hex, `rgba()`) but
|
|
18
|
+
* dropped because Mol* colours and the canvas background are opaque.
|
|
19
|
+
*
|
|
20
|
+
* @throws Error naming the offending value and {@link COLOR_SYNTAX} when the
|
|
21
|
+
* value is not a recognizable colour.
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseColor(value: unknown): number;
|
|
24
|
+
/** Render a normalized colour as `#rrggbb` for tool results and logs. */
|
|
25
|
+
export declare function formatColor(value: number): string;
|
|
26
|
+
/**
|
|
27
|
+
* Blend two colours the way Mol*'s marker shader does:
|
|
28
|
+
* `mix(base, overlay, strength)` per channel.
|
|
29
|
+
*
|
|
30
|
+
* Used to derive the visible colour of a selected element from the colour its
|
|
31
|
+
* representation paints and the canvas selection marker.
|
|
32
|
+
*/
|
|
33
|
+
export declare function mixColor(from: number, to: number, strength: number): number;
|
package/lib/color.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour parsing for the harness-neutral protocol.
|
|
3
|
+
*
|
|
4
|
+
* Mol*'s uniform theme and canvas renderer take a 24-bit `0xRRGGBB` integer.
|
|
5
|
+
* Agents naturally write CSS colours instead (`"red"`, `"#ff0000"`,
|
|
6
|
+
* `"rgb(255, 0, 0)"`), so normalize every accepted form here. The parser is
|
|
7
|
+
* pure and Node-safe: core tests and adapter tool handlers share it without
|
|
8
|
+
* pulling in Mol* or a DOM.
|
|
9
|
+
*
|
|
10
|
+
* @module @gcszhn/mol4agent-core/color
|
|
11
|
+
*/
|
|
12
|
+
/** Syntax accepted by {@link parseColor}; also used in model-facing errors. */
|
|
13
|
+
export const COLOR_SYNTAX = 'a CSS color name (red, lightgrey), #rgb, #rrggbb, #rrggbbaa, 0xRRGGBB, '
|
|
14
|
+
+ 'a bare hex value (B0B0B0), rgb()/rgba(), or a 24-bit integer (16711680)';
|
|
15
|
+
/**
|
|
16
|
+
* Parse one agent-supplied colour into a 24-bit `0xRRGGBB` integer.
|
|
17
|
+
*
|
|
18
|
+
* Alpha is accepted syntactically (`#rrggbbaa`, 4-digit hex, `rgba()`) but
|
|
19
|
+
* dropped because Mol* colours and the canvas background are opaque.
|
|
20
|
+
*
|
|
21
|
+
* @throws Error naming the offending value and {@link COLOR_SYNTAX} when the
|
|
22
|
+
* value is not a recognizable colour.
|
|
23
|
+
*/
|
|
24
|
+
export function parseColor(value) {
|
|
25
|
+
if (typeof value === 'number')
|
|
26
|
+
return parseNumericColor(value);
|
|
27
|
+
if (typeof value !== 'string')
|
|
28
|
+
throw invalidColor(value);
|
|
29
|
+
const text = value.trim().toLowerCase();
|
|
30
|
+
if (text.length === 0)
|
|
31
|
+
throw invalidColor(value);
|
|
32
|
+
const named = CSS_COLOR_NAMES[text];
|
|
33
|
+
if (named !== undefined)
|
|
34
|
+
return named;
|
|
35
|
+
if (text.startsWith('#'))
|
|
36
|
+
return parseHexDigits(text.slice(1), value);
|
|
37
|
+
if (text.startsWith('0x'))
|
|
38
|
+
return parseHexDigits(text.slice(2), value);
|
|
39
|
+
if (text.startsWith('rgb'))
|
|
40
|
+
return parseRgbFunction(text, value);
|
|
41
|
+
if ((text.length === 3 || text.length === 6 || text.length === 8) && /^[0-9a-f]+$/u.test(text)) {
|
|
42
|
+
return parseHexDigits(text, value);
|
|
43
|
+
}
|
|
44
|
+
throw invalidColor(value);
|
|
45
|
+
}
|
|
46
|
+
/** Render a normalized colour as `#rrggbb` for tool results and logs. */
|
|
47
|
+
export function formatColor(value) {
|
|
48
|
+
return `#${(value & 0xffffff).toString(16).padStart(6, '0')}`;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Blend two colours the way Mol*'s marker shader does:
|
|
52
|
+
* `mix(base, overlay, strength)` per channel.
|
|
53
|
+
*
|
|
54
|
+
* Used to derive the visible colour of a selected element from the colour its
|
|
55
|
+
* representation paints and the canvas selection marker.
|
|
56
|
+
*/
|
|
57
|
+
export function mixColor(from, to, strength) {
|
|
58
|
+
const weight = Math.max(0, Math.min(1, strength));
|
|
59
|
+
const blend = (shift) => {
|
|
60
|
+
const base = (from >> shift) & 0xff;
|
|
61
|
+
const overlay = (to >> shift) & 0xff;
|
|
62
|
+
return Math.round(base * (1 - weight) + overlay * weight);
|
|
63
|
+
};
|
|
64
|
+
return (blend(16) << 16) | (blend(8) << 8) | blend(0);
|
|
65
|
+
}
|
|
66
|
+
function parseNumericColor(value) {
|
|
67
|
+
if (!Number.isFinite(value) || !Number.isInteger(value) || value < 0)
|
|
68
|
+
throw invalidColor(value);
|
|
69
|
+
return value & 0xffffff;
|
|
70
|
+
}
|
|
71
|
+
function parseHexDigits(digits, original) {
|
|
72
|
+
if (!/^[0-9a-f]+$/u.test(digits))
|
|
73
|
+
throw invalidColor(original);
|
|
74
|
+
switch (digits.length) {
|
|
75
|
+
case 3: {
|
|
76
|
+
const [r, g, b] = digits;
|
|
77
|
+
return Number.parseInt(`${r}${r}${g}${g}${b}${b}`, 16);
|
|
78
|
+
}
|
|
79
|
+
case 4: {
|
|
80
|
+
// RGBA shorthand; the alpha nibble is ignored.
|
|
81
|
+
const [r, g, b] = digits;
|
|
82
|
+
return Number.parseInt(`${r}${r}${g}${g}${b}${b}`, 16);
|
|
83
|
+
}
|
|
84
|
+
case 6:
|
|
85
|
+
return Number.parseInt(digits, 16);
|
|
86
|
+
case 8:
|
|
87
|
+
// RRGGBBAA; Mol* colours have no alpha channel.
|
|
88
|
+
return Number.parseInt(digits.slice(0, 6), 16);
|
|
89
|
+
default:
|
|
90
|
+
throw invalidColor(original);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function parseRgbFunction(text, original) {
|
|
94
|
+
const match = /^rgba?\(([^)]*)\)$/u.exec(text);
|
|
95
|
+
if (match === null)
|
|
96
|
+
throw invalidColor(original);
|
|
97
|
+
const body = match[1].trim();
|
|
98
|
+
if (body.length === 0)
|
|
99
|
+
throw invalidColor(original);
|
|
100
|
+
const slash = body.split('/');
|
|
101
|
+
if (slash.length > 2)
|
|
102
|
+
throw invalidColor(original);
|
|
103
|
+
const channels = slash[0].trim().split(/[,\s]+/u).filter(Boolean);
|
|
104
|
+
if (channels.length !== 3 && channels.length !== 4)
|
|
105
|
+
throw invalidColor(original);
|
|
106
|
+
if (slash.length === 2 && channels.length !== 3)
|
|
107
|
+
throw invalidColor(original);
|
|
108
|
+
const [r, g, b] = channels.slice(0, 3).map(channel => parseRgbChannel(channel, original));
|
|
109
|
+
if (channels.length === 4)
|
|
110
|
+
parseAlpha(channels[3], original);
|
|
111
|
+
if (slash.length === 2)
|
|
112
|
+
parseAlpha(slash[1].trim(), original);
|
|
113
|
+
return (r << 16) | (g << 8) | b;
|
|
114
|
+
}
|
|
115
|
+
const NUMBER_PATTERN = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/u;
|
|
116
|
+
function parseRgbChannel(channel, original) {
|
|
117
|
+
const percent = channel.endsWith('%');
|
|
118
|
+
const value = parseNumber(percent ? channel.slice(0, -1) : channel);
|
|
119
|
+
if (value === undefined)
|
|
120
|
+
throw invalidColor(original);
|
|
121
|
+
const channelValue = percent ? (value / 100) * 255 : value;
|
|
122
|
+
return Math.max(0, Math.min(255, Math.round(channelValue)));
|
|
123
|
+
}
|
|
124
|
+
function parseAlpha(alpha, original) {
|
|
125
|
+
const percent = alpha.endsWith('%');
|
|
126
|
+
if (parseNumber(percent ? alpha.slice(0, -1) : alpha) === undefined)
|
|
127
|
+
throw invalidColor(original);
|
|
128
|
+
}
|
|
129
|
+
function parseNumber(text) {
|
|
130
|
+
if (!NUMBER_PATTERN.test(text))
|
|
131
|
+
return undefined;
|
|
132
|
+
return Number(text);
|
|
133
|
+
}
|
|
134
|
+
function invalidColor(value) {
|
|
135
|
+
const shown = typeof value === 'string' ? JSON.stringify(value) : String(value);
|
|
136
|
+
return new Error(`invalid color ${shown}; accepted: ${COLOR_SYNTAX}`);
|
|
137
|
+
}
|
|
138
|
+
/** CSS Color 4 named colours (the 148-name X11/CSS table). */
|
|
139
|
+
const CSS_COLOR_NAMES = {
|
|
140
|
+
aliceblue: 0xf0f8ff,
|
|
141
|
+
antiquewhite: 0xfaebd7,
|
|
142
|
+
aqua: 0x00ffff,
|
|
143
|
+
aquamarine: 0x7fffd4,
|
|
144
|
+
azure: 0xf0ffff,
|
|
145
|
+
beige: 0xf5f5dc,
|
|
146
|
+
bisque: 0xffe4c4,
|
|
147
|
+
black: 0x000000,
|
|
148
|
+
blanchedalmond: 0xffebcd,
|
|
149
|
+
blue: 0x0000ff,
|
|
150
|
+
blueviolet: 0x8a2be2,
|
|
151
|
+
brown: 0xa52a2a,
|
|
152
|
+
burlywood: 0xdeb887,
|
|
153
|
+
cadetblue: 0x5f9ea0,
|
|
154
|
+
chartreuse: 0x7fff00,
|
|
155
|
+
chocolate: 0xd2691e,
|
|
156
|
+
coral: 0xff7f50,
|
|
157
|
+
cornflowerblue: 0x6495ed,
|
|
158
|
+
cornsilk: 0xfff8dc,
|
|
159
|
+
crimson: 0xdc143c,
|
|
160
|
+
cyan: 0x00ffff,
|
|
161
|
+
darkblue: 0x00008b,
|
|
162
|
+
darkcyan: 0x008b8b,
|
|
163
|
+
darkgoldenrod: 0xb8860b,
|
|
164
|
+
darkgray: 0xa9a9a9,
|
|
165
|
+
darkgreen: 0x006400,
|
|
166
|
+
darkgrey: 0xa9a9a9,
|
|
167
|
+
darkkhaki: 0xbdb76b,
|
|
168
|
+
darkmagenta: 0x8b008b,
|
|
169
|
+
darkolivegreen: 0x556b2f,
|
|
170
|
+
darkorange: 0xff8c00,
|
|
171
|
+
darkorchid: 0x9932cc,
|
|
172
|
+
darkred: 0x8b0000,
|
|
173
|
+
darksalmon: 0xe9967a,
|
|
174
|
+
darkseagreen: 0x8fbc8f,
|
|
175
|
+
darkslateblue: 0x483d8b,
|
|
176
|
+
darkslategray: 0x2f4f4f,
|
|
177
|
+
darkslategrey: 0x2f4f4f,
|
|
178
|
+
darkturquoise: 0x00ced1,
|
|
179
|
+
darkviolet: 0x9400d3,
|
|
180
|
+
deeppink: 0xff1493,
|
|
181
|
+
deepskyblue: 0x00bfff,
|
|
182
|
+
dimgray: 0x696969,
|
|
183
|
+
dimgrey: 0x696969,
|
|
184
|
+
dodgerblue: 0x1e90ff,
|
|
185
|
+
firebrick: 0xb22222,
|
|
186
|
+
floralwhite: 0xfffaf0,
|
|
187
|
+
forestgreen: 0x228b22,
|
|
188
|
+
fuchsia: 0xff00ff,
|
|
189
|
+
gainsboro: 0xdcdcdc,
|
|
190
|
+
ghostwhite: 0xf8f8ff,
|
|
191
|
+
gold: 0xffd700,
|
|
192
|
+
goldenrod: 0xdaa520,
|
|
193
|
+
gray: 0x808080,
|
|
194
|
+
green: 0x008000,
|
|
195
|
+
greenyellow: 0xadff2f,
|
|
196
|
+
grey: 0x808080,
|
|
197
|
+
honeydew: 0xf0fff0,
|
|
198
|
+
hotpink: 0xff69b4,
|
|
199
|
+
indianred: 0xcd5c5c,
|
|
200
|
+
indigo: 0x4b0082,
|
|
201
|
+
ivory: 0xfffff0,
|
|
202
|
+
khaki: 0xf0e68c,
|
|
203
|
+
lavender: 0xe6e6fa,
|
|
204
|
+
lavenderblush: 0xfff0f5,
|
|
205
|
+
lawngreen: 0x7cfc00,
|
|
206
|
+
lemonchiffon: 0xfffacd,
|
|
207
|
+
lightblue: 0xadd8e6,
|
|
208
|
+
lightcoral: 0xf08080,
|
|
209
|
+
lightcyan: 0xe0ffff,
|
|
210
|
+
lightgoldenrodyellow: 0xfafad2,
|
|
211
|
+
lightgray: 0xd3d3d3,
|
|
212
|
+
lightgreen: 0x90ee90,
|
|
213
|
+
lightgrey: 0xd3d3d3,
|
|
214
|
+
lightpink: 0xffb6c1,
|
|
215
|
+
lightsalmon: 0xffa07a,
|
|
216
|
+
lightseagreen: 0x20b2aa,
|
|
217
|
+
lightskyblue: 0x87cefa,
|
|
218
|
+
lightslategray: 0x778899,
|
|
219
|
+
lightslategrey: 0x778899,
|
|
220
|
+
lightsteelblue: 0xb0c4de,
|
|
221
|
+
lightyellow: 0xffffe0,
|
|
222
|
+
lime: 0x00ff00,
|
|
223
|
+
limegreen: 0x32cd32,
|
|
224
|
+
linen: 0xfaf0e6,
|
|
225
|
+
magenta: 0xff00ff,
|
|
226
|
+
maroon: 0x800000,
|
|
227
|
+
mediumaquamarine: 0x66cdaa,
|
|
228
|
+
mediumblue: 0x0000cd,
|
|
229
|
+
mediumorchid: 0xba55d3,
|
|
230
|
+
mediumpurple: 0x9370db,
|
|
231
|
+
mediumseagreen: 0x3cb371,
|
|
232
|
+
mediumslateblue: 0x7b68ee,
|
|
233
|
+
mediumspringgreen: 0x00fa9a,
|
|
234
|
+
mediumturquoise: 0x48d1cc,
|
|
235
|
+
mediumvioletred: 0xc71585,
|
|
236
|
+
midnightblue: 0x191970,
|
|
237
|
+
mintcream: 0xf5fffa,
|
|
238
|
+
mistyrose: 0xffe4e1,
|
|
239
|
+
moccasin: 0xffe4b5,
|
|
240
|
+
navajowhite: 0xffdead,
|
|
241
|
+
navy: 0x000080,
|
|
242
|
+
oldlace: 0xfdf5e6,
|
|
243
|
+
olive: 0x808000,
|
|
244
|
+
olivedrab: 0x6b8e23,
|
|
245
|
+
orange: 0xffa500,
|
|
246
|
+
orangered: 0xff4500,
|
|
247
|
+
orchid: 0xda70d6,
|
|
248
|
+
palegoldenrod: 0xeee8aa,
|
|
249
|
+
palegreen: 0x98fb98,
|
|
250
|
+
paleturquoise: 0xafeeee,
|
|
251
|
+
palevioletred: 0xdb7093,
|
|
252
|
+
papayawhip: 0xffefd5,
|
|
253
|
+
peachpuff: 0xffdab9,
|
|
254
|
+
peru: 0xcd853f,
|
|
255
|
+
pink: 0xffc0cb,
|
|
256
|
+
plum: 0xdda0dd,
|
|
257
|
+
powderblue: 0xb0e0e6,
|
|
258
|
+
purple: 0x800080,
|
|
259
|
+
rebeccapurple: 0x663399,
|
|
260
|
+
red: 0xff0000,
|
|
261
|
+
rosybrown: 0xbc8f8f,
|
|
262
|
+
royalblue: 0x4169e1,
|
|
263
|
+
saddlebrown: 0x8b4513,
|
|
264
|
+
salmon: 0xfa8072,
|
|
265
|
+
sandybrown: 0xf4a460,
|
|
266
|
+
seagreen: 0x2e8b57,
|
|
267
|
+
seashell: 0xfff5ee,
|
|
268
|
+
sienna: 0xa0522d,
|
|
269
|
+
silver: 0xc0c0c0,
|
|
270
|
+
skyblue: 0x87ceeb,
|
|
271
|
+
slateblue: 0x6a5acd,
|
|
272
|
+
slategray: 0x708090,
|
|
273
|
+
slategrey: 0x708090,
|
|
274
|
+
snow: 0xfffafa,
|
|
275
|
+
springgreen: 0x00ff7f,
|
|
276
|
+
steelblue: 0x4682b4,
|
|
277
|
+
tan: 0xd2b48c,
|
|
278
|
+
teal: 0x008080,
|
|
279
|
+
thistle: 0xd8bfd8,
|
|
280
|
+
tomato: 0xff6347,
|
|
281
|
+
turquoise: 0x40e0d0,
|
|
282
|
+
violet: 0xee82ee,
|
|
283
|
+
wheat: 0xf5deb3,
|
|
284
|
+
white: 0xffffff,
|
|
285
|
+
whitesmoke: 0xf5f5f5,
|
|
286
|
+
yellow: 0xffff00,
|
|
287
|
+
yellowgreen: 0x9acd32,
|
|
288
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
export * from './protocol.ts';
|
|
10
10
|
export * from './formats.ts';
|
|
11
11
|
export * from './sources.ts';
|
|
12
|
+
export * from './inspect.ts';
|
|
12
13
|
export { summarizeStructure } from './parse/index.ts';
|
|
13
14
|
export { createMolViewer } from './viewer.ts';
|
|
14
15
|
export type { MolViewer, MolViewerOptions } from './viewer.ts';
|
|
15
|
-
export { compileSelector, selectorSchema } from './select.ts';
|
|
16
|
+
export { compileSelector, describeSelector, selectorExpression, selectorSchema } from './select.ts';
|
|
16
17
|
export type { CompiledSelector } from './select.ts';
|
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
export * from "./protocol.js";
|
|
10
10
|
export * from "./formats.js";
|
|
11
11
|
export * from "./sources.js";
|
|
12
|
+
export * from "./inspect.js";
|
|
12
13
|
export { summarizeStructure } from "./parse/index.js";
|
|
13
14
|
export { createMolViewer } from "./viewer.js";
|
|
14
|
-
export { compileSelector, selectorSchema } from "./select.js";
|
|
15
|
+
export { compileSelector, describeSelector, selectorExpression, selectorSchema } from "./select.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn Mol* structure locations into plain residue records.
|
|
3
|
+
*
|
|
4
|
+
* Only Mol*'s model layer is imported here: no plugin, DOM, or viewer state, so
|
|
5
|
+
* the extraction can be exercised against a real fixture outside a browser.
|
|
6
|
+
* Placement uses `Element.Location.position`, which applies the unit
|
|
7
|
+
* conformation, so coordinates are where Mol* actually draws the atom rather
|
|
8
|
+
* than the raw model row.
|
|
9
|
+
*
|
|
10
|
+
* @module @gcszhn/mol4agent-core/inspect-mol
|
|
11
|
+
*/
|
|
12
|
+
import { StructureElement } from 'molstar/lib/mol-model/structure';
|
|
13
|
+
import { type ResidueRecord } from './inspect.ts';
|
|
14
|
+
/** Residues plus totals extracted from one target's locations. */
|
|
15
|
+
export interface ExtractedStructure {
|
|
16
|
+
readonly records: readonly ResidueRecord[];
|
|
17
|
+
/** Distinct chain ids, in first-seen order. */
|
|
18
|
+
readonly chains: readonly string[];
|
|
19
|
+
readonly models: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Collect residues from every location of `lociList`.
|
|
23
|
+
*
|
|
24
|
+
* `lociList` may span several structures (one viewer can hold more than one);
|
|
25
|
+
* residue identity is therefore keyed per structure instance as well as per
|
|
26
|
+
* unit and residue, so two structures never merge into one row.
|
|
27
|
+
*/
|
|
28
|
+
export declare function extractResidues(lociList: readonly StructureElement.Loci[]): ExtractedStructure;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn Mol* structure locations into plain residue records.
|
|
3
|
+
*
|
|
4
|
+
* Only Mol*'s model layer is imported here: no plugin, DOM, or viewer state, so
|
|
5
|
+
* the extraction can be exercised against a real fixture outside a browser.
|
|
6
|
+
* Placement uses `Element.Location.position`, which applies the unit
|
|
7
|
+
* conformation, so coordinates are where Mol* actually draws the atom rather
|
|
8
|
+
* than the raw model row.
|
|
9
|
+
*
|
|
10
|
+
* @module @gcszhn/mol4agent-core/inspect-mol
|
|
11
|
+
*/
|
|
12
|
+
import { Structure, StructureElement, StructureProperties, Unit } from 'molstar/lib/mol-model/structure';
|
|
13
|
+
import { centroid, kindOfEntity } from "./inspect.js";
|
|
14
|
+
/** Read a property that some models (e.g. small-molecule formats) cannot answer. */
|
|
15
|
+
function optional(read) {
|
|
16
|
+
try {
|
|
17
|
+
const value = read();
|
|
18
|
+
return Number.isFinite(value) ? value : undefined;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Collect residues from every location of `lociList`.
|
|
26
|
+
*
|
|
27
|
+
* `lociList` may span several structures (one viewer can hold more than one);
|
|
28
|
+
* residue identity is therefore keyed per structure instance as well as per
|
|
29
|
+
* unit and residue, so two structures never merge into one row.
|
|
30
|
+
*/
|
|
31
|
+
export function extractResidues(lociList) {
|
|
32
|
+
const residues = new Map();
|
|
33
|
+
const chains = new Set();
|
|
34
|
+
const models = new Set();
|
|
35
|
+
const structureIds = new Map();
|
|
36
|
+
const position = [0, 0, 0];
|
|
37
|
+
const location = StructureElement.Location.create();
|
|
38
|
+
const structureId = (structure) => {
|
|
39
|
+
const existing = structureIds.get(structure);
|
|
40
|
+
if (existing !== undefined)
|
|
41
|
+
return existing;
|
|
42
|
+
const id = structureIds.size;
|
|
43
|
+
structureIds.set(structure, id);
|
|
44
|
+
return id;
|
|
45
|
+
};
|
|
46
|
+
for (const loci of lociList) {
|
|
47
|
+
if (StructureElement.Loci.isEmpty(loci))
|
|
48
|
+
continue;
|
|
49
|
+
const prefix = structureId(loci.structure);
|
|
50
|
+
StructureElement.Loci.forEachLocation(loci, loc => {
|
|
51
|
+
// Coarse (sphere/gaussian) units have no atom hierarchy to report.
|
|
52
|
+
if (!Unit.isAtomic(loc.unit))
|
|
53
|
+
return;
|
|
54
|
+
const chain = StructureProperties.chain.auth_asym_id(loc);
|
|
55
|
+
const seqId = StructureProperties.residue.auth_seq_id(loc);
|
|
56
|
+
const labelSeqId = StructureProperties.residue.label_seq_id(loc);
|
|
57
|
+
const compId = StructureProperties.residue.label_comp_id(loc);
|
|
58
|
+
const key = `${prefix}:${loc.unit.id}:${StructureProperties.residue.key(loc)}`;
|
|
59
|
+
let pending = residues.get(key);
|
|
60
|
+
if (pending === undefined) {
|
|
61
|
+
pending = {
|
|
62
|
+
chain,
|
|
63
|
+
seqId,
|
|
64
|
+
labelSeqId,
|
|
65
|
+
compId,
|
|
66
|
+
kind: kindOfEntity(StructureProperties.entity.type(loc), StructureProperties.entity.subtype(loc)),
|
|
67
|
+
points: [],
|
|
68
|
+
atoms: [],
|
|
69
|
+
};
|
|
70
|
+
residues.set(key, pending);
|
|
71
|
+
chains.add(chain);
|
|
72
|
+
models.add(loc.unit.model);
|
|
73
|
+
}
|
|
74
|
+
StructureElement.Location.position(position, loc);
|
|
75
|
+
const point = { x: position[0], y: position[1], z: position[2] };
|
|
76
|
+
pending.points.push(point);
|
|
77
|
+
const occupancy = optional(() => StructureProperties.atom.occupancy(loc));
|
|
78
|
+
const bFactor = optional(() => StructureProperties.atom.B_iso_or_equiv(loc));
|
|
79
|
+
const altId = StructureProperties.atom.label_alt_id(loc);
|
|
80
|
+
pending.atoms.push({
|
|
81
|
+
name: StructureProperties.atom.label_atom_id(loc),
|
|
82
|
+
element: StructureProperties.atom.type_symbol(loc),
|
|
83
|
+
...point,
|
|
84
|
+
...(occupancy === undefined ? {} : { occupancy }),
|
|
85
|
+
...(bFactor === undefined ? {} : { bFactor }),
|
|
86
|
+
...(altId === '' || altId === undefined ? {} : { altId }),
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const records = [];
|
|
91
|
+
for (const pending of residues.values()) {
|
|
92
|
+
records.push({
|
|
93
|
+
chain: pending.chain,
|
|
94
|
+
seqId: pending.seqId,
|
|
95
|
+
labelSeqId: pending.labelSeqId,
|
|
96
|
+
compId: pending.compId,
|
|
97
|
+
kind: pending.kind,
|
|
98
|
+
center: centroid(pending.points),
|
|
99
|
+
atoms: pending.atoms,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return { records, chains: [...chains], models: models.size };
|
|
103
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Viewer-side inspection: resolve an {@link InspectRequest} against the live
|
|
3
|
+
* Mol* plugin and assemble the result.
|
|
4
|
+
*
|
|
5
|
+
* Everything here reads current state, never the session log, so a human who
|
|
6
|
+
* clicked a residue or switched a representation in the Mol* panel is visible
|
|
7
|
+
* to the agent exactly as the screen shows it.
|
|
8
|
+
*
|
|
9
|
+
* @module @gcszhn/mol4agent-core/inspect-plugin
|
|
10
|
+
*/
|
|
11
|
+
import type { PluginContext } from 'molstar/lib/mol-plugin/context';
|
|
12
|
+
import { type Structure } from 'molstar/lib/mol-model/structure';
|
|
13
|
+
import { type InspectRequest, type InspectResult } from './protocol.ts';
|
|
14
|
+
type StructuresFor = (structureId: string) => Structure[];
|
|
15
|
+
/**
|
|
16
|
+
* Inspect the loaded structure and the way the viewer draws it.
|
|
17
|
+
*
|
|
18
|
+
* Throws {@link Mol4AgentError} with `NO_STRUCTURE` when nothing is loaded; an
|
|
19
|
+
* empty selection or focus is a valid empty result instead of an error.
|
|
20
|
+
*/
|
|
21
|
+
export declare function inspectViewer(plugin: PluginContext, request?: InspectRequest, structuresFor?: StructuresFor): InspectResult;
|
|
22
|
+
export {};
|