1 /*   mmdbapi.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
29 *
30 * Version Creation Date:   08/19/95
31 *
32 * $Revision: 6.12 $
33 *
34 * File Description: MMDBAPI wrappers.
35 *
36 * Modifications:
37 * --------------------------------------------------------------------------
38 * Date     Name        Description of modification
39 * -------  ----------  -----------------------------------------------------
40 *
41 *
42 *
43 * $Log: mmdbapi.c,v $
44 * Revision 6.12  1999/08/04 21:21:25  lewisg
45 * made ClearStructure turn off neighbor mode in non-cn3d mode
46 *
47 * Revision 6.11  1999/03/05 22:43:59  vakatov
48 * Made ClearStructures() function be NLM_EXTERN -- for the DLL build on PC
49 *
50 * Revision 6.10  1999/03/05 20:22:31  kans
51 * ClearStructures clears neighbor bit
52 *
53 * Revision 6.9  1998/08/26 18:02:38  kans
54 * fixed -v -fd warnings
55 *
56 * Revision 6.8  1998/08/12 21:54:37  lewisg
57 * pick correct master in SetNeighborOn
58 *
59 * Revision 6.7  1998/05/06 16:25:27  lewisg
60 * get rid of clear all structures bug
61 *
62 * Revision 6.6  1998/04/16 00:30:00  lewisg
63 * get rid of dead code
64 *
65 * Revision 6.5  1998/04/05 04:29:01  lewisg
66 * make SetNeighborOn set the right slaves in cn3d mode
67 *
68 * Revision 6.4  1998/04/04 00:57:52  lewisg
69 * corrected several functions to behave properly in neighbor mode
70 *
71 * Revision 6.3  1998/03/13 22:34:18  lewisg
72 * fix neighbor mode
73 *
74 * Revision 6.2  1998/03/06 01:16:00  lewisg
75 * merge
76 *
77 * Revision 6.0  1997/08/25 18:10:48  madden
78 * Revision changed to 6.0
79 *
80 * Revision 5.0  1996/05/28 14:02:09  ostell
81 * Set to revision 5.0
82 *
83  * Revision 1.6  1996/05/15  17:01:35  hogue
84  * Fixed compiler warnings.
85  *
86  * Revision 1.5  1996/04/18  17:05:49  hogue
87  * Added neighbor mode functionality, setting Master & Slave structures.  GetFirstModelstruc gets first slave now when in neighbor mode.  Backing out of neighbor mode resets pdnmsSelected to the Master.
88  *
89  * Revision 1.4  1996/01/31  21:30:49  hogue
90  * Major changes to accomodate multiple structures, added mmdbAPI
91  * data structure, list pointers for traversing structures,
92  * list pointers for PFB selection holders, etc.
93  *
94  * Revision 1.3  1995/10/07  15:24:19  hogue
95  * Counting biostrucs from 0 to n.
96  *
97  * Revision 1.2  1995/10/05  20:22:42  hogue
98  * Fixed some structure tracking bugs, added ClearStructures()
99  *
100  * Revision 1.1  1995/09/20  13:45:47  kans
101  * Initial revision
102  *
103 *
104 * ==========================================================================
105 */
106 
107 
108 /*
109 
110   WARNING!  The way some of these functions work is dependent on the setting of
111   pmmdbapi->fCn3D, which is set to TRUE when being run by Cn3D.  This is to
112   eliminate the modality of those functions when running in Neighbor mode.
113 
114   */
115 
116 #include <mmdbapi.h>
117 
118 
119 
120 static pmmdbAPI pmmdbapi = NULL;
121 
IsMMDBAPIOpen(void)122 Boolean LIBCALL IsMMDBAPIOpen(void)
123 {
124   return (Boolean) (pmmdbapi != NULL);
125 }
126 
fnMMDBCn3Dmode(void)127 void LIBCALL fnMMDBCn3Dmode (void)  /* may I never live this down */
128 {
129   pmmdbapi->fCn3D = TRUE;
130 }
131 
NewMMDBAPI(void)132 pmmdbAPI LIBCALL NewMMDBAPI(void)
133 {
134   pmmdbAPI papi = NULL;
135 
136   papi = (pmmdbAPI)MemNew((size_t)(sizeof(mmdbAPI)));
137   return papi;
138 }
139 
OpenMMDBAPI(Byte bExtent,CharPtr pcDictName)140 Int2 LIBCALL OpenMMDBAPI(Byte bExtent, CharPtr pcDictName)
141 {
142 
143   if (pmmdbapi != NULL) return 1;
144   else pmmdbapi = NewMMDBAPI();
145   if (bExtent == 0)
146     pmmdbapi->bExtent = (Byte) POWER_VIEW;
147   else
148     pmmdbapi->bExtent = (Byte) bExtent;
149  if (pcDictName == NULL)
150     pmmdbapi->prgdDict =  LoadDict(DICT_DEFAULT);
151   else
152     pmmdbapi->prgdDict =  LoadDict(pcDictName);
153   if (!pmmdbapi->prgdDict)
154     {
155 	ErrClear();
156 	ErrPostEx(SEV_FATAL,0,0,  "Unable to Load Residue Dictionary");
157 	ErrShow();
158 	return 0;
159     }
160   if (! (objmmdb1AsnLoad() && objmmdb2AsnLoad() && objmmdb3AsnLoad() ))
161      {
162 	ErrClear();
163 	ErrPostEx(SEV_FATAL,0,0, "Internal objmmdbNAsnLoad() Failure");
164 	ErrShow();
165 	return 0;
166     }
167   pmmdbapi->iLocalCount = 0;
168   pmmdbapi->iFeatureSet = 0;
169   pmmdbapi->iFeatureId = 0;
170   pmmdbapi->pvnsffHead = NULL;
171   pmmdbapi->pColorFunc = NULL;
172   pmmdbapi->Neighbor = FALSE;
173   pmmdbapi->fCn3D = FALSE;
174   return 1;
175 }
176 
177 
FetchBiostrucPDB(CharPtr pcAcession,Int4 iModelLevel,Int4 iMaxModels)178 BiostrucPtr LIBCALL FetchBiostrucPDB(CharPtr pcAcession, Int4 iModelLevel, Int4 iMaxModels)
179 {
180   BiostrucPtr pbsTemp;
181   pbsTemp = FetchBS(pcAcession, INP_PDB_ACCESSION, iModelLevel,
182 		iMaxModels, pmmdbapi->bExtent);
183   return pbsTemp;
184 }
185 
MakeAModelstruc(BiostrucPtr pbsThis)186 PDNMS LIBCALL MakeAModelstruc(BiostrucPtr pbsThis)
187 {
188     PDNMS pdnmsTemp = NULL;
189     PDNMS pdnmsCount = NULL;
190     PMSD  pmsdThis = NULL;
191     PMSD  pmsdMaster = NULL;
192     Int4  iCount = 0;
193 
194     if (!pmmdbapi)
195       {
196          ErrClear();
197 	 ErrPostEx(SEV_FATAL,0,0, "MMDBAPI Not Initialized\n  Use InitMMDBAPI() \n");
198 	 ErrShow();
199          return pdnmsTemp;
200       }
201 
202     if (!pbsThis) return pdnmsTemp;
203     if (pmmdbapi->Neighbor && pmmdbapi->pdnmsMaster)
204       {
205          pmsdMaster = (PMSD) pmmdbapi->pdnmsMaster->data.ptrvalue;
206          pmmdbapi->iLocalCount++;
207          pdnmsTemp = Biostruc2Modelstruc(&pmsdMaster->pdnmsSlaves,  pbsThis,  pmmdbapi->prgdDict,
208 				     pmmdbapi->bExtent, pmmdbapi->iLocalCount, NULL, (Int2)0);
209       }
210     else
211       {
212         pmmdbapi->iLocalCount++;
213         pdnmsTemp = Biostruc2Modelstruc(&pmmdbapi->pdnmsMain,  pbsThis,  pmmdbapi->prgdDict,
214 				     pmmdbapi->bExtent, pmmdbapi->iLocalCount, NULL, (Int2)0);
215       }
216     if (!pdnmsTemp)
217        {
218 	 pmmdbapi->iLocalCount--;  /* back it off the counter */
219          return pdnmsTemp;  /* NOTE THE BIOSTRUC pbsThis is FREED if this fails!!*/
220        }
221 
222     if (pmmdbapi->Neighbor)
223       {
224          pmmdbapi->pdnmsSlave  = pdnmsTemp;
225                   pmmdbapi->pdnmsSelected = pmmdbapi->pdnmsMaster;
226       }
227     else  pmmdbapi->pdnmsSelected = pdnmsTemp;
228 
229     pmsdThis = (PMSD) pdnmsTemp->data.ptrvalue;
230     pmsdThis->iHashChange = MakeHashChange(pdnmsTemp);
231     AssignBackBone(pdnmsTemp);
232     AssignVirtual(pdnmsTemp);
233     AssignIons(pdnmsTemp);
234 
235     return pdnmsTemp;
236 }
237 
238 
GetPDNMSMain(void)239 PDNMS LIBCALL GetPDNMSMain(void)
240 {
241     return pmmdbapi->pdnmsMain;
242 }
243 
GetPRGDDictionary(void)244 PRGD LIBCALL GetPRGDDictionary(void)
245 {
246     return pmmdbapi->prgdDict;
247 }
248 
249 
ChangeMMDBAPIbExtent(Byte bExtent)250 void LIBCALL ChangeMMDBAPIbExtent(Byte bExtent)
251 {
252     pmmdbapi->bExtent = bExtent;
253 }
254 
255 
GetMMDBAPIbExtent(void)256 Byte LIBCALL GetMMDBAPIbExtent(void)
257 {
258     return pmmdbapi->bExtent;
259 }
260 
261 
262 
263 
MakeHashChange(PDNMS pdnmsThis)264 Int4 LIBCALL MakeHashChange(PDNMS pdnmsThis)
265 {
266   PMSD pmsdThis = NULL;
267   Int4 iHashCalc = 0;
268   Int4 iAtomLocs = 0;
269 
270   if (!pdnmsThis) return iHashCalc;
271   pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
272   iAtomLocs =  AssignAtomLocId(pdnmsThis);
273   if (!(pmsdThis->bUpdate & (Byte) AM_CHANGED))
274        return pmsdThis->iMMDBid;
275 
276   /* only changed if dirty bit set ... */
277   iHashCalc = pmsdThis->iMMDBid + pmsdThis->iModels + pmsdThis->iFeatures +
278 	    pmsdThis->iObjCount + pmsdThis->iDensCount + 100000 + iAtomLocs;
279   iHashCalc++; /* inc by one... for incremental changes (eg transform'ns) */
280   return iHashCalc;
281 
282 }
283 
CountModelstrucs(void)284 Int4 LIBCALL CountModelstrucs(void)
285 {
286     Int4 iCount = 0;
287     PDNMS pdnmsCount = NULL;
288 
289     if (!pmmdbapi) return iCount;
290     pdnmsCount = pmmdbapi->pdnmsMain;
291     while (pdnmsCount)
292       {
293 	  iCount++;
294 	  pdnmsCount = pdnmsCount->next;
295       }
296     return iCount;
297 }
298 
299 
FreeAModelstruc(PDNMS pdnmsThis)300 void LIBCALL FreeAModelstruc(PDNMS pdnmsThis)
301 {
302     Int2 iLocalId;
303     ValNodePtr pvnKillSelectList = NULL;
304 
305 
306     iLocalId = (Int2) pdnmsThis->choice;
307     pvnKillSelectList = ValNodeExtractList(&pmmdbapi->pvnPFBSelect, iLocalId);
308     ValNodeFree(pvnKillSelectList);  /* selection list purged for this Modelstruc */
309     if (pdnmsThis == pmmdbapi->pdnmsSelected)
310 	  pmmdbapi->pdnmsSelected = NULL;
311     if (pdnmsThis == pmmdbapi->pdnmsPrevious)
312    	  pmmdbapi->pdnmsPrevious = NULL;
313     if (pdnmsThis == pmmdbapi->pdnmsSlave)
314           pmmdbapi->pdnmsSlave = NULL;
315     if (pdnmsThis == pmmdbapi->pdnmsHolder)
316           pmmdbapi->pdnmsHolder = pdnmsThis->last;
317     if(AreNeighborsOn() && pdnmsThis != pmmdbapi->pdnmsMaster)
318       FreeDNMS(((PMSD)(pmmdbapi->pdnmsMaster->data.ptrvalue))->pdnmsSlaves, pdnmsThis);
319       else
320       {
321          if (pdnmsThis == pmmdbapi->pdnmsMaster) pmmdbapi->pdnmsMaster = NULL;
322          pmmdbapi->pdnmsMain = FreeDNMS(pmmdbapi->pdnmsMain, pdnmsThis);
323       }
324 }
325 
326 
ClearStructures(void)327 NLM_EXTERN void LIBCALL ClearStructures(void)
328 {
329   PDNMS pdnmsThis = NULL;
330 
331      if (!pmmdbapi) return;
332      pdnmsThis = GetFirstModelstruc();
333      if (!pdnmsThis) return;
334      FreeListDNMS(pdnmsThis);
335      if (!pmmdbapi->Neighbor || pmmdbapi->fCn3D)
336        {
337         pmmdbapi->pdnmsMain = NULL;
338         pmmdbapi->pdnmsMaster = NULL;
339        }
340        pmmdbapi->Neighbor = 0;
341        pmmdbapi->pdnmsSlave  = NULL;
342        pmmdbapi->pdnmsHolder = NULL;
343        pmmdbapi->pdnmsSelected = NULL;
344        pmmdbapi->pdnmsPrevious = NULL;
345        ValNodeFree(pmmdbapi->pvnPFBSelect);
346        pmmdbapi->pvnPFBSelect = NULL;
347        pmmdbapi->pvnUndoMark = NULL;
348 /* lyg.  no freeing of feature data in pmmdbapi.  Is this a good idea? */
349 }
350 
351 
CloseMMDBAPI(void)352 void LIBCALL CloseMMDBAPI(void)
353 {
354     if (!pmmdbapi) return;
355     if (pmmdbapi->pdnmsMain) FreeListDNMS(pmmdbapi->pdnmsMain);
356     if (pmmdbapi->pvnPFBSelect) ValNodeFree(pmmdbapi->pvnPFBSelect);
357     if (pmmdbapi->prgdDict) BiostrucResidueGraphSetFree(pmmdbapi->prgdDict);
358         if (pmmdbapi->pvnsffHead) FreeListVNSFF(pmmdbapi->pvnsffHead);
359     if (pmmdbapi) MemFree(pmmdbapi);
360     return;
361 }
362 
363 
364 
GetStrucStrings(PDNMS pdnmsThis,Int4 iPickType)365 CharPtr LIBCALL GetStrucStrings(PDNMS pdnmsThis, Int4 iPickType)
366 {
367     PMSD pmsdThis;
368 
369     if (!pdnmsThis) pdnmsThis = pmmdbapi->pdnmsMain;
370     if (!pdnmsThis) return NULL;
371     pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
372     if (pmsdThis->bMe != (Byte) AM_MSD) return NULL;
373     if (iPickType == LONG_NAME) return (pmsdThis->pcChemName);
374     if (iPickType == PDB_ACC) return (pmsdThis->pcPDBName);
375     if (iPickType == PDB_CLASS) return (pmsdThis->pcPdbClass);
376     if (iPickType == PDB_SOURCE) return (pmsdThis->pcPdbSource);
377     return NULL;
378 }
379 
380 
381 
SetSelectedModelstruc(PDNMS pdnmsThis)382 void LIBCALL SetSelectedModelstruc(PDNMS pdnmsThis)
383 {
384 
385 /* doesn't check to see if structure is a really a slave if Neighbor mode on */
386 
387     if (pmmdbapi->Neighbor == TRUE && !(pmmdbapi->fCn3D))
388       {
389          pmmdbapi->pdnmsSlave = pdnmsThis;
390          pmmdbapi->pdnmsSelected = pdnmsThis;
391       }
392     else
393        pmmdbapi->pdnmsSelected = pdnmsThis;
394 }
395 
SetNeighborOn(void)396 void LIBCALL SetNeighborOn(void)
397 {
398 
399    if (!pmmdbapi->pdnmsMaster)
400        pmmdbapi->pdnmsMaster = GetSelectedModelstruc();
401 
402    if ( pmmdbapi->pdnmsMaster && !(pmmdbapi->fCn3D))
403      {
404       pmmdbapi->Neighbor = TRUE;
405       if (!pmmdbapi->pdnmsSlave)
406         {
407           pmmdbapi->pdnmsSlave = GetFirstModelstruc(); /* NULL if none loaded */
408         }
409       else pmmdbapi->Neighbor = FALSE;
410      }
411   else
412     {
413       pmmdbapi->pdnmsMaster = GetSelectedModelstruc();
414       if (pmmdbapi->pdnmsMaster)
415       {
416         pmmdbapi->pdnmsSlave = ((PMSD)(pmmdbapi->pdnmsMaster->data.ptrvalue))->pdnmsSlaves;
417         pmmdbapi->Neighbor = TRUE;
418       }
419     }
420 }
421 
422 
AreNeighborsOn(void)423 Boolean LIBCALL AreNeighborsOn(void)
424 {
425   return pmmdbapi->Neighbor;
426 }
427 
SetNeighborOff(void)428 void LIBCALL SetNeighborOff(void)
429 {
430    pmmdbapi->pdnmsSelected = pmmdbapi->pdnmsMaster;
431    pmmdbapi->Neighbor = FALSE;
432    pmmdbapi->pdnmsSlave = NULL;
433 }
434 
GetSelectedModelstruc(void)435 PDNMS LIBCALL GetSelectedModelstruc(void)
436 {
437      if (pmmdbapi->Neighbor == TRUE && !(pmmdbapi->fCn3D))
438      return pmmdbapi->pdnmsSlave;
439    else
440     return pmmdbapi->pdnmsSelected;
441 }
442 
SwapModelstruc(void)443 PDNMS LIBCALL SwapModelstruc(void)
444 {
445     PDNMS pdnmsTemp;
446     pdnmsTemp = pmmdbapi->pdnmsSelected;
447     pmmdbapi->pdnmsSelected = pmmdbapi->pdnmsPrevious;
448     pmmdbapi->pdnmsPrevious = pdnmsTemp;
449     return pmmdbapi->pdnmsSelected;
450 }
451 
452 
SetMasterModelstruc(PDNMS pdnmsThis)453 void LIBCALL SetMasterModelstruc(PDNMS pdnmsThis)
454 {
455     pmmdbapi->pdnmsMaster = pdnmsThis;
456 }
457 
GetMasterModelstruc(void)458 PDNMS LIBCALL GetMasterModelstruc(void)
459 {
460     return pmmdbapi->pdnmsMaster;
461 }
462 
SetSlaveModelstruc(PDNMS pdnmsThis)463 void LIBCALL SetSlaveModelstruc(PDNMS pdnmsThis)
464 {
465     pmmdbapi->pdnmsSlave = pdnmsThis;
466 }
467 
GetSlaveModelstruc(void)468 PDNMS LIBCALL GetSlaveModelstruc(void)
469 {
470    return pmmdbapi->pdnmsSlave;
471 }
472 
473 /**************************************************/
474 /*  GetFirstModelstruc, GetNextModelstruc and     */
475 /*  GetPreviousModelstruc are provided to walk    */
476 /*  the doubly-linked list of structures that     */
477 /*  MMDBAPI has in memory...                      */
478 /*  SetHolderModelstruc allows one to set the     */
479 /*  pointer to any of the structures              */
480 /**************************************************/
481 
GetFirstModelstruc(void)482 PDNMS LIBCALL GetFirstModelstruc(void)
483 {
484 
485    PMSD pmsdThis = NULL;
486    PDNMS pdnmsThis = NULL;
487 
488    if (pmmdbapi->Neighbor && !(pmmdbapi->fCn3D))
489      {
490        pdnmsThis = GetMasterModelstruc();
491        if (pdnmsThis)
492          {
493            pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
494            pmmdbapi->pdnmsHolder = pmsdThis->pdnmsSlaves;
495            return pmsdThis->pdnmsSlaves;
496          }
497         else return NULL;
498      }
499     pmmdbapi->pdnmsHolder = pmmdbapi->pdnmsMain;
500     return pmmdbapi->pdnmsMain;
501 }
502 
503 
SetHolderModelstruc(PDNMS pdnmsThis)504 void LIBCALL SetHolderModelstruc(PDNMS pdnmsThis)
505 {
506     pmmdbapi->pdnmsHolder = pdnmsThis;
507 }
508 
GetNextModelstruc(void)509 PDNMS LIBCALL GetNextModelstruc(void)
510 {
511     if (pmmdbapi->pdnmsHolder)
512       {
513 	 pmmdbapi->pdnmsHolder = pmmdbapi->pdnmsHolder->next;
514 	 return pmmdbapi->pdnmsHolder;
515       }
516     return NULL;
517 }
518 
GetPreviousModelstruc(void)519 PDNMS LIBCALL GetPreviousModelstruc(void)
520 {  /* node which came before this one */
521     if (pmmdbapi->pdnmsHolder)
522       {
523 	pmmdbapi->pdnmsHolder = pmmdbapi->pdnmsHolder->last;
524     	return pmmdbapi->pdnmsHolder;
525       }
526     return NULL;
527 }
528 
AddPFBSelect(PFB pfbThis)529 ValNodePtr LIBCALL AddPFBSelect(PFB pfbThis)
530 {
531     PDNMS pdnmsStruc = NULL;
532     PMSD  pmsdStruc = NULL;
533 
534     if (!pfbThis) return NULL;
535     pdnmsStruc = DNFromPFB((PFB)ToMSDParent(pfbThis));
536     pmmdbapi->pvnUndoMark = ValNodeAddPointer(&pmmdbapi->pvnPFBSelect,
537 		       pdnmsStruc->choice, (VoidPtr) pfbThis);
538     /* the choice field allows us to extract pointers
539      * to different structures from the list,  esp. when freeing
540      * specific structures.
541      */
542     return pmmdbapi->pvnUndoMark;
543 }
544 
545 
ClearPFBSelectList(void)546 void LIBCALL ClearPFBSelectList(void)
547 {
548     ValNodeFree(pmmdbapi->pvnPFBSelect);
549     pmmdbapi->pvnPFBSelect = NULL;
550     return;
551 }
552 
UndoPFBSelectList(void)553 void LIBCALL UndoPFBSelectList(void)
554 {
555     ValNodeFree(pmmdbapi->pvnUndoMark);
556     return;
557 }
558 
559 
560 
DoesFeatureTypeHaveFuncs(Uint1 iCode)561 PSFF LIBCALL DoesFeatureTypeHaveFuncs(Uint1 iCode)
562 {
563   ValNodePtr pvn = NULL;
564   PSFF psffThis = NULL;
565 
566   ValNodeFindNext(pmmdbapi->pvnsffHead, NULL, iCode);
567   if (pvn)
568     {
569       psffThis = (PSFF) pvn->data.ptrvalue;
570       return psffThis;
571     }
572   return NULL;
573 }
574 
Call3DColorProc(ColorPropPtr pcpThis)575 Uint1 LIBCALL Call3DColorProc(ColorPropPtr pcpThis)
576 {
577   Uint1 i = 0;
578   if (pmmdbapi->pColorFunc)
579      (*(pmmdbapi->pColorFunc)) (pcpThis, &i);
580  return i;
581 }
582 
583