1 /*   mmdbapi2.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:  mmdbapi2.c
27 *
28 * Author:  Christopher Hogue, Hitomi Ohkawa
29 *
30 * Version Creation Date:   07/24/95
31 *
32 * $Revision: 6.9 $
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 *                      PDB file generators, Kinemage file renderers moved
43 *                      to mmdbapi3.c
44 * 95/08/30 C. Hogue    Moved globals into mmdbapi2.c.
45 *
46 * $Log: mmdbapi2.c,v $
47 * Revision 6.9  2003/12/03 02:11:28  kans
48 * added defines missing from Mac OS 10.3 headers
49 *
50 * Revision 6.8  2000/08/22 19:47:33  lewisg
51 * fix GetMMFromMSDBySeqId
52 *
53 * Revision 6.7  2000/03/31 22:30:47  lewisg
54 * fix output of CONECT, create intrabond traverser, misc bugs
55 *
56 * Revision 6.6  2000/03/01 16:16:45  thiessen
57 * modified backbone definitions, added AM_PARTIALBACKBONE
58 *
59 * Revision 6.5  1999/03/30 23:15:03  kans
60 * included sequtil.h
61 *
62 * Revision 6.4  1999/03/30 22:31:12  ywang
63 * add GetMGFromMM & GetMMFromMSDBySeqId
64 *
65 * Revision 6.3  1998/12/31 17:04:08  ywang
66 * work around reassign protein or NA as ion problem
67 *
68  * Revision 6.2  1998/08/26  18:02:41  kans
69  * fixed -v -fd warnings
70  *
71 * Revision 6.1  1998/03/06 23:17:21  lewisg
72 * codewarrior fixes
73 *
74 * Revision 6.0  1997/08/25 18:11:03  madden
75 * Revision changed to 6.0
76 *
77 * Revision 5.5  1997/07/18 14:29:10  vakatov
78 * Provide return values (NULLs) in MolFromPDBChain() and SeqStringFromMol()
79 *
80 * Revision 5.4  1997/03/19 16:48:40  hogue
81 * Added fn SeqStringFromMol.
82 *
83  * Revision 5.3  1997/01/27  21:41:10  hogue
84  * Added function to find a chain based on PDB chain code (char)
85  *
86  * Revision 5.2  1996/08/19  21:08:10  vakatov
87  * Removed redundant(and dangerous) function castings to "pNodeFunc"
88  *
89  * Revision 5.1  1996/07/15  21:28:00  hogue
90  * Fixed glutamine labelling error.
91  *
92  * Revision 5.0  1996/05/28  14:02:09  ostell
93  * Set to revision 5.0
94  *
95  * Revision 1.17  1996/05/22  20:43:16  hogue
96  * Added new API functions GetFirstGraph, GetNextGraph, GetPrevGraph, GetMainAtom
97  *
98  * Revision 1.16  1996/05/06  21:23:14  hogue
99  * added pointer tests to ToMSDParent, SetActiveModel, GetActiveModel
100  *
101  * Revision 1.15  1996/04/12  18:07:05  hogue
102  * Changed flagging of atoms - AM_OCARBNYL now is anything branching off the backbone including N and H.  Added provisions for identifying D for Hydrogen in standard naming schemes.
103  *
104  * Revision 1.14  1996/03/29  19:34:57  hogue
105  * Changed table of atomic radii, added H backbone flag, added ALD access in GetParent functions.
106  *
107  * Revision 1.13  1996/01/31  21:37:17  hogue
108  * Removed extraneous traversing of structures - traversers limited to only one structure at a time.
109  *
110  * Revision 1.12  1995/11/15  19:16:37  hogue
111  * TraverseSolids handles solids/densities on a per-model basis now.
112  *
113  * Revision 1.11  1995/10/10  12:27:08  hogue
114  * Shut off unknown element error message
115  *
116  * Revision 1.10  1995/10/07  15:31:09  hogue
117  * Selenium not an ion...
118  *
119  * Revision 1.9  1995/09/18  21:25:34  hogue
120  * ProgMon() calls added.
121  *
122  * Revision 1.8  1995/09/14  15:56:00  kans
123  * changed UInt1 to Uint1
124  *
125  * Revision 1.7  1995/09/14  14:22:30  hogue
126  * Removed 254 and 255 in IsNotIon -> replaced with 0 (other) and INT1_MAX (unk)
127  *
128  * Revision 1.6  1995/09/13  21:50:32  hogue
129  * Fixed Uint1 to array IsNotIon
130  *
131  * Revision 1.5  1995/09/05  19:10:33  hogue
132  * Unknown element handling
133  *
134  * Revision 1.4  1995/08/30  19:36:44  kans
135  * numerous changes
136  *
137  * Revision 1.3  1995/08/28  19:31:53  kans
138  * version 4.1
139  *
140  * Revision 1.1  1995/08/02  17:07:15  kans
141  * Initial revision
142  *
143 *
144 * ==========================================================================
145 */
146 /*****************************************************************************
147 *
148 *  mmdbapi2.c
149 *
150 *  UNDER CONSTRUCTION NOTICE.
151 *	SUBJECT TO RADICAL CHANGES...
152 *
153 *  programmer C.W.V. Hogue
154 *  created: 24 JULY 95
155 *  last mod: 28 AUGUST 95
156 *****************************************************************************/
157 
158 
159 #include <ncbi.h>
160 #include <asn.h>
161 #include <mmdbapi1.h>
162 #include <mmdbapi2.h>
163 #include <sequtil.h>
164 
165 /* Missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
166 #ifdef __MWERKS__
167 #ifdef OS_UNIX_DARWIN
168 #ifndef __SCHAR_MAX__
169 #define __SCHAR_MAX__ 127
170 #endif
171 #endif
172 #endif
173 /* End missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
174 
175 CharPtr NCBIstdaaUC = "-ABCDEFGHIKLMNPQRSTVWXYZU*";
176 CharPtr NCBI4naUC = "-ACMGRSVTUWYHKDBN";
177 CharPtr NCBI4naLC = "-acmgrsvtuwyhkdbn";
178 CharPtr NCBIstdaaLC = "-abcdefghiklmnpqrstvwxyzu*";
179 
180 
181 /* these are acessed via AminoAcidName() */
182 static CharPtr NCBIstdaaMC3[MAX_NCBIstdaa] = {
183 	"---",
184 	"Ala",
185 	"Asx",
186 	"Cys",
187 	"Asp",
188 	"Glu",
189 	"Phe",
190 	"Gly",
191 	"His",
192 	"Ile",
193 	"Lys",
194 	"Leu",
195 	"Met",
196 	"Asn",
197 	"Pro",
198 	"Gln",
199 	"Arg",
200 	"Ser",
201 	"Thr",
202 	"Val",
203 	"Trp",
204 	"Xxx",
205 	"Tyr",
206 	"Glx",
207 	"Sec",
208 	"Ter" };
209 
210 static  CharPtr NCBIstdaaUC3[MAX_NCBIstdaa] = {
211 	"---",
212 	"ALA",
213 	"ASX",
214 	"CYS",
215 	"ASP",
216 	"GLU",
217 	"PHE",
218 	"GLY",
219 	"HIS",
220 	"ILE",
221 	"LYS",
222 	"LEU",
223 	"MET",
224 	"ASN",
225 	"PRO",
226 	"GLN",
227 	"ARG",
228 	"SER",
229 	"THR",
230 	"VAL",
231 	"TRP",
232 	"XXX",
233 	"TYR",
234 	"GLX",
235 	"SEC",
236 	"TER" };
237 
238 
239 static  CharPtr NCBIstdaaLC3[MAX_NCBIstdaa] = {
240 	"---",
241 	"ala",
242 	"asx",
243 	"cys",
244 	"asp",
245 	"glu",
246 	"phe",
247 	"gly",
248 	"his",
249 	"ile",
250 	"lys",
251 	"leu",
252 	"met",
253 	"asn",
254 	"pro",
255 	"gln",
256 	"arg",
257 	"ser",
258 	"thr",
259 	"val",
260 	"trp",
261 	"xxx",
262 	"tyr",
263 	"glx",
264 	"sec",
265 	"ter" };
266 
267 
268 static CharPtr NCBIstdaaLCF[MAX_NCBIstdaa] = {
269 	"---",
270 	"alanine",
271 	"asx",
272 	"cysteine",
273 	"aspartate",
274 	"glutamate",
275 	"phenylalanine",
276 	"glycine",
277 	"histidine",
278 	"isoleucine",
279 	"lysine",
280 	"leucine",
281 	"methionine",
282 	"asparagine",
283 	"proline",
284 	"glutamine",
285 	"arginine",
286 	"serine",
287 	"threonine",
288 	"valine",
289 	"tryptophan",
290 	"xxx",
291 	"tyrosine",
292 	"glx",
293 	"selenocysteine",
294 	"termination" };
295 
296 
297 /* accessed via ElementName() */
298 static CharPtr IUPACElement[MAX_ELEMENTS] = {
299  	" ",
300 	"H",
301 	"He",
302 	"Li",
303 	"Be",
304 	"B",
305 	"C",
306 	"N",
307 	"O",
308 	"F",
309 	"Ne",
310 	"Na",
311 	"Mg",
312 	"Al",
313 	"Si",
314 	"P",
315 	"S",
316 	"Cl",
317 	"Ar",
318 	"K",
319 	"Ca",
320 	"Sc",
321 	"Ti",
322 	"V",
323 	"Cr",
324 	"Mn",
325 	"Fe",
326 	"Co",
327 	"Ni",
328 	"Cu",
329 	"Zn",
330 	"Ga",
331 	"Ge",
332 	"As",
333 	"Se",
334 	"Br",
335 	"Kr",
336 	"Rb",
337 	"Sr",
338 	"Y",
339 	"Zr",
340 	"Nb",
341 	"Mo",
342 	"Tc",
343 	"Ru",
344 	"Rh",
345 	"Pd",
346 	"Ag",
347 	"Cd",
348 	"In",
349 	"Sn",
350 	"Sb",
351 	"Te",
352 	"I",
353 	"Xe",
354 	"Cs",
355 	"Ba",
356 	"La",
357 	"Ce",
358 	"Pr",
359 	"Nd",
360 	"Pm",
361 	"Sm",
362 	"Eu",
363 	"Gd",
364 	"Tb",
365 	"Dy",
366 	"Ho",
367 	"Er",
368 	"Tm",
369 	"Yb",
370 	"Lu",
371 	"Hf",
372 	"Ta",
373 	"W",
374 	"Re",
375 	"Os",
376 	"Ir",
377 	"Pt",
378 	"Au",
379 	"Hg",
380 	"Tl",
381 	"Pb",
382 	"Bi",
383 	"Po",
384 	"At",
385 	"Rn",
386 	"Fr",
387 	"Ra",
388 	"Ac",
389 	"Th",
390 	"Pa",
391 	"U",
392 	"Np",
393 	"Pu",
394 	"Am",
395 	"Cm",
396 	"Bk",
397 	"Cf",
398 	"Es",
399 	"Fm",
400 	"Md",
401 	"No",
402 	"Lr"};
403 
404 
405 
406 /* this is from the Sargent-welch periodic table */
407 /* Bigger of atomic/covalent radius in Angstroms */
408 /* accessed via ElementSize() */
409 static FloatLo CommonSize[MAX_ELEMENTS] = {
410  	0.0,
411 	0.79,
412 	0.93,
413 	2.05,
414 	1.40,
415 	1.17,
416 	1.30, /* fuued C */
417 	1.20, /* fuued N */
418 	1.10, /* fuued O */
419 	0.72,
420 	0.71,
421 	2.23,
422 	1.72,
423 	1.82,
424 	1.46,
425 	1.35, /* fuued P */
426 	1.30, /* fuued S */
427 	0.99,
428 	0.98,
429 	2.77,
430 	2.23,
431 	2.09,
432 	2.00,
433 	1.92,
434 	1.85,
435 	1.79,
436 	1.72,
437 	1.67,
438 	1.62,
439 	1.57,
440 	1.53,
441 	1.81,
442 	1.52,
443 	1.33,
444 	1.22,
445 	1.12,
446 	1.12,
447 	2.98,
448 	2.45,
449 	2.27,
450 	2.56,
451 	2.08,
452 	2.01,
453 	1.95,
454 	1.89,
455 	1.83,
456 	1.79,
457 	1.75,
458 	1.71,
459 	2.00,
460 	1.71,
461 	1.53,
462 	1.42,
463 	1.33,
464 	1.31,
465 	3.34,
466 	2.78,
467 	2.74,
468 	2.70,
469 	2.67,
470 	2.64,
471 	2.62,
472 	2.59,
473 	2.56,
474 	2.54,
475 	2.51,
476 	2.49,
477 	2.47,
478 	2.45,
479 	2.42,
480 	2.40,
481 	2.25,
482 	2.16,
483 	2.09,
484 	2.02,
485 	1.97,
486 	1.92,
487 	1.87,
488 	1.83,
489 	1.79,
490 	1.76,
491 	2.08,
492 	1.81,
493 	1.63,
494 	1.53,
495 	1.45,
496 	1.34,
497 	3.40, /* rest are est*/
498 	3.00,
499 	2.90,
500 	2.85,
501 	2.80,
502 	2.75,
503 	2.63,  /*rest are est */
504 	2.62,
505 	2.85,
506 	2.61,
507 	2.59,
508 	2.59,
509 	2.58,
510 	2.57,
511 	2.56,
512 	2.74,
513 	2.56};
514 
515 
516 static Uint1 IsNotIon[MAX_NOT_ION] = {0,1,2,5,6,7,8,10,14,15,16,18,34,36,54,86,INT1_MAX};
517 
518 
519 /* END OF GLOBAL VARIABLES */
520 
521 
522 
TraverseSolids(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)523 Int2 LIBCALL TraverseSolids( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
524 			  Pointer ptr, pNodeFunc pfnCallMe)
525 /*	 Walks the structure children from the current pointer.  Performs
526 	 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at MDD,  MSD
527 	 (as Flags Block). callbacks are void return types.
528 
529 */
530 {
531     PFB pfbThis;
532     PDNMS pdnmsThis = NULL;
533     PMSD pmsdThis = NULL;
534     PVNMO pvnmoThis = NULL;
535     PMOD pmodThis = NULL;
536     PVNMD pvnmdThis = NULL;
537     PMDD pmddThis = NULL;
538     if (pdnModel)
539       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
540     else return FALSE;
541     switch ((int)pfbThis->bMe)  /* set the proper type */
542       {
543 	  case AM_MSD:
544 	      pdnmsThis = (PDNMS) pdnModel;
545 	    break;
546           default:
547             return FALSE;
548       }
549     if (pdnmsThis)
550      {
551           pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
552 	  /* traverse Objects */
553 	  pvnmoThis = pmsdThis->pvnmoHead;
554 	  while (pvnmoThis)
555 	    {
556  		if (pvnmoThis->choice == iModel)
557   		  {	/* only those models stated */
558 			pmodThis = (PMOD) pvnmoThis->data.ptrvalue;
559 			(*pfnCallMe)((PFB) pmodThis, iModel, iIndex, ptr);
560 		  }
561 		pvnmoThis = pvnmoThis->next;
562 	    }
563 
564           /* traverse Densities */
565 	  pvnmdThis = pmsdThis->pvnmdHead;
566 	  while (pvnmdThis)
567 	    {
568 		if (pvnmdThis->choice == iModel)
569 		  {	/* only those models stated */
570 			pmddThis = (PMDD) pvnmdThis->data.ptrvalue;
571 			(*pfnCallMe)((PFB) pmddThis, iModel, iIndex, ptr);
572 		  }
573 		pvnmdThis = pvnmdThis->next;
574 	    }
575      return TRUE;
576     }
577   return FALSE;
578 }
579 
580 
581 
TraverseBonds(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)582 Int2 LIBCALL TraverseBonds( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
583 			  Pointer ptr, pNodeFunc pfnCallMe)
584 /*	 Walks the structure children from the current pointer.  Performs
585 	 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at each PMBD
586 	 (as Flags Block). callbacks are void return types.
587 
588 */
589 {
590     PFB pfbThis;
591     PDNMS pdnmsThis = NULL;
592     PMSD pmsdThis = NULL;
593     PDNMM pdnmmThis = NULL;
594     PMMD pmmdThis = NULL;
595     PDNMG pdnmgThis = NULL;
596     PMGD pmgdThis = NULL;
597     PVNMB pvnmbThis = NULL;
598     PMBD pmbdThis = NULL;
599     if (pdnModel)
600       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
601     else return FALSE;
602     switch ((int)pfbThis->bMe)  /* set the proper type */
603       {
604 	  case AM_MSD:
605 	      pdnmsThis = (PDNMS) pdnModel;
606 	    break;
607 	  case AM_MMD:
608 	      pdnmmThis = (PDNMM) pdnModel;
609 	    break;
610 	  case AM_MGD:
611 	      pdnmgThis = (PDNMM) pdnModel;
612 	    break;
613           default:
614             return FALSE;
615       }
616 
617     if (pdnmgThis)
618        {
619         while(pdnmgThis)
620 		  {   /*graph*/
621 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
622 		      pvnmbThis = pmgdThis->pvnmbBHead;
623 		      while (pvnmbThis)
624 		        {   /*bond*/
625 			   pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
626 			   (*pfnCallMe)((PFB) pmbdThis,  iModel, iIndex, ptr); /* do the callback */
627 			   pvnmbThis = pvnmbThis->next;
628 			} /*bond*/
629 
630 		      pdnmgThis = pdnmgThis->next;
631 		  }/*graph*/
632         return TRUE;
633        } /* if pdnmgThis */
634     if (pdnmmThis)
635        {
636           while(pdnmmThis)
637 	    {   /*molecule*/
638 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
639 		pdnmgThis =  pmmdThis->pdnmgHead;
640 		while(pdnmgThis)
641 		  {   /*graph*/
642 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
643 		      pvnmbThis = pmgdThis->pvnmbBHead;
644 		      while (pvnmbThis)
645 		        {   /*bond*/
646 			   pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
647 			   (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
648 			   pvnmbThis = pvnmbThis->next;
649 			} /*bond*/
650 		      pdnmgThis = pdnmgThis->next;
651 		  }/*graph*/
652 		/* traverse inter-residue bonds */
653 		pvnmbThis = pmmdThis->pvnmbIRBHead;
654 		while (pvnmbThis)
655 		  { /* inter-residue bonds */
656 		    pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
657 		    (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr);  /* do the callback */
658 		    pvnmbThis = pvnmbThis->next;
659 		  }
660 		pdnmmThis = pdnmmThis->next;
661 	    }   /*molecule*/
662         return TRUE;
663        } /* if pdnmmThis */
664     if (pdnmsThis)
665      {
666           pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
667 	  pdnmmThis = pmsdThis->pdnmmHead;
668 	  while(pdnmmThis)
669 	    {   /*molecule*/
670 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
671 	        pdnmgThis =  pmmdThis->pdnmgHead;
672 		while(pdnmgThis)
673 		  {   /*graph*/
674 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
675 		      pvnmbThis = pmgdThis->pvnmbBHead;
676 		      while (pvnmbThis)
677 		        {   /*bond*/
678 			   pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
679 			   (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
680 			   pvnmbThis = pvnmbThis->next;
681 			} /*bond*/
682 		      pdnmgThis = pdnmgThis->next;
683 		  }/*graph*/
684 		/* traverse inter-residue bonds */
685 		pvnmbThis = pmmdThis->pvnmbIRBHead;
686 		while (pvnmbThis)
687 		  { /* inter-residue bonds */
688 		    pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
689 		    (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr);  /* do the callback */
690 		    pvnmbThis = pvnmbThis->next;
691 		  }
692 		pdnmmThis = pdnmmThis->next;
693 	    }   /*molecule*/
694 	  /* traverse inter-molecule bonds */
695 	  pvnmbThis = pmsdThis->pvnmbIMBHead;
696 	  while (pvnmbThis)
697 	    { /* inter-molecule bonds */
698 		pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
699 		(*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
700 		pvnmbThis = pvnmbThis->next;
701 	    }
702      return TRUE;
703     }
704   return FALSE;
705 }
706 
707 /* like TraverseBonds, but minus the interresidue and intermolecular bonds */
TraverseIntraBonds(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)708 Int2 LIBCALL TraverseIntraBonds( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
709                                 Pointer ptr, pNodeFunc pfnCallMe)
710 /*	 Walks the structure children from the current pointer.  Performs
711 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at each PMBD
712 (as Flags Block). callbacks are void return types.
713 */
714 {
715     PFB pfbThis;
716     PDNMS pdnmsThis = NULL;
717     PMSD pmsdThis = NULL;
718     PDNMM pdnmmThis = NULL;
719     PMMD pmmdThis = NULL;
720     PDNMG pdnmgThis = NULL;
721     PMGD pmgdThis = NULL;
722     PVNMB pvnmbThis = NULL;
723     PMBD pmbdThis = NULL;
724     if (pdnModel)
725         pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
726     else return FALSE;
727     switch ((int)pfbThis->bMe)  /* set the proper type */
728     {
729     case AM_MSD:
730         pdnmsThis = (PDNMS) pdnModel;
731         break;
732     case AM_MMD:
733         pdnmmThis = (PDNMM) pdnModel;
734         break;
735     case AM_MGD:
736         pdnmgThis = (PDNMM) pdnModel;
737         break;
738     default:
739         return FALSE;
740     }
741 
742     if (pdnmgThis)
743     {
744         while(pdnmgThis)
745         {   /*graph*/
746             pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
747             pvnmbThis = pmgdThis->pvnmbBHead;
748             while (pvnmbThis)
749             {   /*bond*/
750                 pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
751                 (*pfnCallMe)((PFB) pmbdThis,  iModel, iIndex, ptr); /* do the callback */
752                 pvnmbThis = pvnmbThis->next;
753             } /*bond*/
754             pdnmgThis = pdnmgThis->next;
755         }/*graph*/
756         return TRUE;
757     } /* if pdnmgThis */
758     if (pdnmmThis)
759     {
760         while(pdnmmThis)
761         {   /*molecule*/
762             pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
763             pdnmgThis =  pmmdThis->pdnmgHead;
764             while(pdnmgThis)
765             {   /*graph*/
766                 pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
767                 pvnmbThis = pmgdThis->pvnmbBHead;
768                 while (pvnmbThis)
769                 {   /*bond*/
770                     pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
771                     (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
772                     pvnmbThis = pvnmbThis->next;
773                 } /*bond*/
774                 pdnmgThis = pdnmgThis->next;
775             }/*graph*/
776             pdnmmThis = pdnmmThis->next;
777         }   /*molecule*/
778         return TRUE;
779     } /* if pdnmmThis */
780     if (pdnmsThis)
781     {
782         pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
783         pdnmmThis = pmsdThis->pdnmmHead;
784         while(pdnmmThis)
785         {   /*molecule*/
786             pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
787             pdnmgThis =  pmmdThis->pdnmgHead;
788             while(pdnmgThis)
789             {   /*graph*/
790                 pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
791                 pvnmbThis = pmgdThis->pvnmbBHead;
792                 while (pvnmbThis)
793                 {   /*bond*/
794                     pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
795                     (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
796                     pvnmbThis = pvnmbThis->next;
797                 } /*bond*/
798                 pdnmgThis = pdnmgThis->next;
799             }/*graph*/
800             pdnmmThis = pdnmmThis->next;
801         }   /*molecule*/
802         /* traverse inter-molecule bonds */
803         return TRUE;
804     }
805     return FALSE;
806 }
807 
808 
809 
TraverseIBonds(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)810 Int2 LIBCALL TraverseIBonds( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
811 			  Pointer ptr, pNodeFunc pfnCallMe)
812 /*	 Walks the structure children from the current pointer.  Performs
813 	 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr)
814 	 at each PMBD Inter-residue or Inter-molecule (as Flags Block).
815 	 callbacks are void return types.
816 
817 */
818 {
819     PFB pfbThis;
820     PDNMS pdnmsThis = NULL;
821     PMSD pmsdThis = NULL;
822     PDNMM pdnmmThis = NULL;
823     PMMD pmmdThis = NULL;
824     PVNMB pvnmbThis = NULL;
825     PMBD pmbdThis = NULL;
826     if (pdnModel)
827       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
828     else return FALSE;
829     switch ((int)pfbThis->bMe)  /* set the proper type */
830       {
831 	  case AM_MSD:
832 	      pdnmsThis = (PDNMS) pdnModel;
833 	    break;
834 	  case AM_MMD:
835 	      pdnmmThis = (PDNMM) pdnModel;
836 	    break;
837          default:
838             return FALSE;
839       }
840 
841     if (pdnmmThis)
842        {
843           while(pdnmmThis)
844 	    {   /*molecule*/
845 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
846 		/* traverse inter-residue bonds */
847 		pvnmbThis = pmmdThis->pvnmbIRBHead;
848 		while (pvnmbThis)
849 		  { /* inter-residue bonds */
850 		    pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
851 		    (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr);  /* do the callback */
852 		    pvnmbThis = pvnmbThis->next;
853 		  }
854 		pdnmmThis = pdnmmThis->next;
855 	    }   /*molecule*/
856         return TRUE;
857        } /* if pdnmmThis */
858     if (pdnmsThis)
859      {
860           pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
861 	  pdnmmThis = pmsdThis->pdnmmHead;
862 	  while(pdnmmThis)
863 	    {   /*molecule*/
864 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
865 		/* traverse inter-residue bonds */
866 		pvnmbThis = pmmdThis->pvnmbIRBHead;
867 		while (pvnmbThis)
868 		  { /* inter-residue bonds */
869 		    pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
870 		    (*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr);  /* do the callback */
871 		    pvnmbThis = pvnmbThis->next;
872 		  }
873 		pdnmmThis = pdnmmThis->next;
874 	    }   /*molecule*/
875 	  /* traverse inter-molecule bonds */
876 	  pvnmbThis = pmsdThis->pvnmbIMBHead;
877 	  while (pvnmbThis)
878 	    { /* inter-molecule bonds */
879 		pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
880 		(*pfnCallMe)((PFB) pmbdThis, iModel, iIndex, ptr); /* do the callback */
881 		pvnmbThis = pvnmbThis->next;
882 	    }
883      return TRUE;
884     }
885   return FALSE;
886 }
887 
888 
889 
890 
TraverseAtoms(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)891 Int2 LIBCALL TraverseAtoms( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
892 			  Pointer ptr, pNodeFunc pfnCallMe)
893 /*	 Walks the structure children from the current pointer.  Performs
894 	 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at each PMAD
895 	 (as Flags Block). callbacks are void return types.
896 
897 */
898 {
899     PFB pfbThis;
900     PDNMS pdnmsThis = NULL;
901     PMSD pmsdThis = NULL;
902     PDNMM pdnmmThis = NULL;
903     PMMD pmmdThis = NULL;
904     PDNMG pdnmgThis = NULL;
905     PMGD pmgdThis = NULL;
906     PVNMA pvnmaThis = NULL;
907     PMAD pmadThis = NULL;
908     if (pdnModel)
909       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
910     else return FALSE;
911     switch ((int)pfbThis->bMe)  /* set the proper type */
912       {
913 	  case AM_MSD:
914 	      pdnmsThis = (PDNMS) pdnModel;
915 	    break;
916 	  case AM_MMD:
917 	      pdnmmThis = (PDNMM) pdnModel;
918 	    break;
919 	  case AM_MGD:
920 	      pdnmgThis = (PDNMM) pdnModel;
921 	    break;
922           default:
923             return FALSE;
924       }
925 
926     if (pdnmgThis)
927        {
928         while(pdnmgThis)
929 		  {   /*graph*/
930 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
931 		      pvnmaThis = pmgdThis->pvnmaAHead;
932 		      while (pvnmaThis)
933 		        {   /*atom*/
934 			    pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
935 			    (*pfnCallMe)((PFB) pmadThis, iModel, iIndex, ptr); /* do the callback */
936 
937 			    pvnmaThis = pvnmaThis->next;
938 			}   /*atom*/
939 		       pdnmgThis = pdnmgThis->next;
940 		  }/*graph*/
941         return TRUE;
942        } /* if pdnmgThis */
943     if (pdnmmThis)
944        {
945           while(pdnmmThis)
946 	    {   /*molecule*/
947 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
948 	        pdnmgThis =  pmmdThis->pdnmgHead;
949 		while(pdnmgThis)
950 		  {   /*graph*/
951 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
952 		      pvnmaThis = pmgdThis->pvnmaAHead;
953 		      while (pvnmaThis)
954 		        {   /*atom*/
955 			    pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
956 			    (*pfnCallMe)((PFB) pmadThis, iModel, iIndex, ptr); /* do the callback */
957 
958 			    pvnmaThis = pvnmaThis->next;
959 			}   /*atom*/
960 		      pdnmgThis = pdnmgThis->next;
961 		  }/*graph*/
962 		pdnmmThis = pdnmmThis->next;
963 	    }   /*molecule*/
964         return TRUE;
965        } /* if pdnmmThis */
966     if (pdnmsThis)
967      {
968           pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
969           pdnmmThis = pmsdThis->pdnmmHead;
970 	  while(pdnmmThis)
971 	    {   /*molecule*/
972 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
973 	        pdnmgThis =  pmmdThis->pdnmgHead;
974 		while(pdnmgThis)
975 		  {   /*graph*/
976 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
977 		      pvnmaThis = pmgdThis->pvnmaAHead;
978 		      while (pvnmaThis)
979 		        {   /*atom*/
980 			    pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
981 			    (*pfnCallMe)((PFB) pmadThis, iModel, iIndex, ptr); /* do the callback */
982 
983 			    pvnmaThis = pvnmaThis->next;
984 			}   /*atom*/
985 
986 		      pdnmgThis = pdnmgThis->next;
987 		  }/*graph*/
988 		pdnmmThis = pdnmmThis->next;
989 	    }   /*molecule*/
990      return TRUE;
991     }
992   return FALSE;
993 }
994 
995 
996 
997 
TraverseGraphs(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)998 Int2 LIBCALL TraverseGraphs( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
999 			  Pointer ptr, pNodeFunc pfnCallMe)
1000 /*	 Walks the structure children from the current pointer.  Performs
1001 	 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at PMGD
1002 	 (as Flags Block). callbacks are void return types.
1003 
1004 */
1005 {
1006     PFB pfbThis;
1007     PDNMS pdnmsThis = NULL;
1008     PMSD pmsdThis = NULL;
1009     PDNMM pdnmmThis = NULL;
1010     PMMD pmmdThis = NULL;
1011     PDNMG pdnmgThis = NULL;
1012     PMGD pmgdThis = NULL;
1013     if (pdnModel)
1014       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
1015     else return FALSE;
1016     switch ((int)pfbThis->bMe)  /* set the proper type */
1017       {
1018 	  case AM_MSD:
1019 	      pdnmsThis = (PDNMS) pdnModel;
1020 	    break;
1021 	  case AM_MMD:
1022 	      pdnmmThis = (PDNMM) pdnModel;
1023 	    break;
1024 	  case AM_MGD:
1025 	      pdnmgThis = (PDNMM) pdnModel;
1026 	    break;
1027           default:
1028             return FALSE;
1029       }
1030 
1031     if (pdnmgThis)
1032        {
1033         while(pdnmgThis)
1034 		  {   /*graph*/
1035 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1036 		      (*pfnCallMe)((PFB) pmgdThis, iModel, iIndex, ptr); /* do the callback */
1037 		      pdnmgThis = pdnmgThis->next;
1038 		  }/*graph*/
1039         return TRUE;
1040        } /* if pdnmgThis */
1041     if (pdnmmThis)
1042        {
1043           while(pdnmmThis)
1044 	    {   /*molecule*/
1045 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1046 	        pdnmgThis =  pmmdThis->pdnmgHead;
1047 		while(pdnmgThis)
1048 		  {   /*graph*/
1049 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1050 		      (*pfnCallMe)((PFB) pmgdThis, iModel, iIndex, ptr); /* do the callback */
1051 		       pdnmgThis = pdnmgThis->next;
1052 		  }/*graph*/
1053 		pdnmmThis = pdnmmThis->next;
1054 	    }   /*molecule*/
1055         return TRUE;
1056        } /* if pdnmmThis */
1057     if (pdnmsThis)
1058      {
1059           pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
1060 	  pdnmmThis = pmsdThis->pdnmmHead;
1061 	  while(pdnmmThis)
1062 	    {   /*molecule*/
1063 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1064 	        pdnmgThis =  pmmdThis->pdnmgHead;
1065 		while(pdnmgThis)
1066 		  {   /*graph*/
1067 		      pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1068 		      (*pfnCallMe)((PFB) pmgdThis, iModel, iIndex, ptr); /* do the callback */
1069 		      pdnmgThis = pdnmgThis->next;
1070 		  }/*graph*/
1071 		pdnmmThis = pdnmmThis->next;
1072 	    }   /*molecule*/
1073      return TRUE;
1074     }
1075   return FALSE;
1076 }
1077 
1078 
1079 
1080 
TraverseMolecules(DValNodePtr pdnModel,Int4 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)1081 Int2 LIBCALL TraverseMolecules( DValNodePtr pdnModel, Int4 iModel, Int4 iIndex,
1082 			  Pointer ptr, pNodeFunc pfnCallMe)
1083 /*	 Walks the structure children from the current pointer.  Performs
1084 	 (*pfnCallMe)((PFB) pxxxxThis,  Int4 iMode, Int4 iIndex, Pointer ptr) at each PMMD
1085 	 (as Flags Block). callbacks are void return types.
1086 
1087 */
1088 {
1089     PFB pfbThis;
1090     PDNMS pdnmsThis = NULL;
1091     PMSD pmsdThis = NULL;
1092     PDNMM pdnmmThis = NULL;
1093     PMMD pmmdThis = NULL;
1094 
1095     if (pdnModel)
1096       pfbThis = (PFB) pdnModel->data.ptrvalue;  /* get the data block */
1097     else return FALSE;
1098     switch ((int)pfbThis->bMe)  /* set the proper type */
1099       {
1100 	  case AM_MSD:
1101 	      pdnmsThis = (PDNMS) pdnModel;
1102 	    break;
1103 	  case AM_MMD:
1104 	      pdnmmThis = (PDNMM) pdnModel;
1105 	    break;
1106           default:
1107             return FALSE;
1108       }
1109     if (pdnmmThis)
1110        {
1111           while(pdnmmThis)
1112 	    {   /*molecule*/
1113 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1114 		(*pfnCallMe)((PFB) pmmdThis, iModel, iIndex, ptr); /* do the callback */
1115 		pdnmmThis = pdnmmThis->next;
1116 	    }   /*molecule*/
1117         return TRUE;
1118        } /* if pdnmmThis */
1119     if (pdnmsThis)
1120      {
1121           pmsdThis = (PMSD) pdnmsThis->data.ptrvalue;
1122 	  pdnmmThis = pmsdThis->pdnmmHead;
1123 	  while(pdnmmThis)
1124 	    {   /*molecule*/
1125 		pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1126 		(*pfnCallMe)((PFB) pmmdThis, iModel, iIndex, ptr); /* do the callback */
1127 	      pdnmmThis = pdnmmThis->next;
1128 	    }   /*molecule*/
1129      return TRUE;
1130     }
1131   return FALSE;
1132 }
1133 
1134 
1135 
TraverseModels(DValNodePtr pdnModel,Int2 iTraverse,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)1136 Int2 LIBCALL TraverseModels(DValNodePtr pdnModel, Int2 iTraverse,
1137      Int4 iIndex, Pointer ptr,  pNodeFunc pfnCallMe)
1138 {
1139   /* this routine iterates through all the models in a MSD */
1140   /* it sets the iActiveModel setting in the parent MSD    */
1141   /* then calls TraverseAll - it is up to you to look at   */
1142   /* iActiveModel with GetActiveModel(pfbThis) */
1143   /* you are allowed to call this from the level of */
1144   /* structure, molecule or graph */
1145   PMSD pmsdThis = NULL;
1146   PDNML pdnmlThis = NULL;
1147   Int2 iHoldActive = 1;
1148   Int2 iTest = FALSE;
1149   pmsdThis = ToMSDParent(PFBFromDN(pdnModel));
1150   if (!pmsdThis) return 0;
1151   pdnmlThis = pmsdThis->pdnmlModels; /* the linked-list of models */
1152   iHoldActive = pmsdThis->iActiveModel;
1153   if (pdnmlThis)
1154   while (pdnmlThis)
1155     {
1156       pmsdThis->iActiveModel = (Int2) pdnmlThis->choice;
1157       switch (iTraverse)
1158         {
1159 	    case TRAVERSE_SOLID:
1160 	       iTest = TraverseSolids( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1161 	       break;
1162 	    case TRAVERSE_BOND:
1163 	       iTest = TraverseBonds( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1164 	       break;
1165         case TRAVERSE_INTRABOND:
1166 	       iTest = TraverseIntraBonds( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1167 	       break;
1168 	    case TRAVERSE_IBOND:
1169 	       iTest = TraverseIBonds( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1170 	       break;
1171 	    case TRAVERSE_ATOM:
1172 	       iTest = TraverseAtoms( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1173 	       break;
1174 	    case TRAVERSE_GRAPH:
1175 	       iTest = TraverseGraphs( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1176                break;
1177 	    case TRAVERSE_MOLECULE:
1178 	       iTest = TraverseMolecules( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1179 	       break;
1180 	    case TRAVERSE_ALL:
1181 	    default:
1182 	       iTest = TraverseAll( pdnModel, (Int4) pdnmlThis->choice, iIndex, ptr, pfnCallMe);
1183 
1184 	}
1185       if (iTest == FALSE) break;
1186       pdnmlThis = pdnmlThis->next;
1187     }
1188   pmsdThis->iActiveModel = iHoldActive;
1189   return iTest;
1190 }
1191 
1192 
TraverseOneModel(DValNodePtr pdnModel,Int2 iTraverse,Int2 iModel,Int4 iIndex,Pointer ptr,pNodeFunc pfnCallMe)1193 Int2 LIBCALL TraverseOneModel(DValNodePtr pdnModel, Int2 iTraverse, Int2 iModel,
1194 			Int4 iIndex, Pointer ptr, pNodeFunc pfnCallMe)
1195 {
1196   Int2 iHoldActive = 1;
1197   Int2 iTest = FALSE;
1198 
1199   iHoldActive = SetActiveModel(PFBFromDN(pdnModel), iModel);
1200      switch (iTraverse)
1201         {
1202 	    case TRAVERSE_SOLID:
1203 	       iTest = TraverseSolids( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1204 	       break;
1205 	    case TRAVERSE_BOND:
1206 	       iTest = TraverseBonds( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1207 	       break;
1208         case TRAVERSE_INTRABOND:
1209 	       iTest = TraverseIntraBonds( pdnModel, (Int4)  iModel, iIndex, ptr, pfnCallMe);
1210 	       break;
1211 	    case TRAVERSE_IBOND:
1212 	       iTest = TraverseIBonds( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1213 	       break;
1214 	    case TRAVERSE_ATOM:
1215 	       iTest = TraverseAtoms( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1216 	       break;
1217 	    case TRAVERSE_GRAPH:
1218 	       iTest = TraverseGraphs( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1219                break;
1220 	    case TRAVERSE_MOLECULE:
1221 	       iTest = TraverseMolecules( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1222 	       break;
1223 	    case TRAVERSE_ALL:
1224 	    default:
1225 	       iTest = TraverseAll( pdnModel, (Int4) iModel, iIndex, ptr, pfnCallMe);
1226 
1227 	}
1228   iModel = SetActiveModel(PFBFromDN(pdnModel), iHoldActive);
1229   return iTest;
1230 }
1231 
1232 
1233 
1234 
1235 
IndexFromNode(PFB pfbNode)1236 Int2 LIBCALL IndexFromNode(PFB pfbNode)
1237 {
1238     PMSD  pmsdThis;
1239     PMMD  pmmdThis;
1240     PMGD  pmgdThis;
1241     PMAD  pmadThis;
1242     if (!pfbNode) return 0;
1243     switch ((int)pfbNode->bMe)
1244       {
1245 	  case AM_MSD:
1246 		pmsdThis = (PMSD) pfbNode;
1247 		return pmsdThis->pdnmsLink->choice;
1248 	  case AM_MMD:
1249 		pmmdThis = (PMMD) pfbNode;
1250 		return pmmdThis->pdnmmLink->choice;
1251 	  case AM_MGD:
1252 		pmgdThis = (PMGD) pfbNode;
1253 		return pmgdThis->pdnmgLink->choice;
1254 	  case AM_MAD:
1255 	        pmadThis = (PMAD) pfbNode;
1256 		return pmadThis->iIndex;
1257 	  case AM_MBD:
1258 	  case AM_MOD:
1259           case AM_MDD:
1260 	  case AM_UNK:
1261 	     return 0;
1262       }
1263     return 0;
1264 }
1265 
ToMSDParent(PFB pfbThis)1266 PMSD LIBCALL ToMSDParent(PFB pfbThis)
1267  {
1268    PMSD pmsdThis = NULL;
1269 
1270     if (! pfbThis) return NULL;
1271      if (!pfbThis->pfbParent)
1272        {
1273          if (pfbThis->bMe & (Byte) AM_MSD) return (PMSD) pfbThis;
1274        }
1275      else
1276        {
1277         pmsdThis = ToMSDParent((PFB) pfbThis->pfbParent);
1278 	return pmsdThis;
1279        }
1280     return NULL;
1281  }
1282 
ParentStrucName(PFB pfbThis)1283 CharPtr LIBCALL ParentStrucName(PFB pfbThis)
1284  {
1285    PMSD pmsdThis = NULL;
1286    pmsdThis = ToMSDParent(pfbThis);
1287    if (pmsdThis) return pmsdThis->pcPDBName;
1288     else return NULL;
1289  }
1290 
GetParentGraph(PFB pfbThis)1291 PMGD LIBCALL GetParentGraph(PFB pfbThis)
1292  {  /* use for atoms and bonds */
1293      PFB pfbHold = NULL;
1294 
1295      pfbHold = pfbThis->pfbParent;
1296      if (pfbThis->bMe == (Byte) AM_ALD) pfbHold = pfbHold->pfbParent;
1297      if ((pfbThis->bMe & (Byte) AM_MAD) || (pfbThis->bMe & (Byte) AM_MBD)
1298          || (pfbThis->bMe & (Byte) AM_ALD))
1299        {
1300          if (pfbHold->bMe & (Byte) AM_MGD)
1301 	    return (PMGD) pfbHold;
1302 	 else return NULL;  /* IRBonds and IMBonds have no parent ! */
1303        }
1304      else return NULL;
1305  }
1306 
ParentGraphPDBNo(PFB pfbThis)1307 CharPtr LIBCALL ParentGraphPDBNo(PFB pfbThis)
1308  {
1309      PMGD pmgdThis = NULL;
1310      pmgdThis = GetParentGraph(pfbThis);
1311      if (pmgdThis)
1312         return pmgdThis->pcGraphNum;
1313      else return NULL;
1314  }
1315 
ParentGraphPDBName(PFB pfbThis)1316 CharPtr LIBCALL ParentGraphPDBName(PFB pfbThis)
1317  {
1318      PMGD pmgdThis = NULL;
1319      pmgdThis = GetParentGraph(pfbThis);
1320      if (pmgdThis)
1321         return pmgdThis->pcGraphName;
1322      else return NULL;
1323  }
1324 
ParentGraphIUPAC1(PFB pfbThis)1325 CharPtr LIBCALL ParentGraphIUPAC1(PFB pfbThis)
1326  {  /* returns X when not one of 20 amino acids or 4 NT bases */
1327      PMGD pmgdThis = NULL;
1328      pmgdThis = GetParentGraph(pfbThis);
1329      if (pmgdThis)
1330         return pmgdThis->pcIUPAC;
1331      else return NULL;
1332  }
1333 
1334 
ParentGraphNum(PFB pfbThis)1335 Int2 LIBCALL ParentGraphNum(PFB pfbThis)
1336  {
1337    PMGD pmgdThis = NULL;
1338      pmgdThis = GetParentGraph(pfbThis);
1339    if (pmgdThis)
1340       {
1341         return pmgdThis->pdnmgLink->choice;
1342       }
1343    return 0;
1344  }
1345 
GetParentMol(PFB pfbThis)1346 PMMD LIBCALL GetParentMol(PFB pfbThis)
1347  { /* use for bonds,  atom,  graphs */
1348    PFB pfbHold = NULL;
1349    if (!pfbThis) return NULL;
1350    pfbHold = pfbThis->pfbParent;
1351    switch ((int)pfbThis->bMe)
1352     {
1353       case AM_MBD:
1354           switch ((int)pfbHold->bMe)
1355 	     {
1356 	       case AM_MGD:  /* graph bond */
1357 	           pfbHold = pfbHold->pfbParent;
1358 		   if (pfbHold->bMe & (Byte) AM_MMD) return (PMMD) pfbHold;
1359 		   else return NULL;
1360 	       case AM_MMD:  /* inter-res Bond */
1361 	           return (PMMD) pfbHold;
1362 	       case AM_MSD:  /* inter-mol bond */
1363 	       default:
1364 		    return NULL; /* no parent */
1365 	     }
1366        case AM_ALD:
1367            pfbHold = pfbHold->pfbParent;
1368        case AM_MAD:
1369 	   if (pfbHold->bMe & (Byte) AM_MGD) pfbHold = pfbHold->pfbParent;
1370 	   else return NULL;
1371 	   if (pfbHold->bMe & (Byte) AM_MMD) return (PMMD) pfbHold;
1372 	   else return NULL;
1373        case AM_MGD:
1374 	   if (pfbHold->bMe & (Byte) AM_MMD) return (PMMD) pfbHold;
1375 	   else return NULL;
1376        default:
1377 	   pfbHold = NULL;
1378     }
1379     return (PMMD) pfbHold;
1380  }
1381 
1382 
1383 
1384 
ParentMolName(PFB pfbThis)1385 CharPtr LIBCALL ParentMolName(PFB pfbThis)
1386  {
1387      PMMD pmmdThis;
1388      pmmdThis = GetParentMol(pfbThis);
1389      if (pmmdThis) return pmmdThis->pcMolName;
1390      else return NULL;
1391  }
1392 
1393 
ParentMolNum(PFB pfbThis)1394 Int2 LIBCALL ParentMolNum(PFB pfbThis)
1395  {
1396    PMMD pmmdThis = NULL;
1397    pmmdThis = GetParentMol(pfbThis);
1398    if (pmmdThis)
1399       {
1400         return pmmdThis->pdnmmLink->choice;
1401       }
1402    return 0;
1403  }
1404 
PFBFromVN(ValNodePtr pvnModel)1405 PFB LIBCALL PFBFromVN(ValNodePtr pvnModel)
1406  {
1407      PFB pfbThis = NULL;
1408      pfbThis =   (PFB) pvnModel->data.ptrvalue;
1409      return pfbThis;
1410  }
1411 
PFBFromDN(DValNodePtr pdnModel)1412 PFB LIBCALL PFBFromDN(DValNodePtr pdnModel)
1413  {
1414      PFB pfbThis = NULL;
1415      pfbThis =  (PFB) pdnModel->data.ptrvalue;
1416      return pfbThis;
1417  }
1418 
DNFromPFB(PFB pfbThis)1419 DValNodePtr DNFromPFB(PFB pfbThis)
1420 {
1421     PMSD pmsdThis;
1422     PMMD pmmdThis;
1423     PMGD pmgdThis;
1424     DValNodePtr pdnThis = NULL;
1425     if (pfbThis)
1426     switch ((int)pfbThis->bMe)
1427       {
1428 	case AM_MSD:
1429 	    pmsdThis = (PMSD) pfbThis;
1430 	    pdnThis = (DValNodePtr) pmsdThis->pdnmsLink;
1431 	    return pdnThis;
1432 	case AM_MMD:
1433 	    pmmdThis = (PMMD) pfbThis;
1434 	    pdnThis = (DValNodePtr) pmmdThis->pdnmmLink;
1435 	    return pdnThis;
1436 	case AM_MGD:
1437 	    pmgdThis = (PMGD) pfbThis;
1438 	    pdnThis = (DValNodePtr) pmgdThis->pdnmgLink;
1439 	    return pdnThis;
1440 	default:
1441 	  return NULL;
1442       }
1443    return NULL;
1444 }
1445 
1446 
SetActiveModel(PFB pfbThis,Int2 iModel)1447 Int2 LIBCALL SetActiveModel(PFB pfbThis, Int2 iModel)
1448  {
1449    PMSD pmsdThis;
1450    Int2 iOldModel;
1451    if (!pfbThis) return 0;
1452    pmsdThis = ToMSDParent(pfbThis);
1453    iOldModel = pmsdThis->iActiveModel;
1454    pmsdThis->iActiveModel = iModel;
1455    return iOldModel;
1456  }
1457 
1458 
GetActiveModel(PFB pfbThis)1459 Int2 LIBCALL GetActiveModel(PFB pfbThis)
1460  {
1461      PMSD pmsdThis = NULL;
1462      if (!pfbThis) return 0;
1463      pmsdThis = (PMSD) ToMSDParent(pfbThis);
1464      return pmsdThis->iActiveModel;
1465  }
1466 
1467 
GetAtomLocs(PMAD pmadThis,Int2 iModel)1468 PALD LIBCALL GetAtomLocs(PMAD pmadThis,  Int2 iModel)
1469  {
1470      PVNAL pvnalThis = NULL;
1471      if (pmadThis)
1472      if (iModel == 0)
1473 	{
1474 	   if (pmadThis->pvnalLocate)
1475 	     return (PALD) pmadThis->pvnalLocate->data.ptrvalue;
1476 	}
1477      else
1478 	{
1479 	    pvnalThis = ValNodeFindNext(pmadThis->pvnalLocate, NULL, iModel);
1480 	     if (pvnalThis) return (PALD) pvnalThis->data.ptrvalue;
1481 	}
1482     return NULL;
1483  }
1484 
1485 
1486 
1487 
1488 /* can call: paldThis = GetLocation(GetAtomLocs(pmadThis, GetActiveModel(pmadThis)), 0, ' ') */
1489 /* need a GetActiveCoordSet(iModel), GetEnsembleString(iModel, iCoordset ) */
1490 
1491 
1492 
1493 
GetLocation(PALD paldList,Int2 iCoord,Char cAlt)1494 PALD LIBCALL GetLocation(PALD paldList,  Int2 iCoord,  Char cAlt)
1495 {
1496     PALD paldThis = NULL;
1497     if (iCoord) ; /* later eh */
1498     paldThis = paldList;
1499     while (paldThis)
1500        {
1501 	    if (paldThis->cAltConf == cAlt)
1502 	         {
1503 		     return paldThis;
1504 		 }
1505 	    paldThis = paldThis->next;
1506        }
1507     return NULL;
1508 }
1509 
1510 
1511 
1512 
1513 
1514 /* not for external-use only for AssignAtomLocId */
UniqueLocId(PFB pfbThis,Int4 iModel,Int4 iIndex,Pointer ptr)1515 static void LIBCALLBACK UniqueLocId(PFB pfbThis, Int4 iModel, Int4 iIndex, Pointer ptr)
1516 {
1517    PMAD pmadThis = NULL;
1518    PALD paldThis = NULL;
1519    Int4Ptr piIndex;
1520    piIndex = (Int4Ptr) ptr;
1521    if (pfbThis->bMe == (Byte) AM_MAD)
1522      {  /* iIndex isn't used */
1523 	pmadThis = (PMAD) pfbThis;
1524 	paldThis = GetAtomLocs(pmadThis, iModel);
1525  	while (paldThis)
1526 	   {  /* dosen't care about coord-set id's, alt-confs or ensembles */
1527 	     *piIndex = (Int4) (*piIndex + 1);
1528 	     paldThis->iUniqueId = (Int4) (*piIndex);
1529 	     paldThis = paldThis->next;
1530 	   }
1531      }
1532 }
1533 
1534 
AssignAtomLocId(PDNMS pdnmsThis)1535 Int4 LIBCALL AssignAtomLocId(PDNMS pdnmsThis)
1536 {
1537     Int2 iTest = 0;
1538     Int4 iReservedIndex = 0;
1539     ProgMon("Assigning Location ID's");
1540     iTest = TraverseModels(pdnmsThis, TRAVERSE_ATOM,
1541               0, (Pointer) &iReservedIndex, UniqueLocId );
1542     return iReservedIndex;  /* the number of locs assigned - also the number of xyz coords */
1543 }
1544 
1545 
ElementName(Int1 iAtomicNo)1546 CharPtr LIBCALL ElementName(Int1 iAtomicNo)
1547 {
1548 	if ((iAtomicNo < MAX_ELEMENTS) && (iAtomicNo > 0))
1549 	  return IUPACElement[iAtomicNo];
1550 	else return NULL;
1551 }
1552 
ElementSize(Int1 iAtomicNo)1553 FloatLo LIBCALL ElementSize(Int1 iAtomicNo)
1554 {
1555 	if ((iAtomicNo < MAX_ELEMENTS) && (iAtomicNo > 0))
1556 	  return  CommonSize[iAtomicNo];
1557 	else return (FloatLo) 0.0;
1558 }
1559 
1560 
AminoAcidName(Char cThis,Int2 iRetCode)1561 CharPtr LIBCALL AminoAcidName(Char cThis, Int2 iRetCode)
1562 {
1563 	CharPtr pcTemp = NULL;
1564 	Int2 iCount = 0;
1565 
1566 	pcTemp = NCBIstdaaUC;
1567 	while (pcTemp[iCount])
1568    	  {
1569 		if (pcTemp[iCount] == cThis)
1570 		  switch(iRetCode)
1571 		   {
1572 			case USE_UPPERCASE:
1573 			  return NCBIstdaaUC3[iCount];
1574 			case USE_LOWERCASE:
1575 			  return NCBIstdaaLC3[iCount];
1576 		 	case USE_FULLNAME:
1577 			  return NCBIstdaaLCF[iCount];
1578 			case USE_MIXCASE:
1579 			default:
1580 			  return NCBIstdaaMC3[iCount];
1581 		   }
1582 		iCount++;
1583 	  }
1584 	return NULL;
1585 }
1586 
1587 
AminoAcidNameFromIdx(Int2 iIndex,Int2 iRetCode)1588 CharPtr LIBCALL AminoAcidNameFromIdx(Int2 iIndex, Int2 iRetCode)
1589 {
1590 	 if (iIndex < MAX_NCBIstdaa)
1591 	    switch(iRetCode)
1592 		   {
1593 			case USE_UPPERCASE:
1594 			  return NCBIstdaaUC3[iIndex];
1595 			case USE_LOWERCASE:
1596 			  return NCBIstdaaLC3[iIndex];
1597 		 	case USE_FULLNAME:
1598 			  return NCBIstdaaLCF[iIndex];
1599 			case USE_MIXCASE:
1600 			default:
1601 			  return NCBIstdaaMC3[iIndex];
1602 		   }
1603 	return NULL;
1604 }
1605 
SetBackBone(PFB pfbThis,Int4 iModel,Int4 iIndex,Pointer ptr)1606 void LIBCALLBACK SetBackBone(PFB pfbThis, Int4 iModel, Int4 iIndex, Pointer ptr)
1607 {
1608    PMAD pmadThis = NULL;
1609    PMMD pmmdThis = NULL;
1610 
1611     if (pfbThis->bMe != (Byte) AM_MAD)
1612         return;
1613     pmadThis = (PMAD) pfbThis;
1614     if (!pmadThis->pcAName) return;
1615 
1616     pmmdThis = GetParentMol((PFB) pmadThis);
1617     if (!pmmdThis) return;
1618 
1619     if ((pmmdThis->bWhat & (Byte) AM_DNA) || (pmmdThis->bWhat & (Byte) AM_RNA)) {
1620         if (!StringICmp(pmadThis->pcAName, " P  "))
1621             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_PALPHA | (Byte) AM_PARTIALBACKBONE | (Byte) AM_BACKBONE);
1622         else if (!StringICmp(pmadThis->pcAName, " O2P"))
1623             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE);
1624         else if (!StringICmp(pmadThis->pcAName, " O1P"))
1625             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE);
1626         else if (!StringICmp(pmadThis->pcAName, " C1*"))
1627             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_C1RIBOSE | (Byte) AM_BACKBONE);
1628         else if (!StringICmp(pmadThis->pcAName, " C2*"))
1629             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE);
1630         else if (!StringICmp(pmadThis->pcAName, " O2*"))
1631             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE);
1632         else if (!StringICmp(pmadThis->pcAName, " C3*"))
1633             pmadThis->bWhat = (Byte) (pmadThis->bWhat | (Byte) AM_BACKBONE | (Byte) AM_PARTIALBACKBONE);
1634         else if (!StringICmp(pmadThis->pcAName, " O3*"))
1635             pmadThis->bWhat = (Byte) (pmadThis->bWhat | (Byte) AM_BACKBONE | (Byte) AM_PARTIALBACKBONE);
1636         else if (!StringICmp(pmadThis->pcAName, " C4*"))
1637             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_C4RIBOSE | (Byte) AM_PARTIALBACKBONE| (Byte) AM_BACKBONE);
1638         else if (!StringICmp(pmadThis->pcAName, " O4*"))
1639             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE);
1640         else if (!StringICmp(pmadThis->pcAName, " C5*"))
1641             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE | (Byte) AM_PARTIALBACKBONE);
1642         else if (!StringICmp(pmadThis->pcAName, " O5*"))
1643             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE | (Byte) AM_PARTIALBACKBONE);
1644 /*     code that must test parent graph for Pyr or Pur
1645         else if (!StringICmp(pmadThis->pcAName, " N9 "))
1646         pmadThis->bWhat = (Byte) (pmadThis->bWhat | (Byte) AM_NBETA);
1647     else if (!StringICmp(pmadThis->pcAName, " N1 "))
1648             {    if A or G don't set this
1649         pmadThis->bWhat = (Byte) (pmadThis->bWhat | (Byte) AM_NBETA);
1650         return;
1651         }
1652     */
1653     }
1654 
1655     else if (pmmdThis->bWhat & (Byte) AM_PROT) {
1656         if (!StringICmp(pmadThis->pcAName," CA "))
1657             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_CALPHA | (Byte) AM_BACKBONE | (Byte) AM_PARTIALBACKBONE);
1658         else if (!StringICmp(pmadThis->pcAName, " C  ") ||
1659                  !StringICmp(pmadThis->pcAName, " N  "))
1660             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE | (Byte) AM_PARTIALBACKBONE);
1661 
1662         else if (!StringICmp(pmadThis->pcAName, " H  ") ||
1663                  !StringICmp(pmadThis->pcAName, " D  ") ||
1664                  !StringICmp(pmadThis->pcAName, "1H  ") ||
1665                  !StringICmp(pmadThis->pcAName, "2H  ") ||
1666                  !StringICmp(pmadThis->pcAName, "3H  ") ||
1667                  !StringICmp(pmadThis->pcAName, "1D  ") ||
1668                  !StringICmp(pmadThis->pcAName, "2D  ") ||
1669                  !StringICmp(pmadThis->pcAName, "3D  ") ||
1670                  !StringICmp(pmadThis->pcAName, " O  ") ||
1671                  !StringICmp(pmadThis->pcAName, " OXT") ||
1672                  !StringICmp(pmadThis->pcAName, " HXT") ||
1673                  !StringICmp(pmadThis->pcAName, " DXT"))
1674             pmadThis->bWhat = (Byte)(pmadThis->bWhat | (Byte) AM_BACKBONE);
1675 
1676         else if (!StringICmp(pmadThis->pcAName," CB "))
1677             pmadThis->bWhat = (Byte) (pmadThis->bWhat | (Byte) AM_CBETA);
1678     } /* if AM_PROT */
1679 
1680     return;
1681 }
1682 
1683 
AssignBackBone(PDNMS pdnmsThis)1684 void LIBCALL AssignBackBone(PDNMS pdnmsThis)
1685 {
1686     Int2 iTest = 0;
1687     ProgMon("Assigning Backbone");
1688     iTest =  TraverseAtoms(pdnmsThis,  0, 0, NULL,  SetBackBone);
1689     return;
1690 }
1691 
AtomDistanceSq(PMAD pmadFrom,PMAD pmadTo,Int2 iModel,Char cAlt)1692 FloatLo LIBCALL AtomDistanceSq(PMAD pmadFrom, PMAD pmadTo, Int2 iModel, Char  cAlt)
1693 {
1694   PALD paldFrom = NULL;
1695   PALD paldTo = NULL;
1696   FloatLo flDist = 0.0;
1697   FloatLo flX, flY, flZ;
1698 
1699 
1700   if (!(pmadFrom)) return 0.0;
1701   if (!(pmadTo)) return 0.0;
1702   paldFrom = GetLocation(GetAtomLocs(pmadFrom, iModel), 0, cAlt);
1703   paldTo = GetLocation(GetAtomLocs(pmadTo, iModel), 0, cAlt);
1704   if ((!paldFrom) || (!paldTo)) return -1.0;
1705   flX = paldFrom->pflvData[0] - paldTo->pflvData[0];
1706   flY = paldFrom->pflvData[1] - paldTo->pflvData[1];
1707   flZ = paldFrom->pflvData[2] - paldTo->pflvData[2];
1708   flDist = flX * flX + flY * flY + flZ * flZ;
1709   return flDist;
1710 }
1711 
1712 
LinkAlpha(PFB pfbThis,Int4 iModel,Int4 iIndex,Pointer ptr)1713 void LIBCALLBACK LinkAlpha(PFB pfbThis,  Int4 iModel,  Int4 iIndex,  Pointer ptr)
1714 {  /* only used while walking within a single molecule */
1715    /* iModel is NOT passed through */
1716     PMAD pmadThis = NULL;
1717     PMAD pmadHold = NULL;
1718     PPFB ppfbHold = NULL;
1719     PVNMB pvnmbThis = NULL;
1720     PMBD pmbdThis = NULL;
1721     PMGD pmgdThis = NULL;
1722     FloatLo flDistSq = 0.0;
1723     ValNodePtr pvnThis = NULL;
1724     if (pfbThis->bMe == (Byte) AM_MAD)
1725     if ((pfbThis->bWhat & (Byte) AM_CALPHA)  ||
1726 	 (pfbThis->bWhat & (Byte) AM_PALPHA))
1727       {  /* do the link to the last Alpha pointed to by ptr */
1728   	ppfbHold = (PPFB) ptr;
1729 	pmadHold = (PMAD) *ppfbHold;
1730 	pmadThis = (PMAD) pfbThis;
1731         /* set the hold */
1732 	*ppfbHold = pfbThis;
1733 	if (pmadHold)
1734 	  {  /* we have a bond */
1735 	     pmgdThis = GetParentGraph(pfbThis);
1736 	     /* check the distance for the outrageous */
1737 	     flDistSq = AtomDistanceSq(pmadHold, pmadThis, GetActiveModel(pfbThis), ' ');
1738 	     if ((pmgdThis->bWhat & RES_AA) && ( flDistSq > VIRTUAL_BOND_AA_MAX_SQ))
1739                  return;
1740        	     if (((pmgdThis->bWhat & RES_RNA) || (pmgdThis->bWhat & RES_DNA)) &&
1741 	           ( flDistSq > VIRTUAL_BOND_NA_MAX_SQ))
1742                  return;
1743 
1744 	     pvnmbThis = NewVNMB(&pmgdThis->pvnmbBHead,(Int2) VIRTUAL_BOND_CHOICE); /* 7 = Other */
1745 	     if (!pvnmbThis) return;  /* callback fails silently */
1746 	     pmgdThis->iBondCount += 1;
1747 	     pmbdThis = (PMBD) pvnmbThis->data.ptrvalue;
1748 	     pmbdThis->pfbParent = (PFB) pmgdThis;
1749 	     pmbdThis->bWhat = (Byte)(pmbdThis->bWhat | (Byte) BOND_VIRTUAL);
1750 	     pmbdThis->pmadFrom = pmadThis;
1751 	     pvnThis = ValNodeAddPointer(&pmadThis->pvnBonds,
1752 					 (Int2) VIRTUAL_BOND_CHOICE,
1753 					 (Nlm_VoidPtr) pmbdThis);
1754 	     if (!pvnThis) return;
1755 	     pmbdThis->pmadTo = pmadHold;
1756 	     pvnThis = ValNodeAddPointer(&pmadHold->pvnBonds,
1757 					(Int2) VIRTUAL_BOND_CHOICE,
1758 					(Nlm_VoidPtr) pmbdThis);
1759 	     if (!pvnThis) return;
1760 	  }
1761      }
1762 
1763    return;
1764 }
1765 
1766 
SetVirtualBB(PFB pfbThis,Int4 iModel,Int4 iIndex,Pointer ptr)1767 void LIBCALLBACK SetVirtualBB(PFB pfbThis,  Int4 iModel,  Int4 iIndex,  Pointer ptr)
1768 {
1769     PDNMG pdnmgThis = NULL;
1770     PMMD pmmdThis = NULL;
1771     PFB pfbHold = NULL;
1772 
1773     if (pfbThis->bMe == (Byte) AM_MMD)
1774       {
1775 	  pmmdThis = (PMMD) pfbThis;
1776 	  if ((pmmdThis->bWhat == (Byte) AM_PROT) ||
1777 	     (pmmdThis->bWhat == (Byte) AM_DNA) ||
1778 	      (pmmdThis->bWhat == (Byte) AM_RNA))
1779 	    {
1780 		pdnmgThis = pmmdThis->pdnmgHead;
1781 		TraverseAtoms(pdnmgThis, iModel, iIndex, (Pointer) &pfbHold,
1782                    LinkAlpha);
1783 	    }
1784       }
1785    return;
1786 }
1787 
AssignVirtual(PDNMS pdnmsThis)1788 void LIBCALL AssignVirtual(PDNMS pdnmsThis)
1789 {
1790     Int2 iTest = 0;
1791     ProgMon("Assigning Virtual Bonds");
1792     iTest = TraverseMolecules(pdnmsThis,  0,  0,  NULL,  SetVirtualBB);
1793    /* iTest = TraverseIBonds(pdnmsThis,  0, 0, NULL, SetHetVirt); */
1794 
1795 }
1796 
SetIons(PFB pfbThis,Int4 iModel,Int4 iIndex,Pointer ptr)1797 void LIBCALLBACK SetIons(PFB pfbThis,  Int4 iModel,  Int4 iIndex,  Pointer ptr)
1798 {
1799     PMAD pmadThis = NULL;
1800     PMMD pmmdThis = NULL;
1801     PMSD pmsdThis = NULL;
1802     Int2 iCount;
1803     Boolean bIon = FALSE;
1804     if (pfbThis->bMe == (Byte) AM_MAD)
1805       {
1806 	  pmadThis = (PMAD) pfbThis;
1807 	  pmmdThis = GetParentMol(pfbThis);
1808 	  pmsdThis = ToMSDParent(pfbThis);
1809 	  if ((Int1) pmadThis->pvnmaLink->choice > MAX_ELEMENTS)
1810 	     {  /* report on unknown elements */
1811 	/*	 MsgAlert(KEY_NONE,SEV_ERROR,"MMDB Warning", "Unknown Element\nNotify NCBI of Accession No: %s",pmsdThis->pcPDBName);
1812 	*/	 return;
1813 	     }
1814 	  /* catch-C,O,N,H - will return faster than the other way */
1815 	  /* for the large majority of biomolecular atoms */
1816 	  if ((Int1) pmadThis->pvnmaLink->choice == 1) return;
1817 	  if ((Int1) pmadThis->pvnmaLink->choice == 6) return;
1818 	  if ((Int1) pmadThis->pvnmaLink->choice == 8) return;
1819 	  if ((Int1) pmadThis->pvnmaLink->choice == 7) return;
1820 	  bIon = TRUE; /* assume that it is an ion  */
1821 	  for (iCount = 0; iCount < MAX_NOT_ION; iCount++)
1822 	     {
1823 	       if ((Uint1) pmadThis->pvnmaLink->choice == IsNotIon[iCount])
1824 	         {  /* found it is not an ion */
1825 		   bIon = FALSE;
1826 		   break;
1827 		 }
1828 	     }
1829 	  if (bIon) {
1830          if(pmmdThis->bWhat != AM_PROT && pmmdThis->bWhat != AM_DNA && pmmdThis->bWhat != AM_RNA){
1831 		    pmmdThis->bWhat = (Byte) (AM_ION);
1832 		    pmadThis->bUpdate = (Byte) (pmadThis->bUpdate | AM_ION);
1833 		    pmsdThis->bWhat = (Byte) (pmsdThis->bWhat | AM_ION);
1834 	     }
1835       }
1836       }
1837    return;
1838 }
1839 
1840 
1841 
1842 
1843 
AssignIons(PDNMS pdnmsThis)1844 void LIBCALL AssignIons(PDNMS pdnmsThis)
1845 {
1846     Int2 iTest = 0;
1847     ProgMon("Assigning Ions");
1848     iTest = TraverseAtoms(pdnmsThis,  0,  0,  NULL,  SetIons);
1849 }
1850 
1851 
1852 
GetFirstGraph(PMMD pmmdThis)1853 PDNMG LIBCALL GetFirstGraph(PMMD pmmdThis)
1854 {
1855   if (!pmmdThis) return NULL;
1856   return pmmdThis->pdnmgHead;
1857 }
1858 
GetLastGraph(PMMD pmmdThis)1859 PDNMG LIBCALL GetLastGraph(PMMD pmmdThis)
1860 {
1861   PDNMG pdnmgThis = NULL;
1862   if (!pmmdThis) return NULL;
1863   pdnmgThis = pmmdThis->pdnmgHead;
1864   while (pdnmgThis)
1865    {
1866       if (pdnmgThis->next == NULL) return pdnmgThis;
1867       pdnmgThis = pdnmgThis->next;
1868    }
1869  return NULL;
1870 }
1871 
1872 
GetNextGraph(PDNMG pdnmgThis)1873 PDNMG LIBCALL GetNextGraph(PDNMG pdnmgThis)
1874 {
1875   if (!pdnmgThis) return NULL;
1876   return pdnmgThis->next;
1877 }
1878 
GetPrevGraph(PDNMG pdnmgThis)1879 PDNMG LIBCALL GetPrevGraph(PDNMG pdnmgThis)
1880 {
1881   if (!pdnmgThis) return NULL;
1882   return pdnmgThis->last;
1883 }
1884 
GetMainAtom(PDNMG pdnmgThis)1885 PMAD LIBCALL GetMainAtom(PDNMG pdnmgThis)
1886 {
1887   PMGD pmgdThis = NULL;
1888   PVNMA pvnmaThis = NULL;
1889   PMAD pmadThis = NULL;
1890   Byte MainAtom = 0;
1891 
1892   if (!pdnmgThis) return NULL;
1893   pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1894   if ((pmgdThis->bWhat & (Byte) RES_RNA) || (pmgdThis->bWhat & (Byte) RES_DNA))
1895      MainAtom = AM_PALPHA;
1896   if (pmgdThis->bWhat & (Byte) RES_AA) MainAtom = AM_CALPHA;
1897   pvnmaThis = pmgdThis->pvnmaAHead;
1898   while (pvnmaThis)
1899    {
1900      pmadThis = (PMAD) pvnmaThis->data.ptrvalue;
1901      if (pmadThis->bWhat & MainAtom)
1902        return pmadThis;
1903      pvnmaThis = pvnmaThis->next;
1904    }
1905  return NULL;
1906 }
1907 
FindMolByChar(PFB pfbThis,Int4 iModel,Int4 iIndex,Pointer ptr)1908 void LIBCALLBACK FindMolByChar(PFB pfbThis, Int4 iModel, Int4 iIndex, Pointer ptr)
1909 {
1910   PMMD pmmdMol;
1911   Char c;
1912   PDNMM *ppdnmmFound;
1913 
1914     ppdnmmFound = (PDNMM *)ptr;
1915     if (pfbThis->bMe == AM_MMD)
1916       {
1917           /* cast to the correct PMMD pointer type */
1918           pmmdMol = (PMMD) pfbThis;
1919           c = (char) iIndex;
1920           if (c == pmmdMol->pcMolName[0])
1921             {
1922                *ppdnmmFound = pmmdMol->pdnmmLink;
1923             }
1924       }
1925 }
1926 
1927 
MolFromPDBChain(PDNMS pdnmsThis,char c)1928 PDNMM LIBCALL MolFromPDBChain(PDNMS pdnmsThis,  char c)
1929 {
1930 
1931     Int2 iTest;
1932     PDNMM pdnmmThis = NULL;
1933 
1934     if (!pdnmsThis)
1935       return NULL;
1936 
1937     iTest = TraverseMolecules( (DValNodePtr) pdnmsThis, 1, c,
1938                           &pdnmmThis, FindMolByChar);
1939     return pdnmmThis;
1940 }
1941 
1942 
SeqStringFromMol(PDNMM pdnmmThis)1943 CharPtr LIBCALL SeqStringFromMol(PDNMM pdnmmThis)
1944 {
1945    PMMD pmmdThis = NULL;
1946    PDNMG pdnmgThis = NULL;
1947    PMGD pmgdThis = NULL;
1948    Int4 iTest =0;
1949    CharPtr pcSeq = NULL;
1950    Int4 i = 0;
1951 
1952    if (!pdnmmThis)
1953      return NULL;
1954 
1955    pmmdThis = (PMMD) pdnmmThis->data.ptrvalue;
1956 
1957    if (pmmdThis->iResCount > 1)
1958      {
1959        pcSeq = (CharPtr) MemNew((size_t) ((pmmdThis->iResCount + 1) * sizeof(Char)) );
1960        pdnmgThis = pmmdThis->pdnmgHead;
1961        while (pdnmgThis)
1962          {
1963 	     pmgdThis = (PMGD) pdnmgThis->data.ptrvalue;
1964 	     pcSeq[i] = pmgdThis->pcIUPAC[0];
1965 	     i++;
1966 	     pdnmgThis = pdnmgThis->next;
1967 	 }
1968        pcSeq[i] = '\0';
1969      }
1970    return pcSeq;
1971 }
1972 /*------------------------------------------------------*/
GetMGFromMM(PMMD pmmdThis,Int4 iRes)1973 PMGD LIBCALL GetMGFromMM(PMMD pmmdThis, Int4 iRes)
1974 {
1975   PDNMG pdnmgThis = NULL;
1976   PMGD  pmgdThis = NULL;
1977 
1978   pdnmgThis = pmmdThis->pdnmgHead;
1979   if(pdnmgThis == NULL) return(NULL);
1980 
1981   while(pdnmgThis){
1982      if(pdnmgThis->choice == iRes){
1983         pmgdThis = pdnmgThis->data.ptrvalue;
1984         break;
1985      }
1986      pdnmgThis = pdnmgThis->next;
1987   }
1988 
1989   return(pmgdThis);
1990 
1991 }
1992 /*------------------------------------------------------*/
GetMMFromMSDBySeqId(PMSD pmsdThis,SeqIdPtr sip)1993 PMMD LIBCALL GetMMFromMSDBySeqId(PMSD pmsdThis, SeqIdPtr sip)
1994 {
1995   PDNMM pdnmmHead;
1996   PMMD pmmdThis;
1997 
1998   pdnmmHead = pmsdThis->pdnmmHead;
1999   while(pdnmmHead){
2000      pmmdThis = pdnmmHead->data.ptrvalue;
2001      if(pmmdThis){
2002         if(SeqIdForSameBioseq(pmmdThis->pSeqId, sip)) return pmmdThis;
2003      }
2004      pdnmmHead = pdnmmHead->next;
2005   }
2006 
2007   return(NULL);
2008 }
2009