-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsymgen.py
1409 lines (1310 loc) · 52.3 KB
/
symgen.py
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
from xyzMath import Vec, Mat, Xform, RAD, projperp, SYMTET, SYMOCT, isvec, randnorm
import itertools
import re
import os
import inspect
from pymol_util import cgo_cyl, pymol
from pymol import cmd
symelem_nshow = 0
class SymElem(object):
"""docstring for SymElem"""
def __init__(self, kind, axis=Vec(0, 0, 1), cen=Vec(0, 0, 0), axis2=Vec(1, 0, 0), col=None,
input_xform=None):
super(SymElem, self).__init__()
self.kind = kind
self.axis = axis.normalized()
self.cen = cen
self.axis2 = axis2.normalized()
self.col = col
self.frames = []
self.input_xform = input_xform
if self.kind.startswith("C"):
assert not input_xform
self.nfold = int(self.kind[1:])
for i in range(self.nfold):
deg = i * 360.0 / self.nfold
self.frames.append(RAD(self.axis, deg, cen))
elif self.kind.startswith("D"):
assert not input_xform
self.nfold = int(self.kind[1:])
assert abs(axis.dot(axis2)) < 0.00001
for i in range(self.nfold):
deg = i * 360.0 / self.nfold
cx = RAD(self.axis, deg, cen)
self.frames.append(cx)
self.frames.append(RAD(self.axis2, 180.0, cen) * cx)
elif self.kind == "T":
self.frames = [
Xform(Mat(Vec(1, 0, 0), Vec(0, 1, 0), Vec(0, 0, 1)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, 0, 1), Vec(1, 0, 0), Vec(0, 1, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, 0, 1), Vec(-1, 0, 0), Vec(0, -1, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, 0, -1), Vec(1, 0, 0), Vec(0, -1, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, 0, -1), Vec(-1, 0, 0), Vec(0, 1, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, 1, 0), Vec(0, 0, 1), Vec(1, 0, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, 1, 0), Vec(0, 0, -1), Vec(-1, 0, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, -1, 0), Vec(0, 0, 1), Vec(-1, 0, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(0, -1, 0), Vec(0, 0, -1), Vec(1, 0, 0)), Vec(0, 0, 0)),
Xform(Mat(Vec(1, 0, 0), Vec(0, -1, 0), Vec(0, -0, -1)), Vec(0, 0, 0)),
Xform(Mat(Vec(-1, 0, 0), Vec(0, 1, 0), Vec(0, 0, -1)), Vec(0, 0, 0)),
Xform(Mat(Vec(-1, 0, 0), Vec(0, -1, 0), Vec(0, 0, 1)), Vec(0, 0, 0))
]
if input_xform:
xc = Xform(cen) * input_xform
else:
xc = Xform(cen)
for i, x in enumerate(self.frames):
self.frames[i] = xc * x * (~xc)
elif self.kind == "O":
self.frames = [
Xform(Mat(Vec(+1, +0, -0), Vec(+0, +1, +0), Vec(+0, -0, +1)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, +1, +0), Vec(+1, +0, -0), Vec(-0, +0, -1)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, -0, +1), Vec(+1, +0, -0), Vec(-0, +1, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+1, +0, -0), Vec(+0, -0, +1), Vec(+0, -1, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-0, +0, -1), Vec(+0, +1, +0), Vec(+1, -0, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-0, +1, +0), Vec(+0, -0, +1), Vec(+1, +0, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, +1, +0), Vec(-0, +0, -1), Vec(-1, +0, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, -0, +1), Vec(-0, +1, +0), Vec(-1, -0, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, -1, -0), Vec(+1, +0, +0), Vec(+0, -0, +1)), Vec(0, 0, 0)),
Xform(Mat(Vec(+1, -0, -0), Vec(-0, +0, -1), Vec(+0, +1, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+1, +0, +0), Vec(+0, -1, -0), Vec(-0, +0, -1)), Vec(0, 0, 0)),
Xform(Mat(Vec(-0, +0, -1), Vec(+1, -0, -0), Vec(-0, -1, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-1, +0, +0), Vec(+0, +1, -0), Vec(-0, +0, -1)), Vec(0, 0, 0)),
Xform(Mat(Vec(-1, -0, +0), Vec(+0, +0, +1), Vec(-0, +1, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, -0, +1), Vec(+0, -1, -0), Vec(+1, +0, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, +1, -0), Vec(-1, +0, +0), Vec(+0, -0, +1)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, +0, +1), Vec(-1, -0, +0), Vec(+0, -1, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, -1, -0), Vec(-0, -0, +1), Vec(-1, -0, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-0, -1, -0), Vec(-0, +0, -1), Vec(+1, -0, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-0, +0, -1), Vec(-1, +0, +0), Vec(+0, +1, -0)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, +0, -1), Vec(-0, -1, -0), Vec(-1, +0, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-1, +0, +0), Vec(-0, +0, -1), Vec(-0, -1, +0)), Vec(0, 0, 0)),
Xform(Mat(Vec(-1, -0, -0), Vec(+0, -1, -0), Vec(-0, -0, +1)), Vec(0, 0, 0)),
Xform(Mat(Vec(+0, -1, -0), Vec(-1, -0, -0), Vec(+0, +0, -1)), Vec(0, 0, 0)),
]
if input_xform:
xc = Xform(cen) * input_xform
else:
xc = Xform(cen)
for i, x in enumerate(self.frames):
self.frames[i] = (xc) * x * (~xc)
assert self.frames
if not self.frames[0] == Xform():
print(self.kind, self.frames[0].pretty())
assert self.frames[0] == Xform()
def show(self, label=None, **kwargs):
if not label:
global symelem_nshow
label = "SymElem_%i" % symelem_nshow
symelem_nshow += 1
pymol.cmd.delete(label)
v = pymol.cmd.get_view()
CGO = self.cgo(**kwargs)
pymol.cmd.load_cgo(CGO, label)
pymol.cmd.set_view(v)
def cgo(self, length=20.0, radius=0.5, sphereradius=1.5, col=None, showshape=0, **kwargs):
if not col and self.col:
col = self.col
CGO = []
x = Xform()
if "xform" in kwargs:
x = kwargs["xform"]
if self.kind[0] in "CD":
if not col:
if self.kind is "C2":
col = (1.0, 0.0, 0.0)
elif self.kind is "C3":
col = (0.0, 1.0, 0.0)
elif self.kind is "C4":
col = (0.0, 0.0, 1.0)
elif self.kind is "C5":
col = (1.0, 0.7, 0.8)
elif self.kind is "C6":
col = (1.0, 1.0, 0.0)
elif self.kind is "D2":
col = (1.0, 0.0, 1.0)
elif self.kind is "D3":
col = (0.0, 1.0, 1.0)
elif self.kind is "D4":
col = (1.0, 0.5, 0.0)
elif self.kind is "D6":
col = (0.5, 1.0, 0.0)
else:
raise NotImplementedError("unknown symelem kind " + self.kind)
a = self.axis * length / 2.0
c = self.cen
c1, c2 = self.cen - a, self.cen + a
c = x * c
c1 = x * c1
c2 = x * c2
c.round0()
c1.round0()
c2.round0()
CGO.extend([
#pymol.cgo.BEGIN, pymol.cgo.LINES,
#pymol.cgo.COLOR, col[0], col[1],col[2],
#pymol.cgo.VERTEX, self.cen.x-a.x, self.cen.y-a.y, self.cen.z-a.z,
#pymol.cgo.VERTEX, self.cen.x+a.x, self.cen.y+a.y, self.cen.z+a.z,
# pymol.cgo.END,
pymol.cgo.CYLINDER,
c1.x,
c1.y,
c1.z,
c2.x,
c2.y,
c2.z,
radius,
# pymol.cgo.CYLINDER, 0, 50, 0, 0, -50, 0, radius,
col[0],
col[1],
col[2],
col[0],
col[1],
col[2],
pymol.cgo.SPHERE,
c.x,
c.y,
c.z,
sphereradius
])
if self.kind.startswith("D"):
for i in range(self.nfold):
xtmp = self.frames[2 * i].R
if self.nfold == 2 and i == 1:
xtmp = RAD(self.axis, 90)
a = xtmp * self.axis2 * length / 2.0
c = self.cen
c1, c2 = self.cen - a, self.cen + a
if "xform" in kwargs:
x = kwargs["xform"]
c = x * c
c1 = x * c1
c2 = x * c2
c.round0()
c1.round0()
c2.round0()
r = radius if self.nfold == 2 else radius
CGO.extend([
pymol.cgo.CYLINDER,
c1.x,
c1.y,
c1.z,
c2.x,
c2.y,
c2.z,
r,
col[0],
col[1],
col[2],
col[0],
col[1],
col[2],
])
elif self.kind == "T":
cen = x * self.cen
cen.round0()
CGO.extend(cgo_sphere(cen, 1.6 * sphereradius, col=(0.5, 0.5, 1)))
seen2, seen3 = list(), list()
for f in self.frames:
c2b = x.R * f.R * (Vec(1, 0, 0).normalized() * length / 2.0)
c3a = x.R * f.R * (-Vec(1, 1, 1).normalized() * length / 2.0)
c3b = x.R * f.R * (Vec(1, 1, 1).normalized() * length / 2.0)
c2b.round0()
c3a.round0()
c3b.round0()
if not c2b in seen2:
CGO.extend(cgo_cyl(cen, cen + c2b, radius, col=(1, 0, 0)))
seen2.append(c2b)
if not c3a in seen3:
CGO.extend(cgo_cyl(cen + c3a, cen + c3b, radius, col=(0, 1, 0)))
seen3.append(c3a)
seen3.append(c3b)
elif self.kind == "O":
cen = x * self.cen
cen.round0()
CGO.extend(cgo_sphere(cen, 1.6 * sphereradius, col=(0.5, 0.5, 1)))
seen2, seen3, seen4 = list(), list(), list()
for f in self.frames:
c2a = x.R * f.R * (-Vec(1, 1, 0).normalized() * length / 2.0)
c2b = x.R * f.R * (Vec(1, 1, 0).normalized() * length / 2.0)
c3a = x.R * f.R * (-Vec(1, 1, 1).normalized() * length / 2.0)
c3b = x.R * f.R * (Vec(1, 1, 1).normalized() * length / 2.0)
c4a = x.R * f.R * (-Vec(1, 0, 0).normalized() * length / 2.0)
c4b = x.R * f.R * (Vec(1, 0, 0).normalized() * length / 2.0)
c2a.round0()
c2b.round0()
c3a.round0()
c3b.round0()
c4a.round0()
c4b.round0()
if not c2b in seen2:
CGO.extend(cgo_cyl(cen + c2a, cen + c2b, radius, col=(1, 0, 0)))
seen2.append(c2a)
seen2.append(c2b)
if not c3a in seen3:
CGO.extend(cgo_cyl(cen + c3a, cen + c3b, radius, col=(0, 1, 0)))
seen3.append(c3a)
seen3.append(c3b)
if not c4a in seen4:
CGO.extend(cgo_cyl(cen + c4a, cen + c4b, radius, col=(0, 0, 1)))
seen4.append(c4a)
seen4.append(c4b)
if showshape:
if self.kind == "C2":
axs = x.R * self.axis
cen = x * self.cen
p1 = x.R * projperp(self.axis, Vec(7, 3, 1)).normalized() * 30.0
p2 = RAD(axs, 180.0) * p1
p3 = x.R * projperp(self.axis, Vec(1, 3, 7)).normalized() * 30.0
p4 = RAD(axs, 180.0) * p3
p1 = cen + p1
p2 = cen + p2
p3 = cen + p3
p4 = cen + p4
axs.round0()
p1.round0()
p2.round0()
p3.round0()
p4.round0()
# print p1,p2,p3,p4
CGO.extend([
pymol.cgo.BEGIN,
pymol.cgo.TRIANGLES,
pymol.cgo.COLOR,
col[0],
col[1],
col[2],
pymol.cgo.ALPHA,
1,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p1.x + axs.x / 10.0,
p1.y + axs.y / 10.0,
p1.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p2.x + axs.x / 10.0,
p2.y + axs.y / 10.0,
p2.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p3.x + axs.x / 10.0,
p3.y + axs.y / 10.0,
p3.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p1.x + axs.x / 10.0,
p1.y + axs.y / 10.0,
p1.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p2.x + axs.x / 10.0,
p2.y + axs.y / 10.0,
p2.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p4.x + axs.x / 10.0,
p4.y + axs.y / 10.0,
p4.z + axs.z / 10.0,
pymol.cgo.COLOR,
1 - col[0],
1 - col[1],
1 - col[2],
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p1.x - axs.x / 10.0,
p1.y - axs.y / 10.0,
p1.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p2.x - axs.x / 10.0,
p2.y - axs.y / 10.0,
p2.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p3.x - axs.x / 10.0,
p3.y - axs.y / 10.0,
p3.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p1.x - axs.x / 10.0,
p1.y - axs.y / 10.0,
p1.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p2.x - axs.x / 10.0,
p2.y - axs.y / 10.0,
p2.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p4.x - axs.x / 10.0,
p4.y - axs.y / 10.0,
p4.z - axs.z / 10.0,
pymol.cgo.END,
])
if self.kind == "C3":
axs = x.R * self.axis
cen = x * self.cen
p1 = projperp(axs, Vec(1, 2, 3)).normalized() * 35.0
p2 = RAD(axs, 120.0) * p1
p3 = RAD(axs, 240.0) * p1
p1 = cen + p1
p2 = cen + p2
p3 = cen + p3
CGO.extend([
pymol.cgo.BEGIN,
pymol.cgo.TRIANGLES,
pymol.cgo.COLOR,
col[0],
col[1],
col[2],
pymol.cgo.ALPHA,
1,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p1.x + axs.x / 10.0,
p1.y + axs.y / 10.0,
p1.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p2.x + axs.x / 10.0,
p2.y + axs.y / 10.0,
p2.z + axs.z / 10.0,
pymol.cgo.NORMAL,
axs.x,
axs.y,
axs.z,
pymol.cgo.VERTEX,
p3.x + axs.x / 10.0,
p3.y + axs.y / 10.0,
p3.z + axs.z / 10.0,
pymol.cgo.COLOR,
1 - col[0],
1 - col[1],
1 - col[2],
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p1.x - axs.x / 10.0,
p1.y - axs.y / 10.0,
p1.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p2.x - axs.x / 10.0,
p2.y - axs.y / 10.0,
p2.z - axs.z / 10.0,
pymol.cgo.NORMAL,
-axs.x,
-axs.y,
-axs.z,
pymol.cgo.VERTEX,
p3.x - axs.x / 10.0,
p3.y - axs.y / 10.0,
p3.z - axs.z / 10.0,
pymol.cgo.END,
])
return CGO
def __eq__(self, other):
return self.kind == other.kind and self.cen == other.cen and self.axis == other.axis and self.axis2 == other.axis2
def __str__(self):
return self.kind + " " + str(self.axis)
class SymElemPosition(object):
"""docstring for SymElemPosition"""
def __init__(self, symelem, xform):
super(SymElemPosition, self).__init__()
self.symelem = symelem
self.xform = xform
def __eq__(self, other):
return self.symelem == other.symelem and self.xform == other.xform
class SymElemGroupManager(object):
"""docstring for SymElemGroupManager"""
def __init__(self):
super(SymElemGroupManager, self).__init__()
self.node2elems = dict()
self.elem2nodes = dict()
def add_symelem(self, symelem, xform, node):
xelem = SymElemPosition(symelem, xform)
seenit = False
for oldelem in list(self.elem2nodes.keys()):
if oldelem == xelem:
xelem = oldelem
assert not seenit
seenit = True
if xelem not in list(self.elem2nodes.keys()):
self.elem2nodes[xelem] = list()
self.elem2nodes[xelem].append(node)
if node not in list(self.node2elems.keys()):
self.node2elems[node] = list()
self.node2elem[node].append(xelem)
class SymTrieNode(object):
"""docstring for SymTrieNode"""
def __init__(self, generators, ielem, iframe, depth, position):
super(SymTrieNode, self).__init__()
self.generators = generators
self.ielem = ielem
self.iframe = iframe
self.depth = depth
self.children = []
self.parent = None
self.position = position
def add_child(self, node):
self.children.append(node)
def visit(self, visitor, depth=0, xform=Xform()):
parentxform = xform
if self.parent:
if not self.parent.position == parentxform:
print("parentxform mismatch!", self.parent.position.pretty())
print("parentxform mismatch!", parentxform.pretty())
assert self.parent.position == parentxform
xform = parentxform * self.generators[self.ielem].frames[self.iframe]
visitor(self, depth=depth, xform=xform, parentxform=parentxform)
for c in self.children:
c.visit(visitor, depth=depth + 1, xform=xform)
def __str__(self):
return "elem %2i frame %2i depth %2i nchild %2i" % (self.ielem, self.iframe, self.depth,
len(self.children))
class SymTrieSanityCheckVisitor(object):
"""docstring for SymTrieSanityCheckVisitor"""
def __init__(self):
super(SymTrieSanityCheckVisitor, self).__init__()
self.seenit = list() # [ Xform() ]
def __call__(self, STN, **kwargs):
assert STN.position not in self.seenit
self.seenit.append(STN.position)
for c in STN.children:
assert c.parent is STN
if STN.parent:
assert STN in STN.parent.children
def generate_sym_trie_recurse(generators, depth, opts, body, heads, newheads, igen):
if depth < 1:
return
print("GEN SYM TRIE", "depth", depth, "igen", igen, "heads", len(heads), "newheads",
len(newheads))
newnewheads = []
# for ielem, elem in enumerate(generators):
ielem = igen
elem = generators[igen]
for iframe, frame in enumerate(elem.frames):
if iframe is 0:
continue # skip identity
# print "FRAME",frame.pretty()
for head, xpos in itertools.chain(newheads, heads):
candidate = xpos * frame
# print candidate.pretty()
seenit = False
for seennode, seenframe in itertools.chain(newnewheads, newheads, heads, body):
if candidate == seenframe:
seenit = True
# assert ~candidate * seenframe == Xform()
break
if not seenit:
newhead = SymTrieNode(generators, ielem, iframe, head.depth + 1, position=candidate)
head.add_child(newhead)
newhead.parent = head
newnewheads.append((newhead, candidate))
# print "NEWHEAD",candidate
newheads.extend(newnewheads)
if igen + 1 == len(generators):
body.extend(heads)
heads = newheads
newheads = []
# print len(newheads),igen,len(generators)
if depth > 1: # and newheads:
generate_sym_trie_recurse(generators, depth - 1, opts, body, heads, newheads,
(igen + 1) % len(generators))
def generate_sym_trie(generators, depth=10, opts=None):
if opts is None:
opts = dict()
print("NEW SYM TRIE")
root = SymTrieNode(generators, 0, 0, 0, Xform())
heads = [
(root, Xform()),
]
body = list()
newheads = list()
generate_sym_trie_recurse(generators, depth, opts, body, heads, newheads, 0)
sanity_check = SymTrieSanityCheckVisitor()
root.visit(sanity_check)
return root
##########################################################################
##########################################################################
####################################### move the above to SymTrie!!! #####
##########################################################################
##########################################################################
newpath = os.path.dirname(inspect.getfile(inspect.currentframe())) # script directory
import sys
if not newpath in sys.path:
sys.path.append(newpath)
from xyzMath import Vec, Mat, Xform, RAD, projperp, Ux, Uy, Uz
from pymol_util import cgo_sphere, cgo_segment, cgo_cyl
#from SymTrie import SymElem, SymTrieNode, generate_sym_trie
import itertools
import random
import functools
# run /Users/sheffler/pymol/una.py; make_d3oct("test*","o33*",depth=3)
def makesym(frames0, sele="all", newobj="MAKESYM", depth=None, maxrad=9e9, n=9e9):
v = cmd.get_view()
cmd.delete(newobj)
sele = "((" + sele + ") and (not TMP_makesym_*))"
selechains = cmd.get_chains(sele)
print(selechains)
if not depth:
frames = frames0
else:
frames = expand_xforms(frames0, N=depth, maxrad=maxrad)
# order on COM transform dis
cen = com(sele)
frames = sorted(frames, key=lambda x: cen.distance(x * cen))
# make new objs
for i, x in enumerate(frames):
if i >= n:
break
# print i, x.pretty()
tmpname = "TMP_makesym_%i" % i
cmd.create(tmpname, sele)
for j, c in enumerate(selechains):
cmd.alter(tmpname + " and chain " + c,
"chain='%s'" % ROSETTA_CHAINS[len(selechains) * i + j])
xform(tmpname, x)
cmd.create(newobj, "TMP_makesym_*")
cmd.delete("TMP_makesym_*")
cmd.set_view(v)
# util.cbc()
def makecx(sel='all', name="TMP", n=5, axis=Uz):
if sel == 'all':
for i, o in enumerate(cmd.get_object_list()):
makecx(sel=o, name="TMP%i" % i, n=n, axis=axis)
return
v = cmd.get_view()
cmd.delete("TMP__C%i_*" % n)
chains = ROSETTA_CHAINS
for i in range(n):
cmd.create("TMP__C%i_%i" % (n, i), sel + " and (not TMP__C%i_*)" % n)
for i in range(n):
rot("TMP__C%i_%i" % (n, i), axis, -360.0 * float(i) / float(n))
for i in range(n):
cmd.alter("TMP__C%i_%i" % (n, i), "chain = '%s'" % chains[i])
util.cbc("TMP__C*")
# for i in range(n): cmd.alter("TMP__C%i_%i"%(n, i),"resi=str(int(resi)+%i)"%(1000*i));
# util.cbc("TMP__C*")
cmd.create(name, "TMP__*")
cmd.delete("TMP__*")
cmd.set_view(v)
cmd.disable(sel)
cmd.enable(name)
def mofview():
cmd.set('sphere_scale', 0.3)
cmd.hide('ev')
# cmd.show('sti')
#cmd.show('lines', 'name n+ca+c')
cmd.show('sti', 'resn asp+das+cys+dcs+his+dhi+glu+dgu+zn and not hydro and not name n+c+o')
# cmd.show('sti', 'resn cys and name HG')
cmd.show('sph', 'name ZN')
# cmd.show('car')
cmd.show('sti', 'name n+ca+c+cb')
cmd.show('sph', 'name cb and not resn asp+das+cys+dcs+his+dhi+glu+dgu')
# util.cbag('all')
# cmd.color('green', 'name N')
cmd.unbond('name zn', 'all')
cmd.bond('name zn', '(not elem H+C) within 3 of name zn')
showline(Vec(-2, -1, 1) * 20, Vec(0, 0, 0))
showline(Vec(-1, -1, 0) * 20, Vec(0, 0, 0))
showaxes()
cmd.show('cgo')
# makec3(axis=Vec(1, 1, 1))
# cmd.hide('sti')
# util.cbag()
# cmd.zoom()
# cmd.show('lin')
# cmd.show('sti', 'resn asp+das+cys+dcs+his+dhi+glu+dgu+zn')
cmd.extend('mofview', mofview)
def makedx(sel='all', n=2, name=None):
if not name:
name = sel.replace("+", "").replace(" ", "") + "_D%i" % n
cmd.delete(name)
v = cmd.get_view()
cmd.delete("_TMP_D%i_*" % n)
ALLCHAIN = ROSETTA_CHAINS
chains = cmd.get_chains(sel)
for i in range(n):
dsel = "_TMP_D%i_%i" % (n, i)
dsel2 = "_TMP_D%i_%i" % (n, n + i)
cmd.create(dsel, sel + " and (not _TMP_D%i_*)" % n)
rot(dsel, Uz, 360.0 * float(i) / float(n))
cmd.create(dsel2, dsel)
rot(dsel2, Ux, 180.0)
for ic, c in enumerate(chains):
cmd.alter("((%s) and chain %s )" % (dsel, c),
"chain = '%s'" % ALLCHAIN[len(chains) * (i) + ic])
cmd.alter("((%s) and chain %s )" % (dsel2, c),
"chain = '%s'" % ALLCHAIN[len(chains) * (i + n) + ic])
cmd.create(name, "_TMP_D*")
util.cbc(name)
cmd.delete("_TMP_D*")
cmd.set_view(v)
cmd.disable(sel)
cmd.enable(name)
for i in range(2, 21):
globals()['makec%i' % i] = functools.partial(makecx, n=i)
for i in range(2, 21):
globals()['maked%i' % i] = functools.partial(makedx, n=i)
def makecxauto():
for o in cmd.get_object_list():
n = int(re.search("_C\d+_", o).group(0)[2:-1])
makecx(o, n)
def maketet(sel='all', name="TET", n=12):
makesym(frames0=SYMTET, sele=sel, newobj=name, n=n)
def makeoct(sel='all', name="OCT", n=24):
makesym(frames0=SYMOCT, sele=sel, newobj=name, n=n)
def makeicos(sel='all', name="ICOS", n=60):
makesym(frames0=SYMICS, sele=sel, newobj=name, n=n)
def make_d3oct(d3, cage, cage_trimer_chain="A", depth=4, maxrad=9e9):
print(
cmd.super("((" + cage + ") and (chain " + cage_trimer_chain + "))",
"((" + d3 + ") and (chain A))"))
zcagecen = com(cage + " and name ca").z
print(zcagecen)
# return
x = alignvectors(Vec(1, 1, 1), Vec(1, -1, 0), Vec(0, 0, 1), Vec(1, 0, 0))
# print x * Vec(1,1,1), x*Vec(1,-1,0)
# RAD(Ux,180), RAD(Uy,120),
G = [
RAD(Ux, 180),
RAD(Uz, 120),
RAD(x * Vec(1, 0, 0), 90, Vec(0, 0, zcagecen)),
RAD(x * Vec(1, 1, 0), 180, Vec(0, 0, zcagecen)),
]
makesym(G, sele="((" + d3 + ") and ((chain A+B) and name CA))", depth=depth, maxrad=maxrad)
cmd.show("sph", "MAKESYM")
# cmd.disable("all")
cmd.enable("MAKESYM")
def make_d3tet(d3, cage, cage_trimer_chain="A", depth=4, maxrad=9e9):
print(
cmd.super("((" + cage + ") and (chain " + cage_trimer_chain + "))",
"((" + d3 + ") and (chain A))"))
zcagecen = com(cage + " and name ca").z
print(zcagecen)
# return
x = alignvectors(Vec(1, 1, 1), Vec(1, -1, 0), Vec(0, 0, 1), Vec(1, 0, 0))
# print x * Vec(1,1,1), x*Vec(1,-1,0)
# RAD(Ux,180), RAD(Uy,120),
G = [
RAD(Ux, 180),
RAD(Uz, 120),
RAD(x * Vec(1, 0, 0), 180, Vec(0, 0, zcagecen)),
]
makesym(G, sele="((" + d3 + ") and ((chain A+B) and name CA))", depth=depth, maxrad=maxrad)
cmd.show("sph", "MAKESYM")
# cmd.disable("all")
cmd.enable("MAKESYM")
def print_node(node, **kwargs):
print(kwargs['depth'] * " ", node, kwargs['xform'].pretty())
def show_node(node, **kwargs):
if node.iframe == 1:
node.show(xform=kwargs['xform'])
class CountFrames(object):
"""docstring for CountFrames"""
def __init__(self):
super(CountFrames, self).__init__()
self.count = 0
def __call__(self, *args, **kwkwargs):
self.count += 1
def cgo_cyl_arrow(c1, c2, r, col=(1, 1, 1), col2=None, arrowlen=4.0):
if not col2:
col2 = col
CGO = []
c1.round0()
c2.round0()
CGO.extend(cgo_cyl(c1, c2 + randnorm() * 0.0001, r=r, col=col, col2=col2))
dirn = (c2 - c1).normalized()
perp = projperp(dirn, Vec(0.2340790923, 0.96794275, 0.52037438472304783)).normalized()
arrow1 = c2 - dirn * arrowlen + perp * 2.0
arrow2 = c2 - dirn * arrowlen - perp * 2.0
# -dirn to shift to sphere surf
CGO.extend(cgo_cyl(c2 - dirn * 3.0, arrow1 - dirn * 3.0, r=r, col=col2))
# -dirn to shift to sphere surf
CGO.extend(cgo_cyl(c2 - dirn * 3.0, arrow2 - dirn * 3.0, r=r, col=col2))
return CGO
class BuildCGO(object):
"""docstring for BuildCGO"""
def __init__(self, nodes, maxrad=9e9, origin=Vec(0, 0, 0), bbox=(Vec(
-9e9, -9e9, -9e9), Vec(9e9, 9e9, 9e9)), showlinks=False, showelems=True, label="BuildCGO",
arrowlen=10.0, **kwargs):
super(BuildCGO, self).__init__()
self.nodes = nodes
self.CGO = cgo_sphere(Vec(0, 0, 0), 3.0)
self.jumps = set()
self.maxrad = maxrad
self.origin = origin
self.bbox = bbox
self.bbox[0].x, self.bbox[1].x = min(self.bbox[0].x, self.bbox[1].x), max(
self.bbox[0].x, self.bbox[1].x)
self.bbox[0].y, self.bbox[1].y = min(self.bbox[0].y, self.bbox[1].y), max(
self.bbox[0].y, self.bbox[1].y)
self.bbox[0].z, self.bbox[1].z = min(self.bbox[0].z, self.bbox[1].z), max(
self.bbox[0].z, self.bbox[1].z)
self.showlinks = showlinks
self.showelems = showelems
self.label = label
self.arrowlen = arrowlen
self.colors = list()
self.colors = [
(1, 1, 0),
(0, 1, 1),
(1, 0, 1),
(0.5, 0.5, 0.5),
]
self.kwargs = kwargs
def bounds_check(self, v):
if self.origin.distance(v) > self.maxrad:
return False
if not self.bbox[0].x <= v.x <= self.bbox[1].x:
return False
if not self.bbox[0].y <= v.y <= self.bbox[1].y:
return False
if not self.bbox[0].z <= v.z <= self.bbox[1].z:
return False
return True
def __call__(self, node, **kwargs):
x = kwargs["xform"]
cencen = Vec(0, 0, 0)
px = kwargs["parentxform"]
if self.nodes:
pcen = px * self.nodes[-1]
# pcen = px*self.nodes[0]
# show "nodes"
for icen, cen in enumerate(self.nodes):
xcen = x * cen
if pcen:
self.jumps.add(pcen.distance(xcen))
if self.showlinks:
if self.bounds_check(pcen) or self.bounds_check(xcen):
# print "DEBUG",icen,px.pretty(),px==Xform()
if icen != 0 or node.parent: # skip node 0 for root
self.add_segment(pcen, xcen, icen)
if self.bounds_check(xcen):
self.add_sphere(x * (cen + Vec(0, 0, 0)), 2.0,
text="%s%i" % ("ABCD"[icen], node.depth), icol=icen)
self.add_sphere(x * (cen + Vec(2, 0, 0)), 2.0,
text="%s%i" % ("ABCD"[icen], node.depth), icol=icen)
self.add_sphere(x * (cen + Vec(0, 2, 0)), 2.0,
text="%s%i" % ("ABCD"[icen], node.depth), icol=icen)
pcen = xcen
# show symelems
if self.showelems:
for elem in node.generators:
if self.bounds_check(x * elem.cen):
mergeargs = dict(list(kwargs.items()) + list(self.kwargs.items()))
self.add_symelem(elem, x, **mergeargs)
def add_symelem(self, elem, x, **kwargs):
# should add duplicate checks here
self.CGO.extend(elem.cgo(**kwargs))
def add_sphere(self, cen, rad, text="", icol=0):
# should add duplicate checks here
cen.round0()
if text:
pos = [cen.x + 1.0, cen.y + 1.0, cen.z + 1.0]
v = cmd.get_view()
axes = [[v[0], v[3], v[6]], [v[1], v[4], v[7]], [v[2], v[5], v[8]]]
# pymol.cgo.wire_text(self.CGO,pymol.vfont.plain,pos,text,axes)
self.CGO.extend(cgo_sphere(cen, rad, col=self.colors[icol]))
def add_segment(self, c1, c2, icol):
# should add duplicate checks here
if c1.distance(c2) < 1.0:
return
self.CGO.extend(
cgo_cyl_arrow(c1, c2, r=0.5, col=self.colors[max(0, icol - 1)], col2=self.colors[icol]))
def show(self, **kwargs):
v = cmd.get_view()
cmd.delete(self.label)
cmd.load_cgo(self.CGO, self.label)
cmd.set_view(v)
# for i,c in enumerate(self.nodes):
# showsphere(c,1.5,col=self.colors[i])
print(self.jumps)
# TODO move to xyzMath
class VecDict(object):
"""docstring for VecDict"""
def __init__(self):
super(VecDict, self).__init__()
self.keys_ = list()
self.values_ = list()
def keys(self):
return tuple(self.keys_)
def values(self):
return tuple(self.values_)
def items(self):
return zip(self.keys_, self.values_)
def __getitem__(self, key):
i = self.keys_.index(key)
return self.values_[i]
def __setitem__(self, key, val):
try:
i = self.keys_.index(key)
self.values_[i] = val
except ValueError:
assert len(self.keys_) == len(self.values_)
self.keys_.append(key)
self.values_.append(val)
class ComponentCenterVisitor(object):
"""docstring for ComponentCenterVisitor"""
def __init__(self, symelems, extranodes=[], label="NODES", colors=list(), showlinks=1,
**kwargs):
super(ComponentCenterVisitor, self).__init__()
# if len(symelems) > 2:
# raise NotImplementedError("num components > 2 not working yet... BFS fails")
self.symelems = symelems
CCs = [elem.cen for elem in symelems]
CCs.extend(extranodes)
self.primaryCCs = CCs
self.label = label
self.priCCtoCClist = VecDict()
self.priCCtoCCframes = VecDict()
for n in self.primaryCCs:
assert isvec(n)
self.priCCtoCClist[n] = [n]
self.priCCtoCCframes[n] = VecDict()
self.parentmap = None
self.childmap = None
self.colors = colors
self.colors.extend(((1, 1, 0), (0, 1, 1), (1, 0, 1), (0.7, 0.7, 0.7)))
self.showlinks = showlinks
def __call__(self, sym_trie_node, xform, parentxform, **kwargs):
assert list(self.priCCtoCCframes.keys()) == list(self.priCCtoCClist.keys())
for priCC in list(self.priCCtoCClist.keys()):
CC = xform * priCC
CClist = self.priCCtoCClist[priCC]
CCframes = self.priCCtoCCframes[priCC]
if not CC in CClist:
CClist.append(CC)
if not CC in list(CCframes.keys()):
CCframes[CC] = list()
CCframes[CC].append(sym_trie_node)
def makeCCtree(self, dhint=1.001):
root = self.priCCtoCClist[self.primaryCCs[0]][0]
self.parentmap = VecDict()
self.parentmap[root] = None
self.childmap = dict()