-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathMolecule.ts
507 lines (429 loc) · 15.9 KB
/
Molecule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* Copyright (c) 2016 - now David Sehnal, licensed under Apache 2.0, See LICENSE file for more info.
*/
namespace LiteMol.Core.Structure {
"use strict";
import DataTable = Utils.DataTable
export interface Position {
x: number,
y: number,
z: number
}
export interface Atom {
id: number,
name: string,
authName: string,
elementSymbol: string,
altLoc: string | null,
occupancy: number,
tempFactor: number,
residueIndex: number,
chainIndex: number,
entityIndex: number,
rowIndex: number
}
export interface Residue {
name: string,
seqNumber: number,
asymId: string,
authName: string,
authSeqNumber: number,
authAsymId: string,
insCode: string | null,
entityId: string,
isHet: number,
atomStartIndex: number,
atomEndIndex: number,
chainIndex: number,
entityIndex: number,
secondaryStructureIndex: number
}
export interface Chain {
asymId: string,
authAsymId: string,
entityId: string,
atomStartIndex: number,
atomEndIndex: number,
residueStartIndex: number,
residueEndIndex: number,
entityIndex: number,
// used by computed molecules (symmetry, assembly)
sourceChainIndex: number,
operatorIndex: number
}
export interface Entity {
entityId: string,
atomStartIndex: number,
atomEndIndex: number,
residueStartIndex: number,
residueEndIndex: number,
chainStartIndex: number,
chainEndIndex: number,
type: Entity.Type
}
export namespace Entity {
export type Type = 'polymer' | 'non-polymer' | 'water' | 'unknown'
}
export interface Bond {
atomAIndex: number,
atomBIndex: number,
type: BondType
}
export interface ModifiedResidue {
asymId: string,
seqNumber: number,
insCode: string | null,
parent: string,
details: string | null
}
export class ComponentBondInfoEntry {
map: Utils.FastMap<string, Utils.FastMap<string, BondType>> = Utils.FastMap.create<string, Utils.FastMap<string, BondType>>();
add(a: string, b: string, order: BondType, swap = true) {
let e = this.map.get(a);
if (e !== void 0) {
let f = e.get(b);
if (f === void 0) {
e.set(b, order);
}
} else {
let map = Utils.FastMap.create<string, BondType>();
map.set(b, order);
this.map.set(a, map);
}
if (swap) this.add(b, a, order, false);
}
constructor(public id: string) {
}
}
export class ComponentBondInfo {
entries: Utils.FastMap<string, ComponentBondInfoEntry> = Utils.FastMap.create<string, ComponentBondInfoEntry>();
newEntry(id: string) {
let e = new ComponentBondInfoEntry(id);
this.entries.set(id, e);
return e;
}
}
/**
* Identifier for a reside that is a part of the polymer.
*/
export class PolyResidueIdentifier {
constructor(public asymId: string, public seqNumber: number, public insCode: string | null) { }
static areEqual(a: PolyResidueIdentifier, index: number, bAsymId: string[], bSeqNumber: number[], bInsCode: string[]) {
return a.asymId === bAsymId[index]
&& a.seqNumber === bSeqNumber[index]
&& a.insCode === bInsCode[index];
}
static compare(a: PolyResidueIdentifier, b: PolyResidueIdentifier) {
if (a.asymId === b.asymId) {
if (a.seqNumber === b.seqNumber) {
if (a.insCode === b.insCode) return 0;
if (a.insCode === void 0) return -1;
if (b.insCode === void 0) return 1;
return a.insCode! < b.insCode! ? -1 : 1;
}
return a.seqNumber < b.seqNumber ? -1 : 1;
}
return a.asymId < b.asymId ? -1 : 1;
}
static compareResidue(a: PolyResidueIdentifier, index: number, bAsymId: string[], bSeqNumber: number[], bInsCode: string[]) {
if (a.asymId === bAsymId[index]) {
if (a.seqNumber === bSeqNumber[index]) {
if (a.insCode === bInsCode[index]) return 0;
if (a.insCode === void 0) return -1;
if (bInsCode[index] === void 0) return 1;
return a.insCode! < bInsCode[index] ? -1 : 1;
}
return a.seqNumber < bSeqNumber[index] ? -1 : 1;
}
return a.asymId < bAsymId[index] ? -1 : 1;
}
}
export const enum SecondaryStructureType { None = 0, Helix = 1, Turn = 2, Sheet = 3, AminoSeq = 4, Strand = 5 }
export class SecondaryStructureElement {
startResidueIndex: number = -1;
endResidueIndex: number = -1;
get length() {
return this.endResidueIndex - this.startResidueIndex;
}
constructor(
public type: SecondaryStructureType,
public startResidueId: PolyResidueIdentifier,
public endResidueId: PolyResidueIdentifier,
public info: any = {}) {
}
}
export class SymmetryInfo {
constructor(
public spacegroupName: string,
public cellSize: number[],
public cellAngles: number[],
public toFracTransform: number[],
public isNonStandardCrytalFrame: boolean) {
}
}
/**
* Wraps _struct_conn mmCIF category.
*/
export class StructConn {
private _residuePairIndex: Utils.FastMap<string, StructConn.Entry[]> | undefined = void 0;
private _atomIndex: Utils.FastMap<number, StructConn.Entry[]> | undefined = void 0;
private static _resKey(rA: number, rB: number) {
if (rA < rB) return `${rA}-${rB}`;
return `${rB}-${rA}`;
}
private getResiduePairIndex() {
if (this._residuePairIndex) return this._residuePairIndex;
this._residuePairIndex = Utils.FastMap.create();
for (const e of this.entries) {
const ps = e.partners;
const l = ps.length;
for (let i = 0; i < l - 1; i++) {
for (let j = i + i; j < l; j++) {
const key = StructConn._resKey(ps[i].residueIndex, ps[j].residueIndex);
if (this._residuePairIndex.has(key)) {
this._residuePairIndex.get(key)!.push(e);
} else {
this._residuePairIndex.set(key, [e]);
}
}
}
}
return this._residuePairIndex;
}
private getAtomIndex() {
if (this._atomIndex) return this._atomIndex;
this._atomIndex = Utils.FastMap.create();
for (const e of this.entries) {
for (const p of e.partners) {
const key = p.atomIndex;
if (this._atomIndex.has(key)) {
this._atomIndex.get(key)!.push(e);
} else {
this._atomIndex.set(key, [e]);
}
}
}
return this._atomIndex;
}
private static _emptyEntry = [];
getResidueEntries(residueAIndex: number, residueBIndex: number): ReadonlyArray<StructConn.Entry> {
return this.getResiduePairIndex().get(StructConn._resKey(residueAIndex, residueBIndex)) || StructConn._emptyEntry;
}
getAtomEntries(atomIndex: number): ReadonlyArray<StructConn.Entry> {
return this.getAtomIndex().get(atomIndex) || StructConn._emptyEntry;
}
constructor(public entries: StructConn.Entry[]) {
}
}
export namespace StructConn {
export interface Entry {
distance: number,
bondType: BondType,
partners: { residueIndex: number, atomIndex: number, symmetry: string }[]
}
}
/**
* Wraps an assembly operator.
*/
export class AssemblyOperator { constructor(public id: string, public name: string, public operator: number[]) { } }
/**
* Wraps a single assembly gen entry.
*/
export class AssemblyGenEntry {
constructor(public operators: string[][], public asymIds: string[]) { }
}
/**
* Wraps an assembly generation template.
*/
export class AssemblyGen {
gens: AssemblyGenEntry[] = [];
constructor(public name: string) { }
}
/**
* Information about the assemblies.
*/
export class AssemblyInfo {
constructor(public operators: { [id: string]: AssemblyOperator }, public assemblies: AssemblyGen[]) {
}
}
export type PositionTable = DataTable<Position>
export type AtomTable = DataTable<Atom>
export type ResidueTable = DataTable<Residue>
export type ChainTable = DataTable<Chain>
export type EntityTable = DataTable<Entity>
export type BondTable = DataTable<Bond>
export type ModifiedResidueTable = DataTable<ModifiedResidue>
/**
* Default Builders
*/
export namespace Tables {
const int32 = DataTable.typedColumn(Int32Array);
const float32 = DataTable.typedColumn(Float32Array);
const str = DataTable.stringColumn;
const nullStr = DataTable.stringNullColumn;
export const Positions: DataTable.Definition<Position> = {
x: float32,
y: float32,
z: float32
};
export const Atoms: DataTable.Definition<Atom> = {
id: int32,
altLoc: str,
residueIndex: int32,
chainIndex: int32,
entityIndex: int32,
name: str,
elementSymbol: str,
occupancy: float32,
tempFactor: float32,
authName: str,
rowIndex: int32
};
export const Residues: DataTable.Definition<Residue> = {
name: str,
seqNumber: int32,
asymId: str,
authName: str,
authSeqNumber: int32,
authAsymId: str,
insCode: nullStr,
entityId: str,
isHet: DataTable.typedColumn(Int8Array),
atomStartIndex: int32,
atomEndIndex: int32,
chainIndex: int32,
entityIndex: int32,
secondaryStructureIndex: int32
};
export const Chains: DataTable.Definition<Chain> = {
asymId: str,
entityId: str,
authAsymId: str,
atomStartIndex: int32,
atomEndIndex: int32,
residueStartIndex: int32,
residueEndIndex: int32,
entityIndex: int32,
sourceChainIndex: void 0,
operatorIndex: void 0
};
export const Entities: DataTable.Definition<Entity> = {
entityId: str,
type: DataTable.customColumn<Entity.Type>(),
atomStartIndex: int32,
atomEndIndex: int32,
residueStartIndex: int32,
residueEndIndex: int32,
chainStartIndex: int32,
chainEndIndex: int32
};
export const Bonds: DataTable.Definition<Bond> = {
atomAIndex: int32,
atomBIndex: int32,
type: DataTable.typedColumn(Int8Array)
};
export const ModifiedResidues: DataTable.Definition<ModifiedResidue> = {
asymId: str,
seqNumber: int32,
insCode: nullStr,
parent: str,
details: nullStr
};
}
export class Operator {
apply(v: Geometry.LinearAlgebra.Vector3) {
Geometry.LinearAlgebra.Vector3.transformMat4(v, v, this.matrix)
}
static applyToModelUnsafe(matrix: number[], m: Molecule.Model) {
let v = Geometry.LinearAlgebra.Vector3.zero();
let {x, y, z} = m.positions;
for (let i = 0, _b = m.positions.count; i < _b; i++) {
v[0] = x[i]; v[1] = y[i]; v[2] = z[i];
Geometry.LinearAlgebra.Vector3.transformMat4(v, v, matrix);
x[i] = v[0]; y[i] = v[1]; z[i] = v[2];
}
}
constructor(public matrix: number[], public id: string, public isIdentity: boolean) {
}
}
export interface Molecule {
readonly properties: Molecule.Properties,
readonly id: string,
readonly models: Molecule.Model[]
}
export namespace Molecule {
export function create(id: string, models: Model[], properties: Properties = {}): Molecule {
return { id, models, properties };
}
export interface Properties {
experimentMethods?: string[]
}
export interface Bonds {
readonly structConn?: StructConn,
readonly input?: BondTable,
readonly component?: ComponentBondInfo
}
export interface Model extends Model.Base {
readonly queryContext: Query.Context
}
export namespace Model {
export function create(model: Base): Model {
let ret = Utils.extend({}, model);
let queryContext: Query.Context | undefined = void 0
Object.defineProperty(ret, 'queryContext', { enumerable: true, configurable: false, get: function() {
if (queryContext) return queryContext;
queryContext = Query.Context.ofStructure(ret as Model);
return queryContext;
}});
return ret as Model;
}
export enum Source {
File,
Computed
}
export interface Base {
readonly id: string,
readonly modelId: string,
readonly positions: PositionTable,
readonly data: Data,
readonly source: Source,
readonly parent?: Model,
readonly operators?: Operator[],
}
export interface Data {
readonly atoms: AtomTable,
readonly residues: ResidueTable,
readonly chains: ChainTable,
readonly entities: EntityTable,
readonly bonds: Bonds,
readonly secondaryStructure: SecondaryStructureElement[],
readonly modifiedResidues?: ModifiedResidueTable,
readonly symmetryInfo?: SymmetryInfo,
readonly assemblyInfo?: AssemblyInfo
}
export function withTransformedXYZ<T>(
model: Model, ctx: T,
transform: (ctx: T, x: number, y: number, z: number, out: Geometry.LinearAlgebra.Vector3) => void) {
const {x,y,z} = model.positions;
const tAtoms = model.positions.getBuilder(model.positions.count).seal();
const {x:tX, y:tY, z:tZ} = tAtoms;
const t = Geometry.LinearAlgebra.Vector3.zero();
for (let i = 0, _l = model.positions.count; i < _l; i++) {
transform(ctx, x[i], y[i], z[i], t);
tX[i] = t[0];
tY[i] = t[1];
tZ[i] = t[2];
}
return create({
id: model.id,
modelId: model.modelId,
data: model.data,
positions: tAtoms,
parent: model.parent,
source: model.source,
operators: model.operators
});
}
}
}
}