forked from THSchmidt/lambada-align
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambada
executable file
·1538 lines (1177 loc) · 58 KB
/
lambada
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
#!/usr/bin/perl -w
# Copyright 2012 Thomas H. Schmidt & Christian Kandt
#
# This file is part of LAMBADA.
#
# LAMBADA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# LAMBADA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with LAMBADA; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
### Load Packages & Modules ####################################################
use strict;
#use Cwd;
#use Fcntl;
use IO::Handle;
use Math::Trig;
use FindBin qw($RealBin); # Absolute path to THIS script.
use lib $RealBin . "/modules";
autoflush STDOUT 1; # For direct output (IO:Handle).
use cmdline;
use FileIO::gro;
use protein;
################################################################################
use File::Which;
### Default Parameters #########################################################
our $version = "rc1"; # Version number.
our $year = "2012"; # Year (of change).
our $verbose = 0; # Be loud and noisy (and global); default: silent.
my $protInFile = 'protein.gro'; # Protein coordinates file.
my $protNdxInFile = ''; # Protein NDX file.
my $membInFile = 'membrane.gro'; # Membrane coordinates file. Layer should be in XY plane.
my $membNdxInFile = ''; # Membrane NDX file.
my $groOutFile = 'prot_memb.gro'; # Output GRO file.
my $cgProtGroFile = ''; # Output GRO file of the coarse-grained protein.
my $gridOutFile = ''; # Output GRO file of the grid.
my $orient = 1; # Orient the protein.
my $hsprofOutFile = ''; # Output file of the hydrophilicity-surface score (profile).
my $cavityExcl = 1; # Exclude charged surface residue detection for protein internal volumes.
my $gridDeltaZ = 0.3; # Grid size to detect the area of the protein [nm].
my $lipidResname = '^(D|P)(A|I|L|O|P|U)P[ACEGIS123]?$'; # Regex for the automatic detection of lipid molecules in the membrane GRO file.
my $lsLandscape = 0; # Determine and write out the energy landscape.
my $angRangeLimit = 5; # For protein orientation: stop if recursive refinement of the rotation angle is less than this value.
#my $nMembranes = 1;
my $withPlanes = 0; # Add the planes for highlighting the borders of the hydrophobic range.
my $thicknHphobic = 2.4; # The width of the hydrophobic belt.
#my $membThickness = 4.86; # ...if it could not be measured.
my $isTMProt = 1;
my $helpAndQuit = 0; # Print out program help.
my $xWater = 0; # Enable support of crystal water in the protein structure.
#my @rotAngs = (0, 90, 0, 90);
################################################################################
### Internal parameters ########################################################
my %protData; # Filled by "GROFiles::readGro(<GROFILE>)".
my %membData; # Filled by "GROFiles::readGro(<GROFILE>)".
my @membNdxData; # Filled by "NDXFiles::readNdx(<NDXFILE>)".
my @membGroupIds; # Filled by "NDXFiles::selectGroupIds(...)".
my @selMembAtomIds; # A list of atom IDs encoding the membrane atoms.
my $membZCenter = 0;
my $thicknHphobicUp;
my $thicknHphobicLow;
my $gridRef;
#my $hphobicGridRange;
#my $contProtRef;
my $sliceScoreRef;
my $beltZCenter;
#my $orderedRangesRef;
#my $beltHashRef;
our $xRotAng = 0;
our $yRotAng = 0;
our %absMinScore = ('score' => 50000,
'xRotAng' => 0,
'yRotAng' => 0);
my %cgProtData;
my %cgMembData;
my @tmpArray;
################################################################################
### Print out program headlines ################################################
printHead();
################################################################################
### Handle commandline parameters ##############################################
Cmdline::addCmdlParam('scalar', 'f1', 'Input', \$protInFile, $protInFile, 'Structure file: gro');
Cmdline::addCmdlParam('scalar', 'n1', 'Input, Opt.', \$protNdxInFile, 'protein.ndx', 'Index file');
Cmdline::addCmdlParam('scalar', 'f2', 'Input', \$membInFile, $membInFile, 'Structure file: gro');
Cmdline::addCmdlParam('scalar', 'n2', 'Input, Opt.', \$membNdxInFile, 'membrane.ndx', 'Index file');
Cmdline::addCmdlParam('scalar', 'o', 'Output', \$groOutFile, $groOutFile, 'Structure file: gro');
Cmdline::addCmdlParam('scalar', 'cg', 'Output, Opt.', \$cgProtGroFile, $cgProtGroFile, 'Structure file: gro');
Cmdline::addCmdlParam('scalar', 'grd', 'Output, Opt.', \$gridOutFile, $gridOutFile, 'Structure file: gro');
#Cmdline::addCmdlParam('array', 't', 'Input, Mult.', \@multiArray, 'traj.gro', 'Trajectory: gro'); # NOTE: Just an example.
Cmdline::addCmdlParam('scalar', 'hs', 'Output, Opt.', \$hsprofOutFile, $hsprofOutFile, 'Generic data file');
Cmdline::addCmdlParam('flag', 'h', 'bool', \$helpAndQuit, $helpAndQuit ? 'yes' : 'no', 'Print help info and quit');
Cmdline::addCmdlParam('scalar', 'gz', 'real', \$gridDeltaZ, $gridDeltaZ, 'Grid spacing along the Z axis');
Cmdline::addCmdlParam('flag', 'orient', 'bool', \$orient, $orient ? 'yes' : 'no', 'Orient the protein');
Cmdline::addCmdlParam('flag', 'landscape', 'bool', \$lsLandscape, $lsLandscape ? 'yes' : 'no', 'Write out the LAMBADA score landscape of different rotation angle combinations');
Cmdline::addCmdlParam('scalar', 'anglimit', 'real', \$angRangeLimit, $angRangeLimit, 'For protein orientation: stop if recursive refinement of the rotation angle is less than this value');
#Cmdline::addCmdlParam('scalar', 'nmemb', 'int', \$nMembranes, $nMembranes, 'Number of hydrophobic belts for detection');
Cmdline::addCmdlParam('flag', 'planes', 'bool', \$withPlanes, $withPlanes ? 'yes' : 'no', 'Add dummy atoms representing the membrane plane');
#Cmdline::addCmdlParam('flag', 'tm', 'bool', \$isTMProt, $isTMProt, 'The protein is a transmembrane protein');
Cmdline::addCmdlParam('flag', 'v', 'bool', \$verbose, $verbose ? 'yes' : 'no', 'Be loud and noisy');
Cmdline::addCmdlParam('scalar', 'bw', 'real', \$thicknHphobic, $thicknHphobic, 'Hydrophobic belt width (nm) (obsolete if a membrane NDX file is given)');
Cmdline::addCmdlParam('scalar', 'lr', 'string', \$lipidResname, $lipidResname, 'Regex for the detection of the bilayer atoms of the membrane GRO file (obsolete if a membrane NDX file is given)');
Cmdline::addCmdlParam('flag', 'xwater', 'bool', \$xWater, $xWater ? 'yes' : 'no', 'Keep water in the protein structure');
Cmdline::parser();
################################################################################
############################# Get gromacs tools ################################
my $editconf;
my $make_ndx;
sub prepareGromacsTools {
$editconf = getEditconf();
$make_ndx = getMakeNdx();
}
sub getEditconf {
return getGromacsTool('editconf');
}
sub getMakeNdx {
return getGromacsTool('make_ndx');
}
sub getGromacsTool {
my $toolname = shift;
my $tool = which($toolname);
unless ($tool) {
$tool = which('gmx');
unless(defined $tool) {
print "Neither '$toolname' nor 'gmx' could be found in PATH.\n";
exit 1;
}
$tool .= ' ' . $toolname;
}
return $tool;
}
prepareGromacsTools();
### Modifications after CMDLine ################################################
$thicknHphobicUp = $thicknHphobic/2;
$thicknHphobicLow = $thicknHphobic/2;
################################################################################
### Print program help if the user set the flag ################################
printHelp(Cmdline::getCmdlParamRef(), 1) if $helpAndQuit;
################################################################################
### Read the membrane GRO file #################################################
%membData = GRO::readGro($membInFile); # Read membrane input GRO file.
################################################################################
### Get the membrane NDX data ##################################################
if ($membNdxInFile) {
@membNdxData = NDXFiles::readNdx($membNdxInFile); # Read input NDX file.
NDXFiles::printNdxGroups(@membNdxData);
@membGroupIds = selectGroupIds(\@membNdxData, 'membrane headgroups');
foreach (@membGroupIds) {
push(@selMembAtomIds, @{$membNdxData[$_]{'atoms'}});
}
}
else {
@selMembAtomIds = getAtomIdsByResname($membData{'atoms'}, $lipidResname);
}
################################################################################
### Detect the bilayer thickness ###############################################
die "ERROR: no atoms found to determine the bilayer center.\nSorry...\n" unless @selMembAtomIds;
$membZCenter = getMembZCenter($membData{'atoms'}, \@selMembAtomIds);
#my @tmpArray = getBilayerThickness($membData{'atoms'}, $membNdxData[$membHeadGroupIds[0]]{'atoms'});
#$membThickness = $tmpArray[0];
#my $membZCenter = $tmpArray[1];
################################################################################
### Define the parameters for the protein grid #################################
Protein::setGridDelta(0.1, 0.1, $gridDeltaZ); # Set the grid spacing in x, y & z.
################################################################################
### Orient the protein #########################################################
if ($orient) {
### Rotate the protein along its principal axis ############################
unless ($protNdxInFile) {
$protNdxInFile = $protInFile;
unless($protNdxInFile =~ s/\.(gro|pdb)$/.ndx/) {
$protNdxInFile .= '.ndx';
}
}
`echo q | $make_ndx -f $protInFile -o $protNdxInFile`;
if ($xWater) {
`echo 0 3 0 | $editconf -f $protInFile -n $protNdxInFile -d 8 -princ -o oriented.princ.gro 1>> editconf.log 2>> editconf.log`;
}
else {
`echo 1 3 1 | $editconf -f $protInFile -n $protNdxInFile -d 8 -princ -o oriented.princ.gro 1>> editconf.log 2>> editconf.log`;
}
############################################################################
### Load the GRO file ######################################################
$protInFile = "oriented.princ.gro";
%protData = GRO::readGro($protInFile); # Read protein input GRO file.
############################################################################
### Build protein CG coordinates set #######################################
$cgProtData{'title'} = $protData{'title'};
$cgProtData{'box'} = $protData{'box'};
$cgProtData{'atoms'} = buildCgProt($protData{'atoms'});
$cgProtData{'atoms'} = renumAtoms($cgProtData{'atoms'});
$cgProtData{'nAtoms'} = scalar(@{$cgProtData{'atoms'}}) - 1;
GRO::writeGro($cgProtGroFile, \%cgProtData) if $cgProtGroFile;
############################################################################
### Extract the CG atom IDs ################################################
my @cgProtAtomIds;
for (my $i=1; $i<@{$cgProtData{'atoms'}}; $i++) {
push(@cgProtAtomIds, $i);
}
my %rotPoint = getGeoCenter(\@cgProtAtomIds, $cgProtData{'atoms'});
############################################################################
### Find recursively the orientation with the least score ##################
if ($lsLandscape) {
getLeastScoreLandscape(\@cgProtAtomIds, $cgProtData{'atoms'}, \%rotPoint); # Write out the values for the rotation angle landscape.
exit;
}
recRotXY2MinScore(\@cgProtAtomIds, $cgProtData{'atoms'}, \%rotPoint, 90, 90, 90, $angRangeLimit);
############################################################################
### Rotate the atomistic model #############################################
`echo q | $make_ndx -f oriented.princ.gro -o temp.ndx`;
if ($xWater) {
`echo 3 0 | $editconf -f oriented.princ.gro -n temp.ndx -d 2 -rotate $absMinScore{'xRotAng'} $absMinScore{'yRotAng'} 0 -o oriented.final.gro 1>> editconf.log 2>> editconf.log`;
}
else {
`echo 3 1 | $editconf -f oriented.princ.gro -n temp.ndx -d 2 -rotate $absMinScore{'xRotAng'} $absMinScore{'yRotAng'} 0 -o oriented.final.gro 1>> editconf.log 2>> editconf.log`;
}
`rm temp.ndx`;
############################################################################
### Load the GRO file ######################################################
%protData = GRO::readGro('oriented.final.gro'); # Read protein input GRO file.
############################################################################
}
else {
### Load the GRO file ######################################################
%protData = GRO::readGro($protInFile);
############################################################################
}
################################################################################
### Build protein CG coordinates set ###########################################
$cgProtData{'title'} = $protData{'title'};
$cgProtData{'box'} = $protData{'box'};
$cgProtData{'atoms'} = buildCgProt($protData{'atoms'});
$cgProtData{'atoms'} = renumAtoms($cgProtData{'atoms'});
$cgProtData{'nAtoms'} = (scalar(@{$cgProtData{'atoms'}}) - 1);
GRO::writeGro('final.cgprot.gro', \%cgProtData) if $cgProtGroFile;
################################################################################
### Build protein grid #########################################################
my @cgProtAtomIds;
for (my $i=1; $i<@{$cgProtData{'atoms'}}; $i++) {
push(@cgProtAtomIds, $i);
}
@tmpArray = Protein::analyze(\@cgProtAtomIds,
$cgProtData{'atoms'});
$gridRef = $tmpArray[2];
Protein::grid2GroFile('final.grid.gro') if $gridOutFile; # Print grid-coordinates.
################################################################################
### Calculate the score for each slice along the Z axis (HS) ###################
$sliceScoreRef = getSliceScore($gridRef, $gridDeltaZ, $hsprofOutFile);
################################################################################
### Determine the hydophobic belt ##############################################
my %belt = detectBelts($sliceScoreRef, $gridDeltaZ, $thicknHphobicUp, $thicknHphobicLow, $thicknHphobic, $isTMProt);
################################################################################
#$sliceScoreRef = getSliceScore($gridRef, $gridDeltaZ, $contProtRef, $hsprofOutFile);
$beltZCenter = $belt{'hphobCent'} * $gridDeltaZ if $belt{'hphobCent'};
my @protAtomIds = getAtomIds($protData{'atoms'});
my @membAtomIds = getAtomIds($membData{'atoms'});
#### Translate all protein atoms to the center of the membrane box #############
centerGroup(\@protAtomIds, $protData{'atoms'}, $membData{'box'});
################################################################################
### For validation only: build a couple of dummy atoms #########################
buildDummyPlane($protData{'atoms'}, \@protAtomIds, $beltZCenter, $thicknHphobicUp, $thicknHphobicLow) if $withPlanes;
################################################################################
unless (defined $beltZCenter) {
print "Sorry, don't found a hydrophobic belt...\n";
exit;
}
### Translate all bilayer atoms to the center (z-axis) of the hydrophob. belt. #
my $zTranslVec = $beltZCenter - $membZCenter;
# print $zTranslVec . " $membZCenter $beltZCenter\n";
zTranslateGroup(\@membAtomIds, $membData{'atoms'}, $zTranslVec);
################################################################################
### Combine system components and write out the coordinates ####################
my %combData = combGroData(\%protData, \%membData);
GRO::writeGro($groOutFile, \%combData);
exit;
################################################################################
################################################################################
### Subroutines ################################################################
################################################################################
sub printHead {
print <<EndOfHead;
################################################################################
LAMBADA $version
Orient, align and combine membrane protein systems
Copyright Thomas H. Schmidt & Christian Kandt, $year
http://code.google.com/p/lambada-align
LAMBADA comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; type `-copyright' for details.
################################################################################
EndOfHead
}
sub printFoot {
print <<EndOfFoot;
Please cite:
[1] Schmidt, T. H. & Kandt C. LAMBADA & InflateGRO2: Efficient Membrane Alignment
and Insertion of Membrane Proteins for Molecular Dynamics Simulations.
J. Chem. Inf. Model. (2012).[http://dx.doi.org/10.1021/ci3000453]
EndOfFoot
}
sub printHelp {
my $cmdLParamRef = shift;
my $quitAfterPrint = shift;
print <<EndOfHelp;
DESCRIPTION
-----------
LAMBADA reads the coordinate files of a membrane protein and a lipid bilayer.
The protein is oriented according to its hydrophobic belt planes parallel to the
XY plane of the simulation box. The membrane is then aligned on the level
of the hydrophobic belt. Hence, the input structure of the lipid bilayer has
to be in the XY plane. The combined system is put out ready for protein
insertion (e.g. using InflateGRO2).
USAGE: lambada -f1 PROTEINGROFILE -f2 MEMBRANEGROFILE -o OUTPUTGROFILE
EndOfHelp
Cmdline::printParamHelp($cmdLParamRef);
printFoot();
exit if $quitAfterPrint;
}
sub printCopyright {
print <<"EndOfCopyright";
LAMBADA is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
LAMBADA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with LAMBADA; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
EndOfCopyright
exit;
}
sub getLeastScoreLandscape {
my $rotAtomIdsRef = shift;
my $atomCoordsRef = shift;
my $rotPointRef = shift;
my $angRange = 5;
my $xStart = 0;
my $xEnd = 180;
my $yStart = 0;
my $yEnd = 180;
my @minScores;
# $angRange = 1 if $angRange < 2;
open(ANGFILE, ">landscape." . $angRange . "deg.dat") || die "ERROR: Cannot open \"landscape." . $angRange . "deg.dat\": $!\n";
print "\nChecking range from x = ($xStart to $xEnd) and y = ($yStart to $yEnd) (angle range $angRange)\n";
my %xRotAxis = ('cooX' => 10000, 'cooY' => 0, 'cooZ' => 0);
my %yRotAxis = ('cooX' => 0, 'cooY' => 10000, 'cooZ' => 0);
### ROTATION ###############################################################
for (my $xRotAng=$xStart; $xRotAng<=$xEnd; $xRotAng+=$angRange) {
### Rotate the protein around the x axis ###############################
my @rotatedCoords = @{$atomCoordsRef};
@rotatedCoords = rotateAroundVec($atomCoordsRef, \%xRotAxis, $rotPointRef, $xRotAng) unless $xRotAng == 0;
########################################################################
for (my $yRotAng=$yStart; $yRotAng<=$yEnd; $yRotAng+=$angRange) {
### Rotate the protein around the y axis ###########################
my @rotatedCoords2 = @rotatedCoords;
@rotatedCoords2 = rotateAroundVec(\@rotatedCoords, \%yRotAxis, $rotPointRef, $yRotAng) unless $yRotAng == 0;
####################################################################
### Build the rotated CG model #####################################
my %rotProtData;
$rotProtData{'box'} = $cgProtData{'box'};
$rotProtData{'nAtoms'} = (scalar(@rotatedCoords2) - 1);
$rotProtData{'title'} = $cgProtData{'title'};
$rotProtData{'atoms'} = \@rotatedCoords2;
# my @rotProtAtomIds = getAtomIds($rotProtData{'atoms'});
# GRO::writeGro(sprintf("rotated.%02d-%02d.gro", $xRotAng, $yRotAng), \%rotProtData);
####################################################################
### Build protein grid #############################################
$gridRef = 0;
@tmpArray = Protein::analyze($rotAtomIdsRef,
\@rotatedCoords2);
$gridRef = $tmpArray[2];
# Protein::grid2GroFile($gridOutFile) if $gridOutFile; # Print grid-coordinates.
####################################################################
### Count the z distribution (hydrophilicity profile) ##############
$sliceScoreRef = getSliceScore($gridRef, $gridDeltaZ, $hsprofOutFile);
####################################################################
### Determine the hydophobic belts #################################
my %belt = detectBelts($sliceScoreRef, $gridDeltaZ, $thicknHphobicUp, $thicknHphobicLow, $thicknHphobic, $isTMProt);
next unless $belt{'score'};
$beltZCenter = $belt{'hphobCent'} * $gridDeltaZ if $belt{'hphobCent'};
my @rotProtAtomIds = getAtomIds($rotProtData{'atoms'});
buildDummyPlane($rotProtData{'atoms'}, \@rotProtAtomIds, $beltZCenter, $membData{'box'});
$rotProtData{'nAtoms'} = (scalar(@{$rotProtData{'atoms'}}) - 1);
GRO::writeGro(sprintf("rotated.%02d-%02d.gro", $xRotAng, $yRotAng), \%rotProtData);
my %tmpHash = ('xRotAng' => $xRotAng, 'yRotAng' => $yRotAng, 'score' => $belt{'score'});
%absMinScore = %tmpHash if $absMinScore{'score'} > $belt{'score'};
push(@minScores, \%tmpHash);
print ANGFILE "$xRotAng $yRotAng $belt{'score'}\n";
print " MinScore: " . $belt{'score'} . " (x = $xRotAng, y = $yRotAng)" if $main::verbose;
print " (Abs. MinScore = " . $absMinScore{'score'} . " (x = " . $absMinScore{'xRotAng'} . ", y = " . $absMinScore{'yRotAng'} . "))\n" if $main::verbose;
undef %belt;
# print " Abs. MinScore: " . $minHs{'score'} . " x = $minHs{'xRotAng'}, y = $minHs{'yRotAng'}\n";
####################################################################
}
}
close(ANGFILE);
############################################################################
### Sort all found least scores (ASC) ######################################
return unless @minScores;
my @sortedMinScores = sort { $a->{'score'} <=> $b->{'score'} } @minScores;
my $minScore = int($sortedMinScores[0]{'score'});
foreach (@sortedMinScores) {
next if int($$_{'score'}) > $minScore;
# print "-> Going deeper with score " . $$_{'score'} . " at x = $$_{'xRotAng'}, y = $$_{'yRotAng'} (angle range $angRange)\n";
# recRotXY2MinScore($rotAtomIdsRef, $atomCoordsRef, $rotPointRef, $$_{'xRotAng'}, $$_{'yRotAng'}, $angRange/2);
# print "<- Going upper with score " . $$_{'score'} . " at x = $$_{'xRotAng'}, y = $$_{'yRotAng'} (angle range $angRange)\n";
}
############################################################################
}
sub recRotXY2MinScore {
my $rotAtomIdsRef = shift;
my $atomCoordsRef = shift;
my $rotPointRef = shift;
my $middleXRotAng = shift;
my $middleYRotAng = shift;
my $angRange = shift;
my $angRangeLimit = shift;
return if $angRange < $angRangeLimit;
my $xStart = $middleXRotAng - $angRange;
my $xEnd = $middleXRotAng + $angRange;
my $yStart = $middleYRotAng - $angRange;
my $yEnd = $middleYRotAng + $angRange;
my @minScores;
# $angRange = 1 if $angRange < 2;
open(ANGFILE, ">ang.$angRange.dat") || die "ERROR: Cannot open \"ang.$angRange.dat\": $!\n";
print "\nChecking range from x = ($xStart to $xEnd) and y = ($yStart to $yEnd) (angle range $angRange)\n";
my %xRotAxis = ('cooX' => 10000, 'cooY' => 0, 'cooZ' => 0);
my %yRotAxis = ('cooX' => 0, 'cooY' => 10000, 'cooZ' => 0);
### ROTATION ###############################################################
for (my $xRotAng=$xStart; $xRotAng<=$xEnd; $xRotAng+=$angRange) {
### Rotate the protein around the x axis ###############################
### Add the principal axis for publication #############################
# if ($angRange == 90 && $xRotAng == 0) {
# my %geoCenter = getGeoCenter($rotAtomIdsRef, $atomCoordsRef);
# my $residue = $$atomCoordsRef[-1]{'residue'};
# my $serial = $$atomCoordsRef[-1]{'serial'};
# for (my $x=$geoCenter{'cooX'}-5; $x<$geoCenter{'cooX'}+5; $x+=1) {
# my %dummyCoords = ('cooX' => $x,
# 'cooY' => $geoCenter{'cooY'},
# 'cooZ' => $geoCenter{'cooZ'});
# push(@{$atomCoordsRef}, setCgAtom(++$residue, 'LIN', 'LIN', ++$serial, \%dummyCoords));
# }
# }
########################################################################
my @rotatedCoords = @{$atomCoordsRef};
@rotatedCoords = rotateAroundVec($atomCoordsRef, \%xRotAxis, $rotPointRef, $xRotAng) unless $xRotAng == 0;
########################################################################
for (my $yRotAng=$yStart; $yRotAng<=$yEnd; $yRotAng+=$angRange) {
### Rotate the protein around the y axis ###########################
my @rotatedCoords2 = @rotatedCoords;
@rotatedCoords2 = rotateAroundVec(\@rotatedCoords, \%yRotAxis, $rotPointRef, $yRotAng) unless $yRotAng == 0;
####################################################################
### Build the rotated CG model #####################################
my %rotProtData;
$rotProtData{'box'} = $cgProtData{'box'};
$rotProtData{'nAtoms'} = (scalar(@rotatedCoords2) - 1);
$rotProtData{'title'} = $cgProtData{'title'};
$rotProtData{'atoms'} = \@rotatedCoords2;
# my @rotProtAtomIds = getAtomIds($rotProtData{'atoms'});
# buildDummyPlane($rotProtData{'atoms'}, \@rotProtAtomIds, $beltZCenter, $membData{'box'});
# GRO::writeGro(sprintf("rotated.%02d-%02d.gro", $xRotAng, $yRotAng), \%rotProtData);
####################################################################
### Build protein grid #############################################
$gridRef = 0;
@tmpArray = Protein::analyze($rotAtomIdsRef,
\@rotatedCoords2);
$gridRef = $tmpArray[2];
Protein::grid2GroFile($gridOutFile) if $gridOutFile; # Print grid-coordinates.
####################################################################
### Count the z distribution (hydrophilicity profile) ##############
$sliceScoreRef = getSliceScore($gridRef, $gridDeltaZ, $hsprofOutFile);
####################################################################
### Determine the hydophobic belts #################################
my %belt = detectBelts($sliceScoreRef, $gridDeltaZ, $thicknHphobicUp, $thicknHphobicLow, $thicknHphobic, $isTMProt);
next unless $belt{'score'};
$beltZCenter = $belt{'hphobCent'} * $gridDeltaZ if $belt{'hphobCent'};
my @rotProtAtomIds = getAtomIds($rotProtData{'atoms'});
buildDummyPlane($rotProtData{'atoms'}, \@rotProtAtomIds, $beltZCenter, $membData{'box'});
$rotProtData{'nAtoms'} = (scalar(@{$rotProtData{'atoms'}}) - 1);
GRO::writeGro(sprintf("rotated.%02d-%02d.gro", $xRotAng, $yRotAng), \%rotProtData);
my %tmpHash = ('xRotAng' => $xRotAng, 'yRotAng' => $yRotAng, 'score' => $belt{'score'});
%absMinScore = %tmpHash if $absMinScore{'score'} > $belt{'score'};
push(@minScores, \%tmpHash);
print ANGFILE "$xRotAng $yRotAng $belt{'score'}\n";
print " MinScore: " . $belt{'score'} . " (x = $xRotAng, y = $yRotAng)" if $main::verbose;
print " (Abs. MinScore = " . $absMinScore{'score'} . " (x = " . $absMinScore{'xRotAng'} . ", y = " . $absMinScore{'yRotAng'} . "))\n" if $main::verbose;
undef %belt;
# print " Abs. MinScore: " . $minHs{'score'} . " x = $minHs{'xRotAng'}, y = $minHs{'yRotAng'}\n";
####################################################################
}
}
close(ANGFILE);
############################################################################
### Sort all found least scores (ASC) ######################################
return unless @minScores;
my @sortedMinScores = sort { $a->{'score'} <=> $b->{'score'} } @minScores;
my $minScore = int($sortedMinScores[0]{'score'});
foreach (@sortedMinScores) {
next if int($$_{'score'}) > $minScore;
# print "-> Going deeper with score " . $$_{'score'} . " at x = $$_{'xRotAng'}, y = $$_{'yRotAng'} (angle range $angRange)\n";
recRotXY2MinScore($rotAtomIdsRef, $atomCoordsRef, $rotPointRef, $$_{'xRotAng'}, $$_{'yRotAng'}, $angRange/2, $angRangeLimit);
# print "<- Going higher with score " . $$_{'score'} . " at x = $$_{'xRotAng'}, y = $$_{'yRotAng'} (angle range $angRange)\n";
}
############################################################################
}
sub getAtomIdsByResname {
my $atomDataRef = shift;
my $resnameRegex = shift;
my @atomIds;
for (my $i=1; $i<@{$atomDataRef}; $i++) {
push(@atomIds, $i) if $$atomDataRef[$i]{'resName'} =~ /$resnameRegex/;
}
# print "Found " . scalar(@atomIds) . " atoms of the bilayer\n"; exit;
return @atomIds;
}
sub getMembZCenter {
my $atomDataRef = shift;
my $atomIdsRef = shift;
my $membZCenter = 0;
foreach (@{$atomIdsRef}) {
$membZCenter += $$atomDataRef[$_]{'cooZ'};
}
return $membZCenter /= @{$atomIdsRef};
}
sub buildDummyPlane {
my $atomDataRef = shift;
my $atomIdsRef = shift;
my $beltZCenter = shift;
my $residue = $$atomDataRef[-1]{'residue'};
my $serial = $$atomDataRef[-1]{'serial'};
my $radius = 4;
my $radius2 = $radius*$radius;
my $gridSpacing = 0.1;
### Detect the geometrical center and place a bead #########################
my %geoCenter = getGeoCenter($atomIdsRef, $atomDataRef);
my %dummyCoords = ('cooX' => $geoCenter{'cooX'},
'cooY' => $geoCenter{'cooY'},
'cooZ' => $beltZCenter);
push(@{$atomDataRef}, setCgAtom(++$residue, 'DUM', 'CEN', ++$serial, \%dummyCoords));
############################################################################
### Build the plane around the geometrical center ##########################
for (my $x=$geoCenter{'cooX'}-$radius; $x<=$geoCenter{'cooX'}+$radius; $x+=$gridSpacing) {
for (my $y=$geoCenter{'cooY'}-$radius; $y<=$geoCenter{'cooY'}+$radius; $y+=$gridSpacing) {
my $dx = $geoCenter{'cooX'} - $x;
my $dy = $geoCenter{'cooY'} - $y;
next if ($dx*$dx + $dy*$dy) > $radius2;
my %dummyCoords = ('cooX' => $x,
'cooY' => $y,
'cooZ' => $beltZCenter);
push(@{$atomDataRef}, setCgAtom(++$residue, 'DUM', 'MID', ++$serial, \%dummyCoords));
}
}
############################################################################
### Build the plane limiting the hydrophobic range upwards #################
for (my $x=$geoCenter{'cooX'}-$radius; $x<=$geoCenter{'cooX'}+$radius; $x+=$gridSpacing) {
for (my $y=$geoCenter{'cooY'}-$radius; $y<=$geoCenter{'cooY'}+$radius; $y+=$gridSpacing) {
my $dx = $geoCenter{'cooX'} - $x;
my $dy = $geoCenter{'cooY'} - $y;
next if ($dx*$dx + $dy*$dy) > $radius2;
my %dummyCoords = ('cooX' => $x,
'cooY' => $y,
'cooZ' => $beltZCenter+$thicknHphobicUp);
push(@{$atomDataRef}, setCgAtom(++$residue, 'DUM', 'UPP', ++$serial, \%dummyCoords));
}
}
############################################################################
### Build the plane limiting the hydrophobic range downwards ###############
for (my $x=$geoCenter{'cooX'}-$radius; $x<=$geoCenter{'cooX'}+$radius; $x+=$gridSpacing) {
for (my $y=$geoCenter{'cooY'}-$radius; $y<=$geoCenter{'cooY'}+$radius; $y+=$gridSpacing) {
my $dx = $geoCenter{'cooX'} - $x;
my $dy = $geoCenter{'cooY'} - $y;
next if ($dx*$dx + $dy*$dy) > $radius2;
my %dummyCoords = ('cooX' => $x,
'cooY' => $y,
'cooZ' => $beltZCenter-$thicknHphobicLow);
push(@{$atomDataRef}, setCgAtom(++$residue, 'DUM', 'LOW', ++$serial, \%dummyCoords));
}
}
############################################################################
}
sub vSub {
my %vecDiff = ('cooX' => $_[0]{'cooX'} - $_[1]{'cooX'},
'cooY' => $_[0]{'cooY'} - $_[1]{'cooY'},
'cooZ' => $_[0]{'cooZ'} - $_[1]{'cooZ'});
return %vecDiff;
}
sub vLen {
return sqrt($_[0]{'cooX'}*$_[0]{'cooX'} + $_[0]{'cooY'}*$_[0]{'cooY'} + $_[0]{'cooZ'}*$_[0]{'cooZ'});
}
sub vNorm {
my $vecLen = vLen($_[0]);
my %vecNorm = ('cooX' => $_[0]{'cooX'}/$vecLen,
'cooY' => $_[0]{'cooY'}/$vecLen,
'cooZ' => $_[0]{'cooZ'}/$vecLen);
return %vecNorm;
}
sub vXprod {
my %vecXprod = ('cooX' => $_[0]{'cooY'} * $_[1]{'cooZ'} - $_[0]{'cooZ'} * $_[1]{'cooY'},
'cooY' => $_[0]{'cooZ'} * $_[1]{'cooX'} - $_[0]{'cooX'} * $_[1]{'cooZ'},
'cooZ' => $_[0]{'cooX'} * $_[1]{'cooY'} - $_[0]{'cooY'} * $_[1]{'cooX'});
return %vecXprod;
}
sub vDotprod {
return ($_[0]{'cooX'} * $_[1]{'cooX'} + $_[0]{'cooY'} * $_[1]{'cooY'} + $_[0]{'cooZ'} * $_[1]{'cooZ'});
}
sub rotateAroundVec {
my $coordDataRef = shift;
my $rotAxisRef = shift;
my $rotPointRef = shift;
my $rotAngle = shift;
my @coordData = @$coordDataRef;
my %rotatAxis = vSub($rotAxisRef, $rotPointRef); # Set the rotational point to 0.
my %unitVecZ = vNorm(\%rotatAxis); # -> Get unit vector z; For rotation around this axis.
my @rotatedCoords;
### Find the direction (x,y,z) of the vector with the lowest value.
my $minComponent = 'cooX';
if ($unitVecZ{'cooY'} < $unitVecZ{'cooX'} && $unitVecZ{'cooY'} < $unitVecZ{'cooZ'}) {
$minComponent = 'cooY';
}
elsif ($unitVecZ{'cooZ'} < $unitVecZ{'cooX'} && $unitVecZ{'cooZ'} < $unitVecZ{'cooY'}) {
$minComponent = 'cooZ';
}
my %tmpVector = ('cooX' => 0,
'cooY' => 0,
'cooZ' => 0);
$tmpVector{$minComponent} = 1;
### Calculate the second and third basis vector (unit vectors x and y).
my %crossGetY = vXprod(\%unitVecZ, \%tmpVector); # Calculate the CrossProduct to get the direction of Y.
my %unitVecY = vNorm(\%crossGetY); # -> Get unit vector y.
my %unitVecX = vXprod(\%unitVecY, \%unitVecZ); # -> Get unit vector x.
### Build the new orthonormal system (and check it).
my @newOrthoSys = (\%unitVecX, \%unitVecY, \%unitVecZ); # New Base with rotational axis = z.
# my $toler2Zero = 1e-6;
# my $chkOrthoXY = dot_prod(\@unitVecX, \@unitVecY);
# my $chkOrthoXZ = dot_prod(\@unitVecX, \@unitVecZ);
# my $chkOrthoYZ = dot_prod(\@unitVecY, \@unitVecZ);
# if (abs($chkOrthoXY) > $toler2Zero or abs($chkOrthoXZ) > $toler2Zero or abs($chkOrthoYZ) > $toler2Zero) {
# die "ERROR: Cannot build orthogonal system for the rotation.\n".
# "Unit vector x = (".join(", ", @unitVecX)."),\n".
# "unit vector y = (".join(", ", @unitVecY)."),\n".
# "unit vector z = (".join(", ", @unitVecZ).")\n";
# }
#####################################################
### Rotation ########################################
#####################################################
my $angCos = cos(deg2rad($rotAngle));
my $angSin = sin(deg2rad($rotAngle));
for (my $atomId=1; $atomId<@coordData; $atomId++) {
my %coordCenter = vSub($coordData[$atomId], $rotPointRef);
my $radiusX = vDotprod(\%unitVecX, \%coordCenter);
my $radiusY = vDotprod(\%unitVecY, \%coordCenter);
my $compouZ = vDotprod(\%unitVecZ, \%coordCenter);
my $coeffiX = $radiusX*$angCos - $radiusY*$angSin;
my $coeffiY = $radiusX*$angSin + $radiusY*$angCos;
my @coeffiMat = ($coeffiX, $coeffiY, $compouZ); # z is the rotational axis.
my %tmpRotated = ('cooX' => 0,
'cooY' => 0,
'cooZ' => 0);
for (my $k=0; $k<3; $k++) {
$tmpRotated{'cooX'} += $coeffiMat[$k]*$newOrthoSys[$k]{'cooX'};
$tmpRotated{'cooY'} += $coeffiMat[$k]*$newOrthoSys[$k]{'cooY'};
$tmpRotated{'cooZ'} += $coeffiMat[$k]*$newOrthoSys[$k]{'cooZ'};
}
### Translate back ##############################
foreach my $key (keys %{$coordData[$atomId]}) {
$rotatedCoords[$atomId]{$key} = $coordData[$atomId]{$key};
}
$rotatedCoords[$atomId]{'cooX'} = $tmpRotated{'cooX'} + $$rotPointRef{'cooX'};
$rotatedCoords[$atomId]{'cooY'} = $tmpRotated{'cooY'} + $$rotPointRef{'cooY'};
$rotatedCoords[$atomId]{'cooZ'} = $tmpRotated{'cooZ'} + $$rotPointRef{'cooZ'};
}
return @rotatedCoords;
}
################################################################################
### Subroutines ################################################################
################################################################################
sub getAtomIds {
my $coordsRef = shift;
my @atomIds;
for (my $i=0; $i<@{$coordsRef}; $i++) {
push(@atomIds, $i) if $$coordsRef[$i]{'resId'};
}
return @atomIds;
}
sub centerGroup {
my $atomIdsRef = shift;
my $coordsRef = shift;
my $boxRef = shift;
my %groupGeoCenter = getGeoCenter($atomIdsRef, $coordsRef);
my %translVector = ('cooX' => ($$boxRef{'cooX'}*0.5-$groupGeoCenter{'cooX'}),
'cooY' => ($$boxRef{'cooY'}*0.5-$groupGeoCenter{'cooY'}));
foreach (@{$atomIdsRef}) {
$$coordsRef[$_]{'cooX'} += $translVector{'cooX'};
$$coordsRef[$_]{'cooY'} += $translVector{'cooY'};
}
}
sub zTranslateGroup {
my $atomIdsRef = shift;
my $coordsRef = shift;
my $zTranslVec = shift;
foreach (@{$atomIdsRef}) {
$$coordsRef[$_]{'cooZ'} += $zTranslVec;
}
}
sub translateGroup {
my $atomIdsRef = shift;
my $coordsRef = shift;
my $xTranslVec = shift;
my $yTranslVec = shift;
my $zTranslVec = shift;
foreach (@{$atomIdsRef}) {
$$coordsRef[$_]{'cooX'} += $xTranslVec;
$$coordsRef[$_]{'cooY'} += $yTranslVec;
$$coordsRef[$_]{'cooZ'} += $zTranslVec;
}
}
sub combGroData {
my $protGroDataRef = shift;
my $membGroDataRef = shift;
my %combGroData;
foreach (@{$$protGroDataRef{'atoms'}}) {
next unless $$_{'resId'};
push(@{$combGroData{'atoms'}}, $_);
}
foreach (@{$$membGroDataRef{'atoms'}}) {
next unless $$_{'resId'};
push(@{$combGroData{'atoms'}}, $_);
}
$combGroData{'title'} = $$protGroDataRef{'title'} . ' + ' . $$membGroDataRef{'title'};