-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.c
executable file
·2979 lines (2573 loc) · 99.7 KB
/
analysis.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
/*<<<
* ------------------------------------------------------------------------
* This file is part of the SCHNAarP software package for the analysis and
* rebuilding of double helical nucleic acid structures. Detailed description
* of the underlying algorithms can be found in the following three papers:
*
* (1) Xiang-Jun Lu, M. A. El Hassan and C. A. Hunter (1997), "Structure
* and Conformation of Helical Nucleic Acids: Analysis Program
* (SCHNAaP)", J. Mol. Biol. 273, 668--680.
*
* (2) Xiang-Jun Lu, M. A. El Hassan and C. A. Hunter (1997), "Structure
* and Conformation of Helical Nucleic Acids: Rebuilding Program
* (SCHNArP)", J. Mol. Biol. 273, 681--691.
*
* (3) M. A. El Hassan and C. R. Calladine (1995), "The Assessment of the
* Geometry of Dinucleotide Steps in Double-Helical DNA; a New Local
* Calculation Scheme", J. Mol. Biol. 251, 648--664.
*
* Copyright (C) 1996-2002 Xiang-Jun Lu. Permission to use, copy, and modify
* this software is hereby granted to all academic and not-for-profit
* institutions without fee, provided that the above papers (or part of) are
* properly cited in related publications. However, the right to use this
* software in conjunction with for profit activities, and the right to
* distribute the software or modified or extended versions thereof for profit
* are *NOT* granted except by prior arrangement and written consent of the
* copyright holders.
*
* The software is provided "AS-IS" and without warranty of any kind, express,
* implied or otherwise, including without limitation, any warranty of
* merchantability or fitness for a particular purpose.
*
* The software was written by Xiang-Jun Lu ([email protected]). It started
* off his PhD thesis work at Dr. Chris Hunter Laboratory at University of
* Sheffield, U.K. The source code should be valuable for understanding the
* elegant CEHS algorithm, and many other aspects of nucleic acid structures.
* For real world applications, however, you are strongly recommended to
* switch to 3DNA (http://3dna.rutgers.edu).
*
* Revision history -- please see file 'Change.log'
*
* $LastChangedDate: 2009-06-01 18:34:25 -0400 (Mon, 01 Jun 2009) $
* $LastChangedRevision: 1556 $
* $LastChangedBy: xiangjun $
* ------------------------------------------------------------------------
>>>*/
#include "schna_ar_p.h"
void get_data(long *RY_idx, char **atom_id, char **bp_seq, double **xyz,
char *base_type, char *strand, long *base_num,
long *num_atom, long *num_bp, char *inpfil, FILE * fo)
{
FILE *fi;
char str[BUF512], asym[4], btype;
long bnum, n = 0, ns = 0, ne = 0, N_purine, i, j, k;
double x, y, z;
long *bidx, *bsidx, *beidx, *N9_idx, *tmp1, *tmp2;
long numA, numB, ncircle, inum, icol, ist, max_base = 21, tnum = 100;
char *tmp_str;
/* Print out the date & time of running this program */
time_t run_time;
fi = open_file(inpfil, "r");
while (fgets(str, sizeof(str), fi) != NULL) {
upper(str, strlen(str));
sscanf(str, "%s %c %ld %lf %lf %lf", asym, &btype, &bnum, &x, &y, &z);
if (asym[0] != 'H') {
n++;
i = strlen(asym);
if (i < 1 || i > 3)
nrerror("Wrong atomic idenfication!");
if (i == 1)
strcat(asym, " \0");
if (i == 2)
strcat(asym, " \0");
if (asym[2] == '*')
asym[2] = '\'';
if (strcmp(asym, "O1'") == 0)
strcpy(asym, "O4'");
if (strcmp(asym, "OL ") == 0)
strcpy(asym, "O1P");
if (strcmp(asym, "OR ") == 0)
strcpy(asym, "O2P");
strcpy(atom_id[n], asym);
base_type[n] = btype;
base_num[n] = bnum;
xyz[n][1] = x;
xyz[n][2] = y;
xyz[n][3] = z;
}
}
fclose(fi);
if (!n)
nrerror("Empty input file!");
bidx = lvector(1, n);
bsidx = lvector(1, n);
beidx = lvector(1, n);
N9_idx = lvector(1, n);
tmp1 = lvector(1, n - 1);
tmp2 = lvector(1, n + 1);
for (i = 1; i <= n; i++)
bidx[i] = base_num[i] + 100 * base_type[i]; /* char as integer & use bidx temp */
i_diff(tmp1, bidx, n);
logical_not(tmp1, tmp1, n - 1);
tmp2[1] = 0;
tmp2[n + 1] = 0;
for (i = 1; i <= n - 1; i++)
tmp2[i + 1] = tmp1[i];
i_diff(bidx, tmp2, n + 1);
for (i = 1; i <= n; i++) {
if (bidx[i] == 1) {
ns++;
bsidx[ns] = i;
}
if (bidx[i] == -1) {
ne++;
beidx[ne] = i;
}
}
if (ns % 2)
nrerror("The two strands are not of equal length!");
str_idx(&N_purine, N9_idx, atom_id, "N9 ", n);
zero_lvector(RY_idx, n);
for (i = 1; i <= N_purine; i++) {
for (j = 1; j <= ns; j++) {
if ((N9_idx[i] >= bsidx[j]) && (N9_idx[i] <= beidx[j]))
break;
}
for (k = bsidx[j]; k <= beidx[j]; k++)
RY_idx[k] = 1;
}
*num_bp = ns / 2;
*num_atom = n;
k = *num_bp; /* K used as num_bp for short */
numA = bsidx[k + 1] - 1;
numB = n - numA;
if (n > tnum)
tnum = n;
tmp_str = cvector(0, tnum);
nrptx(tmp_str, 'A', numA + 1);
strcpy(strand, tmp_str);
nrptx(tmp_str, 'B', numB + 1);
strcat(strand, tmp_str);
for (i = 1; i <= k; i++) {
bp_seq[i][1] = base_type[bsidx[i]];
bp_seq[i][2] = base_type[bsidx[2 * k - i + 1]];
}
/* Write a short note about the program and the structure */
prt_sep(fo, '*', 76);
nrptx(tmp_str, ' ', 14);
fprintf(fo, "%sMain output file from SCHNAaP (Version 1.1, Aug. 1998)\n\n", tmp_str);
fprintf(fo, " An analysis program for double-helical");
fprintf(fo, " nucleic acid structures\n");
nrptx(tmp_str, ' ', 37);
fprintf(fo, "\n%sby\n\n", tmp_str);
nrptx(tmp_str, ' ', 14);
fprintf(fo, "%sXiang-Jun Lu & C. A. Hunter", tmp_str);
fprintf(fo, " (University of Sheffield)\n\n");
nrptx(tmp_str, ' ', 20);
fprintf(fo, "%sM. A. El Hassan (University of Cambridge)\n\n", tmp_str);
prt_sep(fo, '*', 76);
fprintf(fo, "1. The list of the parameters given below are along the 5' to 3' \n");
fprintf(fo, " direction of strand I and 3' to 5' direction of strand II\n\n");
fprintf(fo, "2. All torsion angles are given in the range of [-180, +180]\n\n");
prt_sep(fo, '*', 76);
run_time = time(NULL);
fprintf(fo, "Name of input data file: %s\n", inpfil);
fprintf(fo, "Date and time of this analysis: ");
fprintf(fo, "%s\n", ctime(&run_time));
fprintf(fo, "\nNo. of heavy atoms: %4ld\n", n);
fprintf(fo, "No. of base-pairs: %4ld\n", k);
fprintf(fo, "\nBase pair sequence: \n");
/* max_base (21) bases per line maximum */
ncircle = (long) ceil(k / (double) max_base);
for (inum = 1; inum <= ncircle; inum++) {
if (inum == ncircle) {
icol = k % max_base; /* Remainder */
if (!icol)
icol = max_base;
} else
icol = max_base;
ist = (inum - 1) * max_base; /* Starting column */
fprintf(fo, " ");
for (i = ist + 1; i <= ist + icol; i++)
fprintf(fo, "%3ld", i);
fprintf(fo, "\n");
fprintf(fo, "Strand I: ");
for (i = ist + 1; i <= ist + icol; i++)
fprintf(fo, "%3c", bp_seq[i][1]);
fprintf(fo, "\n");
fprintf(fo, " ");
for (i = ist + 1; i <= ist + icol; i++)
fprintf(fo, "%3c", '|');
fprintf(fo, "\n");
fprintf(fo, "Strand II: ");
for (i = ist + 1; i <= ist + icol; i++)
fprintf(fo, "%3c", bp_seq[i][2]);
fprintf(fo, "\n\n");
}
free_lvector(bidx, 1, n);
free_lvector(bsidx, 1, n);
free_lvector(beidx, 1, n);
free_lvector(N9_idx, 1, n);
free_lvector(tmp1, 1, n - 1);
free_lvector(tmp2, 1, n + 1);
free_cvector(tmp_str, 0, tnum);
}
void get_list(long *C1, long *N, long *P, long *O4, long *C68, long *chi,
long *mc, long *nmc, long *sugar, long *base, long *n_base,
long *RY_idx, char **atom_id, long num_bp, long num_atom, double **xyz)
{
long i, j, idx, lbn, nR = 0, nY = 0;
long *Ridx, *Yidx;
char **Ratom, **Yatom;
long nRC1, *RC1_idx, *RC1; /* For RC1' atoms numbering */
long nYC1, *YC1_idx, *YC1; /* For YC1' atoms numbering */
long nRN9, *RN9_idx, *RN9; /* For RN9 atoms numbering */
long nYN1, *YN1_idx, *YN1; /* For YN1 atoms numbering */
long nRC8, *RC8_idx, *RC8; /* For RC8 atoms numbering */
long nYC6, *YC6_idx, *YC6; /* For YC6 atoms numbering */
long nC1, nN, nC68;
/* Main chain atoms */
long nO5, *O5, nC5, *C5, nC4, *C4, nC3, *C3, nO3, *O3, nP;
/* chi torsion angle */
long nRC4, *RC4_idx, *RC4; /* For RC4 atoms numbering */
long nYC2, *YC2_idx, *YC2; /* For YC2 atoms numbering */
long *C24; /* For RC4/YC2 atoms numbering */
/* sugar torsion angle */
long nO4, nC2, *C2;
/* To check for special O3' linkage case */
double *dd, aveC3_O3, *tmp_vec;
char yn, str[BUF512];
long iA, iB;
lbn = 2 * num_bp; /* The sequential no# of last base */
*n_base = 0;
/* Get R and Y atom symbols */
Ridx = lvector(1, num_atom);
Yidx = lvector(1, num_atom);
Ratom = cmatrix(1, num_atom, 1, 4);
Yatom = cmatrix(1, num_atom, 1, 4);
for (i = 1; i <= num_atom; i++) {
if (RY_idx[i] == 1) {
nR++;
Ridx[nR] = i;
strcpy(Ratom[nR], atom_id[i]);
} else {
nY++;
Yidx[nY] = i;
strcpy(Yatom[nY], atom_id[i]);
}
}
/* Get the atom seq# list for RC1/YC1, RN9/YN1 and RC8/YC6 */
/* RC1' atoms */
RC1_idx = lvector(1, nR);
str_idx(&nRC1, RC1_idx, Ratom, "C1'", nR);
RC1 = lvector(1, nRC1);
for (i = 1; i <= nRC1; i++)
RC1[i] = Ridx[RC1_idx[i]];
/* YC1' atoms */
YC1_idx = lvector(1, nY);
str_idx(&nYC1, YC1_idx, Yatom, "C1'", nY);
YC1 = lvector(1, nYC1);
for (i = 1; i <= nYC1; i++)
YC1[i] = Yidx[YC1_idx[i]];
/* Sort all C1' atoms together */
nC1 = nRC1 + nYC1;
if (nC1 != lbn)
nrerror("C1' atom #s does not match #s of base-pairs!");
for (i = 1; i <= nRC1; i++)
C1[i] = RC1[i];
j = nRC1;
for (i = 1; i <= nYC1; i++)
C1[j + i] = YC1[i];
isort(nC1, C1);
/* ---------------------------------------- */
/* RN9 atoms */
RN9_idx = lvector(1, nR);
str_idx(&nRN9, RN9_idx, Ratom, "N9 ", nR);
RN9 = lvector(1, nRN9);
for (i = 1; i <= nRN9; i++)
RN9[i] = Ridx[RN9_idx[i]];
/* YN1 atoms */
YN1_idx = lvector(1, nY);
str_idx(&nYN1, YN1_idx, Yatom, "N1 ", nY);
YN1 = lvector(1, nYN1);
for (i = 1; i <= nYN1; i++)
YN1[i] = Yidx[YN1_idx[i]];
/* Sort all RN9-YN1 atoms together */
nN = nRN9 + nYN1;
if (nN != lbn)
nrerror("RN9/YN1 atom #s does not match #s of base-pairs!");
for (i = 1; i <= nRN9; i++)
N[i] = RN9[i];
j = nRN9;
for (i = 1; i <= nYN1; i++)
N[j + i] = YN1[i];
isort(nN, N);
/* ---------------------------------------- */
/* RC8 atoms */
RC8_idx = lvector(1, nR);
str_idx(&nRC8, RC8_idx, Ratom, "C8 ", nR);
RC8 = lvector(1, nRC8);
for (i = 1; i <= nRC8; i++)
RC8[i] = Ridx[RC8_idx[i]];
/* YC6 atoms */
YC6_idx = lvector(1, nY);
str_idx(&nYC6, YC6_idx, Yatom, "C6 ", nY);
YC6 = lvector(1, nYC6);
for (i = 1; i <= nYC6; i++)
YC6[i] = Yidx[YC6_idx[i]];
/* Sort all RC8-YC1 atoms together */
nC68 = nRC8 + nYC6;
if (nC68 != lbn)
nrerror("RC8/YC6 atom #s does not match #s of base-pairs!");
for (i = 1; i <= nRC8; i++)
C68[i] = RC8[i];
j = nRC8;
for (i = 1; i <= nYC6; i++)
C68[j + i] = YC6[i];
isort(nC68, C68);
/* =============== Main chain: O5'-C5'-C4'-C3'-O3'-P =============== */
O5 = lvector(1, lbn);
C5 = lvector(1, lbn);
C4 = lvector(1, lbn);
C3 = lvector(1, lbn);
O3 = lvector(1, lbn);
str_idx(&nO5, O5, atom_id, "O5'", num_atom);
str_idx(&nC5, C5, atom_id, "C5'", num_atom);
str_idx(&nC4, C4, atom_id, "C4'", num_atom);
str_idx(&nC3, C3, atom_id, "C3'", num_atom);
str_idx(&nO3, O3, atom_id, "O3'", num_atom);
str_idx(&nP, P, atom_id, "P ", num_atom);
if (nO5 != lbn)
nrerror("O5' atom #s does not match #s of base-pairs!");
if (nC5 != lbn)
nrerror("C5' atom #s does not match #s of base-pairs!");
if (nC4 != lbn)
nrerror("C4' atom #s does not match #s of base-pairs!");
if (nC3 != lbn)
nrerror("C3' atom #s does not match #s of base-pairs!");
if (nO3 != lbn)
nrerror("O3' atom #s does not match #s of base-pairs!");
/*% In some cases, O3(i) is linked to P(i) instead of P(i+1) (Convention)
% Here I use the distance between O3'(i)--C3'(i) as a measure to
% distinguish between the two, and give a warning message. */
dd = dvector(1, lbn);
tmp_vec = dvector(1, 3);
for (i = 1; i <= lbn; i++) {
for (j = 1; j <= 3; j++)
tmp_vec[j] = xyz[O3[i]][j] - xyz[C3[i]][j];
dd[i] = sqrt(dot_dvector(tmp_vec, tmp_vec, 3));
}
aveC3_O3 = mean_dvector(dd, lbn);
if (aveC3_O3 > 2.8) {
printf("\nThe the mean C3'(i)--O3'(i) length is (%4.1f) > 2.8 A\n", aveC3_O3);
printf("It seems this structure has the linkage O3(i)--P(i)\n");
printf("instead of the conventional (IUPAC-IUB) O3(i)--P(i+1)\n\n");
printf("Is this the case [y(Dft)/n]: ");
read_stdin_str(str);
yn = toupper(str[0]);
if (yn != 'N') {
iA = O3[1];
iB = O3[num_bp + 1]; /* Move the 1st to the last */
for (i = 1; i < num_bp; i++)
O3[i] = O3[i + 1];
O3[num_bp] = iA;
for (i = num_bp + 2; i < lbn; i++)
O3[i] = O3[i + 1];
O3[lbn] = iB;
printf("\nI have re-assigned O3'' atoms according to IUPAC-IUB\n");
printf("NB: The backbone torsion angles--alpha & delta--which are\n");
printf(" associated with O3'' atom, for the 3'' terminal residue\n");
printf(" along each strand are MEANINGLESS in the output file.\n");
}
}
/* End of special handling of O3' issue */
*nmc = nO5 + nC5 + nC4 + nC3 + nO3 + nP; /* 5*lbn+nP */
if (nP == lbn) { /* With terminal P */
for (i = 1; i <= lbn; i++) {
j = (i - 1) * 6;
mc[j + 1] = P[i];
mc[j + 2] = O5[i];
mc[j + 3] = C5[i];
mc[j + 4] = C4[i];
mc[j + 5] = C3[i];
mc[j + 6] = O3[i];
}
} else if (nP == lbn - 2) {
if (P[1] < O5[1]) /* Missing P in the middle */
nrerror("There are missing P atoms in the middle of the strucutre!");
/* For strand I */
for (i = 1; i <= num_bp; i++) {
j = (i - 1) * 6;
mc[j + 1] = O5[i];
mc[j + 2] = C5[i];
mc[j + 3] = C4[i];
mc[j + 4] = C3[i];
mc[j + 5] = O3[i];
if (i != num_bp)
mc[j + 6] = P[i];
}
idx = j + 5;
/* For strand II */
for (i = num_bp + 1; i <= lbn; i++) {
j = idx + (i - num_bp - 1) * 6;
mc[j + 1] = O5[i];
mc[j + 2] = C5[i];
mc[j + 3] = C4[i];
mc[j + 4] = C3[i];
mc[j + 5] = O3[i];
if (i != lbn)
mc[j + 6] = P[i - 1];
}
} else
nrerror("P atom #s does not match #s of base-pairs!");
/* =============== chi angle atoms =============== */
/* chi of R: O4'-C1'-N9-C4, Y: O4'-C1'-N1-C2 */
RC4_idx = lvector(1, nR);
str_idx(&nRC4, RC4_idx, Ratom, "C4 ", nR);
RC4 = lvector(1, nRC4);
for (i = 1; i <= nRC4; i++)
RC4[i] = Ridx[RC4_idx[i]];
YC2_idx = lvector(1, nY);
str_idx(&nYC2, YC2_idx, Yatom, "C2 ", nY);
YC2 = lvector(1, nYC2);
for (i = 1; i <= nYC2; i++)
YC2[i] = Yidx[YC2_idx[i]];
/* Combine and sort RC4 and YC2 */
if (nRC4 + nYC2 != lbn)
nrerror("RC4'/YC2' atom #s does not match #s of base-pairs!");
C24 = lvector(1, lbn);
for (i = 1; i <= nRC4; i++)
C24[i] = RC4[i];
for (i = 1; i <= nYC2; i++)
C24[nRC4 + i] = YC2[i];
isort(lbn, C24);
str_idx(&nO4, O4, atom_id, "O4'", num_atom);
if (nO4 != lbn)
nrerror("O4' atom #s does not match #s of base-pairs!");
for (i = 1; i <= lbn; i++) {
j = (i - 1) * 4;
chi[j + 1] = O4[i];
chi[j + 2] = C1[i];
chi[j + 3] = N[i];
chi[j + 4] = C24[i];
}
/* =============== sugar ring atoms =============== */
C2 = lvector(1, lbn);
str_idx(&nC2, C2, atom_id, "C2'", num_atom);
if (nC2 != lbn)
nrerror("C2' atom #s does not match #s of base-pairs!");
for (i = 1; i <= lbn; i++) {
j = (i - 1) * 5;
sugar[j + 1] = C4[i];
sugar[j + 2] = O4[i];
sugar[j + 3] = C1[i];
sugar[j + 4] = C2[i];
sugar[j + 5] = C3[i];
}
/* =============== base atoms =============== */
for (i = 1; i <= num_atom; i++) {
if ((atom_id[i][0] != 'P') &&
(atom_id[i][2] != 'P') && (atom_id[i][2] != '\'') && (atom_id[i][0] != 'H')) {
(*n_base)++;
base[*n_base] = i;
}
}
/* Free all the temporary integer vectors */
free_lvector(Ridx, 1, num_atom);
free_lvector(Yidx, 1, num_atom);
free_lvector(RC1_idx, 1, nR);
free_lvector(RC1, 1, nRC1);
free_lvector(YC1_idx, 1, nY);
free_lvector(YC1, 1, nYC1);
free_lvector(RN9_idx, 1, nR);
free_lvector(RN9, 1, nRN9);
free_lvector(YN1_idx, 1, nY);
free_lvector(YN1, 1, nYN1);
free_lvector(RC8_idx, 1, nR);
free_lvector(RC8, 1, nRC8);
free_lvector(YC6_idx, 1, nY);
free_lvector(YC6, 1, nYC6);
free_lvector(O5, 1, lbn);
free_lvector(C5, 1, lbn);
free_lvector(C4, 1, lbn);
free_lvector(C3, 1, lbn);
free_lvector(O3, 1, lbn);
free_lvector(RC4_idx, 1, nR);
free_lvector(RC4, 1, nRC4);
free_lvector(YC2_idx, 1, nY);
free_lvector(YC2, 1, nYC2);
free_lvector(C2, 1, lbn);
free_cmatrix(Ratom, 1, num_atom, 1, 4);
free_cmatrix(Yatom, 1, num_atom, 1, 4);
free_lvector(C24, 1, lbn);
free_dvector(dd, 1, lbn);
free_dvector(tmp_vec, 1, 3);
}
void set_str(double **xyz, long *C1, long *N, long *C68, long *base,
long *base_num, long n_base, long num_atom, long num_bp, FILE * fo)
/* SET_STR set the structure with regard to the global reference frame
if it is NOT strongly curved (see also: get_frame)
Use C1' & RN9/YN1 equivalent atom-pair vectors to define the
"best-fit" helical axis
Note: xyz--both as input & output */
{
long i, j, k, n_vec, nbp1, lbn;
long *C1_I, *C1_II, *N_I, *N_II, *b_vecNum, *e_vecNum;
long *tmp1, *tmp2, *idx;
long *base_bn, *base_en, ns = 0, ne = 0;
long *bp1_idx, num_bp1;
double **vec_xyz;
double *z_axis, mrise, *pnts_dist;
double **rotmat, **rotmat_T, **xyz2;
double **bp1_xyz, *bp1_normal, *bp1x, *bp1y, *bp1z, **bp1xyz;
double *hinge, ang_deg, **glbxyz;
double **b_xy, **e_xy, **dxy, *g, exy2, bxy2;
double **t2x2, **dxy_T, **inv_t2x2, *org_xyz;
nbp1 = num_bp - 1;
lbn = 2 * num_bp; /* Last base number */
if (num_bp < 2)
nrerror("Not enough vectors to define helical axis!");
C1_I = lvector(1, num_bp);
C1_II = lvector(1, num_bp);
N_I = lvector(1, num_bp);
N_II = lvector(1, num_bp);
/* Get vector indexing for each strand in 5'-->3' of strand I */
for (i = 1; i <= num_bp; i++) {
j = lbn - i + 1;
C1_I[i] = C1[i];
C1_II[i] = C1[j];
N_I[i] = N[i];
N_II[i] = N[j];
}
n_vec = 4 * nbp1;
b_vecNum = lvector(1, n_vec);
e_vecNum = lvector(1, n_vec);
vec_xyz = dmatrix(1, n_vec, 1, 3);
/* Vector beginning numbering */
j = 0;
for (i = 1; i <= nbp1; i++)
b_vecNum[j + i] = C1_I[i];
j += nbp1;
for (i = 1; i <= nbp1; i++)
b_vecNum[j + i] = C1_II[i];
j += nbp1;
for (i = 1; i <= nbp1; i++)
b_vecNum[j + i] = N_I[i];
j += nbp1;
for (i = 1; i <= nbp1; i++)
b_vecNum[j + i] = N_II[i];
/* Vector ending numbering */
j = 0;
for (i = 2; i <= num_bp; i++)
e_vecNum[j + i - 1] = C1_I[i];
j += nbp1;
for (i = 2; i <= num_bp; i++)
e_vecNum[j + i - 1] = C1_II[i];
j += nbp1;
for (i = 2; i <= num_bp; i++)
e_vecNum[j + i - 1] = N_I[i];
j += nbp1;
for (i = 2; i <= num_bp; i++)
e_vecNum[j + i - 1] = N_II[i];
/* Get the actual vectors for defining the helical axis */
for (i = 1; i <= n_vec; i++) {
for (j = 1; j <= 3; j++)
vec_xyz[i][j] = xyz[e_vecNum[i]][j] - xyz[b_vecNum[i]][j];
}
z_axis = dvector(1, 3);
pnts_dist = dvector(1, n_vec);
/* Get the "best-fit" helical axis, i.e. z_axis */
lsplane(z_axis, &mrise, pnts_dist, vec_xyz, n_vec);
/* Align the structure along +z_axis (5'-->3' of strand I) */
rotmat = dmatrix(1, 3, 1, 3);
rotmat_T = dmatrix(1, 3, 1, 3);
xyz2 = dmatrix(1, num_atom, 1, 3);
if (mrise < 0) {
for (i = 1; i <= 3; i++)
z_axis[i] = -z_axis[i];
}
fprintf(fo, "Helical axis: %9.3f%9.3f%9.3f\n\n", z_axis[1], z_axis[2], z_axis[3]);
alignz(rotmat, z_axis);
tr_dmatrix(rotmat_T, rotmat, 3, 3);
mul_dmatrix(xyz2, xyz, rotmat_T, num_atom, 3, 3, 3);
/* z_axis is now [0 0 1] */
z_axis[1] = 0;
z_axis[2] = 0;
z_axis[3] = 1;
/* Get the indexing of the first base-pair & its normal vector */
tmp1 = lvector(1, n_base - 1);
tmp2 = lvector(1, n_base + 1);
idx = lvector(1, n_base);
/* Good for handling bases with continuous sequential # */
for (i = 1; i <= n_base; i++)
idx[i] = base[i] + 100 * base_num[base[i]];
i_diff(tmp1, idx, n_base);
for (i = 1; i <= n_base - 1; i++)
tmp1[i] = tmp1[i] - 1;
logical_not(tmp1, tmp1, n_base - 1);
tmp2[1] = 0;
tmp2[n_base + 1] = 0;
for (i = 1; i <= n_base - 1; i++)
tmp2[i + 1] = tmp1[i];
i_diff(idx, tmp2, n_base + 1);
base_bn = lvector(1, lbn);
base_en = lvector(1, lbn);
for (i = 1; i <= n_base; i++) {
if (idx[i] == 1) {
ns++;
if (ns > lbn)
nrerror("Something wrong with naming in you PDB file!");
base_bn[ns] = base[i];
}
if (idx[i] == -1) {
ne++;
if (ne > lbn)
nrerror("Something wrong with naming in you PDB file!");
base_en[ne] = base[i];
}
}
/* Note: ns & ne should be 2*num_bp */
if (ns != lbn)
nrerror("Something wrong with naming in you PDB file!");
num_bp1 = (base_en[1] - base_bn[1] + 1) + (base_en[lbn] - base_bn[lbn] + 1);
bp1_idx = lvector(1, num_bp1);
for (i = base_bn[1]; i <= base_en[1]; i++) {
j = i - base_bn[1] + 1;
bp1_idx[j] = i;
}
k = j;
for (i = base_bn[lbn]; i <= base_en[lbn]; i++) {
j = i - base_bn[lbn] + 1;
bp1_idx[k + j] = i;
}
bp1_xyz = dmatrix(1, num_bp1, 1, 3);
bp1_normal = dvector(1, 3);
bp1x = dvector(1, 3);
bp1y = dvector(1, 3);
bp1z = dvector(1, 3);
bp1xyz = dmatrix(1, 3, 1, 3);
for (i = 1; i <= num_bp1; i++) {
k = bp1_idx[i];
for (j = 1; j <= 3; j++)
bp1_xyz[i][j] = xyz2[k][j];
}
/* Get the first base-pair's xyz-axes */
free_dvector(pnts_dist, 1, n_vec); /* Free original */
pnts_dist = dvector(1, num_bp1); /* Create a new one */
lsplane(bp1_normal, &mrise, pnts_dist, bp1_xyz, num_bp1);
for (i = 1; i <= 3; i++)
bp1y[i] = xyz2[C68[1]][i] - xyz2[C68[lbn]][i];
norm_dvector(bp1y, bp1y, 3);
vec_orth(bp1z, bp1_normal, bp1y);
cross_dvector(bp1x, bp1y, bp1z);
for (i = 1; i <= 3; i++) {
bp1xyz[i][1] = bp1x[i];
bp1xyz[i][2] = bp1y[i];
bp1xyz[i][3] = bp1z[i];
}
/* Align the first base-pair's z-axis with global z-axis */
hinge = dvector(1, 3);
glbxyz = dmatrix(1, 3, 1, 3);
cross_dvector(hinge, z_axis, bp1z);
ang_deg = magang(z_axis, bp1z);
arbrot(rotmat, hinge, -ang_deg);
mul_dmatrix(glbxyz, rotmat, bp1xyz, 3, 3, 3, 3);
/* Use a least-square method to define global origin's xy coordinates */
b_xy = dmatrix(1, n_vec, 1, 2);
e_xy = dmatrix(1, n_vec, 1, 2);
dxy = dmatrix(1, n_vec, 1, 2);
g = dvector(1, n_vec);
for (i = 1; i <= n_vec; i++) {
exy2 = 0;
bxy2 = 0;
for (j = 1; j <= 2; j++) {
b_xy[i][j] = xyz2[b_vecNum[i]][j];
e_xy[i][j] = xyz2[e_vecNum[i]][j];
dxy[i][j] = 2 * (e_xy[i][j] - b_xy[i][j]);
exy2 = exy2 + e_xy[i][j] * e_xy[i][j];
bxy2 = bxy2 + b_xy[i][j] * b_xy[i][j];
}
g[i] = exy2 - bxy2;
}
t2x2 = dmatrix(1, 2, 1, 2);
dxy_T = dmatrix(1, 2, 1, n_vec);
inv_t2x2 = dmatrix(1, 2, 1, 2);
org_xyz = dvector(1, 3);
tr_dmatrix(dxy_T, dxy, n_vec, 2);
mul_dmatrix(t2x2, dxy_T, dxy, 2, n_vec, n_vec, 2);
dinverse(t2x2, 2, inv_t2x2);
/* Using b_xy as a temporary array */
mul_dmatrix(b_xy, dxy, inv_t2x2, n_vec, 2, 2, 2);
zero_dvector(org_xyz, 3);
for (i = 1; i <= 2; i++)
for (j = 1; j <= n_vec; j++)
org_xyz[i] += g[j] * b_xy[j][i];
/* Set global origin's z coordinate to be that of the first base-pair */
org_xyz[3] = 0.5 * (xyz2[C68[1]][3] + xyz2[C68[lbn]][3]);
/* Reset the whole structure in the global reference frame */
for (i = 1; i <= num_atom; i++)
for (j = 1; j <= 3; j++)
xyz2[i][j] = xyz2[i][j] - org_xyz[j];
mul_dmatrix(xyz, xyz2, glbxyz, num_atom, 3, 3, 3);
free_lvector(C1_I, 1, num_bp);
free_lvector(C1_II, 1, num_bp);
free_lvector(N_I, 1, num_bp);
free_lvector(N_II, 1, num_bp);
free_lvector(b_vecNum, 1, n_vec);
free_lvector(e_vecNum, 1, n_vec);
free_dvector(z_axis, 1, 3);
free_lvector(tmp1, 1, n_base - 1);
free_lvector(tmp2, 1, n_base + 1);
free_lvector(idx, 1, n_base);
free_lvector(base_bn, 1, lbn);
free_lvector(base_en, 1, lbn);
free_lvector(bp1_idx, 1, num_bp1);
free_dvector(bp1_normal, 1, 3);
free_dvector(bp1x, 1, 3);
free_dvector(bp1y, 1, 3);
free_dvector(bp1z, 1, 3);
free_dvector(pnts_dist, 1, num_bp1);
free_dvector(hinge, 1, 3);
free_dvector(g, 1, n_vec);
free_dvector(org_xyz, 1, 3);
free_dmatrix(vec_xyz, 1, n_vec, 1, 3);
free_dmatrix(rotmat, 1, 3, 1, 3);
free_dmatrix(rotmat_T, 1, 3, 1, 3);
free_dmatrix(xyz2, 1, num_atom, 1, 3);
free_dmatrix(bp1_xyz, 1, num_bp1, 1, 3);
free_dmatrix(bp1xyz, 1, 3, 1, 3);
free_dmatrix(glbxyz, 1, 3, 1, 3);
free_dmatrix(b_xy, 1, n_vec, 1, 2);
free_dmatrix(e_xy, 1, n_vec, 1, 2);
free_dmatrix(dxy, 1, n_vec, 1, 2);
free_dmatrix(t2x2, 1, 2, 1, 2);
free_dmatrix(dxy_T, 1, 2, 1, n_vec);
free_dmatrix(inv_t2x2, 1, 2, 1, 2);
}
void wrt_hel_dat(long num_atom, char **atom_id, char *base_type,
char *strand, long *base_num, double **xyz,
long *base, long n_base, char *filstr)
/* WRT_HEL_DAT write the helical structural data in PDB format */
{
char **b_asym, *b_btype, *b_strand, filnam[BUF512];
long i, j, *b_bnum;
double **b_xyz;
/* First: Write out the whole structure */
strcpy(filnam, filstr);
wrtpdb(num_atom, atom_id, base_type, strand, base_num, xyz, strcat(filnam, "all"));
/* Second: Write out the structure with ONLY base */
b_asym = cmatrix(1, n_base, 1, 4);
b_btype = cvector(1, n_base);
b_strand = cvector(1, n_base);
b_bnum = lvector(1, n_base);
b_xyz = dmatrix(1, n_base, 1, 3);
for (i = 1; i <= n_base; i++) {
strcpy(b_asym[i], atom_id[base[i]]);
b_btype[i] = base_type[base[i]];
b_strand[i] = strand[base[i]];
b_bnum[i] = base_num[base[i]];
for (j = 1; j <= 3; j++)
b_xyz[i][j] = xyz[base[i]][j];
}
strcpy(filnam, filstr);
wrtpdb(n_base, b_asym, b_btype, b_strand, b_bnum, b_xyz, strcat(filnam, "bp"));
free_cmatrix(b_asym, 1, n_base, 1, 4);
free_cvector(b_btype, 1, n_base);
free_cvector(b_strand, 1, n_base);
free_lvector(b_bnum, 1, n_base);
free_dmatrix(b_xyz, 1, n_base, 1, 3);
}
void main_chain(long *mc, long nmc, long *chi, long num_bp, double **xyz,
char **bp_seq, FILE * fo)
/* MAIN_CHAIN get the torsion angles of the sugar-phosphate backbone
and glycosyl angle chi */
{
long ip1; /* Indicator for P atom */
long nmcta; /* Number of main-chain torsion angles */
double *mcta1, *mcta2, *mcta_total, **mcta_matrix, *ep_ze;
long num2; /* Beginning number of strand II */
long i, j, n, k, nt, *tor_idx;
double **tmc_xyz;
double *chi1, *chi2;
char bstr[BUF512];
/* Get the main chain torsion angles, all in 5'-->3' direction of I */
ip1 = (nmc / 2) % 2; /* 0 if first atom is P, 1 for O5' */
nt = 6 * num_bp; /* Number of total torsion angles */
nmcta = nt - 3 - ip1;
mcta1 = dvector(1, nmcta);
mcta2 = dvector(1, nmcta);
mcta_total = dvector(1, nt);
mcta_matrix = dmatrix(1, num_bp, 1, 6);
tor_idx = lvector(1, 4);
tmc_xyz = dmatrix(1, 4, 1, 3); /* Four sets of xyz coordinates */
for (i = 0; i <= 3; i++)
tor_idx[i + 1] = i;
num2 = nmc / 2;
for (i = 1; i <= nmcta; i++) {
for (j = 1; j <= 4; j++) /* For strand I */
for (k = 1; k <= 3; k++)
tmc_xyz[j][k] = xyz[mc[i + tor_idx[j]]][k];
mcta1[i] = torsion(tmc_xyz);
for (j = 1; j <= 4; j++) /* For strand II */
for (k = 1; k <= 3; k++)
tmc_xyz[j][k] = xyz[mc[num2 + i + tor_idx[j]]][k];
mcta2[i] = torsion(tmc_xyz);
}
/* Get the chi torsion angles */
chi1 = dvector(1, num_bp);
chi2 = dvector(1, num_bp);
num2 = 4 * num_bp;
for (i = 1; i <= num_bp; i++) {
n = (i - 1) * 4 + 1;
for (j = 1; j <= 4; j++)
for (k = 1; k <= 3; k++)
tmc_xyz[j][k] = xyz[chi[n + tor_idx[j]]][k];
chi1[i] = torsion(tmc_xyz);
for (j = 1; j <= 4; j++)
for (k = 1; k <= 3; k++)
tmc_xyz[j][k] = xyz[chi[num2 + n + tor_idx[j]]][k];
chi2[i] = torsion(tmc_xyz);
}
if (ip1 == 1)
mcta_total[2] = 0.0; /* First beta */
mcta_total[1] = 0.0; /* First alpha */
mcta_total[nt - 1] = 0.0; /* Last epsilon */
mcta_total[nt] = 0.0; /* Last zeta */
ep_ze = dvector(1, num_bp);
/* For strand I */
for (i = 1; i <= nmcta; i++)
mcta_total[i + ip1 + 1] = mcta1[i];
for (i = 1; i <= num_bp; i++) {
for (j = 1; j <= 6; j++)
mcta_matrix[i][j] = mcta_total[(i - 1) * 6 + j];
ep_ze[i] = mcta_matrix[i][5] - mcta_matrix[i][6];
}
/* Print out the main chain torsion angle and chi */
prt_sep(fo, '*', 76);
fprintf(fo, "Main chain torsion angles: \n\n");
fprintf(fo, "Note: alpha: O3'-P-O5'-C5'\n");
fprintf(fo, " beta: P-O5'-C5'-C4'\n");
fprintf(fo, " gamma: O5'-C5'-C4'-C3'\n");
fprintf(fo, " delta: C5'-C4'-C3'-O3'\n");
fprintf(fo, " epsilon: C4'-C3'-O3'-P\n");
fprintf(fo, " zeta: C3'-O3'-P-O5'\n\n");
fprintf(fo, " chi for pyrimidines: O4'-C1'-N1-C2\n");
fprintf(fo, " chi for purines: O4'-C1'-N9-C4\n\n");
fprintf(fo, "Strand I\n");
fprintf(fo, " base alpha beta gamma delta");
fprintf(fo, " epsilon zeta ep-ze chi\n");
strcpy(bstr, " -- ");
i = 1; /* For the first set of torsion angles: No alpha OR beta */
fprintf(fo, "%2ld%3c ", i, bp_seq[i][1]);
if (ip1 == 1)
fprintf(fo, "%s%s", bstr, bstr);
else
fprintf(fo, "%s", bstr);
for (j = ip1 + 2; j <= 6; j++)