Repository navigation
Expand file tree
/
Copy pathZEAL.m
More file actions
1377 lines (1031 loc) · 58.4 KB
/
Copy pathZEAL.m
File metadata and controls
1377 lines (1031 loc) · 58.4 KB
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
classdef ZEAL < handle
%ZEAL Class to perform shape-based superposition of proteins.
% See description in the ZEAL method below for input details and
% usage or the manual at https://andre-lab.github.io/ZEAL/
%
%
% CITE AS
% ZEAL: F. Ljung and I. André, "Protein structure alignment based on
% shape similarity", Bioinformatics (2020)
%
%
% Last modified 2021-01
%
% TODO: Add event listeners so that ZEAL responds automatically when
% Settings property is chanegd -> recompute shapeFunction or ZC moments
% or ZEALscore etc. Useful when we will build the GUI from this class
%
% TODO: Unit tests
%
% ---------------------------------------------------------------------
properties
fixed % holder for data specific for fixed structure: PDB data, ZC moments etc
rotating % holder for data specific for rotating structure: PDB data, ZC moments etc
Settings % holder for various settings: PDB selection, ZC, shape function
Search % holder for data after alignment search
ZCDdistance = [] % the Eucldiean distance between the shape descriptors of the fixed and rotating structure
Score = [] % the ZEAL score
end
properties (Hidden)
AlignMode % true/false
% true if ZEAL should align (2 shapes defined), otherwise
% compute shape descriptors (1 shape defined)
Logging
AlignLater % true/false (false by default). If true then ZEAL will not start aligning automatically
InputParserOnly % true/false ->
end
properties (Constant)
ChiCoeffs = ChiCoeffs % A singleton (=only one instance) object (ChiCoeffs class)
% that handles the loading of pre-computed
% chi-coefficients from .mat-files
% The coefficients are visible (like a "global" variable) for all
% instances of ZEAL, but they are only loaded
% to memory once. See also the
% reloadChiCoeffs method in this class.
end
methods
function obj = ZEAL(fix, varargin)
%ZEAL Construct an instance of this class
%
% INPUT
% -------------------------------------------------------------
% <fix> : string
% filepath to pdb file or 4-letter PDB ID code which ZEAL will
% try to download. If 4+1 -letter code, then ZEAL assumes last
% letter is the chain ID that should be extracted
%
% OPTIONAL
% 'name'-value pairs : type (default value)
% (or given as a structure: the parser expands structures into separate inputs,
% where each field name corresponds to an input parameter name)
%
% 'rot' : string : same as for <fix>, but defines the
% structure that will be rotated
%
% 'Order' : integer (20)
% 'GridRes' : integer (64)
% 'FunEvals' : integer (300)
% Number of ZEAL score evaluations during search =
% the stopping criterium for search
%
% 'AlignLater' : true/false (false)
% If true then ZEAL will not start searching for best alignment
% automatically. Manuall start the search using the method
% shapeAlign(ZEALobject).
%
% 'InputParserOnly' : true/false (false)
% If true then only the option parsing is performed
%
% 'LogLevel' : 'basic' / 'standard' / 'detailed' / 'none' ('standard')
% Sets the level of information printed to the console.
% 'detailed' not recommended unless option 'AlignLater' is set
% to true;
%
% 'ShapeFunction' : string ('MS') {choose between 'SAS','MS','vdw','electron_density'}
% MS = molecular surface; SAS = solvent-accessible surface; vdw
% = van der Waals; electron_density = Gaussian atom
% representation
%
% 'ProbeRadius' : integer (1.4)
% 'SmearFactor' : double (0.3) - for electron_density shape
% 'ShellThickness' : integer (1)
%
%
% 'ChiCoeffPath ' : string ([pwd '/chi_coefficients/'])
% folder path to precomputed mat files ('chiCoeffs_order_N.mat') with the
% object-independent Chi coefficients computed for order N. These can be computed
% using ZC.computeChiCoeffs.
%
%
% 'fix_includeHetatoms' : true/false (false)
% 'fix_includeHatoms' : true/false (false)
% 'fix_chainID' : string ('A')
% 'fix_altLocID' : string ('A')
% 'fix_residueNumbers' : vector ([] = all residues)
%
% 'rot_includeHetatoms' : true/false (false)
% 'rot_includeHatoms' : true/false (false)
% 'rot_chainID' : string ('A')
% 'rot_altLocID' : string 'A'
% 'rot_residueNumbers' : vector ([] = all residues)
%
% 'PCAalign' : true/false (false)
% If true, atom coordinates are aligned along their principal axes in the molShape class: coordinates are first rotated so that
% the largest eigenvector is along the z-axis, the second
% largest along the y-axis, and the last along the
% x-axis.
%
%
% OUTPUT and USAGE
% -------------------------------------------------------------
%
% 1) If only one structure is defined, then ZEAL will only compute the ZC moments
% and shape descripors for that strucure.
%
% Ex:
% ZCD_5MOK = ZEAL('5MOK');
%
% % get the Zernike-Canterakis shape descriptors
% ZCD_5MOK.fixed.ShapeDescriptors
%
%
% 2) If two structrues are given, then ZEAL will compute the shape similarity
% between the structures (the Euclidean distance between their shape descriptors (ZEAL.ZCDdistance)),
% and then search for the best superposition (unless 'AlignLater' = true )
%
% Ex:
% aligned_shapeMatch = ZEAL('5MOKA', 'rot', '2HO1A');
%
% % get the Euclidean distance between the shape descriptors
% aligned_shapeMatch.ZCDdistance
%
% % get the ZEAL score for the superposition
% aligned_shapeMatch.Score
%
%
% ------------ CHANGING DEFAULT SEARCH SETTINGS --------------
% Ex:
%
% - stopping-criteria for the search: change the maximum number of
% function evaluations
% ZEAL('5MOKA', 'rot', '2HO1A', 'FunEvals', 500);
%
%
% - shape function to use: change the shape parameter
% ZEAL('5MOKA', 'rot', '2HO1A', 'Shape', 'electron_density');
%
% -------------------------------------------------------------
% --- parse input and set parameters ---
% Set default values for parameters
default_Order = 20;
default_GridRes = 64;
default_FunEvals = 300;
expected_Shapes = {'SAS','MS','vdw','electron_density'};
default_Shape = 'MS';
default_ProbeRadius = 1.4;
default_SmearFactor = 0.3;
default_ShellThickness = 2;
default_ChiCoeffPath = fullfile(pwd,'chi_coefficients');
default_includeHetatoms = false;
default_includeHatoms = false;
default_chainID = 'all';
default_altLocID = 'A';
expected_ShowLog = {'basic', 'standard', 'detailed', 'none'};
default_LogOption = 'standard';
default_AlignLater = false;
default_inputParserOnly = false;
default_searchSaveFactor = 1.01;
default_rot = ''; % = no rotating structure -> zeal computes shape descriptors for fixed only
% Setup input parser
p = inputParser;
addRequired(p, 'fix');
addOptional(p, 'rot', default_rot);
addOptional(p, 'Order', default_Order, @(x)validateattributes(x,{'numeric'}, {'nonempty','integer', 'positive'}, 'Order'));
addOptional(p, 'GridRes', default_GridRes, @(x)validateattributes(x,{'numeric'}, {'nonempty','integer', '>=', 32}, 'GridRes'));
addOptional(p, 'FunEvals', default_FunEvals, @(x)validateattributes(x,{'numeric'}, {'nonempty','integer', 'positive'}, 'Order'));
addOptional(p, 'ShapeFunction', default_Shape, @(x) any(validatestring(x,expected_Shapes)));
addOptional(p, 'ProbeRadius', default_ProbeRadius, @(x)validateattributes(x,{'numeric'}, {'nonempty', 'positive'}, 'ProbeRadius'));
addOptional(p, 'SmearFactor', default_SmearFactor, @(x)validateattributes(x,{'numeric'}, {'nonempty', 'positive', '>',0, '<', 1}, 'SmearFactor'));
addOptional(p, 'ShellThickness', default_ShellThickness, @(x)validateattributes(x,{'numeric'}, {'nonempty','integer', 'positive'}, 'ShellThickness'));
addOptional(p, 'fix_includeHetatoms', default_includeHetatoms, @(x)validateattributes(x,{'numeric', 'logical'}, {'nonempty'}, 'fix_includeHetatoms'));
addOptional(p, 'fix_includeHatoms', default_includeHatoms, @(x)validateattributes(x,{'numeric', 'logical'}, {'nonempty'}, 'fix_includeHatoms'));
addOptional(p, 'fix_chainID', default_chainID);
addOptional(p, 'fix_altLocID', default_altLocID);
addOptional(p, 'fix_residueNumbers', []);
addOptional(p, 'rot_includeHetatoms', default_includeHetatoms, @(x)validateattributes(x,{'numeric', 'logical'}, {'nonempty'}, 'rot_includeHetatoms'));
addOptional(p, 'rot_includeHatoms', default_includeHatoms, @(x)validateattributes(x,{'numeric', 'logical'}, {'nonempty'}, 'rot_includeHatoms'));
addOptional(p, 'rot_chainID', default_chainID);
addOptional(p, 'rot_altLocID', default_altLocID);
addOptional(p, 'rot_residueNumbers', []);
addOptional(p, 'ChiCoeffPath', default_ChiCoeffPath);
addOptional(p, 'AlignLater', default_AlignLater);
addOptional(p, 'InputParserOnly', default_inputParserOnly);
addOptional(p, 'LogLevel', default_LogOption, @(x) any(validatestring(x,expected_ShowLog)));
addOptional(p, 'SearchSaveFactor', default_searchSaveFactor);
addOptional(p, 'PCAalign', false);
% Parse and set defaults
parse(p, fix, varargin{:});
% --- Assign parameters ---
obj.Settings.Order = p.Results.Order;
obj.Settings.GridRes = p.Results.GridRes;
obj.Settings.ChiCoeffPath = p.Results.ChiCoeffPath;
obj.Settings.molShape.FunctionType = p.Results.ShapeFunction;
obj.Settings.molShape.GridRes = p.Results.GridRes;
obj.Settings.molShape.ProbeRadius = p.Results.ProbeRadius;
obj.Settings.molShape.SmearFactor = p.Results.SmearFactor;
obj.Settings.molShape.ShellThickness = p.Results.ShellThickness;
obj.Settings.molShape.PCAalign = p.Results.PCAalign;
obj.AlignLater = p.Results.AlignLater;
obj.InputParserOnly = p.Results.InputParserOnly;
obj.Search.Performed = false;
obj.Search.EulerAngles = [0 0 0];
obj.Logging.Display = p.Results.LogLevel;
obj.Settings.molShape.ShowLog = false;
obj.Search.History.SearchSaveFactor = p.Results.SearchSaveFactor;
switch obj.Logging.Display
case 'basic'
obj.Logging.Level = 1;
case 'standard'
obj.Logging.Level = 2;
case 'detailed'
obj.Logging.Level = 3;
obj.Settings.molShape.ShowLog = true;
obj.fixed.Selection.ShowLog = true;
obj.rotating.Selection.ShowLog = true;
case 'none'
obj.Logging.Level = 0;
end
% Set surrogate optimization options
obj.Settings.SurrOpt = optimoptions('surrogateopt');
obj.Settings.SurrOpt.MaxFunctionEvaluations = double(p.Results.FunEvals);
obj.Settings.SurrOpt.PlotFcn = [];
obj.Settings.SurrOpt.Display = 'none';
obj.Settings.SurrOpt.OutputFcn = @(a1,a2,a3)surrOptiOutputFcn(a1,a2,a3,obj);
obj.Settings.EulerAngles.LowerBound = [0 0 0];
obj.Settings.EulerAngles.UpperBound = [2*pi pi 2*pi];
obj.Settings.EulerAngles.convention = 'zyz';
% Fix
obj.fixed.Name = p.Results.fix;
obj.fixed.Selection.includeHetatoms = p.Results.fix_includeHetatoms;
obj.fixed.Selection.includeHatoms = p.Results.fix_includeHatoms;
obj.fixed.Selection.chainID = p.Results.fix_chainID;
obj.fixed.Selection.altLocID = p.Results.fix_altLocID;
obj.fixed.Selection.residueNumbers = p.Results.fix_residueNumbers;
if ~obj.InputParserOnly
if isempty(p.Results.rot)
obj.AlignMode = false;
if obj.Logging.Level > 0
fprintf('\n\n Running ZEAL in single mode');
end
else % rot structure given -> align mode activated
obj.AlignMode = true;
if obj.Logging.Level > 0
fprintf('\n\n Running ZEAL in Align mode');
end
end
% --- Import structures ---
if obj.Logging.Level > 0
fprintf('\n - Importing fixed structure: %s', obj.fixed.Name);
end
obj.fixed.PDB = PDB(obj.fixed.Name, obj.fixed.Selection);
obj.fixed.Rg = computeRadiusOfGyration(obj, obj.fixed.PDB.Data);
if obj.AlignMode
% Set PDB parameters for rotating if align mode activated
obj.rotating.Name = p.Results.rot;
obj.rotating.Selection.includeHetatoms = p.Results.rot_includeHetatoms;
obj.rotating.Selection.includeHatoms = p.Results.rot_includeHatoms;
obj.rotating.Selection.chainID = p.Results.rot_chainID;
obj.rotating.Selection.altLocID = p.Results.rot_altLocID;
obj.rotating.Selection.residueNumbers = p.Results.rot_residueNumbers;
if obj.Logging.Level > 0
fprintf('\n - Importing rotating structure: %s', obj.rotating.Name);
end
obj.rotating.PDB = PDB(obj.rotating.Name, obj.rotating.Selection);
obj.rotating.Rg = computeRadiusOfGyration(obj, obj.rotating.PDB.Data);
end
% --- Compute shape functions ---
if obj.Logging.Level > 0
fprintf('\n - Computing shape function for fixed structure');
end
obj.fixed.molShape = molShape(obj.fixed.PDB.Data, obj.Settings.molShape);
if obj.AlignMode
if obj.Logging.Level > 0
fprintf('\n - Computing shape function for rotating structure');
end
obj.rotating.molShape = molShape(obj.rotating.PDB.Data, obj.Settings.molShape);
end
% --- Computing ZC moments ---
if obj.Logging.Level > 0
fprintf('\n - Computing ZC moments for fixed structure');
end
% Reload Chi Coeffs. if expansion order differs from order in
% singelton property (ChiCoeffs)
if ~isequal(obj.Settings.Order, obj.ChiCoeffs.Order)
obj.reloadChiCoeffs('Order',obj.Settings.Order);
end
obj.fixed.ZC = ZC(obj.fixed.molShape.FunctionData, obj.Settings.Order, obj.ChiCoeffs, 'ShowLog', obj.Settings.molShape.ShowLog);
computeDescriptors(obj.fixed.ZC);
if obj.AlignMode
if obj.Logging.Level > 0
fprintf('\n - Computing ZC moments for rotating structure');
end
obj.rotating.ZC = ZC(obj.rotating.molShape.FunctionData, obj.Settings.Order, obj.ChiCoeffs, 'ShowLog', obj.Settings.molShape.ShowLog);
computeDescriptors(obj.rotating.ZC);
% Compute the Euclidean distance between shape descriptors
computeZCDdistance(obj);
% Compute the initial ZEAL score
obj.Search.InitialScore = computeScore(obj);
if ~obj.AlignLater
% Perform shape alignment
shapeAlign(obj);
end
end
if obj.Logging.Level > 0
fprintf('\n');
end
end
end
function shapeAlign(obj)
%shapeAlign Start searching for best shape alingment using
% the surrogate optimization algorithm.
[ obj.Search.SurrOptOut.EulerAngles, ...
obj.Search.SurrOptOut.Score, ...
obj.Search.SurrOptOut.exitflag, ...
obj.Search.SurrOptOut.output, ...
obj.Search.SurrOptOut.trials] = surrogateopt(@objFun, ...
obj.Settings.EulerAngles.LowerBound, ...
obj.Settings.EulerAngles.UpperBound, ...
obj.Settings.SurrOpt);
obj.Score = -1*obj.Search.SurrOptOut.Score;
obj.Search.Performed = true;
obj.Search.EulerAngles = obj.Search.SurrOptOut.EulerAngles;
obj.Search.Score = -1*obj.Search.SurrOptOut.Score;
function [d] = objFun(x)
% objFun The objective function, i.e. the "expensive" ZEAL
% score function
% get rotation matrix from Euler angles
R = ZEAL.euler2rotMat(x);
% rotate orignal atom coordinates
atomlistRot_rotated = obj.rotating.molShape.AtomData;
atomlistRot_rotated(:,1:3) = atomlistRot_rotated(:,1:3) * R;
% Compute new shape function
updateShapeFunction(obj, atomlistRot_rotated);
% Compute new ZC moments
computeMoments(obj.rotating.ZC)
% Compute new ZEALscore
d = -1*computeScore(obj);
end
end
function updateShapeFunction(obj, atomList)
% updateShapeFunction Computes new shape function from
% transformed atom coordinates (atomList)
switch obj.rotating.molShape.FunctionType
case 'MS' % molecular surface
[obj.rotating.ZC.ShapeFunction, ~] = molShape.MSsurface(atomList, obj.Settings.molShape.GridRes, obj.Settings.molShape.ProbeRadius, obj.Settings.molShape.ShellThickness);
case 'SAS' % solvent accessible surface
[obj.rotating.ZC.ShapeFunction, ~] = molShape.SAsurface(atomList, obj.Settings.molShape.GridRes, obj.Settings.molShape.ProbeRadius, obj.Settings.molShape.ShellThickness);
case 'vdw' % van der Waals surface
probeRadius = 0;
[obj.rotating.ZC.ShapeFunction, ~] = molShape.SAsurface(atomList, obj.Settings.molShape.GridRes, probeRadius, obj.Settings.molShape.ShellThickness);
otherwise % electron density
[obj.rotating.ZC.ShapeFunction, ~, ~] = molShape.electronDensity(atomLista, obj.Settings.molShape.GridRes, obj.Settings.molShape.SmearFactor);
end
end
function updateShapeFunction_fixed(obj, atomList)
% updateShapeFunction Computes new shape function from
% transformed atom coordinates (atomList)
switch obj.fixed.molShape.FunctionType
case 'MS' % molecular surface
[obj.fixed.ZC.ShapeFunction, ~] = molShape.MSsurface(atomList, obj.Settings.molShape.GridRes, obj.Settings.molShape.ProbeRadius, obj.Settings.molShape.ShellThickness);
case 'SAS' % solvent accessible surface
[obj.fixed.ZC.ShapeFunction, ~] = molShape.SAsurface(atomList, obj.Settings.molShape.GridRes, obj.Settings.molShape.ProbeRadius, obj.Settings.molShape.ShellThickness);
case 'vdw' % van der Waals surface
probeRadius = 0;
[obj.fixed.ZC.ShapeFunction, ~] = molShape.SAsurface(atomList, obj.Settings.molShape.GridRes, probeRadius, obj.Settings.molShape.ShellThickness);
otherwise % electron density
[obj.fixed.ZC.ShapeFunction, ~, ~] = molShape.electronDensity(atomLista, obj.Settings.molShape.GridRes, obj.Settings.molShape.SmearFactor);
end
end
function stop = surrOptiOutputFcn(x,optimvalues,state, obj)
% surrOptiOutputFcn Output function that is called
% during the surrogate optimization.
stop = false;
switch state
case 'init' % surrogateopt is starting up
if obj.Logging.Level > 0
fprintf('\n\n /////////////////////////////////////////////////////////////////////////////\n');
fprintf('\n\tSearching for best shape superposition\n');
fprintf('\n\tStopping after %d function evaluations\n', obj.Settings.SurrOpt.MaxFunctionEvaluations);
end
if obj.Logging.Level > 1
fprintf('\n ----------------------------------------------------------------------------');
fprintf('\n Best score Euler (zyz) iteration time (s) ');
fprintf('\n ----------------------------------------------------------------------------');
end
obj.Search.History.Iteration = [];
obj.Search.History.Score = [];
obj.Search.History.EulerAngles = [];
obj.Search.History.prev_val = 0;
obj.Search.History.startTime = tic;
obj.Search.Timings = zeros(optimvalues.funccount);
obj.Search.ResetCount = zeros(optimvalues.funccount);
obj.Search.currFlag = cell(optimvalues.funccount);
case 'iter' % surrogateopt is running
searchTime = toc(obj.Search.History.startTime);
obj.Search.Timings(optimvalues.iteration+1) = searchTime;
obj.Search.ResetCount(optimvalues.iteration+1) = optimvalues.surrogateResetCount;
obj.Search.currFlag(optimvalues.iteration+1) = {optimvalues.currentFlag};
if optimvalues.fval < (obj.Search.History.prev_val*obj.Search.History.SearchSaveFactor)
obj.Search.History.prev_val = optimvalues.fval;
obj.Search.History.Iteration = [obj.Search.History.Iteration optimvalues.iteration];
obj.Search.History.EulerAngles = [obj.Search.History.EulerAngles; x];
obj.Search.History.Score = [obj.Search.History.Score -1*optimvalues.fval];
obj.Search.History.BestScore = -1*optimvalues.fval;
obj.Search.History.BestTime = searchTime;
obj.Search.History.BestIteration = optimvalues.iteration;
obj.Search.History.BestEulerAngles = x;
if obj.Logging.Level > 1
fprintf('\n\t %2.2f \t\t %2.2f %2.2f %2.2f \t %d \t\t %4.1f', obj.Search.History.BestScore, x(1), x(2), x(3), obj.Search.History.BestIteration, obj.Search.History.BestTime);
end
end
case 'done' % surrogateopt is finished
searchTime = toc(obj.Search.History.startTime);
obj.Search.History.Iteration = [obj.Search.History.Iteration optimvalues.iteration];
obj.Search.History.EulerAngles = [obj.Search.History.EulerAngles; x];
obj.Search.History.Score = [obj.Search.History.Score -1*optimvalues.fval];
obj.Search.Optimvalues = optimvalues;
if obj.Logging.Level > 0
fprintf('\n ----------------------------------------------------------------------------');
fprintf('\n\n\tSearch completed after %3.1f s.\n', searchTime);
fprintf('\n\tBest score %2.2f found after %d iterations (%3.1f s)\n', obj.Search.History.BestScore, obj.Search.History.BestIteration, obj.Search.History.BestTime);
fprintf('\n\tusing Euler angles (zyz) [%3.3f %3.3f %3.3f]', obj.Search.History.BestEulerAngles(1), obj.Search.History.BestEulerAngles(2), obj.Search.History.BestEulerAngles(3));
fprintf('\n\n /////////////////////////////////////////////////////////////////////////////\n');
end
stop = true;
end
end
function reloadChiCoeffs(obj, varargin)
% reloads chicoeffs using the master class ChiCoeff
obj.ChiCoeffs.loadData(obj, varargin{:}); %all instances of ZEAL will see the newly loaded chi coeffs
end
function Score = computeScore(obj)
% computeScore Compute the ZEAL score for the shape superposition.
% The ZEAL score is the ZC moment correlation between the fixed and the
% rotating structure, computed as the cosine of the angle
% between the N-dimensional moment-vectors. Thus, cos(small
% angle) corrsponds to high moment-correlation = the ZEAL
% score. Because the moments are complex, the can angle can be defined
% differently. Here we use the so called Euclidean angle, but a so called
% Hermitian angle can also be defined.
% ZEAL score = Cos(Euclidean angle)
Score = real(dot(obj.rotating.ZC.Moments.Values, obj.fixed.ZC.Moments.Values)) / (norm(obj.rotating.ZC.Moments.Values)*norm(obj.fixed.ZC.Moments.Values));
% Hermitian angle
% d = -1*abs(dot(ZC_mom_list, ZCref.mom_list)) / (norm(ZC_mom_list)*norm(ZCref.mom_list));
end
function computeZCDdistance(obj)
% computeZCDdistance Computes the Euclidean distance between
% shape descriptors of the fixed and rotating structure
%
% The distance is computed as the 2-norm of the difference
% between the vector elements containing the shape-descriptor
% values.
if isempty(obj.fixed.ZC.Descriptors)
computeDescriptors(obj.fixed.ZC);
end
if isempty(obj.rotating.ZC.Descriptors)
computeDescriptors(obj.rotating.ZC);
end
obj.ZCDdistance = norm(obj.fixed.ZC.Descriptors - obj.rotating.ZC.Descriptors, 2);
end
function saveSuccessFul = save2pdb(obj,varargin)
% save2pdb Export strucure to pdb file
% By default, if both structures exist in the object (fixed and rotated)
% then those are exported to pdbfiles in the current folder.
% The fixed strucure is exported if ZEAL is run in 'single
% structure' mode (i.e. shape analysis of one structure = the fixed structure).
% OPTIONAL INPUT
% 'name'-value pairs : type : default value {expected}
%
% 'structure' : char : 'all' {'fixed','rotating','all'}
%
% 'includeHetatoms' : logical : false {true/false}
%
% 'includeAll' : logical : false {true/false}
%
% 'folderPath' : char : current directory
%
% 'fixName' : char : <source_name>_ZEAL.pdb
%
% 'rotName' : char : <source_name>_ZEAL.pdb
expectedStructures = {'fixed', 'rotating', 'all'};
% set defaults
defaultPath = pwd;
defaultHetatoms = false;
defaultAll = false;
defaultFixName = [obj.fixed.PDB.Source.Name '_ZEAL.pdb'];
if ~isempty(obj.rotating)
defaultStructure = 'all';
defaultRotName = [obj.rotating.PDB.Source.Name '_ZEAL.pdb'];
else
defaultStructure = 'fixed';
defaultRotName = '';
end
% do option parsing
p = inputParser;
addOptional(p, 'structure', defaultStructure, @(x) any(validatestring(x,expectedStructures)));
addOptional(p, 'includeHetatoms', defaultHetatoms);
addOptional(p, 'includeAll', defaultAll);
addOptional(p, 'fixName', defaultFixName);
addOptional(p, 'rotName', defaultRotName);
addOptional(p, 'folderPath', defaultPath);
parse(p, varargin{:});
selectedStructure = p.Results.structure;
includeHetatoms = p.Results.includeHetatoms;
includeAll = p.Results.includeAll;
folderpath = p.Results.folderPath;
fixName = p.Results.fixName;
rotName = p.Results.rotName;
try
if strcmp(selectedStructure,'fixed') || strcmp(selectedStructure,'all')
selection = obj.fixed.PDB.Selection;
if includeHetatoms || includeAll
selection.includeHetatoms = includeHetatoms;
end
if includeAll
selection.chainID = 'all';
selection.altLocID = 'all';
selection.residueNumbers = [];
end
T = getTranslationMatrix(obj, 'fixed');
R = getRotationMatrix(obj, 'fixed');
pdbData = PDB.parsePDBstruct(obj.fixed.PDB.AllData, selection);
xyz = [pdbData.X pdbData.Y pdbData.Z];
xyzFix = [ xyz ones(length(xyz),1)] * T * R;
pdbData.X = xyzFix(:,1);
pdbData.Y = xyzFix(:,2);
pdbData.Z = xyzFix(:,3);
filePath=fullfile(folderpath, fixName);
exportPdb(obj, 'fixed', pdbData, filePath)
end
if strcmp(selectedStructure,'rotating') || strcmp(selectedStructure,'all')
selection = obj.rotating.PDB.Selection;
if includeHetatoms || includeAll
selection.includeHetatoms = includeHetatoms;
end
if includeAll
selection.chainID = 'all';
selection.altLocID = 'all';
selection.residueNumbers = [];
end
T = getTranslationMatrix(obj, 'rotating');
R = getRotationMatrix(obj, 'rotating');
pdbData = PDB.parsePDBstruct(obj.rotating.PDB.AllData, selection);
xyz = [pdbData.X pdbData.Y pdbData.Z];
xyzRot = [ xyz ones(length(xyz),1)] * T * R;
pdbData.X = xyzRot(:,1);
pdbData.Y = xyzRot(:,2);
pdbData.Z = xyzRot(:,3);
filePath=fullfile(folderpath, rotName);
exportPdb(obj, 'rotating', pdbData, filePath)
end
saveSuccessFul = true;
catch ME
saveSuccessFul = false;
rethrow(ME)
end
end
function T = getTranslationMatrix(obj, structure)
switch structure
case 'fixed'
XYZ = [obj.fixed.PDB.Data.X obj.fixed.PDB.Data.Y obj.fixed.PDB.Data.Z];
case 'rotating'
XYZ = [obj.rotating.PDB.Data.X obj.rotating.PDB.Data.Y obj.rotating.PDB.Data.Z];
end
COM = mean(XYZ);
T = eye(4);
T(end, :) = [-COM 1];
end
function R = getRotationMatrix(obj, structure)
R = eye(4);
switch structure
case 'fixed'
R(end,end) = 0;
case 'rotating'
R(1:3,1:3) = ZEAL.euler2rotMat(obj.Search.EulerAngles);
end
end
function exportPdb(obj, structure, pdbData, fileSavePath)
% Save pdbdata to pdbfile with ZEAL info in REMARK fields (if 2 structures loaded)
%
% INPUT
% typr expected value
% <structure> : char : {'fixed', 'rotating'}
% Structure to save
%
% <pdbData> : struct : structure from PDB class
%
% <fileSavePath> : char
% path to the file that we create and write data data
% get file name from path
[~, name, ~] = fileparts(fileSavePath);
fprintf('outputting PDB in file %s.pdb ...', name);
% create and open file
fid = fopen(fileSavePath, 'w');
if fid == -1
error('Author:Function:OpenFile', 'Cannot open file: %s', name);
end
% Write
if ~isempty(obj.fixed) && ~isempty(obj.rotating)
% write settings used for the shape alignment to REMARK record
fprintf(fid, '%-12s ZEAL Protein shape alignment %s\n', 'REMARK' , datestr(now));
fprintf(fid, '%-12s \n', 'REMARK');
if strcmp(structure,'fixed')
fprintf(fid, '%-12s ZEAL Fixed (this file): %s CHAIN %s\n', 'REMARK' , obj.fixed.Name, obj.fixed.PDB.Selection.chainID);
fprintf(fid, '%-12s ZEAL Rotating: %s CHAIN %s\n', 'REMARK' , obj.rotating.Name, obj.fixed.PDB.Selection.chainID);
elseif strcmp(structure,'rotating')
fprintf(fid, '%-12s ZEAL Rotating (this file): %s CHAIN %s\n', 'REMARK' , obj.rotating.Name, obj.rotating.PDB.Selection.chainID);
fprintf(fid, '%-12s ZEAL Fixed: %s CHAIN %s\n', 'REMARK' , obj.fixed.Name, obj.fixed.PDB.Selection.chainID);
end
fprintf(fid, '%-12s ZEAL score: %5.5f \n', 'REMARK' , obj.Score);
fprintf(fid, '%-12s \n', 'REMARK');
fprintf(fid, '%-12s ZEAL SETTINGS\n', 'REMARK');
fprintf(fid, '%-12s ZEAL Maximum order of Zernike-Canterakis moments: %d\n', 'REMARK', obj.Settings.Order);
fprintf(fid, '%-12s ZEAL Grid resolution:\t%d\n', 'REMARK', obj.Settings.GridRes);
fprintf(fid, '%-12s ZEAL Shape function:\t%s\n', 'REMARK', obj.Settings.molShape.FunctionType);
if strcmp(obj.Settings.molShape.FunctionType, 'electron_density')
fprintf(fid, '%-12s ZEAL \t\t\t\t Smear factor: %2.2f\n', 'REMARK', obj.Settings.molShape.SmearFactor);
else
fprintf(fid, '%-12s ZEAL \t\t\t\t Probe radius: %2.2f Å\n', 'REMARK', obj.Settings.molShape.ProbeRadius);
fprintf(fid, '%-12s ZEAL \t\t\t\t Shell thickness: %d\n', 'REMARK', obj.Settings.molShape.ShellThickness);
end
fprintf(fid, '%-12s \n', 'REMARK');
fprintf(fid, '%-12s The 4x4 affine transformation matrix (Tmatrix)\n', 'REMARK');
fprintf(fid, '%-12s to get new coordinates (this file) from original (orig) coordinates\n', 'REMARK');
fprintf(fid, '%-12s \n', 'REMARK');
T = getTranslationMatrix(obj, structure);
R = getRotationMatrix(obj, structure);
transMat = T*R;
fprintf(fid, '%-12s Tmatrix = [\n', 'REMARK');
fprintf(fid, '%-12s \t\t %5.5f %5.5f %5.5f %5.5f\n', 'REMARK', transMat(1,1), transMat(1,2), transMat(1,3), transMat(1,4));
fprintf(fid, '%-12s \t\t %5.5f %5.5f %5.5f %5.5f\n', 'REMARK', transMat(2,1), transMat(2,2), transMat(2,3), transMat(2,4));
fprintf(fid, '%-12s \t\t %5.5f %5.5f %5.5f %5.5f\n', 'REMARK', transMat(3,1), transMat(3,2), transMat(3,3), transMat(3,4));
fprintf(fid, '%-12s \t\t %5.5f %5.5f %5.5f %5.5f\n', 'REMARK', transMat(4,1), transMat(4,2), transMat(4,3), transMat(4,4));
fprintf(fid, '%-12s \t\t\t ] \n', 'REMARK');
fprintf(fid, '%-12s \n', 'REMARK');
fprintf(fid, '%-12s [ new_X new_Y new_Z ] = [ orig_X orig_Y orig_Z 1 ] * Tmatrix \n', 'REMARK');
fprintf(fid, '%-12s \n', 'REMARK');
fprintf(fid, '%-12s \n', 'REMARK');
fprintf(fid, '%-12s Cite: \n', 'REMARK');
fprintf(fid, '%-12s F. Ljung and I. André, \n', 'REMARK');
fprintf(fid, '%-12s ZEAL: Structure alignment based on shape similarity, Bioinformatics (2020) \n', 'REMARK');
fprintf(fid, '%-12s Bioinformatics (2020) \n', 'REMARK');
fprintf(fid, '%-12s \n', 'REMARK');
end
% check integrity of some data fields
nRecords = length(pdbData.atomNum);
checkIfEmpty = @(x) isempty(x);
% occupancy
if isempty(pdbData.occupancy)
pdbData.occupancy = zeros(nRecords,1);
end
% betaFactor
if isempty(pdbData.betaFactor)
pdbData.betaFactor = zeros(nRecords,1);
end
% element
if nRecords == sum(cellfun(checkIfEmpty, pdbData.element))
pdbData.element = cell(nRecords,1);
end
% charge
if nRecords == sum(cellfun(checkIfEmpty, pdbData.charge))
pdbData.charge = cell(nRecords,1);
end
ZEAL.writeModel(fid, pdbData)
fprintf( fid, 'END\n');
% close file
fprintf('\n done! closing file...\n');
fclose(fid);
end
function data = exportTrials(obj, varargin)
p = inputParser;
default_AllTrials = false;
default_onefile = true;
default_folderPath = pwd;
default_sorted = false;
default_includeAll = false;
default_bestFoundSoFar = true;
addOptional(p, 'all', default_AllTrials);
addOptional(p, 'oneFile', default_onefile);
addOptional(p, 'folderPath', default_folderPath);
addOptional(p, 'sorted', default_sorted);
addOptional(p, 'bestFoundSoFar', default_bestFoundSoFar);
addOptional(p,'includeAll', default_includeAll);
parse(p, varargin{:});
allTrials = p.Results.all;
oneFile = p.Results.oneFile;
folderPath = p.Results.folderPath;
sortTrials = p.Results.sorted;
includeAll = p.Results.includeAll;
bestFoundSoFar = p.Results.bestFoundSoFar;
structureName = split(obj.rotating.Name,'/');
structureName = structureName{end};
structureName = split(structureName,'.');
structureName = structureName{1};
% get original PDBdata and parse based on chosen selection
selection = obj.rotating.PDB.Selection;
if includeAll
selection.chainID = 'all';
selection.altLocID = 'all';
selection.residueNumbers = [];
end
pdbData = PDB.parsePDBstruct(obj.rotating.PDB.AllData, selection);
T = getTranslationMatrix(obj, 'rotating');
xyz_original = [pdbData.X pdbData.Y pdbData.Z];
if sortTrials
% sort trials data
[Fval, ind] = sort(-1*obj.Search.SurrOptOut.trials.Fval);
X = obj.Search.SurrOptOut.trials.X(ind,:);
if isequal(allTrials,false)
% remove trials worse than original
init_ind = find((sum(X==0,2)) == 3);
Fval(1:init_ind) = [];
X(1:init_ind,:) = [];
end
else