forked from ugobas/tnm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation.c
1437 lines (1324 loc) · 48.3 KB
/
mutation.c
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
#include "coord.h"
#include "tnm.h"
#include "nma.h"
#include "nma_para.h"
#include "allostery.h"
#include "mutation.h"
#include "contacts.h"
#include "vector.h"
#include "atom_numb.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "allocate.h"
#include "contacts.h"
#include "optimization.h"
#include "externals.h"
#include "ridge_regression.h"
float RC=3.25; // length scale for rescaling mutation parameters
float REXP=1; // REXP=RC^EXP_F C_size=C_SIZE*KAPPA*REXP
static int OPT_COEFF=0;
static char OPT_SCORE='C';
// C= Cartesian correlation; A=absolute value correlation;
//static float Lambda=0.10; // Penalization in optimization
static float EXP_FORCE=0, Exp;
float COLL_THR_MUT=0.01; // Minimal collectivity of normal modes to predict mut
//float C_SIZE=0.015, C_STAB=1.4, C_DIST=1.0;
float C_SIZE=0.36, C_STAB=0.22, C_DIST=4.81;
int PRINT_FLANK=1;
float scale_C;
int ncmax=100;
int mut_cont[100], wt_cont[100];
int mut_res[100], wt_res[100];
float Econt_norm[21][21], D_Res_norm[21][21], natm_norm[21];
int ini_fit=1;
float Lambda_RRR=0;
int amax=3;
// Econt D_Residues atom_numb
void Read_pred_para(char *name_in);
void Normalize_parameters();
float Pred_mut_str(float C_size, float C_stab, float C_dist,
float *DE, float *MSD_mut, float *MSD_cross,
float *A_mut, float *A_nomut, float *A_cross, float *offset,
float *pred_mut_3, float *pred_mut_2,
float *pred_mut_tot_2,
int Na, int *n_cont_mut, float *Force, float *Force_coeff,
float *pred_mut_Cart, int N_Cart,
float *pred_mut_Tors, int N_axes,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
struct residue *seq, int Nres, atom *atoms, int *resatom,
struct Reference Ref_kin,
int *kin_atom, struct Normal_Mode NM,
float *sigma2, float **B3, float *B_pred,
float *str_diff_3, float *str_diff_2);
float Optimize_coeff(float *C_size, float *C_stab, float *C_dist,
float *MSD_mut, float *MSD_cross,
float *A_mut,float *A_nomut,float *A_cross,float *offset,
float *pred_mut_3, float *pred_mut_2,
float *pred_mut_tot_2,
int Na, int *n_cont_mut, float *Force, float *Force_coeff,
float *pred_mut_Cart, int N_Cart,
float *pred_mut_Tors, int N_axes,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
struct residue *seq, int Nres, atom *atoms, int *resatom,
struct Reference Ref_kin,
int *kin_atom, struct Normal_Mode NM, float *sigma2,
float **B3, float *B_pred,
float *str_diff_3, float *str_diff_2);
int Mutation_force(float *Force, int N_Cart,
float C_size, float C_stab, float C_dist,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
struct residue *seq, int nres, atom *atoms,
struct Reference Ref_kin, int *kin_atom);
int Mut_neighbor(int *mut_neigh, int Na,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
atom *atoms, int nres);
int Extract_reference_atoms(int *resatom, atom *atoms, int natoms,
struct residue *seq, int Nres);
void Compute_mut_def(float *pred_mut_Cart, int N_Cart,
float *pred_mut_Tors, int N_axes,
float *DE, float *Force_coeff, float *Force,
struct Normal_Mode NM, float *sigma2, float mass);
float *Convert_to_tors(float *Cart, int N3, float **J_ar, int N_axes);
void Match_atoms(int *ref_atom, atom *atoms, int natoms,
int *atom_ref, int N_ref);
float Cosine(float *scale, float *xx, float *yy, int n);
extern float Fit_2(float *Y, float *X0, float *X1, int N,
float *A0, float *A1, float *b, int first);
extern float Fit_1(float *Y, float *X, int N, float *A, float *b);
extern void f_sort(float d[], int n, int *i_rank);
extern void f_Diagonalize(int N, float **MATRIX, float *eigen_values,
float **eigen_vector, int SIGN);
int Mutation(float *mut_Tors_out, int N_axes, float *mut_CC_out,
float KAPPA, struct Reference Ref_kin,
int Nmut, char *AAwt, int *Posmut, char *AAmut,
//int Force_type, 0=size
struct Normal_Mode NM, atom *atoms, int natoms,
struct interaction *Int_list, int N_int,
struct residue *seq,
float *B_pred,
float *Confchange_Cart, struct Reference Ref_CC,
float *Confchange_Tors, float *Tors_fluct,
char *nameout1, int anhar, char *Mut_para)
{
// Determine mutations
if(Nmut==0){
printf("Sorry, no mutation is present to predict its effect\n");
return(0);
}
// Parameters of the prediction
/**************************************************************************
Normalize size, distance and energy param. for calculations
Set parameters for gaps
****************************************************************************/
Read_pred_para(Mut_para); //"Mutation_para.in"
Normalize_parameters();
/****************************************************
Find mutated residues
******************************************************/
printf("Examining the effect of mutations: ");
// Interactions directed along representative atoms
int Nres=atoms[Ref_kin.atom_num[Ref_kin.N_ref-1]].res+1;
int i, imut=1;
char mut_string[1000]="", tmp[200];
for(i=0; i<Nmut; i++){
int pos=Posmut[i];
if((pos<0)||(pos>=Nres)||(Code_AA(AAmut[i])<0)||(Code_AA(AAmut[i])>20)){
printf("\nWARNIG mutation does not exist (only %d residues)\n", Nres);
imut=0;
}
sprintf(tmp, "%c%d%c", AAwt[i], pos, AAmut[i]); strcat(mut_string, tmp);
if(i < (Nmut-1))strcat(mut_string, "_");
}
printf(" %s\n", mut_string);
if(imut==0)return(0);
// Anharmonicity
float *sigma2; char name_har[80];
if(anhar==0){sigma2=NM.sigma2; strcpy(name_har, ""); }
else{sigma2=NM.sigma2_anhar; strcpy(name_har, "_anharmonic");}
/* Three nested sets of atoms:
- All (compute interactions)
- kin (Cartesian normal modes) -> kin_atom
- CC (Conformation change) -> cc_kin
- res (one per residue) -> B_fact
The Cartesian force is computed for kin atoms and
projected onto normal modes to obtain the torsional force
*/
/**************************************************************************
Prepare reference atoms and determine their displacement str_diff_3
****************************************************************************/
// Match any atom to closest kinetic atom
int N_kin=Ref_kin.N_ref, N_Cart=3*N_kin, kin_atom[natoms];
Match_atoms(kin_atom, atoms, natoms, Ref_kin.atom_num, N_kin);
// Structural deformation profile for reference atoms
// Extract one reference atom per residue
char SEL[4]="CA"; if(strcmp(REF_CC, "CB")==0)strcpy(SEL, "CB");
// resatom[i]= Reference atom for residue i
// Center of mass and inertia tensor
//double **corr_sum=Allocate_mat2_d(3, 3);
//double **inertia=Allocate_mat2_d(3, 3);
int Na=0, j, resatom[Nres]; double m_tot=0;
float str_diff_3[3*Nres], *str=str_diff_3;
float str_diff_ave[3]; for(j=0; j<3; j++)str_diff_ave[j]=0;
//float *xr= x_ref;
for(i=0; i<Ref_CC.N_ref; i++){
atom *atm=atoms+Ref_CC.atom_num[i];
if((strncmp(atm->name, SEL, 2)!=0)&&
((seq[atm->res].amm!='G')||(strncmp(atm->name, "CA", 2)!=0)))continue;
resatom[Na]=Ref_CC.atom_num[i];
float m=Ref_CC.mass_atom[i];
float *x=Confchange_Cart+3*i;
//float *r=atoms[resatom[Na]].r;
for(j=0; j<3; j++){
*str=*x; str_diff_ave[j]+=m*(*x); str++; x++; // r++;
}
m_tot+=m; Na++;
}
if(Na!=Nres)printf("WARNING, %d residues expected, %d found\n", Nres, Na);
if(Na > Nres){printf("Leaving\n"); return(0);}
// Subtract center of mass
for(j=0; j<3; j++)str_diff_ave[j]/=m_tot;
float str_diff_2[Na]; //, str_diff_abs[Na];
double RMSD=0;
str=str_diff_3;
for(i=0; i<Na; i++){
double d2=0;
for(j=0; j<3; j++){*str-=str_diff_ave[j]; d2+=(*str)*(*str); str++;}
str_diff_2[i]=d2;
//str_diff_abs[i]=sqrt(d2);
RMSD+=d2;
}
// Predicted fluctuations B1
double B_sum=0; for(i=0; i<Na; i++)B_sum+=B_pred[i];
float MSD_nomut=(B_sum/Na);
//double B_norm=sqrt(B_sum/RMSD);
RMSD=sqrt(RMSD/Na);
printf("RMSD(%s atoms)= %.2f\n", SEL, RMSD);
// Contact list
int *nc, **clist, **cnum;
Get_contact_list(&nc, &clist, &cnum, Nres, atoms, Int_list, N_int);
int mut_neigh[Na],
n_neigh=Mut_neighbor(mut_neigh, Na, AAwt, Posmut, AAmut, Nmut,
clist, cnum, Int_list, atoms, Nres);
/******************** Compute excess confchange ********************/
float ave_diff=0;
for(i=0; i<Na; i++)ave_diff+=str_diff_2[i];
float ratio=B_sum/ave_diff; //j=0;
float excess[Na], max_exc_mut=0, max_exc_nomut=0;
for(i=0; i<Na; i++){
excess[i]=ratio*str_diff_2[i]/B_pred[i];
if(mut_neigh[i]){
if(excess[i]>max_exc_mut)max_exc_mut=excess[i];
}else if(excess[i]>max_exc_nomut){
max_exc_nomut=excess[i];
}
//for(int k=0; k<3; k++){exc_3[j]=str_diff_3[j]/B1[i]; j++;}
}
int N3=3*Na;
float *B3[3];
for(j=0; j<3; j++){
B3[j]=malloc(N3*sizeof(float));
for(i=0; i<N3; i++)B3[j][i]=0;
}
int B_EVEC=0;
/* B_EVEC=1: amax=3, B3[3][n] B3_ji is the j Cartesian component of the
// eigenvector of sum_a s^2_a va_ik va_il for the i atom
// B_EVEC=0: amax=1, B3[1][n] B3_0i = sum_a s_a va_i
*/
if(B_EVEC){
amax=3;
float *B_mat[3]; for(i=0; i<3; i++)B_mat[i]=malloc(3*sizeof(float));
float **e_vec=Allocate_mat2_f(3, 3), e_val[3];
for(i=0; i<Na; i++){
int jj=3*kin_atom[resatom[i]], k;
for(j=0; j<3; j++)for(k=0; k<=j; k++)B_mat[j][k]=0;
for(int a=0; a<NM.N; a++){
if(sigma2[a]<=0)continue;
float *va=NM.Cart[a]+jj;
for(j=0; j<3; j++){
float sv=sigma2[a]*va[j];
for(k=0; k<=j; k++)B_mat[j][k]+=sv*va[k];
}
} // end normal modes, symmetrize:
for(j=0; j<3; j++){
for(k=j+1; k<3; k++)B_mat[j][k]=B_mat[k][j];
}
f_Diagonalize(3, B_mat, e_val, e_vec, 1);
jj=3*i;
for(int a=0; a<3; a++){
float lam=sqrt(e_val[a]), *ev=e_vec[a], *B=B3[a];
for(j=0; j<3; j++){B[jj]=lam*ev[j]; jj++;}
}
} // end i
}else{
amax=1;
for(int a=0; a<NM.N; a++){
if(sigma2[a]<=0)continue;
float om_minus_1=sqrt(sigma2[a]), *va=NM.Cart[a]; int k=0;
for(i=0; i<Na; i++){
int jj=3*kin_atom[resatom[i]];
for(j=0; j<3; j++){B3[0][k]+=om_minus_1*va[jj]; jj++; k++;}
}
}
}
/************************************************************************
Prepare output
*************************************************************************/
// Header
char header[2000];
sprintf(header,
"# RMSD(%s atoms)= %.2f N= %d residues n= %d kinetic atoms\n",
SEL, RMSD, Nres, Ref_kin.N_ref);
if(Nres!=Na){
sprintf(tmp, "# gap= %d residues\n", Nres-Na); strcat(header, tmp);
}
sprintf(tmp,
"# RMSD predicted (no mutation): %.3g "
"with force constant kappa= %.3g\n", sqrt(MSD_nomut), KAPPA);
strcat(header, tmp);
//Correlation predicted_fluctuations confchange (Cartesian):
float slope, offset, r_CC_B;
/*r_CC_B=Corr_coeff(B1, str_diff_abs, Na, &slope, &offset);
sprintf(tmp, "# Correlation predicted_fluct confchange (abs): %.3f"
" slope= %.3g offset= %.3g\n", r_CC_B, slope, offset);
strcat(header, tmp); */
r_CC_B=Corr_coeff(B_pred, str_diff_2, Na, &slope, &offset);
sprintf(tmp, "# Correlation predicted_fluct confchange (^2) : %.3f"
" slope= %.3g offset= %.3g\n", r_CC_B, slope, offset);
strcat(header, tmp);
//Correlation predicted_fluctuations confchange (torsional):
float Tors_fluct_obs[N_axes];
for(i=0; i<N_axes; i++)
Tors_fluct_obs[i]=Confchange_Tors[i]*Confchange_Tors[i];
float r=Corr_coeff(Tors_fluct, Tors_fluct_obs, N_axes, &slope, &offset);
strcat(header, "# Correlation predicted_Tors_fluct Tors_confchange:");
sprintf(tmp, " %.3f slope=%.2g offset=%.2g\n", r,slope,offset);
strcat(header, tmp);
sprintf(tmp, "# Mut: Max.conf_change^2(obs/pred)= %.3g\n"
"# No-mut: Max.conf_change^2(obs/pred)= %.3g\n",
max_exc_mut, max_exc_nomut);
strcat(header, tmp);
char nameout[200];
sprintf(nameout, "%s%s_EXPF%.1f_pred_mut.dat",
nameout1, name_har, EXP_FORCE); //, OPT_SCORE
FILE *file_out=fopen(nameout, "w");
fprintf(file_out, "%s", header);
fprintf(file_out, "# Input value of force constant: %.4g\n", KAPPA);
fprintf(file_out, "# Prediction based on %d mutations: %s\n",
Nmut, mut_string);
fprintf(file_out, "# Predicted Mut force based on combination of changes "
" of size, stability and optimal distance\n");
fprintf(file_out, "# Mut.force directed along axes between contacts,"
"each weighted with r^-%.1f\n", EXP_FORCE);
/**************************************************************************
Compute predicted mutations
****************************************************************************/
// Exponent of the force for computation
Exp=(1.+EXP_FORCE)/2;
REXP=pow(RC, EXP_FORCE);
scale_C=KAPPA*REXP;
// prediction based on combination of size, stability, distance
// Coefficients for the computation of the force
/*if(C_SIZE==0){C_SIZE=1;}
if(C_DIST==0){C_DIST=1;}
if(C_STAB==0){C_STAB=1;}*/
float C_size=C_SIZE*scale_C, C_stab=C_STAB*scale_C, C_dist=C_DIST*scale_C;
float Force[N_Cart], Force_coeff[NM.N];
float pred_mut_Cart[N_Cart], pred_mut_Tors[N_axes],
pred_mut_3[Na], pred_mut_tot_2[Na], pred_mut_2[N3]; //, pred_mut_abs[Na]
float sigma2_sc[NM.N], B_pred_sc[Na], *B3_sc[3]; // B1_sc[Na];
for(i=0; i<NM.N; i++)sigma2_sc[i]=sigma2[i];
for(i=0; i<Na; i++){
B_pred_sc[i]=B_pred[i];
//B1_sc[i]=B1[i];
}
for(j=0; j<3; j++){
B3_sc[j]=malloc(N3*sizeof(float));
for(i=0; i<N3; i++)B3_sc[j][i]=B3[j][i];
}
float Kappa_sc=KAPPA, MSD_nomut_sc=MSD_nomut;
// Loop on predictions
int N_force=1; if(OPT_COEFF)N_force++;
float cc_opt=0; float DE=0;
for(int iforce=0; iforce<N_force; iforce++){
ini_fit=1;
// Compute the force due to the mutation
if(iforce)Kappa_sc=KAPPA;
scale_C=Kappa_sc*REXP;
float cc, MSD_mut, MSD_cross, A_mut=0,A_nomut=0,A_cross=0,offset,scale=1;
int n_cont_mut;
if(OPT_COEFF && iforce==1){
// Optimize coefficients
cc=Optimize_coeff(&C_size, &C_stab, &C_dist, &MSD_mut, &MSD_cross,
&A_mut, &A_nomut, &A_cross, &offset,
pred_mut_3, pred_mut_2, pred_mut_tot_2, Na,
&n_cont_mut, Force, Force_coeff,
pred_mut_Cart, N_Cart, pred_mut_Tors, N_axes,
AAwt, Posmut, AAmut, Nmut,
clist, cnum, Int_list, seq, Nres,
atoms, resatom, Ref_kin, kin_atom,
NM, sigma2_sc, B3_sc, B_pred_sc, //, B1_sc
str_diff_3, str_diff_2); //, str_diff_abs
}else{
cc=Pred_mut_str(C_size, C_stab, C_dist, &DE, &MSD_mut, &MSD_cross,
&A_mut, &A_nomut, &A_cross, &offset,
pred_mut_3, pred_mut_2, pred_mut_tot_2, Na,
&n_cont_mut, Force, Force_coeff,
pred_mut_Cart, N_Cart, pred_mut_Tors, N_axes,
AAwt, Posmut, AAmut, Nmut,
clist, cnum, Int_list, seq, Nres,
atoms, resatom, Ref_kin, kin_atom,
NM, sigma2_sc, B3_sc, B_pred_sc,
str_diff_3, str_diff_2); //, pred_mut_abs[Na]
}
printf("Mut_pred_score: %.4g Weights: %.3g %.3g %.3g ",
cc, C_size/scale_C, C_stab/scale_C, C_dist/scale_C);
printf("Predicted RMSD due to mutation= %.3g\n", sqrt(MSD_mut));
if(cc>cc_opt){cc_opt=cc;}
/***********************************************************
Print and store the prediction
*******************************************************/
// Print
if(iforce==0){
// Columns printed in the output file for each prediction type
fprintf(file_out, "# %d contacts of mutated residues:\n#", n_neigh);
//fprintf(file_out, "# Prediction based on %d contacts: ", n_cont_mut);
for(i=0; i<n_cont_mut; i++){
fprintf(file_out, " %c%d-%c%d",
AA_code[wt_cont[i]], wt_res[i],
AA_code[mut_cont[i]], mut_res[i]);
if(i==ncmax)break;
}
fprintf(file_out, "\n");
fprintf(file_out,
"# MSD_tot=A_mut*MSD_mut+A_nomut*MSD_nomut+A_cross*MSD_cross\n");
fprintf(file_out, "#1=MSD 2=MSD_pred(tot) "
"3=Correlation(confchange^2,pred_tot^2) "
"4=MSD_pred(mut) 5=MSD_pred(no mut) 6=MSD_cross 7=offset "
"8=A_mut 9=A_nomut 10=A_cross ");
//"11=Cosine(confchange,pred_mut) 12=scale_cosine");
//" 10=Corr(pred_mut,confchange)_tors 11=slope 12=offset\n");
fprintf(file_out, "\n#\n");
}
for(int iter=0; iter<5; iter++){
if(iter){ // rescale mutation parameters and force constant
if(iforce==0)continue;
printf("Rescaling weights and Kappa, iter= %d\n", iter);
ini_fit=1;
cc=Pred_mut_str(C_size, C_stab, C_dist, &DE, &MSD_mut, &MSD_cross,
&A_mut, &A_nomut, &A_cross, &offset,
pred_mut_3, pred_mut_2, pred_mut_tot_2, Na,
&n_cont_mut, Force, Force_coeff,
pred_mut_Cart, N_Cart, pred_mut_Tors, N_axes,
AAwt, Posmut, AAmut, Nmut,
clist, cnum, Int_list, seq, Nres,
atoms, resatom, Ref_kin, kin_atom,
NM, sigma2_sc, B3_sc, B_pred_sc,
str_diff_3, str_diff_2); //, str_diff_abs
printf("Mut_pred_score: %.4g Weights: %.3g %.3g %.3g ",
cc, C_size/scale_C, C_stab/scale_C, C_dist/scale_C);
printf("Predicted RMSD due to mutation= %.3g\n", sqrt(MSD_mut));
}
scale_C=REXP*Kappa_sc;
fprintf(file_out,"# Force constant: %.4g ", Kappa_sc);
fprintf(file_out," C_SIZE= %.3g C_STAB= %.3g C_DIST= %.3g ",
C_size/scale_C, C_stab/scale_C, C_dist/scale_C);
if(iforce==1 && OPT_COEFF){fprintf(file_out," optimized");}
else{fprintf(file_out, " input");}
fprintf(file_out, " weights");
if(iter)fprintf(file_out, ", rescaled by %.3g", scale);
fprintf(file_out, "\n");
fprintf(file_out, "%.3f", RMSD*RMSD);
float MSD_all=A_mut*MSD_mut+A_nomut*MSD_nomut_sc+A_cross*MSD_cross;
fprintf(file_out, "\t%.3f", MSD_all);
fprintf(file_out, "\t%.4g", cc);
fprintf(file_out, "\t%.3f", MSD_mut);
fprintf(file_out, "\t%.3f", MSD_nomut_sc);
fprintf(file_out, "\t%.3f", MSD_cross);
fprintf(file_out, "\t%.3g\t%.3g\t%.3g\t%.3g",
offset, A_mut, A_nomut, A_cross);
//float c_scale, cos=Cosine(&c_scale, pred_mut_3, str_diff_3, N3);
//fprintf(file_out, "\t%.3g\t%.3g", cos, c_scale);
//Correlation predicted_mutation confchange (torsional):
//for(i=0; i<N_axes; i++)pred_mut_Tors[i]*=pred_mut_Tors[i];
//r=Corr_coeff(pred_mut_Tors, Confchange_Tors, N_axes, &slope, &offset);
//fprintf(file_out, "\t%.3f\t%.2g\t%.2g", r, slope, offset);
fprintf(file_out, "\n");
float scale_K2=1;
if(iforce){
// Rescale parameters for next iteration
if(A_nomut>0)scale_K2=A_nomut;
scale=1./scale_K2;
if(A_mut>0)scale*=sqrt(A_mut);
C_size*=scale; C_stab*=scale; C_dist*=scale;
for(i=0; i<NM.N; i++)sigma2_sc[i]*=scale_K2;
float scale_K1=sqrt(scale_K2);
for(i=0; i<Na; i++){
B_pred_sc[i]*=scale_K2;
for(j=0; j<3; j++)B3_sc[j][i]*=scale_K1;
}
Kappa_sc/=scale_K2;
MSD_nomut_sc*=scale_K2;
fprintf(file_out,"# Next force constant: %.4g ", Kappa_sc);
if(A_nomut<=0){ // Do not rescale
fprintf(file_out,"(Not rescaled, A_nomut=%.2g<=0)\n", A_nomut);
}else{
fprintf(file_out,"(Kappa/A_nomut=%.3g)\n", A_nomut);
}
fprintf(file_out,"# Next parameters: "
"C_SIZE= %.3g C_STAB= %.3g C_DIST= %.3g ",
C_size/scale_C, C_stab/scale_C, C_dist/scale_C);
if(scale==1){ // Do not rescale
fprintf(file_out,"(Not rescaled, A=%.2g)\n#\n", A_nomut);
}else{
fprintf(file_out,"(C*scale=%.3g)\n#\n", scale);
}
}
if(fabs(scale-1)<0.05 && fabs(scale_K2-1)<0.05)break;
} // end iter
if(0){
// Print contribution of normal modes to the prediction
int N_print=30, a; char nameout[200];
sprintf(nameout, "%s%s_mutation_%d.dat", nameout1, name_har, iforce);
FILE *file_out=fopen(nameout, "w");
printf("Writing %s\n", nameout);
float kappa=Collectivity_norm2(Force, N_Cart);
fprintf(file_out, "# Collectivity of force: %.2f\n", kappa/3);
fprintf(file_out,"#mod sigma2 contr2cc contr2force collectivity\n");
double sum=0;
for(a=0; a<NM.N_relevant; a++){
Force_coeff[a]*=Force_coeff[a]; sum+=Force_coeff[a];
}
for(a=0; a<N_print; a++){
fprintf(file_out, "%d\t%.4f\t%.4f\t%.4f\t%.3f\n", //
a, sigma2[a], NM.confchange2[a],
Force_coeff[a]/sum, NM.Cart_coll[a]);
}
fclose(file_out);
}
} // end iforce
printf("Writing %s\n", nameout);
fclose(file_out);
// Output predicted deformation of reference atoms mut_CC
float *m_CC=mut_CC_out;
for(i=0; i<Ref_CC.N_ref; i++){
float *x=pred_mut_Cart+3*kin_atom[Ref_CC.atom_num[i]];
for(int j=0; j<3; j++){*m_CC=*x; x++; m_CC++;}
}
for(i=0; i<N_axes; i++)mut_Tors_out[i]=pred_mut_Tors[i];
if(0){
// Print on screen (disabled)
printf("Force: ");
for(i=0; i<N_Cart; i++)printf(" %.2f", Force[i]);
printf("\n");
printf("mut_Cart: ");
for(i=0; i<N_Cart; i++)printf(" %.2f",pred_mut_Cart[i]);
printf("\n");
printf("mut_CC: ");
for(i=0; i<Ref_CC.N_Cart; i++)printf(" %.2f", mut_CC_out[i]);
printf("\n");
printf("mut_Tors: ");
for(i=0; i<N_axes; i++)printf(" %.2f", mut_Tors_out[i]);
printf("\n");
}
/******************************************************************/
if(1){
// Write excess_confchange and predicted mutations for every residue
sprintf(nameout, "%s%s_EXPF%.1f_mutation.dat", nameout1,name_har,EXP_FORCE);
file_out=fopen(nameout, "w");
fprintf(file_out, "%s", header);
fprintf(file_out,"#1=conf_ch^2 2=pred_tot^2 3=pred_mut^2 4=mut_neighbor");
//int k; for(k=1; k<=4; k++)fprintf(file_out, " %d=pred_mut_%d", k+4, k);
fprintf(file_out, " pos aa\n");
for(i=0; i<Na; i++){
fprintf(file_out, "%.3g\t%.3g\t%.4g\t%d",
str_diff_2[i], pred_mut_tot_2[i], pred_mut_2[i], mut_neigh[i]);
//for(k=0; k<N_TYPES; k++)
//fprintf(file_out, "\t%.3g", pred_mut_abs_f[k][i]);
int a=atoms[resatom[i]].res;
fprintf(file_out, "\t%s\t%c\n", seq[a].pdbres, seq[a].amm);
}
fclose(file_out);
printf("Writing %s\n", nameout);
}
/******************************************************************/
// Clean memory
Empty_matrix_i(clist, Nres);
Empty_matrix_i(cnum, Nres); free(nc);
for(j=0; j<3; j++){
free(B3[j]); free(B3_sc[j]);
}
return(1);
}
float Pred_mut_str(float C_size, float C_stab, float C_dist,
float *DE, float *MSD_mut, float *MSD_cross,
float *A_mut, float *A_nomut, float *A_cross, float *offset,
float *pred_mut_3, float *pred_mut_2, float *pred_mut_tot_2,
int Na, int *n_cont_mut, float *Force, float *Force_coeff,
float *pred_mut_Cart, int N_Cart,
float *pred_mut_Tors, int N_axes,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
struct residue *seq, int Nres, atom *atoms, int *resatom,
struct Reference Ref_kin,
int *kin_atom, struct Normal_Mode NM,
float *sigma2, float **B3, float *B_pred,
float *str_diff_3, float *str_diff_2)
{
// Compute force
*n_cont_mut=
Mutation_force(Force, N_Cart, C_size, C_stab, C_dist,
AAwt, Posmut, AAmut, Nmut, clist, cnum, Int_list,
seq, Nres, atoms, Ref_kin, kin_atom);
// Compute deformation mut_Cart and mut_Tors
Compute_mut_def(pred_mut_Cart, N_Cart, pred_mut_Tors, N_axes,
DE, Force_coeff, Force, NM, sigma2, Ref_kin.mass_sum);
// Deformation of resatom mut_3, mut_abs2 and MSD_mut
float pred_mut_ave[3], *str=pred_mut_3; int i, j;
for(j=0; j<3; j++)pred_mut_ave[j]=0;
for(i=0; i<Na; i++){
float *x=pred_mut_Cart+3*kin_atom[resatom[i]];
for(j=0; j<3; j++){*str=*x; pred_mut_ave[j]+=*x; x++; str++;}
}
// Subtract center of mass
for(j=0; j<3; j++)pred_mut_ave[j]/=Na;
*MSD_mut=0; str=pred_mut_3;
for(i=0; i<Na; i++){
double d2=0;
for(j=0; j<3; j++){*str-=pred_mut_ave[j]; d2+=(*str)*(*str); str++;}
*MSD_mut+=d2;
pred_mut_2[i]=d2;
}
*MSD_mut/=Na;
//for(i=0; i<20; i++){printf("%.3g ",pred_mut_2[i]);} printf("\n");
//printf("Predicted MSD: %.4g\n", *MSD_mut);
*MSD_cross=0;
// No mutant structure is present for computing observed differences
if((str_diff_3==NULL)||(str_diff_2==NULL))return(0);
// pred_mut_cross[i]= 2 pred_mut[i] X B3[i]
// B3_0i = sum_a s_a va_i (B_EVEC==0)
// Does it make sense? va_i and -va_i are equivalent,
// the dot product should be zero...
float pred_mut_cross[Na];
for(i=0; i<Na; i++){pred_mut_cross[i]=0;}
for(int a=0; a<amax; a++){ // amax=1 (B_EVEC==0) or 3 (B_EVEC==1)
float *bb=B3[a]; str=pred_mut_3;
// Scalar product pred_mut_3*B3
for(i=0; i<Na; i++){
double d_cross=0;
for(j=0; j<3; j++){d_cross+=(*str)*(*bb); str++; bb++;}
pred_mut_cross[i]+=d_cross*d_cross;
}
}
(*MSD_cross)=0;
for(i=0; i<Na; i++){
pred_mut_cross[i]=4*sqrt(pred_mut_cross[i]); // Note the factor 4!
(*MSD_cross)+=pred_mut_cross[i];
}
*MSD_cross/=Na;
// Fit observed deformation profile squared str_diff_2[i]
float score;
int Npar=3;
float D_out[Npar], Y_pred[Na], *X[Na];
for(i=0; i<Na; i++){
X[i]=malloc(Npar*sizeof(float));
X[i][0]=pred_mut_2[i];
X[i][1]=B_pred[i];
X[i][2]=pred_mut_cross[i];
}
struct ridge_fit fit; fit.A=malloc(Npar*sizeof(float)); char type;
if(ini_fit){type='C';} // Specific-heat fit
else{type='L'; fit.Lambda=Lambda_RRR;}
score=Ridge_regression(&fit, Y_pred, D_out, "ridge_regression",
X, str_diff_2, Na, Npar, type);
printf("RRR3: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
fit.A[0], fit.A[1], fit.A[2]);
if(fit.A[0]<=0 || fit.A[1]<=0){ //*A_mut<=0 *A_nomut<0
// Fit without A_cross
fit.A[2]=0; Npar=2;
score=Ridge_regression(&fit, Y_pred, D_out, "ridge_regression",
X, str_diff_2, Na, Npar, type);
printf("RRR2: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
fit.A[0], fit.A[1], fit.A[2]);
}
Lambda_RRR=fit.Lambda;
*A_mut=fit.A[0];
*A_nomut=fit.A[1];
*A_cross=fit.A[2];
*offset=0;
for(i=0; i<Na; i++){
(*offset)+=str_diff_2[i]-Y_pred[i];
pred_mut_tot_2[i]=Y_pred[i];
free(X[i]);
}
(*offset)/=Na;
free(fit.A);
ini_fit=0;
if(*A_nomut<=0){
// Fit only with the predicted effect of mutation
score=Fit_1(str_diff_2, pred_mut_2, Na, A_mut,offset);
*A_nomut=0; *A_cross=0;
printf("Fit1: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
*A_mut, *A_nomut, *A_cross);
}else if(*A_mut<=0){
// Fit only with thermal fluctuations
score=Fit_1(str_diff_2, B_pred, Na, A_nomut,offset);
*A_mut=0; *A_cross=0;
printf("Fit1: A_mut= %.3g A_nomut= %.3g A_cross= %.3g\n",
*A_mut, *A_nomut, *A_cross);
}
return(score);
}
int Mutation_force(float *Force, int N_Cart,
float C_size, float C_stab, float C_dist,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
struct residue *seq, int nres, atom *atoms,
struct Reference Ref_kin, int *kin_atom)
{
int am=-1, ai, j, nc=0, iter;
for(j=0; j<N_Cart; j++)Force[j]=0;
for(int kmut=0; kmut<Nmut; kmut++){
int pmut=Posmut[kmut];
int aaw=Code_AA(AAwt[kmut]); // wild-type aa
int aam=Code_AA(AAmut[kmut]); // mutated aa
double dsize=(natm_norm[aam]-natm_norm[aaw]);
if((aaw==20)||(aam==20)){iter=2;} // gap!
else{iter=1;}
/* Since atoms in the gap are removed, we have to analyze
the flanking residues */
int p1=-1;
for(int it=0; it<iter; it++){
if(iter==2){
if(it==0){
while((pmut>=0)&&(clist[pmut][0]<=0))pmut--;
if(pmut<0)continue;
p1=pmut;
}else if(it==1){
pmut=Posmut[kmut]+1; while((pmut<nres)&&(clist[pmut][0]<=0))pmut++;
if(pmut>=nres)continue;
}
}
int *ic=clist[pmut]; // residue in contact with pmut
int *ii=cnum[pmut]; // interaction label of the contact
while(*ic >= 0){
//if(abs(pmut-(*ic))<3)goto next; // short range contact discarded
int i1=Int_list[*ii].i1, i2=Int_list[*ii].i2;
if(atoms[i1].res==pmut){
am=i1; ai=i2;
}else if(atoms[i2].res==pmut){
am=i2; ai=i1;
}else{
printf("ERROR in mutation, interaction %d res %d - %d pmut= %d\n",
*ii, atoms[i1].res, atoms[i2].res, pmut); exit(8);
}
int aai=Code_AA(seq[*ic].amm);
ic++; ii++;
int res_i=atoms[ai].res;
//if((abs(res_i-pmut)<3))goto next; //&&(FORCE_TYPE==1)
// contacts of main chain atoms of mutant residue are discarded (?)
if(PRINT_FLANK &&(res_i==p1)){
printf("Gap flanking residues %d and %d\n", p1, pmut);
PRINT_FLANK=0;
//}else if(Mainchain(atoms[am].name)){
//continue; //&& FORCE_TYPE!=3
}
float f =C_size*dsize;
f+=C_dist*(D_Res[aam][aai]-D_Res[aaw][aai]);
float dE=(Econt[aam][aai]-Econt[aaw][aai]);
if(((aaw==20)&&(dE<0))|| // Insertion, attracting
((aam==20)&&(dE>0)))continue; // Deletion, repelling
f+=C_stab*dE;
// Record contacts considered for force computation
if(nc<ncmax){
mut_cont[nc]=aai; mut_res[nc]=res_i;
wt_cont[nc]=aaw; wt_res[nc]=pmut;
}
nc++;
double r[3], r2=0, *rm=atoms[am].r, *ri=atoms[ai].r;
for(j=0; j<3; j++){
r[j]=rm[j]-ri[j]; r2+=r[j]*r[j];
}
f/=pow(r2, Exp);
int km=3*kin_atom[am], ki=3*kin_atom[ai];
for(j=0; j<3; j++){
float fj=f*r[j];
Force[km+j]+=fj; //?
Force[ki+j]-=fj;
}
} // end contact
} // end iter (in case of gap)
}
//printf("Force computation based on %d contacts\n", nc);
return(nc);
}
void Compute_mut_def(float *pred_mut_Cart, int N_Cart,
float *pred_mut_Tors, int N_axes,
float *DE, float *Force_coeff, float *Force,
struct Normal_Mode NM, float *sigma2, float mass)
{
int a, i;
for(i=0; i<N_Cart; i++)pred_mut_Cart[i]=0;
for(i=0; i<N_axes; i++)pred_mut_Tors[i]=0;
/*double F=0;
for(i=0; i<N_Cart; i++){F+=Force[i]*Force[i];}
printf("RMS of mutational force: %.3f\n", sqrt(3*F/N_Cart)); */
double DE_tmp=0;
// Compute predicted deformation of kinetic atoms (Cart)
for(a=0; a<NM.N; a++){
if((NM.Cart_coll[a]<COLL_THR_MUT)||(sigma2[a]<=0)){
Force_coeff[a]=0; continue;
}
float *f=Force, *x=NM.Cart[a];
double xf=0; // Projection of force on normal mode
for(i=0; i<N_Cart; i++){xf+=(*x)*(*f); x++; f++;}
Force_coeff[a]=xf;
//(*DE)+=xf*xf/sigma2[a];
DE_tmp+=xf*xf*sigma2[a]; // OK
xf*=sigma2[a];
float *m=pred_mut_Cart; x=NM.Cart[a];
for(i=0; i<N_Cart; i++){
(*m)+=(*x)*xf; x++; m++;
}
m=pred_mut_Tors; x=NM.Tors[a];
for(i=0; i<N_axes; i++){
(*m)+=(*x)*xf; x++; m++;
}
}
*DE=DE_tmp;
}
int Mainchain(char *atom_name){
if(strncmp(atom_name,"N ",2)==0)return(1);
if(strncmp(atom_name,"CA",2)==0)return(1);
if(strncmp(atom_name,"C ",2)==0)return(1);
if(strncmp(atom_name,"O ",2)==0)return(1);
return(0);
}
float *Convert_to_tors(float *Cart, int N3, float **J_ar, int N_axes)
{
// Torsional force = J^t f = sum_i dr_i/dphi_a f_i
float *Tors=malloc(N_axes*sizeof(float)); int a, i;
for(a=0; a<N_axes; a++){
double sum=0; float *J=J_ar[a], *f=Cart;
for(i=0; i<N3; i++){sum+=(*J)*(*f); J++; f++;}
Tors[a]=sum;
}
return(Tors);
}
void Match_atoms(int *ref_atom, atom *atoms, int natoms,
int *atom_ref, int N_ref)
{
/* For every atom i=1,natoms ref_atom[i] is the index of the reference
atom that is closest to i */
int i; for(i=0; i<natoms; i++)ref_atom[i]=-1;
for(i=0; i<N_ref; i++)ref_atom[atom_ref[i]]=i;
int Nr1=N_ref-1, ini_r=0, n=0;
for(i=0; i<natoms; i++){
if(ref_atom[i]>=0)continue;
n++;
atom *atom=atoms+i, *atom2;
while((ini_r<Nr1)&&(atoms[atom_ref[ini_r]].res<atom->res))ini_r++;
if(atoms[atom_ref[ini_r]].res != atom->res){
printf("ERROR unmatch_atom in residue %c%d\n", atom->aa, atom->res);
exit(8);
}
float d2_min=1000; int ir=ini_r, ir_min=-1;
while(ir<N_ref){
atom2=atoms+atom_ref[ir];
if(atom2->res==atom->res){
float d2=Distance_square(atom->r, atom2->r);
if((d2<d2_min)||(ir_min<0)){d2_min=d2; ir_min=ir;}
}
ir++;
}
if(ir_min<0){
printf("ERROR, reference atom %d in atom %d residue %c%d not valid\n",
ir_min, i, atom->aa, atom->res);
}
ref_atom[i]=ir_min;
}
if((N_ref+n)!=natoms){
printf("ERROR, N_ref+%d=%d atoms matched over %d\n",
n, N_ref+n, natoms); exit(8);
}
if(0){
for(i=0; i<natoms; i++){
atom *atom=atoms+i;
printf("%d %s(%c%d) ", i, atom->name, atom->aa, atom->res);
int j=atom_ref[ref_atom[i]]; atom=atoms+j;
printf(" ref: %d %s(%c%d)\n", j, atom->name, atom->aa, atom->res);
}
exit(8);
}
}
float Cosine(float *scale, float *xx, float *yy, int n)
{
double sum_xy=0, sum_x2=0, sum_y2=0;
float *x=xx, *y=yy;
for(int i=0; i<n; i++){
sum_xy+=(*x)*(*y);
sum_x2+=(*x)*(*x);
sum_y2+=(*y)*(*y);
x++; y++;
}
if(sum_x2){
*scale=sqrt(sum_y2/sum_x2);
return(sum_xy/sqrt(sum_x2*sum_y2));
}else{
*scale=0;
return(0);
}
}
int Mut_neighbor(int *mut_neigh, int Na,
char *AAwt, int *Posmut, char *AAmut, int Nmut,
int **clist, int **cnum, struct interaction *Int_list,
atom *atoms, int nres)
{
int i, iter;
for(i=0; i<Na; i++)mut_neigh[i]=0;
for(int kmut=0; kmut<Nmut; kmut++){
int pmut=Posmut[kmut];
int aaw=Code_AA(AAwt[kmut]); // wild-type aa
int aam=Code_AA(AAmut[kmut]); // mutated aa
if((aaw==20)||(aam==20)){pmut--; iter=2;} //
else{iter=1;}
for(int it=0; it<iter; it++){
if(pmut<0)continue;
if(it==1){pmut=Posmut[kmut]+1; if(pmut>=nres)continue;}
int *ii=cnum[pmut]; // interaction label of the contact
int *ic=clist[pmut]; // residue in contact with pmut
while(*ic >= 0){
//if(abs(pmut-(*ic))<3)goto next; // short range contact discarded
int res1=atoms[Int_list[*ii].i1].res, res2=atoms[Int_list[*ii].i2].res;
if((res1 !=pmut)&&(res2!=pmut)){
printf("ERROR in mutation, interaction %d res %d - %d pmut= %d\n",
*ii, res1, res2, pmut); exit(8);
}
mut_neigh[res1]=1;
mut_neigh[res2]=1;
ic++; ii++;
}
}
}
int n=0; for(i=0; i<Na; i++)if(mut_neigh[i])n++;
return(n-1);
}
void Read_pred_para(char *name_in){
FILE *file_in=fopen(name_in, "r");
if(file_in==NULL){
printf("WARNING, parameter file %s not found\n", name_in);