-
Notifications
You must be signed in to change notification settings - Fork 191
/
parsers.js
2855 lines (2632 loc) · 89 KB
/
parsers.js
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @ts-nocheck
/* eslint-disable vars-on-top */
/* eslint-disable eqeqeq */
/**
* Parsers stores functions for parsing molecular data. They all take a string of molecular data
* and options. The default behavior is to only read the first model in the case of multimodel files, and
* all parsers return a list of atom list(s)
*
* Parsers.<ext> corresponds to the parsers for files with extension ext
*/
import base64ToArray from "./util/base64ToArray";
import { conversionMatrix3, Matrix3, Matrix4, Vector3 } from "./WebGL/math";
import MMTF from "./MMTF";
export default (function () {
const parsers = {};
// Covalent radii
const bondTable = {
H: 0.37,
He: 0.32,
Li: 1.34,
Be: 0.9,
B: 0.82,
C: 0.77,
N: 0.75,
O: 0.73,
F: 0.71,
Ne: 0.69,
Na: 1.54,
Mg: 1.3,
Al: 1.18,
Si: 1.11,
P: 1.06,
S: 1.02,
Cl: 0.99,
Ar: 0.97,
K: 1.96,
Ca: 1.74,
Sc: 1.44,
Ti: 1.56,
V: 1.25,
/* Cr */ Mn: 1.39,
Fe: 1.25,
Co: 1.26,
Ni: 1.21,
Cu: 1.38,
Zn: 1.31,
Ga: 1.26,
Ge: 1.22,
/* As */ Se: 1.16,
Br: 1.14,
Kr: 1.1,
Rb: 2.11,
Sr: 1.92,
Y: 1.62,
Zr: 1.48,
Nb: 1.37,
Mo: 1.45,
Tc: 1.56,
Ru: 1.26,
Rh: 1.35,
Pd: 1.31,
Ag: 1.53,
Cd: 1.48,
In: 1.44,
Sn: 1.41,
Sb: 1.38,
Te: 1.35,
I: 1.33,
Xe: 1.3,
Cs: 2.25,
Ba: 1.98,
Lu: 1.6,
Hf: 1.5,
Ta: 1.38,
W: 1.46,
Re: 1.59,
Os: 1.44,
Ir: 1.37,
Pt: 1.28,
Au: 1.44,
Hg: 1.49,
Tl: 1.48,
Pb: 1.47,
Bi: 1.46,
/* Po */ /* At */ Rn: 1.45,
// None of the bottom row or any of the Lanthanides have bond lengths
};
const anumToSymbol = {
1: 'H',
2: 'He',
3: 'Li',
4: 'Be',
5: 'B',
6: 'C',
7: 'N',
8: 'O',
9: 'F',
10: 'Ne',
11: 'Na',
12: 'Mg',
13: 'Al',
14: 'Si',
15: 'P',
16: 'S',
17: 'Cl',
18: 'Ar',
19: 'K',
20: 'Ca',
21: 'Sc',
22: 'Ti',
23: 'V',
24: 'Cr',
25: 'Mn',
26: 'Fe',
27: 'Co',
28: 'Ni',
29: 'Cu',
30: 'Zn',
31: 'Ga',
32: 'Ge',
33: 'As',
34: 'Se',
35: 'Br',
36: 'Kr',
37: 'Rb',
38: 'Sr',
39: 'Y',
40: 'Zr',
41: 'Nb',
42: 'Mo',
43: 'Tc',
44: 'Ru',
45: 'Rh',
46: 'Pd',
47: 'Ag',
48: 'Cd',
49: 'In',
50: 'Sn',
51: 'Sb',
52: 'Te',
53: 'I',
54: 'Xe',
55: 'Cs',
56: 'Ba',
71: 'Lu',
72: 'Hf',
73: 'Ta',
74: 'W',
75: 'Re',
76: 'Os',
77: 'Ir',
78: 'Pt',
79: 'Au',
80: 'Hg',
81: 'Tl',
82: 'Pb',
83: 'Bi',
84: 'Po',
85: 'At',
86: 'Rn',
87: 'Fr',
88: 'Ra',
104: 'Rf',
105: 'Db',
106: 'Sg',
107: 'Bh',
108: 'Hs',
109: 'Mt',
110: 'Ds',
111: 'Rg',
112: 'Cn',
113: 'Nh',
114: 'Fl',
115: 'Mc',
116: 'Lv',
117: 'Ts',
118: 'Og',
57: 'La',
58: 'Ce',
59: 'Pr',
60: 'Nd',
61: 'Pm',
62: 'Sm',
63: 'Eu',
64: 'Gd',
65: 'Tb',
66: 'Dy',
67: 'Ho',
68: 'Er',
69: 'Tm',
70: 'Yb',
89: 'Ac',
90: 'Th',
91: 'Pa',
92: 'U',
93: 'Np',
94: 'Pu',
95: 'Am',
96: 'Cm',
97: 'Bk',
98: 'Cf',
99: 'Es',
100: 'Fm',
101: 'Md',
102: 'No',
};
const bondLength = function (elem) {
return bondTable[elem] || 1.6;
};
// return true if atom1 and atom2 are probably bonded to each other
// based on distance alone
const areConnected = function (atom1, atom2) {
let maxsq = bondLength(atom1.elem) + bondLength(atom2.elem);
maxsq += 0.25; // fudge factor, especially important for md frames, also see 1i3d
maxsq *= maxsq;
let xdiff = atom1.x - atom2.x;
xdiff *= xdiff;
if (xdiff > maxsq) return false;
let ydiff = atom1.y - atom2.y;
ydiff *= ydiff;
if (ydiff > maxsq) return false;
let zdiff = atom1.z - atom2.z;
zdiff *= zdiff;
if (zdiff > maxsq) return false;
const distSquared = xdiff + ydiff + zdiff;
if (isNaN(distSquared)) return false;
if (distSquared < 0.5) return false; // maybe duplicate position.
if (distSquared > maxsq) return false;
if (atom1.altLoc != atom2.altLoc && atom1.altLoc != ' ' && atom2.altLoc != ' ') return false; // don't connect across alternate locations
return true;
};
/**
* @param {import("./specs").AtomSpec[]} atoms
*/
const assignBonds = function (atoms) {
// assign bonds - yuck, can't count on connect records
for (let i = 0, n = atoms.length; i < n; i++) {
// Don't reindex if atoms are already indexed
if (!atoms[i].index) atoms[i].index = i;
}
const grid = {};
const MAX_BOND_LENGTH = 4.95; // (largest bond length, Cs) 2.25 * 2 * 1.1 (fudge factor)
for (let index = 0; index < atoms.length; index++) {
const atom = atoms[index];
const x = Math.floor(atom.x || 0 / MAX_BOND_LENGTH);
const y = Math.floor(atom.y || 0 / MAX_BOND_LENGTH);
const z = Math.floor(atom.z || 0 / MAX_BOND_LENGTH);
if (!grid[x]) {
grid[x] = {};
}
if (!grid[x][y]) {
grid[x][y] = {};
}
if (!grid[x][y][z]) {
grid[x][y][z] = [];
}
grid[x][y][z].push(atom);
}
const findConnections = function (points, otherPoints) {
for (let i = 0; i < points.length; i++) {
const atom1 = points[i];
for (let j = 0; j < otherPoints.length; j++) {
const atom2 = otherPoints[j];
if (areConnected(atom1, atom2)) {
// gracefully handle one-sided bonds
const a2i = atom1.bonds.indexOf(atom2.index);
const a1i = atom2.bonds.indexOf(atom1.index);
if (a2i == -1 && a1i == -1) {
atom1.bonds.push(atom2.index);
atom1.bondOrder.push(1);
atom2.bonds.push(atom1.index);
atom2.bondOrder.push(1);
} else if (a2i == -1) {
atom1.bonds.push(atom2.index);
atom1.bondOrder.push(atom2.bondOrder[a1i]);
} else if (a1i == -1) {
atom2.bonds.push(atom1.index);
atom2.bondOrder.push(atom1.bondOrder[a2i]);
}
}
}
}
};
/* const */ const OFFSETS = [
{x: 0, y: 0, z: 1},
{x: 0, y: 1, z: -1},
{x: 0, y: 1, z: 0},
{x: 0, y: 1, z: 1},
{x: 1, y: -1, z: -1},
{x: 1, y: -1, z: 0},
{x: 1, y: -1, z: 1},
{x: 1, y: 0, z: -1},
{x: 1, y: 0, z: 0},
{x: 1, y: 0, z: 1},
{x: 1, y: 1, z: -1},
{x: 1, y: 1, z: 0},
{x: 1, y: 1, z: 1},
];
for (let x in grid) {
x = parseInt(x);
for (let y in grid[x]) {
y = parseInt(y);
for (let z in grid[x][y]) {
z = parseInt(z);
const points = grid[x][y][z];
for (let i = 0; i < points.length; i++) {
const atom1 = points[i];
for (let j = i + 1; j < points.length; j++) {
const atom2 = points[j];
if (areConnected(atom1, atom2)) {
if (atom1.bonds.indexOf(atom2.index) == -1) {
atom1.bonds.push(atom2.index);
atom1.bondOrder.push(1);
atom2.bonds.push(atom1.index);
atom2.bondOrder.push(1);
}
}
}
}
for (let o = 0; o < OFFSETS.length; o++) {
const offset = OFFSETS[o];
if (
!grid[x + offset.x] ||
!grid[x + offset.x][y + offset.y] ||
!grid[x + offset.x][y + offset.y][z + offset.z]
)
continue;
const otherPoints = grid[x + offset.x][y + offset.y][z + offset.z];
findConnections(points, otherPoints);
}
}
}
}
};
const standardResidues = new Set([
'ABU',
'ACD',
'ALA',
'ALB',
'ALI',
'ARG',
'AR0',
'ASN',
'ASP',
'ASX',
'BAS',
'CYS',
'CYH',
'CYX',
'CSS',
'CSH',
'GLN',
'GLU',
'GLX',
'GLY',
'HIS',
'HIE',
'HID',
'HIP',
'HYP',
'ILE',
'ILU',
'LEU',
'LYS',
'MET',
'PCA',
'PGA',
'PHE',
'PR0',
'PRO',
'PRZ',
'SER',
'THR',
'TYR',
'VAL',
'A',
'1MA',
'C',
'5MC',
'OMC',
'G',
'1MG',
'2MG',
'M2G',
'7MG',
'OMG',
'YG',
'I',
'T',
'U',
'+U',
'H2U',
'5MU',
'PSU',
'ACE',
'F0R',
'H2O',
'HOH',
'WAT',
]);
// this is optimized for proteins where it is assumed connected
// atoms are on the same or next residue
/**
* @param {import("./specs").AtomSpec[]}
* atomsarray
*/
const assignPDBBonds = function (atomsarray) {
// assign bonds - yuck, can't count on connect records
const protatoms = [];
const hetatoms = [];
let i;
let n;
for (i = 0, n = atomsarray.length; i < n; i++) {
const atom = atomsarray[i];
atom.index = i;
if (atom.hetflag || !standardResidues.has(atom.resn)) hetatoms.push(atom);
else protatoms.push(atom);
}
assignBonds(hetatoms);
// sort by resid
protatoms.sort((a, b) => {
if (a.chain != b.chain) return a.chain < b.chain ? -1 : 1;
return a.resi - b.resi;
});
// for identifying connected residues
let currentResi = -1;
let reschain = -1;
let lastResConnected;
for (i = 0, n = protatoms.length; i < n; i++) {
const ai = protatoms[i];
if (ai.resi !== currentResi) {
currentResi = ai.resi;
if (!lastResConnected) reschain++;
lastResConnected = false;
}
ai.reschain = reschain;
for (let j = i + 1; j < protatoms.length; j++) {
const aj = protatoms[j];
if (aj.chain != ai.chain) break;
if (aj.resi - ai.resi > 1)
// can't be connected
break;
if (areConnected(ai, aj)) {
if (ai.bonds.indexOf(aj.index) === -1) {
// only add if not already there
ai.bonds.push(aj.index);
ai.bondOrder.push(1);
aj.bonds.push(ai.index);
aj.bondOrder.push(1);
}
if (ai.resi !== aj.resi) lastResConnected = true;
}
}
}
};
// this will identify all hydrogen bonds between backbone
// atoms; assume atom names are correct, only identifies
// single closest hbond
const assignBackboneHBonds = function (atomsarray) {
const maxlength = 3.2;
const maxlengthSq = 10.24;
const atoms = [];
let i;
let j;
let n;
for (i = 0, n = atomsarray.length; i < n; i++) {
atomsarray[i].index = i;
// only consider 'N' and 'O'
const atom = atomsarray[i];
if (!atom.hetflag && (atom.atom === 'N' || atom.atom === 'O')) {
atoms.push(atom);
atom.hbondOther = null;
atom.hbondDistanceSq = Number.POSITIVE_INFINITY;
}
}
atoms.sort((a, b) => a.z - b.z);
for (i = 0, n = atoms.length; i < n; i++) {
const ai = atoms[i];
for (j = i + 1; j < n; j++) {
const aj = atoms[j];
const zdiff = aj.z - ai.z;
if (zdiff > maxlength)
// can't be connected
break;
if (aj.atom == ai.atom) continue; // can't be connected, but later might be
const ydiff = Math.abs(aj.y - ai.y);
if (ydiff > maxlength) continue;
const xdiff = Math.abs(aj.x - ai.x);
if (xdiff > maxlength) continue;
const dist = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff;
if (dist > maxlengthSq) continue;
if (aj.chain == ai.chain && Math.abs(aj.resi - ai.resi) < 4) continue; // ignore bonds between too close residues
// select closest hbond
if (dist < ai.hbondDistanceSq) {
ai.hbondOther = aj;
ai.hbondDistanceSq = dist;
}
if (dist < aj.hbondDistanceSq) {
aj.hbondOther = ai;
aj.hbondDistanceSq = dist;
}
}
}
};
const computeSecondaryStructure = function (atomsarray) {
assignBackboneHBonds(atomsarray);
// compute, per residue, what the secondary structure is
const chres = {}; // lookup by chain and resid
let i;
let il;
let c;
let r; // i: used in for loop, il: length of atomsarray
let atom;
let val;
// identify helices first
for (i = 0, il = atomsarray.length; i < il; i++) {
atom = atomsarray[i];
if (typeof chres[atom.chain] === 'undefined') chres[atom.chain] = [];
if (isFinite(atom.hbondDistanceSq)) {
const other = atom.hbondOther;
if (typeof chres[other.chain] === 'undefined') chres[other.chain] = [];
if (Math.abs(other.resi - atom.resi) === 4) {
// helix
chres[atom.chain][atom.resi] = 'h';
}
}
}
// plug gaps in helices
for (c in chres) {
for (r = 1; r < chres[c].length - 1; r++) {
const valbefore = chres[c][r - 1];
const valafter = chres[c][r + 1];
val = chres[c][r];
if (valbefore == 'h' && valbefore == valafter && val != valbefore) {
chres[c][r] = valbefore;
}
}
}
// now potential sheets - but only if mate not part of helix
for (i = 0, il = atomsarray.length; i < il; i++) {
atom = atomsarray[i];
if (isFinite(atom.hbondDistanceSq) && chres[atom.chain][atom.resi] != 'h' && atom.ss != 'h') {
chres[atom.chain][atom.resi] = 'maybesheet';
}
}
// sheets must bond to other sheets
for (let i = 0, il = atomsarray.length; i < il; i++) {
atom = atomsarray[i];
if (isFinite(atom.hbondDistanceSq) && chres[atom.chain][atom.resi] == 'maybesheet') {
const other = atom.hbondOther;
const otherval = chres[other.chain][other.resi];
if (otherval == 'maybesheet' || otherval == 's') {
// true sheet
chres[atom.chain][atom.resi] = 's';
chres[other.chain][other.resi] = 's';
}
}
}
// plug gaps in sheets and remove singletons
for (const c in chres) {
for (let r = 1; r < chres[c].length - 1; r++) {
const valbefore = chres[c][r - 1];
const valafter = chres[c][r + 1];
val = chres[c][r];
if (valbefore == 's' && valbefore == valafter && val != valbefore) {
chres[c][r] = valbefore;
}
}
for (let r = 0; r < chres[c].length; r++) {
const val = chres[c][r];
if (val == 'h' || val == 's') {
if (chres[c][r - 1] != val && chres[c][r + 1] != val) delete chres[c][r];
}
}
}
// assign to all atoms in residue, keep track of start
for (i = 0, il = atomsarray.length; i < il; i++) {
atom = atomsarray[i];
val = chres[atom.chain][atom.resi];
// clear hbondOther to eliminate circular references that prohibit serialization
delete atom.hbondOther;
delete atom.hbondDistanceSq;
if (typeof val == 'undefined' || val == 'maybesheet') continue;
atom.ss = val;
if (chres[atom.chain][atom.resi - 1] != val) atom.ssbegin = true;
if (chres[atom.chain][atom.resi + 1] != val) atom.ssend = true;
}
};
// make sure bonds are actually two way
const validateBonds = function (atomsarray, serialToIndex) {
for (let i = 0, n = atomsarray.length; i < n; i++) {
const atom = atomsarray[i];
for (let b = 0; b < atom.bonds.length; b++) {
const a2i = atom.bonds[b];
const atom2 = atomsarray[a2i];
const atomi = serialToIndex[atom.serial];
if (atom2 && atomi) {
const a1i = atom2.bonds.indexOf(atomi);
if (a1i < 0) {
atom2.bonds.push(atomi);
atom2.bondOrder.push(atom.bondOrder[b]);
}
}
}
}
};
// adds symmetry info to either duplicate and rotate/translate biological unit later or add extra atoms now
// matrices may be modified if normalization is requested
const processSymmetries = function (copyMatrices, atoms, options, cryst) {
const dontDuplicate = !options.duplicateAssemblyAtoms;
const end = atoms.length;
let offset = end;
let t;
let l;
let n; // Used in for loops
let modifiedIdentity = -1;
if (options.normalizeAssembly && cryst) {
// to normalize, translate every symmetry so that the centroid is
// in the unit cell. To do this, convert back to fractional coordinates,
// compute the centroid, calculate any adjustment needed to get it in [0,1],
// convert the adjustment to a cartesian translation, and then add it to
// the symmetry matrix
const conversionMatrix = conversionMatrix3(
cryst.a,
cryst.b,
cryst.c,
cryst.alpha,
cryst.beta,
cryst.gamma
);
const toFrac = new Matrix3();
toFrac.getInverse3(conversionMatrix);
for (t = 0; t < copyMatrices.length; t++) {
// transform with the symmetry, and then back to fractional coordinates
/** @type {Vector3 | number[]} */
let center = new Vector3(0, 0, 0);
for (n = 0; n < end; n++) {
const xyz = new Vector3(atoms[n].x, atoms[n].y, atoms[n].z);
xyz.applyMatrix4(copyMatrices[t]);
xyz.applyMatrix3(toFrac);
// figure out
center.add(xyz);
}
center.divideScalar(end);
center = [center.x, center.y, center.z];
/** @type {number[]|Vector3} */
let adjustment = [0.0, 0.0, 0.0];
for (let i = 0; i < 3; i++) {
while (center[i] < -0.001) {
center[i] += 1.0;
adjustment[i] += 1.0;
}
while (center[i] > 1.001) {
center[i] -= 1.0;
adjustment[i] -= 1.0;
}
}
// convert adjustment to non-fractional
adjustment = new Vector3(adjustment[0], adjustment[1], adjustment[2]);
adjustment.applyMatrix3(conversionMatrix);
// modify symmetry matrix to include translation
if (copyMatrices[t].isNearlyIdentity() && adjustment.lengthSq() > 0.001) {
modifiedIdentity = t; // keep track of which matrix was identity
}
copyMatrices[t].translate(adjustment);
}
}
if (!dontDuplicate) {
// do full assembly
for (n = 0; n < end; n++) {
atoms[n].sym = -1; // if identity matrix is present, original labeled -1
}
for (t = 0; t < copyMatrices.length; t++) {
if (!copyMatrices[t].isNearlyIdentity() && modifiedIdentity != t) {
const xyz = new Vector3();
for (n = 0; n < end; n++) {
const bondsArr = [];
for (l = 0; l < atoms[n].bonds.length; l++) {
bondsArr.push(atoms[n].bonds[l] + offset);
}
xyz.set(atoms[n].x, atoms[n].y, atoms[n].z);
xyz.applyMatrix4(copyMatrices[t]);
const newAtom = {};
for (const i in atoms[n]) {
newAtom[i] = atoms[n][i];
}
newAtom.x = xyz.x;
newAtom.y = xyz.y;
newAtom.z = xyz.z;
newAtom.bonds = bondsArr;
newAtom.sym = t; // so symmetries can be selected
newAtom.index = atoms.length;
atoms.push(newAtom);
}
offset = atoms.length;
} else {
for (n = 0; n < end; n++) {
atoms[n].sym = t;
}
}
}
if (modifiedIdentity >= 0) {
// after applying the other transformations, apply this one in place
const xyz = new Vector3();
for (n = 0; n < end; n++) {
xyz.set(atoms[n].x, atoms[n].y, atoms[n].z);
xyz.applyMatrix4(copyMatrices[modifiedIdentity]);
atoms[n].x = xyz.x;
atoms[n].y = xyz.y;
atoms[n].z = xyz.z;
}
}
// we have explicitly duplicated the atoms, remove model symmetry information
copyMatrices.length = 0;
} else if (copyMatrices.length > 1) {
for (t = 0; t < atoms.length; t++) {
const symmetries = [];
for (l = 0; l < copyMatrices.length; l++) {
if (!copyMatrices[l].isNearlyIdentity()) {
const newXYZ = new Vector3();
newXYZ.set(atoms[t].x, atoms[t].y, atoms[t].z);
newXYZ.applyMatrix4(copyMatrices[l]);
symmetries.push(newXYZ);
}
}
atoms[t].symmetries = symmetries;
}
}
};
/**
* @param {string} str
* @param {import("./specs").ParserOptionsSpec} options
*/
parsers.vasp = parsers.VASP = function (str /* ,options */) {
const atoms = [[]];
const lattice = {};
const lines = str.replace(/^\s+/, '').split(/\r?\n/);
if (lines.length < 3) {
return atoms;
}
if (lines[1].match(/\d+/)) {
lattice.length = parseFloat(lines[1]);
} else {
console.log('Warning: second line of the vasp structure file must be a number');
return atoms;
}
if (lattice.length < 0) {
console.log('Warning: Vasp implementation for negative lattice lengths is not yet available');
return atoms;
}
lattice.xVec = new Float32Array(lines[2].replace(/^\s+/, '').split(/\s+/));
lattice.yVec = new Float32Array(lines[3].replace(/^\s+/, '').split(/\s+/));
lattice.zVec = new Float32Array(lines[4].replace(/^\s+/, '').split(/\s+/));
const matrix = new Matrix3(
lattice.xVec[0],
lattice.xVec[1],
lattice.xVec[2],
lattice.yVec[0],
lattice.yVec[1],
lattice.yVec[2],
lattice.zVec[0],
lattice.zVec[1],
lattice.zVec[2]
);
matrix.multiplyScalar(lattice.length);
atoms.modelData = [{symmetries: [], cryst: {matrix}}];
const atomSymbols = lines[5].replace(/\s+/, '').replace(/\s+$/, '').split(/\s+/);
const atomSpeciesNumber = new Int16Array(lines[6].replace(/^\s+/, '').split(/\s+/));
let vaspMode = lines[7].replace(/\s+/, '');
if (vaspMode.match(/C/)) {
vaspMode = 'cartesian';
} else if (vaspMode.match(/D/)) {
vaspMode = 'direct';
} else {
console.log(
'Warning: Unknown vasp mode in POSCAR file: mode must be either C(artesian) or D(irect)'
);
return atoms;
}
if (atomSymbols.length != atomSpeciesNumber.length) {
console.log('Warning: declaration of atomary species wrong:');
console.log(atomSymbols);
console.log(atomSpeciesNumber);
return atoms;
}
lines.splice(0, 8);
let atomCounter = 0;
for (let i = 0, len = atomSymbols.length; i < len; i++) {
const atomSymbol = atomSymbols[i];
for (let j = 0, atomLen = atomSpeciesNumber[i]; j < atomLen; j++) {
const coords = new Float32Array(lines[atomCounter + j].replace(/^\s+/, '').split(/\s+/));
const atom = {};
atom.elem = atomSymbol;
if (vaspMode == 'cartesian') {
atom.x = lattice.length * coords[0];
atom.y = lattice.length * coords[1];
atom.z = lattice.length * coords[2];
} else {
atom.x =
lattice.length *
(coords[0] * lattice.xVec[0] +
coords[1] * lattice.yVec[0] +
coords[2] * lattice.zVec[0]);
atom.y =
lattice.length *
(coords[0] * lattice.xVec[1] +
coords[1] * lattice.yVec[1] +
coords[2] * lattice.zVec[1]);
atom.z =
lattice.length *
(coords[0] * lattice.xVec[2] +
coords[1] * lattice.yVec[2] +
coords[2] * lattice.zVec[2]);
}
atom.bonds = [];
atoms[0].push(atom);
}
atomCounter += atomSpeciesNumber[i];
}
return atoms;
};
/**
* @param {string} str
* @param {import("./specs").ParserOptionsSpec} [options]
* @returns {any}
*/
parsers.cube = parsers.CUBE = function (str, options) {
options = options || {};
const atoms = [[]];
let lines = str.split(/\r?\n/);
const assignbonds = options.assignBonds === undefined ? true : options.assignBonds;
if (lines.length < 6) return atoms;
let lineArr = lines[2].replace(/^\s+/, '').replace(/\s+/g, ' ').split(' ');
const natoms = Math.abs(parseFloat(lineArr[0]));
const cryst = {};
const origin = (cryst.origin = new Vector3(
parseFloat(lineArr[1]),
parseFloat(lineArr[2]),
parseFloat(lineArr[3])
));
lineArr = lines[3].replace(/^\s+/, '').replace(/\s+/g, ' ').split(' ');
lineArr = lines[3].replace(/^\s+/, '').replace(/\s+/g, ' ').split(' ');
// might have to convert from bohr units to angstroms
// there is a great deal of confusion here:
// n>0 means angstroms: http://www.gaussian.com/g_tech/g_ur/u_cubegen.htm
// n<0 means angstroms: http://paulbourke.net/dataformats/cube/
// always assume bohr: openbabel source code
// always assume angstrom: http://www.ks.uiuc.edu/Research/vmd/plugins/molfile/cubeplugin.html
// we are going to go with n<0 means angstrom - note this is just the first n
const convFactor = lineArr[0] > 0 ? 0.529177 : 1;
origin.multiplyScalar(convFactor);
const nX = Math.abs(lineArr[0]);
const xVec = new Vector3(
parseFloat(lineArr[1]),
parseFloat(lineArr[2]),
parseFloat(lineArr[3])
).multiplyScalar(convFactor);
lineArr = lines[4].replace(/^\s+/, '').replace(/\s+/g, ' ').split(' ');
const nY = Math.abs(lineArr[0]);
const yVec = new Vector3(
parseFloat(lineArr[1]),
parseFloat(lineArr[2]),
parseFloat(lineArr[3])
).multiplyScalar(convFactor);
lineArr = lines[5].replace(/^\s+/, '').replace(/\s+/g, ' ').split(' ');
const nZ = Math.abs(lineArr[0]);
const zVec = new Vector3(
parseFloat(lineArr[1]),
parseFloat(lineArr[2]),
parseFloat(lineArr[3])
).multiplyScalar(convFactor);
cryst.size = {x: nX, y: nY, z: nZ};
cryst.unit = new Vector3(xVec.x, yVec.y, zVec.z);
if (xVec.y != 0 || xVec.z != 0 || yVec.x != 0 || yVec.z != 0 || zVec.x != 0 || zVec.y != 0) {
// need a transformation matrix
cryst.matrix4 = new Matrix4(
xVec.x,
yVec.x,
zVec.x,
0,
xVec.y,
yVec.y,
zVec.y,
0,
xVec.z,
yVec.z,
zVec.z,
0,
0,
0,
0,
1
);
// include translation in matrix
const t = new Matrix4().makeTranslation(origin.x, origin.y, origin.z);
cryst.matrix4 = cryst.matrix4.multiplyMatrices(t, cryst.matrix4);
cryst.matrix = cryst.matrix4.matrix3FromTopLeft();
// all translation and scaling done by matrix, so reset origin and unit
cryst.origin = new Vector3(0, 0, 0);
cryst.unit = new Vector3(1, 1, 1);
}
atoms.modelData = [{cryst}];
// Extract atom portion; send to new GLModel...
lines = lines.splice(6, natoms);
const start = atoms[atoms.length - 1].length;
const end = start + lines.length;
for (let i = start; i < end; ++i) {
const atom = {};
atom.serial = i;
const line = lines[i - start];
const tokens = line.replace(/^\s+/, '').replace(/\s+/g, ' ').split(' ');
atom.elem = anumToSymbol[tokens[0]];
atom.x = parseFloat(tokens[2]) * convFactor;
atom.y = parseFloat(tokens[3]) * convFactor;