File: mmdbapi4.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 241,628 kB
  • ctags: 101,236
  • sloc: ansic: 1,431,713; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (1904 lines) | stat: -rw-r--r-- 62,343 bytes parent folder | download | duplicates (12)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
/*   mmdbapi4.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
* ===========================================================================
*
* File Name:  mmdbapi4.c
*
* Author:  Christopher Hogue
*
* Version Creation Date:   10/18/95
*
* $Revision: 6.8 $
*
* File Description: Code for Modelstruc -> Biostruc conversion
*                   And User-feature handling.
*
* Modifications:  
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
* $Log: mmdbapi4.c,v $
* Revision 6.8  2003/05/13 13:48:20  bauer
* changed iCountAtoms in RefreshModelAsnMem from Int2 to Int4
*
* Revision 6.7  2000/12/05 22:13:07  ywang
* fix bugs for MakePDBSeqId2
*
* Revision 6.6  1999/03/12 18:37:08  kans
* fixed ErrPostEx problem
*
* Revision 6.5  1999/02/25 23:11:53  ywang
* synchronize with cn3d to save out mime data
*
* Revision 6.4  1998/09/02 16:25:27  kans
* removed comments around debug ifdef to suppress printf statement
*
* Revision 6.3  1998/08/26 18:02:43  kans
* fixed -v -fd warnings
*
* Revision 6.2  1998/03/10 04:31:08  lewisg
* change name of MakePDBSeqId to MakePDBSeqId2 to avoid name conflict
*
* Revision 6.1  1998/03/06 23:17:29  lewisg
* codewarrior fixes
*
* Revision 6.0  1997/08/25 18:11:16  madden
* Revision changed to 6.0
*
* Revision 5.3  1996/11/26 19:15:44  epstein
* represent gaps correctly in sequence alignments
*
 * Revision 5.2  1996/11/25  16:14:41  epstein
 * fix off-by-one error since VAST alignments are indexed beginning at position 1 rather than 0
 *
 * Revision 5.1  1996/11/22  21:16:59  epstein
 * add BiostrucAnnotSetToSeqAnnot()
 *
 * Revision 5.0  1996/05/28  14:02:09  ostell
 * Set to revision 5.0
 *
 * Revision 1.4  1996/02/07  23:05:56  hogue
 * fixed a type cast in FreeSingleModel
 *
 * Revision 1.3  1996/02/02  21:26:26  hogue
 * changed to include only mmdbapi.h (brings in all 1-4)
 *
 * Revision 1.2  1996/02/02  19:45:01  hogue
 * Initial Revision
 *
*
* ==========================================================================
*/

/*****************************************************************************
*
*  mmdbapi4.c
*   
*   UNDER CONSTRUCTION NOTICE. 
*	EVERYTHING SUBJECT TO RADICAL CHANGES!
*   
*  programmer C.W.V. Hogue
*  created: 18 Oct 95
*  last mod: 
*****************************************************************************/

#include <mmdbapi.h>
#include <sequtil.h>






static Int4 ScaleToInt(FloatLo f)  
{

  Int4 iI;
  FloatHi fh;
  fh = f * SAVE_XYZ_SCALE_FACTOR;  /* scale floating point portion */
  if (fh > 0)  
    fh += 0.5;   /* this will push a 2.99999 to 3.099999 */
  else  fh -= 0.5;  /* or a -2172.9999 to a -2173.0999  */
  iI = (Int4) fh;  /* which is truncated here */
  return iI;
}		


Boolean LIBCALL  RefreshSurface(SurfaceCoordinatesPtr  pscThis, PMOD pmodThis)
{

    CylinderPtr pcylThis = NULL;
    BrickPtr pbrkThis = NULL;
    ConePtr pconThis = NULL;
    TMeshPtr ptmshThis = NULL;
    TrianglesPtr ptriThis = NULL;
    SpherePtr psphThis = NULL;
    ModelSpacePointPtr pmspTop = NULL;
    ModelSpacePointPtr pmspBottom = NULL;
    ModelSpacePointPtr pmspTemp = NULL;
    RealValuePtr prvRadius = NULL;
    ValNodePtr pvnSwap = NULL;
    ValNodePtr pvnX = NULL;
    ValNodePtr pvnY = NULL;
    ValNodePtr pvnZ = NULL;
    Int4 iCount = 0;
    Int4 iCoords = 0;
    Int4 iVal = 0;
    Boolean rsult = FALSE;

    switch (pscThis->Surface_surface->choice)
      {
	   case Surface_surface_cylinder:
 	     if (!(pmodThis->bWhat & (Byte) OBJ_CYLINDER)) return FALSE;
	     pcylThis = (CylinderPtr) pscThis->Surface_surface->data.ptrvalue;
	     if (!pmodThis->ppflObject) return FALSE;
	     pmspTop = pcylThis->axis_top;
             pmspBottom = pcylThis->axis_bottom;
	     prvRadius = pcylThis->radius;
 	     pmspTop->x = ScaleToInt(pmodThis->ppflObject[0][0]); 
	     pmspTop->y = ScaleToInt(pmodThis->ppflObject[0][1]);
	     pmspTop->z = ScaleToInt(pmodThis->ppflObject[0][2]); 
	     pmspTop->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspBottom->x = ScaleToInt(pmodThis->ppflObject[1][0]);  
	     pmspBottom->y = ScaleToInt(pmodThis->ppflObject[1][1]);
	     pmspBottom->z = ScaleToInt(pmodThis->ppflObject[1][2]); 
	     pmspBottom->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     prvRadius->scaled_integer_value = ScaleToInt(pmodThis->flRadius);
	     prvRadius->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     return TRUE;
	   case Surface_surface_sphere:
 	     if (!(pmodThis->bWhat & (Byte) OBJ_SPHERE)) return FALSE;
	     psphThis = (SpherePtr) pscThis->Surface_surface->data.ptrvalue;
	     if (!pmodThis->ppflObject) return FALSE;
	     pmspTop = psphThis->center;
             prvRadius = psphThis->radius;
 	     pmspTop->x = ScaleToInt(pmodThis->ppflObject[0][0]);
	     pmspTop->y = ScaleToInt(pmodThis->ppflObject[0][1]);
	     pmspTop->z = ScaleToInt(pmodThis->ppflObject[0][2]);
	     pmspTop->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     prvRadius->scaled_integer_value = ScaleToInt(pmodThis->flRadius);
	     prvRadius->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     return TRUE;
	   case Surface_surface_brick:
 	     if (!(pmodThis->bWhat &(Byte) OBJ_BRICK)) return FALSE;
	     pbrkThis = (BrickPtr) pscThis->Surface_surface->data.ptrvalue;
	     if (!pmodThis->ppflObject) return FALSE;
	     pmspTemp = pbrkThis->corner_000;
	     pmspTemp->x = ScaleToInt(pmodThis->ppflObject[0][0]);  
	     pmspTemp->y = ScaleToInt(pmodThis->ppflObject[0][1]); 
	     pmspTemp->z = ScaleToInt(pmodThis->ppflObject[0][2]);  
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_001;
	     pmspTemp->x = ScaleToInt(pmodThis->ppflObject[1][0]);  
	     pmspTemp->y = ScaleToInt(pmodThis->ppflObject[1][1]); 
	     pmspTemp->z = ScaleToInt(pmodThis->ppflObject[1][2]); 
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_010;
	     pmspTemp->x =  ScaleToInt(pmodThis->ppflObject[2][0]); 
	     pmspTemp->y =  ScaleToInt(pmodThis->ppflObject[2][1]); 
	     pmspTemp->z =  ScaleToInt(pmodThis->ppflObject[2][2]); 
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_011;
	     pmspTemp->x =  ScaleToInt(pmodThis->ppflObject[3][0]); 
	     pmspTemp->y =  ScaleToInt(pmodThis->ppflObject[3][1]);  
	     pmspTemp->z =  ScaleToInt(pmodThis->ppflObject[3][2]);  
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_100;
	     pmspTemp->x =  ScaleToInt(pmodThis->ppflObject[4][0]); 
	     pmspTemp->y =  ScaleToInt(pmodThis->ppflObject[4][1]); 
	     pmspTemp->z =  ScaleToInt(pmodThis->ppflObject[4][2]); 
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_101;
	     pmspTemp->x =  ScaleToInt(pmodThis->ppflObject[5][0]); 
	     pmspTemp->y =  ScaleToInt(pmodThis->ppflObject[5][1]); 
	     pmspTemp->z =  ScaleToInt(pmodThis->ppflObject[5][2]); 
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_110;
	     pmspTemp->x =  ScaleToInt(pmodThis->ppflObject[6][0]); 
	     pmspTemp->y =  ScaleToInt(pmodThis->ppflObject[6][1]); 
	     pmspTemp->z =  ScaleToInt(pmodThis->ppflObject[6][2]); 
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspTemp = pbrkThis->corner_111;
	     pmspTemp->x =  ScaleToInt(pmodThis->ppflObject[7][0]);
	     pmspTemp->y =  ScaleToInt(pmodThis->ppflObject[7][1]);
	     pmspTemp->z =  ScaleToInt(pmodThis->ppflObject[7][2]);
	     pmspTemp->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     return TRUE;	   
	   case Surface_surface_cone:
 	     if (!(pmodThis->bWhat & (Byte) OBJ_CONE)) return FALSE;
	     pconThis = (ConePtr) pscThis->Surface_surface->data.ptrvalue;
	     if (!pmodThis->ppflObject) return FALSE;
	     pmspTop = pconThis->axis_top;
             pmspBottom = pconThis->axis_bottom;
	     prvRadius = pconThis->radius_bottom;
 	     pmspTop->x = ScaleToInt(pmodThis->ppflObject[0][0]);
	     pmspTop->y = ScaleToInt(pmodThis->ppflObject[0][1]);
	     pmspTop->z = ScaleToInt(pmodThis->ppflObject[0][2]);
	     pmspTop->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     pmspBottom->x = ScaleToInt(pmodThis->ppflObject[1][0]);
	     pmspBottom->y = ScaleToInt(pmodThis->ppflObject[1][1]);
	     pmspBottom->z=  ScaleToInt(pmodThis->ppflObject[1][2]);
	     pmspBottom->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     prvRadius->scaled_integer_value = ScaleToInt(pmodThis->flRadius);
	     prvRadius->scale_factor = SAVE_XYZ_SCALE_FACTOR;;
	     return TRUE;
	   case Surface_surface_tmesh:
	     if (!(pmodThis->bWhat & (Byte) OBJ_TMESH)) return FALSE;
	     ptmshThis = (TMeshPtr) pscThis->Surface_surface->data.ptrvalue;
	     if (!pmodThis->ppflObject) return FALSE;
	     if (!pmodThis->pucSwap) return FALSE; 
	     ptmshThis->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     iCoords = pmodThis->iCoordNo;
	     for (iCount = 0; iCount < iCoords; iCount++) 
	        {
		    pvnX = ValNodeAddInt(&pvnX, 0, ScaleToInt(pmodThis->ppflObject[iCount][0]));
		    pvnY = ValNodeAddInt(&pvnY, 0, ScaleToInt(pmodThis->ppflObject[iCount][1]));
		    pvnZ = ValNodeAddInt(&pvnZ, 0, ScaleToInt(pmodThis->ppflObject[iCount][2]));
		    pvnSwap = ValNodeAddBoolean(&pvnSwap, 0, (Boolean)(pmodThis->pucSwap[iCount] == 
					    (Uchar) 1)); 
		    if (ptmshThis->x == NULL)
		       {
			 ptmshThis->x = pvnX;  /* these have been set to null */
		         ptmshThis->y = pvnY;
			 ptmshThis->z = pvnZ;
			 ptmshThis->swap = pvnSwap;
		       } 
		    if ((pvnX == NULL) || (pvnY == NULL) || (pvnZ == NULL) || (pvnSwap == NULL))
			goto objallocerr;
		} 
	     ptmshThis->number_of_points = iCoords;
	     return TRUE;
	   case Surface_surface_triangles:
 	     if (!(pmodThis->bWhat & (Byte) OBJ_TRIANGLES)) return FALSE;
	     ptriThis = (TrianglesPtr) pscThis->Surface_surface->data.ptrvalue;
	     if (!pmodThis->ppflObject) return FALSE;
	     if (!pmodThis->ppi4Triangles) return FALSE;
	     ptriThis->scale_factor = SAVE_XYZ_SCALE_FACTOR;
	     iCoords = pmodThis->iCoordNo;
	     for (iCount = 0; iCount < iCoords; iCount++) 
	        {
		    pvnX = ValNodeAddInt(&pvnX, 0, ScaleToInt(pmodThis->ppflObject[iCount][0]));
		    pvnY = ValNodeAddInt(&pvnY, 0, ScaleToInt(pmodThis->ppflObject[iCount][1]));
		    pvnZ = ValNodeAddInt(&pvnZ, 0, ScaleToInt(pmodThis->ppflObject[iCount][2]));
		    if (ptmshThis->x == NULL)
		       {
			 ptriThis->x = pvnX;  /* these have been set to null */
		         ptriThis->y = pvnY;
			 ptriThis->z = pvnZ;
		       } 
		    if ((pvnX == NULL) || (pvnY == NULL) || (pvnZ == NULL))
			goto objallocerr;
		} 
	     ptriThis->number_of_points = iCoords;  
	     iCoords = pmodThis->iTriNo;
	     pvnX = NULL; 
	     pvnY = NULL;
	     pvnZ = NULL;
	     for (iCount = 0; iCount < iCoords; iCount++) 
	        {
		    pvnX = ValNodeAddInt(&pvnX, 0, pmodThis->ppi4Triangles[iCount][0]);
		    pvnY = ValNodeAddInt(&pvnY, 0, pmodThis->ppi4Triangles[iCount][1] );
		    pvnZ = ValNodeAddInt(&pvnZ, 0, pmodThis->ppi4Triangles[iCount][2] );
		    if (ptmshThis->x == NULL)
		       {
			 ptriThis->v1 = pvnX;  /* these have been set to null */
		         ptriThis->v2 = pvnY;
			 ptriThis->v3 = pvnZ;
		       } 
		    if ((pvnX == NULL) || (pvnY == NULL) || (pvnZ == NULL))
			goto objallocerr;
		 
		}  
	     ptriThis->number_of_triangles = iCoords;
	     return TRUE;
	   default:
	     rsult = FALSE;
       }
    return rsult;   
    
    objallocerr:
   
      FreeSurfaceModelAsnList(pscThis);
      return FALSE;
}



		
			

static void FreeCoordinatesAsnLists(CoordinatesPtr pcoThis)
{
    AtomicCoordinatesPtr 		pacThis = NULL;
    SurfaceCoordinatesPtr		pscThis = NULL;
    DensityCoordinatesPtr		pdcThis = NULL; 
    
  
    switch (pcoThis->choice)
     {
      case Coordinates_surface:
        pscThis = (SurfaceCoordinatesPtr) pcoThis->data.ptrvalue;
	FreeSurfaceModelAsnList(pscThis);
        return;
      case Coordinates_density:
	pdcThis = (DensityCoordinatesPtr) pcoThis->data.ptrvalue;
/*	FreeDensityModelAsnList(pdcThis); */
        return;
      case Coordinates_atomic:
	pacThis = (AtomicCoordinatesPtr) pcoThis->data.ptrvalue;
	FreeAtomicModelAsnLists(pacThis);
	return;
      default:
        return;
     }
}	
	
	
void LIBCALL   FreeRedundantAsn(PDNMS pdnmsThis)
{
   /* does un-hooking of dict from chemical graph; frees chem-graph */
   /* Recoverable - out of memory errors are handled with this */
   /* frees model data ASN.1 & parts of chemical graph  */

    PMLD pmldThis = NULL;
    PMSD pmsdThis = NULL;
    PDNML pdnmlThis = NULL;
    BiostrucModelPtr pbsmThis = NULL;
    ModelCoordinateSetPtr pmcsThis = NULL;
    CoordinatesPtr pcoThis = NULL;
    PDNMM pdnmmThis = NULL;
    PMMD pmmdThis = NULL;
    MoleculeGraphPtr pmgThis = NULL;

    /* get rid of model redundant stuff */
    if (!pdnmsThis) return;
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
    if (!pmsdThis) return;
    
    /* remove redundant parts of Chemical Graph */
    /* keep, dictionary, descrs and seq_id's */
    
    if (pmsdThis->pbsBS->chemical_graph)
      {
	/* unhook dict from chem graph */
	if (pmsdThis->pbsBS->chemical_graph->residue_graphs)
	pmsdThis->pbsBS->chemical_graph->residue_graphs = NULL;
	/* these are pointed to also by pmsdThis->pDictLocal */

	/* unhook the Descr's from chem graph */
	if (pmsdThis->pbsBS->chemical_graph->descr)
	  pmsdThis->pbsBS->chemical_graph->descr = NULL;
	/* pointed to by pmsdThis->pGraphDescr */
    
	/* walk the molecules in the chem graph, unhook the pMolDescr, pSeqId */
	pmgThis = pmsdThis->pbsBS->chemical_graph->molecule_graphs;
	while (pmgThis) /* walk up the molecule_graphs */
	{
	    pmgThis->descr = NULL;
	    pmgThis->seq_id = NULL;
	    pmgThis = pmgThis->next;
	}
      
	/* free chem-graph */
	BiostrucGraphFree(pmsdThis->pbsBS->chemical_graph);
	pmsdThis->pbsBS->chemical_graph = NULL;
      }
  
    /* remove redundant parts of models */ 
    
    pdnmlThis = pmsdThis->pdnmlModels;
    while (pdnmlThis)
      {
	pmldThis = (PMLD) pdnmlThis->data.ptrvalue;
	if (!pmldThis) return;	
	while (pmldThis) 
	{  
	    pmcsThis = pmldThis->pbsmModel->model_coordinates;
	    while (pmcsThis)
		{  /* match the two together */  
		    if (pmcsThis->id == pmldThis->iNoCoordSet) break;
		    pmcsThis = pmcsThis->next;
		}  /* find the Asn.1 stub */
	    pcoThis = (CoordinatesPtr) pmcsThis->Coordinates_coordinates->data.ptrvalue;
	    if (pcoThis) FreeCoordinatesAsnLists(pcoThis);  /* pare down to stubs */
	    pmldThis = pmldThis->next;
	}
       pdnmlThis = pdnmlThis->next;
     }
  return;  
}	


 
Boolean LIBCALL RefreshModelAsnMem(PDNMS pdnmsThis,  Int2 iModel)
{

/* Does a direct copy of all Modelstruc coordinates into Biostruc Model      */
/* data structures. Used to populate coordinates before saving               */
/* The ASN.1 stub is validated - and it must be present and complete, or     */ 
/* the routine will fail */
/* The stub would be absent under conditions where a model was               */
/* constructed by hand without using the NewModel() function 	             */


    PMAD	pmadThis = NULL;
    PALD	paldThis = NULL;
    PMOD	pmodThis = NULL;
    PMDD	pmddThis = NULL;
    PMLD	pmldThis = NULL;
    PDNML	pdnmlThis = NULL;
    PMSD	pmsdThis = NULL;
    PDNMM	pdnmmThis = NULL; 
    PMMD	pmmdThis = NULL; 
    PDNMG	pdnmgThis = NULL;
    PMGD	pmgdThis = NULL;  
    PVNMA	pvnmaThis = NULL;
    
    AlternateConformationIdsPtr 	paciThis = NULL;
    BiostrucModelPtr 			pbsmThis = NULL;
    AtomicCoordinatesPtr 		pacThis = NULL;
    IsotropicTemperatureFactorsPtr 	pitfThis = NULL;
    AnisotropicTemperatureFactorsPtr 	patfThis = NULL;
    AtomicOccupanciesPtr 		pocThis = NULL;
    ModelSpacePointsPtr 		pmspThis = NULL;
    AtomPntrsPtr			pappThis = NULL;
    CoordinatesPtr			pcoThis = NULL;
    ModelCoordinateSetPtr		pmcsThis = NULL;
    SurfaceCoordinatesPtr		pscThis = NULL;
  /*  DensityCoordinatesPtr		pdcThis = NULL; 
  */   
    CharPtr				pcTemp = NULL;
    Int4				iCountAtoms = 0;
    Int4				iX,  iY,  iZ;
    Int4				iCoordSets = 0;
    Boolean				bFirst = TRUE;
    Boolean				bGotoCalled = FALSE;
    ValNodePtr	pvnX = NULL;
    ValNodePtr	pvnY = NULL;
    ValNodePtr	pvnZ = NULL;
    ValNodePtr	pvnB11 = NULL;
    ValNodePtr	pvnB12 = NULL;
    ValNodePtr	pvnB13 = NULL;
    ValNodePtr	pvnB22 = NULL;
    ValNodePtr	pvnB23 = NULL;
    ValNodePtr	pvnB33 = NULL;
    ValNodePtr	pvnOcc = NULL;
    ValNodePtr	pvnAlt = NULL;
    ValNodePtr	pvnMid = NULL;
    ValNodePtr	pvnRid = NULL;
    ValNodePtr	pvnAid = NULL;
    ValNodePtr	pmdrThis = NULL;       
    int		iBailno = 0;
   
 
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
 /* find the right model in the doubly-linked list */
    if (!pmsdThis->pdnmlModels) 
      {

#ifdef _DEBUG_5 
printf("No models in structure...\n");
#endif    
   
	 return FALSE;
      }
    pdnmlThis = DValNodeFindNext(pmsdThis->pdnmlModels, NULL, iModel); 
    if (!pdnmlThis)
      {
#ifdef _DEBUG_5 
printf("No such model in RefreshModelAsn \n");
   
         pdnmlThis = pmsdThis->pdnmlModels;
 	 while (pdnmlThis)
	  {
 	    printf("but found model %d\n", (int) pdnmlThis->choice);
	    pdnmlThis = pdnmlThis->next;
	  }
	 return FALSE;  /* no such model */
#endif 
      }
 /* cast to the model node */
    pmldThis = (PMLD) pdnmlThis->data.ptrvalue;  
    if (!pmldThis->pbsmModel) { iBailno = 1; goto errorbail; }
 /* there must be a pbsmModel at this point */
    pbsmThis = pmldThis->pbsmModel;
    pbsmThis->id =  pmldThis->iId;   /* refresh the id, type */
    pbsmThis->type = pmldThis->iType;
    
    /* refresh  name string   */  
    /* find the old string */
    pmdrThis = ValNodeFindNext(pbsmThis->descr, NULL, ModelDescr_name);
    if (pmdrThis)
      {
         /* free it */
	 if (pmdrThis->data.ptrvalue) 
	   MemFree((CharPtr) pmdrThis->data.ptrvalue);
	 /* replace it */
	 if (pmldThis->pcModelName) 
	    pmdrThis->data.ptrvalue = StringSave(pmldThis->pcModelName);
	 else 
	    pmdrThis->data.ptrvalue = NULL;
      }  
    else
      {
	  ValNodeCopyStr(&pmldThis->pbsmModel->descr, ModelDescr_name, pmldThis->pcModelName);
	  /* if no string, this does nothing */
      }
      /* conditionally rebuild the ASN.1 stub */
    if (!pbsmThis->model_coordinates)
      {
	iBailno = 1;
	goto errorbail;
      }
      
    /* model_space is optional - if it wasn't already there we don't fill it in here... */
      
    if (!pbsmThis->model_coordinates) { iBailno = 2; goto errorbail; }
       /* there must be a model_coordinates node */
      
    /* Do each coord set for this one model */
    while (pmldThis) 
     {  
     
      /*
       * each pmldThis contains either one chemical graph's worth of ald's or
       * one object or one density  pointer 
       * There must be one pmcsThis for each pmldThis. 
       */   
	
	/* find the matching id in the ASN.1 stub model_coordinates list */
	pmcsThis = pbsmThis->model_coordinates;
	while (pmcsThis)
	  {
	    if (pmcsThis->id == pmldThis->iNoCoordSet) break;
	    pmcsThis = pmcsThis->next;
	  }  
	if (!pmcsThis){ iBailno = 3; goto errorbail; } /* not found - ASN.1 stub error condition */
	
	
	
	/* find all the heads of pointers we'll need to be updating */
        /* from within the ASN.1 stub */
    
	  
	if (!pmcsThis->Coordinates_coordinates) { iBailno = 4; goto errorbail; }  
	  /* this part of the ASN.1 stub must be present */
	if (pmcsThis->Coordinates_coordinates->choice == Coordinates_coordinates_reference) goto skipnext;
    	    /* cannot handle coordinates by reference yet */
		
	pcoThis = (CoordinatesPtr) pmcsThis->Coordinates_coordinates->data.ptrvalue;
	if (!pcoThis) { iBailno = 5; goto errorbail; } /* this part of the ASN.1 stub must be present */
   
          /* see what kind of data pmldThis contains, surface, density or atoms */
	  
        pmodThis = pmldThis->pmodObject;
	if (pmodThis) /* is it a surface object ? */
	  {
	    if (pcoThis->choice != Coordinates_surface){ iBailno = 6; goto errorbail; } /* ASN.1 stub mismatch */
	      /* rebuild or refresh ASN.1 object */
	      pscThis = (SurfaceCoordinatesPtr) pcoThis->data.ptrvalue;
	      FreeSurfaceModelAsnList(pscThis); 
	      if (!RefreshSurface(pscThis, pmodThis)) { iBailno = 6; goto memnewbail; } /* memory error */;
	      goto skipnext;
	  }
 
	pmddThis = pmldThis->pmddDensity;
	if (pmddThis)  /* is it a density object ?  */
	  {
	    if (pcoThis->choice != Coordinates_density) { iBailno = 7; goto errorbail; }  /* ASN.1 stub mismatch */
	      /* rebuild or refresh ASN.1 density */
	      
	/* 	FreeDensityAsnList();
		RefreshDensity();
	*/
	      goto skipnext;
	  }
	  
	/* still here - MUST be atoms */
	if (pcoThis->choice != Coordinates_atomic) { iBailno = 8; goto errorbail; }  /* ASN.1 stub mismatch */
	
	pacThis = NULL;
	pacThis = (AtomicCoordinatesPtr) pcoThis->data.ptrvalue;
	if (!pacThis) { iBailno = 9; goto errorbail; }  /* ASN.1 stub not complete */
	
	/* Free all remaining data off AtomicCoordinatesPtr pacThis */
	
	FreeAtomicModelAsnLists(pacThis);
	
	/* at this point pacThis is clean excepting any !!!CONF-ENSEMBLES!! that may be present */
	
	/* go ahead and rebuild the pacThis model linked-lists from the ALD node data */

	iCountAtoms = 0;
	bFirst = TRUE;
	bGotoCalled = FALSE;
	pdnmmThis = pmsdThis->pdnmmHead;
	if (pmldThis->ppAsnOrder)
	  {   /* if the order matrix is present, then     */
	      /* traverse nodes in order they were loaded */
	      /* This matrix begins with element #1 for ASN.1 indexing purposes */
	      /* and ends with a null pointer */
	    for (iCountAtoms = 1; pmldThis->ppAsnOrder[iCountAtoms] != NULL; iCountAtoms++)
	      {
		  paldThis = pmldThis->ppAsnOrder[iCountAtoms];
		  bGotoCalled = TRUE;
		  pmadThis = (PMAD) paldThis->pfbParent;
		  goto aldsaveblockstart;
		   /* goto used to avoid  code duplication                         */
		   /* making a separate function is impractical in this case owing */
		   /* to the large number of linked-lists kept as overhead         */
		   /* This way, everything stays in scope.                         */
		  aldsaveblockreturn: ;
		  		 
	      }  /* for */
	    iCountAtoms--;  /* back it up one to the correct count */
	  }  /* if ppAsnOrder */
	else  
	  /* traverse the hierachical structure to find paldThis */
	while(pdnmmThis)
	    {   /*molecule*/
		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
		pdnmgThis =  pmmdThis->pdnmgHead;
		while(pdnmgThis)
		{   /*graph*/
		    pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
		    pvnmaThis = pmgdThis->pvnmaAHead;
		    while (pvnmaThis)
			{   /*atom*/
			    pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
			    paldThis = GetAtomLocs(pmadThis,  iModel);
			    while (paldThis)
			      {
				iCountAtoms++;
	aldsaveblockstart:   /* goto used in the for loop to avoid code duplication */
				if ((!paldThis->pflvData)  || (paldThis->iFloatNo <  2)) goto errorbail; /* coords missing */
				iX =  ScaleToInt(paldThis->pflvData[0]);
				iY =  ScaleToInt(paldThis->pflvData[1]);
				iZ =  ScaleToInt(paldThis->pflvData[2]);
				   
				if (bFirst)
				  {
				     /* initialize lists off of pacThis */
				     /* note that each ALD must have same amt of data as first */
				    pacThis->atoms = AtomPntrsNew();
				    if (!pacThis->atoms) { iBailno = 10; goto memnewbail; } 
				       /* number_of_ptrs, molecule_ids, resiude_ids, atom_ids */
				    pacThis->atoms->molecule_ids = 
					ValNodeAddInt(	NULL, 
							0, 
							IndexFromNode((PFB)GetParentMol((PFB)pmadThis)));
				    if (!pacThis->atoms->molecule_ids) { iBailno = 11; goto memnewbail; } 
				    pvnMid = pacThis->atoms->molecule_ids;
				    pacThis->atoms->residue_ids = 
					ValNodeAddInt(	NULL, 
							0, 
							IndexFromNode((PFB)GetParentGraph((PFB)pmadThis)));
				    if (!pacThis->atoms->residue_ids){ iBailno = 12; goto memnewbail; } 	
				    pvnRid = pacThis->atoms->residue_ids;					
				    pacThis->atoms->atom_ids =
					ValNodeAddInt(	NULL, 
							0, 
							IndexFromNode((PFB)pmadThis));
				    if (!pacThis->atoms->atom_ids) { iBailno = 13; goto memnewbail; } 	  
				    pvnAid = pacThis->atoms->atom_ids; 
				    pacThis->sites = ModelSpacePointsNew();
				    if (!pacThis->sites){ iBailno = 14; goto memnewbail; } 
				      /* scale_factor, x, y, z */
				    pacThis->sites->x = ValNodeAddInt(NULL, 0, iX);
				    if (!pacThis->sites->x) { iBailno = 15; goto memnewbail; } 
				    pvnX = pacThis->sites->x;
				    pacThis->sites->y = ValNodeAddInt(NULL, 0, iY);
				    if (!pacThis->sites->y) { iBailno = 16; goto memnewbail; } 
				    pvnY = pacThis->sites->y;
				    pacThis->sites->z = ValNodeAddInt(NULL, 0, iZ); 
				    if (!pacThis->sites->z) { iBailno = 17; goto memnewbail; } 
				    pvnZ = pacThis->sites->z;
				    pacThis->sites->scale_factor = SAVE_XYZ_SCALE_FACTOR;
				    if (paldThis->iFloatNo >= 3)
				      {
					  pacThis->occupancies = AtomicOccupanciesNew();
					  if (!pacThis->occupancies) { iBailno = 18; goto memnewbail; } 
					     /* scale_factor, o */
					  iZ =  ScaleToInt(paldThis->pflvData[3]);
					  pacThis->occupancies->o = ValNodeAddInt(NULL, 0, iZ);
					  if (!pacThis->occupancies->o) { iBailno = 19; goto memnewbail; } 
					  pvnOcc = pacThis->occupancies->o;
					  pacThis->occupancies->scale_factor = SAVE_XYZ_SCALE_FACTOR;
				      }
				    if (paldThis->iFloatNo == 4) /* Isotropic Temp Factors */
				      {
					  pitfThis = IsotropicTemperatureFactorsNew();
					  if (!pitfThis) { iBailno = 20; goto memnewbail; } 
					  pacThis->temperature_factors = ValNodeAddPointer(NULL, 
										    AtomicTemperatureFactors_isotropic, 
										    pitfThis);
					  if (!pacThis->temperature_factors) { iBailno = 21; goto memnewbail; } 
					  iZ =  ScaleToInt(paldThis->pflvData[4]);
					  pitfThis->b = ValNodeAddInt(NULL, 0,  iZ);
					  if (!pitfThis->b){ iBailno = 22; goto memnewbail; } 
					  pvnB11 = pitfThis->b;
					  pitfThis->scale_factor = SAVE_XYZ_SCALE_FACTOR;
				      }
				    if (paldThis->iFloatNo == 9) /* Anisotropic Temp Factors */
				      {
					patfThis = AnisotropicTemperatureFactorsNew();
					if (!patfThis) { iBailno = 23; goto memnewbail; } 
					pacThis->temperature_factors = ValNodeAddPointer(NULL, 
										    AtomicTemperatureFactors_anisotropic, 
										    patfThis);
					if (!pacThis->temperature_factors) { iBailno = 24; goto memnewbail; } 
					patfThis->scale_factor = SAVE_XYZ_SCALE_FACTOR;
					iZ = ScaleToInt(paldThis->pflvData[4]);
					patfThis->b_11 = ValNodeAddInt(NULL, 0,  iZ);
					if (!patfThis->b_11) { iBailno = 25; goto memnewbail; } 
					pvnB11 = patfThis->b_11;
					iZ =  ScaleToInt(paldThis->pflvData[5]);
					patfThis->b_12 = ValNodeAddInt(NULL, 0,  iZ);
					if (!patfThis->b_12) { iBailno = 26; goto memnewbail; } 
					pvnB12 = patfThis->b_12;
					iZ =  ScaleToInt(paldThis->pflvData[6]);
					patfThis->b_13 = ValNodeAddInt(NULL, 0,  iZ);
					if (!patfThis->b_13) { iBailno = 27; goto memnewbail; } 
					pvnB13 = patfThis->b_13;
					iZ =  ScaleToInt(paldThis->pflvData[7]);
					patfThis->b_22 = ValNodeAddInt(NULL, 0,  iZ);
					if (!patfThis->b_22) { iBailno = 28; goto memnewbail; } 
					pvnB22 = patfThis->b_22;
					iZ =  ScaleToInt(paldThis->pflvData[8]);
					patfThis->b_23 = ValNodeAddInt(NULL, 0,  iZ);
					if (!patfThis->b_23) { iBailno = 29; goto memnewbail; } 
					pvnB23 = patfThis->b_23;
					iZ =  ScaleToInt(paldThis->pflvData[9]);
					patfThis->b_33 = ValNodeAddInt(NULL, 0,  iZ);
					if (!patfThis->b_33) { iBailno = 30; goto memnewbail; } 
					pvnB33 = patfThis->b_33;
				      }   
				    if (pmldThis->pcAltConf) /* is there an alt-conf string for this model ? */
				      {
				 	pacThis->alternate_conf_ids = ValNodeCopyStr(NULL, 0, STRING_UNK_MOL);
					if (!pacThis->alternate_conf_ids) { iBailno = 31; goto memnewbail; } 
					pcTemp = (CharPtr) pacThis->alternate_conf_ids->data.ptrvalue;
					pcTemp[0] = paldThis->cAltConf;
				        pvnAlt = pacThis->alternate_conf_ids;
				      }
				  }  /* first-case */
				else
				  {
				    /* otherwise the parent pointers are already initialized */
				    /* Allocate new nodes; x,y,z,o,m_ids,r_ids,a_ids,temp(s) */
				    
				    pvnMid->next = ValNodeAddInt(   NULL, 
								    0, 
								    IndexFromNode((PFB)GetParentMol((PFB)pmadThis)));
				    if (!pvnMid->next) { iBailno = 32; goto memnewbail; } 
				    pvnMid = pvnMid->next;
				    pvnRid->next = ValNodeAddInt(   NULL, 
								    0, 
								    IndexFromNode((PFB)GetParentGraph((PFB)pmadThis)));
				    if (!pvnRid->next) { iBailno = 33; goto memnewbail; } 
				    pvnRid = pvnRid->next;
				    pvnAid->next = ValNodeAddInt(   NULL, 
								    0, 
								    IndexFromNode((PFB)pmadThis));
				    if (!pvnAid->next) { iBailno = 34; goto memnewbail; } 
				    pvnAid = pvnAid->next;
				    pvnX->next = ValNodeAddInt(NULL, 0, iX);
				    if (!pvnX->next){ iBailno = 35; goto memnewbail; } 
				    pvnX = pvnX->next;
				    pvnY->next = ValNodeAddInt(NULL, 0, iY);
				    if (!pvnY->next) { iBailno = 36; goto memnewbail; } 
				    pvnY = pvnY->next;
				    pvnZ->next = ValNodeAddInt(NULL, 0, iZ); 
				    if (!pvnZ->next) { iBailno = 37; goto memnewbail; } 
				    pvnZ = pvnZ->next;
				    if (paldThis->iFloatNo == 4)
				      {
					iZ =  ScaleToInt(paldThis->pflvData[4]);
					pvnB11->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB11->next) { iBailno = 38; goto memnewbail; } 
					pvnB11 = pvnB11->next;
				      }	
				    if (paldThis->iFloatNo == 9)
				      {
					iZ =  ScaleToInt(paldThis->pflvData[4]);
					pvnB11->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB11->next) { iBailno = 39; goto memnewbail; } 
					pvnB11 = pvnB11->next;
					iZ =  ScaleToInt(paldThis->pflvData[5]);
					pvnB12->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB12->next) { iBailno = 40; goto memnewbail; } 
					pvnB12 = pvnB12->next;
					iZ =  ScaleToInt(paldThis->pflvData[6]);
					pvnB13->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB13->next) { iBailno = 41; goto memnewbail; } 
					pvnB13 = pvnB13->next;
					iZ =  ScaleToInt(paldThis->pflvData[7]);
					pvnB22->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB22->next) { iBailno = 42; goto memnewbail; } 
					pvnB22 = pvnB22->next;
					iZ =  ScaleToInt(paldThis->pflvData[8]);
					pvnB23->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB23->next) { iBailno = 43; goto memnewbail; } 
					pvnB23 = pvnB23->next;
					iZ =  ScaleToInt(paldThis->pflvData[9]);
					pvnB33->next = ValNodeAddInt(NULL, 0,  iZ);
					if (!pvnB33->next) { iBailno = 44; goto memnewbail; } 
					pvnB33 = pvnB33->next;
				      }
				    if (pvnOcc)
				      {  
					iZ =  ScaleToInt(paldThis->pflvData[3]);
					pvnOcc->next =   ValNodeAddInt(NULL, 0, iZ);
					if (!pvnOcc->next) { iBailno = 45; goto memnewbail; } 
					pvnOcc = pvnOcc->next;
				      }
				    if (pvnAlt)
				      {
					pvnAlt->next = ValNodeCopyStr(NULL, 0, STRING_UNK_MOL);
					if (!pvnAlt->next) { iBailno = 46; goto memnewbail; } 
					pcTemp = (CharPtr) pvnAlt->next->data.ptrvalue;
					pcTemp[0] = paldThis->cAltConf;
				        pvnAlt = pvnAlt->next;
				      }
 				  }  /* continuation of linked-lists */
				  
				  bFirst = FALSE; 
	if (bGotoCalled) goto aldsaveblockreturn;  /* ends the for loop block goto */
				  paldThis = paldThis->next;  /* traverse alt-confs */
			      }  /* while paldThis */
			    pvnmaThis = pvnmaThis->next;
			}   /*atom*/
		    pdnmgThis = pdnmgThis->next;
		}/*graph*/
		pdnmmThis = pdnmmThis->next;
	    }   /*molecule*/

    /* copy COUNTER into appropriate places */
 	    
	    
	pacThis->number_of_points = iCountAtoms;
	pacThis->atoms->number_of_ptrs = iCountAtoms;
   
    skipnext:	 
       pmldThis = pmldThis->next;  /* next coordinate set in same model */
     
     } /* while pmldThis */

return TRUE;
    
    
    errorbail:
#ifdef _DEBUG_5
printf("errorbail out %d - ASN.1 stub mismatch \n ",iBailno );
#endif    
      /* Unrecoverable stub mismach or Modelstruc indexing errors */
      /* fatal */
 	ErrClear(); 
	ErrPostEx(SEV_FATAL,0,0, "Save operation found corrupt data structures\n Error at %d.",iBailno);
	ErrShow();
	  
      return FALSE;
      
    memnewbail:
#ifdef _DEBUG_5
printf("memnewbail out %d - out of memory error \n ", iBailno );
#endif 
      /* Recoverable out of memory error */
      /* clear all ASN data out - must start save process from scratch */
  
  
    FreeRedundantAsn(pdnmsThis);
    ErrClear(); 
    ErrPostEx(SEV_ERROR,0,0, "Out of memory (at %d) while trying to save.\n Try again after closing other windows/programs.",iBailno);
    ErrShow();
    return FALSE;

}


	


Boolean LIBCALL RebuildChemGraphAsn(PDNMS pdnmsThis)
{
   /* looks for kept chemical graps - if not ASN.1 minimum
    * or if it was not kept,  it reconstructs the chemical graph.
    * Upon TRUE,  pdnmsThis' Chemical Graph ASN.1 is repopulated.
    * FASE means it cannot be repopulated.
    */

    Boolean			bOK = TRUE;
    Boolean			bFirstMol = TRUE;
    Boolean			bFirstRes = TRUE;
    Boolean     		bFirstIRB = TRUE;
    Int1    			iDictChoice;
    PMSD 			pmsdThis = NULL;
    PDNMM 			pdnmmThis = NULL;
    PMMD 			pmmdThis = NULL;
    MoleculeGraphPtr 		pmgNew = NULL;
    MoleculeGraphPtr 		pmgLast = NULL;
    ResiduePtr  		prNew = NULL;
    ResiduePtr  		prLast = NULL;
    PDNMG 			pdnmgThis = NULL;
    PMGD  			pmgdThis = NULL;
    BiostrucResidueGraphSetPntrPtr 	pbrgsNew = NULL;
    DbtagPtr 			pdbtNew = NULL;
    PVNMB 			pvnmbThis = NULL;
    PMBD 			pmbdThis = NULL;
    InterResidueBondPtr 	pirbNew = NULL;
    InterResidueBondPtr 	pirbLast = NULL;
	   
    
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
    if (!(pmsdThis->bMe == (Byte) AM_MSD)) return FALSE;
    
    /* affix a new chemical graph node to the Biostruc */ 
    pmsdThis->pbsBS->chemical_graph = BiostrucGraphNew();
   
    if (!pmsdThis->pbsBS->chemical_graph) goto allocerr;
    /* re-attach the main descr (ASN optional) */
    pmsdThis->pbsBS->chemical_graph->descr = pmsdThis->pGraphDescr;

    /* re-attach the local dictionary (ASN optional) */
    pmsdThis->pbsBS->chemical_graph->residue_graphs = pmsdThis->pDictLocal;

    /* rebuild molecule_graphs */
    /* here the seq-id's and descr's have been preserved in MMD's */
    pdnmmThis = pmsdThis->pdnmmHead;
    bFirstMol = TRUE;
    while(pdnmmThis)  /* walk the molecule list */
      {
	pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
	pmgNew = MoleculeGraphNew();
	if (!pmgNew) goto allocerr;
	if (bFirstMol) 
	  { 
	    pmsdThis->pbsBS->chemical_graph->molecule_graphs = pmgNew;
	    pmgLast = pmgNew;
	    bFirstMol = FALSE;
	  }
	else
	  {
	    pmgLast->next = pmgNew;
	    pmgLast = pmgNew;
	  }
	   
        /* Fill pmgNew's id, descr, seq_id */ 
	
	pmgNew->id = pdnmmThis->choice;
	pmgNew->descr = pmmdThis->pMolDescr;
	pmgNew->seq_id = pmmdThis->pSeqId;
	
	
	/* residue_sequence */
	
	pdnmgThis = pmmdThis->pdnmgHead; /* top of sequence */
	bFirstRes = TRUE;
	while (pdnmgThis)  /* walk the residues */
	  {
	    pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
	    prNew = ResidueNew();
	    if (!prNew) goto allocerr;
	    if  (bFirstRes)
	      {
		  pmgNew->residue_sequence = prNew;
		  prLast = prNew;
		  bFirstRes = FALSE;
	      }
	    else
	      {
		  prLast->next = prNew;
		  prLast = prNew;
	      }
	    /* Fill in prNew's id, name, residue    */
	        
	    prNew->id = pdnmgThis->choice;
	    prNew->name = StringSave(pmgdThis->pcGraphNum);  /* this gets copied */
	    
	    if (pmgdThis->bWhat & (Byte) DICT_GLOBAL) 
	      {
	         iDictChoice = ResidueGraphPntr_standard;
		 pbrgsNew = BiostrucResidueGraphSetPntrNew();
		 if (!pbrgsNew) goto allocerr;
		 pdbtNew = DbtagNew();
		 if (!pdbtNew) goto allocerr;
		 pdbtNew->db = StringSave(DICT_STANDARD_STRING);
		 pdbtNew->tag = ObjectIdNew();
		 if (!pdbtNew->tag) goto allocerr;
		 pdbtNew->tag->id = DICT_STANDARD_ID;
		 pbrgsNew->biostruc_residue_graph_set_id =  
		 ValNodeAddPointer(NULL, BiostrucId_other_database, pdbtNew);
		 pbrgsNew->residue_graph_id = pmgdThis->iIDict;
		 prNew->residue_graph = ValNodeAddPointer(NULL, iDictChoice, pbrgsNew);
	      }
	    else  
	    if (pmgdThis->bWhat & (Byte) DICT_LOCAL) 
	      {
	         iDictChoice = ResidueGraphPntr_local;
		 prNew->residue_graph = ValNodeAddInt(NULL, iDictChoice, pmgdThis->iIDict);
		 if (!prNew->residue_graph) goto allocerr;
	      }
	    else  
	   /* if (pmgdThis->bWhat & (Byte) DICT_REF)  */
	      {
	      
	            ErrClear(); 
		    ErrPostEx(SEV_FATAL,0,0, "Cannot handle Dictionary \nCannot Reference to Another Biostruc");
		    ErrShow();
		    return FALSE;
	      }
	      
	    pdnmgThis = pdnmgThis->next;
	  }
		
	 /* inter_residue_bonds */
       
        
	pvnmbThis = pmmdThis->pvnmbIRBHead;
	bFirstIRB = TRUE;
	while(pvnmbThis)
	  {
	    pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
	    pirbNew = InterResidueBondNew();
	    if (!pirbNew) goto allocerr;
	    if  (bFirstIRB)
	      {
		  pmgNew->inter_residue_bonds = pirbNew;
		  pirbLast = pirbNew;
		  bFirstIRB = FALSE;
	      }
	    else
	      {
		  pirbLast->next = pirbNew;
		  pirbLast = pirbNew;
	      }
	    /* fill in pirbNew's atom_id_1, atom_id_2, bond_order */
	    pirbNew->atom_id_1 = AtomPntrNew();
	    if (! pirbNew->atom_id_1) goto allocerr;
	    pirbNew->atom_id_2 = AtomPntrNew();
	    if (! pirbNew->atom_id_2) goto allocerr;
	    
	    pirbNew->atom_id_1->molecule_id = IndexFromNode((PFB)pmmdThis);
	    pirbNew->atom_id_1->residue_id = IndexFromNode((PFB)GetParentGraph((PFB)pmbdThis->pmadFrom));
	    pirbNew->atom_id_1->atom_id = IndexFromNode((PFB)pmbdThis->pmadFrom);
	    
	    pirbNew->atom_id_2->molecule_id = IndexFromNode((PFB)pmmdThis);
	    pirbNew->atom_id_2->residue_id = IndexFromNode((PFB)GetParentGraph((PFB)pmbdThis->pmadTo));
	    pirbNew->atom_id_2->atom_id = IndexFromNode((PFB)pmbdThis->pmadTo);
	    
	    pirbNew->bond_order = pvnmbThis->choice;  /* bond order held here */
	    
	    pvnmbThis = pvnmbThis->next;
	  }  /* inter-residue bonds */	 	
	
	pdnmmThis = pdnmmThis->next;
      }  /* molecule */
         
   /* rebuild inter_molecule_bonds */ 	
	
   pvnmbThis = pmsdThis->pvnmbIMBHead;
   bFirstIRB = TRUE;
   while(pvnmbThis)
      {
        pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
        pirbNew = InterResidueBondNew();
        if (!pirbNew) goto allocerr;
        if  (bFirstIRB)
          {
    	    pmsdThis->pbsBS->chemical_graph->inter_molecule_bonds = pirbNew;
    	    pirbLast = pirbNew;
    	    bFirstIRB = FALSE;
          }
        else
          {
    	    pirbLast->next = pirbNew;
    	    pirbLast = pirbNew;
          }
        /* fill in pirbNew's atom_id_1, atom_id_2, bond_order */
        pirbNew->atom_id_1 = AtomPntrNew();
        if (! pirbNew->atom_id_1) goto allocerr;
        pirbNew->atom_id_2 = AtomPntrNew();
        if (! pirbNew->atom_id_2) goto allocerr;
        
        pirbNew->atom_id_1->molecule_id = IndexFromNode((PFB)GetParentMol((PFB)pmbdThis->pmadFrom));
        pirbNew->atom_id_1->residue_id = IndexFromNode((PFB)GetParentGraph((PFB)pmbdThis->pmadFrom));
        pirbNew->atom_id_1->atom_id = IndexFromNode((PFB)pmbdThis->pmadFrom);
        
        pirbNew->atom_id_2->molecule_id = IndexFromNode((PFB)GetParentMol((PFB)pmbdThis->pmadTo));
        pirbNew->atom_id_2->residue_id = IndexFromNode((PFB)GetParentGraph((PFB)pmbdThis->pmadTo));
        pirbNew->atom_id_2->atom_id = IndexFromNode((PFB)pmbdThis->pmadTo);
        
        pirbNew->bond_order = pvnmbThis->choice;  /* bond order held here */
        
        pvnmbThis = pvnmbThis->next;
    }  /* inter-molecule bonds */	 
  
   
   if (pmsdThis->pbsBS->chemical_graph) return TRUE;

  allocerr: 
    /* Recoverable out of memory error */
      /* clear all ASN data out - must start save process from scratch */
    FreeRedundantAsn(pdnmsThis);
    ErrClear(); 
    ErrPostEx(SEV_ERROR,0,0, "Out of memory while trying to save.\n Try again after closing other windows/programs.");
    ErrShow();
    return FALSE;
  
}




Boolean LIBCALL WriteOutBiostruc(BiostrucPtr pbsThis, CharPtr pcSave,  Byte bSave)
{   

    AsnIoPtr paioAIP = NULL;
    Boolean bWriteOK = FALSE;
    CharPtr pcSaveMode = NULL;
    BiostrucFeatureSetPtr pfsThis = NULL; 

    if (!pcSave) return FALSE;
    if (!pbsThis) return FALSE;    
    if (bSave & (Byte) SAVE_BINARY) pcSaveMode = SAVE_BINARY_STRING; 
    else pcSaveMode = SAVE_ASCII_STRING;  
  
    if (bSave & (Byte) NOT_FEATURES)
      {
        pfsThis = pbsThis->features;
	pbsThis->features = NULL;
      } /* unlink features */
   
    paioAIP = AsnIoOpen(pcSave, pcSaveMode);  
    bWriteOK = BiostrucAsnWrite(pbsThis, paioAIP, NULL);
    AsnIoClose (paioAIP); 
   
    if (bSave & (Byte) NOT_FEATURES)
     {
       pbsThis->features = pfsThis;
     } /* re-link features */
      	
    return bWriteOK;
}


Boolean LIBCALL WriteASNChemGraphOnly(PDNMS pdnmsThis, CharPtr pcSave,  Byte bSave)
{
    /* writes out chemical graph only - models are not destroyed */
    AsnIoPtr paioAIP = NULL;
    Boolean bWriteOK = FALSE;
    CharPtr pcSaveMode = NULL;
    Int4 iHash = 0;
    BiostrucFeatureSetPtr pfsThis = NULL; 
    BiostrucModelPtr pbsmThis = NULL;
    PMSD pmsdThis = NULL;
    
    if (!pdnmsThis) return FALSE;
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
      
   if (RebuildChemGraphAsn(pdnmsThis) == FALSE)
     {
	bWriteOK = FALSE;
	goto seeyalater;
     }
#ifdef _DEBUG_5
printf("Chem Graph rebuilt \n");
#endif  
    
    if (bSave & (Byte) SAVE_BINARY) pcSaveMode = SAVE_BINARY_STRING; 
    else pcSaveMode = SAVE_ASCII_STRING;  
      
    if (bSave & (Byte) NOT_FEATURES)
      {
        pfsThis = pmsdThis->pbsBS->features;
	pmsdThis->pbsBS->features = NULL;
      } /* unlink features */
   
   
    pbsmThis = pmsdThis->pbsBS->model;
    pmsdThis->pbsBS->model = NULL;  
    /* unlink models */
   
    paioAIP = AsnIoOpen(pcSave, pcSaveMode);  
    bWriteOK = BiostrucAsnWrite(pmsdThis->pbsBS, paioAIP, NULL);
    AsnIoClose (paioAIP); 
   
    if (bSave & (Byte) NOT_FEATURES)
     {
       pmsdThis->pbsBS->features = pfsThis;
     } /* re-link features */
   
   
    pmsdThis->pbsBS->model = pbsmThis;
    /* relink models */
  seeyalater:    
    FreeRedundantAsn(pdnmsThis);
    /* remove the chem graph */
   
    return bWriteOK;
    
}




Boolean LIBCALL WriteAsnLocalDict(PDNMS pdnmsThis, CharPtr pcSave,  Byte bSave,   Boolean SaveId)
{
    CharPtr pcSaveMode = NULL;
    BiostrucResidueGraphSetPtr pbrgsDict = NULL;
    PMSD pmsdThis = NULL;
    AsnIoPtr paioAIP = NULL;
    Boolean bRet;
    
    if (bSave & (Byte) SAVE_BINARY) pcSaveMode = SAVE_BINARY_STRING; 
    else pcSaveMode = SAVE_ASCII_STRING;  
            
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
    if (pmsdThis->bMe == (Byte) AM_MSD)
	if (pmsdThis->pDictLocal)
	    {
	      pbrgsDict =  BiostrucResidueGraphSetNew();
	      if (!pbrgsDict) return FALSE;
	      pbrgsDict->residue_graphs = pmsdThis->pDictLocal; /* attach dict */
	      if (SaveId) pbrgsDict->id = pmsdThis->pbsBS->id;  /* point at id */
	      paioAIP = AsnIoOpen(pcSave, pcSaveMode); 
	      bRet = BiostrucResidueGraphSetAsnWrite(pbrgsDict, paioAIP, NULL);
	      AsnIoClose(paioAIP);
	      pbrgsDict->residue_graphs = NULL;  /* detach dict */
	      pbrgsDict->id = NULL; /* detach id */
	      BiostrucResidueGraphSetFree(pbrgsDict);
	      return bRet;
	    }
    return FALSE;	
}


static void LIBCALLBACK FreeModelChemGraphLoc(PFB pfbThis, Int4 iModel, Int4 iIndex, Pointer ptr)
{   /* callback for FreeSingleModel - frees the model-specific data from a chem-graph node */
 
   PMAD pmadThis = NULL;
   PALD paldThis = NULL;
   PMGD pmgdThis = NULL;
   PMMD pmmdThis = NULL;
   PVNAL pvnalThis = NULL;
   ValNodePtr *ppvn  = NULL;
   ValNodePtr pvnKillList = NULL;

   
   if (pfbThis->bMe == (Byte) AM_MAD)
     {  /* iIndex & ptr not used */
	pmadThis = (PMAD) pfbThis;
	/* look up the cointainedBy list for model ocurrences */
	paldThis = GetAtomLocs(pmadThis, iModel); 
	if (paldThis) 
 	  {
	    pvnalThis = paldThis->pvnalLink;
 	    FreeALD(paldThis);
	    pvnalThis->data.ptrvalue = NULL;
	    pvnalThis = ValNodeExtract(&pmadThis->pvnalLocate, iModel); 
	    if (pvnalThis) ValNodeFree(pvnalThis);
	  }
	ppvn = &pmadThis->pvnContainedBy;
     }  
     
   if (pfbThis->bMe == (Byte) AM_MGD)
        
     {
	 /* kill the pvnContainedBy entries for this model in choice field */
	 pmgdThis = (PMGD) pfbThis;
	 ppvn = &pmgdThis->pvnContainedBy;
     }
 
   if (pfbThis->bMe == (Byte) AM_MMD)
     {
	 /* kill the pvnContainedBy entries for this model in choice field */
	 pmmdThis = (PMMD) pfbThis;
	 ppvn = &pmmdThis->pvnContainedBy;
     }
   if (!ppvn) return;  
   pvnKillList = ValNodeExtractList(ppvn, iModel);
   if (pvnKillList) ValNodeFree(pvnKillList);  
  
}

Boolean LIBCALL FreeSingleModel(PDNMS pdnmsThis, Int2 iModel)
{
    PMLD pmldThis = NULL;
    PMLD pmldTemp = NULL;
    PMSD pmsdThis = NULL;
    PDNML pdnmlThis = NULL;
    PDNML pdnmlTemp = NULL;
    BiostrucModelPtr pbsmTemp = NULL;
    BiostrucModelPtr pbsmLast = NULL;
    BiostrucModelPtr pbsmHead = NULL;
    ValNodePtr pvn = NULL;
    PDNML pdnml = NULL;
    
    if (!pdnmsThis) return FALSE;
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
    
  
   pdnmlTemp = pmsdThis->pdnmlModels;
   while(pdnmlTemp)
     {
        if (pdnmlTemp->choice == iModel) 
	    break;
	pdnmlTemp = pdnmlTemp->next;
     }
     
   if (!pdnmlTemp) 
     {
#ifdef _DEBUG_5
printf("Cannot find iModel %d to delete it\n",(int) iModel);
#endif  
       return FALSE;
     }
   
   pdnmlThis = pdnmlTemp;
   pmldThis = (PMLD) pdnmlThis->data.ptrvalue;
    
	/* call a traverser callback which removes the model's pvnal lists */
	/* and pvnContainedBy instances from MMD, MGD and MAD. */
#ifdef _DEBUG_5
printf("Calling Traverser iModel %d\n",(int) iModel);
#endif  

    TraverseOneModel(pdnmsThis, TRAVERSE_ALL, 
			iModel, 0,   NULL, (pNodeFunc)FreeModelChemGraphLoc);

       /* unlink all the models' ASN.1 portions */
#ifdef _DEBUG_5
printf("Traverser Done\n");
#endif  
       
    pdnmlTemp =  pmsdThis->pdnmlModels;
    while (pdnmlTemp)  
      { 
	 pmldTemp = (PMLD) pdnmlTemp->data.ptrvalue;
	 if (pmldTemp->pbsmModel)
	    pmldTemp->pbsmModel->next = NULL;  /* unlinked */	   
	 else
	   {
	    ErrClear(); 
	    ErrPostEx(SEV_ERROR,0,0, "Model Lacks ASN.1 Stub\n pmldTemp->pbsmModel");
	    ErrShow();
	    return FALSE;
	   }
	 pdnmlTemp = pdnmlTemp->next;
      }	
      
	/* free the Biostruc model stub off first node's pdnmlThis>>pmldThis->pbsmModel */
	/* these are now all detached from each other and can be deleted */
	/*   without causing a chain reaction */
#ifdef _DEBUG_5
printf("Freeing Biostruc Models\n");
#endif  

    if (pmldThis->pbsmModel) BiostrucModelFree(pmldThis->pbsmModel);
    pmldThis->pbsmModel = NULL;		
   
	 /* extract the MOD's and MDD's from the lists off of pmsdThis and free them */
    pvn = ValNodeExtractList(&pmsdThis->pvnmoHead, iModel);
    if (pvn) FreeListVNMO((PVNMO) pvn);
    pmsdThis->iObjCount = 0;
    pvn = pmsdThis->pvnmoHead;
    while (pvn)
      {
	  (pmsdThis->iObjCount)++;
	  pvn = pvn->next;
      }
    
    pvn = NULL;
    pvn = ValNodeExtractList(&pmsdThis->pvnmdHead, iModel);
    if (pvn) FreeListVNMD((PVNMD) pvn);
    pmsdThis->iDensCount = 0;
    pvn = pmsdThis->pvnmdHead;
    while (pvn)
      {
	  (pmsdThis->iDensCount)++;
	  pvn = pvn->next;
      }
 
   /* free the chain of mld's off of pdnmlThis */
  
  
	/* unhook the structure model node to delete */
#ifdef _DEBUG_5 
        pdnml  = pmsdThis->pdnmlModels;
 	 while (pdnml )
	  {
 	    printf("Before found model %d\n", (int) pdnml->choice);
	    pdnml = pdnml->next;
	  }
#endif	    

    pdnmlThis = DValNodeExtract(&pmsdThis->pdnmlModels, iModel);

#ifdef _DEBUG_5 
        pdnml  = pmsdThis->pdnmlModels;
 	 while (pdnml )
	  {
 	    printf(" After Unlink - found model %d\n", (int) pdnml->choice);
	    pdnml = pdnml->next;
	  }
#endif	  
  
#ifdef _DEBUG_5
printf("Freeing MLD's\n");
#endif  

   if (pdnmlThis) FreeListDNML(pdnmlThis);

#ifdef _DEBUG_5
printf("MLDs freed\n");
#endif      

 
#ifdef _DEBUG_5
printf("Re-linking pbsmModels\n");
#endif  

    	/* re-link the remaining list of pbsmModel's... */
	
    pdnmlTemp = pmsdThis->pdnmlModels;
    while (pdnmlTemp)  /* link the model list ASN.1 portions */
      { 
	 pmldThis = (PMLD) pdnmlTemp->data.ptrvalue;
         if (!pbsmHead) pbsmHead = pmldThis->pbsmModel; /* sets the first one */
	 if (pbsmLast) pbsmLast->next = pmldThis->pbsmModel; /* point to this one */
	 
	 pbsmLast = pmldThis->pbsmModel;
	 pdnmlTemp = pdnmlTemp->next;
      }	
   pmsdThis->pbsBS->model = pbsmHead;  /* reset the Biostruc main model pointer to new list */
   pmsdThis->iModels--;
   return TRUE;
}



Boolean LIBCALL WriteAsnModelList(PDNMS pdnmsThis,   Int2 iNumModels,  Int2Ptr i2Vec,  
				    CharPtr pcSave,  Byte bSave, Boolean iCn3d)
{
    /* The master ASN.1 writer routine */
    /* MODELS not specified for writing are ERASED for congruency */
    /* the User-interface needs to notify the user of this if some models are not saved */
    /* takes a vector containing model numbers as input */
    /* one can save any arbitrary collection of models */
    /* or one can save no models - just features, or dictionaries */
    /* use bSave Byte to  pick binary/ascii modes */
   
    Int2 iIndex = 0;
    Int2 iTest = 0;
    Boolean bFirst = TRUE;
    BiostrucPtr pBSThis = NULL;
    BiostrucModelPtr pbsmThis = NULL;
    PMSD pmsdThis = NULL;
    PDNML pdnmlThis = NULL;
    ValNodePtr pvnDelList = NULL;
    ValNodePtr pvnDelTemp = NULL;
    PDNML pdnmlTemp = NULL;
    PMLD  pmldThis = NULL;
    Boolean bRet;
    ValNodePtr pvn = NULL;  
    AsnIoPtr paioAIP = NULL;
    Int4 iHash = 0;
    Boolean bFound;
   
 
 /*FIRST-PASS VALIDATION*/
    
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
    if (!(pmsdThis->bMe == (Byte) AM_MSD)) return FALSE;
    /* A pbsBS must exist or be created! */
    if (!(pmsdThis->pbsBS))
      {
          ErrClear(); 
	  ErrPostEx(SEV_ERROR,0,0, "Modelstruc Lacks Biostruc ASN.1 Stub\n pmsdThis->pbsBS");
	  ErrShow();
	  return FALSE;
      } 
   
  
    
 /*CHEMICAL-GRAPH*/
    
    if (RebuildChemGraphAsn(pdnmsThis) == FALSE)
      {
	bRet = FALSE;
#ifdef _DEBUG_5
printf("Chem Graph rebuild failed \n");
#endif
        goto outofhere;
      } 
    
#ifdef _DEBUG_5 
printf("Chem Graph rebuilt \n");
#endif    
    


/*MODELS*/   
      
  
    /* walk through models, free the ones not on the list (ALD, MLD, and ASN stubs) */
    pdnmlTemp = pmsdThis->pdnmlModels;
 
    while(pdnmlTemp)
      {
          bFound = FALSE;
	  for (iIndex = 0; iIndex < iNumModels; iIndex++)
	     {
		 if ((Int2)pdnmlTemp->choice == i2Vec[iIndex])
		   {
		     bFound = TRUE;
		   }  
	     }
	  if (!bFound) pvnDelTemp = ValNodeAddPointer(&pvnDelTemp, 0, (VoidPtr) pdnmlTemp); 
	  if (!pvnDelList) pvnDelList = pvnDelTemp;  /* set the head pointer */
	  pdnmlTemp = pdnmlTemp->next;  
      }
    
    pvnDelTemp = pvnDelList;
    while(pvnDelTemp)
     {
        pdnmlTemp = (PDNML) pvnDelTemp->data.ptrvalue;
	if (FreeSingleModel(pdnmsThis, (Int2)pdnmlTemp->choice) == FALSE)
	  {
	     ErrClear(); 
	     ErrPostEx(SEV_FATAL,0,0, "Model Not Correctly Freed. Possibly Corrupt Memory Data\n");
	     ErrShow();
	     goto outofhere;
	  }
	pvnDelTemp = pvnDelTemp->next; 
     }
   
   if (pvnDelList) ValNodeFree(pvnDelList);
   
    /* now pmsdThis->pbsBS->model contains only the models for saving */   
    for (iIndex = 0; iIndex < iNumModels; iIndex++)
       {  
        /* for each model, refresh or rebuild its data */
#ifdef _DEBUG_5
printf("Refreshing Model %d\n ", (int) i2Vec[iIndex]);
#endif
  	
	 if (RefreshModelAsnMem(pdnmsThis, i2Vec[iIndex]) == FALSE)
	   {
 	     bRet = FALSE;
#ifdef _DEBUG_5
printf("Refreshing Model failed %d\n ", (int) i2Vec[iIndex]);
#endif   
  		     goto outofhere;
	   } 
       
       }
      
       
    /* it is up to the bSave parameter to specify whether there is a new ID to  */
    /* be hashed automatically */
 
     

    if (bSave & (Byte) NEW_MMDB_ID) 
      {
        iHash = MakeHashChange(pdnmsThis);
	if (iHash != pmsdThis->iHashChange)
	  { 
	      pmsdThis->iMMDBid = iHash;
	      pvn = pmsdThis->pbsBS->id;
	      pmsdThis->pbsBS->id = NULL;
	      pmsdThis->pbsBS->id = ValNodeAddInt(&pmsdThis->pbsBS->id, BiostrucId_mmdb_id, iHash);
	      if (pvn) ValNodeFree(pvn);
	  }
      }
     
     if(iCn3d){
        if(pmsdThis->pbsBS) bRet = TRUE;
        return bRet;
               /* leave cn3d handle FreeRedundantAsn - yanli */
     }

     bRet = WriteOutBiostruc(pmsdThis->pbsBS, pcSave, bSave);

#ifdef _DEBUG_5
if (!bRet) printf("WriteOutBiostruc Failed\n");
#endif    
  	   
  outofhere:
     FreeRedundantAsn(pdnmsThis);
     
     return bRet;
   
}


Boolean LIBCALL WriteAsnOneModel(PDNMS pdnmsThis,  Int2 iModel,  CharPtr pcSave,  
		Byte bSave)
{
   /* call to save a single model */

    Boolean bRet;
    PMSD pmsdThis = NULL;
    
    pmsdThis = (PMSD)  PFBFromDN(pdnmsThis);
    if (!(pmsdThis->bMe == (Byte) AM_MSD)) return FALSE;
   
    bRet = WriteAsnModelList(pdnmsThis,  1, (Int2Ptr) &iModel, 
				pcSave, bSave, 0);
    return bRet;
}

Boolean LIBCALL WriteAsnAllModel(PDNMS pdnmsThis,  CharPtr pcSave,  Byte bSave)
{

    Int2Ptr i2Vec = NULL;
    PDNML pdnmlThis = NULL;
    PMSD pmsdThis = NULL;
    PMLD pmldThis = NULL;
    Int2 iCount = 0;
    Boolean bRet = FALSE;
 
    pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
    if (!(pmsdThis->bMe == (Byte) AM_MSD)) return FALSE;
    
    pdnmlThis = pmsdThis->pdnmlModels; /* the linked-list of models */
    if (pdnmlThis)
      while (pdnmlThis)
       {
	 pmldThis = (PMLD) pdnmlThis->data.ptrvalue;
	 if (pmldThis)
	   {
	        iCount++;
	   } 
         pdnmlThis = pdnmlThis->next;
       }
    else return bRet;  /* no models */
    i2Vec = I2Vector(0, iCount);  /* allocate vector */
    if (!i2Vec) return bRet; 
    iCount = 0;
    pdnmlThis = pmsdThis->pdnmlModels;
    while (pdnmlThis)
	{  /* copy the model id's into the vector */
 	  pmldThis = (PMLD) pdnmlThis->data.ptrvalue;
	  if (pmldThis)
	   {
	     i2Vec[iCount] = pdnmlThis->choice;
	     iCount++;
	   }
	  pdnmlThis = pdnmlThis->next;
	}
    bRet = WriteAsnModelList(pdnmsThis, iCount, i2Vec, pcSave, bSave, 0);
    if (i2Vec) I2VectorFree(i2Vec, 0);  /* free vector */
    return bRet;	  
}




SeqIdPtr MakePDBSeqId2 (CharPtr pcPDB, Char cChain, int iDomain, Boolean getgi)
{
  SeqIdPtr        sip;
  PDBSeqIdPtr     psip;
  Int4 gi;

  psip = PDBSeqIdNew ();

  psip->chain = cChain;
  psip->rel = NULL;
  psip->mol = StringSave (pcPDB);
  sip = ValNodeNew (NULL);
  sip->choice = SEQID_PDB;
  sip->data.ptrvalue = psip;

  if (getgi)
  {
    if ((gi = GetGIForSeqId (sip)) > 0) {
      SeqIdFree (sip);
      sip = ValNodeNew (NULL);
      sip->choice = SEQID_GI;
      sip->data.intvalue = gi;
    }
  }

  return sip;
}

/* produce a sequence alignment from a structure-alignment */
SeqAnnotPtr LIBCALL BiostrucAnnotSetToSeqAnnot (BiostrucAnnotSetPtr set, Boolean usePValue)
{
  SeqAnnotPtr     sap = NULL;
  SeqAlignPtr     salp;
  ScorePtr        scp;
  Int2 count;
  ObjectIdPtr     oid;
  DenseSegPtr     dsp;
  SeqIdPtr        mastersip = NULL, slavesip;
  SeqIdPtr        pdbsip = NULL;
  BiostrucFeaturePtr feature;
  ChemGraphAlignmentPtr pcga1 = NULL;
  ValNodePtr      loc1 = NULL;
  ValNodePtr      vnp = NULL;
  SeqAlignPtr     tail = NULL;
  ChemGraphPntrsPtr cgpp;
  ResidueIntervalPntrPtr master;
  ResidueIntervalPntrPtr slave;
  ResidueIntervalPntrPtr masterseg;
  ResidueIntervalPntrPtr slaveseg;
  CharPtr         pcPDB;
  int             iDomain;
  ValNodePtr      pvn;
  Char            cChain;
  Char            buf[100];
  Char            buf2[50];
  Int4 nextmasterstart, nextslavestart;


  if (set == NULL || set->features == NULL || set->features->features == NULL)
    return NULL;

  for (feature = set->features->features; feature != NULL; feature = feature->next) {

    /* construct the master SeqId if it hasn't already been constructed */
    if (mastersip == NULL) {
      pcPDB = StringSave (PDBNAME_DEFAULT);
      iDomain = 0;
      cChain = '-';
      pcPDB[0] = feature->name[0];
      pcPDB[1] = feature->name[1];
      pcPDB[2] = feature->name[2];
      pcPDB[3] = feature->name[3];
      cChain = feature->name[4];
      iDomain = atoi ((char *) &feature->name[5]);
      mastersip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);
      pdbsip = MakePDBSeqId2 (pcPDB, cChain, iDomain, FALSE);
      MemFree (pcPDB);
    }
    /* get the embedded PDB code of the hit */
    pcPDB = StringSave (PDBNAME_DEFAULT);
    iDomain = 0;
    cChain = '-';

    if (StringLen (feature->name) >= 13) {
      pcPDB[0] = feature->name[7];
      pcPDB[1] = feature->name[8];
      pcPDB[2] = feature->name[9];
      pcPDB[3] = feature->name[10];
      cChain = feature->name[11];
      iDomain = atoi ((char *) &feature->name[12]);
      slavesip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);
      MemFree (pcPDB);
    }
    pvn = ValNodeFindNext (feature->Location_location, NULL, Location_location_alignment);
    if (pvn)
      pcga1 = (ChemGraphAlignmentPtr) pvn->data.ptrvalue;

    vnp = pcga1->alignment;

    if (vnp->choice != ChemGraphPntrs_residues)
      continue;

    cgpp = (ChemGraphPntrsPtr) vnp->data.ptrvalue;

    if (cgpp->choice != ResiduePntrs_interval)
      continue;
    master = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue;
    cgpp = (ChemGraphPntrsPtr) vnp->next->data.ptrvalue;
    slave = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue;
    
    for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next, slaveseg = slaveseg->next) {
      if (count > 0 && nextmasterstart < masterseg->from)
	  count++;
      if (count > 0 && nextslavestart < slaveseg->from)
	  count++;
      count++;
      nextmasterstart = masterseg->to + 1;
      nextslavestart = slaveseg->to + 1;
    }

    salp = SeqAlignNew ();
    if (sap == NULL) {
      sap = SeqAnnotNew ();
      sap->type = 2;		/* align */
      sap->data = salp;
      sap->desc = ValNodeNew(NULL);
      sap->desc->choice = 2; /* title */
      SeqIdWrite (pdbsip, buf2, PRINTID_FASTA_LONG, sizeof(buf2));
      sprintf (buf, "VAST structural alignment for %s, mapped to sequence alignment", buf2);
      sap->desc->data.ptrvalue = StringSave(buf);
    } else {
      tail->next = salp;
    }
    tail = salp;
    salp->type = 3 /* partial */ ;
    salp->dim = 2;

    scp = ScoreNew ();
    salp->score = scp;
    salp->segtype = 2 /* denseg */ ;
    scp->choice = 2;		/* real */
    scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_score) / pcga1->aligndata->scale_factor;
    oid = ObjectIdNew ();
    scp->id = oid;
    oid->str = StringSave ("VAST score");
    scp->next = ScoreNew ();
    scp = scp->next;
    scp->choice = 2;		/* real */
    scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_mlogp) / pcga1->aligndata->scale_factor;
    oid = ObjectIdNew ();
    scp->id = oid;
    oid->str = StringSave ("VAST p-value");
    scp->next = ScoreNew ();
    scp = scp->next;
    scp->choice = 2;		/* real */
    scp->value.realvalue = ((FloatHi) pcga1->aligndata->rmsd) / pcga1->aligndata->scale_factor;
    oid = ObjectIdNew ();
    scp->id = oid;
    oid->str = StringSave ("VAST root mean square deviation, measured in angstroms");
    scp->next = ScoreNew ();
    scp = scp->next;
    scp->choice = 1;		/* int */
    scp->value.intvalue = pcga1->aligndata->align_res;
    oid = ObjectIdNew ();
    scp->id = oid;
    oid->str = StringSave ("VAST aligned residue count");

    dsp = DenseSegNew ();
    salp->segs = (Pointer) dsp;
    dsp->dim = 2;
    dsp->numseg = count;
    dsp->ids = SeqIdDup (mastersip);
    dsp->ids->next = slavesip;
    dsp->starts = (Int4Ptr) MemNew (count * sizeof (Int4) * 2);
    dsp->lens = (Int4Ptr) MemNew (count * sizeof (Int4));

    for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next, slaveseg = slaveseg->next) {
      if (count > 0 && nextmasterstart < masterseg->from)
      { /* add a gap */
        dsp->starts[count * 2] = nextmasterstart - 1;
        dsp->starts[count * 2 + 1] = -1;
        dsp->lens[count] = masterseg->from - nextmasterstart;
	count++;
      }
      if (count > 0 && nextslavestart < slaveseg->from)
      { /* add a gap */
        dsp->starts[count * 2 + 1] = nextslavestart - 1;
        dsp->starts[count * 2] = -1;
        dsp->lens[count] = slaveseg->from - nextslavestart;
	count++;
      }
      dsp->starts[count * 2] = masterseg->from-1;
      dsp->starts[count * 2 + 1] = slaveseg->from-1;
      dsp->lens[count] = masterseg->to - masterseg->from + 1;
      count++;
      nextmasterstart = masterseg->to + 1;
      nextslavestart = slaveseg->to + 1;
    }

  }

  SeqIdFree (mastersip);
  SeqIdFree (pdbsip);
  return sap;
}