-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElasticNet.cc
1168 lines (996 loc) · 32.5 KB
/
ElasticNet.cc
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
/* Created by Giovanni Pinamonti */
/* PhD student @ SISSA, Trieste */
/* November 20th, 2013 */
#include <time.h>
#include "omp.h"
#include "ElasticNet.h"
#include "io.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include "lapack_matrix_routines_wrapper_v3.cc"
#include "BeadList.h"
#define BROKEN_CHAIN_CUTOFF 7.0
#define DEFAULT_CUTOFF 10.0
#define DEFAULT_NTOP 10
//###### COSTRUTTORI DELLA CLASSE ######
ElasticNet::ElasticNet():
// _covMatrix(0)
_N(0),
_ntop_vectors(DEFAULT_NTOP),
_cutoff(DEFAULT_CUTOFF),
_R_BB(-1),
_R_B(-1),
_R_PP(-1),
_R_prot(-1),
_R_N7(-1),
_TRACCIAMENTO(false),
_Kcov(1.0),
_KBB(1.0),
_KB(1.0),
_KPP(1.0),
_COM(false),
_OUTRES(false),
// _PROTEIN(false),
_FAST(false),
_DPF(false),
_RANDOM(false),
_RARAND(false),
_EXP(false),
_DUMPMODES(false),
_DUMPEIGENVALUES(true),
_DUMPCOVMAT(false),
_DUMPDISTFLUC(false),
_DUMPREDCOVMAT(false),
_DUMPMSD(true)
{
_COM=false;
}
ElasticNet::ElasticNet(const char *filename):
_ntop_vectors(DEFAULT_NTOP),
_cutoff(DEFAULT_CUTOFF),
_R_BB(-1),
_R_B(-1),
_R_PP(-1),
_R_prot(-1),
_R_N7(-1),
_TRACCIAMENTO(false),
_Kcov(1.0),
_KBB(1.0),
_KB(1.0),
_KPP(1.0),
_COM(false),
_OUTRES(false),
// _PROTEIN(false),
_FAST(false),
_DPF(false),
_RANDOM(false),
_RARAND(false),
_EXP(false),
_DUMPMODES(false),
_DUMPEIGENVALUES(true),
_DUMPCOVMAT(false),
_DUMPDISTFLUC(false),
_DUMPREDCOVMAT(false),
_DUMPMSD(true)
{
// cout<<"ElasticNet::reading file "<<filename<<endl;
readPDBFile(filename);
}
ElasticNet::ElasticNet(Structure3d structure):
_ntop_vectors(DEFAULT_NTOP),
_cutoff(DEFAULT_CUTOFF),
_R_BB(-1),
_R_B(-1),
_R_PP(-1),
_R_prot(-1),
_R_N7(-1),
_TRACCIAMENTO(false),
_Kcov(1.0),
_KBB(1.0),
_KB(1.0),
_KPP(1.0),
_COM(false),
_OUTRES(false),
// _PROTEIN(false),
_FAST(false),
_DPF(false),
_RANDOM(false),
_RARAND(false),
_EXP(false),
_DUMPMODES(false),
_DUMPEIGENVALUES(true),
_DUMPCOVMAT(false),
_DUMPDISTFLUC(false),
_DUMPREDCOVMAT(false),
_DUMPMSD(true)
{
cout<<"ElasticNet: setting the structure"<<endl;
setStructure(structure);
// cout<<"ElasticNet: creating the contact map"<<endl;
// constructContactMap();
// cout<<"ElasticNet: creating the interaction matrix"<<endl;
// constructIntMat();
}
#undef DEFAULT_CUTOFF
#undef DEFAULT_NTOP
ElasticNet::~ElasticNet() {
cout<<"deleting"<<endl;
//Be careful: Cmap, in principle, could be not allocated.
free_i2t(_cmap);
}
// ### ### METODI DI ELASTICNET ### ###
//set the structure
void ElasticNet::setStructure(Structure3d structure){
_structure=structure;
_N=_structure.getSize();
// _size=3*_N;
cout<<"ElasticNet:: costruita struttura di dimensione " <<_N<<endl;
}
int ElasticNet::readPDBFile(const char* fname){
FILE* fp;
if ((fp = fopen (fname, "r")) == NULL) {
fprintf (stderr,"Could not open file %s.\n.Exiting.\n", fname);
exit (1); }
int idum= readPDBFile(fp);
fclose(fp);
return idum;
}
//read a PDB file
int ElasticNet::readPDBFile(FILE *fp){
int error;
if(_COM) {error=_structure.centersFromPDBFile(fp);cout<<"***********8COM"<<endl;}
else error=_structure.readFromPDBFile(fp);
_N=_structure.getSize();
// _size=3*N;
cout<<"ElasticNet:: costruita struttura di dimensione "<<_N<<endl;
return error;
};
/* decompose the interaction matrix */
int ElasticNet::Solve(){
//_intMatrix->Decompose()
if(_TRACCIAMENTO) {
if(tracciamento()<0){cout<<"ERROR IN TRACCIAMENTO"<<endl; return -1;}
}
_intMatrix.Decompose();
time_t begin_time2=time(0);
if (_FAST) {cout<<"ElasticNet::I don't compute the covariance matrix to save time!!!"<<endl; return 0;}
else computeCovar();
cout << "Solve: tempo nel ciclo 2 = "<<difftime(time(0),begin_time2)<<endl;
// IntMatrix mat_prova=_intMatrix;
// cout<<"ElasticNet:: Computing the covariance matrix..."<<endl;
// cout<<"ElasticNet:: invert..."<<endl;
// _covMatrix=_intMatrix.GetInverse();
// cout<<"ElasticNet:: decompose..."<<endl;
// _covMatrix.Decompose();
return 0; //success
}
void ElasticNet::computeCovar(){
cout<<"ElasticNet:: Computing the covariance matrix..."<<endl;
cout<<"ElasticNet:: invert..."<<endl;
_covMatrix=_intMatrix.GetInverse();
//cout<<"ElasticNet:: CovMatrix -> decompose..."<<endl;
// _covMatrix.Decompose();
return;
};
/* dump the important stuff of the covariance matrix
AND the mean square displacements */
void ElasticNet::Dump(const char *name){
//qui puoi dumpare quello che vuoi. Non e' male in realta'
string fname(name);
cout<<"dumpmodes "<<_DUMPMODES<<endl;
if(_FAST) {
cout<<" FASTASHELL!!"<<endl;
_intMatrix.dumpCovEigenvalues(fname);
_intMatrix.dumpTopCovVectors(_ntop_vectors,fname);
cout<<" FASTASHELL!!"<<endl;
}
else {
cout<<"ElasticNet::Dump -> _covMatrix.dumpEigenvalues()"<<endl;
_covMatrix.dumpEigenvalues(name);
cout<<"ElasticNet::Dump -> _covMatrix.dumpEigenvectors()"<<endl;
cout<<"vec="<<_ntop_vectors<<endl;
_covMatrix.dumpTopVectors(_ntop_vectors,name);
if(_DUMPCOVMAT) _covMatrix.dumpFullMatrix(fname+"_cov");
if(_DUMPREDCOVMAT) {_covMatrix.dumpReducedMatrix(fname+"_cov");
_covMatrix.dumpNormalizedReducedMatrix(fname+"_cov");}
_intMatrix.dumpReducedMatrix(fname+"_int");
if(_DUMPDISTFLUC) dumpDistFluc(fname);
cout<<"ElasticNet::Dump -> Structure"<<endl;
_structure.dumpPDBFile(fname+"_structure.pdb");
}
_intMatrix.dumpFullMatrix(fname);
cout<<"ElasticNet::Dump -> dumpMSF()()"<<endl;
// _intMatrix.dumpMSF(name);
dumpMSF(name);
if(_DPF){
char filename[200];
ofstream fp;
sprintf(filename,"%s_mean_square_fluc_part.dat",name);
fp.open(filename);
fp<<"# n. bead & atom type & n. res & xx & xy & xz & yy & yz & zz"<<endl;
for(int i=0; i < _intMatrix.GetN(); i++){
fp <<i <<" "
<<_structure.getBead(i).getAtomType()<<" "
<<_structure.getBead(i).getResNum()<<" ";
for(int mu=0;mu<3;mu++){
for(int nu=mu;nu<3;nu++){
// string coord1,coord2;
// if(mu==0) coord1="x";
// if(mu==1) coord1="y";
// if(mu==2) coord1="z";
// if(nu==0) coord2="x";
// if(nu==1) coord2="y";
// if(nu==2) coord2="z";
double temp;
if(_FAST) temp=_intMatrix.GetMSF(i,mu,nu);
else temp=_covMatrix.GetMSF(i,mu,nu);
fp<<temp<<" ";
}}
fp<<endl;
}
fp.close();
cout<<"Beads' mean square partial fluctuations written to file "<<filename<<endl;
}//endif_DPF
if(_DUMPMODES){
cout<<"dumping modes"<<endl;
if(_FAST){ cout<<"ElasticNet -> PROBLEM: won't dump principal modes in FAST configuration!"<<endl; return; }
// if(_ntop_vectors<11) dump_top_modes(_ntop_vectors,5,name);
// else dump_top_modes(10,5,name);
dump_top_modes(_ntop_vectors,5,name);
}
cout<<"dumping the number of NN"<<endl;
dumpNearestNeighbours(name);
}
void ElasticNet::constructContactMap(){
//TODO una cosa del genere?
// 1 -> contatto normale
// 3 -> legame covalente
// 10 -> contatto proteina-proteina
// 11 -> contatto proteina-RNA
// double R_BB=5.0;
if(_R_B<0) _R_B =_cutoff;
if(_R_PP<0) _R_PP =_cutoff;
if(_R_BB<0) _R_BB =_cutoff;
if(_R_prot<0) _R_prot=_cutoff;
if(_R_N7<0) _R_N7 =_cutoff;
cout<<" *** RADIUS OF CUT-OFF *** "<<endl;
cout<<" *** Rc ="<<_cutoff<<endl;
cout<<" *** R_B ="<<_R_B <<endl;
cout<<" *** R_PP="<<_R_PP <<endl;
cout<<" *** R_BB="<<_R_BB <<endl;
cout<<" *** R_N7="<<_R_N7 <<endl;
cout<<" ************************* "<<endl;
_cmap=i2t(_N,_N);
for(int i=0; i < _N-1; i++){
for(int j= i+1; j < _N; j++){
/* Get the distance of beads i and j */
double d = _structure.getDistance(i,j);
/* Check if they are closer than Int_Range */
Bead bi=_structure.getBead(i);
Bead bj=_structure.getBead(j);
double Rc=_cutoff;
int kind_o_int=1;
//22 -> base-base
//20 -> base other
//99 -> P-P
//77 -> prot-prot
//77 -> prot-RNA
if (bi.getAtomType()=="N7"){
// if (bj.getAtomType()=="N7"){
// Rc=_R_BB; kind_o_int=22;}
// else
{Rc=_R_N7; kind_o_int=20;}
}
if (bj.getAtomType()=="N7"){
// if (bi.getAtomType()=="N7"){
// Rc=_R_BB; kind_o_int=22;}
// else
{Rc=_R_N7; kind_o_int=20;}
}
// if ((bi.getAtomType()=="C2")||(bi.getAtomType()=="N7")){
// if ((bj.getAtomType()=="C2")||(bj.getAtomType()=="N7")){
if (bi.getAtomType()=="C2"){
if (bj.getAtomType()=="C2"){
Rc=_R_BB; kind_o_int=22;}
else
{Rc=_R_B; kind_o_int=20;}
}
// if ((bj.getAtomType()=="C2")||(bj.getAtomType()=="N7")){
// if ((bi.getAtomType()=="C2")||(bi.getAtomType()=="N7")){
if (bj.getAtomType()=="C2"){
if (bi.getAtomType()=="C2"){
Rc=_R_BB; kind_o_int=22;}
else
{Rc=_R_B; kind_o_int=20;}
}
if (bi.getAtomType()=="P")
if (bj.getAtomType()=="P"){
Rc=_R_PP; kind_o_int=99;}
double Rhybrid=(_cutoff+_R_prot)*0.5;
//protein RNA interaction
if (bi.getAtomType()=="CA"){
if (bj.getAtomType()=="CA"){
Rc=_R_prot;kind_o_int=77;}
else{ Rc=Rhybrid;kind_o_int=70;}
}
else{
if (bj.getAtomType()=="CA"){
Rc=Rhybrid;kind_o_int=70;}
}
if (d < Rc) _cmap[i][j]=_cmap[j][i]=kind_o_int;
}//end do j
}//end do i
for(int i=0; i < _N-1; i++){
/* ### stiffest springs between bonded nodes ### */
Bead bi=_structure.getBead(i);
if (bi.getAtomType()=="P"){
int j=i+1;
Bead bj=_structure.getBead(j);
if (bj.getAtomType()=="C1'"){
double d=_structure.getDistance(i,j);
if (d < BROKEN_CHAIN_CUTOFF)
_cmap[i][j]=_cmap[j][i]=3;
}//endif
}//endif P
else if (bi.getAtomType()=="C1'"){
int j=i+1;
Bead bj=_structure.getBead(j);
if (bj.getAtomType()=="C2"){
double d=_structure.getDistance(i,j);
if (d < BROKEN_CHAIN_CUTOFF)
_cmap[i][j]=_cmap[j][i]=3;
}//endif
if (bj.getAtomType()=="N7"){
double d=_structure.getDistance(i,j);
if (d < BROKEN_CHAIN_CUTOFF)
_cmap[i][j]=_cmap[j][i]=3;
}//endif
j=i+2;
if(j>_N-1) continue;
bj=_structure.getBead(j);
if (bj.getAtomType()=="P"){
double d=_structure.getDistance(i,j);
if (d < BROKEN_CHAIN_CUTOFF)
_cmap[i][j]=_cmap[j][i]=3;
}//endif
j=i+3;
if(j>_N-1) continue;
bj=_structure.getBead(j);
if (bj.getAtomType()=="P"){
double d=_structure.getDistance(i,j);
if (d < BROKEN_CHAIN_CUTOFF)
_cmap[i][j]=_cmap[j][i]=3;
}//endif
}//endif C1'
else if (bi.getAtomType()=="N7"){
int j=i+1;
Bead bj=_structure.getBead(j);
if (bj.getAtomType()=="C2"){
double d=_structure.getDistance(i,j);
if (d < BROKEN_CHAIN_CUTOFF)
_cmap[i][j]=_cmap[j][i]=3;
}//endif
}//endif N7
}//enddo I
}//end function
void ElasticNet::constructRandMat(double **intmat){
cout<<"ElasticNet: creating a random interaction matrix"<<endl;
for(int i=0; i < _N; i++){
for(int j=0; j < _N; j++){
if (i==j) continue;
double k_int=((double) rand() / ((double) RAND_MAX));
double d = _structure.getDistance(i,j);
/* Let's build the distance vector */
Vector3d dvec= _structure.getDistanceVector(i,j);
for (int mu=0; mu < 3; mu++){
for (int nu=0; nu < 3; nu++){
double temp = 0.5*k_int*dvec[mu]*dvec[nu]/(d*d);
intmat[3*i+mu][3*i+nu] += temp;
intmat[3*i+mu][3*j+nu] += -temp;
intmat[3*j+mu][3*i+nu] += -temp;
intmat[3*j+mu][3*j+nu] += temp;
}//end for nu
}// end for mu
}//i
}//j
return;
}
void ElasticNet::constructDemiRandMat(){
cout<<"ElasticNet: shuffling the connectivity matrix to obtain a demi-random interaction matrix"<<endl;
// int n_conn=0;
vector <Connection> connessioni;
for(int i=0;i<_N;++i){
for (int j=i+1;j<_N;++j){
if (i==j) continue;
if(_cmap[i][j]!=0){
//n_conn++;
if(_cmap[i][j]>1) cout<<i<<" "<<j<<endl;
Connection temp_conn(i,j);
connessioni.push_back(temp_conn);
}
}
}
int N_CONN=connessioni.size();
cout<<"ci sono "<<N_CONN<<" connessioni"<<endl;
int N_STEP_MAX=100*N_CONN;
int n2swap=2*N_CONN;
int n_swapped=0;
for(int n=0;n<N_STEP_MAX;++n){
// cout<<n<<endl;
int i= rand()%N_CONN;
int j= rand()%N_CONN;
Connection tmp_connA=connessioni.at(i);
Connection tmp_connB=connessioni.at(j);
int old_i1_A=tmp_connA.GetI1();
int old_i2_A=tmp_connA.GetI2();
int old_i1_B=tmp_connB.GetI1();
int old_i2_B=tmp_connB.GetI2();
cout<<"Scelgo queste due connessioni: "<<endl
<<"["<<i<<"] "<<tmp_connA.GetI1()<<"-"<<tmp_connA.GetI2()<<endl
<<"["<<j<<"] "<<tmp_connB.GetI1()<<"-"<<tmp_connB.GetI2()<<endl;
if(!(RandSwap(tmp_connA, tmp_connB)) ) {cout<<"Non le scambio!"<<endl; continue;}
int i1_A=tmp_connA.GetI1();
int i2_A=tmp_connA.GetI2();
int i1_B=tmp_connB.GetI1();
int i2_B=tmp_connB.GetI2();
cout<<"ora connetterebbero i seguenti nodi: "<<endl
<<"["<<i<<"] "<<i1_A<<"-"<<i2_A<<endl
<<"["<<j<<"] "<<i1_B<<"-"<<i2_B<<endl;
if(_cmap[i1_A][i2_A]>0){cout<<"[i] were already connected"<<endl; continue;}
if(_cmap[i1_B][i2_B]>0){cout<<"[j] were already connected"<<endl; continue;}
_cmap[i1_A][i2_A]++;
_cmap[i1_B][i2_B]++;
_cmap[i2_A][i1_A]++;
_cmap[i2_B][i1_B]++;
_cmap[old_i1_A][old_i2_A]=0;
_cmap[old_i1_B][old_i2_B]=0;
_cmap[old_i2_A][old_i1_A]=0;
_cmap[old_i2_B][old_i1_B]=0;
connessioni.at(i)=tmp_connA;
connessioni.at(j)=tmp_connB;
n_swapped++;
cout<<"SCAMBIATE!"<<endl;
if(n_swapped>n2swap) break;
}//end for
cout<<"Ho scambiato "<<n_swapped<<" connessioni"<<endl;
int n_conn=0;
for(int i=0;i<_N;++i){
for (int j=i+1;j<_N;++j){
if (i==j) continue;
if(_cmap[i][j]>1) cout<<i<<" "<<j<<endl;
if(_cmap[i][j]==1){
n_conn++;
}
}
}
cout<<"ci sono "<<n_conn<<" connessioni"<<endl;
return;
}
void ElasticNet::constructExpMat(double **intmat){
cout<<"ElasticNet: creating an exponential interaction matrix"<<endl;
for(int i=0; i < _N; i++){
for(int j=0; j < _N; j++){
if (i==j) continue;
double k_int=1;
double d = _structure.getDistance(i,j);
/* Let's build the distance vector */
Vector3d dvec= _structure.getDistanceVector(i,j);
k_int *= exp(-((d/_cutoff)*(d/_cutoff)) ) ;
for (int mu=0; mu < 3; mu++){
for (int nu=0; nu < 3; nu++){
double temp = 0.5*k_int*dvec[mu]*dvec[nu]/(d*d);
intmat[3*i+mu][3*i+nu] += temp;
intmat[3*i+mu][3*j+nu] += -temp;
intmat[3*j+mu][3*i+nu] += -temp;
intmat[3*j+mu][3*j+nu] += temp;
}//end for nu
}// end for mu
}//i
}//j
return;
}
void ElasticNet::constructIntMat(){
double **intmat=d2t(3*_N,3*_N);
/* Include contact interactions i-j*/
if(_RANDOM) constructRandMat(intmat);
else if(_EXP) constructExpMat(intmat);
else {
if(_RARAND) constructDemiRandMat();
cout<<"ElasticNet: start computing the matrix"<<endl;
for(int i=0; i < _N; i++){
for(int j=0; j < _N; j++){
double k_int=1;
if (i==j) continue;
if (_cmap[i][j]==0) continue;
double d = _structure.getDistance(i,j);
/* Let's build the distance vector */
Vector3d dvec= _structure.getDistanceVector(i,j);
/* Are you covalent? */
if(_cmap[i][j]==3) {k_int*=_Kcov;}
else if(_cmap[i][j]==22) k_int*=_KBB;
else if(_cmap[i][j]==20) k_int*=_KB;
else if(_cmap[i][j]==99) k_int*=_KPP;
for (int mu=0; mu < 3; mu++){
for (int nu=0; nu < 3; nu++){
double temp = 0.5*k_int*dvec[mu]*dvec[nu]/(d*d);
intmat[3*i+mu][3*i+nu] += temp;
intmat[3*i+mu][3*j+nu] += -temp;
intmat[3*j+mu][3*i+nu] += -temp;
intmat[3*j+mu][3*j+nu] += temp;
}//end for nu
}// end for mu
}//end for j
}//end for i
}//endif
cout<<"ElasticNet: saving it as a 'Matrix'"<<endl;
_intMatrix.SetMatrix(_N,intmat);
free_d2t(intmat);
printf("End construction of matrix with effective quadratic interactions among beads's\n");
return;
}
void ElasticNet::readParameters(const char* filename){
cout<<"ElasticNet::redParameters"<<endl;
// FILE *fp;
char stringa[1000];
char argument[1000];
char line[1000];
char name[200];
ifstream file_in;
sprintf(name,"%s",filename);
// fp = open_file_r(name);
file_in.open(name);
if(!file_in.is_open()){
cout<<"ElasticNet::readParameters-> Problem opening file: "<<name<<endl;
return;
}
// while(fgets(line,1000,fp)!=NULL){
while(file_in.getline(line,1000)!=NULL){
istringstream iss(line);
// sscanf(line,"%s %s", string, argument);
if(!(iss>>stringa)) continue;
if(!strncmp(stringa,"Int_Range",9)){iss>>argument; sscanf(argument,"%lf",&_cutoff);}
if(!strncmp(stringa,"R_BB",4)){iss>>argument; sscanf(argument,"%lf",&_R_BB);}
if(!strncmp(stringa,"R_Ba",4)){iss>>argument; sscanf(argument,"%lf",&_R_B);}
if(!strncmp(stringa,"R_PP",4)){iss>>argument; sscanf(argument,"%lf",&_R_PP);}
if(!strncmp(stringa,"R_prot",6)){iss>>argument; sscanf(argument,"%lf",&_R_prot);}
if(!strncmp(stringa,"R_N7",4)){iss>>argument; sscanf(argument,"%lf",&_R_N7);}
if(!strncmp(stringa,"N_SLOWEST_EIGENVECT",19)){iss>>argument; sscanf(argument,"%d",&_ntop_vectors);}
// if(!strncmp(stringa,"Smooth_Cutoff",13)) sscanf(argument,"%d",&Smooth_Cutoff);
// if(!strncmp(stringa,"nodesXnucl",10)) sscanf(argument,"%d",&nodesXnucl);
if(!strncmp(stringa,"K_COV",5)) {iss>>argument; sscanf(argument,"%lf",&(_Kcov)); cout<<"K_COV="<<_Kcov<<endl;}
if(!strncmp(stringa,"K_BB",4)) {iss>>argument; sscanf(argument,"%lf",&(_KBB)); cout<<"K_BB="<<_KBB<<endl;}
if(!strncmp(stringa,"K_PP",4)) {iss>>argument; sscanf(argument,"%lf",&(_KPP)); cout<<"K_PP="<<_KPP<<endl;}
if(!strncmp(stringa,"K_Ba",4)) {iss>>argument; sscanf(argument,"%lf",&(_KB)); cout<<"K_Ba="<<_KB<<endl;}
// if(!strncmp(stringa,"SPECIAL_K",9)) sscanf(argument,"%d",&SPECIAL_K);
// if(!strncmp(stringa,"BASE_CUTOFF",11)) sscanf(argument,"%lf",&BASE_CUTOFF);
// if(!strncmp(stringa,"RangeSmooth",11)) sscanf(argument,"%lf",&RangeSmooth);
// if(!strncmp(stringa,"COMPUTE_DIST_MATRIX",19)) sscanf(argument,"%d",&COMPUTE_DIST_MATRIX);
if(!strncmp(stringa,"BEADS:",6)){
cout<<"ElasticNet:: "<<stringa<<" ";
char name[4];
_beadList.clear();
while (iss >> name)
{
cout<<name<<" ";
_beadList.push_back(name);
}
cout<<endl<<"ElasticNet:: N of beads = "<<_beadList.size()<<endl;
_structure.setBeadList(_beadList);
}
//STA COSA NON SERVE, TANTO NON USO MAI STA VARIABILE: SE HO CA FRA I BEAD BENE< ALTRIMENTI CICCE
// if(!strncmp(stringa,"PROTEIN",7)){
// cout<<"ElasticNet:: I want your proteins baby..."<<endl;
// _PROTEIN=true;
// }
if(!strncmp(stringa,"OUTBEADS:",9)){
cout<<"ElasticNet:: "<<stringa<<" ";
char name[4];
_outBeadList.clear();
while (iss >> name)
{
cout<<name<<" ";
_outBeadList.push_back(name);
}
cout<<endl<<"ElasticNet:: N of beads for output = "<<_outBeadList.size()<<endl;
if((_outBeadList.size()>0)&&(_outBeadList.size()<_beadList.size())) _TRACCIAMENTO=true;
if((_outBeadList.size()>0)&&(_beadList.size()<1)){ _TRACCIAMENTO=true; cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;}
// _structure.setBeadList(_outBeadList);
}
if(!strncmp(stringa,"OUTRES:",6)){
cout<<"ElasticNet:: "<<stringa<<" ";
int num;
_outResList.clear();
while (iss >> num)
{
cout<<num<<" ";
_outResList.push_back(num);
}
cout<<endl<<"ElasticNet:: N of res for output = "<<_outResList.size()<<endl;
_TRACCIAMENTO=true;
_OUTRES=true;
}
if(!strncmp(stringa,"COM",3)){
_COM=true;
cout<<" >>> Beads in the centers of mass for sugar and base <<< "<<endl;
}
if(!strncmp(stringa,"FAST",4)){
_FAST=true;
cout<<" >>> I'm gonna be faster than light! <<< "<<endl;
}
if(!strncmp(stringa,"DPF",3)){
_DPF=true;
cout<<" >>> Dump Partial Fluctuations <<< "<<endl;
}
if(!strncmp(stringa,"DUMPCOV",7)){
_DUMPCOVMAT=true;
_DUMPREDCOVMAT=true;
cout<<" >>> Dump covariance matrix <<< "<<endl;
}
if(!strncmp(stringa,"DISTFLUC",7)){
_DUMPDISTFLUC=true;
cout<<" >>> Dump distance fluctuations <<< "<<endl;
}
if(!strncmp(stringa,"RANDOM",6)){
_RANDOM=true;
double seed;
iss>>argument; sscanf(argument,"%lf",&seed);
srand(seed);
cout<<" @!#$$$&^%$! rAnDOm NeTWork &*#$&#%&^@## (seed = "<<seed<<")"<<endl;
}
if(!strncmp(stringa,"RARAND",6)){
_RARAND=true;
double seed;
iss>>argument; sscanf(argument,"%lf",&seed);
srand(seed);
cout<<" @!#$$$&^%$! rAnDOm NeTWork &*#$&#%&^@## (seed = "<<seed<<")"<<endl;
}
if(!strncmp(stringa,"EXP",3)){
_EXP=true;
cout<<" >>> exponential <<< "<<endl;
}
if(!strncmp(stringa,"DUMP_MODES",10)){
_DUMPMODES=true;
cout<<" >>> Dump top modes <<< "<<endl;
}
// if(!strncmp(stringa,"DUMP_EIGENVALUES",16)) sscanf(argument,"%d",&DUMP_EIGENVALUES);
// if(!strncmp(stringa,"DUMP_FULL_COVMAT",16)) sscanf(argument,"%d",&DUMP_FULL_COVMAT);
// if(!strncmp(stringa,"DUMP_REDUCED_COVMAT",19)) sscanf(argument,"%d",&DUMP_REDUCED_COVMAT);
// if(!strncmp(stringa,"DUMP_NORMALISED_REDUCED_COVMAT",30)) sscanf(argument,"%d",&DUMP_NORMALISED_REDUCED_COVMAT);
// if(!strncmp(stringa,"DUMP_MEAN_SQUARE_DISPL",22)) sscanf(argument,"%d",&DUMP_MEAN_SQUARE_DISPL);
}//END LOOP ON FILE LINES
// fclose(fp);
file_in.close();
return;
}
int ElasticNet::tracciamento(){
// MODIFICO PERCHE' CAMBI ANCHE LA STRUTTURA DI RIFERIMENTO!!!
int *cacca=i1t(_N);
int *prog=i1t(_N);
int Na=0;
//*********
cout<<" find the out-beads "<<endl;
if(_outBeadList.size()>_beadList.size()) {cout<<"WARNING: OUTBEADS are more than beads!"<<endl;}
if(_outBeadList.size()==0) {cout<<"WARNING: OUTBEADS is empty!"<<endl;}
for(int i=0;i<_N;++i){
string b=_structure.getBead(i).getAtomType();
// cout<<b<<endl;
for (int k=0;k<_outBeadList.size();++k)
{
string obead_k=_outBeadList.at(k);
// cout<<obead_k<<endl;
replace(obead_k.begin(), obead_k.end(), 'p', '\'');
// cout<<obead_k<<endl;
if (obead_k.compare(b)==0) { cacca[i]=1;}
}
}
//*********
if(_OUTRES){
cout<<"LOOKING FOR THE NODES IN OUTRES"<<endl;
for(int i=0;i<_N;++i){
// cout<<_structure.getBead(i).getResNum()<<endl;
if(cacca[i]==0) continue; //se non e' un outbead non mi preoccupo nemmeno
if(cacca[i]==1) cacca[i]=0; //se si lo metto uguale a zero e...
int n=_structure.getBead(i).getResNum();
// cout<<" "<<n<<" "<<endl;
for (int k=0;k<_outResList.size();++k)
{ //...lo rimetto uguale a uno
if (_outResList.at(k)==n) {cacca[i]=1;cout<<"###"<<n<<endl; /*cout<<"ok"<<endl;*/} // solo se e' anche nella lista
} // dei residui per l'output
}
}
/* save the number of out-beads progressively on an array */
prog[0]=cacca[0]-1;
for(int i=1;i<_N;++i){
prog[i]=prog[i-1]+cacca[i];
}
Na=prog[_N-1]+1;
int Nb=_N-Na;
if(Nb==0) {cout<<endl<<"!!!"<<endl<<"!!! ElasticNet:: tracciamento -> no need for that"<<endl<<"!!!"<<endl; return 0; }
if(Na==0) {cout<<endl<<"!!!"<<endl<<"!!! ElasticNet:: tracciamento -> ERROR with the OUTBEADS list"<<endl<<"!!!"<<endl; return -1; }
cout<<"Na="<<Na<<" Nb="<<Nb<<endl;
double **Ma=d2t(3*Na,3*Na);
double **Mb=d2t(3*Nb,3*Nb);
double **V=d2t(3*Na,3*Nb);
for(int i=0;i<_N;++i){
for(int j=0;j<_N;++j){
////////////////////////////////
/* Wanted-wanted => matrix Ma */
if((cacca[i]==1) && (cacca[j]==1)){
for(int mu=0;mu<3;++mu){
for(int nu=0;nu<3;++nu){
Ma[3*prog[i]+mu][3*prog[j]+nu]=_intMatrix.GetElement(3*i+mu,3*j+nu);
}//nu
}//mu
}//if
/* not wanted - not wanted => matrix Mb */
if((cacca[i]==0) && (cacca[j]==0)){
for(int mu=0;mu<3;++mu){
for(int nu=0;nu<3;++nu){
Mb[3*(i-prog[i]-1)+mu][3*(j-prog[j]-1)+nu]=_intMatrix.GetElement(3*i+mu,3*j+nu);
}//nu
}//mu
}//if
/* wanted - not wanted => matrix V */
if((cacca[i]==1)&&(cacca[j]==0)){
for(int mu=0;mu<3;++mu){
for(int nu=0;nu<3;++nu){
V[3*prog[i]+mu][3*(j-prog[j]-1)+nu]=_intMatrix.GetElement(3*i+mu,3*j+nu);
}//nu
}//mu
}//if
////////////////////////////////
}//j
}//i
double**invMb=d2t(3*Nb,3*Nb);
cout<<"ElasticNet::Tracciamento - inverto la matrice"<<endl;
if(invert_symmetric_matrix_lapack(Mb,3*Nb,invMb)<0){cout<<"ERRORE NELL'INVERSIONE"<<endl;return -1;}
const time_t begin_time=time(0);
// cout<<"0^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
IntMatrix schifo(Na,Ma);
cout<<"inizio lo schifo"<<endl;
double**VinvMb;
VinvMb=d2t(3*Na,3*Nb);
cout<<"cacca"<<endl;
#pragma omp parallel for
for(int i=0;i<3*Na;++i){
for(int l=0; l<3*Nb; ++l){
//#pragma omp parallel for
for(int k=0; k<3*Nb; ++k){
VinvMb[i][l]+=V[i][k]*invMb[k][l];
}
}
}
cout<<"cacca"<<endl;
double**merda;
merda=d2t(3*Na,3*Na);
cout << "tempo nel ciclo = "<<difftime(time(0),begin_time)<<endl;
clock_t new_time=clock();
// cout<<"1^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
#pragma omp parallel for
for(int i=0;i<3*Na;++i){
for(int j=0; j<3*Na; ++j){
for(int l=0; l<3*Nb; ++l){
merda[i][j]+=VinvMb[i][l]*V[j][l];
}
}
}
// cout << float( clock () - new_time ) / CLOCKS_PER_SEC<<endl;
new_time=clock();
// cout<<"2^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
for(int i=0;i<3*Na;++i){
for(int j=0;j<3*Na;++j){
Ma[i][j]-=merda[i][j];
}
}
// cout << float( clock () - new_time ) / CLOCKS_PER_SEC<<endl;
// cout<<"3^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"<<endl;
cout<<"finito lo schifo"<<endl;
_intMatrix=(IntMatrix(Na,Ma));
//*** Creo la nuova struttura di riferimento: ***
cout<<"*** Creo la nuova struttura di riferimento: ***"<<endl;
Structure3d new_struc;
BeadList lista(_outBeadList);
lista.setResNum(_outResList);
new_struc.setBeadList(lista);
for(int i=0;i<_N;++i){
// if(cacca[i]==0) cout<<"no! ";
new_struc.addBead(_structure.getBead(i));
// cout<<i<<" cacca "<<new_struc.getSize()<<endl;
}
cout<<new_struc.getSize()<<endl;
setStructure(new_struc);
cout<<"*** *** *** *** *** *** *** *** *** *** *** ***"<<endl;
//*** *** *** *** *** *** *** *** *** *** *** ***
_structure.dumpPDBFile("strutture.pdb");
free_d2t(Ma);
free_d2t(Mb);
free_d2t(V);
free_d2t(invMb);
free_d2t(merda);
free_d2t(VinvMb);
free_i1t(cacca);
free_i1t(prog);
return 0; //success
}
void ElasticNet::dumpDistVecFluc(const char*fname){
/*************************/
char filename[200];
sprintf(filename,"%s_dist_vec_fluc.dat",fname);
ofstream fp;
fp.open(filename);
for(int i=0; i < _N; i++){
for(int j=0; j < _N; j++){
double Ctilde_ii=0.;
double Ctilde_jj=0.;
double Ctilde_ij=0.;
for(int mu=0;mu<3;mu++){
Ctilde_ii+=_covMatrix.GetElement(3*i+mu,3*i+mu);
Ctilde_jj+=_covMatrix.GetElement(3*j+mu,3*j+mu);
Ctilde_ij+=_covMatrix.GetElement(3*i+mu,3*j+mu);
}
double sigma=Ctilde_ii+Ctilde_jj-2*Ctilde_ij;
fp<<i<<" "<<j<<" "<<sigma<<" "
<<_structure.getBead(i).getAtomType()<<" "
<<_structure.getBead(j).getAtomType()<<" "
<<_structure.getBead(i).getResNum()<<" "
<<_structure.getBead(j).getResNum()<<" "
<<endl;
}
}
fp.close();
printf("Fluctuations of distance vectors between beads written on file %s\n",filename);
}//ENDFUNCTION
void ElasticNet::dumpDistFluc(const char*fname){
/*************************/
char filename[200];
sprintf(filename,"%s_dist_fluc.dat",fname);
ofstream fp;