-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVcontacts-v1-2.c
1943 lines (1722 loc) · 67.5 KB
/
Vcontacts-v1-2.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
/**********************************************************************
* PROGRAM Vcontacts.c
* calculates the atom-atom contacts within a protein, along with the
* solvent accessible surface.
*
* Please cite the following article as a reference:
* Quantification of protein surfaces, volumes and atom-atom contacts using a constrained Voronoi procedure
* B.J.McConkey, V. Sobolev, and M. Edelman (2002), Bioinformatics, 18:1365-1373.
* ====================================================================
* NOTES ON METHODOLOGY:
*
* The method calculates the planes of contact the center atom makes
* will each atom in the contact list, then determines the points
* of intersection of the planes and intersections with a sphere. The
* sphere has a radius equal to the sum of the van der Waals radius of the
* center atom plus the radius of a solvent atom (water). The set of
* intersections of the planes defines a polygon surrounding the center
* atom. This polygon is projected onto the surface of the sphere. The
* area of each projection is calculated as the sum of spherical triangles
* and arc segments, where an arc segment is the difference between a
* angluar segment of a spherical cap and the corresponding spherical triangle.
*
* The points of intersection are calculated using a convex hull algorithm.
* The algorithm operates with efficiency O(Nk), where N is the number of
* input atoms and k is the number of edges on the contact polyhedron.
*
* The convex hull algorithm effectively describes the atom contacts only if
* no 'engulfing' atoms are present, ie., atoms which cover more than 50% of
* the atom contact surface. This may occur between closely bonded atoms of
* different radii, such as some C=O bonds. A correction factor accounts for
* this.
**********************************************************************/
#include "Vpoly.h"
#define PI 3.14159265 // pi.
#define Rw 1.4 // radius of water
#define PAUSE ch=getchar() // for debugging
// ------------------ structure definitions --------------------
struct plane {
double Ai[4]; // parameters A,B,C,D of contact plane Ax+By+Cz+D=0
double dist; // distance from plane to origin
int index; // index to which record in PDB or ligand array.
double area; // contact area in square angstroms
char flag; // 'X' if no contact, 'E' if an engulfing atom.
};
struct vertex {
double xi[3]; // x,y,z coordinates (x1,x2,x3)
double dist; // distance to origin
int plane[3]; // identification of intersecting planes. -1 = sphere.
};
struct ptindex {
int numpts; // number of points defining face
int pt[40]; // index to polyhedron points
};
struct edgevector {
double V[3]; // vector for edge
int startpt; // initial vertex
int endpt; // final vertex
int plane[2]; // planes defining edge (using single atoms for now)
int startplane; // third plane at start point
int endplane; // third plane at end point
char arc; // flag for arc point calculations
};
struct ca_struct {
long int prev; // previous contact location in ca_index
long int atom; // PDBarray number of current contact (NOT PDB record number)
float area; // contact area
float dist; // distance between atoms
};
// ----------------- global variables -------------------
struct ptindex ptorder[100]; // for ordering vertices around each face
struct vertex centerpt[100]; // center points for each contact face
struct vertex poly[200]; // polyhedron vertices
struct plane cont[100]; // atom and contact plane information
struct edgevector vedge[200];
struct ca_struct *ca_rec; // array - contact area records
long int *ca_index; // array - index to first ca_recs for each atom.
long int numcarec = 0;
long int ca_recsize;
int dim; // size of side of box, in units CELLSIZE.
float globalmin[3]; // minimum coordinate value for protein atoms
float globalmax[3]; // maximum coordinate value for protein atoms
char ch; // for debugging
char planedef; // = X, R, or B
char showbonded; // = Y or N.
char normalize; // = Y or N. normalize areas to area of sphere.
long int *seed; // seed vertices for new polyhedra
// --------------------- function prototypes ----------------------
double spherical_arc(struct vertex ptAo, struct vertex ptB, struct vertex ptC, float rado);
double cosPQR( double ptP[], double ptQ[], double ptR[]);
char test_point(double ptX[], struct plane cont[], int NC, float rado, int planeA, int planeB, int planeC);
char order_faces(int atomzero, struct vertex poly[], struct vertex centerpt[], float rado, int NC,
int NV, struct plane cont[], struct ptindex ptorder[]);
void project_points(struct vertex poly[], struct vertex centerpt[], float rado, int NC,
int NV, struct plane cont[]);
int voronoi_poly2(struct atom PDB[], int atomzero, struct plane cont[], float rado,
int NC, struct contactlist contlist[]);
int add_vertex(struct vertex poly[], int vn, double coor[], int planeA, int planeB, int planeC);
void add_vedge(struct edgevector vedge[], int edgenum, struct plane cont[], int plane0, int plane1,
int testplane, struct vertex poly[], int startpt);
int solve_3x3(double eq0[], double eq1[], double eq2[], double pt[]);
int solve_2xS(struct plane eq0, struct plane eq1, float rado, double pt0[], double pt1[]);
int calc_region(struct atom PDB[], int PDBtot, int atomlist[], int numatoms);
void calc_areas(struct vertex poly[], struct vertex centerpt[], float rado, int NC, int NV,
struct plane cont[], struct ptindex ptorder[], int atomzero);
int index_protein(int PDBtot);
void assign_radii(struct atom *PDB, long int PDBtot);
void save_areas(struct plane cont[], struct contactlist contlist[], int NC, int atomzero);
int get_contlist4(struct atom PDB[], int atomzero, struct contactlist contlist[],
int PDBtot, float rado, int dim);
void parse_commandline(int argc, char *argv[], char FILENAME[]);
void save_seeds(struct plane cont[], struct vertex poly[], int NV, int atomzero);
void get_firstvert(struct plane cont[], int *planeA, int *planeB, int *planeC, int NC, int atomzero);
// ========================================================================
int main(int argc, char *argv[])
{
int atomi; // atom number of atomzero
char FILENAME[500]; // input file name
long int PDBtot; // number of PDB atoms
parse_commandline( argc, argv, FILENAME);
PDB = read_PDB(FILENAME, PDB, &PDBtot);
// initialize contact atom index
ca_index = malloc(PDBtot*sizeof(long int));
ca_recsize = 5*PDBtot;
ca_rec = malloc(ca_recsize*sizeof(struct ca_struct));
seed = malloc(3*PDBtot*sizeof(long int));
if((!ca_rec) || (!ca_index) || (!seed)) {
printf("memory allocation error\n");
exit(1);
}
for(atomi=0; atomi<PDBtot; ++atomi) {
PDB[atomi].vol = 0.0;
ca_index[atomi] = -1; //initialize pointer array
seed[atomi*3] = -1; // initialize seed array
}
// assign protein atoms to boxes in cubic grid
dim = index_protein(PDBtot);
// calc volumes for all protein atoms
calc_region(PDB, PDBtot, PDBlist, PDBtot);
return(0);
}
/***************************
* subroutine calc_region
***************************/
// this subroutine calculates the contact areas (SAS) for a given set of atoms.
// Here, the set of atoms is the entire protein.
// needs global variable 'dim'.
int calc_region(struct atom PDB[], int PDBtot, int atomlist[], int numatoms)
{
int atomi; // atom counter
int atomzero; // current center atom
int cai; // contact atom counter for atomzero
int NC; // number of contacts around atomzero
int NV; // number of vertices in polyhedron around atomzero
double areao; // area of atom zero
double areatot; // total contact area
float rado; // radius of atomzero PLUS radius of water
float SAS; // solvent exposed surface in square angstroms
char surfatom; // atom type, 'I' internal, 'S' surface
float SAStot;
long int currindex;
int contnum;
float *coorA, *coorB; // coordinate pointers
for(atomi=0; atomi<numatoms; ++atomi) {
// ============= atom contact calculations =============
atomzero = atomlist[atomi];
rado = PDB[atomzero].radius + Rw;
NC = get_contlist4(PDB, atomzero, contlist, PDBtot, rado, dim);
NV = voronoi_poly2(PDB, atomzero, cont, rado, NC, contlist);
surfatom = order_faces(atomzero, poly, centerpt, rado, NC, NV, cont, ptorder);
calc_areas(poly, centerpt, rado, NC, NV, cont, ptorder, atomzero);
save_areas(cont, contlist, NC, atomzero);
// ============ SAS contact area calculations ============
areao = 4.0*PI*rado*rado;
areatot = 0.0;
for(cai=0; cai<NC; ++cai) {
if(cont[cai].flag != 'X') {
areatot += cont[cai].area;
}
}
SAS = areao - areatot;
if( SAS > 0.0) { // remove fractional negative values (~ -0.0000002)
PDB[atomzero].SAS = SAS;
SAStot += SAS;
} else {
PDB[atomzero].SAS = 0.0;
}
}
// ==================== write output file =======================
// -------------- print header ---------------
printf("# Atom-atom contact areas, calculated using program 'Vcontacts'\n");
printf("# version 1.2\n");
printf("# Authors: B.J.McConkey, V.Sobolev, and M.Edelman\n");
printf("# \n");
if(showbonded == 'Y') {
printf("# -all: contacts include covalently bonded atoms\n");
} else {
printf("# covalently bonded atom contacts are not listed\n");
}
if(planedef == 'X') {
printf("# -planedef X: extended radical plane\n");
} else if(planedef == 'R') {
printf("# -planedef R: radical plane\n"); // used as default
} else if(planedef == 'B') {
printf("# -planedef B: bisecting dividing plane\n");
}
if(normalize == 'Y') {
printf("# -norm: contacts normalized to a percent of total contact area\n");
} else {
printf("# Contacts are given in SAS equivalent units (square angstroms)\n");
}
printf("# \n");
printf("# -------ATOM------- ------CONTACT----- \n");
printf("# NUM NAME RESIDUE NUM NAME RESIDUE AREA%c DIST \n",
(normalize == 'Y')?'%':' ');
printf("#--------------------------------------------------------\n");
// print SAS and contact areas
for(atomi=0; atomi<PDBtot; ++atomi) {
currindex = ca_index[atomi];
rado = PDB[atomi].radius+Rw;
contnum = 1;
printf("%5d %5s %4d %5s %1c Sol_acc_surf %7.2f\n", PDB[atomi].atomnum,
PDB[atomi].atomname, PDB[atomi].resnum, PDB[atomi].res, PDB[atomi].chain,
(normalize=='Y')?(PDB[atomi].SAS*100.0/(rado*rado*4.0*PI)):PDB[atomi].SAS);
while(currindex != -1) {
// normalize if required
if(normalize == 'Y') {
ca_rec[currindex].area *= (100.0/(rado*rado*4.0*PI));
}
if(showbonded == 'Y') {
printf(" %5d %5s %4d %5s %1c %7.2f %4.2f\n",
PDB[ca_rec[currindex].atom].atomnum, PDB[ca_rec[currindex].atom].atomname,
PDB[ca_rec[currindex].atom].resnum, PDB[ca_rec[currindex].atom].res, PDB[atomi].chain,
ca_rec[currindex].area, ca_rec[currindex].dist);
++contnum;
} else {
// use distance check d<2.0 A to identify bonded contacts. always print S-S bonds.
coorA = PDB[atomi].coor;
coorB = PDB[ca_rec[currindex].atom].coor;
if((((coorA[0]-coorB[0])*(coorA[0]-coorB[0]) + (coorA[1]-coorB[1])*(coorA[1]-coorB[1])
+ (coorA[2]-coorB[2])*(coorA[2]-coorB[2])) > 4.0)
|| ((PDB[atomi].atomname[1] == 'S') && (PDB[ca_rec[currindex].atom].atomname[1] == 'S'))) {
printf(" %5d %5s %4d %5s %1c %7.2f %4.2f\n",
PDB[ca_rec[currindex].atom].atomnum, PDB[ca_rec[currindex].atom].atomname,
PDB[ca_rec[currindex].atom].resnum, PDB[ca_rec[currindex].atom].res, PDB[atomi].chain,
ca_rec[currindex].area, ca_rec[currindex].dist);
++contnum;
}
}
currindex = ca_rec[currindex].prev;
}
printf("\n");
}
return(0);
}
/******************************
* subroutine voronoi_poly2
* created 08/07/2001 BJM
******************************/
int voronoi_poly2(struct atom PDB[], int atomzero, struct plane cont[], float rado,
int NC, struct contactlist contlist[])
{
int cai; // contact atom counter
struct atom *ca_ptr; // pointer to pdb atom
double atomdist; // distance to atom
double planedist; // distance to plane
double mindist; // distance to closest plane
int planeA; // closest plane to origin
int planeB; // second plane, with planeA defines closest edge
int planeC; // new intersection plane for edge (endpt)
int oldplaneC; // old intersection plane for edge (startpt)
double vt; // vector parameter 't' for line x=x'+lt, y=y'+mt, z=z'+nt
double vtmin; // minimum value for vt
double vtdiv; // check for division by zero in vt calculation
double temppt[3];
struct vertex *stp; // pointer to start vertex, coordinates
double *V; // pointer to edge vector
int startedge;
int edgenum;
int vn = 0;
char edgeflag;
int edgei; // edge counter
int vi, vj; // vertices counters
double arcpt0[3], arcpt1[3];
int testpA, testpB;
double testvalA, testvalB;
char arcflag = 'N';
// failsafe variables:
char recalc; // flag if hull is being recalculated (orig. unbounded)
float origcoor[3]; // original pdb coordinates for atom.
recalc = 'N';
RESTART:
planeA = -1;
planeB = -1;
planeC = -1;
/* generate planes of contact with D = planedist */
mindist = 9.9e+9;
for(cai=0; cai<NC; ++cai) {
ca_ptr = &PDB[contlist[cai].index];
atomdist = contlist[cai].dist;
if(planedef == 'B') { // bisection - original Voronoi procedure
planedist = atomdist/2.0;
} else if(planedef == 'R') { // radical plane (Gellatly and Finney) - default.
planedist = (atomdist*atomdist + (rado-Rw)*(rado-Rw) - (ca_ptr->radius)*(ca_ptr->radius))/(2*atomdist);
} else { // extended radical plane (McConkey et al).
planedist = (atomdist*atomdist + rado*rado - (Rw + ca_ptr->radius)*(Rw + ca_ptr->radius))/(2*atomdist);
}
cont[cai].Ai[0] = (ca_ptr->coor[0] - PDB[atomzero].coor[0])/atomdist;
cont[cai].Ai[1] = (ca_ptr->coor[1] - PDB[atomzero].coor[1])/atomdist;
cont[cai].Ai[2] = (ca_ptr->coor[2] - PDB[atomzero].coor[2])/atomdist;
cont[cai].Ai[3] = -planedist;
cont[cai].dist = fabs(planedist);
cont[cai].index = contlist[cai].index;
cont[cai].flag = 'X'; // initialize contact flags to 'no contact'
// set plane0 as closest plane
if(cont[cai].dist < mindist) {
mindist = cont[cai].dist;
planeA = cai;
}
}
// add four planes surrounding atom, outer limit for voronoi polyhedron
cont[NC].Ai[0] = 0.707;
cont[NC].Ai[1] = 1.0;
cont[NC].Ai[2] = 0.0;
cont[NC].Ai[3] = -10.0;
cont[NC+1].Ai[0] = 0.707;
cont[NC+1].Ai[1] = -1.0;
cont[NC+1].Ai[2] = 0.0;
cont[NC+1].Ai[3] = -10.0;
cont[NC+2].Ai[0] = -0.707;
cont[NC+2].Ai[1] = 0.0;
cont[NC+2].Ai[2] = 1.0;
cont[NC+2].Ai[3] = -10.0;
cont[NC+3].Ai[0] = -0.707;
cont[NC+3].Ai[1] = 0.0;
cont[NC+3].Ai[2] = -1.0;
cont[NC+3].Ai[3] = -10.0;
// get starting vertex from seed or calc new vertex
get_firstvert(cont, &planeA, &planeB, &planeC, NC, atomzero);
solve_3x3(cont[planeA].Ai, cont[planeB].Ai, cont[planeC].Ai, temppt);
// add first vertex to vertex list
add_vertex(poly, 0, temppt, planeA, planeB, planeC);
// flag contacts as present
cont[planeA].flag = 'Y';
cont[planeB].flag = 'Y';
cont[planeC].flag = 'Y';
// calculate edge vectors
add_vedge(vedge, 0, cont, planeA, planeB, planeC, poly, 0);
add_vedge(vedge, 1, cont, planeB, planeC, planeA, poly, 0);
add_vedge(vedge, 2, cont, planeC, planeA, planeB, poly, 0);
startedge = 0;
edgenum = 3;
vn = 1;
/* --------------------------------------------------- */
/* Generate new polyhedron points from edge vectors */
/* --------------------------------------------------- */
while(1) {
// get next unfinished vector = startedge
while((vedge[startedge].endpt >= 0) && ((edgenum-startedge) > 0)) {
++startedge;
}
if((edgenum-startedge) <= 0) {
// all edges are done, polyhedron complete.
break;
}
vtmin = 9.9e+9; // dummy value
stp = &poly[vedge[startedge].startpt];
V = vedge[startedge].V;
planeA = vedge[startedge].plane[0];
planeB = vedge[startedge].plane[1];
oldplaneC = vedge[startedge].startplane;
planeC = -1;
// get closest positive intersection point
for(cai=0; cai<NC; ++cai) {
// check if contact is to be done - for now, do all.
if((cai != planeA) && (cai != planeB) && (cai != oldplaneC)) {
vtdiv = (cont[cai].Ai[0]*V[0] +cont[cai].Ai[1]*V[1] +cont[cai].Ai[2]*V[2]);
if(vtdiv != 0.0) {
vt = -(cont[cai].Ai[0]*stp->xi[0] +cont[cai].Ai[1]*stp->xi[1]
+cont[cai].Ai[2]*stp->xi[2] +cont[cai].Ai[3])/vtdiv;
if((vt < vtmin) && (vt > 0)) {
vtmin = vt;
planeC = cai;
}
}
}
}
poly[vn].xi[0] = stp->xi[0] + vtmin*V[0];
poly[vn].xi[1] = stp->xi[1] + vtmin*V[1];
poly[vn].xi[2] = stp->xi[2] + vtmin*V[2];
// if point is outside sphere, check vs. external planes
if((poly[vn].xi[0]*poly[vn].xi[0] + poly[vn].xi[1]*poly[vn].xi[1]
+ poly[vn].xi[2]*poly[vn].xi[2]) > rado*rado) {
for(cai=NC; cai<NC+4; ++cai) {
// check if contact is to be done - for now, do all.
if((cai != planeA) && (cai != planeB) && (cai != oldplaneC)) {
vtdiv = (cont[cai].Ai[0]*V[0] +cont[cai].Ai[1]*V[1] +cont[cai].Ai[2]*V[2]);
if(vtdiv != 0.0) {
vt = -(cont[cai].Ai[0]*stp->xi[0] +cont[cai].Ai[1]*stp->xi[1]
+cont[cai].Ai[2]*stp->xi[2] +cont[cai].Ai[3])/vtdiv;
if((vt < vtmin) && (vt > 0)) {
vtmin = vt;
planeC = cai;
}
}
}
}
poly[vn].xi[0] = stp->xi[0] + vtmin*V[0];
poly[vn].xi[1] = stp->xi[1] + vtmin*V[1];
poly[vn].xi[2] = stp->xi[2] + vtmin*V[2];
}
add_vertex(poly, vn, poly[vn].xi, planeA, planeB, planeC);
vedge[startedge].endpt = vn;
vedge[startedge].endplane = planeC;
//flag contact as present
cont[planeC].flag = 'Y';
// ======== ADD EDGES ========
// check edge (planeA, planeC)
edgeflag = 'Y';
edgei = startedge+1;
while(edgei < edgenum) {
if(((vedge[edgei].plane[0] == planeA)&&(vedge[edgei].plane[1] == planeC)) ||
((vedge[edgei].plane[0] == planeC)&&(vedge[edgei].plane[1] == planeA))) {
// already on list, add current vertex as endpt
vedge[edgei].endpt = vn;
vedge[edgei].endplane = planeB;
edgeflag = 'N';
break;
}
++edgei;
}
if(edgeflag == 'Y') { // add edge
add_vedge(vedge, edgenum, cont, planeA, planeC, planeB, poly, vn);
++edgenum;
}
// check edge (planeB, planeC)
edgeflag = 'Y';
edgei = startedge+1;
while(edgei < edgenum) {
if(((vedge[edgei].plane[0] == planeB)&&(vedge[edgei].plane[1] == planeC)) ||
((vedge[edgei].plane[0] == planeC)&&(vedge[edgei].plane[1] == planeB))) {
// already on list, add current vertex as endpt
vedge[edgei].endpt = vn;
vedge[edgei].endplane = planeA;
edgeflag = 'N';
break;
}
++edgei;
}
if(edgeflag == 'Y') { // add edge
add_vedge(vedge, edgenum, cont, planeB, planeC, planeA, poly, vn);
++edgenum;
// ===== failsafe - if solution is not converging, perturb atom =====
// ===== coordinates and recalculate. =====
if(edgenum >= 200) {
//printf("********* invalid solution for hull, recalculating *********\n");
seed[atomzero*3] = -1; // reset to no seed vertex
origcoor[0] = PDB[atomzero].coor[0];
origcoor[1] = PDB[atomzero].coor[1];
origcoor[2] = PDB[atomzero].coor[2];
// perturb atom coordinates
PDB[atomzero].coor[0] += 0.005*(float)(2*rand()-RAND_MAX)/(float)RAND_MAX;
PDB[atomzero].coor[1] += 0.005*(float)(2*rand()-RAND_MAX)/(float)RAND_MAX;
PDB[atomzero].coor[2] += 0.005*(float)(2*rand()-RAND_MAX)/(float)RAND_MAX;
recalc = 'Y';
goto RESTART;
}
}
++vn;
}
/*--------------------------------------------------*/
/* now have voronoi polyhedron around given atom. */
/* remove vertices outside of sphere, and */
/* calculate intersection points with sphere. */
/*--------------------------------------------------*/
// flag edges that may cross sphere boundary
for(edgei=0; edgei<edgenum; ++edgei) {
if((rado < poly[vedge[edgei].startpt].dist) || (rado < poly[vedge[edgei].endpt].dist)) {
// one or both vertices fall outside of sphere
arcflag = 'Y';
vedge[edgei].arc = '?';
} else {
vedge[edgei].arc = 'X';
}
}
// calculate new arc points
for(edgei=0; edgei<edgenum; ++edgei) {
if(vedge[edgei].arc != 'X') {
if(solve_2xS(cont[vedge[edgei].plane[0]], cont[vedge[edgei].plane[1]], rado, arcpt0, arcpt1) == -1) {
vedge[edgei].arc = 'X'; // mark edge as no associated arc point
continue;
}
// test new arc points vs. adjacent planes, add if ok.
testpA = vedge[edgei].startplane;
testpB = vedge[edgei].endplane;
testvalA = cont[testpA].Ai[0]*arcpt0[0] + cont[testpA].Ai[1]*arcpt0[1]
+ cont[testpA].Ai[2]*arcpt0[2] + cont[testpA].Ai[3];
testvalB = cont[testpB].Ai[0]*arcpt0[0] + cont[testpB].Ai[1]*arcpt0[1]
+ cont[testpB].Ai[2]*arcpt0[2] + cont[testpB].Ai[3];
if((testvalA < 0.0) && (testvalB < 0.0)) { // point is good
add_vertex(poly, vn, arcpt0, vedge[edgei].plane[0], vedge[edgei].plane[1], -1);
poly[vn].dist = rado;
++vn;
}
testvalA = cont[testpA].Ai[0]*arcpt1[0] + cont[testpA].Ai[1]*arcpt1[1]
+ cont[testpA].Ai[2]*arcpt1[2] + cont[testpA].Ai[3];
testvalB = cont[testpB].Ai[0]*arcpt1[0] + cont[testpB].Ai[1]*arcpt1[1]
+ cont[testpB].Ai[2]*arcpt1[2] + cont[testpB].Ai[3];
if((testvalA < 0.0) && (testvalB < 0.0)) { // point is good
add_vertex(poly, vn, arcpt1, vedge[edgei].plane[0], vedge[edgei].plane[1], -1);
poly[vn].dist = rado;
++vn;
}
}
}
// reduce poly vertex list
vj=0;
for(vi=0; vi<vn; ++vi) {
if(poly[vi].dist <= rado) {
poly[vj] = poly[vi];
++vj;
}
}
vn = vj;
// ----- calculate center points and mark engulfing contacts -----
for(cai=0; cai<NC; ++cai) {
centerpt[cai].xi[0] = -cont[cai].Ai[0]*cont[cai].Ai[3];
centerpt[cai].xi[1] = -cont[cai].Ai[1]*cont[cai].Ai[3];
centerpt[cai].xi[2] = -cont[cai].Ai[2]*cont[cai].Ai[3];
centerpt[cai].dist = fabs(cont[cai].Ai[3]);
if(cont[cai].Ai[3] > 0.0) {
cont[cai].flag = 'E';
}
}
if(recalc == 'N') {
save_seeds(cont, poly, vn, atomzero);
} else {
// reset atom coordinates to original values
PDB[atomzero].coor[0] = origcoor[0];
PDB[atomzero].coor[1] = origcoor[1];
PDB[atomzero].coor[2] = origcoor[2];
}
return(vn);
}
/****************************
* subroutine get_firstvert
****************************/
void get_firstvert(struct plane cont[], int *planeA, int *planeB, int *planeC, int NC, int atomzero)
{
long int seedi;
int cai;
double mindist;
double ptA[3];
double vectA[4]; // 4 values so it can be used as a plane equation as well
double vt; // vector parameter 't' for line x=x'+lt, y=y'+mt, z=z'+nt
double vtmin; // minimum value for vt
double vtdiv; // check for division by zero in vt calculation
double temppt[3];
double ptdist;
// get previous seed vertex if present
seedi = atomzero*3;
*planeA = -1;
*planeB = -1;
*planeC = -1;
if(seed[seedi] != -1) {
for(cai=0; cai<NC; ++cai) {
if(cont[cai].index == seed[seedi]) {
*planeA = cai;
} else if(cont[cai].index == seed[seedi+1]) {
*planeB = cai;
} else if(cont[cai].index == seed[seedi+2]) {
*planeC = cai;
}
}
}
if((*planeA != -1)&&(*planeB != -1)&&(*planeC != -1)) {
return;
} else {
// ------------- find initial edge, on plane closest to origin -------------
mindist = 9.9e+9; // dummy value
for(cai=0; cai<NC; ++cai) {
if(cont[cai].dist < mindist) {
mindist = cont[cai].dist;
*planeA = cai;
}
}
mindist = 9.9e+9; // dummy value
for(cai=0; cai<NC+4; ++cai) {
if(cai != *planeA) {
vectA[0] = cont[*planeA].Ai[1]*cont[cai].Ai[2] - cont[*planeA].Ai[2]*cont[cai].Ai[1];
vectA[1] = cont[*planeA].Ai[2]*cont[cai].Ai[0] - cont[*planeA].Ai[0]*cont[cai].Ai[2];
vectA[2] = cont[*planeA].Ai[0]*cont[cai].Ai[1] - cont[*planeA].Ai[1]*cont[cai].Ai[0];
vectA[3] = 0.0;
if(solve_3x3(cont[*planeA].Ai, cont[cai].Ai, vectA, temppt) != -1) {
ptdist = sqrt(temppt[0]*temppt[0] + temppt[1]*temppt[1] + temppt[2]*temppt[2]);
if(ptdist < mindist) {
*planeB = cai;
mindist = ptdist;
ptA[0] = temppt[0];
ptA[1] = temppt[1];
ptA[2] = temppt[2];
}
}
}
}
// recalc vector normal to planes A and B
vectA[0] = cont[*planeA].Ai[1]*cont[*planeB].Ai[2] - cont[*planeA].Ai[2]*cont[*planeB].Ai[1];
vectA[1] = cont[*planeA].Ai[2]*cont[*planeB].Ai[0] - cont[*planeA].Ai[0]*cont[*planeB].Ai[2];
vectA[2] = cont[*planeA].Ai[0]*cont[*planeB].Ai[1] - cont[*planeA].Ai[1]*cont[*planeB].Ai[0];
vectA[3] = 0.0;
// get starting vertex on polyhedron
vtmin = 9.9e+9; // dummy value
for(cai=0; cai<NC+4; ++cai) {
if((cai != *planeA) && (cai != *planeB)) {
vtdiv = (cont[cai].Ai[0]*vectA[0] +cont[cai].Ai[1]*vectA[1] +cont[cai].Ai[2]*vectA[2]);
if(vtdiv != 0.0) {
vt = -(cont[cai].Ai[0]*ptA[0] +cont[cai].Ai[1]*ptA[1] +cont[cai].Ai[2]*ptA[2] +cont[cai].Ai[3])/vtdiv;
if(fabs(vt) < vtmin) {
vtmin = fabs(vt);
*planeC = cai;
}
}
}
}
}
return;
}
/*************************
* subroutine save_seeds
*************************/
// saves starting vertices for atoms to be done
void save_seeds(struct plane cont[], struct vertex poly[], int NV, int atomzero)
{
int vi;
int seedi;
for(vi=0; vi<NV; ++vi) {
if(poly[vi].plane[2] != -1) {
seedi = 3*cont[poly[vi].plane[0]].index;
if(seed[seedi] == -1) {
seed[seedi] = atomzero;
seed[seedi+1] = cont[poly[vi].plane[1]].index;
seed[seedi+2] = cont[poly[vi].plane[2]].index;
}
seedi = 3*cont[poly[vi].plane[1]].index;
if(seed[seedi] == -1) {
seed[seedi] = atomzero;
seed[seedi+1] = cont[poly[vi].plane[0]].index;
seed[seedi+2] = cont[poly[vi].plane[2]].index;
}
seedi = 3*cont[poly[vi].plane[2]].index;
if(seed[seedi] == -1) {
seed[seedi] = atomzero;
seed[seedi+1] = cont[poly[vi].plane[0]].index;
seed[seedi+2] = cont[poly[vi].plane[1]].index;
}
}
}
return;
}
/*******************************
* subroutine add_vedge
*******************************/
// adds a new edge to the edge list. Direction of vector is tested so that
// it points away from the startpt, along the body of the polyhedron.
// stores information in variable vedge[edgenum].
void add_vedge(struct edgevector vedge[], int edgenum, struct plane cont[], int plane0, int plane1,
int testplane, struct vertex poly[], int startpt)
{
double testpt[3];
double testval;
vedge[edgenum].V[0] = cont[plane0].Ai[1]*cont[plane1].Ai[2] - cont[plane0].Ai[2]*cont[plane1].Ai[1];
vedge[edgenum].V[1] = cont[plane0].Ai[2]*cont[plane1].Ai[0] - cont[plane0].Ai[0]*cont[plane1].Ai[2];
vedge[edgenum].V[2] = cont[plane0].Ai[0]*cont[plane1].Ai[1] - cont[plane0].Ai[1]*cont[plane1].Ai[0];
vedge[edgenum].startpt = startpt;
vedge[edgenum].endpt = -1; // flag, edge not completed.
vedge[edgenum].plane[0] = plane0;
vedge[edgenum].plane[1] = plane1;
vedge[edgenum].startplane = testplane;
vedge[edgenum].arc = '.'; // dummy value.
// test direction of vector
testpt[0] = poly[startpt].xi[0] + vedge[edgenum].V[0];
testpt[1] = poly[startpt].xi[1] + vedge[edgenum].V[1];
testpt[2] = poly[startpt].xi[2] + vedge[edgenum].V[2];
testval = cont[testplane].Ai[0]*testpt[0] +cont[testplane].Ai[1]*testpt[1]
+ cont[testplane].Ai[2]*testpt[2] +cont[testplane].Ai[3];
if(testval > 0.0) { // vector is in wrong direction
vedge[edgenum].V[0] = -vedge[edgenum].V[0];
vedge[edgenum].V[1] = -vedge[edgenum].V[1];
vedge[edgenum].V[2] = -vedge[edgenum].V[2];
}
return;
}
/*******************************
* subroutine add_vertex
*******************************/
// add polyhedron vertex to local list for current atom contacts
int add_vertex(struct vertex poly[], int vn, double coor[], int planeA, int planeB, int planeC)
{
poly[vn].xi[0] = coor[0];
poly[vn].xi[1] = coor[1];
poly[vn].xi[2] = coor[2];
poly[vn].plane[0] = planeA;
poly[vn].plane[1] = planeB;
poly[vn].plane[2] = planeC;
poly[vn].dist = sqrt(coor[0]*coor[0] +coor[1]*coor[1] +coor[2]*coor[2]);
return(0);
}
/*********************************
* function order_faces
*********************************/
// output is array ptorder, the order of points around each face.
// return values are 'I' for internal atom, 'S' for surface atom.
char order_faces(int atomzero,struct vertex poly[], struct vertex centerpt[], float rado,
int NC, int NV, struct plane cont[], struct ptindex ptorder[])
{
int planeX; // current plane, 1 to NC
int planeY; // second plane, to get adjacent points on polygon
int surfcount; // number of points defining a given plane of contact
int vi, vi2; // vertices counter
int tempsi; // temporary storage for exchanging indices
double tempcos; // for exchanging cosines
double cos10X[50]; // cos of angle between points poly[1],poly[0],and poly[X]
double temppt[3]; // temp coordinates of new arc point
char surfatom; // return value: 'I' internal atom, 'S' surface atom
surfatom = 'I'; // internal atom
for(vi=0; vi<NV; ++vi) {
if(poly[vi].plane[2] == -1) {
surfatom = 'S'; // surface atom
break;
}
}
// for surface calculation only
// if(surfatom == 'I') return('I');
for(planeX=0; planeX < NC; ++planeX) {
if(cont[planeX].flag == 'X') { // hidden
ptorder[planeX].numpts = 0;
continue;
}
surfcount=0;
for(vi=0; vi<NV; ++vi) {
// index all points comprising surface for planeX
if((poly[vi].plane[0]==planeX) || (poly[vi].plane[1]==planeX) || (poly[vi].plane[2]==planeX)) {
ptorder[planeX].pt[surfcount] = vi;
++surfcount;
}
}
if(surfcount > 2) {
/*-------------------------------------------------------------------*/
/* three or more surface points, need additional point so no arcs */
/* are greater than 180 deg. Make point opposite first pt on an arc. */
/* (if no points on arc, extra point isn't needed.) */
/*-------------------------------------------------------------------*/
for(vi=0; vi<surfcount; ++vi) {
if(poly[ptorder[planeX].pt[vi]].plane[2] == -1) { // on arc, calc pt. opposite
// get coordinates of point
temppt[0] = 2.0*centerpt[planeX].xi[0] - poly[ptorder[planeX].pt[vi]].xi[0];
temppt[1] = 2.0*centerpt[planeX].xi[1] - poly[ptorder[planeX].pt[vi]].xi[1];
temppt[2] = 2.0*centerpt[planeX].xi[2] - poly[ptorder[planeX].pt[vi]].xi[2];
//keep point if it's valid
if(test_point(temppt, cont, NC, rado, planeX, -1, -1) == 'Y') {
poly[NV].xi[0] = temppt[0];
poly[NV].xi[1] = temppt[1];
poly[NV].xi[2] = temppt[2];
poly[NV].plane[0] = planeX;
poly[NV].plane[1] = -1;
poly[NV].plane[2] = -1;
poly[NV].dist = rado;
ptorder[planeX].pt[surfcount] = NV;
++surfcount;
++NV;
}
break;
}
}
}
ptorder[planeX].numpts = surfcount;
if(surfcount > 3) {
// get two points on same line (two common planes).
// all points already share one plane (planeX), find another.
if(poly[ptorder[planeX].pt[0]].plane[0] == planeX) {
planeY = poly[ptorder[planeX].pt[0]].plane[1];
} else {
planeY = poly[ptorder[planeX].pt[0]].plane[0];
}
//find another point on the same line (2 common planes)
for(vi=1; vi<surfcount; ++vi) {
if((poly[ptorder[planeX].pt[vi]].plane[0]==planeY) || (poly[ptorder[planeX].pt[vi]].plane[1]==planeY)
|| (poly[ptorder[planeX].pt[vi]].plane[2]==planeY)) {
break;
}
}
//swap index for pt[1] and pt[vi], so points 0 and 1 are on same line
tempsi = ptorder[planeX].pt[vi];
ptorder[planeX].pt[vi] = ptorder[planeX].pt[1];
ptorder[planeX].pt[1] = tempsi;
// calculate cosine between points indexed 1,0,X
for(vi=2; vi<surfcount; ++vi) {
cos10X[vi] = cosPQR(poly[ptorder[planeX].pt[1]].xi, poly[ptorder[planeX].pt[0]].xi,
poly[ptorder[planeX].pt[vi]].xi);
}
// order by cosines, decreasing order
for(vi=2; vi<surfcount-1; ++vi) {
for(vi2=vi+1; vi2<surfcount; ++vi2) {
if(cos10X[vi] < cos10X[vi2]) {
// swap indices if points in wrong order
tempsi = ptorder[planeX].pt[vi];
ptorder[planeX].pt[vi] = ptorder[planeX].pt[vi2];
ptorder[planeX].pt[vi2] = tempsi;
tempcos = cos10X[vi];
cos10X[vi] = cos10X[vi2];
cos10X[vi2] = tempcos;
}
}
}
}
}
return(surfatom);
}
/**************************
* subroutine calc_areas
**************************/
void calc_areas(struct vertex poly[], struct vertex centerpt[], float rado, int NC, int NV,
struct plane cont[], struct ptindex ptorder[], int atomzero)
{
char engflag; // ='Y' if an engulfing plane is present
int planeX; // current plane, 1 to NC
int NP; // number of points on face
int vi; // vertices counter
double area; // area of polygon
int pa1, pa2; // possible common planes
int commplane; // plane shared by adjacent points (not planeX)
int epi; // engulfing plane counter
int engplane[4]; // index to engulfed planes
double maxSAS; // maximum solvent exposed surface, no atoms other than engulfing.
struct vertex ptB, ptC; // arc point intersections
int currpt, nextpt;
double cosNN1[40]; // angle between vertex N and vertex N+1
double cosNzero[40]; // angle between vertex N and vertex N+1
double tanprod; // product of tangents
int v0, va, vb; // vertices for arc calculation
double U,V,W,X;
double tansqrS, tansqrSA, tansqrSB, tansqrSC;
engflag = 'N';
epi = 0;
//RESET AREAS TO ZERO
for(planeX=0; planeX < NC; ++planeX) {
cont[planeX].area = 0.0;
if(cont[planeX].flag == 'E') {
engflag = 'Y';
engplane[epi] = planeX;
++epi;
}
}
if(engflag == 'Y') { // engulfing plane correction - project points onto sphere surface.
project_points(poly, centerpt, rado, NC, NV, cont);
}
/* ---------------------------- */
/* calculate area for each face */
/* ---------------------------- */
for(planeX=0; planeX < NC; ++planeX) {
NP = ptorder[planeX].numpts;
area = 0.0;
if(cont[planeX].flag == 'X') {
continue;
}
// if there are no points on a valid contact, area is spherical cap
if(NP == 0) {
if(test_point(centerpt[planeX].xi, cont, NC, rado, planeX, -1, -1) == 'Y') {
cont[planeX].area = 2.0*PI*rado*(rado-centerpt[planeX].dist);
}
} else if(NP == 2) { // only two contact points, check which part of arc
if(test_point(centerpt[planeX].xi, cont, NC, rado, planeX, -1, -1) == 'Y') { // area is (cap - arc)
cont[planeX].area = 2.0*PI*rado*(rado-centerpt[planeX].dist)
- spherical_arc(centerpt[planeX], poly[ptorder[planeX].pt[0]],
poly[ptorder[planeX].pt[1]], rado);
} else { // area is arc.
cont[planeX].area = spherical_arc(centerpt[planeX], poly[ptorder[planeX].pt[0]],
poly[ptorder[planeX].pt[1]], rado);
}
} else {
// ------ calculate cosines and angles ------
for(vi=0; vi<NP; ++vi) {
v0 = ptorder[planeX].pt[0];
va = ptorder[planeX].pt[vi];
vb = ptorder[planeX].pt[(vi+1)%NP];