NCBI C Toolkit Cross Reference

C/biostruc/mmdbapi1.c


  1 /*   mmdbapi1.c
  2 * ===========================================================================
  3 *
  4 *                            PUBLIC DOMAIN NOTICE
  5 *            National Center for Biotechnology Information (NCBI)
  6 *
  7 *  This software/database is a "United States Government Work" under the
  8 *  terms of the United States Copyright Act.  It was written as part of
  9 *  the author's official duties as a United States Government employee and
 10 *  thus cannot be copyrighted.  This software/database is freely available
 11 *  to the public for use. The National Library of Medicine and the U.S.
 12 *  Government do not place any restriction on its use or reproduction.
 13 *  We would, however, appreciate having the NCBI and the author cited in
 14 *  any work or product based on this material
 15 *
 16 *  Although all reasonable efforts have been taken to ensure the accuracy
 17 *  and reliability of the software and data, the NLM and the U.S.
 18 *  Government do not and cannot warrant the performance or results that
 19 *  may be obtained by using this software or data. The NLM and the U.S.
 20 *  Government disclaim all warranties, express or implied, including
 21 *  warranties of performance, merchantability or fitness for any particular
 22 *  purpose.
 23 *
 24 * ===========================================================================
 25 *
 26 * File Name:  mmdbapi1.c
 27 *
 28 * Author:  Christopher Hogue,  Hitomi Ohkawa
 29 *
 30 * Version Creation Date:   03/14/95
 31 *
 32 * $Revision: 6.47 $
 33 *
 34 * File Description:
 35 *
 36 * Modifications:
 37 * --------------------------------------------------------------------------
 38 * Date     Name        Description of modification
 39 * -------  ----------  -----------------------------------------------------
 40 * 95/08/28 C. Hogue    Major change to new Biostruc ASN.1 v4.0,
 41 *                      Addition of Features, Secondary Structure, Ensembles,
 42 *                      Model nodes and Dictionaries altered...
 43 * 95/08/28 C. Hogue    Put model id into its DValnode choice as it should be.
 44 * 95/08/30 C. Hogue    Minor changes.
 45 *
 46 * $Log: mmdbapi1.c,v $
 47 * Revision 6.47  2005/01/25 15:39:07  thiessen
 48 * tweak LoadDict again
 49 *
 50 * Revision 6.46  2005/01/24 20:42:58  thiessen
 51 * fix memory leak; check current dir for dictionary if ProgramPath returns empty string
 52 *
 53 * Revision 6.45  2004/05/06 19:31:09  chenj
 54 * fixed the bug in fnPBSFtoPSA() to use 2 chars for domain id
 55 *
 56 * Revision 6.44  2003/12/03 02:11:28  kans
 57 * added defines missing from Mac OS 10.3 headers
 58 *
 59 * Revision 6.43  2002/05/10 16:33:18  chenj
 60 * fix bugs in fnPBSFtoPSA
 61 *
 62 * Revision 6.41  2001/01/26 15:06:39  lewisg
 63 * use entrez2 to retrieve structures
 64 *
 65 * Revision 6.40  2000/12/05 22:13:06  ywang
 66 * fix bugs for MakePDBSeqId2
 67 *
 68 * Revision 6.39  2000/07/21 18:56:41  thiessen
 69 * allow dynamic slave->master transformation
 70 *
 71 * Revision 6.38  2000/06/09 14:35:16  lewisg
 72 * fix freeing bugs
 73 *
 74 * Revision 6.37  2000/04/20 23:27:44  lewisg
 75 * misc bug fixes
 76 *
 77 * Revision 6.36  2000/03/13 15:51:25  thiessen
 78 * added storage for PARS to each residue for Cn3D; removed unused fields
 79 *
 80 * Revision 6.35  2000/03/09 17:52:56  thiessen
 81 * changes to MSD, MGD for feature implementation in Cn3D
 82 *
 83 * Revision 6.34  2000/03/08 21:46:12  lewisg
 84 * cn3d saves viewport, misc bugs
 85 *
 86 * Revision 6.33  2000/02/14 19:31:03  lewisg
 87 * fix occupancy bug when temperature factors present
 88 *
 89 * Revision 6.32  1999/11/02 23:06:08  lewisg
 90 * fix cn3d to launch correctly if there is no seqentry associated with bioseq
 91 *
 92 * Revision 6.31  1999/10/05 23:18:17  lewisg
 93 * add ddv and udv to cn3d with memory management
 94 *
 95 * Revision 6.30  1999/06/07 21:23:36  ywang
 96 * add iUserDefinedFeatureOld, FeatureOnOld to MGD
 97 *
 98 * Revision 6.29  1999/05/07 20:48:59  kans
 99 * including new name of prunebsc.h
100 *
101 * Revision 6.28  1999/05/07 14:07:07  zimmerma
102 *  new code to call prunebiostruc (lines starting at 3807)
103 *
104 *  InstBSAnnotSet - added params: Boolean Chain, CharPtr Path and
105 *                 modified to call PruneBiostruc (KA)
106 *
107 *  InstallAlignedSlave -  added params: Boolean Chain, CharPtr Path and
108 *                       modified to call PruneBiostruc (KA)
109 *
110 *  InstallStrucFeature -  added params: Boolean Chain, CharPtr Path and
111 *                       propagate to InstallAlignedSlave (KA)
112 *
113 *  BiostrucAddFeature -  added params: Boolean Chain, CharPtr Path and
114 *                       propagate to InstallStrucFeature (KA)
115 *
116 *  Moved isBiopoly and isHet from mkbioseqB.c - DZ
117 *
118 * Revision 6.27  1999/04/26 20:49:59  lewisg
119 * changed arguments named list to fix visual c++ bug
120 *
121 * Revision 6.26  1999/03/18 21:04:29  lewisg
122 * reverse transform for saving
123 *
124 * Revision 6.25  1999/03/01 20:22:10  ywang
125 * add bTurnedOff flag on MGD
126 *
127 * Revision 6.24  1999/02/25 23:11:32  ywang
128 * keep ASN.1 features intact
129 *
130 * Revision 6.23  1999/02/24 22:58:09  ywang
131 * add iMimeType to MSD node and other minor name change
132 *
133 * Revision 6.22  1999/02/02 22:27:00  ywang
134 * add bJustHighlighted flag to MGD for feature edit
135 *
136  * Revision 6.21  1998/12/16  19:30:12  ywang
137  * add flag for highlight status to MGD
138  *
139  * Revision 6.20  1998/11/06  23:02:01  ywang
140  * add FeatureOn to MGD for feature on/off control
141  *
142  * Revision 6.19  1998/11/04  00:53:39  ywang
143  * add iFeature to MGD for modeling control
144  *
145  * Revision 6.18  1998/10/21  15:43:21  ywang
146  * attach the whole vast alignment data to master structure
147  *
148  * Revision 6.17  1998/10/01  21:56:33  ywang
149  * set display flag for object
150  *
151  * Revision 6.16  1998/09/22  17:52:20  ywang
152  * add flag for display control on MM and MG level
153  *
154  * Revision 6.15  1998/08/26  18:02:39  kans
155  * fixed -v -fd warnings
156  *
157 * Revision 6.14  1998/08/14 20:24:05  lewisg
158 * fix fault when retrieving structure by PDB name and database is unavailable
159 *
160 * Revision 6.13  1998/07/01 22:22:08  ywang
161 * set getgi flag as FALSE in function fnPBSFtoPSA when mastersip and slavesip is made by MakePDBSeqId2
162 *
163  * Revision 6.12  1998/05/12  21:46:16  lewisg
164  * stricter conservation coloring
165  *
166 * Revision 6.11  1998/05/08 21:19:30  ywang
167 * make salp->segs as DenseDiag
168 *
169 * Revision 6.9  1998/04/28 22:51:13  lewisg
170 * created separate pointer to master in MGD
171 *
172 * Revision 6.8  1998/04/21 22:58:46  lewisg
173 * eliminate dead code
174 *
175 * Revision 6.7  1998/04/10 22:41:42  lewisg
176 * eliminated dead code and added a visible bit to MSD
177 *
178 * Revision 6.6  1998/04/04 06:01:24  lewisg
179 * got rid of dos line breaks
180 *
181 * Revision 6.5  1998/04/04 00:56:46  lewisg
182 * corrected freeing of MSD
183 *
184 * Revision 6.4  1998/03/10 04:30:59  lewisg
185 * change name of MakePDBSeqId to MakePDBSeqId2 to avoid name conflict
186 *
187 * Revision 6.3  1998/03/06 23:17:15  lewisg
188 * codewarrior fixes
189 *
190 * Revision 6.2  1998/03/06 01:15:43  lewisg
191 * merge
192 *
193 * Revision 6.0  1997/08/25 18:10:56  madden
194 * Revision changed to 6.0
195 *
196 * Revision 5.3  1997/04/18 19:18:56  hogue
197 * Int2 counters for atoms upgraded to Int4's, made ppAsnOrder alloc optional.
198 *
199  * Revision 5.2  1997/01/15  18:15:04  hogue
200  * Changed to abstraction of Entrez calls requiring mmdbentr or equivalent.  This abstracts the Entrez (CD/Network) calls from MMDB-API,
201  * in preparation to fetching MMDB records in alternate ways
202  * (local filesystem or http GET with MIME-typed Biostrucs) and for
203  * providing a slot for an MMDB-API caching system.
204  *
205  * Revision 5.1  1996/11/18  19:58:51  hogue
206  * eliminate ALLSIMPLEMDL to obtain performance improvement, Update
207  * of MMDB data makes ALLSIMPLEMDL obsolete as vectors now stored in
208  * the ONECOORDATOM model.
209  *
210  * Revision 5.0  1996/05/28  14:02:09  ostell
211  * Set to revision 5.0
212  *
213  * Revision 1.25  1996/05/15  17:01:53  hogue
214  * Fixed compiler warnings
215  *
216  * Revision 1.24  1996/05/01  14:39:12  hogue
217  * Enhanced feature handling, added MakeChemGraphNodeList, added support for domains.
218  *
219  * Revision 1.23  1996/04/18  17:40:05  hogue
220  * Added FETCH_ENTREZ bExtent test to FetchBS to mask EntrezInit/Fini; added mdlLvl BSEVERYTHING support, added a freeer call for pdnmsSlaves.
221  *
222  * Revision 1.22  1996/03/29  19:28:02  hogue
223  * Added pExtra pointer to MSD, fixed PMOD parent pointer
224  *
225  * Revision 1.21  1996/01/31  21:35:00  hogue
226  * Changes to accomodate the free-ing of models, most dramatically is the
227  * encapuslation of interim free-ers to FreeSurfaceModelAsnList and
228  * FreeAtomicModelAsnLists.  KEEP_ASN no longer supported.  PDNSM and its
229  * kin renamed to PDNML.
230  *
231  * Revision 1.20  1995/12/12  23:51:26  kans
232  * removed include vibrant.h
233  *
234  * Revision 1.19  1995/11/30  20:31:24  hogue
235  * Asn order vector in PMLD contains PALD not PMAD's.
236  *
237  * Revision 1.18  1995/11/15  19:15:59  hogue
238  * Traversers now handle solids on a per-model basis.
239  *
240  * Revision 1.17  1995/11/09  22:53:15  hogue
241  * Links between models and locations added, some new free-ers
242  *
243  * Revision 1.16  1995/10/05  17:41:52  hogue
244  * Added rot/translation matrix free-ers
245  *
246  * Revision 1.15  1995/09/21  20:27:00  hogue
247  * Took out call to StrToLong - inserted it longhand for non-vibrant apps
248  *
249  * Revision 1.14  1995/09/19  21:07:22  hogue
250  * Added to FetchBS() Entrez access via PDB or GI
251  *
252  * Revision 1.13  1995/09/18  21:24:37  hogue
253  * Het names added, and ProgMon calls
254  *
255  * Revision 1.12  1995/09/14  14:21:48  hogue
256  * Fix for Element No's 254 (other) and 255 (unk) put into Int1
257  *
258  * Revision 1.11  1995/09/05  19:09:32  hogue
259  * Changed error severity in conversion routine to SEV_ERROR
260  *
261  * Revision 1.10  1995/08/30  19:36:44  kans
262  * numerous changes
263  *
264  * Revision 1.9  1995/08/29  20:03:06  kans
265  * Entrez-like loading now enabled, models fixed
266  *
267  * Revision 1.6  1995/08/28  22:46:40  kans
268  * moved BiostrucAvail to mmdbapi1.[ch]
269  *
270  * Revision 1.5  1995/08/28  20:06:37  kans
271  * *** empty log message ***
272  *
273  * Revision 1.3  1995/08/28  19:31:53  kans
274  * version 4.1
275  *
276  * Revision 1.1  1995/08/02  17:07:15  kans
277  * Initial revision
278  *
279 *
280 * ==========================================================================
281 */
282 
283 /*****************************************************************************
284 *
285 *  mmdbapi1.c
286 *
287 *   UNDER CONSTRUCTION NOTICE.
288 *           SUBJECT TO RADICAL CHANGES...
289 *
290 *  programmer C.W.V. Hogue
291 *  created: 14 Mar 95
292 *  last mod: 28 AUGUST 95
293 *****************************************************************************/
294 #include <ncbi.h>
295 #include <mmdbdata.h>
296 #include <sequtil.h>
297 NLM_EXTERN void VnpHeapSort PROTO ((ValNodePtr PNTR vnp, int (LIBCALLBACK *compar )PROTO ((Nlm_VoidPtr, Nlm_VoidPtr ))));
298   /* this should be #include <utilpub.h> but this conflicts with mmdbdata.h. fix this. lyg */
299 #include <mmdbapi.h>
300 #include <mmdbapi1.h>
301 #include <matrix.h>
302 #include "prunebsc.h"
303 
304 /* Missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
305 #ifdef __MWERKS__
306 #ifdef OS_UNIX_DARWIN
307 #ifndef __SCHAR_MAX__
308 #define __SCHAR_MAX__ 127
309 #endif
310 #endif
311 #endif
312 /* End missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
313 
314 PMBD LIBCALL NewMBD(void)
315 {
316     PMBD pmbdNew = NULL;
317     pmbdNew = (PMBD)MemNew((size_t)(sizeof(MBD)));
318     if (pmbdNew)
319      {
320       pmbdNew->bMe = (Byte) AM_MBD;
321       pmbdNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME);
322      }
323     return pmbdNew;
324 }
325 
326 
327 PMOD LIBCALL NewMOD(void)
328 {
329     PMOD pmodNew = NULL;
330     pmodNew = (PMOD)MemNew((size_t)(sizeof(MOD)));
331     if (pmodNew)
332      {
333       pmodNew->bMe = (Byte) AM_MOD;
334       pmodNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
335       pmodNew->bVisible = 1;
336                    /* turn on object by default -- Yanli */
337       }
338     return pmodNew;
339 }
340 
341 
342 PMDD LIBCALL NewMDD(void)
343 {
344     PMDD pmddNew = NULL;
345     pmddNew = (PMDD)MemNew((size_t)(sizeof(MDD)));
346     if (pmddNew)
347      {
348       pmddNew->bMe = (Byte) AM_MDD;
349       pmddNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
350       }
351     return pmddNew;
352 }
353 
354 PMAD LIBCALL NewMAD(void)
355 {
356     PMAD pmadNew = NULL;
357       pmadNew = (PMAD)MemNew((size_t)(sizeof(MAD)));
358     if (pmadNew)
359      {
360       pmadNew->bMe = (Byte) AM_MAD;
361       pmadNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
362       }
363     return pmadNew;
364 }
365 
366 PALD LIBCALL NewALD(void)
367 {
368     PALD paldNew = NULL;
369     paldNew = (PALD)MemNew((size_t)(sizeof(ALD)));
370     if (paldNew)
371       {
372         paldNew->cAltConf = ' ';
373         paldNew->bMe = (Byte) AM_ALD;
374         paldNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
375       }
376     return paldNew;
377 }
378 
379 PMLD LIBCALL NewMLD(void)
380 {
381     PMLD pmldNew = NULL;
382     pmldNew = (PMLD)MemNew((size_t)(sizeof(MLD)));
383     return pmldNew;
384 }
385 
386 
387 
388 PMGD LIBCALL NewMGD(void)
389 {
390     PMGD pmgdNew = NULL;
391     pmgdNew = (PMGD)MemNew((size_t)(sizeof(MGD)));
392     if (pmgdNew)
393      {
394       pmgdNew->bMe = (Byte) AM_MGD;
395       pmgdNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
396       pmgdNew->bReserved = 0;
397       pmgdNew->bVisible = 1;   /* be displayed by default */
398       pmgdNew->bHighlighted = 0;
399       pmgdNew->pbMasterReserved = NULL;
400       pmgdNew->pvnPARSList = NULL;
401      }
402     return pmgdNew;
403 }
404 
405 PMMD LIBCALL NewMMD(void)
406 {
407     PMMD pmmdNew = NULL;
408     pmmdNew = (PMMD)MemNew((size_t)(sizeof(MMD)));
409     if (pmmdNew)
410      {
411       pmmdNew->bMe = (Byte) AM_MMD;
412       pmmdNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
413       pmmdNew->bVisible = 1;
414       pmmdNew->iTargetRow = -1;
415       }
416       return pmmdNew;
417 }
418 
419 PMSD LIBCALL NewMSD(void)
420 {
421     PMSD pmsdNew = NULL;
422     pmsdNew = (PMSD)MemNew((size_t)(sizeof(MSD)));
423     if (pmsdNew)
424      {
425       pmsdNew->bMe = (Byte) AM_MSD;
426       pmsdNew->bUpdate = (Byte) (REG_STYLE | SHOW_ME );
427       pmsdNew->pfbParent = NULL;
428       pmsdNew->pGlobalPARS = NULL;
429       pmsdNew->pdnSlaveToMasterTransforms = NULL;
430      }
431     return pmsdNew;
432 }
433 
434 
435 
436 PSFF LIBCALL NewSFF(void)
437 {
438   PSFF psffNew = NULL;
439   psffNew = (PSFF) MemNew((size_t)(sizeof(SFF)));
440   return psffNew;
441 }
442 
443 PSFD LIBCALL NewSFD(void)
444 {
445     PSFD psfdNew = NULL;
446     psfdNew = (PSFD) MemNew((size_t)(sizeof(SFD)));
447     return psfdNew;
448 }
449 
450 
451 PSFS LIBCALL NewSFS(void)
452 {
453     PSFS psfsNew = NULL;
454     psfsNew = (PSFS)MemNew((size_t)(sizeof(SFS)));
455     return psfsNew;
456 }
457 
458 PDNSFS LIBCALL NewDNSFS(PDNSFS PNTR ppdnsfsList,  Int2 choice)
459 {
460     PDNSFS pdnsfsNew = NULL;
461     PSFS psfsNew = NULL;
462     psfsNew = NewSFS();
463     if (!psfsNew) return NULL;
464     pdnsfsNew = DValNodeAddPointer(ppdnsfsList, choice, (Nlm_VoidPtr) psfsNew);
465     psfsNew->pdnsfsSet = pdnsfsNew; /* link to parent */
466     return pdnsfsNew;
467 }
468 
469 void LIBCALL FreeSFS(PSFS psfsThis)
470 {
471 
472   if (psfsThis)
473     {
474         if (psfsThis->pcName) MemFree(psfsThis->pcName);
475         if (psfsThis->psfdFeats) FreeSFD(psfsThis->psfdFeats);
476         if (psfsThis->pvnDescr) BiostrucFeatureSetDescrFree(psfsThis->pvnDescr);
477         if (psfsThis->pbsfsFeatSet)
478              MsgAlert(KEY_NONE,SEV_ERROR, "Dangling Feature Set",
479                 "\nMemory leaking at FeatureSet\n.");
480             BiostrucFeatureSetFree(psfsThis->pbsfsFeatSet);
481         MemFree(psfsThis);
482     }
483   return;
484 }
485 
486 
487 
488 PVNSFF LIBCALL NewVNSFF(PVNSFF PNTR ppvnsffList,  Int2 choice)
489 {
490     PVNSFF pvnsffNew = NULL;
491     PSFF psffNew = NULL;
492 
493     psffNew = NewSFF();
494     if (!psffNew) return NULL;
495     pvnsffNew = ValNodeAddPointer(ppvnsffList, choice, (Nlm_VoidPtr) psffNew);
496     return pvnsffNew;
497 }
498 
499 
500 void LIBCALL FreeListVNSFF(PVNSFF pvnsffList)
501 {
502   FreeVNDataFn(pvnsffList,  (pFreeFunc)FreeSFF);
503   return;
504 }
505 
506 
507 void LIBCALL FreeSFF(PSFF psffThis)
508 {
509   if (psffThis)
510     {
511      MemFree(psffThis);
512     }
513   return;
514 }
515 
516 void LIBCALL FreeSFD(PSFD psfdThis)
517 {
518   PSFD psfdNext = NULL;
519   PSFF psffThis = NULL;
520   ValNodePtr vnp = NULL;
521 
522   if (psfdThis)
523     {
524      if (psfdThis->next) psfdNext = psfdThis->next;
525      if (psfdThis->pcName) MemFree(psfdThis->pcName);
526      if (psfdThis->pData)
527        {
528         psffThis = DoesFeatureTypeHaveFuncs(psfdThis->iType);
529         if (psffThis)
530          {  /* this uses the installed function to free pData */
531              (*(psffThis->pFeatToCFn)) ( (Pointer PNTR) &(psfdThis->pData), NULL);
532          }
533         else
534         switch (psfdThis->iType)
535           {
536            case Feature_type_transform:
537               FreeDNTRN((PDNTRN) vnp->data.ptrvalue);
538              break;
539            case Feature_type_camera:
540               vnp = (ValNodePtr) psfdThis->pData;
541               CameraFree((CameraPtr) vnp->data.ptrvalue);
542               ValNodeFree(vnp);
543              break;
544            case Feature_type_script:
545 /* lygmerge           vnp = (ValNodePtr) psfdThis->pData;
546               BiostrucScriptFree((BiostrucScriptPtr) vnp->data.ptrvalue);
547               ValNodeFree(vnp); */
548              break;
549 
550            case Feature_type_multalign:
551            case Feature_type_alignment:
552 
553               ChemGraphAlignmentFree((ChemGraphAlignmentPtr)  psfdThis->pData);
554               ValNodeFree(vnp);
555              break;
556            case Feature_type_similarity:
557               vnp = (ValNodePtr) psfdThis->pData;
558               RegionSimilarityFree((RegionSimilarityPtr) vnp->data.ptrvalue);
559               ValNodeFree(vnp);
560              break;
561            case Feature_type_region:
562               vnp = (ValNodePtr) psfdThis->pData;
563               RegionPntrsFree((RegionPntrsPtr) vnp->data.ptrvalue);
564               ValNodeFree(vnp);
565              break;
566 
567               /* these don't have sub-alloced parts hanging off pData */
568          /*  case Feature_type_indirect:
569              case Feature_type_color:
570              case Feature_type_render:
571              case Feature_type_helix:
572              case Feature_type_strand:
573              case Feature_type_sheet:
574              case Feature_type_turn:
575              case Feature_type_site:
576              case Feature_type_footnote:
577              case Feature_type_subgraph:
578              case Feature_type_core:
579              case Feature_type_supercore:
580              case Feature_type_other: */
581 
582              default: ;
583 
584           } /* switch type */
585          psfdThis->pData = NULL;
586        } /* if pData */
587 
588      if (psfdThis->pbsfFeat)
589         {
590             MsgAlert(KEY_NONE,SEV_ERROR, "Dangling Feature",
591                 "\nMemory leaks at Feature\n.");
592           BiostrucFeatureFree(psfdThis->pbsfFeat);
593         }
594 
595      MemFree(psfdThis);
596      FreeSFD(psfdNext);
597     }
598 }
599 
600 
601 
602 void LIBCALL FreeMBD(PMBD pmbdThis)
603 {
604 #ifdef _DEBUG_2
605 printf("in FreeMBD ");
606 #endif
607     if (pmbdThis)
608        MemFree(pmbdThis);
609 }
610 
611 void LIBCALL FreeMOD(PMOD pmodThis)
612 {
613 #ifdef _DEBUG_2
614 printf("in FreeMOD ");
615 #endif
616     if (pmodThis->pvnContains) ValNodeFree(pmodThis->pvnContains);
617     if (pmodThis->ppflObject) FLMatrixFree(pmodThis->ppflObject, 0, 0);
618     if (pmodThis->pucSwap) UCVectorFree(pmodThis->pucSwap, 0);
619     if (pmodThis->ppi4Triangles) I4MatrixFree(pmodThis->ppi4Triangles, 0, 0);
620     if (pmodThis->pi2vColor) I2VectorFree(pmodThis->pi2vColor,0);
621     if (pmodThis) MemFree(pmodThis);
622 }
623 
624 void LIBCALL FreeMDD(PMDD pmddThis)
625 {
626 #ifdef _DEBUG_2
627 printf("in FreeMDD ");
628 #endif
629     if (pmddThis->pvnContains) ValNodeFree(pmddThis->pvnContains);
630     if (pmddThis)
631        MemFree(pmddThis);
632 }
633 
634 
635 void LIBCALL FreeMAD(PMAD pmadThis)
636 {
637 #ifdef _DEBUG_2
638 printf("in FreeMAD ");
639 #endif
640     if (pmadThis)
641      {
642         if (pmadThis->pvnContainedBy) ValNodeFree(pmadThis->pvnContainedBy);
643        if (pmadThis->pvnBonds) ValNodeFree(pmadThis->pvnBonds);
644        if (pmadThis->pvnalLocate) FreeListVNAL(pmadThis->pvnalLocate);
645        MemFree(pmadThis);
646      }
647 
648 }
649 
650 void LIBCALL FreeALD(PALD paldThis)
651 {
652   PALD paldNext;
653 #ifdef _DEBUG_2
654 printf("in FreeALD ");
655 #endif
656   while (paldThis)
657     {
658         paldNext = paldThis->next;
659         if ((paldThis->iFloatNo) && (paldThis->pflvData))
660           FLVectorFree(paldThis->pflvData, 0);
661         MemFree(paldThis);
662         paldThis = paldNext;
663     }
664 }
665 
666 void LIBCALL FreeMLD(PMLD pmldThis)
667 {
668   PMLD pmldNext;
669 #ifdef _DEBUG_2
670 printf("in FreeMLD ");
671 #endif
672   while (pmldThis)
673     {
674         pmldNext = pmldThis->next;
675          /* models are owned by the Biostruc ptr in pmsd->pbsBS */
676         if (pmldThis->ppAsnOrder) PTRVectorFree(pmldThis->ppAsnOrder, 0);
677         if (pmldThis->pcAltConf) MemFree(pmldThis->pcAltConf);
678         if (pmldThis->pcModelName) MemFree(pmldThis->pcModelName);
679         MemFree(pmldThis);
680         pmldThis = pmldNext;
681     }
682 }
683 
684 
685 
686 void LIBCALL FreeKeptFeature(BiostrucFeatureSetPtr pbsfsThis)
687 {
688 #ifdef _DEBUG_2
689 printf("in FreeKeptFeature ");
690 #endif
691     if (pbsfsThis) BiostrucFeatureSetFree(pbsfsThis);
692 }
693 
694 
695 void LIBCALL FreeMGD(PMGD pmgdThis)
696 {
697 #ifdef _DEBUG_2
698 printf("in FreeMGD ");
699 #endif
700    if (pmgdThis)
701      {
702       if (pmgdThis->pvnContainedBy) ValNodeFree(pmgdThis->pvnContainedBy);
703       if (pmgdThis->pcGraphName) MemFree(pmgdThis->pcGraphName);
704       if (pmgdThis->pcPDBComment) MemFree(pmgdThis->pcPDBComment);
705       if (pmgdThis->pvnmaAHead) FreeListVNMA(pmgdThis->pvnmaAHead);
706       if (pmgdThis->pvnmbBHead) FreeListVNMB(pmgdThis->pvnmbBHead);
707       if (pmgdThis->ppflBoundBox) FLMatrixFree(pmgdThis->ppflBoundBox,0,0);
708       if (pmgdThis->pcIUPAC) MemFree(pmgdThis->pcIUPAC);
709       if (pmgdThis->pcGraphNum) MemFree(pmgdThis->pcGraphNum);
710       MemFree(pmgdThis);
711      }
712 }
713 
714 void LIBCALL FreeMMD(PMMD pmmdThis)
715 {
716 #ifdef _DEBUG_2
717 printf("in FreeMMD ");
718 #endif
719     if (pmmdThis)
720       {
721         if (pmmdThis->pvnContainedBy) ValNodeFree(pmmdThis->pvnContainedBy);
722         if (pmmdThis->pcMolName) MemFree(pmmdThis->pcMolName);
723         if (pmmdThis->pcSeqId) MemFree(pmmdThis->pcSeqId);
724         if (pmmdThis->pcSequence) MemFree(pmmdThis->pcSequence);
725         if (pmmdThis->pdnmgHead) FreeListDNMG(pmmdThis->pdnmgHead);
726         if (pmmdThis->pvnmbIRBHead) FreeListVNMB(pmmdThis->pvnmbIRBHead);
727         if (pmmdThis->ppflBoundBox) FLMatrixFree(pmmdThis->ppflBoundBox,0,0);
728         if (pmmdThis->pMolDescr)
729             AsnGenericChoiceSeqOfFree(pmmdThis->pMolDescr,(AsnOptFreeFunc)BiomolDescrFree);
730         if (pmmdThis->pSeqId) SeqIdFree(pmmdThis->pSeqId);
731         MemFree(pmmdThis);
732       }
733 }
734 
735 void LIBCALL FreeMSD(PMSD pmsdThis)
736 {
737 #ifdef _DEBUG_2
738 printf("in FreeMSD ");
739 #endif
740     if (pmsdThis)
741       {
742 /* ASN.1 parts */
743         if (pmsdThis->pbsBS) BiostrucFree(pmsdThis->pbsBS);
744   /* BiostrucFree kills models, features % saved ASN.1 parts ... */
745         if (pmsdThis->pGraphDescr)
746             AsnGenericChoiceSeqOfFree(pmsdThis->pGraphDescr,(AsnOptFreeFunc)BiomolDescrFree);
747     if (pmsdThis->pDictLocal)
748         AsnGenericUserSeqOfFree(pmsdThis->pDictLocal,(AsnOptFreeFunc)ResidueGraphFree);
749 /* Structure Strings */
750         if (pmsdThis->pcSName) MemFree(pmsdThis->pcSName);
751         if (pmsdThis->pcPDBName) MemFree(pmsdThis->pcPDBName);
752         if (pmsdThis->pcChemName) MemFree(pmsdThis->pcChemName);
753         if (pmsdThis->pcPdbClass) MemFree(pmsdThis->pcPdbClass);
754         if (pmsdThis->pcPdbSource) MemFree(pmsdThis->pcPdbSource);
755 /* Modelstruc parts */
756         if (pmsdThis->pdnmmHead) FreeListDNMM(pmsdThis->pdnmmHead);
757         if (pmsdThis->pvnmbIMBHead) FreeListVNMB(pmsdThis->pvnmbIMBHead);
758         if (pmsdThis->pdnmlModels) FreeListDNML(pmsdThis->pdnmlModels);
759         if (pmsdThis->pvnmoHead) FreeListVNMO(pmsdThis->pvnmoHead);
760         if (pmsdThis->pvnmdHead) FreeListVNMD(pmsdThis->pvnmdHead);
761 /*      if (pmsdThis->pdnsfFeatures) FreeListDNSF(pmsdThis->pdnsfFeatures); */
762         if (pmsdThis->psaStrucAlignment) BiostrucAnnotSetFree(pmsdThis->psaStrucAlignment);
763         if (pmsdThis->ppflRotate) FLMatrixFree(pmsdThis->ppflRotate,0,0);
764         if (pmsdThis->pdnsfsHead) FreeListDNSFS(pmsdThis->pdnsfsHead);
765         if (pmsdThis->pflTranslate) FLVectorFree(pmsdThis->pflTranslate,0);
766         if (pmsdThis->ppflBoundBox) FLMatrixFree(pmsdThis->ppflBoundBox,0,0);
767         if (pmsdThis->pdnmsSlaves) FreeListDNMS(pmsdThis->pdnmsSlaves);
768         MemFree(pmsdThis);
769 
770        }
771 }
772 
773 /* adds after last node in list if *head not NULL
774  * otherwise head is new node
775  */
776 
777 PVNMB LIBCALL NewVNMB(PVNMB PNTR ppvnmbList,  Int2 choice)
778 {
779     PVNMB pvnmbNew = NULL;
780     PMBD pmbdNew = NULL;
781     pmbdNew = NewMBD();
782     if (!pmbdNew) return NULL;
783     pvnmbNew = ValNodeAddPointer(ppvnmbList, choice, (Nlm_VoidPtr) pmbdNew);
784     pmbdNew->pvnmbLink = pvnmbNew;  /* back link */
785     return pvnmbNew;
786 }
787 
788 PVNMO LIBCALL NewVNMO(PVNMO PNTR ppvnmoList,  Int2 choice)
789 {
790     PVNMO pvnmoNew = NULL;
791     PMOD pmodNew = NULL;
792     pmodNew = NewMOD();
793     if (!pmodNew) return NULL;
794     pvnmoNew = ValNodeAddPointer(ppvnmoList, choice, (Nlm_VoidPtr) pmodNew);
795     pmodNew->pvnmoLink = pvnmoNew;  /* back link */
796     return pvnmoNew;
797 }
798 
799 
800 PVNMD LIBCALL NewVNMD(PVNMD PNTR ppvnmdList,  Int2 choice)
801 {
802     PVNMD pvnmdNew = NULL;
803     PMDD pmddNew = NULL;
804     pmddNew = NewMDD();
805     if (!pmddNew) return NULL;
806     pvnmdNew = ValNodeAddPointer(ppvnmdList, choice, (Nlm_VoidPtr) pmddNew);
807     pmddNew->pvnmdLink = pvnmdNew;  /* back link */
808     return pvnmdNew;
809 }
810 
811 
812 PVNMA LIBCALL NewVNMA(PVNMA PNTR ppvnmaList,  Int2 choice)
813 {
814     PVNMA pvnmaNew = NULL;
815     PMAD pmadNew = NULL;
816     pmadNew = NewMAD();
817     if (!pmadNew) return NULL;
818     pvnmaNew = ValNodeAddPointer(ppvnmaList,  choice, (Nlm_VoidPtr) pmadNew);
819     pmadNew->pvnmaLink = pvnmaNew;
820     return pvnmaNew;
821 }
822 
823 PVNAL LIBCALL NewVNAL(PVNAL PNTR ppvnalList, Int2 choice)
824 {
825  /* picks either paldlNew or paldNew as data node */
826  /* choice is model number - which will end up as a Uint1 */
827     PVNAL pvnalNew = NULL;
828     PALD paldNew = NULL;
829     paldNew = NewALD();
830     if (!paldNew) return NULL;
831     pvnalNew = ValNodeAddPointer(ppvnalList, choice, (Nlm_VoidPtr) paldNew);
832     paldNew->pvnalLink = pvnalNew; /* link to ValNode */
833     return pvnalNew;
834 }
835 
836 PDNMG LIBCALL NewDNMG(PDNMG PNTR ppdnmgList,  Int2 choice)
837 {
838     PDNMG pdnmgNew = NULL;
839     PMGD pmgdNew = NULL;
840     pmgdNew = NewMGD();
841     if (!pmgdNew) return NULL;
842     pdnmgNew = DValNodeAddPointer(ppdnmgList, choice, (Nlm_VoidPtr) pmgdNew);
843     pmgdNew->pdnmgLink = pdnmgNew;
844     return pdnmgNew;
845 }
846 
847 PDNMM LIBCALL NewDNMM(PDNMM PNTR ppdnmmList,  Int2 choice)
848 {
849     PDNMM pdnmmNew = NULL;
850     PMMD pmmdNew = NULL;
851     pmmdNew = NewMMD();
852     if (!pmmdNew) return NULL;
853     pdnmmNew = DValNodeAddPointer(ppdnmmList, choice,  (Nlm_VoidPtr) pmmdNew);
854     pmmdNew->pdnmmLink = pdnmmNew;
855     return pdnmmNew;
856 }
857 
858 PDNMS LIBCALL NewDNMS(PDNMS PNTR ppdnmsList,  Int2 choice)
859 {
860     PDNMS pdnmsNew = NULL;
861     PMSD pmsdNew = NULL;
862     pmsdNew = NewMSD();
863     if (!pmsdNew) return NULL;
864     pdnmsNew = DValNodeAddPointer(ppdnmsList,  choice, (Nlm_VoidPtr) pmsdNew);
865     pmsdNew->pdnmsLink = pdnmsNew;
866     return pdnmsNew;
867 }
868 
869 
870 
871 /* need to make equivalent function ptr call in a ValNodeFreeDataFn */
872 ValNodePtr LIBCALL FreeVNDataFn (ValNodePtr vnp,  pFreeFunc freefn)
873 {
874         ValNodePtr next;
875 
876         while (vnp)
877         {
878                 if (freefn)
879                   (*freefn)(vnp->data.ptrvalue);
880                 else
881                    Nlm_MemFree(vnp->data.ptrvalue);
882                 next = vnp->next;
883                 Nlm_MemFree(vnp);
884                 vnp = next;
885         }
886         return NULL;
887 }
888 
889 void LIBCALL FreeListVNAL(PVNAL pvnalList)
890 {
891 #ifdef _DEBUG_2
892 printf("in FreeListVNAL ");
893 #endif
894     FreeVNDataFn(pvnalList,  (pFreeFunc)FreeALD);
895 }
896 
897 void LIBCALL FreeListDNML(PDNML pdnmlList)
898 {
899 #ifdef _DEBUG_2
900 printf("in FreeListDNML ");
901 #endif
902    DValNodeFreeData(pdnmlList, (pFreeFunc)FreeMLD);
903 }
904 
905 void LIBCALL FreeListDNSFS(PDNSFS pdnsfsList)
906 {
907 #ifdef _DEBUG_2
908 printf("in FreeListDNSF ");
909 #endif
910    DValNodeFreeData(pdnsfsList, (pFreeFunc)FreeSFS);  /* don't free Biostruc-dependent features */
911 
912 }
913 
914 void LIBCALL FreeListDNSF(PDNSF pdnsfList)
915 {
916 #ifdef _DEBUG_2
917 printf("in FreeListDNSF ");
918 #endif
919    DValNodeFree(pdnsfList);  /* don't free Biostruc-dependent features */
920 
921 }
922 
923 void LIBCALL FreeListVNMB(PVNMB pvnmbList) /* takes out the whole singly-linked list */
924 {
925 #ifdef _DEBUG_2
926 printf("in FreeListVNMB ");
927 #endif
928     FreeVNDataFn(pvnmbList, (pFreeFunc)FreeMBD);
929 }
930 
931 
932 void LIBCALL FreeListVNMO(PVNMO pvnmoList) /* takes out the whole singly-linked list */
933 {
934 #ifdef _DEBUG_2
935 printf("in FreeListVNMO ");
936 #endif
937     FreeVNDataFn(pvnmoList, (pFreeFunc)FreeMOD);
938 }
939 
940 
941 void LIBCALL FreeListVNMD(PVNMD pvnmdList) /* takes out the whole singly-linked list */
942 {
943 #ifdef _DEBUG_2
944 printf("in FreeListVNMD ");
945 #endif
946     FreeVNDataFn(pvnmdList, (pFreeFunc)FreeMDD);
947 }
948 
949 void LIBCALL FreeListVNMA(PVNMA pvnmaList) /* takes out the whole singly-linked list */
950 {
951 #ifdef _DEBUG_2
952 printf("in FreeListVNMA ");
953 #endif
954    FreeVNDataFn(pvnmaList, (pFreeFunc)FreeMAD);
955 }
956 
957 /* takes out one - at a time ! */
958 PDNMG LIBCALL FreeDNMG(PDNMG pdnmgList, PDNMG pdnmgThis)
959 {
960 #ifdef _DEBUG_2
961 printf("in FreeDNMG ");
962 #endif
963    return DValNodeListDelNode(pdnmgList,  pdnmgThis, (pFreeFunc)FreeMGD);
964 }
965 
966 
967 void LIBCALL FreeListDNMG(PDNMG pdnmgList)
968 {
969     PDNMG pdnmgTemp;
970 #ifdef _DEBUG_2
971 printf("in FreeListDNMG ");
972 #endif
973 
974     pdnmgTemp = pdnmgList;
975     while (pdnmgTemp)
976     {
977         pdnmgList = FreeDNMG(pdnmgTemp, pdnmgTemp);
978         pdnmgTemp = pdnmgList;
979     }
980 }
981 
982 
983 PDNMM LIBCALL FreeDNMM(PDNMM pdnmmList,  PDNMM pdnmmThis)
984 {
985 #ifdef _DEBUG_2
986 printf("in FreeDNMM ");
987 #endif
988    return DValNodeListDelNode(pdnmmList, pdnmmThis, (pFreeFunc)FreeMMD);
989 }
990 
991 void LIBCALL FreeListDNMM(PDNMM pdnmmList)
992 {
993     PDNMM pdnmmTemp;
994 #ifdef _DEBUG_2
995 printf("in FreeListDNMM ");
996 #endif
997     pdnmmTemp = pdnmmList;
998     while (pdnmmTemp)
999     {
1000         pdnmmList = FreeDNMM(pdnmmTemp, pdnmmTemp);
1001         pdnmmTemp = pdnmmList;
1002     }
1003 }
1004 
1005 PDNMS LIBCALL FreeDNMS(PDNMS pdnmsList, PDNMS pdnmsThis)
1006 {
1007 #ifdef _DEBUG_2
1008 printf("in FreeDNMS ");
1009 #endif
1010     return DValNodeListDelNode(pdnmsList,  pdnmsThis, (pFreeFunc)FreeMSD);
1011 }
1012 
1013 
1014 void LIBCALL FreeListDNMS(PDNMS pdnmsList)
1015 {
1016     PDNMS pdnmsTemp;
1017     pdnmsTemp = pdnmsList;
1018 #ifdef _DEBUG_2
1019 printf("in FreeListDNMS ");
1020 #endif
1021     while (pdnmsTemp)
1022     {
1023         pdnmsList = FreeDNMS(pdnmsTemp, pdnmsTemp);
1024         pdnmsTemp = pdnmsList;
1025     }
1026 }
1027 
1028 
1029 void LIBCALL FreeAtomicModelAsnLists(AtomicCoordinatesPtr pacThis)
1030 {
1031    AlternateConformationIdsPtr          paciThis = NULL;
1032    IsotropicTemperatureFactorsPtr       pitfThis = NULL;
1033    AnisotropicTemperatureFactorsPtr     patfThis = NULL;
1034    AtomicOccupanciesPtr                 pocThis = NULL;
1035    ModelSpacePointsPtr                  pmspThis = NULL;
1036    AtomPntrsPtr                         pappThis = NULL;
1037    CoordinatesPtr                       pcoThis = NULL;
1038 
1039     /* Free data off AtomicCoordinatesPtr pacThis */
1040 
1041     if (!pacThis) return;
1042 #ifdef _DEBUG_1
1043 printf("in FreeAtomicModelAsnList\n");
1044 #endif
1045     pappThis = pacThis->atoms; /*  AtomPntrsPtr */
1046     pmspThis =  pacThis->sites; /* ModelSpacePointsPtr */
1047     pocThis = pacThis->occupancies; /*  AtomicOccupanciesPtr */
1048     paciThis = pacThis->alternate_conf_ids; /* AlternateConformationIdsPtr */
1049     if (pacThis->temperature_factors)  /* choice of temp factor types */
1050       {
1051        if (pacThis->temperature_factors->choice == AtomicTemperatureFactors_isotropic)
1052         {
1053           pitfThis = (IsotropicTemperatureFactorsPtr) pacThis->temperature_factors->data.ptrvalue;
1054         }
1055        if (pacThis->temperature_factors->choice == AtomicTemperatureFactors_anisotropic)
1056         {
1057           patfThis = (AnisotropicTemperatureFactorsPtr) pacThis->temperature_factors->data.ptrvalue;
1058         }
1059       }
1060     if (pappThis)
1061          {
1062           AtomPntrsFree(pappThis);
1063           pacThis->atoms = NULL;
1064          }
1065     if (pmspThis)
1066          {
1067           ModelSpacePointsFree(pmspThis);
1068           pacThis->sites = NULL;
1069          }
1070     if (pocThis)
1071          {
1072           AtomicOccupanciesFree(pocThis);
1073           pacThis->occupancies = NULL;
1074          }
1075     if (paciThis)
1076          {
1077           AlternateConformationIdsFree(paciThis);
1078           pacThis->alternate_conf_ids = NULL;
1079          }
1080     if (pitfThis)
1081          {
1082           IsotropicTemperatureFactorsFree (pitfThis);
1083           pacThis->temperature_factors->data.ptrvalue = NULL;
1084          }
1085     if (patfThis)
1086          {
1087           AnisotropicTemperatureFactorsFree(patfThis);
1088           pacThis->temperature_factors->data.ptrvalue = NULL;
1089          }
1090     if (pacThis->temperature_factors)
1091          {
1092            ValNodeFree(pacThis->temperature_factors);
1093            pacThis->temperature_factors = NULL;
1094          }
1095    return;
1096 }
1097 
1098 
1099 
1100 
1101 
1102 PMAD LIBCALL AtomFromMMDBIndex(PDNMS pdnmsList, Int2 iStru, Int2 iMol, Int2 iGraph, Int4 iAtom)
1103 {
1104     PDNMS pdnmsThis;
1105     PMSD  pmsdThis;
1106     PDNMM pdnmmThis;
1107     PMMD  pmmdThis;
1108     PDNMG pdnmgThis;
1109     PMGD  pmgdThis;
1110     PVNMA pvnmaThis;
1111     PMAD  pmadThis;
1112 
1113     pdnmsThis = (PDNMS) DValNodeFindNext((DValNodePtr)pdnmsList, NULL, iStru);
1114     if (pdnmsThis) pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
1115     else return NULL;
1116     pdnmmThis = (PDNMM) DValNodeFindNext((DValNodePtr)pmsdThis->pdnmmHead, NULL, iMol);
1117     if (pdnmmThis) pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1118     else return NULL;
1119     pdnmgThis = (PDNMG) DValNodeFindNext((DValNodePtr)pmmdThis->pdnmgHead,  NULL,  iGraph);
1120     if (pdnmgThis) pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1121     else return NULL;
1122     pvnmaThis = (PVNMA) pmgdThis->pvnmaAHead;
1123     if (pvnmaThis) pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
1124     else return NULL;
1125     while (pvnmaThis)
1126      {
1127          if (pmadThis->iIndex == iAtom)
1128            {
1129              return pmadThis;
1130            }
1131          pvnmaThis = pvnmaThis->next;
1132          if (pvnmaThis) pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
1133      }
1134     return NULL;
1135 }
1136 
1137 
1138 
1139 PMGD LIBCALL GraphFromMMDBIndex(PDNMS pdnmsList, Int2 iStru, Int2 iMol, Int2 iGraph )
1140 {
1141     PDNMS pdnmsThis;
1142     PMSD  pmsdThis;
1143     PDNMM pdnmmThis;
1144     PMMD  pmmdThis;
1145     PDNMG pdnmgThis;
1146     PMGD  pmgdThis;
1147 
1148 
1149     pdnmsThis = (PDNMS) DValNodeFindNext((DValNodePtr)pdnmsList, NULL, iStru);
1150     if (pdnmsThis) pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
1151     else return NULL;
1152     pdnmmThis = (PDNMM) DValNodeFindNext((DValNodePtr)pmsdThis->pdnmmHead, NULL, iMol);
1153     if (pdnmmThis) pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1154     else return NULL;
1155     pdnmgThis = (PDNMG) DValNodeFindNext((DValNodePtr)pmmdThis->pdnmgHead,  NULL,  iGraph);
1156     if (pdnmgThis)
1157        {
1158          pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1159          return pmgdThis;
1160        }
1161     else return NULL;
1162 }
1163 
1164 
1165 PMMD LIBCALL MolFromMMDBIndex(PDNMS pdnmsList, Int2 iStru, Int2 iMol)
1166 {
1167     PDNMS pdnmsThis;
1168     PMSD  pmsdThis;
1169     PDNMM pdnmmThis;
1170     PMMD  pmmdThis;
1171 
1172 
1173     pdnmsThis = (PDNMS) DValNodeFindNext((DValNodePtr)pdnmsList, NULL, iStru);
1174     if (pdnmsThis) pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
1175     else return NULL;
1176     pdnmmThis = (PDNMM) DValNodeFindNext((DValNodePtr)pmsdThis->pdnmmHead, NULL, iMol);
1177     if (pdnmmThis)
1178        {
1179          pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1180          return pmmdThis;
1181        }
1182     else return NULL;
1183 }
1184 
1185 PRGD LIBCALL LoadDict(CharPtr pcDictName)
1186 {
1187 
1188    AsnIoPtr aipr=NULL;
1189    FILE *f;
1190    Char fullpath [PATH_MAX];
1191    CharPtr ptr;
1192    PRGD rgdp;
1193 
1194    aipr = NULL;
1195    rgdp = NULL;
1196 
1197    ProgramPath (fullpath, sizeof (fullpath) - 1);
1198    ptr = StringRChr (fullpath, DIRDELIMCHR);
1199    if (ptr != NULL) {
1200       *ptr = '\0';
1201    } else {
1202       StrCpy(fullpath, ".");
1203    }
1204    FileBuildPath (fullpath, NULL, pcDictName);
1205 
1206    if ((f = FileOpen (fullpath, "rb")) == NULL) {
1207       if (FindPath ("NCBI", "NCBI", "DATA", fullpath, sizeof (fullpath) - 1)) {
1208          FileBuildPath (fullpath, NULL, pcDictName);
1209          aipr = AsnIoOpen (fullpath, "rb");
1210          if (aipr == NULL) {
1211             ErrPost(CTX_NCBIOBJ, 1, "Couldn't open [%s] in LoadDict", fullpath);
1212             return NULL;
1213          }
1214       } else {
1215          ErrPost(CTX_NCBIOBJ, 1, "FindPath failed in LoadDict");
1216          return NULL;
1217       }
1218    } else {
1219       FileClose (f);
1220       aipr = AsnIoOpen(fullpath, "rb");
1221       if (aipr == NULL) {
1222          ErrPost(CTX_NCBIOBJ, 1, "Couldn't open [%s] in LoadDict", fullpath);
1223          return NULL;
1224       }
1225    }
1226 
1227    rgdp = BiostrucResidueGraphSetAsnRead(aipr, NULL);
1228 
1229    AsnIoFlush(aipr);
1230    aipr = AsnIoClose(aipr);
1231 
1232    return rgdp;
1233 }
1234 
1235 
1236 
1237 BiostrucPtr LIBCALL FetchBS(CharPtr pcFetch,  Int2 iType, Int4 mdlLvl,
1238                 Int4 maxModels, Byte bExtent)
1239 {   /* mdlLvl = BSEVERYTHING used to denote fetch everything!  */
1240     AsnIoPtr paioAIP = NULL; /* aip */
1241     BiostrucPtr pbsThe = NULL;
1242     Boolean bIsNetwork = FALSE;
1243     /* LinkSetPtr plsLink = NULL; */
1244     DocUid duUID = 0;
1245   /* StrToLong stuff */
1246   Nlm_Char     ch;
1247   Nlm_Int2     i;
1248   Nlm_Int2     len;
1249   Nlm_Char     local [64];
1250   Nlm_Boolean  nodigits;
1251   Nlm_Boolean  rsult;
1252   long int     val;
1253 
1254 
1255 #ifdef _DEBUG_0
1256 printf("MaxModels=%d ModelLevel=%d\n",(int) maxModels,(int) mdlLvl);
1257 #endif
1258         /* load the parse trees */
1259         if (! (objmmdb1AsnLoad() && objmmdb2AsnLoad() && objmmdb3AsnLoad() ))
1260           {
1261                 ErrPostEx(SEV_FATAL,0,0, "Internal - objmmdbNAsnLoad() Failure");
1262                 return NULL;
1263           } /* DON'T load any more than necessary */
1264 
1265         /* handle the different types of input data */
1266         switch (iType)
1267         {
1268         case INP_ASCII_FILE:
1269                 /* open the ASN.1 input file in the right mode */
1270                 if ((paioAIP = AsnIoOpen (pcFetch, "r")) == NULL)
1271                 {
1272                         ErrClear(); /* toolkit fatal */
1273                         ErrPostEx(SEV_ERROR,0,0, "Unable to open ASCII ASN.1 input file:\n %s\n Check if it exists."
1274                                   ,pcFetch);
1275                         ErrShow();
1276                         return NULL;
1277                 }
1278                 /* get the pointer to the seq entry */
1279 
1280                 if (mdlLvl == BSEVERYTHING)
1281                 {
1282                   if ((pbsThe =  BiostrucAsnRead(paioAIP, NULL)) == NULL)
1283                     {
1284                         ErrClear(); /* toolkit fatal */
1285                         ErrPostEx(SEV_ERROR,0,0, "Unable to read Biostruc in:\n%s\nConfused Binary/ASCII or Wrong File Type!"
1286                         ,pcFetch);
1287                         ErrShow();
1288                         return NULL;
1289                     }}
1290                 else
1291                  {  if ((pbsThe =  BiostrucAsnGet(paioAIP, NULL, mdlLvl, maxModels)) == NULL)
1292                     {
1293                         ErrClear(); /* toolkit fatal */
1294                         ErrPostEx(SEV_ERROR,0,0, "Unable to read Biostruc in:\n%s\nConfused Binary/ASCII or Wrong File Type!"
1295                         ,pcFetch);
1296                         ErrShow();
1297                         return NULL;
1298                     }}
1299                 AsnIoClose(paioAIP);
1300                 /* printf("Got the ascii biostruc\n"); */
1301                 break;
1302         case INP_BINARY_FILE:
1303         /*      printf("in Binary input\n"); */
1304 
1305                 /* open the ASN.1 input file in the right mode */
1306                 if ((paioAIP = AsnIoOpen (pcFetch, "rb")) == NULL)
1307                 {
1308                         ErrClear(); /* toolkit fatal */
1309                         ErrPostEx(SEV_ERROR,0,0, "Unable to open BINARY ASN.1 input file:\n %s\n Check if it exists.",pcFetch);
1310                         ErrShow();
1311                         return NULL;
1312                 }
1313                 /* get the pointer to the seq entry */
1314                 if (mdlLvl == BSEVERYTHING)
1315                 {
1316                   if ((pbsThe =  BiostrucAsnRead(paioAIP, NULL)) == NULL)
1317                     {
1318                         ErrClear(); /* toolkit fatal */
1319                         ErrPostEx(SEV_ERROR,0,0, "Unable to read Biostruc in:\n%s\nConfused Binary/ASCII or Wrong File Type!"
1320                         ,pcFetch);
1321                         ErrShow();
1322                         return NULL;
1323                     }}
1324                 else
1325                  {  if ((pbsThe =  BiostrucAsnGet(paioAIP, NULL, mdlLvl, maxModels)) == NULL)
1326                     {
1327                         ErrClear(); /* toolkit fatal */
1328                         ErrPostEx(SEV_ERROR,0,0, "Unable to read Biostruc in:\n%s\nConfused Binary/ASCII or Wrong File Type!"
1329                         ,pcFetch);
1330                         ErrShow();
1331                         return NULL;
1332                     }}
1333 
1334                 AsnIoClose(paioAIP);
1335                 /* printf("Got the binary biostruc\n"); */
1336                 break;
1337         case INP_GI:
1338                 if (bExtent & (Byte) FETCH_ENTREZ)
1339                 if (!MMDBInit())
1340                   {
1341                         ErrClear(); /* toolkit fatal */
1342                         ErrPostEx(SEV_ERROR,0,0, "Unable to open MMDB input.");
1343                         ErrShow();
1344                         return NULL;
1345                   }
1346 
1347 /* this is code from Nlm StrToLong */
1348   rsult = FALSE;
1349   len = (Nlm_Int2) Nlm_StringLen (pcFetch);
1350   if (len != 0) {
1351     rsult = TRUE;
1352     nodigits = TRUE;
1353     for (i = 0; i < len; i++) {
1354       ch = pcFetch [i];
1355       if (ch == ' ' || ch == '+' || ch == '-') {
1356       } else if (ch < '0' || ch > '9') {
1357         rsult = FALSE;
1358       } else {
1359         nodigits = FALSE;
1360       }
1361     }
1362     if (nodigits) {
1363       rsult = FALSE;
1364     }
1365     if (rsult) {
1366       Nlm_StringNCpy (local, pcFetch, sizeof (local));
1367       if (sscanf (local, "%ld", &val) == 1) {
1368         duUID = val;
1369       }
1370     }
1371   }
1372                 if (duUID == 0)
1373                    {
1374                         ErrClear(); /* toolkit fatal */
1375                         ErrPostEx(SEV_ERROR,0,0, "UID -  %s - not in structure database, \n Check your input.", pcFetch);
1376                         ErrShow();
1377                         if (bExtent & (Byte) FETCH_ENTREZ) MMDBFini();
1378                         return NULL;
1379                   }
1380                 if ((pbsThe = MMDBBiostrucGet(duUID,  mdlLvl, maxModels)) == NULL)
1381                   {
1382                         ErrClear(); /* toolkit fatal */
1383                         ErrPostEx(SEV_ERROR,0,0, "Unable to find UID = %ld \nin database.", (long)duUID);
1384                         ErrShow();
1385                         if (bExtent & (Byte) FETCH_ENTREZ) MMDBFini();
1386                         return NULL;
1387                   }
1388                  if (bExtent & (Byte) FETCH_ENTREZ) MMDBFini();
1389                 break;
1390         case INP_PDB_ACCESSION:
1391                  if (bExtent & (Byte) FETCH_ENTREZ)
1392                  if (!MMDBInit())
1393                   {
1394                         ErrClear(); /* toolkit fatal */
1395                         ErrPostEx(SEV_ERROR,0,0, "Unable to open MMDB input.");
1396                         ErrShow();
1397                         return NULL;
1398                   }
1399                 duUID = MMDBEvalPDB(pcFetch);
1400                 if (duUID != 0)
1401                   {
1402                       if ((pbsThe = MMDBBiostrucGet(duUID,  mdlLvl, maxModels)) == NULL)
1403                         {
1404                             ErrClear(); /* toolkit fatal */
1405                             ErrPostEx(SEV_ERROR,0,0, "Unable to find UID = %ld \nin database.", (long)duUID);
1406                             ErrShow();
1407                             /* LinkSetFree(plsLink); */
1408                             if (bExtent & (Byte) FETCH_ENTREZ) MMDBFini();
1409                             return NULL;
1410                         }
1411                   }
1412                 else
1413                   {
1414                         ErrClear(); /* toolkit fatal */
1415                         ErrPostEx(SEV_ERROR,0,0, "Accession -  %s - not in structure database, \n Check your input.", pcFetch);
1416                         ErrShow();
1417                         if (bExtent & (Byte) FETCH_ENTREZ) MMDBFini();
1418                         return NULL;
1419                   }
1420                 if (bExtent & (Byte) FETCH_ENTREZ) MMDBFini();
1421                 break;
1422 
1423           default:
1424                return NULL;
1425         } /* switch iTYPE */
1426 /* here pbsThe should be valid in-memory Biostruc */
1427     return pbsThe;
1428 }
1429 
1430 
1431 
1432 
1433 
1434 
1435 
1436 
1437 /* make a GetBSRefictiona  */
1438 
1439 
1440 
1441 
1442 
1443 
1444 
1445 
1446 
1447 
1448 /**************************/
1449 /*version 4.1 ASN.1 code **/
1450 /**************************/
1451 
1452 
1453  /* makes ValNode list on MAD, MGD, MDD pvnContainedBy
1454   * and ValNode list on MOD MDD pvnContains.
1455   * ranges are explicitly instantiated,
1456   * but not children.
1457   */
1458 
1459 Boolean LIBCALL AssignSurfaceContents(PDNMS pdnmsList, Int2 iModel, PFB pfbThis, ChemGraphPntrsPtr pcgpContents)
1460 {
1461 
1462    AtomPntrsPtr ppaThis = NULL;
1463    ResiduePntrsPtr pprThis = NULL;
1464    ResidueExplicitPntrsPtr ppreThis = NULL;
1465    ResidueIntervalPntrPtr ppriThis = NULL;
1466    MoleculePntrsPtr ppmThis = NULL;
1467    ValNodePtr pvnAtoms = NULL;
1468    ValNodePtr pvnMol = NULL;
1469    ValNodePtr pvnRes = NULL;
1470    ValNodePtr pvnThis = NULL;
1471    PMAD pmadThis = NULL;
1472    PMGD pmgdThis = NULL;
1473    PMMD pmmdThis = NULL;
1474    PMOD pmodThis = NULL;
1475    PMDD pmddThis = NULL;
1476    Int4 iStart = 0;
1477    Int4 iEnd = 0;
1478 #ifdef _DEBUG_1
1479 printf("in AssignSurfaceContents\n ");
1480 #endif
1481    switch (pcgpContents->choice)
1482      {
1483          case ChemGraphPntrs_atoms:
1484            ppaThis = (AtomPntrsPtr) pcgpContents->data.ptrvalue;
1485            pvnMol = ppaThis->molecule_ids;
1486            pvnRes = ppaThis->residue_ids;
1487            pvnAtoms = ppaThis->atom_ids;
1488            while (pvnAtoms)
1489              {
1490                  pmadThis = AtomFromMMDBIndex(pdnmsList, (Int2) pdnmsList->choice,
1491                           (Int2) pvnMol->data.intvalue, (Int2) pvnRes->data.intvalue,
1492                           (Int4) pvnAtoms->data.intvalue);
1493                  if (pmadThis)
1494                    {
1495                        pvnThis = ValNodeAddPointer(&pmadThis->pvnContainedBy,  iModel, (Pointer) pfbThis);
1496                        if (!pvnThis) return FALSE;
1497                    }
1498                  else return FALSE;
1499                  if (pfbThis->bMe == AM_MOD)
1500                    {
1501                        pmodThis = (PMOD) pfbThis;
1502                        pvnThis = ValNodeAddPointer(&pmodThis->pvnContains,  ChemGraphPntrs_atoms, (Pointer) pmadThis);
1503                    }
1504                  if (pfbThis->bMe == AM_MDD)
1505                    {
1506                        pmddThis = (PMDD) pfbThis;
1507                        pvnThis = ValNodeAddPointer(&pmddThis->pvnContains, ChemGraphPntrs_atoms, (Pointer) pmadThis);
1508                    }
1509                  pvnAtoms = pvnAtoms->next;
1510                  pvnMol = pvnMol->next;
1511                  pvnRes = pvnRes->next;
1512              }
1513           return TRUE;
1514          case ChemGraphPntrs_residues:
1515            pprThis = (ResiduePntrsPtr) pcgpContents->data.ptrvalue;
1516            if (pprThis->choice == ResiduePntrs_explicit)
1517              {
1518 
1519                  ppreThis = (ResidueExplicitPntrsPtr) pprThis->data.ptrvalue;
1520                  pvnMol = ppreThis->molecule_ids;
1521                  pvnRes = ppreThis->residue_ids;
1522 
1523                  while (pvnRes)
1524                    {
1525                     pmgdThis = GraphFromMMDBIndex(pdnmsList, (Int2) pdnmsList->choice,
1526                           (Int2) pvnMol->data.intvalue, (Int2) pvnRes->data.intvalue);
1527                     if (pmgdThis)
1528                         {
1529                             pvnThis = ValNodeAddPointer(&pmgdThis->pvnContainedBy,  iModel, (Pointer) pfbThis);
1530                             if (!pvnThis) return FALSE;
1531                         }
1532                     else return FALSE;
1533                     if (pfbThis->bMe == AM_MOD)
1534                         {
1535                             pmodThis = (PMOD) pfbThis;
1536                             pvnThis = ValNodeAddPointer(&pmodThis->pvnContains,  ChemGraphPntrs_residues, (Pointer) pmgdThis);
1537                         }
1538                     if (pfbThis->bMe == AM_MDD)
1539                         {
1540                             pmddThis = (PMDD) pfbThis;
1541                             pvnThis = ValNodeAddPointer(&pmddThis->pvnContains, ChemGraphPntrs_residues, (Pointer) pmgdThis);
1542                         }
1543                        pvnMol = pvnMol->next;
1544                        pvnRes = pvnRes->next;
1545                    }
1546 
1547                  return TRUE;
1548              }
1549            if (pprThis->choice == ResiduePntrs_interval)
1550              {
1551                 ppriThis = (ResidueIntervalPntrPtr) pprThis->data.ptrvalue;
1552                    while (ppriThis)
1553                      {
1554                         iStart = ppriThis->from;
1555                         iEnd = ppriThis->to;
1556                         if (iStart > ppriThis->to)
1557                           {
1558                               iStart = ppriThis->to;
1559                               iEnd = ppriThis->from;
1560                           }
1561                         for (;iStart <= iEnd; iStart++)
1562                           {
1563                             pmgdThis = GraphFromMMDBIndex(pdnmsList, (Int2) pdnmsList->choice,
1564                                 (Int2) ppriThis->molecule_id, (Int2) iStart);
1565                             if (pmgdThis)
1566                               {
1567                                 pvnThis = ValNodeAddPointer(&pmgdThis->pvnContainedBy,  iModel, (Pointer) pfbThis);
1568                                 if (!pvnThis) return FALSE;
1569                               }
1570                             else return FALSE;
1571                             if (pfbThis->bMe == AM_MOD)
1572                               {
1573                                 pmodThis = (PMOD) pfbThis;
1574                                 pvnThis = ValNodeAddPointer(&pmodThis->pvnContains,  ChemGraphPntrs_residues, (Pointer) pmgdThis);
1575                               }
1576                             if (pfbThis->bMe == AM_MDD)
1577                               {
1578                                 pmddThis = (PMDD) pfbThis;
1579                                 pvnThis = ValNodeAddPointer(&pmddThis->pvnContains, ChemGraphPntrs_residues, (Pointer) pmgdThis);
1580                               }
1581                           } /* for start to end of interval */
1582 
1583                        ppriThis = ppriThis->next;
1584                      }  /* while intervals */
1585                 return TRUE;
1586              }
1587           return FALSE;
1588          case ChemGraphPntrs_molecules:
1589           ppmThis = (MoleculePntrsPtr) pcgpContents->data.ptrvalue;
1590           pvnMol = ppmThis->molecule_ids;
1591             while (pvnMol)
1592               {
1593                  pmmdThis = MolFromMMDBIndex(pdnmsList, (Int2) pdnmsList->choice,
1594                           (Int2) pvnMol->data.intvalue);
1595                  if (pmmdThis)
1596                    {
1597                        pvnThis = ValNodeAddPointer(&pmmdThis->pvnContainedBy,  iModel, (Pointer) pfbThis);
1598                        if (!pvnThis) return FALSE;
1599                    }
1600                  else return FALSE;
1601                  if (pfbThis->bMe == AM_MOD)
1602                    {
1603                        pmodThis = (PMOD) pfbThis;
1604                        pvnThis = ValNodeAddPointer(&pmodThis->pvnContains,  ChemGraphPntrs_molecules, (Pointer) pmmdThis);
1605                    }
1606                  if (pfbThis->bMe == AM_MDD)
1607                    {
1608                        pmddThis = (PMDD) pfbThis;
1609                        pvnThis = ValNodeAddPointer(&pmddThis->pvnContains, ChemGraphPntrs_molecules, (Pointer) pmmdThis);
1610                    }
1611                   pvnMol = pvnMol->next;
1612               }
1613           return TRUE;
1614      }
1615    return FALSE;
1616 }
1617 
1618 
1619 
1620 
1621 
1622 void LIBCALL FreeSurfaceModelAsnList(SurfaceCoordinatesPtr pscThis)
1623 {
1624 
1625     TMeshPtr ptmshThis = NULL;
1626     TrianglesPtr ptriThis = NULL;
1627     ValNodePtr pvnX = NULL;
1628 
1629  if (!pscThis) return;
1630  if (!pscThis->Surface_surface) return;
1631 #ifdef _DEBUG_1
1632 printf("in FreeSurfaceModelAsnList\n");
1633 #endif
1634  switch (pscThis->Surface_surface->choice)
1635       {
1636            case Surface_surface_cylinder:
1637            case Surface_surface_brick:
1638            case Surface_surface_sphere:
1639            case Surface_surface_cone:
1640               return;  /* nothing to free */
1641            case Surface_surface_tmesh:
1642              ptmshThis = (TMeshPtr) pscThis->Surface_surface->data.ptrvalue;
1643              pvnX = ValNodeFree(ptmshThis->x);
1644              ptmshThis->x = NULL;
1645              pvnX = ValNodeFree(ptmshThis->y);
1646              ptmshThis->y = NULL;
1647              pvnX = ValNodeFree(ptmshThis->z);
1648              ptmshThis->z = NULL;
1649              pvnX = ValNodeFree(ptmshThis->swap);
1650              ptmshThis->swap = NULL;
1651              return;
1652            case Surface_surface_triangles:
1653              ptriThis = (TrianglesPtr) pscThis->Surface_surface->data.ptrvalue;
1654              pvnX = ValNodeFree(ptriThis->x);
1655              ptriThis->x = NULL;
1656              pvnX = ValNodeFree(ptriThis->y);
1657              ptriThis->y = NULL;
1658              pvnX = ValNodeFree(ptriThis->z);
1659              ptriThis->z = NULL;
1660              pvnX = ValNodeFree(ptriThis->v1);
1661              ptriThis->v1 = NULL;
1662              pvnX = ValNodeFree(ptriThis->v2);
1663              ptriThis->v2 = NULL;
1664              pvnX = ValNodeFree(ptriThis->v3);
1665              ptriThis->v3 = NULL;
1666              return;
1667            default:
1668              return;
1669        }
1670 }
1671 
1672 
1673 Boolean LIBCALL FillSurface(SurfaceCoordinatesPtr pscThis, PMOD pmodThis)
1674 {
1675 
1676     CylinderPtr pcylThis = NULL;
1677     BrickPtr pbrkThis = NULL;
1678     ConePtr pconThis = NULL;
1679     TMeshPtr ptmshThis = NULL;
1680     TrianglesPtr ptriThis = NULL;
1681     SpherePtr psphThis = NULL;
1682     ModelSpacePointPtr pmspTop = NULL;
1683     ModelSpacePointPtr pmspBottom = NULL;
1684     ModelSpacePointPtr pmspTemp = NULL;
1685     RealValuePtr prvRadius = NULL;
1686     ValNodePtr pvnSwap = NULL;
1687     ValNodePtr pvnX = NULL;
1688     ValNodePtr pvnY = NULL;
1689     ValNodePtr pvnZ = NULL;
1690     Int4 iCount = 0;
1691     Boolean rsult = FALSE;
1692 
1693 #ifdef _DEBUG_1
1694 printf("in FillSurface=");
1695 #endif
1696     if (!pscThis) return FALSE;
1697     if (!pscThis->Surface_surface) return FALSE;
1698 
1699     ProgMon("Instantiating Surface");
1700     switch (pscThis->Surface_surface->choice)
1701       {
1702            case Surface_surface_cylinder:
1703 #ifdef _DEBUG_2
1704 printf("cylinder\n ");
1705 #endif
1706              pmodThis->bWhat = (Byte)(pmodThis->bWhat | (Byte) OBJ_CYLINDER);
1707              pcylThis = (CylinderPtr) pscThis->Surface_surface->data.ptrvalue;
1708              pmodThis->ppflObject = FLMatrix(0, 1, 0, 2); /* 2 by 3 matrix */
1709              if (!pmodThis->ppflObject) return FALSE;
1710              pmodThis->iCoordNo = 2;
1711              pmspTop = pcylThis->axis_top;
1712              pmspBottom = pcylThis->axis_bottom;
1713              prvRadius = pcylThis->radius;
1714              pmodThis->ppflObject[0][0] = (FloatLo)((FloatLo)pmspTop->x/pmspTop->scale_factor);
1715              pmodThis->ppflObject[0][1] = (FloatLo)((FloatLo)pmspTop->y/pmspTop->scale_factor);
1716              pmodThis->ppflObject[0][2] = (FloatLo)((FloatLo)pmspTop->z/pmspTop->scale_factor);
1717              pmodThis->ppflObject[1][0] = (FloatLo)((FloatLo)pmspBottom->x/pmspBottom->scale_factor);
1718              pmodThis->ppflObject[1][1] = (FloatLo)((FloatLo)pmspBottom->y/pmspBottom->scale_factor);
1719              pmodThis->ppflObject[1][2] = (FloatLo)((FloatLo)pmspBottom->z/pmspBottom->scale_factor);
1720              pmodThis->flRadius = (FloatLo)((FloatLo)prvRadius->scaled_integer_value/prvRadius->scale_factor);
1721              return TRUE;
1722            case Surface_surface_brick:
1723 #ifdef _DEBUG_2
1724 printf("brick\n ");
1725 #endif  
1726              pmodThis->bWhat = (Byte)(pmodThis->bWhat | (Byte) OBJ_BRICK);
1727              pbrkThis = (BrickPtr) pscThis->Surface_surface->data.ptrvalue;
1728              pmodThis->ppflObject = FLMatrix(0, 7, 0, 2); /* 8 by 3 matrix */
1729              if (!pmodThis->ppflObject) return FALSE;
1730              pmodThis->iCoordNo = 8;
1731              pmspTemp = pbrkThis->corner_000;
1732              pmodThis->ppflObject[0][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1733              pmodThis->ppflObject[0][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1734              pmodThis->ppflObject[0][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1735              pmspTemp = pbrkThis->corner_001;
1736              pmodThis->ppflObject[1][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1737              pmodThis->ppflObject[1][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1738              pmodThis->ppflObject[1][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1739              pmspTemp = pbrkThis->corner_010;
1740              pmodThis->ppflObject[2][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1741              pmodThis->ppflObject[2][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1742              pmodThis->ppflObject[2][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1743              pmspTemp = pbrkThis->corner_011;
1744              pmodThis->ppflObject[3][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1745              pmodThis->ppflObject[3][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1746              pmodThis->ppflObject[3][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1747              pmspTemp = pbrkThis->corner_100;
1748              pmodThis->ppflObject[4][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1749              pmodThis->ppflObject[4][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1750              pmodThis->ppflObject[4][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1751              pmspTemp = pbrkThis->corner_101;
1752              pmodThis->ppflObject[5][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1753              pmodThis->ppflObject[5][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1754              pmodThis->ppflObject[5][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1755              pmspTemp = pbrkThis->corner_110;
1756              pmodThis->ppflObject[6][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1757              pmodThis->ppflObject[6][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1758              pmodThis->ppflObject[6][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1759              pmspTemp = pbrkThis->corner_111;
1760              pmodThis->ppflObject[7][0] = (FloatLo)((FloatLo)pmspTemp->x/pmspTemp->scale_factor);
1761              pmodThis->ppflObject[7][1] = (FloatLo)((FloatLo)pmspTemp->y/pmspTemp->scale_factor);
1762              pmodThis->ppflObject[7][2] = (FloatLo)((FloatLo)pmspTemp->z/pmspTemp->scale_factor);
1763              return TRUE;
1764            case Surface_surface_sphere:
1765 #ifdef _DEBUG_2
1766 printf("sphere\n ");
1767 #endif               
1768              pmodThis->bWhat = (Byte)(pmodThis->bWhat | (Byte) OBJ_SPHERE);
1769              psphThis = (SpherePtr) pscThis->Surface_surface->data.ptrvalue;
1770              pmodThis->ppflObject = FLMatrix(0, 1, 0, 1); /* 1 by 3 matrix */
1771              if (!pmodThis->ppflObject) return FALSE;
1772              pmodThis->iCoordNo = 1;
1773              pmspTop = psphThis->center;
1774              prvRadius = psphThis->radius;
1775              pmodThis->ppflObject[0][0] = (FloatLo)((FloatLo)pmspTop->x/pmspTop->scale_factor);
1776              pmodThis->ppflObject[0][1] = (FloatLo)((FloatLo)pmspTop->y/pmspTop->scale_factor);
1777              pmodThis->ppflObject[0][2] = (FloatLo)((FloatLo)pmspTop->z/pmspTop->scale_factor);
1778              pmodThis->flRadius = (FloatLo)((FloatLo)prvRadius->scaled_integer_value/prvRadius->scale_factor);
1779              return TRUE;
1780            case Surface_surface_cone:
1781 #ifdef _DEBUG_2
1782 printf("cone\n ");
1783 #endif  
1784              pmodThis->bWhat = (Byte)(pmodThis->bWhat | (Byte) OBJ_CONE);
1785              pconThis = (ConePtr) pscThis->Surface_surface->data.ptrvalue;
1786              pmodThis->ppflObject = FLMatrix(0, 1, 0, 2); /* 2 by 3 matrix */
1787              if (!pmodThis->ppflObject) return FALSE;
1788              pmodThis->iCoordNo = 2;
1789              pmspTop = pconThis->axis_top;
1790              pmspBottom = pconThis->axis_bottom;
1791              prvRadius = pconThis->radius_bottom;
1792              pmodThis->ppflObject[0][0] = (FloatLo)((FloatLo)pmspTop->x/pmspTop->scale_factor);
1793              pmodThis->ppflObject[0][1] = (FloatLo)((FloatLo)pmspTop->y/pmspTop->scale_factor);
1794              pmodThis->ppflObject[0][2] = (FloatLo)((FloatLo)pmspTop->z/pmspTop->scale_factor);
1795              pmodThis->ppflObject[1][0] = (FloatLo)((FloatLo)pmspBottom->x/pmspBottom->scale_factor);
1796              pmodThis->ppflObject[1][1] = (FloatLo)((FloatLo)pmspBottom->y/pmspBottom->scale_factor);
1797              pmodThis->ppflObject[1][2] = (FloatLo)((FloatLo)pmspBottom->z/pmspBottom->scale_factor);
1798              pmodThis->flRadius = (FloatLo)((FloatLo)prvRadius->scaled_integer_value/prvRadius->scale_factor);
1799              return TRUE;
1800            case Surface_surface_tmesh:
1801 #ifdef _DEBUG_2
1802 printf("tmesh\n ");
1803 #endif  
1804              pmodThis->bWhat = (Byte)(pmodThis->bWhat | (Byte) OBJ_TMESH);
1805              ptmshThis = (TMeshPtr) pscThis->Surface_surface->data.ptrvalue;
1806              pmodThis->ppflObject = FLMatrix(0, ptmshThis->number_of_points, 0, 2); /* N by 3 matrix */
1807              if (!pmodThis->ppflObject) return FALSE;
1808              pmodThis->pucSwap = UCVector(0, ptmshThis->number_of_points);  /* N vector for swap flags */
1809              if (!pmodThis->pucSwap) return FALSE;
1810              pvnX = ptmshThis->x;
1811              pvnY = ptmshThis->y;
1812              pvnZ = ptmshThis->z;
1813              pvnSwap = ptmshThis->swap;
1814              iCount = 0;
1815              while (pvnX)
1816                 {
1817                     ProgMon("Instantiating TMesh");
1818                     pmodThis->ppflObject[iCount][0] = (FloatLo)
1819                                 ((FloatLo)pvnX->data.intvalue/ptmshThis->scale_factor);
1820                     pmodThis->ppflObject[iCount][1] = (FloatLo)
1821                                 ((FloatLo)pvnY->data.intvalue/ptmshThis->scale_factor);
1822                     pmodThis->ppflObject[iCount][2] = (FloatLo)
1823                                 ((FloatLo)pvnZ->data.intvalue/ptmshThis->scale_factor);
1824                     if (pvnSwap->data.boolvalue) pmodThis->pucSwap[iCount] = (Uchar) 1;
1825                     iCount++;
1826                     pvnX = pvnX->next;
1827                     pvnY = pvnY->next;
1828                     pvnZ = pvnZ->next;
1829                     pvnSwap = pvnSwap->next;
1830                 }
1831              pmodThis->iCoordNo = iCount;
1832              FreeSurfaceModelAsnList(pscThis);
1833              return TRUE;
1834            case Surface_surface_triangles:
1835 #ifdef _DEBUG_2
1836 printf("triangles\n ");
1837 #endif  
1838              pmodThis->bWhat = (Byte)(pmodThis->bWhat | (Byte) OBJ_TRIANGLES);
1839              ptriThis = (TrianglesPtr) pscThis->Surface_surface->data.ptrvalue;
1840              pmodThis->ppflObject = FLMatrix(0, ptriThis->number_of_points, 0, 2); /* N by 3 matrix */
1841              if (!pmodThis->ppflObject) return FALSE;
1842              pmodThis->ppi4Triangles = I4Matrix(0, ptriThis->number_of_triangles,0,2);  /* N by 3 matrix */
1843              if (!pmodThis->ppi4Triangles) return FALSE;
1844              pvnX = ptriThis->x;
1845              pvnY = ptriThis->y;
1846              pvnZ = ptriThis->z;
1847              iCount = 0;
1848              while (pvnX)
1849                 {
1850                     ProgMon("Instantiating Triangles");
1851                     pmodThis->ppflObject[iCount][0] = (FloatLo)
1852                                 ((FloatLo)pvnX->data.intvalue/ptriThis->scale_factor);
1853                     pmodThis->ppflObject[iCount][1] = (FloatLo)
1854                                 ((FloatLo)pvnY->data.intvalue/ptriThis->scale_factor);
1855                     pmodThis->ppflObject[iCount][2] = (FloatLo)
1856                                 ((FloatLo)pvnZ->data.intvalue/ptriThis->scale_factor);
1857                     iCount++;
1858                     pvnX = pvnX->next;
1859                     pvnY = pvnY->next;
1860                     pvnZ = pvnZ->next;
1861                 }
1862              pmodThis->iCoordNo = iCount;
1863              iCount = 0;
1864              pvnX = ptriThis->v1;
1865              pvnY = ptriThis->v2;
1866              pvnZ = ptriThis->v3;
1867              while (pvnX)
1868                 {
1869                     pmodThis->ppi4Triangles[iCount][0] = (Int4) pvnX->data.intvalue;   
1870                     pmodThis->ppi4Triangles[iCount][1] = (Int4) pvnY->data.intvalue;   
1871                     pmodThis->ppi4Triangles[iCount][2] = (Int4) pvnZ->data.intvalue;   
1872                     iCount++;
1873                     pvnX = pvnX->next;
1874                     pvnY = pvnY->next;
1875                     pvnZ = pvnZ->next;  
1876                 }
1877             pmodThis->iTriNo = iCount;
1878             FreeSurfaceModelAsnList(pscThis);
1879             return TRUE;
1880         
1881          default:
1882              rsult = FALSE;
1883        }
1884     return rsult;   
1885 }
1886 
1887 
1888  
1889   
1890 
1891 
1892 Int2 LIBCALL BiostrucModel2ModelStruc(BiostrucModelPtr pbsmThis, PDNMS pdnmsThis, 
1893                                 Int2 iChoice, Int2 iModelCount, Byte bExtent)
1894 {
1895     PMAD pmadThis = NULL;
1896     PVNMO pvnmoThis = NULL;
1897     PVNMD pvnmdThis = NULL;
1898     PVNAL pvnalThis = NULL;
1899     PVNAL pvnalTemp = NULL;
1900     PALD paldThis = NULL;
1901     PALD paldTemp = NULL;
1902     PMOD pmodThis = NULL;
1903     PMDD pmddThis = NULL;
1904     PMLD pmldThis = NULL;
1905     PMLD pmldFirst = NULL;
1906     PDNML pdnmlModel = NULL;
1907     PMSD pmsdThis = NULL;
1908     
1909     
1910             
1911     IsotropicTemperatureFactorsPtr pitfThis = NULL;
1912     AnisotropicTemperatureFactorsPtr patfThis = NULL;
1913     AlternateConformationIdsPtr paciThis = NULL;
1914     AtomicOccupanciesPtr pocThis = NULL;
1915     ModelSpacePointsPtr pmspThis = NULL;
1916                 
1917     AtomicCoordinatesPtr pacThis = NULL;
1918     AtomPntrsPtr pappThis = NULL;
1919     CoordinatesPtr pcoThis = NULL;
1920     ModelCoordinateSetPtr pmcsThis = NULL;
1921     SurfaceCoordinatesPtr  pscThis = NULL;
1922     DensityCoordinatesPtr pdcThis = NULL; 
1923      
1924     CharPtr pcThis = NULL;
1925     CharPtr pcTemp = NULL;
1926     ValNodePtr pvnX = NULL;
1927     ValNodePtr pvnY = NULL;
1928     ValNodePtr pvnZ = NULL;
1929     ValNodePtr pvnB11 = NULL;
1930     ValNodePtr pvnB12 = NULL;
1931     ValNodePtr pvnB13 = NULL;
1932     ValNodePtr pvnB22 = NULL;
1933     ValNodePtr pvnB23 = NULL;
1934     ValNodePtr pvnB33 = NULL;
1935     ValNodePtr pvnOcc = NULL;
1936     ValNodePtr pvnAlt = NULL;
1937     ValNodePtr pvnMid = NULL;
1938     ValNodePtr pvnRid = NULL;
1939     ValNodePtr pvnAid = NULL;
1940     ValNodePtr pmdrThis = NULL;
1941     Int4 iCountAtoms = 0;
1942     Int4 iCounter = 0;
1943     Int4 iVectLen = 0;
1944     Int4 iCoordScale = 0;
1945     Int4 iCoordSets = 0;
1946     Int4 iOccScale = 0;
1947     Int4 iTempScale = 0;
1948     CharPtr pcTest = NULL;
1949     Byte bAnisotropy = FALSE;
1950     Byte bAltConf = FALSE;
1951     Boolean bFirstCoordSet = TRUE;
1952            
1953 #ifdef _DEBUG_1
1954      printf("Making Model #%d %d \n",iModelCount,  (Int2)bExtent);
1955 #endif    
1956     pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
1957     pmldFirst = NewMLD();
1958     if (!pmldFirst) goto allocerr;
1959     pmldFirst->iType = pbsmThis->type;
1960     pmldFirst->iId = pbsmThis->id;
1961     pmldFirst->bSelected = (Byte) 0x01; /* for saving/conversion */
1962     pmldFirst->pbsmModel = pbsmThis;  /* point to the entire model */
1963     pmldFirst->pmsdParent = (PMSD) pdnmsThis->data.ptrvalue;
1964     pmdrThis = ValNodeFindNext(pbsmThis->descr, NULL, ModelDescr_name);
1965     if (pmdrThis)
1966       {
1967           pmldFirst->pcModelName = StringSave((CharPtr) pmdrThis->data.ptrvalue); 
1968       }
1969     
1970     pmcsThis = pbsmThis->model_coordinates;
1971     while  (pmcsThis)  /* walk the model-coordinate sets - flattened and instantiated */
1972       {  
1973          bAnisotropy = FALSE;
1974          bAltConf = FALSE;
1975          iCoordSets++;
1976 #ifdef _DEBUG_1
1977 printf("Model coord set %d\n ", iCoordSets);
1978 #endif          
1979         if (!bFirstCoordSet)
1980           {
1981               pmldThis->next = NewMLD();
1982               if (!pmldThis->next) goto allocerr;
1983               pmldThis = pmldThis->next;
1984               pmldThis->iType = pbsmThis->type;
1985               pmldThis->iId = pbsmThis->id;
1986               pmldThis->pbsmModel = pbsmThis;  /* point to the entire model */
1987               pmldThis->iNoCoordSet = pmcsThis->id;
1988               pmldThis->pmsdParent = (PMSD) pdnmsThis->data.ptrvalue;
1989    
1990           }
1991         else
1992           {
1993               pmldThis = pmldFirst;
1994               pmldThis->iNoCoordSet = pmcsThis->id;
1995           }
1996 #ifdef _DEBUG_1
1997 printf("Model Type %d\n ", pmldThis->iType);
1998 #endif   
1999         bFirstCoordSet = FALSE;
2000         /* NOT BY REFERENCE YET */
2001         if (pmcsThis->Coordinates_coordinates->choice == Coordinates_coordinates_reference) goto skipnext;
2002         /* rest are literals */
2003         
2004         pcoThis = (CoordinatesPtr) pmcsThis->Coordinates_coordinates->data.ptrvalue;
2005 
2006         /* read in Surface coordinate sets MOD */ 
2007         if (pcoThis->choice == Coordinates_surface)
2008           {
2009 #ifdef _DEBUG_3
2010 printf("surface\n");
2011 #endif 
2012              pmsdThis->iObjCount += 1;
2013              pvnmoThis = NewVNMO(NULL, iModelCount);
2014              if (!pvnmoThis) goto allocerr;
2015              pmodThis = (PMOD) pvnmoThis->data.ptrvalue;
2016              pmodThis->pfbParent = (PFB) pmsdThis;
2017              pscThis = (SurfaceCoordinatesPtr) pcoThis->data.ptrvalue;
2018              if (!(FillSurface(pscThis, pmodThis))) goto allocerr;
2019              if (!(AssignSurfaceContents(pdnmsThis, iModelCount, (PFB) pmodThis, pscThis->contents))) goto allocerr;     
2020              if (pmsdThis->pvnmoHead == NULL)
2021                {
2022                    pmsdThis->pvnmoHead = pvnmoThis;
2023                }
2024              else 
2025                ValNodeListCat(pmsdThis->pvnmoHead, pvnmoThis);
2026              pmodThis->pmldCoordSet = pmldThis;
2027              pmldThis->pmodObject = pmodThis;
2028              goto skipnext;
2029           }
2030           
2031 
2032         /* read in DENSITY coordinate sets MDD */
2033 
2034         if (pcoThis->choice == Coordinates_density)
2035           {
2036 #ifdef _DEBUG_3
2037 printf("density\n");
2038 #endif       
2039              pmsdThis->iDensCount += 1;
2040              pvnmdThis = NewVNMD(NULL, iModelCount);
2041              if (!pvnmdThis) goto allocerr;
2042              pmddThis = (PMDD) pvnmdThis->data.ptrvalue;
2043              pdcThis = (DensityCoordinatesPtr) pcoThis->data.ptrvalue;
2044                /* @@@@@@@@@@@@@@@@@@@@@@@convert the density object */
2045              if (!(AssignSurfaceContents(pdnmsThis, iModelCount,  (PFB) pmddThis, pdcThis->contents))) goto allocerr; 
2046               if (pmsdThis->pvnmdHead == NULL)
2047                {
2048                    pmsdThis->pvnmdHead = pvnmdThis;
2049                }
2050              else 
2051                ValNodeListCat(pmsdThis->pvnmdHead, pvnmdThis);
2052              pmddThis->pmldCoordSet = pmldThis;
2053              pmldThis->pmddDensity = pmddThis;
2054          
2055              goto skipnext;
2056           }
2057 
2058 
2059         /* NOTE that coordinates by REFERENCE are not yet supported */
2060 
2061         pacThis = NULL;
2062         pappThis = NULL;
2063         pmspThis = NULL;
2064         pocThis = NULL;
2065         paciThis = NULL;
2066         if ((pcoThis->choice == Coordinates_atomic) && (bExtent & (Byte) COORDS_DATA))
2067           {
2068              pacThis = (AtomicCoordinatesPtr) pcoThis->data.ptrvalue;
2069              pappThis = pacThis->atoms; /*  AtomPntrsPtr */        
2070              pmspThis =  pacThis->sites; /* ModelSpacePointsPtr */
2071              pocThis = pacThis->occupancies; /*  MolOccPtr */
2072              paciThis = pacThis->alternate_conf_ids; /* AlternateConformationIdsPtr */
2073              if ((pacThis->temperature_factors) && (bExtent & (Byte) TEMP_DATA))
2074                 {
2075 #ifdef _DEBUG_2
2076 printf("Isotropic Temperature factors \n");
2077 #endif  
2078                    if (pacThis->temperature_factors->choice == AtomicTemperatureFactors_isotropic)
2079                      {         
2080                       pitfThis = (IsotropicTemperatureFactorsPtr) pacThis->temperature_factors->data.ptrvalue;
2081                       if (pitfThis) iTempScale = pitfThis->scale_factor;
2082                       pvnB11 = pitfThis->b;
2083                      }
2084                    if (pacThis->temperature_factors->choice == AtomicTemperatureFactors_anisotropic)
2085                      {
2086                       patfThis = (AnisotropicTemperatureFactorsPtr) pacThis->temperature_factors->data.ptrvalue;
2087                       bAnisotropy = TRUE;     
2088                       if (patfThis) 
2089                         { 
2090 #ifdef _DEBUG_2
2091 printf("Anisotropic Temperature factors \n");
2092 #endif                           
2093                           iTempScale = patfThis->scale_factor;
2094                           pvnB11 = patfThis->b_11;
2095                           pvnB12 = patfThis->b_12;
2096                           pvnB13 = patfThis->b_13;
2097                           pvnB22 = patfThis->b_22;
2098                           pvnB23 = patfThis->b_23;
2099                           pvnB33 = patfThis->b_33;
2100                         }
2101                      }
2102                 }
2103              /* else deal with anisotropic ones */
2104           } /* if pcoThis */
2105         else
2106           {
2107               goto skipnext;
2108           }
2109 
2110      
2111         if (pmspThis)
2112           {
2113            pvnX = pmspThis->x;
2114            pvnY = pmspThis->y;
2115            pvnZ = pmspThis->z;
2116            iCoordScale = pmspThis->scale_factor;
2117 #ifdef _DEBUG_2
2118 printf("Coordinates / = %d\n", iCoordScale);
2119 #endif  
2120           } else goto erret;  
2121         if (pappThis)
2122           {
2123            pvnMid = pappThis->molecule_ids;
2124            pvnRid = pappThis->residue_ids;
2125            pvnAid = pappThis->atom_ids;
2126            iCountAtoms = pappThis->number_of_ptrs;
2127            
2128            
2129 #ifdef _DEBUG_2
2130 printf("%d ATOMS In Model\n", iCountAtoms);
2131 #endif             
2132           } else goto erret;   
2133         if ((pocThis) && (bExtent &  (Byte) OCC_DATA))
2134           {
2135            pvnOcc = pocThis->o;
2136            iOccScale = pocThis->scale_factor;
2137 #ifdef _DEBUG_2
2138 printf("Occupancies / %d\n", iOccScale);
2139 #endif   
2140           }
2141         if ((paciThis) && (bExtent & (Byte) ALTCNF_DATA))
2142           {
2143            pvnAlt = paciThis;
2144            bAltConf = TRUE;
2145 #ifdef _DEBUG_3
2146 printf("Alt conformations found \n");
2147 #endif          
2148           }
2149         if (iCoordScale) iVectLen = 2; /* 3 elements 0=X,1=Y,2=Z */
2150         if (iOccScale) iVectLen = 3; 
2151         if (iTempScale) iVectLen = 4; 
2152         if ((iTempScale) && bAnisotropy) iVectLen = 9;
2153 #ifdef _DEBUG_2
2154 printf("iVectLen = %d\n", iVectLen);
2155 #endif          
2156         if ((iCoordScale) && (pappThis && pmspThis)) /* minimum to do the atomic locations */
2157           {
2158              /* do all data collect in one walk-through the vectors to preserve atom loc data */
2159              /* the vector pmdlThis->ppAsnOrder has PALD pointers in it, which begin at       */
2160              /* ppAsnOrder[1] and end at ppAsnOrder[iCountAtoms], so ASN.1 indexing is not    */
2161              /* shifted by (-1) to match C indexing                                           */
2162              /* ppAsnOrder[iCountAtoms+1] is set to NULL as an end-of matrix indicator        */
2163            pmldThis->ppAsnOrder = PTRVector(0, iCountAtoms + 1);
2164            if (pmldThis->ppAsnOrder) /* this should not be fatal - only affects features */
2165              pmldThis->ppAsnOrder[iCountAtoms+1] = NULL;
2166            while (iCounter < iCountAtoms) 
2167             {
2168                 iCounter++;
2169                 ProgMon("Instantiating Atom Locations");
2170 #ifdef _DEBUG_3
2171 printf("[%d] ",iCounter);
2172 #endif                  
2173                 pmadThis = AtomFromMMDBIndex(pdnmsThis, iChoice, (Int2) pvnMid->data.intvalue,
2174                             (Int2) pvnRid->data.intvalue, (Int4) pvnAid->data.intvalue);
2175                 if (!pmadThis) goto erret;              
2176                 pvnalThis = NewVNAL(NULL, iModelCount); 
2177                 if (!pvnalThis) goto allocerr;
2178                 paldThis = (PALD) pvnalThis->data.ptrvalue;
2179                 paldThis->pfbParent = (PFB) pmadThis; /* back-link to pmad */
2180                 paldThis->pflvData = FLVector(0,(Int4) iVectLen); /* alloc the data vector */
2181                 if (!paldThis->pflvData) goto allocerr;
2182                 paldThis->iCoordSet = (Int1) pmcsThis->id;
2183                 paldThis->iFloatNo = (Int1) iVectLen; 
2184                 if (pmldThis->ppAsnOrder) pmldThis->ppAsnOrder[iCounter] = (Pointer) paldThis; /* add to order array */
2185                 if (pmadThis->pvnalLocate)  
2186                   {  /* the case where models are already present */
2187 #ifdef _DEBUG_3
2188 printf("{%d}", iModelCount);
2189 #endif                  
2190                    pvnalTemp = (PVNAL) ValNodeFindNext(pmadThis->pvnalLocate,NULL,iModelCount);
2191                    if (pvnalTemp)
2192                      { /* the case where THIS model has a location already */
2193 #ifdef _DEBUG_3
2194 printf("DISORDER!!\n");
2195 #endif                   
2196                         paldTemp = (PALD) pvnalTemp->data.ptrvalue;  
2197                         while (paldTemp->next)
2198                            {   /* find end of pald list */
2199                                paldTemp = paldTemp->next;
2200                            }
2201                         paldTemp->next = (PALD)pvnalThis->data.ptrvalue; /* attach the new location */
2202                         pvnalThis->data.ptrvalue = NULL;
2203                         pvnalThis = ValNodeFree(pvnalThis); /* lose the ValNode baggage */   
2204                      }
2205                    else
2206                    ValNodeListCat(pmadThis->pvnalLocate, pvnalThis); /* attach to pmadThis */
2207                   }
2208                 else
2209                   pmadThis->pvnalLocate = pvnalThis;
2210         
2211                 paldThis->pflvData[0] = (FloatLo)((FloatLo)pvnX->data.intvalue/iCoordScale);  
2212                 paldThis->pflvData[1] = (FloatLo)((FloatLo)pvnY->data.intvalue/iCoordScale); 
2213                 paldThis->pflvData[2] = (FloatLo)((FloatLo)pvnZ->data.intvalue/iCoordScale);
2214 #ifdef _DEBUG_3
2215 printf("%s  X= %f, Y= %f, Z= %f ", pmadThis->pcAName, paldThis->pflvData[0], 
2216                      paldThis->pflvData[1], paldThis->pflvData[2]);
2217 #endif                  
2218 
2219                 if (iOccScale) 
2220                    {
2221                      paldThis->pflvData[3] = (FloatLo)((FloatLo)pvnOcc->data.intvalue/iOccScale) ;
2222                      pvnOcc = pvnOcc->next;
2223 #ifdef _DEBUG_3
2224 printf(" Occ = %f  ", paldThis->pflvData[3]);
2225 #endif
2226                 
2227                    }
2228         else if(iVectLen >=3 ) paldThis->pflvData[3]  = 1.0;
2229                 if (bAltConf)
2230                    {
2231                      pcThis = (CharPtr) pvnAlt->data.ptrvalue;
2232                      if (pcThis)
2233                         {
2234                           paldThis->cAltConf = (Char) pcThis[0];
2235                           if (!pmldThis->pcAltConf)
2236                             {
2237                               pmldThis->pcAltConf = StringSave(pcThis);
2238                             }
2239                           else 
2240                             { /* no alt conf string yet */
2241                               pcTest = NULL;
2242                               pcTest = StringChr(pmldThis->pcAltConf,(Char) pcThis[0]); 
2243                               if (!(pcTest))
2244                                 { /* char not yet in pmldThis->pcAltConf */
2245 #ifdef _DEBUG_3               
2246 printf("\nNEW STRING\n");
2247 #endif                          
2248                                   pcTemp = (CharPtr) MemNew((size_t)(StringLen(pmldThis->pcAltConf)+2)); /* more room */
2249                                   StringCpy(pcTemp, pmldThis->pcAltConf);  /* copy old string */
2250                                   StringCat(pcTemp, pcThis); /* new letter added */
2251                                   MemFree(pmldThis->pcAltConf); /* lose old string */
2252                                  pmldThis->pcAltConf = pcTemp; /* new string */
2253                                 }
2254                             }
2255                         }
2256 #ifdef _DEBUG_3               
2257 printf(" <%s>pmldThis \n",pmldThis->pcAltConf);
2258 printf("%s-\n",pcThis);
2259 printf(" <%c>paldThis \n",(char) paldThis->cAltConf );
2260 #endif
2261                      pvnAlt = pvnAlt->next;
2262                    }
2263                 if (iTempScale)
2264                   {
2265                    if (bAnisotropy)
2266                     {
2267 #ifdef _DEBUG_3
2268 printf("%d %d %d %d %d %d", pvnB11->data.intvalue,  pvnB12->data.intvalue, 
2269                         pvnB13->data.intvalue, pvnB22->data.intvalue, 
2270                         pvnB23->data.intvalue, pvnB33->data.intvalue);
2271 #endif                        
2272                         paldThis->pflvData[5] = (FloatLo)((FloatLo)pvnB12->data.intvalue/iTempScale);
2273                         paldThis->pflvData[6] = (FloatLo)((FloatLo)pvnB13->data.intvalue/iTempScale);
2274                         paldThis->pflvData[7] = (FloatLo)((FloatLo)pvnB22->data.intvalue/iTempScale);
2275                         paldThis->pflvData[8] = (FloatLo)((FloatLo)pvnB23->data.intvalue/iTempScale);
2276                         paldThis->pflvData[9] = (FloatLo)((FloatLo)pvnB33->data.intvalue/iTempScale);
2277 #ifdef _DEBUG_3
2278 printf("B12=%f,  B13=%f,  B22=%f, B23=%f, B33=%f ", 
2279                         paldThis->pflvData[5], paldThis->pflvData[6], paldThis->pflvData[7], 
2280                         paldThis->pflvData[8], paldThis->pflvData[9] );
2281 #endif                  
2282                         pvnB12 = pvnB12->next;
2283                         pvnB13 = pvnB13->next;
2284                         pvnB22 = pvnB22->next;
2285                         pvnB23 = pvnB23->next;
2286                         pvnB33 = pvnB33->next;
2287                     }
2288                    paldThis->pflvData[4] = (FloatLo)((FloatLo)pvnB11->data.intvalue/iTempScale);
2289 #ifdef _DEBUG_3
2290 printf("B11=%f ", paldThis->pflvData[4]);
2291 #endif          
2292                    pvnB11 = pvnB11->next;
2293                   } /* if iTempScale */
2294 #ifdef _DEBUG_3
2295 printf("\n");
2296 #endif             
2297                 pvnX = pvnX->next;
2298                 pvnY = pvnY->next;
2299                 pvnZ = pvnZ->next;
2300                 pvnMid = pvnMid->next;
2301                 pvnRid = pvnRid->next;
2302                 pvnAid = pvnAid->next;
2303            }   /* while (iCounter < iCountAtoms) */
2304 
2305          } /* if pmspThis && pappThis */
2306          
2307     skipnext:   
2308 #ifdef _DEBUG_3
2309 printf("skipnext");
2310 #endif 
2311         
2312          /* free the model data leaving the ASN.1 stub */
2313        if (pacThis) FreeAtomicModelAsnLists(pacThis);
2314 
2315        pmcsThis = pmcsThis->next;       
2316     } /* while pmcsThis - do next coordinate set*/      
2317       
2318     pdnmlModel = DValNodeAddPointer(&pmsdThis->pdnmlModels, iModelCount, (Nlm_VoidPtr) pmldFirst);
2319     if (!pdnmlModel) goto allocerr;
2320          
2321    return 1;
2322 
2323    allocerr:
2324              ErrClear(); 
2325              ErrPostEx(SEV_ERROR,0,0, "Out Of Memory While Loading Model\nTry loading smaller or fewer Models");
2326              ErrShow();
2327              FreeMLD(pmldFirst);
2328              return 0;
2329  
2330    erret: 
2331         ErrClear(); 
2332         ErrPostEx(SEV_ERROR,0,0, "Internal Indexing Failure in ASN.1\nCheck ASN.1 Input for integrity");
2333         ErrShow();
2334         FreeMLD(pmldFirst);
2335         return -1;
2336 }
2337 
2338 
2339 
2340 ValNodePtr LIBCALL MakeChemGraphNodeList(PDNMS pdnmsThis,  ChemGraphPntrsPtr pcgpThis)
2341 {
2342   
2343     ValNodePtr pvnThis = NULL;
2344     ValNodePtr pvnLocList = NULL;
2345     PFB pfbThis = NULL;
2346     ResiduePntrsPtr prpsThis = NULL;
2347     ResidueExplicitPntrsPtr prepThis = NULL;
2348     ResidueIntervalPntrPtr pripThis = NULL;
2349     AtomPntrsPtr pAtoms = NULL;
2350     MoleculePntrsPtr pMolecules = NULL;
2351     ValNodePtr pvnAtom = NULL;
2352     ValNodePtr pvnMol = NULL;
2353     ValNodePtr pvnRes = NULL;
2354     Int4 iCountPtrs = 0;
2355     Int4 iStart = 0;
2356     Int4 iEnd = 0;
2357  
2358     if (!pdnmsThis) return NULL;
2359     if (!pcgpThis) return NULL;
2360     if (pcgpThis && pdnmsThis)
2361      {
2362        switch (pcgpThis->choice) 
2363          {
2364             case ChemGraphPntrs_atoms:
2365              pAtoms = (AtomPntrsPtr) pcgpThis->data.ptrvalue;
2366              if (!pAtoms) return NULL;
2367              pvnMol = pAtoms->molecule_ids;
2368              pvnRes = pAtoms->residue_ids;
2369              pvnAtom = pAtoms->atom_ids;        
2370              if (pAtoms->number_of_ptrs == 0) return NULL;
2371              for (iCountPtrs = 0; iCountPtrs < pAtoms->number_of_ptrs; iCountPtrs++)
2372                {
2373                    pfbThis = NULL;
2374                    pfbThis = (PFB) AtomFromMMDBIndex(pdnmsThis, (Int2) pdnmsThis->choice, 
2375                                       (Int2) pvnMol->data.intvalue, 
2376                                       (Int2) pvnRes->data.intvalue, 
2377                                       (Int4) pvnAtom->data.intvalue);
2378                    if (pfbThis)
2379                        {
2380                           pvnThis = ValNodeAddPointer(&pvnLocList, 
2381                                                 0, (Nlm_VoidPtr) pfbThis);
2382                        }                      
2383                    pvnMol = pvnMol->next;
2384                    pvnRes = pvnRes->next;
2385                    pvnAtom = pvnAtom->next;
2386                }   
2387             break;
2388             
2389           case ChemGraphPntrs_residues:
2390              prpsThis = (ResiduePntrsPtr) pcgpThis->data.ptrvalue;
2391              if (!prpsThis) return NULL;
2392              if (prpsThis->choice == ResiduePntrs_explicit)
2393                {
2394                   prepThis = (ResidueExplicitPntrsPtr) prpsThis->data.ptrvalue; 
2395                   pvnMol = prepThis->molecule_ids;
2396                   pvnRes = prepThis->residue_ids;                
2397                   while (pvnRes)
2398                    {
2399                      pfbThis = NULL;
2400                      pfbThis = (PFB) GraphFromMMDBIndex(pdnmsThis, (Int2) pdnmsThis->choice, 
2401                           (Int2) pvnMol->data.intvalue, (Int2) pvnRes->data.intvalue);
2402                      if (pfbThis)
2403                        {
2404                           pvnThis = ValNodeAddPointer(&pvnLocList, 
2405                                                 0, (Nlm_VoidPtr) pfbThis);
2406                        }
2407                      else return NULL;
2408                      pvnMol = pvnMol->next;
2409                      pvnRes = pvnRes->next;
2410                    }
2411                 }  /* explicit */
2412              if (prpsThis->choice == ResiduePntrs_interval)
2413                {
2414                    pripThis = (ResidueIntervalPntrPtr) prpsThis->data.ptrvalue;
2415                    while (pripThis)
2416                      {
2417                         iStart = pripThis->from;
2418                         iEnd = pripThis->to;
2419                         if (iStart > pripThis->to)
2420                           {
2421                               iStart = pripThis->to;
2422                               iEnd = pripThis->from;
2423                           }
2424                         for (;iStart <= iEnd; iStart++)
2425                           {  
2426                              pfbThis = NULL;
2427                              pfbThis = (PFB) GraphFromMMDBIndex(pdnmsThis, (Int2) pdnmsThis->choice, 
2428                                 (Int2) pripThis->molecule_id, (Int2) iStart);
2429                              if (pfbThis)
2430                                {
2431                                   pvnThis = ValNodeAddPointer(&pvnLocList, 
2432                                                 0, (Nlm_VoidPtr) pfbThis);
2433                                }
2434                               else return NULL;
2435                           }
2436                         pripThis = pripThis->next;
2437                      } /* while pripThis */
2438                } /* interval */              
2439             break;
2440         
2441           case ChemGraphPntrs_molecules:
2442              pMolecules = (MoleculePntrsPtr) pcgpThis->data.ptrvalue;
2443              if (!pMolecules)  return NULL;
2444              pvnMol = pMolecules->molecule_ids;
2445              if (pMolecules->number_of_ptrs == 0) return NULL;
2446              for (iCountPtrs = 0; iCountPtrs < pMolecules->number_of_ptrs; iCountPtrs++)
2447                {
2448                    pfbThis = NULL;
2449                    pfbThis = (PFB) MolFromMMDBIndex(pdnmsThis, (Int2) pdnmsThis->choice, 
2450                                       (Int2) pvnMol->data.intvalue);
2451                    if (pfbThis)
2452                        {
2453                           pvnThis = ValNodeAddPointer(&pvnLocList, 
2454                                                 0, (Nlm_VoidPtr) pfbThis);
2455                        }                      
2456                    pvnMol = pvnMol->next;
2457                }      
2458             break;
2459             
2460           default: return NULL;
2461          }
2462 
2463     }  /* if pcgpThis && pmsdThis*/
2464 
2465   return pvnLocList;
2466 
2467 }
2468      
2469  
2470 
2471 Int2 SortOn=0;
2472 
2473 PDNTRN LIBCALL NewDNTRN(PDNTRN PNTR ppdntrnList,  Int2 choice)
2474 {
2475     PDNTRN pdntrnNew = NULL;
2476     FloatLoPtr pflvNew = NULL;
2477     FloatLoPtrPtr ppflmNew = NULL;
2478     
2479     if (!choice) return NULL;
2480     if ((int) choice == Move_translate)
2481       {
2482           pflvNew = FLVector(0, 2); 
2483           if (!pflvNew) return NULL;
2484           pdntrnNew = DValNodeAddPointer(ppdntrnList, choice, (Nlm_VoidPtr) pflvNew);
2485       }
2486     if ((int) choice == Move_rotate)
2487       {
2488           ppflmNew = FLMatrix(0, 2, 0, 2);
2489           if (!ppflmNew) return NULL;
2490           pdntrnNew = DValNodeAddPointer(ppdntrnList, choice, (Nlm_VoidPtr) ppflmNew);
2491       }
2492     return pdntrnNew;  
2493 }
2494 
2495 
2496 void LIBCALL FreeDNTRN(PDNTRN pdntrnList)
2497 {
2498     PDNTRN pdntrnThis;
2499     PDNTRN pdntrnNext;
2500     FloatLoPtr pflv = NULL;
2501     FloatLoPtrPtr ppflm = NULL;
2502   
2503     pdntrnThis = pdntrnList;
2504     while(pdntrnThis)
2505      {
2506        pdntrnNext = pdntrnThis->next;
2507        switch(pdntrnThis->choice)  
2508          {
2509             case Move_rotate:
2510                  ppflm = (FloatLoPtrPtr) pdntrnThis->data.ptrvalue;
2511                  FLMatrixFree(ppflm,  0,  0);
2512               break;
2513             case Move_translate: 
2514                  pflv = (FloatLoPtr) pdntrnThis->data.ptrvalue;
2515                  FLVectorFree(pflv,  0);
2516               break;
2517          }
2518          pdntrnThis->next = NULL;
2519          DValNodeFree(pdntrnThis);
2520        pdntrnThis = pdntrnNext;
2521      }
2522 }
2523 
2524 void LIBCALL TransformToDNTRN(PDNTRN PNTR pplist,  TransformPtr transform)
2525 {
2526   PDNTRN pdntrnThis = NULL;
2527   PDNTRN pdntrnFirst = NULL;
2528   MovePtr pm = NULL;
2529   RotMatrixPtr prm = NULL;
2530   TransMatrixPtr ptm = NULL;
2531   FloatLoPtr pflv = NULL;
2532   FloatLoPtrPtr ppflm = NULL;
2533    
2534   if (!transform) return;
2535   
2536   pm = transform->moves;
2537   while (pm)
2538     {
2539         pdntrnThis = NewDNTRN(pplist, (Int2) pm->choice);
2540         if (!pdntrnThis) return ;
2541         if (!pdntrnFirst) pdntrnFirst = pdntrnThis;
2542         if ((int) pm->choice == Move_rotate)
2543           {  /* copy * scale from RotMatrix */
2544            ppflm = (FloatLoPtrPtr) pdntrnThis->data.ptrvalue;
2545            prm = (RotMatrixPtr) pm->data.ptrvalue;
2546            ppflm[0][0] = (FloatLo) ((FloatLo)prm->rot_11 / prm->scale_factor);
2547            ppflm[0][1] = (FloatLo) ((FloatLo)prm->rot_12 / prm->scale_factor);
2548            ppflm[0][2] = (FloatLo) ((FloatLo)prm->rot_13 / prm->scale_factor);
2549            ppflm[1][0] = (FloatLo) ((FloatLo)prm->rot_21 / prm->scale_factor);
2550            ppflm[1][1] = (FloatLo) ((FloatLo)prm->rot_22 / prm->scale_factor);
2551            ppflm[1][2] = (FloatLo) ((FloatLo)prm->rot_23 / prm->scale_factor);
2552            ppflm[2][0] = (FloatLo) ((FloatLo)prm->rot_31 / prm->scale_factor);
2553            ppflm[2][1] = (FloatLo) ((FloatLo)prm->rot_32 / prm->scale_factor);
2554            ppflm[2][2] = (FloatLo) ((FloatLo)prm->rot_33 / prm->scale_factor);
2555           }
2556         if ((int) pm->choice == Move_translate)
2557           {
2558             pflv = (FloatLoPtr) pdntrnThis->data.ptrvalue;
2559             ptm = (TransMatrixPtr) pm->data.ptrvalue;
2560             pflv[0] = (FloatLo) ((FloatLo)ptm->tran_1 / ptm->scale_factor);
2561             pflv[1] = (FloatLo) ((FloatLo)ptm->tran_2 / ptm->scale_factor);
2562             pflv[2] = (FloatLo) ((FloatLo)ptm->tran_3 / ptm->scale_factor);
2563           }
2564         pm = pm->next;
2565     } 
2566 }
2567  
2568 void LIBCALLBACK DoApplyTransform(PFB pfbThis, Int4 iModel, Int4 iIndex, Pointer ptr)
2569 {
2570   PMAD pmadAtom = NULL;
2571   PMOD pmodThis = NULL;
2572   PALD paldLoc = NULL;
2573   PMSD pmsdThis = NULL;
2574   PDNTRN pdntrnThis = NULL;
2575   FloatLoPtr pflv = NULL;
2576   FloatLoPtr pflvData = NULL;
2577   FloatLoPtrPtr ppflmData = NULL;
2578   FloatLoPtrPtr ppflm = NULL;  
2579   FloatLo flX,  flY,  flZ;
2580   Int2 i,  j;
2581   
2582   if (!ptr) return;
2583   
2584   if (IsAtomNode(pfbThis))
2585       {
2586           pmadAtom = (PMAD) pfbThis;
2587           paldLoc = GetAtomLocs(pmadAtom, iModel);
2588           pmsdThis = (PMSD) ptr;
2589           /* go through each location for this particular atom and model */ 
2590           while (paldLoc) 
2591             {    
2592                pdntrnThis = (PDNTRN) ptr;  /* reset to head of transformations */
2593                pflvData = paldLoc->pflvData;
2594                while (pdntrnThis)
2595                  {
2596                    if (pdntrnThis->choice == Move_translate)
2597                      {
2598                       pflv = (FloatLoPtr) pdntrnThis->data.ptrvalue;
2599                       pflvData[0] = pflvData[0] + pflv[0];
2600                       pflvData[1] = pflvData[1] + pflv[1];
2601                       pflvData[2] = pflvData[2] + pflv[2];
2602                      }
2603                   if (pdntrnThis->choice == Move_rotate)
2604                      {
2605                        ppflm = (FloatLoPtrPtr) pdntrnThis->data.ptrvalue;
2606                        flX =(pflvData[0] * ppflm[0][0] +
2607                              pflvData[1] * ppflm[1][0] +
2608                              pflvData[2] * ppflm[2][0]);
2609                        flY =(pflvData[0] * ppflm[0][1] +
2610                              pflvData[1] * ppflm[1][1] +
2611                              pflvData[2] * ppflm[2][1]);
2612                        flZ = (pflvData[0] * ppflm[0][2] +
2613                              pflvData[1]* ppflm[1][2] +
2614                              pflvData[2] * ppflm[2][2]);
2615                        pflvData[0] = flX;
2616                        pflvData[1] = flY;
2617                        pflvData[2] = flZ;       
2618                      } 
2619                   pdntrnThis = pdntrnThis->next; /* get next transform */
2620                }
2621               paldLoc = paldLoc->next; /* get next location */
2622           }  /* while paldLoc */
2623       }  /* if IsAtomNode */
2624       
2625     if (IsObjectNode(pfbThis))
2626       {
2627          /* deal with the objects too */
2628       pmodThis = (PMOD) pfbThis;
2629       pdntrnThis = (PDNTRN) ptr;  /* reset to head of transformations */
2630       ppflmData = pmodThis->ppflObject;
2631       while (pdntrnThis)
2632       {
2633         switch (pmodThis->bWhat)
2634         {
2635           
2636         case OBJ_CYLINDER:
2637                         i = 2;
2638             break;
2639         case OBJ_BRICK:
2640                         i = 8;
2641             break;
2642         default: i=0;
2643         }
2644         
2645         if (pdntrnThis->choice == Move_translate)
2646         {
2647           pflv = (FloatLoPtr) pdntrnThis->data.ptrvalue;
2648           for (j = 0; j<i; j++)
2649                         {                       
2650             pflvData = ppflmData[j];
2651             pflvData[0] = pflvData[0] + pflv[0];
2652             pflvData[1] = pflvData[1] + pflv[1];
2653             pflvData[2] = pflvData[2] + pflv[2];
2654                         }
2655         }
2656         if (pdntrnThis->choice == Move_rotate)
2657         {
2658           ppflm = (FloatLoPtrPtr) pdntrnThis->data.ptrvalue;
2659           for (j = 0; j<i; j++)
2660                         {       
2661             flX =(ppflmData[j][0] * ppflm[0][0] +
2662               ppflmData[j][1] * ppflm[1][0] +
2663               ppflmData[j][2] * ppflm[2][0]);
2664             flY =(ppflmData[j][0] * ppflm[0][1] +
2665               ppflmData[j][1] * ppflm[1][1] +
2666               ppflmData[j][2] * ppflm[2][1]);
2667             flZ = (ppflmData[j][0] * ppflm[0][2] +
2668               ppflmData[j][1]* ppflm[1][2] +
2669               ppflmData[j][2] * ppflm[2][2]);
2670             ppflmData[j][0] = flX;
2671             ppflmData[j][1] = flY;
2672             ppflmData[j][2] = flZ;      
2673             
2674           }
2675         }  
2676         pdntrnThis = pdntrnThis->next; /* get next transform */
2677       } /* while pdntrnThis */
2678    }  /* if IsObjectNode */     
2679 } 
2680 
2681 
2682 static void TransformMultiply(FloatLoPtr pflvData, FloatLoPtrPtr ppflm)
2683 {
2684     Nlm_Matrix Matrix, Vector, Result, Invert;
2685     Nlm_Uint4 m, n;  /* matrix indices */
2686 
2687     Matrix = Nlm_MatrixNew(3, 3);
2688     if (!Matrix) return;
2689     Vector = Nlm_MatrixNew(1, 3);
2690     if (!Vector) return;
2691 
2692     for(m = 0; m < 3; m++) {
2693         for(n = 0; n < 3; n++) {
2694             Nlm_MatrixSetNode(Matrix, m, n, (Nlm_FloatHi) ppflm[m][n]);
2695         }
2696     }
2697     for(m = 0; m < 3; m++) Nlm_MatrixSetNode(Vector, 0, m, (Nlm_FloatHi) pflvData[m]);
2698     Invert = Nlm_MatrixInvert(Matrix);
2699     Result = Nlm_MatrixMultiply(Vector, Invert);
2700     for(m = 0; m < 3; m++) pflvData[m] = (Nlm_FloatLo) Nlm_MatrixNode(Result, 0, m);
2701     Nlm_MatrixDelete(Matrix);
2702     Nlm_MatrixDelete(Vector);
2703     Nlm_MatrixDelete(Result);
2704     Nlm_MatrixDelete(Invert);
2705 }
2706 
2707 
2708 void LIBCALLBACK DoReverseTransform(PFB pfbThis, Int4 iModel, Int4 iIndex, Pointer ptr)
2709 {
2710   PMAD pmadAtom = NULL;
2711   PMOD pmodThis = NULL;
2712   PALD paldLoc = NULL;
2713   PMSD pmsdThis = NULL;
2714   PDNTRN pdntrnThis = NULL;
2715   FloatLoPtr pflv = NULL;
2716   FloatLoPtr pflvData = NULL;
2717   FloatLoPtrPtr ppflmData = NULL;
2718   FloatLoPtrPtr ppflm = NULL;  
2719   /* FloatLo flX,  flY,  flZ; */
2720   Int2 i,  j;
2721   
2722   if (IsAtomNode(pfbThis))
2723       {
2724           pmadAtom = (PMAD) pfbThis;
2725           paldLoc = GetAtomLocs(pmadAtom, iModel);
2726           pmsdThis = (PMSD) ptr;
2727           /* go through each location for this particular atom and model */ 
2728           while (paldLoc) 
2729             {    
2730                pdntrnThis = (PDNTRN) ptr;  /* reset to head of transformations */
2731                /* then go to the end */
2732                while (pdntrnThis->next)  pdntrnThis = pdntrnThis->next;
2733                pflvData = paldLoc->pflvData;
2734                while (pdntrnThis)
2735                  {
2736                    if (pdntrnThis->choice == Move_translate)
2737                      {
2738                       pflv = (FloatLoPtr) pdntrnThis->data.ptrvalue;
2739                       pflvData[0] = pflvData[0] - pflv[0];
2740                       pflvData[1] = pflvData[1] - pflv[1];
2741                       pflvData[2] = pflvData[2] - pflv[2];
2742                      }
2743                   if (pdntrnThis->choice == Move_rotate)
2744                      {
2745                        ppflm = (FloatLoPtrPtr) pdntrnThis->data.ptrvalue;
2746                        TransformMultiply(pflvData, ppflm);
2747                        /*
2748                        flX =(pflvData[0] * ppflm[0][0] +
2749                              pflvData[1] * ppflm[1][0] +
2750                              pflvData[2] * ppflm[2][0]);
2751                        flY =(pflvData[0] * ppflm[0][1] +
2752                              pflvData[1] * ppflm[1][1] +
2753                              pflvData[2] * ppflm[2][1]);
2754                        flZ = (pflvData[0] * ppflm[0][2] +
2755                              pflvData[1]* ppflm[1][2] +
2756                              pflvData[2] * ppflm[2][2]);
2757                        pflvData[0] = flX;
2758                        pflvData[1] = flY;
2759                        pflvData[2] = flZ;
2760                        */
2761                      } 
2762                   pdntrnThis = pdntrnThis->last; /* get previous transform */
2763                }
2764               paldLoc = paldLoc->next; /* get next location */
2765           }  /* while paldLoc */
2766       }  /* if IsAtomNode */
2767       
2768     if (IsObjectNode(pfbThis))
2769       {
2770          /* deal with the objects too */
2771       pmodThis = (PMOD) pfbThis;
2772       pdntrnThis = (PDNTRN) ptr;  /* reset to head of transformations */
2773       /* then go to the end */
2774       while (pdntrnThis->next)  pdntrnThis = pdntrnThis->next;
2775       ppflmData = pmodThis->ppflObject;
2776       while (pdntrnThis)
2777       {
2778         switch (pmodThis->bWhat)
2779         {
2780           
2781         case OBJ_CYLINDER:
2782                         i = 2;
2783             break;
2784         case OBJ_BRICK:
2785                         i = 8;
2786             break;
2787         default: i=0;
2788         }
2789         
2790         if (pdntrnThis->choice == Move_translate)
2791         {
2792           pflv = (FloatLoPtr) pdntrnThis->data.ptrvalue;
2793           for (j = 0; j<i; j++)
2794                         {                       
2795             pflvData = ppflmData[j];
2796             pflvData[0] = pflvData[0] - pflv[0];
2797             pflvData[1] = pflvData[1] - pflv[1];
2798             pflvData[2] = pflvData[2] - pflv[2];
2799                         }
2800         }
2801         if (pdntrnThis->choice == Move_rotate)
2802         {
2803           ppflm = (FloatLoPtrPtr) pdntrnThis->data.ptrvalue;
2804           for (j = 0; j<i; j++)
2805                         {
2806         TransformMultiply(ppflmData[j], ppflm);
2807         /*
2808             flX =(ppflmData[j][0] * ppflm[0][0] +
2809               ppflmData[j][1] * ppflm[1][0] +
2810               ppflmData[j][2] * ppflm[2][0]);
2811             flY =(ppflmData[j][0] * ppflm[0][1] +
2812               ppflmData[j][1] * ppflm[1][1] +
2813               ppflmData[j][2] * ppflm[2][1]);
2814             flZ = (ppflmData[j][0] * ppflm[0][2] +
2815               ppflmData[j][1]* ppflm[1][2] +
2816               ppflmData[j][2] * ppflm[2][2]);
2817             ppflmData[j][0] = flX;
2818             ppflmData[j][1] = flY;
2819             ppflmData[j][2] = flZ;
2820         */
2821             
2822           }
2823         }  
2824         pdntrnThis = pdntrnThis->last; /* get next transform */
2825       } /* while pdntrnThis */
2826    }  /* if IsObjectNode */     
2827 } 
2828 
2829 
2830 
2831 PDNMS LIBCALL FindLoadedBiostruc(CharPtr pcPDBID, Int4 iId)
2832 {
2833  /* replace this with caching mechanism */
2834  PDNMS pdnmsThis = NULL;
2835  PMSD pmsdThis = NULL;
2836  
2837  if ((pcPDBID == NULL) && (iId == 0)) return NULL;
2838 
2839  pdnmsThis = GetFirstModelstruc( );
2840  while (pdnmsThis)
2841   {
2842     pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
2843     if (pmsdThis->iMMDBid == iId) return pdnmsThis;    
2844     if (!(StringCmp(pcPDBID,pmsdThis->pcPDBName))) return pdnmsThis;
2845     pdnmsThis = pdnmsThis->next;
2846   }
2847  return NULL; 
2848 }
2849 
2850 Boolean InstBSAnnotSet(BiostrucAnnotSetPtr pbsasThis, CharPtr JobID,
2851                        Boolean Chain, CharPtr Path) {
2852   Int2 iTest;
2853   Int4 iMMDBId;
2854   PDNMS pdnmsThis = NULL;
2855   PMSD  pmsdThis = NULL;
2856   BiostrucPtr pbsThis = NULL, pbsTemp = NULL;
2857   BiostrucIdPtr pbsidThis = NULL;
2858   BiostrucFeatureSetPtr pbsfsThis = NULL;
2859   BiostrucFeaturePtr pbsfThis = NULL;
2860   PSFS psfsThis = NULL;
2861 
2862   CharPtr szTemp;               /* NEW - K.A. 5/4/99 for pruning master/slave */
2863   Char szName[5];
2864   Char AsnPath[PATH_MAX];
2865   Char AsnName[10];
2866   Char MasterChain[2];
2867   Char SlaveChain[2];
2868 
2869  /* the Feature set either attaches to an in-memory Modelstruc */
2870  /* or, if it specifies a new Modelstruc - attempts a retrieval */
2871  
2872   if (pbsasThis == NULL) return FALSE;
2873   MasterChain[0] = '\0';
2874   SlaveChain[0] = '\0';
2875 
2876 /* grab the id out of the Biostruc-descr */
2877 
2878   pbsidThis = ValNodeFindNext(pbsasThis->id,NULL,BiostrucId_mmdb_id);
2879   if (pbsidThis)  /* only deal with the first one */
2880     {
2881       iMMDBId = (Int4) pbsidThis->data.intvalue;
2882       /*printf("iMMDBID=%ld\n", iMMDBId);*//*master MMDB_ID*/
2883     }
2884 
2885   pdnmsThis = FindLoadedBiostruc(NULL, iMMDBId);
2886 
2887   if (!pdnmsThis) {
2888     szTemp = pbsasThis->features->features->name;  
2889     szName[0] = szTemp[0];
2890     szName[1] = szTemp[1];
2891     szName[2] = szTemp[2];
2892     szName[3] = szTemp[3];
2893     szName[4] = '\0';
2894     /* try to load it using all NCBI models */
2895     /* JobID points to dir containing biostruc of a query protein from VAST Search */  
2896 
2897     /* First get the structure using MMDBBiostrucGet or FetchBS, then prune if needed */
2898 
2899     if (JobID == NULL)
2900       pbsThis = MMDBBiostrucGet(iMMDBId, ALLSIMPLEMDL, 3);
2901     else {
2902       AsnName[0]='\0';
2903       StringCpy(AsnName, "/b");
2904       StringCat(AsnName, szName);
2905       AsnPath[0]='\0';
2906       StringCpy(AsnPath, Path);
2907       StringCat(AsnPath, JobID);
2908       StringCat(AsnPath, AsnName);
2909       pbsThis = FetchBS(AsnPath, 0, ALLSIMPLEMDL, 3, POWER_VIEW);
2910     }
2911 
2912     if (Chain) {
2913       if (szTemp[4] != ' ') {
2914         MasterChain[0] = szTemp[4];
2915         MasterChain[1] = '\0';
2916         pbsTemp = (BiostrucPtr)PruneBiostruc(pbsThis, MasterChain);
2917         pbsThis = NULL;
2918         pbsThis = pbsTemp;
2919       }
2920       SlaveChain[0] = szTemp[11];
2921       SlaveChain[1] = '\0';
2922     } 
2923 
2924     if (!pbsThis) goto nogomem;
2925     pdnmsThis =  MakeAModelstruc(pbsThis);
2926     /* side effect is that this is now the selected modelstruc too */
2927     if (!pdnmsThis) goto nogomem;  
2928   }
2929   pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
2930 
2931   /****** WARNING: fix this if this code is ever used to recognize Neighbor mode *********/
2932   SetSelectedModelstruc(pdnmsThis);  /* set it to selected one */
2933   /* at this point we have annot-set, and its associated Modelstruc */
2934 
2935   pbsfsThis = pbsasThis->features;
2936 
2937   while (pbsfsThis)  /* walk through each feature-set */
2938     {
2939     iTest = BiostrucAddFeature(pbsfsThis,pdnmsThis, Chain, SlaveChain);  /* new slave args passed */
2940     if (iTest < 0) goto nogomem; /* a malloc error */
2941       if (!iTest) goto nogo;  /* bad feature table error - fatal */
2942       pbsfsThis = pbsfsThis->next;
2943     }
2944   /* given a sucessfully registered Biostruc-annot-set
2945      we can now free it */
2946   BiostrucAnnotSetFree(pbsasThis);
2947   return TRUE;
2948   
2949 nogomem:
2950 nogo:
2951   BiostrucAnnotSetFree(pbsasThis);
2952   return FALSE;
2953 }   
2954 
2955 /************************************************************
2956 produce a sequence alignment from a structure-alignment
2957 :)bug fixed: need two loops to go through master and slave selection with
2958 BiostrucAnnotSetPtr->features (master selection), and 
2959 BiostrucAnnotSetPtr->features->features (slave selection) 
2960 in order to pick both master and slave with defination
2961 of molecule name, chain, and domain
2962 ***********************************************************/
2963 SeqAnnotPtr LIBCALL BiosToSeq (BiostrucAnnotSetPtr set, Boolean usePValue, 
2964                                Char* pdbname_master, Char* pdbname_slave)
2965 {
2966   BiostrucFeatureSetPtr pbsfs=NULL;
2967   BiostrucFeaturePtr pbsf=NULL;
2968   SeqAnnotPtr     sap = NULL;
2969   SeqAlignPtr     salp;
2970   ScorePtr        scp;
2971   Int2            count;
2972   ObjectIdPtr     oid;
2973   DenseSegPtr     dsp;
2974   SeqIdPtr        mastersip = NULL, slavesip;
2975   SeqIdPtr        pdbsip = NULL;
2976   BiostrucFeaturePtr feature;
2977   ChemGraphAlignmentPtr pcga1 = NULL;
2978   ValNodePtr      loc1 = NULL;
2979   ValNodePtr      vnp = NULL;
2980   SeqAlignPtr     tail = NULL;
2981   ChemGraphPntrsPtr cgpp;
2982   ResidueIntervalPntrPtr master;
2983   ResidueIntervalPntrPtr slave;
2984   ResidueIntervalPntrPtr masterseg;
2985   ResidueIntervalPntrPtr slaveseg;
2986   CharPtr         pcPDB;
2987   int             iDomain;
2988   ValNodePtr      pvn;
2989   Char            cChain;
2990   Char            buf[100];
2991   Char            buf2[50];
2992   Int4 nextmasterstart, nextslavestart;
2993 
2994   if (set == NULL || set->features == NULL || set->features->features == NULL)
2995     return NULL;
2996   pbsfs=set->features;
2997   while(pbsfs)
2998   {
2999      feature=pbsfs->features;
3000      /*if master name matched*/
3001     if(strcmp((CharPtr)(pbsfs->descr->data.ptrvalue), pdbname_master) == 0)
3002     {
3003      while(feature)
3004     {
3005 
3006     /* construct the master SeqId if it hasn't already been constructed */
3007     if (mastersip == NULL) {
3008       pcPDB = StringSave (PDBNAME_DEFAULT);
3009       iDomain = 0;
3010       cChain = ' ';
3011       pcPDB[0] = feature->name[0];
3012       pcPDB[1] = feature->name[1];
3013       pcPDB[2] = feature->name[2];
3014       pcPDB[3] = feature->name[3];
3015       cChain = feature->name[4];
3016       iDomain = atoi ((char *) &feature->name[5]);
3017       mastersip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);
3018       pdbsip = MakePDBSeqId2 (pcPDB, cChain, iDomain, FALSE);
3019    
3020       MemFree (pcPDB);
3021     }
3022     /* get the embedded PDB code of the hit */
3023     pcPDB = StringSave (PDBNAME_DEFAULT);
3024     iDomain = 0;
3025     cChain = '-';
3026 
3027     if (StringLen (feature->name) >= 13) {
3028       pcPDB[0] = feature->name[7];
3029       pcPDB[1] = feature->name[8];
3030       pcPDB[2] = feature->name[9];
3031       pcPDB[3] = feature->name[10];
3032       cChain = feature->name[11];
3033       iDomain = atoi ((char *) &feature->name[12]);
3034       slavesip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);
3035     
3036       MemFree (pcPDB);
3037     }
3038 
3039 pcPDB[4] = feature->name[11];
3040 pcPDB[5] = feature->name[12];
3041 pcPDB[6]='\0';  
3042   
3043  if (strcmp(pcPDB, pdbname_slave) == 0)
3044 {
3045     pvn = ValNodeFindNext (feature->Location_location, NULL, Location_location_alignment);
3046     if (pvn)
3047       pcga1 = (ChemGraphAlignmentPtr) pvn->data.ptrvalue;
3048 
3049     vnp = pcga1->alignment;
3050 
3051     if (vnp->choice != ChemGraphPntrs_residues)
3052       continue;
3053 
3054     cgpp = (ChemGraphPntrsPtr) vnp->data.ptrvalue;
3055 
3056     if (cgpp->choice != ResiduePntrs_interval)
3057       continue;
3058     master = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue;
3059     cgpp = (ChemGraphPntrsPtr) vnp->next->data.ptrvalue;
3060     slave = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue; 
3061     
3062     for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, 
3063           slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next,
3064            slaveseg = slaveseg->next) {
3065       if (count > 0 && nextmasterstart < masterseg->from)
3066           count++;
3067       if (count > 0 && nextslavestart < slaveseg->from)
3068           count++;
3069       count++;
3070       nextmasterstart = masterseg->to + 1;
3071       nextslavestart = slaveseg->to + 1;
3072     } 
3073 
3074 
3075     salp = SeqAlignNew ();
3076     if (sap == NULL) {
3077       sap = SeqAnnotNew ();
3078       sap->type = 2;            /* align */
3079       sap->data = salp;
3080       sap->desc = ValNodeNew(NULL);
3081       sap->desc->choice = 2; /* title */
3082       SeqIdWrite (pdbsip, buf2, PRINTID_FASTA_LONG, sizeof(buf2));
3083       sprintf (buf, "VAST structural alignment for %s, mapped to sequence alignment", buf2);
3084       sap->desc->data.ptrvalue = StringSave(buf);
3085     } else {
3086       tail->next = salp;
3087     }
3088     tail = salp;
3089     salp->type = 3 /* partial */ ;
3090     salp->dim = 2;
3091 
3092     scp = ScoreNew ();
3093     salp->score = scp;
3094     salp->segtype = 2 /* denseg */ ;
3095     scp->choice = 2;            /* real */
3096     scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_score) / pcga1->aligndata->scale_factor;
3097     oid = ObjectIdNew ();
3098     scp->id = oid;
3099     oid->str = StringSave ("VAST score");
3100     scp->next = ScoreNew ();
3101     scp = scp->next;
3102     scp->choice = 2;            /* real */
3103     scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_mlogp) / pcga1->aligndata->scale_factor;
3104     oid = ObjectIdNew ();
3105     scp->id = oid;
3106     oid->str = StringSave ("VAST p-value");
3107     scp->next = ScoreNew ();
3108     scp = scp->next;
3109     scp->choice = 2;            /* real */
3110     scp->value.realvalue = ((FloatHi) pcga1->aligndata->rmsd) / pcga1->aligndata->scale_factor;
3111     oid = ObjectIdNew ();
3112     scp->id = oid;
3113     oid->str = StringSave ("VAST root mean square deviation, measured in angstroms");
3114     scp->next = ScoreNew ();
3115     scp = scp->next;
3116     scp->choice = 1;            /* int */
3117     scp->value.intvalue = pcga1->aligndata->align_res;
3118     oid = ObjectIdNew ();
3119     scp->id = oid;
3120     oid->str = StringSave ("VAST aligned residue count");
3121 
3122     dsp = DenseSegNew ();
3123     salp->segs = (Pointer) dsp;
3124     dsp->dim = 2;
3125     dsp->numseg = count;
3126     dsp->ids = SeqIdDup (mastersip);
3127     dsp->ids->next = slavesip;
3128     dsp->starts = (Int4Ptr) MemNew (count * sizeof (Int4) * 2);
3129     dsp->lens = (Int4Ptr) MemNew (count * sizeof (Int4));
3130 
3131     for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next, slaveseg = slaveseg->next) {
3132       if (count > 0 && nextmasterstart < masterseg->from)
3133       { /* add a gap */
3134         dsp->starts[count * 2] = nextmasterstart - 1;
3135         dsp->starts[count * 2 + 1] = -1;
3136         dsp->lens[count] = masterseg->from - nextmasterstart;
3137         count++;
3138       }
3139       if (count > 0 && nextslavestart < slaveseg->from)
3140       { /* add a gap */
3141         dsp->starts[count * 2 + 1] = nextslavestart - 1;
3142         dsp->starts[count * 2] = -1;
3143         dsp->lens[count] = slaveseg->from - nextslavestart;
3144         count++;
3145       }
3146       dsp->starts[count * 2] = masterseg->from-1;
3147       dsp->starts[count * 2 + 1] = slaveseg->from-1;
3148       dsp->lens[count] = masterseg->to - masterseg->from + 1;
3149       count++;
3150       nextmasterstart = masterseg->to + 1;
3151       nextslavestart = slaveseg->to + 1;
3152        }
3153       } /*if pdbname matched*/
3154       feature=feature->next;
3155     }/*while feature (pbsf)*/
3156     }/*if pcMaster matches pdbname_master*/
3157     pbsfs=pbsfs->next;
3158   }/*while pbsfs*/
3159   SeqIdFree (mastersip);
3160   SeqIdFree (pdbsip);
3161   return sap;
3162 }
3163 
3164 
3165 
3166 SeqAnnotPtr LIBCALL fnPBSFtoPSA (BiostrucFeaturePtr pbsfSelected)
3167 {
3168   SeqAnnotPtr     sap = NULL;
3169   SeqAlignPtr     salp;
3170   ScorePtr        scp;
3171   Int2            count;
3172   ObjectIdPtr     oid;
3173   DenseSegPtr     dsp;
3174   ValNodePtr      loc1 = NULL;
3175   SeqAlignPtr     tail = NULL;
3176   ChemGraphPntrsPtr cgpp;
3177   ResidueIntervalPntrPtr master;
3178   ResidueIntervalPntrPtr slave;
3179   ResidueIntervalPntrPtr masterseg;
3180   ResidueIntervalPntrPtr slaveseg;
3181   Char            buf[100];
3182   Char            buf2[50];
3183   Int4 nextmasterstart, nextslavestart;
3184   SeqIdPtr        mastersip = NULL, slavesip;
3185   SeqIdPtr        pdbsip = NULL;
3186   int             iDomain;
3187   CharPtr         pcPDB;
3188   Char            cChain;
3189   ValNodePtr pvnAlignment;
3190   ChemGraphAlignmentPtr pcga1;
3191 
3192   Boolean choose_densediag = TRUE;
3193     
3194     pvnAlignment = ValNodeFindNext(pbsfSelected->Location_location,NULL,
3195     Location_location_alignment);
3196      pcga1 = (ChemGraphAlignmentPtr) pvnAlignment->data.ptrvalue;
3197 
3198      if (pcga1->alignment->choice != ChemGraphPntrs_residues)
3199       return NULL;
3200 
3201     cgpp = (ChemGraphPntrsPtr) pcga1->alignment->data.ptrvalue;
3202   
3203   pcPDB = StringSave (PDBNAME_DEFAULT);
3204   iDomain = 0;
3205   cChain = '-';
3206   pcPDB[0] = pbsfSelected->name[0];
3207   pcPDB[1] = pbsfSelected->name[1];
3208   pcPDB[2] = pbsfSelected->name[2];
3209   pcPDB[3] = pbsfSelected->name[3];
3210   cChain = pbsfSelected->name[4];
3211   iDomain = atoi ((char *) &pbsfSelected->name[5]);
3212 /*mastersip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE); */
3213   mastersip = MakePDBSeqId2 (pcPDB, cChain, iDomain, FALSE);
3214   pdbsip = MakePDBSeqId2 (pcPDB, cChain, iDomain, FALSE);
3215   
3216   MemFree (pcPDB);
3217   
3218   /* get the embedded PDB code of the hit */
3219  if (iDomain < 10) {
3220   pcPDB = StringSave (PDBNAME_DEFAULT);
3221   iDomain = 0;
3222   cChain = '-';
3223   
3224   pcPDB[0] = pbsfSelected->name[7];
3225   pcPDB[1] = pbsfSelected->name[8];
3226   pcPDB[2] = pbsfSelected->name[9];
3227   pcPDB[3] = pbsfSelected->name[10];
3228   cChain = pbsfSelected->name[11];
3229   iDomain = atoi ((char *) &pbsfSelected->name[12]);
3230  }
3231  else {         /* have at least 10 domains in 1 str., added by J. Chen */
3232   pcPDB = StringSave (PDBNAME_DEFAULT);
3233   iDomain = 0;
3234   cChain = '-';
3235  
3236   pcPDB[0] = pbsfSelected->name[8];
3237   pcPDB[1] = pbsfSelected->name[9];
3238   pcPDB[2] = pbsfSelected->name[10];
3239   pcPDB[3] = pbsfSelected->name[11];
3240   cChain = pbsfSelected->name[12];
3241   iDomain = atoi ((char *) &pbsfSelected->name[13]);
3242  }
3243 
3244 /*slavesip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);  */
3245   slavesip = MakePDBSeqId2 (pcPDB, cChain, iDomain, FALSE);
3246   
3247   MemFree (pcPDB);
3248   
3249   if (cgpp->choice != ResiduePntrs_interval)
3250     return NULL;
3251   master = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue;
3252   cgpp = (ChemGraphPntrsPtr) pcga1->alignment->next->data.ptrvalue;
3253   slave = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue; 
3254   
3255   for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, 
3256     slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next,
3257     slaveseg = slaveseg->next) {
3258     if (count > 0 && nextmasterstart < masterseg->from)
3259       count++;
3260     if (count > 0 && nextslavestart < slaveseg->from)
3261       count++;
3262     count++;
3263     nextmasterstart = masterseg->to + 1;
3264     nextslavestart = slaveseg->to + 1;
3265   } 
3266   
3267   salp = SeqAlignNew ();
3268   if (sap == NULL) {
3269     sap = SeqAnnotNew ();
3270     sap->type = 2;              /* align */
3271     sap->data = salp;
3272     sap->desc = ValNodeNew(NULL);
3273     sap->desc->choice = 2; /* title */
3274     SeqIdWrite (pdbsip, buf2, PRINTID_FASTA_LONG, sizeof(buf2));
3275     sprintf (buf, "VAST structural alignment for %s, mapped to sequence alignment", buf2);
3276     sap->desc->data.ptrvalue = StringSave(buf);
3277   } else {
3278     tail->next = salp;
3279   }
3280   tail = salp;
3281   salp->type = 3 /* partial */ ;
3282   salp->dim = 2;
3283   
3284   scp = ScoreNew ();
3285   salp->score = scp;
3286   salp->segtype = 2 /* denseg */ ;
3287   scp->choice = 2;              /* real */
3288   scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_score) / pcga1->aligndata->scale_factor;
3289   oid = ObjectIdNew ();
3290   scp->id = oid;
3291   oid->str = StringSave ("VAST score");
3292   scp->next = ScoreNew ();
3293   scp = scp->next;
3294   scp->choice = 2;              /* real */
3295   scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_mlogp) / pcga1->aligndata->scale_factor;
3296   oid = ObjectIdNew ();
3297   scp->id = oid;
3298   oid->str = StringSave ("VAST p-value");
3299   scp->next = ScoreNew ();
3300   scp = scp->next;
3301   scp->choice = 2;              /* real */
3302   scp->value.realvalue = ((FloatHi) pcga1->aligndata->rmsd) / pcga1->aligndata->scale_factor;
3303   oid = ObjectIdNew ();
3304   scp->id = oid;
3305   oid->str = StringSave ("VAST root mean square deviation, measured in angstroms");
3306   scp->next = ScoreNew ();
3307   scp = scp->next;
3308   scp->choice = 1;              /* int */
3309   scp->value.intvalue = pcga1->aligndata->align_res;
3310   oid = ObjectIdNew ();
3311   scp->id = oid;
3312   oid->str = StringSave ("VAST aligned residue count");
3313   
3314  if(!choose_densediag){
3315   dsp = DenseSegNew ();
3316   salp->segs = (Pointer) dsp;
3317   dsp->dim = 2;
3318   dsp->numseg = count;
3319   dsp->ids = SeqIdDup (mastersip);
3320  /* dsp->ids->next = slavesip;  could be a bug, J. Chen */
3321   dsp->ids->next = SeqIdDup(slavesip);
3322   dsp->starts = (Int4Ptr) MemNew (count * sizeof (Int4) * 2);
3323   dsp->lens = (Int4Ptr) MemNew (count * sizeof (Int4));
3324   
3325   for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next, slaveseg = slaveseg->next) {
3326     if (count > 0 && nextmasterstart < masterseg->from)
3327     { /* add a gap */
3328       dsp->starts[count * 2] = nextmasterstart - 1;
3329       dsp->starts[count * 2 + 1] = -1;
3330       dsp->lens[count] = masterseg->from - nextmasterstart;
3331       count++;
3332     }
3333     if (count > 0 && nextslavestart < slaveseg->from)
3334     { /* add a gap */
3335       dsp->starts[count * 2 + 1] = nextslavestart - 1;
3336       dsp->starts[count * 2] = -1;
3337       dsp->lens[count] = slaveseg->from - nextslavestart;
3338       count++;
3339     }
3340     dsp->starts[count * 2] = masterseg->from-1;
3341     dsp->starts[count * 2 + 1] = slaveseg->from-1;
3342     dsp->lens[count] = masterseg->to - masterseg->from + 1;
3343     count++;
3344     nextmasterstart = masterseg->to + 1;
3345     nextslavestart = slaveseg->to + 1;
3346   }
3347  }
3348  else {
3349  DenseDiagPtr ddp, ddp_tmp;
3350 
3351   salp->segs = NULL;
3352   salp->type = 3;
3353   salp->segtype = 1  /* densediag */ ;
3354 
3355   for(count = 0, masterseg = master, slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next, slaveseg = slaveseg->next) {
3356      ddp = DenseDiagNew();
3357      ddp->dim = 2;
3358 
3359      ddp->id = SeqIdDup (mastersip); 
3360 /*     ddp->id->next = slavesip;    bug! fixed by J.Chen */
3361      ddp->id->next = SeqIdDup(slavesip);
3362 
3363      ddp->starts = (Int4Ptr) MemNew ((size_t) 4 * sizeof (Int4));
3364      ddp->starts[0] = masterseg->from-1;
3365      ddp->starts[1] = slaveseg->from -1 ;
3366      ddp->len = masterseg->to - masterseg->from  + 1;
3367      count++;
3368      if(salp->segs == NULL) salp->segs = (Pointer) ddp;
3369      else {
3370         ddp_tmp = (DenseDiagPtr) salp->segs;
3371         while(ddp_tmp->next) ddp_tmp = ddp_tmp->next;
3372         ddp_tmp->next = ddp;
3373      }
3374   }
3375 
3376  }
3377 
3378   
3379   SeqIdFree (mastersip);
3380   SeqIdFree (pdbsip);
3381   slavesip->next = NULL;
3382 
3383   return sap;
3384 }
3385 
3386 /************************************************************
3387 Produce sequence annotation set of a master (name, chain, domain)
3388 with all its slaves according to structure alignments.
3389 ************************************************************/
3390 
3391 /* please don't use this function, use the function above */
3392 
3393 SeqAnnotPtr LIBCALL BiostrToSeqAnnotSet (BiostrucAnnotSetPtr set, 
3394                                Boolean usePValue, Char* pdbname_master)
3395 {
3396   BiostrucFeatureSetPtr pbsfs=NULL;
3397   BiostrucFeaturePtr pbsf=NULL;
3398   SeqAnnotPtr     sap = NULL;
3399   SeqAlignPtr     salp;
3400   ScorePtr        scp;
3401   Int2            count;
3402   ObjectIdPtr     oid;
3403   DenseSegPtr     dsp;
3404   SeqIdPtr        mastersip = NULL, slavesip;
3405   SeqIdPtr        pdbsip = NULL;
3406   BiostrucFeaturePtr feature;
3407   ChemGraphAlignmentPtr pcga1 = NULL;
3408   ValNodePtr      loc1 = NULL;
3409   ValNodePtr      vnp = NULL;
3410   SeqAlignPtr     tail = NULL;
3411   ChemGraphPntrsPtr cgpp;
3412   ResidueIntervalPntrPtr master;
3413   ResidueIntervalPntrPtr slave;
3414   ResidueIntervalPntrPtr masterseg;
3415   ResidueIntervalPntrPtr slaveseg;
3416   CharPtr         pcPDB;
3417   Char  pcMaster[10];  /* fixed lyg */
3418   int             iDomain;
3419   ValNodePtr      pvn;
3420   Char            cChain;
3421   Char            buf[100];
3422   Char            buf2[50];
3423   Int4 nextmasterstart, nextslavestart;
3424 
3425   if (set == NULL || set->features == NULL || set->features->features == NULL)
3426     return NULL;
3427   pbsfs=set->features;
3428   while(pbsfs)
3429   {
3430      feature=pbsfs->features;
3431      strcpy(pcMaster, (char *)pbsfs->descr->data.ptrvalue);
3432      /*if master name matched*/
3433     if(strcmp(pcMaster, pdbname_master) == 0) /* I had this commented out */
3434     {
3435      while(feature)
3436     {
3437 
3438     /* construct the master SeqId if it hasn't already been constructed */
3439     if (mastersip == NULL) {
3440       pcPDB = StringSave (PDBNAME_DEFAULT);
3441       iDomain = 0;
3442       cChain = '-';
3443       pcPDB[0] = feature->name[0];
3444       pcPDB[1] = feature->name[1];
3445       pcPDB[2] = feature->name[2];
3446       pcPDB[3] = feature->name[3];
3447       cChain = feature->name[4];
3448       iDomain = atoi ((char *) &feature->name[5]);
3449       mastersip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);
3450       pdbsip = MakePDBSeqId2 (pcPDB, cChain, iDomain, FALSE);
3451    
3452       MemFree (pcPDB);
3453     }
3454     /* get the embedded PDB code of the hit */
3455     pcPDB = StringSave (PDBNAME_DEFAULT);
3456     iDomain = 0;
3457     cChain = '-';
3458 
3459     if (StringLen (feature->name) >= 13) {
3460       pcPDB[0] = feature->name[7];
3461       pcPDB[1] = feature->name[8];
3462       pcPDB[2] = feature->name[9];
3463       pcPDB[3] = feature->name[10];
3464       cChain = feature->name[11];
3465       iDomain = atoi ((char *) &feature->name[12]);
3466       slavesip = MakePDBSeqId2 (pcPDB, cChain, iDomain, TRUE);
3467     
3468       MemFree (pcPDB);
3469     }
3470 
3471 pcPDB[4] = feature->name[11];
3472 pcPDB[5] = feature->name[12];
3473 pcPDB[6]='\0';  
3474 
3475     pvn = ValNodeFindNext (feature->Location_location, NULL, Location_location_alignment);
3476     if (pvn)
3477       pcga1 = (ChemGraphAlignmentPtr) pvn->data.ptrvalue;
3478 
3479     vnp = pcga1->alignment;
3480 
3481     if (vnp->choice != ChemGraphPntrs_residues)
3482       continue;
3483 
3484     cgpp = (ChemGraphPntrsPtr) vnp->data.ptrvalue;
3485 
3486     if (cgpp->choice != ResiduePntrs_interval)
3487       continue;
3488     master = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue;
3489     cgpp = (ChemGraphPntrsPtr) vnp->next->data.ptrvalue;
3490     slave = (ResidueIntervalPntrPtr) cgpp->data.ptrvalue; 
3491     
3492     for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, 
3493           slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next,
3494            slaveseg = slaveseg->next) {
3495       if (count > 0 && nextmasterstart < masterseg->from)
3496           count++;
3497       if (count > 0 && nextslavestart < slaveseg->from)
3498           count++;
3499       count++;
3500       nextmasterstart = masterseg->to + 1;
3501       nextslavestart = slaveseg->to + 1;
3502     } 
3503 
3504 
3505     salp = SeqAlignNew ();
3506     if (sap == NULL) {
3507       sap = SeqAnnotNew ();
3508       sap->type = 2;            /* align */
3509       sap->data = salp;
3510       sap->desc = ValNodeNew(NULL);
3511       sap->desc->choice = 2; /* title */
3512       SeqIdWrite (pdbsip, buf2, PRINTID_FASTA_LONG, sizeof(buf2));
3513       buf2[10]=pdbname_master[5];
3514       buf2[11]='\0';
3515       sprintf (buf, "VAST structural alignment for %s, mapped to sequence alignment", buf2);
3516       sap->desc->data.ptrvalue = StringSave(buf);
3517     } else {
3518       tail->next = salp;
3519     }
3520     tail = salp;
3521     salp->type = 3 /* partial */ ;
3522     salp->dim = 2;
3523 
3524     scp = ScoreNew ();
3525     salp->score = scp;
3526     salp->segtype = 2 /* denseg */ ;
3527     scp->choice = 2;            /* real */
3528     scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_score) / pcga1->aligndata->scale_factor;
3529     oid = ObjectIdNew ();
3530     scp->id = oid;
3531     oid->str = StringSave ("VAST score");
3532     scp->next = ScoreNew ();
3533     scp = scp->next;
3534     scp->choice = 2;            /* real */
3535     scp->value.realvalue = ((FloatHi) pcga1->aligndata->vast_mlogp) / pcga1->aligndata->scale_factor;
3536     oid = ObjectIdNew ();
3537     scp->id = oid;
3538     oid->str = StringSave ("VAST p-value");
3539     scp->next = ScoreNew ();
3540     scp = scp->next;
3541     scp->choice = 2;            /* real */
3542     scp->value.realvalue = ((FloatHi) pcga1->aligndata->rmsd) / pcga1->aligndata->scale_factor;
3543     oid = ObjectIdNew ();
3544     scp->id = oid;
3545     oid->str = StringSave ("VAST root mean square deviation, measured in angstroms");
3546     scp->next = ScoreNew ();
3547     scp = scp->next;
3548     scp->choice = 1;            /* int */
3549     scp->value.intvalue = pcga1->aligndata->align_res;
3550     oid = ObjectIdNew ();
3551     scp->id = oid;
3552     oid->str = StringSave ("VAST aligned residue count");
3553 
3554     dsp = DenseSegNew ();
3555     salp->segs = (Pointer) dsp;
3556     dsp->dim = 2;
3557     dsp->numseg = count;
3558     dsp->ids = SeqIdDup (mastersip);
3559     dsp->ids->next = slavesip;
3560     dsp->starts = (Int4Ptr) MemNew (count * sizeof (Int4) * 2);
3561     dsp->lens = (Int4Ptr) MemNew (count * sizeof (Int4));
3562 
3563     for (nextmasterstart = 1, nextslavestart = 1, count = 0, masterseg = master, slaveseg = slave; masterseg != NULL && slaveseg != NULL; masterseg = masterseg->next, slaveseg = slaveseg->next) {
3564       if (count > 0 && nextmasterstart < masterseg->from)
3565       { /* add a gap */
3566         dsp->starts[count * 2] = nextmasterstart - 1;
3567         dsp->starts[count * 2 + 1] = -1;
3568         dsp->lens[count] = masterseg->from - nextmasterstart;
3569         count++;
3570       }
3571       if (count > 0 && nextslavestart < slaveseg->from)
3572       { /* add a gap */
3573         dsp->starts[count * 2 + 1] = nextslavestart - 1;
3574         dsp->starts[count * 2] = -1;
3575         dsp->lens[count] = slaveseg->from - nextslavestart;
3576         count++;
3577       }
3578       dsp->starts[count * 2] = masterseg->from-1;
3579       dsp->starts[count * 2 + 1] = slaveseg->from-1;
3580       dsp->lens[count] = masterseg->to - masterseg->from + 1;
3581       count++;
3582       nextmasterstart = masterseg->to + 1;
3583       nextslavestart = slaveseg->to + 1;
3584        }
3585       feature=feature->next;
3586     }/*while feature (pbsf)*/
3587     }/*if pcMaster matches pdbname_master*/
3588     pbsfs=pbsfs->next;
3589   }/*while pbsfs*/
3590   SeqIdFree (mastersip);
3591   SeqIdFree (pdbsip);
3592   return sap;
3593 }
3594 
3595 void LIBCALL ResolveAlignChain(PDNMS pdnmsThis)
3596 {  /* moves the reserved byte value up to pmmd to indicate 
3597      chain is in alignment */
3598     PMSD pmsdThis = NULL;
3599     PDNMM pdnmmThis = NULL;
3600     PMMD pmmdThis = NULL;
3601     PDNMG pdnmgThis = NULL;
3602     PMGD pmgdThis = NULL;
3603     int i, j;
3604  
3605     if (!pdnmsThis) return;
3606     pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
3607 
3608     pdnmmThis = pmsdThis->pdnmmHead;
3609     /*printf("iMMDBid=%ld, iModels=%i\n", pmsdThis->iMMDBid, pmsdThis->iModels);*/
3610     i=1;
3611     while (pdnmmThis)
3612       {
3613          pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
3614          /*printf("I am at #358, pmmdThis->bReserved=%x\n", pmmdThis->bReserved);*/
3615          pdnmgThis = pmmdThis->pdnmgHead;
3616          j=1;
3617          while(pdnmgThis)
3618           {
3619               /*printf("loop pdnmg=%i, loop pdnmm=%i\n", j, i);*/
3620               pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
3621              /* printf("I am at #360, %x\n", pmgdThis->bReserved);*/
3622               if (pmgdThis->bReserved)
3623                 pmmdThis->bReserved = pmgdThis->bReserved;
3624 /*I added the following two lines
3625 
3626               pmmdThis->bReserved = 1;
3627               pmgdThis->bReserved = 1;
3628 */
3629               
3630               pdnmgThis = pdnmgThis->next;
3631               j++;
3632           } 
3633          pdnmmThis = pdnmmThis->next;
3634          i++;
3635       }
3636 }
3637 
3638 
3639 int LIBCALLBACK CompareScores(VoidPtr vp1, VoidPtr vp2)
3640 {
3641         BiostrucFeaturePtr pbf1, pbf2;
3642         ChemGraphAlignmentPtr pcga1 = NULL;
3643         ChemGraphAlignmentPtr pcga2 = NULL;
3644         ValNodePtr loc1 = NULL;
3645         ValNodePtr loc2 = NULL;
3646         ValNodePtr vnp1, vnp2;
3647         ValNodePtr PNTR vnpp1;
3648         ValNodePtr PNTR vnpp2;
3649 
3650         vnpp1 = (ValNodePtr PNTR) vp1;
3651         vnpp2 = (ValNodePtr PNTR) vp2;
3652         vnp1 = *vnpp1;
3653         vnp2 = *vnpp2;
3654 
3655         pbf1 = (BiostrucFeaturePtr) vnp1->data.ptrvalue;
3656         pbf2 = (BiostrucFeaturePtr) vnp2->data.ptrvalue;
3657 
3658 /* First, three cases that should not be, need to be checked for. */
3659         if (pbf1 == NULL && pbf2 != NULL)
3660                 return -1;
3661         if (pbf1 != NULL && pbf2 == NULL)
3662                 return 1;
3663         if (pbf1 == NULL && pbf2 == NULL)
3664                 return 0;
3665 
3666 
3667         /* get at the appropriate data element */
3668         
3669         loc1 = pbf1->Location_location;
3670         loc2 = pbf2->Location_location;
3671         if ((loc1) && (loc1->choice == Location_location_alignment))
3672             pcga1 = (ChemGraphAlignmentPtr) loc1->data.ptrvalue;
3673         if ((loc2) && (loc2->choice == Location_location_alignment))
3674             pcga2 = (ChemGraphAlignmentPtr) loc2->data.ptrvalue;
3675         
3676         if (pcga1 == NULL && pcga2 != NULL)
3677                 return -1;
3678         if (pcga1 != NULL && pcga2 == NULL)
3679                 return 1;
3680         if (pcga1 == NULL && pcga2 == NULL)
3681                 return 0;
3682                 
3683         if (pcga1->aligndata == NULL && pcga2->aligndata != NULL)
3684                 return -1;
3685         if (pcga1->aligndata != NULL && pcga2->aligndata == NULL)
3686                 return 1;
3687         if (pcga1->aligndata == NULL && pcga2->aligndata == NULL)
3688                 return 0;       
3689         switch(SortOn)
3690           {
3691               case 0:
3692               case 1:  /* Score */
3693                    if (pcga1->aligndata->vast_score  > pcga2->aligndata->vast_score)
3694                       return -1;
3695                    else if (pcga1->aligndata->vast_score < pcga2->aligndata->vast_score)
3696                       return 1;
3697                    else 
3698                       return 0;
3699               case 2:  /* P value (inverse sorting) */
3700                    if (pcga1->aligndata->vast_mlogp  > pcga2->aligndata->vast_mlogp)
3701                       return -1;
3702                    else if (pcga1->aligndata->vast_mlogp < pcga2->aligndata->vast_mlogp)
3703                       return 1;
3704                    else 
3705                       return 0;
3706               case 3:  /* RMSD */
3707                    if (pcga1->aligndata->rmsd  < pcga2->aligndata->rmsd)
3708                       return -1;
3709                    else if (pcga1->aligndata->rmsd > pcga2->aligndata->rmsd)
3710                       return 1;
3711                    else 
3712                       return 0;
3713               case 4:  /* Aligned residues */
3714                    if (pcga1->aligndata->align_res  > pcga2->aligndata->align_res)
3715                       return -1;
3716                    else if (pcga1->aligndata->align_res < pcga2->aligndata->align_res)
3717                       return 1;
3718                    else 
3719                       return 0;
3720               case 5: /* Identity */
3721                    if (pcga1->aligndata->other_score  > pcga2->aligndata->other_score)
3722                       return -1;
3723                    else if (pcga1->aligndata->other_score < pcga2->aligndata->other_score)
3724                       return 1;
3725                    else 
3726                       return 0;
3727               default: 
3728                  return 0;
3729           }
3730 } 
3731 
3732 void LIBCALL VastTableSort(BiostrucFeatureSetPtr pbsfs,  Int2 iSort)
3733 {
3734     ValNodePtr pvnSort = NULL;
3735     ValNodePtr pvnList = NULL;
3736     BiostrucFeaturePtr pbsf = NULL;
3737     BiostrucFeaturePtr pbsfThis = NULL;
3738     
3739     if ((!pbsfs)) return;
3740     
3741     pbsf = pbsfs->features;
3742     while(pbsf)
3743       {  /* chop out features into a ValNode list */
3744           pvnSort = ValNodeAddPointer(&pvnList, 0,  (VoidPtr) pbsf);      
3745           pbsfThis = pbsf;
3746           pbsf = pbsf->next;
3747           pbsfThis->next = NULL;  /* detach feature */
3748       }
3749     SortOn = iSort;
3750 
3751     VnpHeapSort(&pvnList,  CompareScores);
3752     
3753       /* reassemble in sorted order */
3754     pbsfs->features = (BiostrucFeaturePtr) pvnList->data.ptrvalue;
3755     pbsf = pbsfs->features;
3756     pvnSort = pvnList->next;
3757     while (pvnSort)
3758       {
3759           pbsf->next = (BiostrucFeaturePtr) pvnSort->data.ptrvalue;
3760           pbsf = pbsf->next;
3761           pvnSort = pvnSort->next;
3762       }
3763     ValNodeFree(pvnList);        
3764     return;
3765 }
3766  
3767 
3768 
3769 
3770 Boolean LIBCALL SetNodeFeatureData(PFB pfbThis, PSFD  psfdThis)
3771 {
3772   Int4Ptr pI4vFeat = NULL;
3773   PointerPtr ppvOld = NULL;
3774   int iNum,  i;
3775 
3776   /* Broadcasts the feature into the node that maps to it.
3777    * This sets the indicator in the PFB that there is a feature
3778    * associated with it.  The variable pI4vFeatID in the pfb is
3779    * a vector of pairs of id's where the first id is the feature-set
3780    * ordinal (not its true id),  and the second id is the 
3781    * true feature-id.
3782    * 
3783    * If there is ->pData associated with the node,  that pointer
3784    * is put into a parallel vector of pointers
3785    * to the pairs of id's (feature-set,  feature), 
3786    * ppFeatData.  This should be NULL for every feature
3787    * that has no associated data structures.
3788    * 
3789    */
3790    
3791   if (!pfbThis) return FALSE;
3792   if (!psfdThis) return FALSE;
3793   
3794   iNum = (int) pfbThis->iNumFeats;
3795   if ((pfbThis->pI4vFeatID) &&(iNum > 0))  
3796      pI4vFeat = pfbThis->pI4vFeatID;
3797   pfbThis->pI4vFeatID = NULL;
3798   pfbThis->pI4vFeatID = (Int4Ptr) MemNew((size_t) (iNum+1)*2*sizeof(Int4));
3799   if (!pfbThis->pI4vFeatID)
3800     {
3801       pfbThis->pI4vFeatID = pI4vFeat;
3802       return FALSE;
3803     }
3804   if (pI4vFeat)
3805     {  /* copy other id's and destroy */
3806         for (i = 0; i<(iNum*2); i++)
3807           {
3808               pfbThis->pI4vFeatID[i] = pI4vFeat[i];
3809           }
3810         MemFree(pI4vFeat);        
3811     }  
3812   pfbThis->pI4vFeatID[iNum*2] = (Int4) psfdThis->pdnsfsSet->choice;  
3813   pfbThis->pI4vFeatID[(iNum*2)+1] = psfdThis->iId; 
3814  
3815  
3816   if (psfdThis->pData) 
3817    {
3818      if (pfbThis->ppvFeatData)
3819        ppvOld = pfbThis->ppvFeatData;
3820      pfbThis->ppvFeatData = NULL;
3821      pfbThis->ppvFeatData = (PointerPtr) MemNew((size_t) (iNum+1)*sizeof(Pointer));
3822      if (!pfbThis->ppvFeatData)
3823        {
3824            pfbThis->ppvFeatData = ppvOld;
3825            return FALSE;
3826        }
3827      if (ppvOld)
3828        {  /* copy other pointers, and free old vector */
3829           for (i = 0; i < (iNum); i++)
3830             pfbThis->ppvFeatData[i] = ppvOld[i];
3831           MemFree(ppvOld);
3832        }   
3833       /* assign the data pointer */ 
3834      pfbThis->ppvFeatData[iNum] = psfdThis->pData;
3835    } 
3836  
3837   pfbThis->iNumFeats++;
3838   return TRUE;
3839 }
3840           
3841 ValNodePtr LIBCALL MakeRegionNodeList(PDNMS pdnmsThis, RegionPntrsPtr prgpThis)
3842 {
3843  
3844    
3845     /* 
3846 
3847     This traverses the ASN.1 RegionPntrs list and makes a list of PFB's  
3848     prgpThis->model_id;
3849     ValNodeFindNext(prgpThis->region ... );
3850     
3851     RegionCoordinates ...  (model coord set, - does the lookup )
3852     
3853 
3854 
3855     RegionBoundary... (sphere,  cone,  cylinder,  brick - does the bounding calc 
3856                        and returns entities within )
3857      */
3858      
3859    return NULL;  
3860 }
3861 
3862 void LIBCALL InstallAlignedSlave(PDNMS pdnmsMaster, ChemGraphAlignmentPtr pcgaAlign,
3863                                  PSFD psfdThis, Boolean Chain, CharPtr SlaveChain) {
3864    Int4 iId = 0;
3865    BiostrucPtr pbsSlave = NULL, pbsTemp = NULL;
3866    PDNMS pdnmsSlave = NULL;
3867    PMSD  pmsdSlave = NULL;
3868    PFB   pfbThis = NULL;
3869    ValNodePtr pvnThis = NULL;
3870    ValNodePtr pvnList = NULL;
3871    ValNodePtr pvnLoc = NULL;
3872    BiostrucIdPtr pbsidThis = NULL;
3873    ChemGraphPntrsPtr pcgpThis = NULL;
3874    PSFS psfsThis = NULL;
3875    Pointer ptr = NULL;
3876    Boolean bTest;
3877    Int2 iTest;
3878 
3879 #ifdef _DEBUG_9
3880 printf("in InstallAlignedSlave \n");
3881 #endif  
3882 
3883    if (!pcgaAlign) return;
3884    if (!pdnmsMaster) return;
3885    
3886    SetSlaveModelstruc(NULL);
3887    SetSelectedModelstruc(pdnmsMaster);
3888    SetNeighborOn(); /* sets master to selected */
3889    /* sets slave to first on master's list if slave is null */
3890 
3891    if (pcgaAlign->dimension != 2) return;
3892 
3893 #ifdef _DEBUG_9
3894 printf("still in InstallAlignedSlave \n");
3895 #endif   
3896 
3897   /* the second id is the slave */
3898   pbsidThis = ValNodeFindNext(pcgaAlign->biostruc_ids,NULL,BiostrucId_mmdb_id);
3899   if (pbsidThis)
3900     {
3901         pbsidThis = ValNodeFindNext(pcgaAlign->biostruc_ids,pbsidThis,BiostrucId_mmdb_id);
3902         if (pbsidThis) iId = (Int4) pbsidThis->data.intvalue;  
3903     }   
3904    
3905    if (iId == 0) 
3906      {
3907 #ifdef _DEBUG_9
3908 printf("Biostruc Slave Fetch failed %d \n", (int) iId);
3909 #endif  
3910       return; 
3911     }
3912 
3913    pbsSlave = MMDBBiostrucGet(iId, ALLSIMPLEMDL, 3);
3914 /*****************************************************************************/
3915 /* This is code added by KA so that we only have to call one MMDBBiostrucGet */   
3916 /*****************************************************************************/
3917    if (Chain == TRUE) {
3918      if (SlaveChain[0] != ' ') {
3919        pbsTemp = (BiostrucPtr)PruneBiostruc(pbsSlave, SlaveChain);
3920        pbsSlave = NULL;
3921        pbsSlave = pbsTemp;
3922       }
3923    }  
3924 /*****************************************************************************/
3925    if (!pbsSlave) { 
3926           SetNeighborOff(); 
3927 
3928 #ifdef _DEBUG_9
3929 printf("Biostruc Slave Fetch failed %d \n", (int) iId);
3930 #endif  
3931           return; }
3932    else
3933     {
3934 #ifdef _DEBUG_9
3935 printf("Biostruc Slave Fetched! %d \n", (int) iId);
3936 #endif 
3937     }
3938    pdnmsSlave = MakeAModelstruc(pbsSlave);
3939    /* hangs off of master and sets slave and sets selected=master */
3940    SetNeighborOff();
3941    /* resets selected to master only */
3942    if (!pdnmsSlave) {
3943 #ifdef _DEBUG_9
3944 printf("Biostruc Slave Convert failed %d \n", (int) iId);
3945 #endif 
3946      return;
3947     }
3948 
3949    SetSlaveModelstruc(pdnmsSlave);  /* defaults *on* next time SetNeighborOn called */
3950   
3951    /* get the master data - > apply alignment CGP to master structure */
3952    /* using SetNodeFeatureData */
3953 
3954    pvnThis = NULL;
3955    pvnThis = pcgaAlign->alignment;
3956    if (pvnThis)
3957                    {
3958     /********TEMPORARY******/
3959                      pcgpThis = ( ChemGraphPntrsPtr ) pvnThis;
3960                      pvnList = MakeChemGraphNodeList(pdnmsMaster, pcgpThis);
3961                      if (!pvnList)  {
3962 #ifdef _DEBUG_9
3963 printf("Master MakeChemGraphNodeList failed \n");
3964 #endif 
3965                         return; }
3966                      pvnLoc = pvnList;
3967                      while (pvnLoc)
3968                        {
3969                            pfbThis = (PFB) pvnLoc->data.ptrvalue;
3970                            pfbThis->bReserved++;
3971                            pvnLoc = pvnLoc->next;
3972                        }  /* while pvnLoc */
3973                       ValNodeFree(pvnList);
3974                       pvnList = NULL;
3975 
3976                      pcgpThis = ( ChemGraphPntrsPtr ) pvnThis->next;
3977                      pvnList = MakeChemGraphNodeList(pdnmsSlave, pcgpThis);
3978                      if (!pvnList)  {
3979 #ifdef _DEBUG_9
3980 printf("Master MakeChemGraphNodeList failed \n");
3981 #endif 
3982                         return; }
3983                      pvnLoc = pvnList;
3984                      while (pvnLoc)
3985                        {
3986                          pfbThis = (PFB) pvnLoc->data.ptrvalue;
3987                          /* register feature in each location */
3988                            bTest = SetNodeFeatureData( pfbThis,  psfdThis); 
3989                            psfdThis->bPFBtype = (psfdThis->bPFBtype | pfbThis->bMe); 
3990                       /********TEMPORARY******/
3991                            pfbThis->bReserved++;
3992                            if (bTest == FALSE)  {
3993 #ifdef _DEBUG_9
3994 printf("Master SetNodeFeatureData failed \n");
3995 #endif 
3996                         return; } 
3997                          pvnLoc = pvnLoc->next;
3998                        }  /* while pvnLoc */
3999                       ValNodeFree(pvnList);
4000                       pvnList = NULL;
4001                    }  /* if pvnThis */
4002   
4003                
4004    TransformToDNTRN((PDNTRN PNTR) &ptr, pcgaAlign->transform ); 
4005    
4006    /* apply the transform to the slave */
4007    
4008    iTest = TraverseModels( pdnmsSlave,
4009                                 TRAVERSE_ATOM, 
4010                                 0, ptr,
4011                                 (pNodeFunc)(DoApplyTransform));
4012    iTest = TraverseModels( pdnmsSlave, 
4013                                 TRAVERSE_SOLID, 
4014                                 0, ptr,
4015                                 (pNodeFunc)(DoApplyTransform));
4016 #ifdef _DEBUG_9
4017 printf("Transformed Models \n");
4018 #endif 
4019                                 
4020    /* free the transform data */
4021    
4022    FreeDNTRN((PDNTRN) ptr);
4023   
4024 
4025    /* set dirty bit */ 
4026    pmsdSlave = (PMSD) pdnmsSlave->data.ptrvalue;
4027    pmsdSlave->bUpdate = (Byte) (pmsdSlave->bUpdate | (Byte) AM_CHANGED);
4028    /* enforce an MMDB-ID change */
4029    pmsdSlave->iHashChange = MakeHashChange(pdnmsSlave);
4030  
4031   /* Do nothing with the domain data... */
4032   /* that's all folks, now just use SetNeighborOn()... */
4033   /* selected = master after this is through */
4034   return;
4035 }
4036             
4037 
4038 PSFD  LIBCALL InstallStrucFeature(PDNMS pdnmsThis, PDNSFS pdnsfsThis, BiostrucFeaturePtr pbsfThis,
4039                                   Boolean Chain, CharPtr SlaveChain)
4040 {
4041 
4042 /* This procedure adds a new Feature to the Feature-Set registry
4043  * specified.  If there is "global" data to be kept (e.g. color, 
4044  * render,  transform values,  camera values, label values
4045  */
4046   PSFF psffFunc = NULL;
4047   PSFS psfsThis = NULL;
4048   PSFD psfdTemp = NULL;
4049   Uint1 iColor = 0;
4050   ColorPropPtr pcpThis = NULL;
4051    
4052   
4053   if (!(pdnsfsThis || pbsfThis)) return NULL;
4054  
4055 #ifdef _DEBUG_9
4056 printf("in InstallStrucFeature \n");
4057 #endif   
4058 
4059   psfsThis = (PSFS) pdnsfsThis->data.ptrvalue;
4060   psfdTemp = psfsThis->psfdFeats;
4061   psfsThis->psfdFeats = NewSFD(); /* insert new feature at head LIFO */
4062   if (!psfsThis->psfdFeats) 
4063     {
4064       psfsThis->psfdFeats = psfdTemp;
4065       return NULL;
4066     }
4067   psfsThis->psfdFeats->next = psfdTemp; /* attach rest of features */
4068   psfsThis->psfdFeats->iType = pbsfThis->type;
4069   psfsThis->psfdFeats->pdnsfsSet = pdnsfsThis;
4070   psfsThis->psfdFeats->iId = pbsfThis->id;
4071   if (pbsfThis->name) psfsThis->psfdFeats->pcName = StringSave(pbsfThis->name);
4072  
4073 
4074   psffFunc = DoesFeatureTypeHaveFuncs(pbsfThis->type);
4075   if (psffFunc)
4076     {  /* this uses the installed functions associated with the feature type */
4077        /* to copy from the User-object into a newly-allocated data object */
4078        /* which is owned by psfsThis->psfdFeats */
4079 
4080         (*(psffFunc->pFeatToCFn)) ( (Pointer PNTR) &(psfsThis->psfdFeats->pData),  
4081                           (UserObjectPtr PNTR) &pbsfThis->Property_property->data.ptrvalue);
4082     }
4083   
4084   
4085 /* this is to instantiate the property data */     
4086   switch(pbsfThis->type)
4087      {
4088         case Feature_type_color:
4089           /* register color - gets back index into palette */
4090           pcpThis = (ColorPropPtr) pbsfThis->Property_property->data.ptrvalue;
4091           iColor = Call3DColorProc(pcpThis);
4092           psfsThis->psfdFeats->iColor = iColor;   
4093           break;
4094           
4095         case Feature_type_render:
4096           /* register render code */
4097           psfsThis->psfdFeats->iRender = (Int4) pbsfThis->Property_property->data.intvalue;
4098           break;
4099 
4100         case Feature_type_transform:
4101           TransformToDNTRN((PDNTRN PNTR) &psfsThis->psfdFeats->pData, 
4102                                (TransformPtr) pbsfThis->Property_property->data.ptrvalue);
4103           break;
4104           
4105         case Feature_type_camera:
4106         case Feature_type_script:
4107            /* detach & save the property data in ASN.1 format */
4108 /*        psfsThis->psfdFeats->pData = (Pointer) pbsfThis->Property_property; */
4109 /*        pbsfThis->Property_property = NULL;*/
4110           break;
4111           
4112         case Feature_type_cn3dstate:
4113         case Feature_type_comment:
4114         case Feature_type_label:        
4115            /* globally applied user-object in use */
4116             /* the ASN.1 has been converted to the C by registered function */                   
4117           break;
4118           
4119         case Feature_type_other:  
4120           break;  /* other not allowed to have data... */  
4121           
4122         case Feature_type_multalign:
4123         case Feature_type_alignment:
4124 
4125 #ifdef _DEBUG_9
4126 printf("in  alignment case \n");
4127 #endif               
4128             /* cast property to alignment ASN.1 type */
4129             /* detach location & save it in ASN.1 format  */
4130              psfsThis->psfdFeats->pData = (Pointer) pbsfThis->Location_location->data.ptrvalue;
4131              pbsfThis->Location_location = NULL; 
4132              InstallAlignedSlave(pdnmsThis, 
4133                                  (ChemGraphAlignmentPtr) psfsThis->psfdFeats->pData, 
4134                                   psfsThis->psfdFeats, Chain, SlaveChain);
4135           break;
4136         case Feature_type_similarity:
4137         case Feature_type_region:
4138           break;   
4139         case Feature_type_indirect:
4140             /* deal with mapping the chem-graph-ptrs and "prototypes" */ 
4141           break;
4142          
4143       /* these don't get put into flag bytes */
4144       /* but they all have subgraph locations */
4145      /*   case Feature_type_helix:
4146         case Feature_type_strand:
4147         case Feature_type_sheet:
4148         case Feature_type_turn:
4149         case Feature_type_site:              
4150         case Feature_type_footnote:
4151         case Feature_type_subgraph:
4152         case Feature_type_core:
4153         case Feature_type_supercore: */
4154         default: ;
4155           /* these have no special indication other than Type */
4156      }  /* switch generic feature-type */
4157   
4158   return psfsThis->psfdFeats;
4159 }
4160 
4161 Int2 LIBCALL BiostrucAddFeature(BiostrucFeatureSetPtr pbsfsThis, PDNMS pdnmsThis,
4162                                 Boolean Chain, CharPtr SlaveChain)
4163 {
4164   BiostrucFeaturePtr pbsfThis = NULL;
4165   PDNSFS pdnsfsThis = NULL;
4166   PSFS psfsThis = NULL;
4167   PSFD psfdThis = NULL;
4168   PMSD pmsdThis = NULL;
4169   PMGD pmgdThis = NULL;
4170   PFB pfbThis = NULL;
4171   ValNodePtr pvnThis = NULL;
4172   DValNodePtr pdvnThis = NULL;
4173   ChemGraphPntrsPtr pcgpThis = NULL;
4174   RegionPntrsPtr prgpThis = NULL;
4175   CharPtr pcThis = NULL;
4176   ValNodePtr pvnList = NULL;
4177   ValNodePtr pvnLoc = NULL;
4178   Byte bSet = 0;
4179   Boolean ncbiSS = FALSE;
4180   Boolean pdbSS = FALSE;
4181   Boolean pdbSite = FALSE;
4182   Boolean pdbFootnote  = FALSE;
4183   Boolean ncbiDomain = FALSE;
4184   Boolean ncbiVast = FALSE;
4185   Boolean bTest = FALSE;
4186 
4187 
4188 #ifdef _DEBUG_9
4189 printf("in BiostrucAddFeature \n");
4190 #endif      
4191   if (!pbsfsThis) return 0;
4192   if (!pdnmsThis) return 0;
4193   
4194   pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
4195  
4196 /* first chore is to make a new feature-set node off this structure */
4197 
4198   pdnsfsThis = NewDNSFS(&pmsdThis->pdnsfsHead, pmsdThis->iFeatures+1); 
4199   if (!pdnsfsThis) return 0;
4200   pmsdThis->iFeatures++;
4201   psfsThis = (PSFS) pdnsfsThis->data.ptrvalue;
4202   psfsThis->iId = pbsfsThis->id;
4203   
4204 /*psfsThis->pvnDescr = (ValNodePtr) pbsfsThis->descr; */ /* link stub for descr */
4205 /*pbsfsThis->descr = NULL; */ /* detach and save from free-ing descr  */
4206                /* yanli comment the above two line out, instead do the following */
4207   psfsThis->pvnDescr = (ValNodePtr)AsnIoMemCopy((ValNodePtr) pbsfsThis->descr, (AsnReadFunc)BiostrucFeatureSetDescrAsnRead, (AsnWriteFunc)BiostrucFeatureSetDescrAsnWrite); 
4208 
4209   pvnThis = NULL;
4210   pvnThis = ValNodeFindNext(psfsThis->pvnDescr, NULL,  BiostrucFeatureSetDescr_name); 
4211   if (pvnThis)
4212     {
4213      pcThis = (CharPtr) pvnThis->data.ptrvalue;
4214      psfsThis->pcName = StringSave(pcThis);
4215 
4216   /* these strings categorize known feature sets that come bundled
4217    with MMDB data */
4218 
4219      if (!(StringCmp(pcThis, STRING_NCBI_SS)))
4220        {
4221          ncbiSS = TRUE;
4222        }
4223      if (!(StringCmp(pcThis, STRING_PDB_SS)))
4224        {
4225          pdbSS = TRUE;
4226        }  
4227      if (!(StringCmp(pcThis, STRING_PDB_SITE)))
4228        {
4229          pdbSite = TRUE;
4230        }       
4231      if (!(StringCmp(pcThis, STRING_PDB_FOOTNOTE)))
4232        {
4233          pdbFootnote = TRUE;
4234        }  
4235      if (!(StringCmp(pcThis, STRING_NCBI_DOMAIN)))
4236        {
4237          ncbiDomain = TRUE;
4238        }
4239      if (!(StringCmp(pcThis, STRING_NCBI_VAST)))
4240        {
4241        /* lyg: this no longer works.  The id is simply the mmdb # hash of the 2 proteins */
4242          ncbiVast = TRUE;
4243        }
4244     }
4245   
4246 
4247   pbsfThis = pbsfsThis->features;
4248   while (pbsfThis)
4249     {  /* walk through each feature */
4250 
4251       if (ncbiSS || pdbSS)
4252         {  /* set bytes for secondary strucutre flags */
4253         switch (pbsfThis->type)
4254           {
4255             case Feature_type_helix:
4256               bSet = (Byte) SS_HELIX;
4257               break;
4258             case Feature_type_strand:
4259               bSet =  (Byte) SS_STRAND;
4260               break;
4261             case Feature_type_sheet:
4262               bSet = (Byte) (SS_STRAND | SS_SHEET);
4263               break;
4264             case Feature_type_turn:
4265               bSet = (Byte) SS_TURN;
4266               break;
4267             default:
4268              return -1;
4269           }      
4270        }  /* if bSet - Sec stru to set */
4271 
4272      /* Installs feature entry in feature registry */   
4273      /* any data is attached and owned by the psfdThis node */
4274 
4275      psfdThis = InstallStrucFeature(pdnmsThis, pdnsfsThis, pbsfThis, Chain, SlaveChain);
4276 
4277      if (!psfdThis) return 0;
4278      
4279      /* this registers the installed feature into the appropriate PFB node(s) */
4280      switch(psfdThis->iType)
4281            {
4282  
4283              case Feature_type_alignment:
4284              case Feature_type_multalign:
4285                /* these are already registered  */
4286                 break;
4287              case Feature_type_similarity:
4288              case Feature_type_region:
4289              case Feature_type_cn3dstate:
4290              case Feature_type_camera:
4291              case Feature_type_script:
4292                       /*  locations are assumed MSD */
4293                 bTest = SetNodeFeatureData((PFB) pmsdThis,  psfdThis );
4294                 psfdThis->bPFBtype = pmsdThis->bMe;  /* set to AM_MSD */
4295 
4296                break;
4297  /*
4298              case Feature_type_helix:
4299              case Feature_type_strand:
4300              case Feature_type_sheet:
4301              case Feature_type_turn:
4302              case Feature_type_site:              
4303              case Feature_type_footnote:
4304              case Feature_type_subgraph:
4305              case Feature_type_color:
4306              case Feature_type_render:
4307              case Feature_type_comment:
4308              case Feature_type_label:
4309              case Feature_type_transform:
4310              case Feature_type_core:
4311              case Feature_type_supercore:
4312              case Feature_type_other:
4313    */
4314              default:
4315                   /* do chem-graph-pointers */
4316                  pvnThis = NULL;
4317                  pvnThis = ValNodeFindNext(pbsfThis->Location_location, NULL, Location_location_subgraph);
4318                  if (pvnThis)
4319                    {
4320                      pcgpThis = (ChemGraphPntrsPtr) pvnThis->data.ptrvalue;
4321                      pvnList = MakeChemGraphNodeList(pdnmsThis, pcgpThis);
4322                      if (!pvnList) return -1;
4323                      pvnLoc = pvnList;
4324                      while (pvnLoc)
4325                        {
4326                          pfbThis = (PFB) pvnLoc->data.ptrvalue;
4327                          
4328                          /* register feature in each location node */ 
4329                          bTest = SetNodeFeatureData( pfbThis,  psfdThis );
4330                          if (bTest == FALSE) return 0; 
4331                          psfdThis->bPFBtype = (psfdThis->bPFBtype | pfbThis->bMe);
4332                            /* special case for Sec Stru - set flag bits & strings */
4333                          if (ncbiSS || pdbSS || ncbiDomain)
4334                            {
4335                               pmgdThis = (PMGD) pfbThis;
4336                               if (pmgdThis->bMe != AM_MGD) return -1;
4337                               if (ncbiSS)
4338                                     { 
4339                                         pmgdThis->pcNCBISS = psfdThis->pcName;
4340                                         pmgdThis->bNCBISecStru = pmgdThis->bNCBISecStru | bSet;
4341                                     }
4342                                 if (pdbSS) 
4343                                     {
4344                                         pmgdThis->pcPDBSS = psfdThis->pcName;
4345                                         pmgdThis->bPDBSecStru = pmgdThis->bPDBSecStru |  bSet;
4346                                     } 
4347                                 if (ncbiDomain)
4348                                     {
4349                                         pmgdThis->iDomain = pbsfThis->id;
4350                                     }
4351                             }     /* special cases */
4352                          pvnLoc = pvnLoc->next;
4353                        }  /* while pvnLoc */
4354                       ValNodeFree(pvnList);
4355                       pvnList = NULL;
4356                    }  /* if pvnThis */
4357                    
4358                    
4359                    /* do regions */
4360                    
4361                  pvnThis = NULL;
4362                  pvnThis = ValNodeFindNext(pbsfThis->Location_location, NULL, Location_location_region);
4363                  if (pvnThis)
4364                    {
4365                      prgpThis = (RegionPntrsPtr) pvnThis->data.ptrvalue;
4366                      pvnList = MakeRegionNodeList(pdnmsThis, prgpThis);
4367                      if (!pvnList) return -1;
4368                      pvnLoc = pvnList;
4369                      while (pvnLoc)
4370                        {
4371                          pfbThis = (PFB) pvnLoc->data.ptrvalue;
4372                          /* register feature in each location */
4373                          bTest = SetNodeFeatureData( pfbThis,  psfdThis);  
4374                          psfdThis->bPFBtype = (psfdThis->bPFBtype | pfbThis->bMe); 
4375                          if (bTest == FALSE) return 0;
4376                          pvnLoc = pvnLoc->next;
4377                        }  /* while pvnLoc */
4378                       ValNodeFree(pvnList);
4379                       pvnList = NULL;
4380                    }  /* if pvnThis */
4381   
4382             }  /* switch generic feature-type */
4383   
4384      pbsfThis = pbsfThis->next;
4385    } /* while pbsfThis : next feature in feature-set */
4386   
4387   /* now that they are instantiated - the caller can delete the ASN.1 forms */
4388   /* of the features - we have kept the descr and other non-volatile elements */
4389   /* like camera's and tranforms, and null-ed out their pointers */  
4390   
4391   return TRUE;
4392 }
4393  
4394 
4395 static Int4 FindGiSeq(ValNodePtr pvnSeqId)
4396 {
4397     ValNodePtr pvn;
4398     pvn = ValNodeFindNext(pvnSeqId, NULL, SEQID_GI);
4399     if (pvn)
4400       return (Int4) pvn->data.intvalue;
4401     else
4402       return 0;
4403 }
4404 
4405 
4406 
4407 
4408 Byte LIBCALL SetBondOrder(Byte bThis,  Int2 iType)
4409 {
4410        switch (iType)
4411         {
4412          case Bond_order_partial_double:
4413                 return (Byte)(bThis | BOND_PDOUBLE);
4414          case Bond_order_aromatic:
4415                 return (Byte)(bThis | BOND_PDOUBLE | BOND_DOUBLE);
4416          case Bond_order_double:
4417                 return (Byte)(bThis | BOND_DOUBLE);
4418          case Bond_order_triple:
4419                 return (Byte)(bThis | BOND_TRIPLE);
4420          case Bond_order_other:
4421          case Bond_order_unknown:
4422                 return bThis;
4423          case Bond_order_single:
4424          default:
4425                 bThis = (Byte)(bThis | BOND_SINGLE);
4426         }
4427   return bThis; 
4428 }
4429 
4430 
4431 /*
4432  * Biostruc2Modelstruc is the main routine to fetch and convert a 
4433  * Biostruc into Modelstruc form - which is a hybrid ASN.1 / hand
4434  * made data structure.
4435  * 
4436  */
4437 
4438 
4439 
4440  
4441 
4442 PDNMS LIBCALL Biostruc2Modelstruc(PDNMS PNTR ppdnmsList, BiostrucPtr pbsBS, PRGD prgdDict, 
4443                          Byte  bExtent, Int2 iChoice,  
4444                          CharPtr pcFetch, Int2 iType)
4445 /* pdnmsList is the master list of structures or NULL for first */
4446 /* if pbsBS = NULL - use the pcFetch functions.. */
4447 {
4448         BiostrucModelPtr pbsmThis = NULL;
4449         IntraResidueBondPtr pirbThis = NULL;
4450         InterResidueBondPtr pierbThis = NULL;
4451         ValNodePtr pvnThis = NULL;
4452         AtomPntrPtr papIndex = NULL;
4453         
4454         PDNMS pdnmsThis = NULL; 
4455         PMSD pmsdThis = NULL; 
4456         PDNMM pdnmmThis = NULL; 
4457         PDNMM pdnmmPrev = NULL; 
4458         PMMD pmmdThis = NULL; 
4459         PDNMG pdnmgThis = NULL;
4460         PDNMG pdnmgPrev = NULL; 
4461         PMGD pmgdThis = NULL;  
4462         PVNMA pvnmaThis = NULL;
4463         PVNMA pvnmaPrev = NULL;
4464         PMAD pmadThis = NULL;
4465         PVNMB pvnmbThis = NULL;
4466         PVNMB pvnmbPrev = NULL;
4467         PMBD pmbdThis = NULL;
4468         Int2 iTest = TRUE;
4469         Int4 iGraphId = 0;
4470                                          
4471 
4472         Int4 iMolCount = 0; 
4473         Int4 iResCount = 0;
4474         Int4 iAtomCount = 0;
4475         Int4 iBondCount = 0;
4476         Int4 iModelCount = 0;
4477 
4478         /* version 4.0 additions */
4479         BiostrucIdPtr pbsidThis = NULL;
4480         BiostrucDescrPtr pbsdrThis = NULL;
4481         BiomolDescrPtr pbmdrThis = NULL;
4482         MoleculeGraphPtr pmgThis = NULL;
4483         ResiduePtr prThis = NULL; 
4484         ValNodePtr pvnrgThis = NULL; /* a ValNodePtr */
4485         ResidueGraphPtr prgDict = NULL;  /* a ValNodePtr */
4486         ValNodePtr pvnStrings = NULL;
4487         AtomPtr  pAtom;
4488         BiostrucFeatureSetPtr pbsfsThis = NULL;
4489         BiostrucResidueGraphSetPntrPtr ppdDict = NULL;
4490 
4491         Int4 iMolGraphType = 0; 
4492 
4493         
4494          /* ErrSetFatalLevel - allow malloc failure to return */
4495 #ifdef _DEBUG_0
4496         printf("Biostruc2Modelstruc\n");
4497 #endif
4498         if (!pbsBS) pbsBS = FetchBS(pcFetch,iType, ALLMDL,3000,bExtent);
4499 
4500         
4501         /* traverse the chemical graph to make C memory
4502          * strucures */
4503 
4504         if (pbsBS)
4505           {
4506 
4507            pdnmsThis = NewDNMS(ppdnmsList, iChoice); 
4508            if (!pdnmsThis) goto allocerror;
4509            pmsdThis = (PMSD) pdnmsThis->data.ptrvalue; /*set the pointer to the data */
4510 
4511      pmsdThis->pdnmsSlaves = NULL;  
4512      pmsdThis->bAligned = 0;  /* nothing aligned to it */
4513      pmsdThis->pbAligned = NULL;
4514      pmsdThis->bMaster = TRUE; /* default to being a master structure */
4515      pmsdThis->iMimeType = 0;
4516 
4517                    /* non-MMDB dependent info */ 
4518            pmsdThis->pcSName = StringSave(pcFetch);
4519            pmsdThis->iType = iType;  /* copy iType also used in fetch for future ref */ 
4520 
4521                   /* MMDB dependent stuff */
4522    
4523 
4524            pbsidThis = ValNodeFindNext(pbsBS->id,NULL,BiostrucId_mmdb_id);
4525            if (pbsidThis)
4526              {
4527                 pmsdThis->iMMDBid = (Int4) pbsidThis->data.intvalue;  /* Get MMDB id no (only the first one) */ 
4528              }
4529            else 
4530              {
4531                 ;  /* assign it a hashed ID number based on iChoice?? */
4532              }
4533            pbsdrThis = ValNodeFindNext(pbsBS->descr,NULL,BiostrucDescr_name);
4534              /* just want the first one */
4535            pmsdThis->pcPDBName = StringSave(PDBNAME_DEFAULT);
4536            if (pbsdrThis)
4537              {
4538                 StringNCpy(pmsdThis->pcPDBName,(CharPtr) pbsdrThis->data.ptrvalue, sizeof(pmsdThis->pcPDBName));
4539              }
4540 #ifdef _DEBUG_0
4541 printf("[%d]id [%s] pdb\n",pmsdThis->iMMDBid,pmsdThis->pcPDBName); 
4542 printf("<%s>\n",pmsdThis->pcSName);
4543 #endif               
4544             /* ATTACH THE ENTIRE Biostruc */ 
4545            pmsdThis->pbsBS = pbsBS;  
4546            
4547            /* the chemical_graph and model_structure portions are erased at the end  */
4548            
4549            pmsdThis->pGraphDescr = pbsBS->chemical_graph->descr; /* save the chem-graph descr (ValNode) */
4550            pmsdThis->bVisible = 1;  /* can be seen */
4551            pbmdrThis = ValNodeFindNext(pbsBS->chemical_graph->descr, NULL, BiomolDescr_name);
4552            if (pbmdrThis)
4553              {
4554                  pmsdThis->pcChemName = StringSave((CharPtr)pbmdrThis->data.ptrvalue); 
4555              }
4556            else 
4557              {
4558                  pmsdThis->pcChemName = StringSave(STRING_UNK_PDB);
4559              }
4560            
4561            pbmdrThis = ValNodeFindNext(pbsBS->chemical_graph->descr, NULL, BiomolDescr_pdb_class);
4562            if (pbmdrThis)
4563              {
4564               pmsdThis->pcPdbClass = StringSave((CharPtr)pbmdrThis->data.ptrvalue);
4565              } 
4566            else 
4567              {
4568                 pmsdThis->pcPdbClass = StringSave(STRING_UNK_PDB); 
4569              }
4570              
4571            pbmdrThis = ValNodeFindNext(pbsBS->chemical_graph->descr, NULL, BiomolDescr_pdb_source);
4572            if (pbmdrThis)
4573              {
4574               pmsdThis->pcPdbSource = StringSave((CharPtr)pbmdrThis->data.ptrvalue);
4575              } 
4576            else 
4577              {
4578                 pmsdThis->pcPdbSource = StringSave(STRING_UNK_PDB); 
4579              }
4580              
4581            pbmdrThis = ValNodeFindNext(pbsBS->chemical_graph->descr, NULL, BiomolDescr_assembly_type);
4582            if (pbmdrThis)
4583              {
4584                pmsdThis->iPDBType = (Int2) pbmdrThis->data.intvalue;
4585              }
4586            else 
4587              {
4588                pmsdThis->iPDBType = Assembly_Type_Other;
4589              }  
4590            
4591           pbsBS->chemical_graph->descr = NULL;  /* stops the free-er from killing it prematurely */
4592           
4593            
4594 #ifdef _DEBUG_0      
4595             printf("{%s} pcChemName \n", pmsdThis->pcChemName); 
4596             printf("{%s} pcPdbClass \n", pmsdThis->pcPdbClass); 
4597             printf("{%s} pcPdbSource\n", pmsdThis->pcPdbSource);
4598 #endif     
4599 
4600 /* flesh-out the chemical graph */
4601             pmgThis = pbsBS->chemical_graph->molecule_graphs;
4602             ProgMon("Instantiating Chemical Graph");
4603             while (pmgThis) /* walk up the molecule_graphs */
4604              {
4605                 iMolCount++; 
4606                 iResCount = 0; 
4607                 pdnmmPrev = pdnmmThis;
4608                 pdnmmThis = NewDNMM(NULL, (Int2) pmgThis->id); 
4609                 if (!pdnmmThis) goto allocerror;
4610                 pmmdThis = (PMMD) pdnmmThis->data.ptrvalue; /*set the pointer to the data */
4611                 pmmdThis->pfbParent = (PFB) pmsdThis;
4612     
4613     /* turn off the selection.  lyg */
4614     pmmdThis->pdnmgStartSelect = NULL;
4615     pmmdThis->pdnmgEndSelect = NULL;
4616 
4617                 /* get the sequence for each one */ 
4618                 pmmdThis->iChainId = pmgThis->id; 
4619 #ifdef _DEBUG_0
4620 printf("\n<%d> ChainId ", pmmdThis->iChainId); 
4621 #endif
4622                 /* this gets chain names, like A,B etc... */
4623                 
4624                 pbmdrThis = ValNodeFindNext(pmgThis->descr, NULL, BiomolDescr_name);
4625                 if (pbmdrThis)
4626                   {
4627                    pmmdThis->pcMolName = StringSave((CharPtr)pbmdrThis->data.ptrvalue); 
4628                   }
4629                 else 
4630                   {
4631                     pmmdThis->pcMolName = StringSave(STRING_UNK_MOL);
4632                   }
4633                 
4634 #ifdef _DEBUG_0 
4635 printf("'%s' name \n", pmmdThis->pcMolName); 
4636 #endif
4637                 
4638         
4639                 pbmdrThis = ValNodeFindNext(pmgThis->descr, NULL, BiomolDescr_molecule_type);
4640                 if (pbmdrThis)
4641                   {
4642                     iMolGraphType = (Int4) pbmdrThis->data.intvalue;
4643                   } 
4644                 else 
4645                   {  
4646                     iMolGraphType = Molecule_type_other;
4647                   }
4648                   
4649                 pmmdThis->pMolDescr = pmgThis->descr; /* save molecule descr */
4650                 pmgThis->descr = NULL; /* stops from premature free-ing when chem-graph turfed */
4651                   
4652                 switch (iMolGraphType)
4653                   {
4654                    case Molecule_type_dna:
4655 #ifdef _DEBUG_2
4656 printf("dna ");
4657 #endif
4658                       pmmdThis->bWhat = (Byte) AM_DNA;
4659                       pmsdThis->bWhat = (Byte) (pmsdThis->bWhat | (Byte) AM_DNA);
4660                       pmmdThis->iGi = FindGiSeq(pmgThis->seq_id);
4661                       break;
4662                    case Molecule_type_rna:
4663 #ifdef _DEBUG_2
4664 printf("rna ");
4665 #endif 
4666                       pmmdThis->bWhat = (Byte) AM_RNA; 
4667                       pmsdThis->bWhat = (Byte) (pmsdThis->bWhat | (Byte) AM_RNA);
4668                       pmmdThis->iGi = FindGiSeq(pmgThis->seq_id);
4669                     
4670                       break;
4671                    case Molecule_type_protein:
4672 #ifdef _DEBUG_2
4673 printf("protein ");
4674 #endif
4675 
4676                       pmmdThis->bWhat = (Byte) AM_PROT;
4677                       pmsdThis->bWhat = (Byte) (pmsdThis->bWhat | (Byte) AM_PROT);
4678                       pmmdThis->iGi = FindGiSeq(pmgThis->seq_id);
4679                     
4680                       break; 
4681                    case Molecule_type_other_biopolymer:
4682 #ifdef _DEBUG_2
4683 printf("other biopolymer ");
4684 #endif
4685                       pmmdThis->bWhat = (Byte) AM_POLY; 
4686                       pmsdThis->bWhat = (Byte) (pmsdThis->bWhat | (Byte)AM_POLY);
4687                       break; 
4688                    case Molecule_type_solvent:
4689 #ifdef _DEBUG_2
4690 printf("solvent ");
4691 #endif             
4692                       pmmdThis->bWhat = (Byte) AM_SOL;
4693                       pmsdThis->bWhat = (pmsdThis->bWhat | (Byte) AM_SOL);
4694                       break; 
4695                    case Molecule_type_other_nonpolymer:
4696 #ifdef _DEBUG_2
4697 printf("other ");
4698 #endif                   
4699                       pmmdThis->bWhat = (Byte) AM_HET;
4700                       pmsdThis->bWhat = (pmsdThis->bWhat | (Byte) AM_HET);
4701                       break; 
4702                    case Molecule_type_other:
4703                    default:
4704 #ifdef _DEBUG_2
4705 printf("unknown ");
4706 #endif                     
4707                       pmmdThis->bWhat = (Byte) AM_UNK;
4708                       pmsdThis->bWhat = (pmsdThis->bWhat | (Byte) AM_UNK);
4709                   } /*switch*/
4710  
4711                   pmmdThis->pSeqId = pmgThis->seq_id;
4712                   pmgThis->seq_id = NULL;  /* copies & protects the ASN.1 seq_id */
4713 
4714 
4715                     
4716                   prThis = pmgThis->residue_sequence; 
4717 
4718                   while (prThis)
4719                     {  /* flesh - out the molecule graphs residue-by-residue */
4720                          ProgMon("Instantiating Chemical Graph");
4721                          iResCount++; 
4722                          iAtomCount = 0;
4723                          iBondCount = 0;
4724                          pdnmgPrev = pdnmgThis; 
4725                          pdnmgThis  = NewDNMG(NULL,(Int2) prThis->id); 
4726                          if (!pdnmgThis) goto allocerror;
4727                          pmgdThis = (PMGD)  pdnmgThis->data.ptrvalue; /*set the pointer to the data */
4728                          pmgdThis->pfbParent = (PFB) pmmdThis;
4729                          pmgdThis->pcGraphNum = StringSave(prThis->name);
4730 #ifdef _DEBUG_1
4731 printf(":%s: PDB No ", pmgdThis->pcGraphNum);
4732 #endif                           
4733 
4734 
4735                          pvnrgThis = prThis->residue_graph;
4736                          /* USE ENTREZ-FETCH TO GET REF-ED BIOSTRUCS */
4737                          switch (pvnrgThis->choice)
4738                           {
4739                              case ResidueGraphPntr_local:
4740                                 prgDict = pbsBS->chemical_graph->residue_graphs;
4741                                 iGraphId = (Int4) pvnrgThis->data.intvalue;
4742                                 pmsdThis->pDictLocal = prgDict;
4743                                 pmgdThis->bWhat = (Byte) (pmgdThis->bWhat | (Byte) DICT_LOCAL);
4744                                 break;
4745                              case ResidueGraphPntr_standard:
4746                                /* get the id of the graph to lookup */
4747                                 ppdDict = (BiostrucResidueGraphSetPntrPtr) pvnrgThis->data.ptrvalue;
4748                                 iGraphId = (Int4) ppdDict->residue_graph_id;
4749                                 pmgdThis->bWhat = (Byte) (pmgdThis->bWhat | (Byte) DICT_GLOBAL);
4750                                /* ****use the dictionary id to get the right global dictionary */
4751                                 prgDict = prgdDict->residue_graphs;  /* the one passed as a parameter */
4752                                 break;
4753                              case ResidueGraphPntr_biostruc:
4754                              default:
4755                                 prgDict = NULL;
4756                                 pmgdThis->bWhat = (Byte) (pmgdThis->bWhat | (Byte) DICT_REF);
4757 
4758                           }
4759                          while (prgDict)
4760                           {
4761                           
4762                             if (prgDict->id == iGraphId)
4763                              {
4764                                /* found the right dictionary entry */
4765                                 pbmdrThis = ValNodeFindNext(prgDict->descr, NULL, BiomolDescr_name);
4766                                 if (pbmdrThis)
4767                                   {
4768                                     pmgdThis->pcGraphName = StringSave((CharPtr)pbmdrThis->data.ptrvalue); 
4769                                   }
4770                                 else 
4771                                   {
4772                                      pmgdThis->pcGraphName = StringSave(STRING_UNK_RES);
4773                                   }
4774                                 pbmdrThis = ValNodeFindNext(prgDict->descr, NULL, BiomolDescr_pdb_comment);
4775                                 if (pbmdrThis)
4776                                   {
4777                                     pmgdThis->pcPDBComment = StringSave((CharPtr) pbmdrThis->data.ptrvalue);
4778                                   }
4779                                 pvnStrings = prgDict->iupac_code;
4780                                 if (pvnStrings)  /* only interpret the first one!!! */
4781                                   {
4782                                     pmgdThis->pcIUPAC = StringSave((CharPtr)pvnStrings->data.ptrvalue);
4783                                   }
4784                                   
4785 #ifdef _DEBUG_1
4786 printf("@%s@%s@",pmgdThis->pcIUPAC, pmgdThis->pcGraphName);
4787 #endif                          
4788 
4789                                 switch(prgDict->residue_type)
4790                                    {
4791                                         case  Residue_deoxyribonucleotide:
4792                                             pmgdThis->bWhat = (Byte) (pmgdThis->bWhat | (Byte) RES_DNA);
4793                                             break;
4794                                         case Residue_ribonucleotide:
4795                                             pmgdThis->bWhat = (Byte) (pmgdThis->bWhat | (Byte) RES_RNA);
4796                                             break;
4797                                         case Residue_amino_acid:
4798                                             pmgdThis->bWhat = (Byte) (pmgdThis->bWhat | (Byte) RES_AA);
4799                                             break;
4800                                         case Residue_other:
4801                                         default:
4802                                             ;
4803                                    }
4804                                pmgdThis->iIDict = iGraphId;
4805                                pAtom = prgDict->atoms;
4806                                while (pAtom)
4807                                 {
4808                                     iAtomCount++;
4809                                     pvnmaPrev = pvnmaThis;
4810                                     if (pAtom->element < 254) 
4811                                        pvnmaThis = NewVNMA(NULL, (Int2) pAtom->element);
4812                                     else 
4813                                       {
4814                                        if (pAtom->element == 254)
4815                                           pvnmaThis = NewVNMA(NULL, 0);
4816                                        else /* bigger usually is 255 */
4817                                           pvnmaThis = NewVNMA(NULL, (Int2) INT1_MAX);
4818                                       }
4819                                     if (!pvnmaThis) goto allocerror;
4820                                     pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
4821                                     pmadThis->pfbParent = (PFB) pmgdThis;
4822                                     pmadThis->iIndex = (Int2) pAtom->id;
4823         /**/                        pmadThis->pcAName = pAtom->name;
4824                                     /* note the name is still in the dictionary !!*/
4825                                     /* no link back to dictionary source and 
4826                                      * no link back to the iupac_code valnode list
4827                                      * as this is redundant with the element lookup table
4828                                      * and more compact to save an Int1 element number
4829                                      * for each atom
4830                                      */
4831 #ifdef _DEBUG_1
4832 printf(" ^%s^ ", pmadThis->pcAName);
4833 #endif                           
4834                                    
4835                                     pAtom = pAtom->next;
4836                                     if (iAtomCount == 1) pmgdThis->pvnmaAHead = pvnmaThis; /* first case */
4837                                     else ValNodeListCat(pvnmaPrev, pvnmaThis);  
4838                                         /* faster linking with pvnmaPrev - dosen't have to walk the chain... */
4839                                 }
4840                              
4841                              
4842                              
4843                                 /* do intra-residue bonds here */       
4844                                 pirbThis = prgDict->bonds;
4845                                 while (pirbThis)
4846                                  {
4847                                      iBondCount++;
4848                                      pvnmbPrev = pvnmbThis;
4849                                      pvnmbThis = NewVNMB(NULL, (Int2) pirbThis->bond_order);
4850                                     
4851                                      if (!pvnmbThis) goto allocerror;
4852                                      pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
4853                                      pmbdThis->pfbParent = (PFB) pmgdThis;
4854                                      pmbdThis->bWhat = SetBondOrder(pmbdThis->bWhat,
4855                                                                 (Int2)pirbThis->bond_order);
4856                                      /* register the bond in both the MBD and MAD */
4857                                      pvnmaThis = pmgdThis->pvnmaAHead;
4858                                      
4859                                      while (pvnmaThis)
4860                                        {
4861                                            pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
4862                                            if (pmadThis->iIndex == (Int2) pirbThis->atom_id_1)
4863                                              {
4864                                                 pmbdThis->pmadFrom = pmadThis;
4865                                                 pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds, 
4866                                                 (Int2) pirbThis->bond_order, (Nlm_VoidPtr) pmbdThis);
4867                                                 if (!pvnThis) goto allocerror;
4868                                                  
4869                                              }
4870                                            if (pmadThis->iIndex == (Int2) pirbThis->atom_id_2)
4871                                              {
4872                                                pmbdThis->pmadTo = pmadThis;
4873                                                pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds, 
4874                                                 (Int2) pirbThis->bond_order, (Nlm_VoidPtr) pmbdThis);
4875                                                if (!pvnThis) goto allocerror;
4876                                              }    
4877                                            pvnmaThis = pvnmaThis->next;
4878                                            
4879                                        }  /* while (pvnmaThis) */
4880                                    /* check that pmadFrom and pmadTo are filled */
4881 #ifdef _DEBUG_1
4882 printf("\nbond from %s to %s ", pmbdThis->pmadFrom->pcAName, 
4883                                     pmbdThis->pmadTo->pcAName);
4884 #endif                                     
4885                                     pirbThis = pirbThis->next;
4886                                     if (iBondCount == 1) pmgdThis->pvnmbBHead = pvnmbThis; /* first case */
4887                                     else ValNodeListCat(pvnmbPrev, pvnmbThis);  
4888                                         /* faster linking with pvnmbPrev - dosen't have to walk the chain... */
4889                                  }  /* while (pirbThis) */
4890                                break; /*  out of while (prgDict) */
4891                              }
4892                             prgDict = prgDict->next; 
4893                           } /* while (prgDict) */
4894                          if (!pmgdThis->iIDict) goto erret;  /* dictionary failed to resolve an ID */
4895                          pmgdThis->iAtomCount = iAtomCount;
4896                          pmgdThis->iBondCount = iBondCount;
4897                          if (iResCount == 1) pmmdThis->pdnmgHead = pdnmgThis;  /* link first case */ 
4898                          else DValNodeListCat(pdnmgPrev, pdnmgThis);  /* link it on */
4899                          prThis = prThis->next; 
4900                      }  /* while (prThis) */
4901                  /* put in error messages when not got sequence
4902                   */  
4903                 pmmdThis->iResCount = iResCount;
4904                 if (iMolCount == 1) pmsdThis->pdnmmHead = pdnmmThis;
4905                 /* link first case */ 
4906                 else DValNodeListCat(pdnmmPrev,pdnmmThis);  /* link it on */
4907                 /* the molecule must be linked onto the structure 
4908                  * or the AtomFromMMDBIndex won't find it...
4909                  */
4910                 iBondCount = 0;
4911                 pvnmbPrev = NULL;
4912                 pierbThis = pmgThis->inter_residue_bonds;
4913                 while (pierbThis)
4914                      { /*  ...  fill in the inter_residue_bonds list... */
4915                          iBondCount++;
4916                          pvnmbPrev = pvnmbThis;
4917                          pvnmbThis = NewVNMB(NULL, (Int2) pierbThis->bond_order);
4918                          
4919                          if (!pvnmbThis) goto allocerror;
4920                          pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
4921                          pmbdThis->pfbParent = (PFB) pmmdThis;
4922                          pmbdThis->bWhat = SetBondOrder(pmbdThis->bWhat,
4923                                                    (Int2)pierbThis->bond_order);
4924                           /* register the bond in both the MBD and MAD */
4925                                    
4926                          papIndex = pierbThis->atom_id_1;
4927                          pmadThis = AtomFromMMDBIndex(pdnmsThis, iChoice, papIndex->molecule_id, 
4928                                       papIndex->residue_id,  papIndex->atom_id);
4929                          if (pmadThis) pmbdThis->pmadFrom = pmadThis;
4930                          else goto erret;
4931                          pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds, 
4932                                         (Int2) pierbThis->bond_order, (Nlm_VoidPtr) pmbdThis);
4933                          if (!pvnThis) goto allocerror;
4934                          
4935                          papIndex = pierbThis->atom_id_2;
4936                          pmadThis = AtomFromMMDBIndex(pdnmsThis, iChoice, papIndex->molecule_id, 
4937                                       papIndex->residue_id,  papIndex->atom_id);
4938                          if (pmadThis) pmbdThis->pmadTo = pmadThis;
4939                          else goto erret;
4940                          pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds, 
4941                                         (Int2) pierbThis->bond_order, (Nlm_VoidPtr) pmbdThis);
4942                          if (!pvnThis) goto allocerror;
4943                 
4944                          /* check that pmadFrom and pmadTo are filled */
4945 #ifdef _DEBUG_1
4946 printf("\nINTER-RES bond from %s to %s ", pmbdThis->pmadFrom->pcAName, 
4947                                     pmbdThis->pmadTo->pcAName);
4948 #endif                  
4949                          if (iBondCount == 1) pmmdThis->pvnmbIRBHead = pvnmbThis; /* first case */
4950                          else ValNodeListCat(pvnmbPrev, pvnmbThis);     
4951                          pierbThis = pierbThis->next;
4952                        } /* while (pierbThis) */
4953                 pmgThis = pmgThis->next; 
4954                 pmmdThis->iIRBCount = iBondCount;       
4955              } /*while (pmgThis) */
4956            pierbThis = pbsBS->chemical_graph->inter_molecule_bonds;
4957            iBondCount = 0;
4958            pvnmbPrev = NULL;
4959            while (pierbThis)
4960              { /*  ...  fill in the inter_molecule_bonds list... */
4961                  iBondCount++;
4962                  pvnmbPrev = pvnmbThis;
4963                  pvnmbThis = NewVNMB(NULL, (Int2) pierbThis->bond_order);
4964                   
4965                  if (!pvnmbThis) goto allocerror;
4966                  pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
4967                  pmbdThis->pfbParent = (PFB) pmsdThis;
4968                  pmbdThis->bWhat = SetBondOrder(pmbdThis->bWhat,
4969                                                  (Int2)pierbThis->bond_order);
4970                   /* register the bond in both the MBD and MAD */
4971                            
4972                  papIndex = pierbThis->atom_id_1;
4973                  pmadThis = AtomFromMMDBIndex(pdnmsThis, iChoice, papIndex->molecule_id, 
4974                               papIndex->residue_id,  papIndex->atom_id);
4975                  if (pmadThis) pmbdThis->pmadFrom = pmadThis;
4976                  else goto erret;
4977                  pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds, 
4978                                 (Int2) pierbThis->bond_order, (Nlm_VoidPtr) pmbdThis);
4979                  if (!pvnThis) goto allocerror;
4980                  
4981                  papIndex = pierbThis->atom_id_2;
4982                  pmadThis = AtomFromMMDBIndex(pdnmsThis, iChoice, papIndex->molecule_id, 
4983                               papIndex->residue_id,  papIndex->atom_id);
4984                  if (pmadThis) pmbdThis->pmadTo = pmadThis;
4985                  else goto erret;
4986                  pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds, 
4987                                 (Int2) pierbThis->bond_order, (Nlm_VoidPtr) pmbdThis);
4988                  if (!pvnThis) goto allocerror;
4989                 
4990                  /* check that pmadFrom and pmadTo are filled */
4991 #ifdef _DEBUG_1
4992 printf("\nINTER-MOL bond from %s to %s ", pmbdThis->pmadFrom->pcAName, 
4993                             pmbdThis->pmadTo->pcAName);
4994 #endif                  
4995                         
4996                  if (iBondCount == 1) pmsdThis->pvnmbIMBHead = pvnmbThis; /* first case */
4997                  else ValNodeListCat(pvnmbPrev, pvnmbThis);     
4998                  pierbThis = pierbThis->next;
4999              } /* while (pierbThis) */  
5000                         
5001   
5002            pmsdThis->iMolCount = iMolCount; 
5003            pmsdThis->iIMBCount = iBondCount;
5004               
5005            /* REMOVE ALL BUT THE {id, descr, history} PART OF pbsBS */
5006            /* TURF THE CHEMICAL GRAPH (NOT A SEQUENCE) */
5007            /* keep Local Dict from destruction when I turf the chem graph */
5008            if (pbsBS->chemical_graph->residue_graphs) 
5009                pbsBS->chemical_graph->residue_graphs = NULL; 
5010            BiostrucGraphFree(pbsBS->chemical_graph);
5011            pbsBS->chemical_graph = NULL;
5012            
5013            /* Set up the Internal Feature Set */
5014            pbsfsThis = pbsBS->features;
5015            while (pbsfsThis)
5016              {
5017                 iTest = BiostrucAddFeature(pbsfsThis, pdnmsThis, FALSE, NULL);
5018           if (iTest < 0) goto erret;
5019                 if (!iTest) goto allocerror;
5020                 pbsfsThis=pbsfsThis->next;
5021              }
5022                       /* blow away the internal Feature Set ASN.1 */
5023 /*         BiostrucFeatureSetFree(pbsfsThis);
5024            pbsBS->features = NULL;      */
5025            /* stubs to reconstruc Feature-set descr fields remain */
5026            /* as well as feature-contents */
5027 
5028            /* CONVERT all Models one at a time */
5029             pbsmThis = pbsBS->model; 
5030             while (pbsmThis) 
5031              {
5032                iModelCount++;
5033                iTest = BiostrucModel2ModelStruc(pbsmThis,pdnmsThis,iChoice, iModelCount, bExtent);
5034                if (iTest < 0) goto erret;
5035                if (iTest == 0) goto allocerror;
5036                pmsdThis->iModels++;
5037                pbsmThis = pbsmThis->next;
5038              } 
5039             /* The Biostruc owns the models - they are always kept and are always freed at pbsBS level */ 
5040             /* if not keeping ASN.1 the coordinates are all stripped out in the next routine */
5041             /* but the model stubs still remain */
5042           } /* if pbsBS */
5043 
5044   
5045     return pdnmsThis; 
5046     
5047     allocerror:
5048              ErrClear(); 
5049              ErrPostEx(SEV_ERROR,0,0, "Not enough memory to handle Biostruc\nFreeing Biostruc");
5050              ErrShow();
5051              FreeListDNMS(pdnmsThis);
5052              MsgAlert(KEY_NONE,SEV_ERROR, "Biostruc to Modelstruc Failed", 
5053                   "\nBiostruc was removed from memory.\nTry again with smaller or fewer Models.");
5054              return NULL;
5055                 
5056     erret:
5057              ErrClear(); 
5058              ErrPostEx(SEV_ERROR,0,0, "Biostruc ASN.1 Internal Indexing Failure\nFreeing Biostruc");
5059              ErrShow();
5060              FreeListDNMS(pdnmsThis);   
5061              return NULL;
5062     
5063 }
5064 
5065 
5066 /* TraverseAll
5067  *
5068  * Arguments:
5069  *     pdnModel = a DValNode pointer USED for MSD, MMD, MGD.
5070  *                You cannot begin traversal at terminal nodes 
5071  *                MAD,MBD,MOD,MDD.
5072  *     iIndex   = any index to be passed to pfnCallme
5073  *                
5074  *     ptr      = any pointer to be passed to pfnCallme
5075  *     pfnCallMe = The callback to be performed at every node.
5076  *
5077  */
5078 
5079 
5080 Int2 LIBCALL TraverseAll( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex, 
5081                           Pointer ptr, pNodeFunc pfnCallMe)
5082 /*       Walks the structure children from the current pointer.  Performs
5083          (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at each data node 
5084          (as Flags Block). callbacks are void return types.
5085  
5086 */
5087 {
5088     PFB pfbThis;
5089     PDNMS pdnmsThis = NULL; 
5090     PMSD pmsdThis = NULL; 
5091     PDNMM pdnmmThis = NULL; 
5092     PMMD pmmdThis = NULL; 
5093     PDNMG pdnmgThis = NULL;
5094     PMGD pmgdThis = NULL;  
5095     PVNMA pvnmaThis = NULL;
5096     PMAD pmadThis = NULL;
5097     PVNMB pvnmbThis = NULL;
5098     PMBD pmbdThis = NULL;
5099     PVNMO pvnmoThis = NULL;
5100     PMOD pmodThis = NULL;
5101     PVNMD pvnmdThis = NULL;
5102     PMDD pmddThis = NULL;
5103     if (pdnModel)
5104       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
5105     else return FALSE;
5106     switch ((int)pfbThis->bMe)  /* set the proper type */
5107       {
5108           case AM_MSD: 
5109               pdnmsThis = (PDNMS) pdnModel;
5110             break;
5111           case AM_MMD:
5112               pdnmmThis = (PDNMM) pdnModel;
5113             break;
5114           case AM_MGD:
5115               pdnmgThis = (PDNMM) pdnModel;
5116             break;
5117           default:
5118             return FALSE;
5119       }
5120    
5121     if (pdnmgThis)
5122        {
5123         while(pdnmgThis)
5124                   {   /*graph*/
5125                       pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
5126                       (*pfnCallMe)((PFB) pmgdThis, iModel, iIndex, ptr); /* do the callback */
5127                       pvnmaThis = pmgdThis->pvnmaAHead;
5128                       while (pvnmaThis)
5129                         {   /*atom*/
5130                             pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
5131                             (*pfnCallMe)((PFB) pmadThis, iModel, iIndex, ptr); /* do the callback */
5132                             
5133                             pvnmaThis = pvnmaThis->next;
5134                         }   /*atom*/
5135                       pvnmbThis = pmgdThis->pvnmbBHead;
5136                       while (pvnmbThis)
5137                         {   /*bond*/
5138                            pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
5139                            (*pfnCallMe)((PFB) pmbdThis,  iModel, iIndex, ptr); /* do the callback */
5140                            
5141                            
5142                            pvnmbThis = pvnmbThis->next;
5143                         } /*bond*/      
5144                       
5145                       pdnmgThis = pdnmgThis->next;
5146                   }/*graph*/
5147         return TRUE;
5148        } /* if pdnmgThis */
5149     if (pdnmmThis) 
5150        {
5151           while(pdnmmThis)
5152             {   /*molecule*/
5153                 pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
5154                 (*pfnCallMe)((PFB) pmmdThis, iModel, iIndex, ptr); /* do the callback */
5155                 pdnmgThis =  pmmdThis->pdnmgHead;
5156                 while(pdnmgThis)
5157                   {   /*graph*/
5158                       pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
5159                       (*pfnCallMe)((PFB) pmgdThis, iModel, iIndex, ptr); /* do the callback */
5160                       pvnmaThis = pmgdThis->pvnmaAHead;
5161                       while (pvnmaThis)
5162                         {   /*atom*/
5163                             pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
5164                             (*pfnCallMe)((PFB) pmadThis, iModel, iIndex, ptr); /* do the callback */
5165                             
5166                             pvnmaThis = pvnmaThis->next;
5167                         }   /*atom*/
5168                       pvnmbThis = pmgdThis->pvnmbBHead;
5169                       while (pvnmbThis)
5170                         {   /*bond*/
5171                            pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
5172                            (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
5173                            
5174                            
5175                            pvnmbThis = pvnmbThis->next;
5176                         } /*bond*/      
5177                       
5178                       pdnmgThis = pdnmgThis->next;
5179                   }/*graph*/
5180                 /* traverse inter-residue bonds */
5181                 pvnmbThis = pmmdThis->pvnmbIRBHead;
5182                 while (pvnmbThis)
5183                   { /* inter-residue bonds */
5184                     pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
5185                     (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr);  /* do the callback */
5186                       
5187                     pvnmbThis = pvnmbThis->next;
5188                   }
5189                 pdnmmThis = pdnmmThis->next;
5190             }   /*molecule*/
5191         return TRUE;
5192        } /* if pdnmmThis */
5193     if (pdnmsThis)  
5194      {
5195          pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
5196           (*pfnCallMe)((PFB) pmsdThis, iModel, iIndex, ptr);  /* do the callback */
5197           pdnmmThis = pmsdThis->pdnmmHead;
5198           while(pdnmmThis)
5199             {   /*molecule*/
5200                 pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
5201                 (*pfnCallMe)((PFB) pmmdThis, iModel, iIndex, ptr); /* do the callback */
5202                 pdnmgThis =  pmmdThis->pdnmgHead;
5203                 while(pdnmgThis)
5204                   {   /*graph*/
5205                       pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
5206                       (*pfnCallMe)((PFB) pmgdThis, iModel, iIndex, ptr); /* do the callback */
5207                       pvnmaThis = pmgdThis->pvnmaAHead;
5208                       while (pvnmaThis)
5209                         {   /*atom*/
5210                             pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
5211                             (*pfnCallMe)((PFB) pmadThis, iModel, iIndex, ptr); /* do the callback */
5212                             
5213                             pvnmaThis = pvnmaThis->next;
5214                         }   /*atom*/
5215                       pvnmbThis = pmgdThis->pvnmbBHead;
5216                       while (pvnmbThis)
5217                         {   /*bond*/
5218                            pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
5219                            (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
5220                            
5221                            
5222                            pvnmbThis = pvnmbThis->next;
5223                         } /*bond*/      
5224                       
5225                       pdnmgThis = pdnmgThis->next;
5226                   }/*graph*/
5227                 /* traverse inter-residue bonds */
5228                 pvnmbThis = pmmdThis->pvnmbIRBHead;
5229                 while (pvnmbThis)
5230                   { /* inter-residue bonds */
5231                     pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
5232                     (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr);  /* do the callback */
5233                       
5234                     pvnmbThis = pvnmbThis->next;
5235                   }
5236                 pdnmmThis = pdnmmThis->next;
5237             }   /*molecule*/
5238           /* traverse inter-molecule bonds */
5239           pvnmbThis = pmsdThis->pvnmbIMBHead;
5240           while (pvnmbThis)
5241             { /* inter-molecule bonds */
5242                 pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
5243                 (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
5244                 
5245                 pvnmbThis = pvnmbThis->next;
5246             }
5247           /* traverse Objects */
5248           pvnmoThis = pmsdThis->pvnmoHead;
5249           while (pvnmoThis)
5250             { 
5251                 if (pvnmoThis->choice == iModel)
5252                   {     /* only those models stated */
5253                         pmodThis = (PMOD) pvnmoThis->data.ptrvalue;
5254                         (*pfnCallMe)((PFB) pmodThis, iModel, iIndex, ptr);
5255                   }
5256                 pvnmoThis = pvnmoThis->next;
5257             }
5258          
5259           /* traverse Densities */
5260           pvnmdThis = pmsdThis->pvnmdHead;
5261           while (pvnmdThis)
5262             { 
5263                 if (pvnmdThis->choice == iModel)
5264                   {     /* only those models stated */
5265                         pmddThis = (PMDD) pvnmdThis->data.ptrvalue;
5266                         (*pfnCallMe)((PFB) pmddThis, iModel, iIndex, ptr);
5267                   }
5268                 pvnmdThis = pvnmdThis->next;
5269             }
5270      return TRUE;
5271     }
5272   return FALSE;
5273 }
5274 
5275 /*  Function to retrieve Biostrucs from ASN.1 files  */
5276 /************************************************************/
5277 /*                                                          */
5278 /*      BiostrucAsnGet()                                    */
5279 /*                                                          */
5280 /*      Retrieves Biostruc with a structure model of        */
5281 /*      choice by ignoring portions of the I/O stream       */
5282 /*                                                          */
5283 /************************************************************/
5284 
5285 BiostrucPtr LIBCALL BiostrucAsnGet(AsnIoPtr aip, AsnTypePtr atp, Int4 mdlLvl, Int4 maxModels)
5286 {
5287     BiostrucPtr retval;
5288     DataVal av;
5289     AsnOptionPtr aop;
5290 
5291 
5292     av.intvalue = mdlLvl;
5293     aop = AsnIoOptionNew(aip, OP_NCBIOBJSTR, BIOSTRUC_CHECK_MODEL, av, NULL);
5294     av.intvalue = maxModels;
5295     aop = AsnIoOptionNew(aip, OP_NCBIOBJSTR, BIOSTRUC_MAX_MODELS, av, NULL);
5296     
5297     if (mdlLvl == ALLSIMPLEMDL)
5298     { /* this is a reaction to an earlier kludge, when ONECOORDATOM didn't
5299          contain all of the models.  Now they do contain all the models, so
5300          by forcing this value here we avoid the unnecessary fetching of
5301          multiple models.  J. Epstein per C. Hogue, 11/18/96 */
5302         mdlLvl = ONECOORDATOM;
5303     }
5304 
5305     retval = BiostrucAsnRead(aip, atp);
5306 
5307     AsnIoOptionFree(aip, OP_NCBIOBJSTR, BIOSTRUC_CHECK_MODEL);
5308     AsnIoOptionFree(aip, OP_NCBIOBJSTR, BIOSTRUC_MAX_MODELS);
5309     if (mdlLvl == ONECOORDATOM || mdlLvl == ONECOORDRES || mdlLvl == VECMODEL)
5310     { /* we have left AsnIoStream in a hopelessly messed-up state, so reset it */
5311         AsnIoReset(aip);
5312     }
5313 
5314     return retval;
5315 }
5316 
5317 /* The accmmdbs.c stub has BiostrucAvail returning FALSE,
5318    and empty BiostrucAsn Read and Write functions.
5319    It should not be added to project libraries (Mac), or
5320    will be be overridden on linking (PC or UNIX). */
5321 
5322 Boolean LIBCALL BiostrucAvail (void)
5323 
5324 {
5325   return TRUE;
5326 }
5327 
5328 /*      These 2 Bool funs were moved from mkbioseqB.c - DZ 4/27/99    */
5329 
5330 /***********************************************************************/
5331 /*  isBiopoly()                                                        */
5332 /*                                                                     */
5333 /*  Given a molecule id, checks whether the corresponding molecule is  */
5334 /*  either protein, DNA, or RNA and has coordinates.                   */
5335 /*                                                                     */
5336 /***********************************************************************/ 
5337 
5338 Boolean LIBCALL isBiopoly(Int4 molecule_id, MoleculeGraphPtr currentbp)
5339 {
5340   Boolean ret=FALSE;
5341   while (currentbp) {
5342     if (currentbp->id == molecule_id) {
5343       ret = TRUE;
5344       break;
5345     }
5346     currentbp = currentbp->next;
5347   }
5348   return ret;
5349 }
5350 /*************************************************************************/
5351 /*      isHet()                                                          */
5352 /*                                                                       */
5353 /*  Given a molecule id, checks whether the corresponding molecule is    */
5354 /*  a heterogen but not solvent or of unknown type, and has coordinates. */
5355 /*                                                                       */
5356 /*************************************************************************/ 
5357 
5358 Boolean LIBCALL isHet(Int4 molecule_id, MoleculeGraphPtr currenthet)
5359 {
5360   Boolean ret=FALSE;
5361   while (currenthet) {
5362     if (currenthet->id == molecule_id) {
5363       ret = TRUE;
5364       break;
5365     }
5366     currenthet = currenthet->next;
5367   }
5368   return ret;
5369 }
5370 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.