1 /*  objseq.c
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
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 have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name:  objseq.c
27 *
28 * Author:  James Ostell
29 *
30 * Version Creation Date: 4/1/91
31 *
32 * $Revision: 6.42 $
33 *
34 * File Description:  Object manager for module NCBI-Seq
35 *
36 * Modifications:
37 * --------------------------------------------------------------------------
38 * Date       Name        Description of modification
39 * -------  ----------  -----------------------------------------------------
40 *
41 *
42 *
43 *
44 * ==========================================================================
45 */
46 
47 /** for ErrPostEx() ****/
48 
49 static char *this_module = "ncbiobj";
50 #define THIS_MODULE this_module
51 static char *this_file = __FILE__;
52 #define THIS_FILE this_file
53 
54 /**********************/
55 
56 #include <objseq.h>           /* the pub interface */
57 #include <asnseq.h>        /* the AsnTool header */
58 #include <objmgr.h>
59 #include <sequtil.h>
60 #include <objgbseq.h>
61 #include <objinsdseq.h>
62 #include <objtseq.h>
63 #include <objtable.h>
64 
65 static Boolean SeqDescrAsnWriteExtra (ValNodePtr anp, AsnIoPtr aip, AsnTypePtr orig,
66                     Boolean anp_not_null);
67 static Boolean SeqAnnotSetAsnWriteExtra (SeqAnnotPtr sap, AsnIoPtr aip, AsnTypePtr set,
68                      AsnTypePtr element, Boolean sap_not_null);
69 static Boolean SeqAnnotAsnWriteExtra (SeqAnnotPtr sap, AsnIoPtr aip, AsnTypePtr orig,
70                           ValNodePtr extras);
71 
72 /*****************************************************************************
73 *
74 *   Bioseq ObjMgr Routines
75 *
76 *****************************************************************************/
77 static CharPtr bioseqtypename = "Bioseq";
78 
BioseqNewFunc(void)79 static Pointer LIBCALLBACK BioseqNewFunc (void)
80 {
81     return (Pointer) BioseqNew();
82 }
83 
BioseqFreeFunc(Pointer data)84 static Pointer LIBCALLBACK BioseqFreeFunc (Pointer data)
85 {
86     return (Pointer) BioseqFree ((BioseqPtr) data);
87 }
88 
BioseqAsnWriteFunc(Pointer data,AsnIoPtr aip,AsnTypePtr atp)89 static Boolean LIBCALLBACK BioseqAsnWriteFunc (Pointer data, AsnIoPtr aip, AsnTypePtr atp)
90 {
91     return BioseqAsnWrite((BioseqPtr)data, aip, atp);
92 }
93 
BioseqAsnReadFunc(AsnIoPtr aip,AsnTypePtr atp)94 static Pointer LIBCALLBACK BioseqAsnReadFunc (AsnIoPtr aip, AsnTypePtr atp)
95 {
96     return (Pointer) BioseqAsnRead (aip, atp);
97 }
98 
BioseqLabelFunc(Pointer data,CharPtr buffer,Int2 buflen,Uint1 content)99 static Int2 LIBCALLBACK BioseqLabelFunc ( Pointer data, CharPtr buffer, Int2 buflen, Uint1 content)
100 {
101     return BioseqLabel ((BioseqPtr)data, buffer, buflen, content);
102 }
103 
BioseqLabel(BioseqPtr bsp,CharPtr buffer,Int2 buflen,Uint1 content)104 NLM_EXTERN Int2 LIBCALL BioseqLabel ( BioseqPtr bsp, CharPtr buffer, Int2 buflen, Uint1 content)
105 {
106     CharPtr tmp;
107     Char label[40];
108     Int2 diff, len;
109 
110     if ((bsp == NULL) || (buflen < 1))
111         return 0;
112 
113     len = buflen;
114     label[0] = '\0';
115 
116     if (content != OM_LABEL_TYPE)
117     {
118         SeqIdWrite(bsp->id, label, PRINTID_FASTA_LONG, 39);
119         if (content == OM_LABEL_CONTENT)
120             return LabelCopy(buffer, label, buflen);
121 
122         diff = LabelCopyExtra(buffer, label, buflen, NULL, ": ");
123         buflen -= diff;
124         buffer += diff;
125     }
126 
127     label[0] = '\0';
128     tmp = label;
129     tmp = StringMove(tmp, AsnEnumTypeStr(SEQ_INST_repr, (Int2)(bsp->repr)));
130     tmp = StringMove(tmp, ", ");
131     tmp = StringMove(tmp, AsnEnumTypeStr(SEQ_INST_mol, (Int2)(bsp->mol)));
132     sprintf(tmp, " len= %ld", (long)(bsp->length));
133     diff = LabelCopy(buffer, label, buflen);
134     buflen -= diff;
135     buffer += diff;
136 
137     if (content != OM_LABEL_SUMMARY)
138         return (len - buflen);
139 
140     return (len - buflen);     /* SUMMARY not done yet */
141 }
142 
BioseqSubTypeFunc(Pointer ptr)143 static Uint2 LIBCALLBACK BioseqSubTypeFunc (Pointer ptr)
144 {
145     if (ptr == NULL)
146         return 0;
147     return (Uint2)((BioseqPtr)ptr)->repr;
148 }
149 
150 /*****************************************************************************
151 *
152 *   SeqAnnot ObjMgr Routines
153 *
154 *****************************************************************************/
155 static CharPtr seqannottypename = "SeqAnnot";
156 
SeqAnnotNewFunc(void)157 static Pointer LIBCALLBACK SeqAnnotNewFunc (void)
158 {
159     return (Pointer) SeqAnnotNew();
160 }
161 
SeqAnnotFreeFunc(Pointer data)162 static Pointer LIBCALLBACK SeqAnnotFreeFunc (Pointer data)
163 {
164     return (Pointer) SeqAnnotFree ((SeqAnnotPtr) data);
165 }
166 
SeqAnnotAsnWriteFunc(Pointer data,AsnIoPtr aip,AsnTypePtr atp)167 static Boolean LIBCALLBACK SeqAnnotAsnWriteFunc (Pointer data, AsnIoPtr aip, AsnTypePtr atp)
168 {
169     return SeqAnnotAsnWrite((SeqAnnotPtr)data, aip, atp);
170 }
171 
SeqAnnotAsnReadFunc(AsnIoPtr aip,AsnTypePtr atp)172 static Pointer LIBCALLBACK SeqAnnotAsnReadFunc (AsnIoPtr aip, AsnTypePtr atp)
173 {
174     return (Pointer) SeqAnnotAsnRead (aip, atp);
175 }
176 
SeqAnnotLabelFunc(Pointer data,CharPtr buffer,Int2 buflen,Uint1 content)177 static Int2 LIBCALLBACK SeqAnnotLabelFunc ( Pointer data, CharPtr buffer, Int2 buflen, Uint1 content)
178 {
179     return SeqAnnotLabel ((SeqAnnotPtr)data, buffer, buflen, content);
180 }
181 
SeqAnnotLabel(SeqAnnotPtr sap,CharPtr buffer,Int2 buflen,Uint1 content)182 NLM_EXTERN Int2 LIBCALL SeqAnnotLabel (SeqAnnotPtr sap, CharPtr buffer, Int2 buflen, Uint1 content)
183 {
184     CharPtr name;
185     static CharPtr sap_types[6] = {
186         "SeqAnnot..",
187         "FeatureTable",
188         "Alignments",
189         "Graphs" ,
190         "SeqIds" ,
191         "SeqLocs" };
192     Int2 rsult = 0;
193 
194     if ((sap == NULL) || (buflen < 1))
195         return 0;
196 
197     if (sap->type <= 5)
198         name = sap_types[sap->type];
199     else
200         name = sap_types[0];
201 
202     switch (content)
203     {
204         case OM_LABEL_BOTH:
205         case OM_LABEL_CONTENT:
206         case OM_LABEL_SUMMARY:
207         case OM_LABEL_TYPE:
208         default:
209             rsult = LabelCopy(buffer, name, buflen);
210     }
211     return rsult;
212 }
213 
SeqAnnotSubTypeFunc(Pointer ptr)214 static Uint2 LIBCALLBACK SeqAnnotSubTypeFunc (Pointer ptr)
215 {
216     if (ptr == NULL)
217         return 0;
218     return (Uint2)((SeqAnnotPtr)ptr)->type;
219 }
220 
221 /*****************************************************************************
222 *
223 *   SeqDesc ObjMgr Routines
224 *
225 *****************************************************************************/
226 static CharPtr seqdesctypename = "SeqDesc";
227 
SeqDescNewFunc(void)228 static Pointer LIBCALLBACK SeqDescNewFunc (void)
229 {
230     return (Pointer) SeqDescrNew(NULL);
231 }
232 
SeqDescFreeFunc(Pointer data)233 static Pointer LIBCALLBACK SeqDescFreeFunc (Pointer data)
234 {
235     return (Pointer) SeqDescFree ((SeqDescPtr) data);
236 }
237 
SeqDescAsnWriteFunc(Pointer data,AsnIoPtr aip,AsnTypePtr atp)238 static Boolean LIBCALLBACK SeqDescAsnWriteFunc (Pointer data, AsnIoPtr aip, AsnTypePtr atp)
239 {
240     return SeqDescAsnWrite((SeqDescPtr)data, aip, atp);
241 }
242 
SeqDescAsnReadFunc(AsnIoPtr aip,AsnTypePtr atp)243 static Pointer LIBCALLBACK SeqDescAsnReadFunc (AsnIoPtr aip, AsnTypePtr atp)
244 {
245     return (Pointer) SeqDescAsnRead (aip, atp);
246 }
247 
248 
SeqDescLabelContent(ValNodePtr vnp,CharPtr buf,Int2 buflen)249 static Int2 NEAR SeqDescLabelContent (ValNodePtr vnp, CharPtr buf, Int2 buflen)
250 {
251     OrgRefPtr orp=NULL;
252     UserObjectPtr uop;
253     BioSourcePtr bsrcp;
254     PubdescPtr pdp;
255     CharPtr label = NULL;
256     Char tbuf[40];
257     ValNodePtr vnp2;
258     ValNode vn;
259     Int2 diff, len;
260     DbtagPtr dbt;
261     CharPtr prefix = NULL;
262     MolInfoPtr mip;
263     CharPtr mipsptr[4];
264     Int2 i;
265     ObjectIdPtr oip;
266 
267     if (vnp == NULL) return 0;
268 
269     switch (vnp->choice)
270     {
271         case Seq_descr_comment:
272         case Seq_descr_region:
273         case Seq_descr_het:
274         case Seq_descr_title:
275         case Seq_descr_name:
276             label = (CharPtr)(vnp->data.ptrvalue);
277             break;
278 
279         case Seq_descr_create_date:
280         case Seq_descr_update_date:
281             if (vnp->data.ptrvalue != NULL && DatePrint((DatePtr)(vnp->data.ptrvalue), tbuf))
282                 label = tbuf;
283             break;
284 
285         case Seq_descr_org:
286             orp = (OrgRefPtr)(vnp->data.ptrvalue);
287 orgref:        if (orp != NULL) {
288                 if (orp->taxname != NULL)
289                     label = (orp->taxname);
290                 else if (orp->common != NULL)
291                     label = (orp->common);
292             }
293             break;
294 
295         case Seq_descr_pub:
296             pdp = (PubdescPtr)(vnp->data.ptrvalue);
297             if (pdp == NULL) return 0;
298             vn.choice = PUB_Equiv;
299             vn.data.ptrvalue = pdp->pub;
300             vn.next = NULL;
301             return PubLabel(&vn, buf, buflen, OM_LABEL_CONTENT);
302 
303         case Seq_descr_user:
304             uop = (UserObjectPtr)(vnp->data.ptrvalue);
305             if (uop == NULL) return 0;
306             label = (uop->_class);
307             if (label == NULL) {
308                 oip = uop->type;
309                 if (oip != NULL) {
310                     label = oip->str;
311                 }
312             }
313             break;
314 
315         case Seq_descr_method:
316             label =  AsnEnumTypeStr(SEQDESC_method,(Int2)(vnp->data.intvalue));
317             break;
318 
319         case Seq_descr_mol_type:
320             label =  AsnEnumTypeStr(SEQDESC_mol_type,(Int2)(vnp->data.intvalue));
321             break;
322 
323         case Seq_descr_modif:
324             len = buflen;
325             vnp2 = (ValNodePtr)(vnp->data.ptrvalue);
326             diff = 0;
327             while (vnp2 != NULL)
328             {
329                 if (diff)
330                 {
331                     prefix = ",";
332                 }
333                 label = AsnEnumTypeStr(SEQDESC_modif_E, (Int2)(vnp2->data.intvalue));
334                 diff = LabelCopyExtra(buf, label, buflen, prefix, NULL);
335                 buflen -= diff;
336                 buf += diff;
337                 vnp2 = vnp2->next;
338             }
339             return (len - buflen);
340 
341         case Seq_descr_source:
342             bsrcp = (BioSourcePtr)(vnp->data.ptrvalue);
343             if (bsrcp == NULL) return 0;
344             orp = bsrcp->org;
345             goto orgref;
346 
347         case Seq_descr_maploc:
348             dbt = (DbtagPtr)(vnp->data.ptrvalue);
349             if (dbt == NULL) return 0;
350             len = buflen;
351             if (dbt->db != NULL)
352             {
353                 diff = LabelCopyExtra(buf, dbt->db, buflen, NULL, ": ");
354                 buflen -= diff;
355                 buf += diff;
356             }
357             if (dbt->tag != NULL)
358             {
359                 diff = LabelCopy(buf, dbt->tag->str, buflen);
360                 buflen -= diff;
361             }
362             return len - buflen;
363 
364         case Seq_descr_molinfo:
365             mip = (MolInfoPtr)(vnp->data.ptrvalue);
366             if (mip == NULL) return 0;
367             if (mip->biomol)
368                 mipsptr[0] = AsnEnumTypeStr(MOLINFO_biomol, (Int2)(mip->biomol));
369             else
370                 mipsptr[0] = NULL;
371             if (mip->tech)
372                 mipsptr[1] = AsnEnumTypeStr(MOLINFO_tech, (Int2)(mip->tech));
373             else
374                 mipsptr[1] = NULL;
375             mipsptr[2] = mip->techexp;
376             if (mip->completeness)
377                 mipsptr[3] = AsnEnumTypeStr(MOLINFO_completeness, (Int2)(mip->completeness));
378             else
379                 mipsptr[3] = NULL;
380             prefix = NULL;
381             len = buflen;
382             for (i = 0; i < 4; i++)
383             {
384                 if (mipsptr[i] != NULL)
385                 {
386                     diff = LabelCopyExtra(buf, mipsptr[i], buflen, prefix, NULL);
387                     buflen -= diff;
388                     buf += diff;
389                     prefix = ", ";
390                 }
391             }
392             return len - buflen;
393 
394 
395         case Seq_descr_modelev:
396             break;
397 
398         case Seq_descr_dbxref:
399             if (vnp->data.ptrvalue == NULL) return 0;
400             len = buflen;
401             diff = DbtagLabel((DbtagPtr)(vnp->data.ptrvalue), buf, buflen);
402             buflen -= diff;
403             buf += diff;
404             return len - buflen;
405 
406         default:
407             break;
408     }
409 
410     return LabelCopy (buf, label, buflen);
411 }
412 
SeqDescLabelFunc(Pointer data,CharPtr buffer,Int2 buflen,Boolean content)413 static Int2 LIBCALLBACK SeqDescLabelFunc ( Pointer data, CharPtr buffer, Int2 buflen, Boolean content)
414 {
415     return SeqDescLabel ((ValNodePtr)data, buffer, buflen, content);
416 }
417 
SeqDescLabel(ValNodePtr vnp,CharPtr buffer,Int2 buflen,Boolean content)418 NLM_EXTERN Int2 LIBCALL SeqDescLabel (ValNodePtr vnp, CharPtr buffer, Int2 buflen, Boolean content)
419 {
420     CharPtr name;
421     static CharPtr descrs [26] = {
422         "SeqDesc..",
423         "MolType",
424         "Modifier",
425         "Method",
426         "Name",
427         "Title",
428         "Organism",
429         "Comment",
430         "Numbering",
431         "MapLocation",
432         "PIR",
433         "Genbank",
434         "Pub",
435         "Region",
436         "UserObj",
437         "SWISSPROT",
438         "DbXref",
439         "EMBL",
440         "CreateDate",
441         "UpdateDate",
442         "PRF",
443         "PDB",
444         "Heterogen",
445         "BioSrc",
446         "MolInfo",
447         "ModelEv"
448         };
449     Int2 len, diff, rsult = 0;
450 
451     if ((vnp == NULL) || (buflen < 1))
452         return 0;
453 
454     if (vnp->choice <= 25)
455         name = descrs[vnp->choice];
456     else
457         name = descrs[0];
458 
459     len = buflen;
460     switch (content)
461     {
462         case OM_LABEL_BOTH:
463             diff = LabelCopyExtra(buffer, name, buflen, NULL, ": ");
464             buffer += diff;
465             buflen -= diff;
466         case OM_LABEL_CONTENT:
467         case OM_LABEL_SUMMARY:
468             diff = SeqDescLabelContent(vnp, buffer, buflen);
469             buflen -= diff;
470             return (len - buflen);
471         case OM_LABEL_TYPE:
472         default:
473             rsult = LabelCopy(buffer, name, buflen);
474     }
475     return rsult;
476 }
477 
SeqDescSubTypeFunc(Pointer ptr)478 static Uint2 LIBCALLBACK SeqDescSubTypeFunc (Pointer ptr)
479 {
480     if (ptr == NULL)
481         return 0;
482     return (Uint2)((ValNodePtr)ptr)->choice;
483 }
484 
485 /*****************************************************************************
486 *
487 *   AnnotDesc ObjMgr Routines
488 *
489 *****************************************************************************/
490 static CharPtr annotdesctypename = "AnnotDesc";
491 
AnnotDescNewFunc(void)492 static Pointer LIBCALLBACK AnnotDescNewFunc (void)
493 {
494     return (Pointer) AnnotDescrNew(NULL);
495 }
496 
AnnotDescFreeFunc(Pointer data)497 static Pointer LIBCALLBACK AnnotDescFreeFunc (Pointer data)
498 {
499     return (Pointer) AnnotDescFree ((AnnotDescPtr) data);
500 }
501 
AnnotDescAsnWriteFunc(Pointer data,AsnIoPtr aip,AsnTypePtr atp)502 static Boolean LIBCALLBACK AnnotDescAsnWriteFunc (Pointer data, AsnIoPtr aip, AsnTypePtr atp)
503 {
504     return AnnotDescAsnWrite((AnnotDescPtr)data, aip, atp);
505 }
506 
AnnotDescAsnReadFunc(AsnIoPtr aip,AsnTypePtr atp)507 static Pointer LIBCALLBACK AnnotDescAsnReadFunc (AsnIoPtr aip, AsnTypePtr atp)
508 {
509     return (Pointer) AnnotDescAsnRead (aip, atp);
510 }
511 
512 
AnnotDescLabelContent(ValNodePtr vnp,CharPtr buf,Int2 buflen)513 static Int2 NEAR AnnotDescLabelContent (ValNodePtr vnp, CharPtr buf, Int2 buflen)
514 {
515     UserObjectPtr uop;
516     PubdescPtr pdp;
517     CharPtr label = NULL;
518     Char tbuf[40];
519     ValNode vn;
520     ObjectIdPtr oip;
521 
522     if (vnp == NULL) return 0;
523 
524     switch (vnp->choice)
525     {
526         case Annot_descr_name:
527         case Annot_descr_title:
528         case Annot_descr_comment:
529             label = (CharPtr)(vnp->data.ptrvalue);
530             break;
531 
532         case Annot_descr_pub:
533             pdp = (PubdescPtr)(vnp->data.ptrvalue);
534             if (pdp == NULL) return 0;
535             vn.choice = PUB_Equiv;
536             vn.data.ptrvalue = pdp->pub;
537             vn.next = NULL;
538             return PubLabel(&vn, buf, buflen, OM_LABEL_CONTENT);
539 
540         case Annot_descr_user:
541             uop = (UserObjectPtr)(vnp->data.ptrvalue);
542             if (uop == NULL) return 0;
543             label = (uop->_class);
544             if (label == NULL) {
545                 oip = uop->type;
546                 if (oip != NULL) {
547                     label = oip->str;
548                 }
549             }
550             break;
551 
552         case Annot_descr_create_date:
553         case Annot_descr_update_date:
554             if (vnp->data.ptrvalue != NULL && DatePrint((DatePtr)(vnp->data.ptrvalue), tbuf))
555                 label = tbuf;
556             break;
557 
558         case Annot_descr_src:
559         case Annot_descr_align:
560         case Annot_descr_region:
561             label = "???";
562 
563         default:
564             break;
565     }
566 
567     return LabelCopy (buf, label, buflen);
568 }
569 
AnnotDescLabelFunc(Pointer data,CharPtr buffer,Int2 buflen,Boolean content)570 static Int2 LIBCALLBACK AnnotDescLabelFunc ( Pointer data, CharPtr buffer, Int2 buflen, Boolean content)
571 {
572     return AnnotDescLabel ((ValNodePtr)data, buffer, buflen, content);
573 }
574 
AnnotDescLabel(AnnotDescPtr anp,CharPtr buffer,Int2 buflen,Boolean content)575 NLM_EXTERN Int2 LIBCALL AnnotDescLabel (AnnotDescPtr anp, CharPtr buffer, Int2 buflen, Boolean content)
576 {
577     CharPtr name;
578     static CharPtr descrs [25] = {
579         "AnnotDesc..",
580         "Name",
581         "Title",
582         "Comment",
583         "Pub",
584         "UserObj",
585         "CreateDate",
586         "UpdateDate",
587         "Src",
588         "Align",
589         "Region"
590         };
591     Int2 len, diff, rsult = 0;
592 
593     if ((anp == NULL) || (buflen < 1))
594         return 0;
595 
596     if (anp->choice <= 10)
597         name = descrs[anp->choice];
598     else
599         name = descrs[0];
600 
601     len = buflen;
602     switch (content)
603     {
604         case OM_LABEL_BOTH:
605             diff = LabelCopyExtra(buffer, name, buflen, NULL, ": ");
606             buffer += diff;
607             buflen -= diff;
608         case OM_LABEL_CONTENT:
609         case OM_LABEL_SUMMARY:
610             diff = AnnotDescLabelContent(anp, buffer, buflen);
611             buflen -= diff;
612             return (len - buflen);
613         case OM_LABEL_TYPE:
614         default:
615             rsult = LabelCopy(buffer, name, buflen);
616     }
617     return rsult;
618 }
619 
AnnotDescSubTypeFunc(Pointer ptr)620 static Uint2 LIBCALLBACK AnnotDescSubTypeFunc (Pointer ptr)
621 {
622     if (ptr == NULL)
623         return 0;
624     return (Uint2)((ValNodePtr)ptr)->choice;
625 }
626 
627 /*****************************************************************************
628 *
629 *   SeqAsnLoad()
630 *
631 *****************************************************************************/
632 
633 static Boolean loaded = FALSE;
634 
SeqAsnLoad(void)635 NLM_EXTERN Boolean LIBCALL SeqAsnLoad (void)
636 {
637     if (loaded)
638         return TRUE;
639     loaded = TRUE;
640 
641     if (! GeneralAsnLoad())
642     {
643         loaded = FALSE;
644         return FALSE;
645     }
646     if (! SeqLocAsnLoad())
647     {
648         loaded = FALSE;
649         return FALSE;
650     }
651     if (! SeqAlignAsnLoad())
652     {
653         loaded = FALSE;
654         return FALSE;
655     }
656     if (! SeqFeatAsnLoad())
657     {
658         loaded = FALSE;
659         return FALSE;
660     }
661     if (! SeqResAsnLoad())
662     {
663         loaded = FALSE;
664         return FALSE;
665     }
666     if (! PubAsnLoad())
667     {
668         loaded = FALSE;
669         return FALSE;
670     }
671     if (! SeqCodeAsnLoad())
672     {
673         loaded = FALSE;
674         return FALSE;
675     }
676     if (! SeqBlockAsnLoad())
677     {
678         loaded = FALSE;
679         return FALSE;
680     }
681     if (! objgbseqAsnLoad())
682     {
683         loaded = FALSE;
684         return FALSE;
685     }
686     if (! objinsdseqAsnLoad()) {
687         loaded = FALSE;
688         return FALSE;
689     }
690     if (! objtseqAsnLoad())
691     {
692         loaded = FALSE;
693         return FALSE;
694     }
695     if (! AsnLoad())
696     {
697         loaded = FALSE;
698         return FALSE;
699     }
700 
701     ObjMgrTypeLoad(OBJ_BIOSEQ, "Bioseq", bioseqtypename, "Biological Sequence",
702         BIOSEQ, BioseqNewFunc, BioseqAsnReadFunc, BioseqAsnWriteFunc,
703         BioseqFreeFunc, BioseqLabelFunc, BioseqSubTypeFunc);
704 
705     ObjMgrTypeLoad(OBJ_SEQANNOT, "Seq-annot", seqannottypename, "Sequence Annotation",
706         SEQ_ANNOT, SeqAnnotNewFunc, SeqAnnotAsnReadFunc, SeqAnnotAsnWriteFunc,
707         SeqAnnotFreeFunc, SeqAnnotLabelFunc, SeqAnnotSubTypeFunc);
708 
709     ObjMgrTypeLoad(OBJ_SEQDESC, "Seqdesc", seqdesctypename, "Sequence Descriptor",
710         SEQDESC, SeqDescNewFunc, SeqDescAsnReadFunc, SeqDescAsnWriteFunc,
711         SeqDescFreeFunc, SeqDescLabelFunc, SeqDescSubTypeFunc);
712 
713     ObjMgrTypeLoad(OBJ_ANNOTDESC, "Annotdesc", annotdesctypename, "Annot Descriptor",
714         ANNOTDESC, AnnotDescNewFunc, AnnotDescAsnReadFunc, AnnotDescAsnWriteFunc,
715         AnnotDescFreeFunc, AnnotDescLabelFunc, AnnotDescSubTypeFunc);
716 
717     return TRUE;
718 }
719 
720 
721 /*****************************************************************************
722 *
723 *   Bioseq Routines
724 *
725 *****************************************************************************/
726 
727 /*****************************************************************************
728 *
729 *   BioseqNew()
730 *
731 *****************************************************************************/
BioseqNew(void)732 NLM_EXTERN BioseqPtr LIBCALL BioseqNew (void)
733 {
734     BioseqPtr bsp;
735 
736     bsp = (BioseqPtr)MemNew(sizeof(Bioseq));
737     if (bsp == NULL) return bsp;
738 
739     bsp->length = -1;    /* not set */
740     bsp->topology = 1;   /* DEFAULT = linear */
741 
742     if (!SeqMgrAdd (SM_BIOSEQ, (Pointer)bsp)) {   /* add to Bioseq list */
743       bsp = BioseqFree (bsp);
744     }
745 
746     return bsp;
747 }
748 
749 /*****************************************************************************
750 *
751 *   BioseqFree(bsp)
752 *       Frees one Bioseq and associated data
753 *
754 *****************************************************************************/
BioseqFree(BioseqPtr bsp)755 NLM_EXTERN BioseqPtr LIBCALL BioseqFree (BioseqPtr bsp)
756 {
757     Boolean top = FALSE;
758 
759     if (bsp == NULL)
760         return bsp;
761 
762     if (bsp->idx.parentptr == NULL || bsp->idx.parenttype == OBJ_SEQSUB) {
763         if (bsp->seqentry != NULL) {
764             SeqMgrDeleteIndexesInRecord (bsp->seqentry);
765             top = TRUE;
766         }
767     }
768 
769     SeqIdSetFree(bsp->id);
770     BioseqFreeComponents(bsp);
771 
772     if (! SeqMgrDelete(SM_BIOSEQ, (Pointer)bsp))
773         ErrPostEx(SEV_ERROR, 0,0, "BioseqFree: pointer not registered");
774 
775     if (top) {
776         ObjMgrDeleteAllInRecord ();
777     }
778 
779     return (BioseqPtr)MemFree(bsp);
780 }
781 
782 /*****************************************************************************
783 *
784 *   BioseqFreeComponents(bsp)
785 *       Frees data associated with the Bioseq.
786 *       Does not free SeqIds
787 *       Does not free Bioseq itself
788 *       Used by SeqMgr for caching out
789 *       Called by BioseqFree()
790 *
791 *****************************************************************************/
BioseqFreeComponents(BioseqPtr bsp)792 NLM_EXTERN BioseqPtr LIBCALL BioseqFreeComponents (BioseqPtr bsp)
793 {
794     SeqAnnotPtr sp, spnext;
795     SeqFeatPtr sfp, sfpnext;
796     DeltaSeqPtr dsp, dspnext;
797 
798     if (bsp == NULL)
799         return bsp;
800 
801     bsp->descr = SeqDescrFree(bsp->descr);
802     bsp->seq_data = SeqDataFree(bsp->seq_data, bsp->seq_data_type);
803     bsp->fuzz = IntFuzzFree(bsp->fuzz);
804     switch (bsp->seq_ext_type)
805     {
806         case 1:    /* seg-ext */
807             SeqLocSetFree((ValNodePtr)bsp->seq_ext);
808             break;
809         case 2:   /* reference */
810             SeqLocFree((ValNodePtr)bsp->seq_ext);
811             break;
812         case 3:   /* map */
813             sfp = (SeqFeatPtr)bsp->seq_ext;
814             while (sfp != NULL)
815             {
816                 sfpnext = sfp->next;
817                 SeqFeatFree(sfp);
818                 sfp = sfpnext;
819             }
820             break;
821         case 4:  /* delta */
822             dsp = (DeltaSeqPtr)(bsp->seq_ext);
823             while (dsp != NULL)
824             {
825                 dspnext = dsp->next;
826                 DeltaSeqFree(dsp);
827                 dsp = dspnext;
828         }
829             break;
830 
831     }
832     bsp->seq_ext = NULL;
833     sp = bsp->annot;
834     while (sp != NULL)
835     {
836         spnext = sp->next;
837         SeqAnnotFree(sp);
838         sp = spnext;
839     }
840     bsp->annot = NULL;
841     bsp->hist = SeqHistFree(bsp->hist);
842 
843     return bsp;
844 }
845 
846 /*****************************************************************************
847 *
848 *   BioseqAsnWrite(bsp, aip, atp)
849 *       atp is the current type (if identifier of a parent struct)
850 *       if atp == NULL, then assumes it stands alone (Bioseq ::=)
851 *
852 *****************************************************************************/
BioseqAsnWrite(BioseqPtr bsp,AsnIoPtr aip,AsnTypePtr orig)853 NLM_EXTERN Boolean LIBCALL BioseqAsnWrite (BioseqPtr bsp, AsnIoPtr aip, AsnTypePtr orig)
854 {
855     AsnTypePtr atp;
856     ValNodePtr anp;
857     Boolean retval = FALSE;
858 
859     if (! loaded)
860     {
861         if (! SeqAsnLoad())
862             return FALSE;
863     }
864 
865     if (aip == NULL)
866         return FALSE;
867 
868     if (! ProgMon("Write Bioseq"))
869         return FALSE;
870 
871     atp = AsnLinkType(orig, BIOSEQ);   /* link local tree */
872     if (atp == NULL) goto erret;
873 
874     if (bsp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
875 
876     if (! AsnOpenStruct(aip, atp, (Pointer)bsp)) goto erret;
877 
878     if (! AsnOpenStruct(aip, BIOSEQ_id, (Pointer)bsp->id)) goto erret;    /* ids required */
879     anp = bsp->id;
880     while (anp != NULL)
881     {
882         if (! SeqIdAsnWrite(anp, aip, BIOSEQ_id_E)) goto erret;
883         anp = anp->next;
884     }
885     if (! AsnCloseStruct(aip, BIOSEQ_id, (Pointer)bsp->id)) goto erret;
886 
887     if (bsp->descr != NULL)              /* Seq-descr optional */
888     {
889         if (! SeqDescrAsnWrite(bsp->descr, aip, BIOSEQ_descr)) goto erret;
890     }
891     else
892     {
893         if (! SeqDescrExtraCheck(aip, BIOSEQ_descr)) goto erret;
894     }
895 
896     if (! BioseqInstAsnWrite(bsp, aip, BIOSEQ_inst)) goto erret;
897 
898     if (bsp->annot != NULL)              /* annotation optional */
899     {
900         if (! SeqAnnotSetAsnWrite(bsp->annot, aip, BIOSEQ_annot, BIOSEQ_annot_E))
901             goto erret;
902     }
903     else   /* look for extra features from SeqEntryAsnOut() */
904     {
905         if (! SeqAnnotSetExtraCheck(aip, BIOSEQ_annot, BIOSEQ_annot_E)) goto erret;
906     }
907 
908     if (! AsnCloseStruct(aip, atp, (Pointer)bsp)) goto erret;
909     retval = TRUE;
910 erret:
911     AsnUnlinkType(orig);       /* unlink local tree */
912     return retval;
913 }
914 
915 /*****************************************************************************
916 *
917 *   BioseqAsnRead(aip, atp)
918 *       atp is the current type (if identifier of a parent struct)
919 *            assumption is readIdent has occurred
920 *       if atp == NULL, then assumes it stands alone and read ident
921 *            has not occurred.
922 *
923 *****************************************************************************/
BioseqAsnRead(AsnIoPtr aip,AsnTypePtr orig)924 NLM_EXTERN BioseqPtr LIBCALL BioseqAsnRead (AsnIoPtr aip, AsnTypePtr orig)
925 {
926     DataVal av;
927     AsnTypePtr atp;
928     BioseqPtr bsp=NULL;
929     AsnOptionPtr aop;
930     Op_objseqPtr osp;
931     Boolean check_seqid = FALSE;
932     Int2 level;
933 
934     if (! loaded)
935     {
936         if (! SeqAsnLoad())
937             return bsp;
938     }
939 
940     if (aip == NULL)
941         return bsp;
942 
943     if (! ProgMon("Read Bioseq"))
944         return bsp;
945 
946     if ((aop = AsnIoOptionGet(aip, OP_NCBIOBJSEQ, BIOSEQ_CHECK_ID, NULL)) != NULL)
947     {
948         osp = (Op_objseqPtr)aop->data.ptrvalue;
949         if ((osp->found_it) && (osp->load_by_id))   /* already got it */
950         {
951             AsnSkipValue(aip, orig);
952             return bsp;
953         }
954         check_seqid = TRUE;
955     }
956 
957     if (orig == NULL)           /* Bioseq ::= (self contained) */
958         atp = AsnReadId(aip, amp, BIOSEQ);
959     else
960         atp = AsnLinkType(orig, BIOSEQ);    /* link in local tree */
961     if (atp == NULL) return bsp;
962 
963     bsp = BioseqNew();
964     if (bsp == NULL) goto erret;
965 
966     level = AsnGetLevel(aip);     /* for skipping */
967 
968     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the start struct */
969 
970     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* id required, start struct */
971     bsp->id = SeqIdSetAsnRead(aip, atp, BIOSEQ_id_E);
972     if (bsp->id == NULL) goto erret;
973     if (check_seqid)
974     {
975         if (SeqIdIn(osp->sip, bsp->id))
976             osp->found_it = TRUE;
977         else if (osp->load_by_id)   /* don't load it */
978         {
979             while (AsnGetLevel(aip) > level)  /* skip everything else */
980             {
981                 atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
982                 if (AsnReadVal(aip, atp, NULL) <= 0) goto erret;
983             }
984             goto erret;      /* didn't find it -- not really an error */
985         }
986     }
987 
988     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
989     if (atp == BIOSEQ_descr)           /* descr optional */
990     {
991         bsp->descr = SeqDescrAsnRead(aip, atp);
992         if (bsp->descr == NULL) goto erret;
993         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
994     }
995 
996     if (! BioseqInstAsnRead(bsp, aip, atp)) goto erret;
997 
998     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
999 
1000     if (atp == BIOSEQ_annot)
1001     {
1002         bsp->annot = SeqAnnotSetAsnRead(aip, atp, BIOSEQ_annot_E);
1003 // 15dec2011: do not treat empty set as error
1004 //        if (bsp->annot == NULL) goto erret;
1005         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1006     }
1007 
1008     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end Bioseq */
1009 ret:
1010     AsnUnlinkType(orig);       /* unlink local tree */
1011     return bsp;
1012 erret:
1013     aip->io_failure = TRUE;
1014     bsp = BioseqFree(bsp);
1015     goto ret;
1016 }
1017 
1018 /**********************************************************/
SeqDataAsnWriteXML(SeqDataPtr sdp,Uint1 seqtype,AsnIoPtr aip,AsnTypePtr orig,Int4 seqlen)1019 Boolean LIBCALL SeqDataAsnWriteXML(SeqDataPtr sdp, Uint1 seqtype, AsnIoPtr aip, AsnTypePtr orig, Int4 seqlen)
1020 {
1021     AsnTypePtr atp;
1022     AsnTypePtr tmp;
1023     DataVal    av;
1024     Boolean    tofree;
1025     Boolean    retval = FALSE;
1026     Uint1      newseqtype;
1027     ByteStorePtr  bsp;
1028     AsnWriteFunc func = NULL;
1029 
1030     if((!loaded && !SeqAsnLoad()) || aip == NULL)
1031         return(FALSE);
1032 
1033     atp = AsnLinkType(orig, SEQ_DATA);  /* link local tree */
1034     if(atp == NULL)
1035         return(FALSE);
1036 
1037     if(sdp == NULL)
1038     {
1039         AsnNullValueMsg(aip, atp);
1040         AsnUnlinkType(orig);            /* unlink local tree */
1041         return(FALSE);
1042     }
1043 
1044     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
1045 
1046     /* if (seqtype == Seq_code_gap) return FALSE; */
1047     bsp = (ByteStorePtr) sdp;
1048 
1049     tofree = FALSE;
1050     if(aip->type & ASNIO_XML)
1051     {
1052         if(seqtype == Seq_code_ncbi2na || seqtype == Seq_code_ncbi4na ||
1053            seqtype == Seq_code_ncbi8na || seqtype == Seq_code_ncbipna)
1054             newseqtype = Seq_code_iupacna;
1055         else if(seqtype == Seq_code_ncbi8aa || seqtype == Seq_code_ncbieaa ||
1056                 seqtype == Seq_code_ncbipaa || seqtype == Seq_code_ncbistdaa)
1057             newseqtype = Seq_code_iupacaa;
1058         else
1059             newseqtype = seqtype;
1060 
1061         if(newseqtype != seqtype)
1062         {
1063             bsp = BSConvertSeq(BSDup(bsp), newseqtype, seqtype, seqlen);
1064             seqtype = newseqtype;
1065             tofree = TRUE;
1066         }
1067     }
1068 
1069     av.ptrvalue = bsp;
1070     if(!AsnWriteChoice(aip, atp, (Int2) seqtype, &av))  /* CHOICE */
1071     {
1072         AsnUnlinkType(orig);            /* unlink local tree */
1073         return(FALSE);
1074     }
1075 
1076     if(seqtype == Seq_code_iupacna)
1077         tmp = SEQ_DATA_iupacna;
1078     else if(seqtype == Seq_code_iupacaa)
1079         tmp = SEQ_DATA_iupacaa;
1080     else if(seqtype == Seq_code_ncbi2na)
1081         tmp = SEQ_DATA_ncbi2na;
1082     else if(seqtype == Seq_code_ncbi4na)
1083         tmp = SEQ_DATA_ncbi4na;
1084     else if(seqtype == Seq_code_ncbi8na)
1085         tmp = SEQ_DATA_ncbi8na;
1086     else if(seqtype == Seq_code_ncbipna)
1087         tmp = SEQ_DATA_ncbipna;
1088     else if(seqtype == Seq_code_ncbi8aa)
1089         tmp = SEQ_DATA_ncbi8aa;
1090     else if(seqtype == Seq_code_ncbieaa)
1091         tmp = SEQ_DATA_ncbieaa;
1092     else if(seqtype == Seq_code_ncbipaa)
1093         tmp = SEQ_DATA_ncbipaa;
1094     else if(seqtype == Seq_code_ncbistdaa)
1095         tmp = SEQ_DATA_ncbistdaa;
1096     else if(seqtype == Seq_code_gap) {
1097         tmp = SEQ_DATA_gap;
1098         func = (AsnWriteFunc) SeqGapAsnWrite;
1099     } else
1100         tmp = NULL;
1101 
1102     if (func != NULL) {
1103         retval = (* func) ((SeqGapPtr) sdp, aip, tmp);
1104     } else {
1105         retval = AsnWrite(aip, tmp, &av);
1106     }
1107 
1108     AsnUnlinkType(orig);            /* unlink local tree */
1109     if(tofree != FALSE)
1110         BSFree(bsp);
1111 
1112     return(retval);
1113 }
1114 
1115 /*****************************************************************************
1116 *
1117 *   BioseqInstAsnWrite(bsp, aip, orig)
1118 *
1119 *****************************************************************************/
BioseqInstAsnWrite(BioseqPtr bsp,AsnIoPtr aip,AsnTypePtr orig)1120 NLM_EXTERN Boolean LIBCALL BioseqInstAsnWrite (BioseqPtr bsp, AsnIoPtr aip, AsnTypePtr orig)
1121 {
1122     DataVal av;
1123     AsnTypePtr atp;
1124     Boolean retval = FALSE;
1125 /*    Uint1 newcode;*/
1126 
1127     if (! loaded)
1128     {
1129         if (! SeqAsnLoad())
1130             return FALSE;
1131     }
1132 
1133     if (aip == NULL)
1134         return FALSE;
1135 
1136     if (! ProgMon("Write SeqInst"))
1137         return FALSE;
1138 
1139     atp = AsnLinkType(orig, SEQ_INST);   /* link local tree */
1140     if (atp == NULL) return FALSE;
1141 
1142     if (bsp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1143 
1144     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
1145 
1146     if (! AsnOpenStruct(aip, atp, (Pointer)bsp)) goto erret; /* instance required */
1147     av.intvalue = bsp->repr;
1148     if (! AsnWrite(aip, SEQ_INST_repr, &av)) goto erret;
1149     av.intvalue = bsp->mol;
1150     if (! AsnWrite(aip, SEQ_INST_mol, &av)) goto erret;
1151     if (bsp->length >= 0)
1152     {
1153         av.intvalue = bsp->length;
1154         if (! AsnWrite(aip, SEQ_INST_length, &av)) goto erret;
1155     }
1156     if (bsp->fuzz != NULL)
1157     {
1158         if (! IntFuzzAsnWrite(bsp->fuzz, aip, SEQ_INST_fuzz)) goto erret;
1159     }
1160     if (bsp->topology != 1)
1161     {
1162         av.intvalue = bsp->topology;
1163         if (! AsnWrite(aip, SEQ_INST_topology, &av)) goto erret;
1164     }
1165     if (bsp->strand)
1166     {
1167         av.intvalue = bsp->strand;
1168         if (! AsnWrite(aip, SEQ_INST_strand, &av)) goto erret;
1169     }
1170 
1171                   /** for XML, make it text ****/
1172 /*    if (aip->type & ASNIO_XML)
1173     {
1174     if (ISA_aa(bsp->mol))
1175         newcode = Seq_code_ncbieaa;
1176     else
1177         newcode = Seq_code_iupacna;
1178     BioseqConvert(bsp, newcode);
1179     }*/
1180 
1181     if (bsp->seq_data != NULL)
1182     {
1183         if (aip->type & ASNIO_XML)
1184         {
1185             if (! SeqDataAsnWriteXML(bsp->seq_data, bsp->seq_data_type, aip, SEQ_INST_seq_data, bsp->length))
1186                 goto erret;
1187         }
1188         else if (! SeqDataAsnWrite(bsp->seq_data, bsp->seq_data_type, aip, SEQ_INST_seq_data))
1189             goto erret;
1190     }
1191     if (bsp->seq_ext != NULL)
1192     {
1193         av.ptrvalue = (Pointer)bsp->seq_ext;
1194         if (! AsnWriteChoice(aip, SEQ_INST_ext, (Int2)bsp->seq_ext_type, &av)) goto erret;
1195         switch (bsp->seq_ext_type)
1196         {
1197             case 1:        /* seg */
1198                 if (! SeqLocSetAsnWrite((ValNodePtr)bsp->seq_ext, aip, SEQ_EXT_seg, SEG_EXT_E))
1199                     goto erret;
1200                 break;
1201             case 2:       /* ref */
1202                 if (! SeqLocAsnWrite((ValNodePtr)bsp->seq_ext, aip, SEQ_EXT_ref)) goto erret;
1203                 break;
1204             case 3:              /* map ext */
1205                 if (! SeqFeatSetAsnWrite((SeqFeatPtr)bsp->seq_ext, aip, SEQ_EXT_map, MAP_EXT_E)) goto erret;
1206                 break;
1207             case 4:        /* delta */
1208                 if (! DeltaSeqSetAsnWrite((DeltaSeqPtr)bsp->seq_ext, aip, SEQ_EXT_delta, DELTA_EXT_E))
1209                     goto erret;
1210                 break;
1211         }
1212     }
1213 
1214     if (bsp->hist != NULL)              /* history optional */
1215     {
1216         if (! SeqHistAsnWrite(bsp->hist, aip, SEQ_INST_hist))
1217             goto erret;
1218     }
1219 
1220     if (! AsnCloseStruct(aip, atp, (Pointer)bsp)) goto erret;
1221     retval = TRUE;
1222 erret:
1223     AsnUnlinkType(orig);       /* unlink local tree */
1224     return retval;
1225 }
1226 
1227 /*****************************************************************************
1228 *
1229 *   BioseqInstAsnRead(bsp, aip, atp)
1230 *
1231 *****************************************************************************/
BioseqInstAsnRead(BioseqPtr bsp,AsnIoPtr aip,AsnTypePtr orig)1232 NLM_EXTERN Boolean LIBCALL BioseqInstAsnRead (BioseqPtr bsp, AsnIoPtr aip, AsnTypePtr orig)
1233 {
1234     DataVal av;
1235     AsnTypePtr atp;
1236     Uint1 choice;
1237     Boolean retval = FALSE;
1238 
1239     if (! loaded)
1240     {
1241         if (! SeqAsnLoad())
1242             return FALSE;
1243     }
1244 
1245     if ((aip == NULL) || (bsp == NULL))
1246         return FALSE;
1247 
1248     if (! ProgMon("Read SeqInst"))
1249         return FALSE;
1250 
1251     if (orig == NULL)           /* Seq-inst ::= (self contained) */
1252         atp = AsnReadId(aip, amp, SEQ_INST);
1253     else
1254         atp = AsnLinkType(orig, SEQ_INST);    /* link in local tree */
1255     if (atp == NULL) goto erret;
1256 
1257     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;         /* inst required */
1258 
1259     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1260     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1261     bsp->repr = (Uint1) av.intvalue;
1262 
1263     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1264     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1265     bsp->mol = (Uint1) av.intvalue;
1266 
1267     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1268 
1269     if (atp == SEQ_INST_length)
1270     {
1271         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1272         bsp->length = av.intvalue;
1273         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1274     }
1275 
1276     if (atp == SEQ_INST_fuzz)
1277     {
1278         bsp->fuzz = IntFuzzAsnRead(aip, atp);
1279         if (bsp->fuzz == NULL) goto erret;
1280         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1281     }
1282 
1283     if (atp == SEQ_INST_topology)
1284     {
1285         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1286         bsp->topology = (Uint1) av.intvalue;
1287         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1288     }
1289 
1290     if (atp == SEQ_INST_strand)
1291     {
1292         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1293         bsp->strand = (Uint1) av.intvalue;
1294         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1295     }
1296 
1297     if (atp == SEQ_INST_seq_data)
1298     {
1299         bsp->seq_data = SeqDataAsnRead(aip, atp, &bsp->seq_data_type, bsp->length);
1300         if (bsp->seq_data == NULL) goto erret;
1301 
1302         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1303     }
1304 
1305     if (atp == SEQ_INST_ext)            /* extensions */
1306     {
1307         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* CHOICE */
1308         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* what choice? */
1309         choice = 0;
1310         if (atp == SEQ_EXT_seg)
1311             choice = 1;
1312         else if (atp == SEQ_EXT_ref)
1313             choice = 2;
1314         else if (atp == SEQ_EXT_map)
1315             choice = 3;
1316         else if (atp == SEQ_EXT_delta)
1317             choice = 4;
1318         bsp->seq_ext_type = choice;
1319         switch (choice)
1320         {
1321             case 1:
1322                 bsp->seq_ext = (Pointer) SeqLocSetAsnRead(aip, atp, SEG_EXT_E);
1323                 break;
1324             case 2:
1325                 bsp->seq_ext = (Pointer) SeqLocAsnRead(aip, atp);
1326                 break;
1327             case 3:
1328                 bsp->seq_ext = (Pointer) SeqFeatSetAsnRead(aip, atp, MAP_EXT_E);
1329                 break;
1330             case 4:
1331                 bsp->seq_ext = (Pointer) DeltaSeqSetAsnRead(aip, atp, DELTA_EXT_E);
1332                 break;
1333         }
1334         if (bsp->seq_ext == NULL) goto erret;
1335         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1336     }
1337 
1338     if (atp == SEQ_INST_hist)
1339     {
1340         bsp->hist = SeqHistAsnRead(aip, atp);
1341         if (bsp->hist == NULL) goto erret;
1342         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
1343     }
1344 
1345     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct inst */
1346     retval = TRUE;
1347 erret:
1348     AsnUnlinkType(orig);       /* unlink local tree */
1349     return retval;
1350 }
1351 
1352 /*****************************************************************************
1353 *
1354 *   SeqDataFree(bsp, seqtype)
1355 *
1356 *****************************************************************************/
SeqDataFree(SeqDataPtr sdp,Uint1 seqtype)1357 NLM_EXTERN SeqDataPtr LIBCALL SeqDataFree (SeqDataPtr sdp, Uint1 seqtype)
1358 
1359 {
1360     switch (seqtype)
1361     {
1362         case Seq_code_gap:
1363             SeqGapFree ((SeqGapPtr) sdp);
1364             break;
1365         default:
1366             BSFree ((ByteStorePtr) sdp);
1367             break;
1368     }
1369     return NULL;
1370 }
1371 
1372 /*****************************************************************************
1373 *
1374 *   SeqDataAsnWrite(bsp, seqtype, aip, orig)
1375 *
1376 *****************************************************************************/
SeqDataAsnWrite(SeqDataPtr sdp,Uint1 seqtype,AsnIoPtr aip,AsnTypePtr orig)1377 NLM_EXTERN Boolean LIBCALL SeqDataAsnWrite (SeqDataPtr sdp, Uint1 seqtype, AsnIoPtr aip, AsnTypePtr orig)
1378 {
1379     DataVal av;
1380     AsnTypePtr atp, tmp;
1381     Boolean retval = FALSE;
1382     AsnWriteFunc func = NULL;
1383 
1384     if (! loaded)
1385     {
1386         if (! SeqAsnLoad())
1387             return FALSE;
1388     }
1389 
1390     if (aip == NULL)
1391         return FALSE;
1392 
1393     atp = AsnLinkType(orig, SEQ_DATA);   /* link local tree */
1394     if (atp == NULL) return FALSE;
1395 
1396     if (sdp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1397 
1398     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
1399 
1400     av.ptrvalue = sdp;
1401     if (! AsnWriteChoice(aip, atp, (Int2)seqtype, &av)) goto erret;   /* CHOICE */
1402     tmp = NULL;
1403     switch (seqtype)
1404     {
1405         case Seq_code_iupacna:
1406             tmp = SEQ_DATA_iupacna;
1407             break;
1408         case Seq_code_iupacaa:
1409             tmp = SEQ_DATA_iupacaa;
1410             break;
1411         case Seq_code_ncbi2na:
1412             tmp = SEQ_DATA_ncbi2na;
1413             break;
1414         case Seq_code_ncbi4na:
1415             tmp = SEQ_DATA_ncbi4na;
1416             break;
1417         case Seq_code_ncbi8na:
1418             tmp = SEQ_DATA_ncbi8na;
1419             break;
1420         case Seq_code_ncbipna:
1421             tmp = SEQ_DATA_ncbipna;
1422             break;
1423         case Seq_code_ncbi8aa:
1424             tmp = SEQ_DATA_ncbi8aa;
1425             break;
1426         case Seq_code_ncbieaa:
1427             tmp = SEQ_DATA_ncbieaa;
1428             break;
1429         case Seq_code_ncbipaa:
1430             tmp = SEQ_DATA_ncbipaa;
1431             break;
1432         case Seq_code_ncbistdaa:
1433             tmp = SEQ_DATA_ncbistdaa;
1434             break;
1435         case Seq_code_gap:
1436             tmp = SEQ_DATA_gap;
1437             func = (AsnWriteFunc) SeqGapAsnWrite;
1438             break;
1439     }
1440     if (func != NULL) {
1441       if (! (* func)((SeqGapPtr) sdp, aip, tmp))  goto erret;
1442     } else {
1443       if (! AsnWrite(aip, tmp, &av)) goto erret;
1444     }
1445 
1446     retval = TRUE;
1447 erret:
1448     AsnUnlinkType(orig);       /* unlink local tree */
1449     return retval;
1450 }
1451 
1452 /*****************************************************************************
1453 *
1454 *   SeqDataAsnRead(aip, atp, typeptr, length)
1455 *
1456 *****************************************************************************/
SeqDataAsnRead(AsnIoPtr aip,AsnTypePtr orig,Uint1Ptr typeptr,Int4 length)1457 NLM_EXTERN SeqDataPtr LIBCALL SeqDataAsnRead (AsnIoPtr aip, AsnTypePtr orig, Uint1Ptr typeptr, Int4 length)
1458 {
1459     DataVal av;
1460     AsnTypePtr atp;
1461     Uint1 choice;
1462     ByteStorePtr retval = NULL;
1463     ByteStorePtr tmpbs;
1464     Int2 residue;
1465     Int4 ctr1, ctr2;
1466     Char buf[100];
1467     Boolean is_one_let = FALSE;  /* assume not a 1 letter upper case code */
1468     AsnReadFunc func = NULL;
1469 
1470     if (! loaded)
1471     {
1472         if (! SeqAsnLoad())
1473             return FALSE;
1474     }
1475 
1476     if ((aip == NULL) || (typeptr == NULL))
1477         return FALSE;
1478 
1479     if (orig == NULL)           /* Seq-inst ::= (self contained) */
1480         atp = AsnReadId(aip, amp, SEQ_DATA);
1481     else
1482         atp = AsnLinkType(orig, SEQ_DATA);    /* link in local tree */
1483     if (atp == NULL) goto erret;
1484 
1485         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* CHOICE */
1486         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* what kind of choice? */
1487         choice = 0;
1488         if (atp == SEQ_DATA_iupacna)
1489         {
1490             is_one_let = TRUE;
1491             choice = Seq_code_iupacna;
1492         }
1493         else if (atp == SEQ_DATA_iupacaa)
1494         {
1495             is_one_let = TRUE;
1496             choice = Seq_code_iupacaa;
1497         }
1498         else if (atp == SEQ_DATA_ncbi2na)
1499             choice = Seq_code_ncbi2na;
1500         else if (atp == SEQ_DATA_ncbi4na)
1501             choice = Seq_code_ncbi4na;
1502         else if (atp == SEQ_DATA_ncbi8na)
1503             choice = Seq_code_ncbi8na;
1504         else if (atp == SEQ_DATA_ncbipna)
1505             choice = Seq_code_ncbipna;
1506         else if (atp == SEQ_DATA_ncbi8aa)
1507             choice = Seq_code_ncbi8aa;
1508         else if (atp == SEQ_DATA_ncbieaa)
1509         {
1510             is_one_let = TRUE;
1511             choice = Seq_code_ncbieaa;
1512         }
1513         else if (atp == SEQ_DATA_ncbipaa)
1514             choice = Seq_code_ncbipaa;
1515         else if (atp == SEQ_DATA_ncbistdaa)
1516             choice = Seq_code_ncbistdaa;
1517         else if (atp == SEQ_DATA_gap) {
1518             choice = Seq_code_gap;
1519             func = (AsnReadFunc) SeqGapAsnRead;
1520         }
1521         if (func != NULL) {
1522             retval = (* func)(aip, atp);
1523         } else {
1524           if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
1525           retval = (ByteStorePtr) av.ptrvalue;
1526           ctr1 = BSLen(retval);
1527           if ((is_one_let) && (ctr1 > length) &&
1528               (aip->type == ASNIO_TEXT_IN))  /* could have spaces */
1529           {
1530             tmpbs = retval;
1531             retval = BSNew(length);
1532             BSSeek(tmpbs, 0, SEEK_SET);
1533             ctr2 = 0;
1534             while (ctr1)
1535             {
1536                 residue = BSGetByte(tmpbs);
1537                 ctr1--;
1538                 if (IS_UPPER(residue))
1539                 {
1540                     buf[ctr2] = (Char)residue;
1541                     ctr2++;
1542                     if (ctr2 == 100)
1543                     {
1544                         BSWrite(retval, buf, ctr2);
1545                         ctr2 = 0;
1546                     }
1547                 }
1548             }
1549             if (ctr2)
1550                 BSWrite(retval, buf, ctr2);
1551             BSFree(tmpbs);
1552           }
1553         }
1554         *typeptr = choice;
1555 
1556 ret:
1557     AsnUnlinkType(orig);       /* unlink local tree */
1558     return (SeqDataPtr) retval;
1559 erret:
1560     retval = BSFree(retval);
1561     goto ret;
1562 }
1563 
1564 
1565 /**************************************************
1566 *
1567 *    LinkageEvidenceNew()
1568 *
1569 **************************************************/
1570 NLM_EXTERN
1571 LinkageEvidencePtr LIBCALL
LinkageEvidenceNew(void)1572 LinkageEvidenceNew(void)
1573 {
1574    LinkageEvidencePtr ptr = MemNew((size_t) sizeof(LinkageEvidence));
1575 
1576    return ptr;
1577 
1578 }
1579 
1580 
1581 /**************************************************
1582 *
1583 *    LinkageEvidenceFree()
1584 *
1585 **************************************************/
1586 NLM_EXTERN
1587 LinkageEvidencePtr LIBCALL
LinkageEvidenceFree(LinkageEvidencePtr ptr)1588 LinkageEvidenceFree(LinkageEvidencePtr ptr)
1589 {
1590 
1591    if(ptr == NULL) {
1592       return NULL;
1593    }
1594    return MemFree(ptr);
1595 }
1596 
1597 
1598 /**************************************************
1599 *
1600 *    LinkageEvidenceAsnRead()
1601 *
1602 **************************************************/
1603 NLM_EXTERN
1604 LinkageEvidencePtr LIBCALL
LinkageEvidenceAsnRead(AsnIoPtr aip,AsnTypePtr orig)1605 LinkageEvidenceAsnRead(AsnIoPtr aip, AsnTypePtr orig)
1606 {
1607    DataVal av;
1608    AsnTypePtr atp;
1609    AsnReadFunc func;
1610    LinkageEvidencePtr ptr;
1611 
1612    if (! loaded)
1613    {
1614       if (! SeqAsnLoad()) {
1615          return NULL;
1616       }
1617    }
1618 
1619    if (aip == NULL) {
1620       return NULL;
1621    }
1622 
1623    if (orig == NULL) {         /* LinkageEvidence ::= (self contained) */
1624       atp = AsnReadId(aip, amp, LINKAGE_EVIDENCE);
1625    } else {
1626       atp = AsnLinkType(orig, LINKAGE_EVIDENCE);
1627    }
1628    /* link in local tree */
1629    if (atp == NULL) {
1630       return NULL;
1631    }
1632 
1633    ptr = LinkageEvidenceNew();
1634    if (ptr == NULL) {
1635       goto erret;
1636    }
1637    if (AsnReadVal(aip, atp, &av) <= 0) { /* read the start struct */
1638       goto erret;
1639    }
1640 
1641    atp = AsnReadId(aip,amp, atp);
1642    func = NULL;
1643 
1644    if (atp == LINKAGE_EVIDENCE_type) {
1645       if ( AsnReadVal(aip, atp, &av) <= 0) {
1646          goto erret;
1647       }
1648       ptr -> type = av.intvalue;
1649       atp = AsnReadId(aip,amp, atp);
1650    }
1651 
1652    if (AsnReadVal(aip, atp, &av) <= 0) {
1653       goto erret;
1654    }
1655    /* end struct */
1656 
1657 ret:
1658    AsnUnlinkType(orig);       /* unlink local tree */
1659    return ptr;
1660 
1661 erret:
1662    aip -> io_failure = TRUE;
1663    ptr = LinkageEvidenceFree(ptr);
1664    goto ret;
1665 }
1666 
1667 
1668 
1669 /**************************************************
1670 *
1671 *    LinkageEvidenceAsnWrite()
1672 *
1673 **************************************************/
1674 NLM_EXTERN Boolean LIBCALL
LinkageEvidenceAsnWrite(LinkageEvidencePtr ptr,AsnIoPtr aip,AsnTypePtr orig)1675 LinkageEvidenceAsnWrite(LinkageEvidencePtr ptr, AsnIoPtr aip, AsnTypePtr orig)
1676 {
1677    DataVal av;
1678    AsnTypePtr atp;
1679    Boolean retval = FALSE;
1680 
1681    if (! loaded)
1682    {
1683       if (! SeqAsnLoad()) {
1684          return FALSE;
1685       }
1686    }
1687 
1688    if (aip == NULL) {
1689       return FALSE;
1690    }
1691 
1692    atp = AsnLinkType(orig, LINKAGE_EVIDENCE);   /* link local tree */
1693    if (atp == NULL) {
1694       return FALSE;
1695    }
1696 
1697    if (ptr == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1698 
1699     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
1700 
1701    if (! AsnOpenStruct(aip, atp, (Pointer) ptr)) {
1702       goto erret;
1703    }
1704 
1705    av.intvalue = ptr -> type;
1706    retval = AsnWrite(aip, LINKAGE_EVIDENCE_type,  &av);
1707    if (! AsnCloseStruct(aip, atp, (Pointer)ptr)) {
1708       goto erret;
1709    }
1710    retval = TRUE;
1711 
1712 erret:
1713    AsnUnlinkType(orig);       /* unlink local tree */
1714    return retval;
1715 }
1716 
1717 
1718 /**************************************************
1719 *
1720 *    SeqGapNew()
1721 *
1722 **************************************************/
1723 NLM_EXTERN
1724 SeqGapPtr LIBCALL
SeqGapNew(void)1725 SeqGapNew(void)
1726 {
1727    SeqGapPtr ptr = MemNew((size_t) sizeof(SeqGap));
1728 
1729    return ptr;
1730 
1731 }
1732 
1733 
1734 /**************************************************
1735 *
1736 *    SeqGapFree()
1737 *
1738 **************************************************/
1739 NLM_EXTERN
1740 SeqGapPtr LIBCALL
SeqGapFree(SeqGapPtr ptr)1741 SeqGapFree(SeqGapPtr ptr)
1742 {
1743 
1744    if(ptr == NULL) {
1745       return NULL;
1746    }
1747    AsnGenericValNodeSetFree(ptr -> linkage_evidence, (AsnOptFreeFunc) LinkageEvidenceFree);
1748    return MemFree(ptr);
1749 }
1750 
1751 
1752 /**************************************************
1753 *
1754 *    SeqGapAsnRead()
1755 *
1756 **************************************************/
1757 NLM_EXTERN
1758 SeqGapPtr LIBCALL
SeqGapAsnRead(AsnIoPtr aip,AsnTypePtr orig)1759 SeqGapAsnRead(AsnIoPtr aip, AsnTypePtr orig)
1760 {
1761    DataVal av;
1762    AsnTypePtr atp;
1763    Boolean isError = FALSE;
1764    AsnReadFunc func;
1765    SeqGapPtr ptr;
1766 
1767    if (! loaded)
1768    {
1769       if (! SeqAsnLoad()) {
1770          return NULL;
1771       }
1772    }
1773 
1774    if (aip == NULL) {
1775       return NULL;
1776    }
1777 
1778    if (orig == NULL) {         /* SeqGap ::= (self contained) */
1779       atp = AsnReadId(aip, amp, SEQ_GAP);
1780    } else {
1781       atp = AsnLinkType(orig, SEQ_GAP);
1782    }
1783    /* link in local tree */
1784    if (atp == NULL) {
1785       return NULL;
1786    }
1787 
1788    ptr = SeqGapNew();
1789    if (ptr == NULL) {
1790       goto erret;
1791    }
1792    if (AsnReadVal(aip, atp, &av) <= 0) { /* read the start struct */
1793       goto erret;
1794    }
1795 
1796    atp = AsnReadId(aip,amp, atp);
1797    func = NULL;
1798 
1799    if (atp == SEQ_GAP_type) {
1800       if ( AsnReadVal(aip, atp, &av) <= 0) {
1801          goto erret;
1802       }
1803       ptr -> type = av.intvalue;
1804       atp = AsnReadId(aip,amp, atp);
1805    }
1806    if (atp == SEQ_GAP_linkage) {
1807       if ( AsnReadVal(aip, atp, &av) <= 0) {
1808          goto erret;
1809       }
1810       ptr -> linkage = av.intvalue;
1811       atp = AsnReadId(aip,amp, atp);
1812    }
1813    if (atp == SEQ_GAP_linkage_evidence) {
1814       ptr -> linkage_evidence = AsnGenericValNodeSetAsnRead(aip, amp, atp, &isError, (AsnReadFunc) LinkageEvidenceAsnRead, (AsnOptFreeFunc) LinkageEvidenceFree);
1815       if (isError && ptr -> linkage_evidence == NULL) {
1816          goto erret;
1817       }
1818       atp = AsnReadId(aip,amp, atp);
1819    }
1820 
1821    if (AsnReadVal(aip, atp, &av) <= 0) {
1822       goto erret;
1823    }
1824    /* end struct */
1825 
1826 ret:
1827    AsnUnlinkType(orig);       /* unlink local tree */
1828    return ptr;
1829 
1830 erret:
1831    aip -> io_failure = TRUE;
1832    ptr = SeqGapFree(ptr);
1833    goto ret;
1834 }
1835 
1836 
1837 
1838 /**************************************************
1839 *
1840 *    SeqGapAsnWrite()
1841 *
1842 **************************************************/
1843 NLM_EXTERN Boolean LIBCALL
SeqGapAsnWrite(SeqGapPtr ptr,AsnIoPtr aip,AsnTypePtr orig)1844 SeqGapAsnWrite(SeqGapPtr ptr, AsnIoPtr aip, AsnTypePtr orig)
1845 {
1846    DataVal av;
1847    AsnTypePtr atp;
1848    Boolean retval = FALSE;
1849 
1850    if (! loaded)
1851    {
1852       if (! SeqAsnLoad()) {
1853          return FALSE;
1854       }
1855    }
1856 
1857    if (aip == NULL) {
1858       return FALSE;
1859    }
1860 
1861    atp = AsnLinkType(orig, SEQ_GAP);   /* link local tree */
1862    if (atp == NULL) {
1863       return FALSE;
1864    }
1865 
1866    if (ptr == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
1867 
1868     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
1869 
1870    if (! AsnOpenStruct(aip, atp, (Pointer) ptr)) {
1871       goto erret;
1872    }
1873 
1874    av.intvalue = ptr -> type;
1875    retval = AsnWrite(aip, SEQ_GAP_type,  &av);
1876    av.intvalue = ptr -> linkage;
1877    retval = AsnWrite(aip, SEQ_GAP_linkage,  &av);
1878    if (ptr -> linkage_evidence != NULL) {
1879       AsnGenericValNodeSetAsnWrite(ptr -> linkage_evidence, (AsnWriteFunc) LinkageEvidenceAsnWrite, aip, SEQ_GAP_linkage_evidence, SEQ_GAP_linkage_evidence_E);
1880    }
1881    if (! AsnCloseStruct(aip, atp, (Pointer)ptr)) {
1882       goto erret;
1883    }
1884    retval = TRUE;
1885 
1886 erret:
1887    AsnUnlinkType(orig);       /* unlink local tree */
1888    return retval;
1889 }
1890 
1891 
1892 
1893 /*****************************************************************************
1894 *
1895 *   SeqDescrFree(anp)
1896 *       Frees a chain of SeqDescr and associated data
1897 *
1898 *****************************************************************************/
SeqDescrFree(SeqDescrPtr anp)1899 NLM_EXTERN SeqDescrPtr LIBCALL SeqDescrFree (SeqDescrPtr anp)
1900 {
1901     SeqDescrPtr next;
1902 
1903     while (anp != NULL)
1904     {
1905         next = anp->next;
1906         SeqDescFree(anp);
1907         anp = next;
1908     }
1909     return anp;
1910 }
1911 
1912 /*****************************************************************************
1913 *
1914 *   SeqDescrFree(anp)
1915 *       Frees a chain of SeqDescr and associated data
1916 *
1917 *****************************************************************************/
SeqDescFree(SeqDescPtr anp)1918 NLM_EXTERN SeqDescPtr LIBCALL SeqDescFree (SeqDescPtr anp)
1919 {
1920     Pointer pnt;
1921 
1922     if (anp == NULL) return anp;
1923 
1924     ObjMgrDelete(OBJ_SEQDESC, (Pointer)anp);
1925 
1926     pnt = anp->data.ptrvalue;
1927     switch (anp->choice)
1928     {
1929         case 1:     /* mol-type */
1930         case 3:      /* method */
1931             break;
1932         case 2:     /* modif */
1933             ValNodeFree((ValNodePtr)pnt);
1934             break;
1935         case 4:      /* name */
1936         case 5:      /* title */
1937         case 7:      /* comment */
1938         case 13:     /* region */
1939         case 22:     /* heterogen */
1940             MemFree(pnt);
1941             break;
1942         case 6:      /* org */
1943             OrgRefFree((OrgRefPtr)pnt);
1944             break;
1945         case 8:      /* num */
1946             NumberingFree((ValNodePtr)pnt);
1947             break;
1948         case 9:      /* maploc */
1949             DbtagFree((DbtagPtr)pnt);
1950             break;
1951         case 10:     /* pir */
1952             PirBlockFree((PirBlockPtr)pnt);
1953             break;
1954         case 11:     /* genbank */
1955             GBBlockFree((GBBlockPtr)pnt);
1956             break;
1957         case 12:     /* pub */
1958             PubdescFree((PubdescPtr)pnt);
1959             break;
1960         case 14:     /* user */
1961             UserObjectFree((UserObjectPtr)pnt);
1962             break;
1963         case 15:     /* SP */
1964             SPBlockFree((SPBlockPtr)pnt);
1965             break;
1966         case 16:     /* dbxref */
1967             DbtagFree((DbtagPtr)pnt);
1968             break;
1969         case 17:     /* EMBL */
1970             EMBLBlockFree((EMBLBlockPtr)pnt);
1971             break;
1972         case 18:     /* create-date */
1973         case 19:     /* update-date */
1974             DateFree((DatePtr)pnt);
1975             break;
1976         case 20:     /* PRF */
1977             PrfBlockFree((PrfBlockPtr)pnt);
1978             break;
1979         case 21:     /* PDB */
1980             PdbBlockFree((PdbBlockPtr)pnt);
1981             break;
1982         case 23:     /* BioSource */
1983             BioSourceFree((BioSourcePtr)pnt);
1984             break;
1985         case 24:     /* MolInfo */
1986             MolInfoFree((MolInfoPtr)pnt);
1987             break;
1988         case 25:     /* ModelEvidenceSupport */
1989             ModelEvidenceSupportFree((ModelEvidenceSupportPtr)pnt);
1990             break;
1991     }
1992     anp = (SeqDescPtr)MemFree(anp);
1993     return anp;
1994 }
1995 
1996 
1997 /*****************************************************************************
1998 *
1999 *   SeqDescrAsnWrite(anp, aip, atp)
2000 *       atp is the current type (if identifier of a parent struct)
2001 *       if atp == NULL, then assumes it stands alone (SeqDescr ::=)
2002 *
2003 *****************************************************************************/
SeqDescrAsnWrite(ValNodePtr anp,AsnIoPtr aip,AsnTypePtr orig)2004 NLM_EXTERN Boolean LIBCALL SeqDescrAsnWrite (ValNodePtr anp, AsnIoPtr aip, AsnTypePtr orig)
2005 {
2006     return SeqDescrAsnWriteExtra(anp, aip, orig, TRUE);
2007 }
2008 
2009 /*****************************************************************************
2010 *
2011 *   SeqDescrExtraCheck(aip, atp)
2012 *       atp is the current type (if identifier of a parent struct)
2013 *       if atp == NULL, then assumes it stands alone (SeqDescr ::=)
2014 *       checks for extra descriptors from SeqEntryAsnOut, when descr is NULL
2015 *
2016 *****************************************************************************/
SeqDescrExtraCheck(AsnIoPtr aip,AsnTypePtr orig)2017 NLM_EXTERN Boolean LIBCALL SeqDescrExtraCheck (AsnIoPtr aip, AsnTypePtr orig)
2018 {
2019     return SeqDescrAsnWriteExtra((ValNodePtr)NULL, aip, orig, FALSE);
2020 }
2021 /*****************************************************************************
2022 *
2023 *   SeqDescrAsnWriteExtra(anp, aip, atp, anp_not_null)
2024 *       atp is the current type (if identifier of a parent struct)
2025 *       if atp == NULL, then assumes it stands alone (SeqDescr ::=)
2026 *       either anp or extras from SeqEntryAsnOut can be written
2027 *       if anp_not_null, means anp cannot be NULL
2028 *
2029 *****************************************************************************/
SeqDescrAsnWriteExtra(ValNodePtr anp,AsnIoPtr aip,AsnTypePtr orig,Boolean anp_not_null)2030 static Boolean SeqDescrAsnWriteExtra (ValNodePtr anp, AsnIoPtr aip, AsnTypePtr orig,
2031                     Boolean anp_not_null)
2032 {
2033     AsnTypePtr atp;
2034     ValNodePtr oldanp = NULL, tanp;
2035     Boolean retval = FALSE, had_extra = FALSE, had_anp = FALSE;
2036     AsnOptionPtr aopp;
2037     ValNodePtr extras = NULL;
2038 
2039     if (! loaded)
2040     {
2041         if (! SeqAsnLoad())
2042             return FALSE;
2043     }
2044 
2045     if (aip == NULL)
2046         return FALSE;
2047 
2048     if (anp != NULL) had_anp = TRUE;
2049     aopp = AsnIoOptionGet(aip, OP_NCBIOBJSEQ, CHECK_EXTRA_DESC, NULL);
2050     if (aopp != NULL)
2051     {
2052         had_extra = TRUE;
2053         extras = (ValNodePtr)(aopp->data.ptrvalue);
2054     }
2055     else if ((anp == NULL) && (! anp_not_null))  /* nothing to write */
2056         return TRUE;
2057 
2058     if (! ProgMon("Write SeqDescr"))
2059         return FALSE;
2060 
2061     atp = AsnLinkType(orig, SEQ_DESCR);   /* link local tree */
2062     if (atp == NULL) return FALSE;
2063 
2064     if (anp_not_null)
2065         oldanp = anp;
2066     else if (extras != NULL)
2067         oldanp = (ValNodePtr)(extras->data.ptrvalue);
2068 
2069     if (oldanp == NULL)
2070         { AsnNullValueMsg(aip, atp); goto erret; }
2071 
2072     if (! AsnOpenStruct(aip, atp, (Pointer)oldanp)) goto erret; /* SET OF */
2073 
2074     while (extras != NULL)    /* extras first */
2075     {
2076     tanp = (ValNodePtr)(extras->data.ptrvalue);
2077     if (! SeqDescAsnWrite(tanp, aip, SEQ_DESCR_E)) goto erret;
2078     extras = extras->next;
2079     }
2080 
2081     while (anp != NULL)
2082     {
2083         if (! SeqDescAsnWrite (anp, aip, SEQ_DESCR_E)) goto erret;
2084         anp = anp->next;
2085     }
2086     if (! AsnCloseStruct(aip, atp, oldanp)) goto erret;
2087     retval = TRUE;
2088 erret:
2089     if (had_extra)
2090     {
2091         AsnIoOptionFree(aip, OP_NCBIOBJSEQ, CHECK_EXTRA_DESC);
2092     }
2093     AsnUnlinkType(orig);       /* unlink local tree */
2094     return retval;
2095 }
2096 
2097 /*****************************************************************************
2098 *
2099 *   SeqDescAsnWrite(anp, aip, orig)
2100 *       Writes one Seqdesc
2101 *
2102 *****************************************************************************/
SeqDescAsnWrite(SeqDescPtr anp,AsnIoPtr aip,AsnTypePtr orig)2103 NLM_EXTERN Boolean LIBCALL SeqDescAsnWrite (SeqDescPtr anp, AsnIoPtr aip, AsnTypePtr orig)
2104 {
2105     DataValPtr dvp;
2106     AsnTypePtr atp;
2107     Pointer pnt;
2108     ValNodePtr tmp;
2109     Boolean retval = FALSE;
2110 
2111     if (! loaded)
2112     {
2113         if (! SeqAsnLoad())
2114             return FALSE;
2115     }
2116 
2117     if (aip == NULL)
2118         return FALSE;
2119 
2120     atp = AsnLinkType(orig, SEQDESC);   /* link local tree */
2121     if (atp == NULL) return FALSE;
2122 
2123     if (anp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
2124 
2125    dvp = &anp->data;
2126 
2127    if (! AsnWriteChoice(aip, atp, (Int2)anp->choice, dvp)) goto erret;
2128    pnt = anp->data.ptrvalue;
2129    switch (anp->choice)
2130    {
2131        case 1:                   /* moltype */
2132            if (! AsnWrite(aip, SEQDESC_mol_type, dvp)) goto erret;
2133            break;
2134        case 2:                   /* modif */
2135            if (! AsnOpenStruct(aip, SEQDESC_modif, anp->data.ptrvalue)) goto erret;
2136            tmp = (ValNodePtr)anp->data.ptrvalue;
2137            while (tmp != NULL)
2138            {
2139                if (! AsnWrite(aip, SEQDESC_modif_E, &tmp->data)) goto erret;
2140                tmp = tmp->next;
2141            }
2142            if (! AsnCloseStruct(aip, SEQDESC_modif, anp->data.ptrvalue)) goto erret;
2143            break;
2144        case 3:                   /* method */
2145            if (! AsnWrite(aip, SEQDESC_method, dvp)) goto erret;
2146            break;
2147        case 4:                   /* name */
2148            if (! AsnWrite(aip, SEQDESC_name, dvp)) goto erret;
2149            break;
2150        case 5:                   /* title */
2151            if (! AsnWrite(aip, SEQDESC_title, dvp)) goto erret;
2152            break;
2153        case 6:                   /* org */
2154            if (! OrgRefAsnWrite((OrgRefPtr)pnt, aip, SEQDESC_org)) goto erret;
2155            break;
2156        case 7:                   /* comment */
2157            if (! AsnWrite(aip, SEQDESC_comment, dvp)) goto erret;
2158            break;
2159        case 8:                   /* num */
2160            if (! NumberingAsnWrite((ValNodePtr)pnt, aip, SEQDESC_num)) goto erret;
2161            break;
2162        case 9:                  /* maploc */
2163            if (! DbtagAsnWrite((DbtagPtr)pnt, aip, SEQDESC_maploc)) goto erret;
2164            break;
2165        case 10:                   /* pir */
2166            if (! PirBlockAsnWrite((PirBlockPtr)pnt, aip, SEQDESC_pir)) goto erret;
2167            break;
2168        case 11:                  /* genbank */
2169            if (! GBBlockAsnWrite((GBBlockPtr)pnt, aip, SEQDESC_genbank)) goto erret;
2170            break;
2171        case 12:                  /* pubdesc */
2172            if (! PubdescAsnWrite((PubdescPtr)pnt, aip, SEQDESC_pub)) goto erret;
2173            break;
2174        case 13:                  /* region */
2175            if (! AsnWrite(aip, SEQDESC_region, dvp)) goto erret;
2176            break;
2177        case 14:                   /* user */
2178            if (! UserObjectAsnWrite((UserObjectPtr)pnt, aip, SEQDESC_user)) goto erret;
2179            break;
2180        case 15:                   /* SP */
2181            if (! SPBlockAsnWrite((SPBlockPtr)pnt, aip, SEQDESC_sp)) goto erret;
2182            break;
2183        case 16:                   /* dbxref */
2184            if (! DbtagAsnWrite((DbtagPtr)pnt, aip, SEQDESC_dbxref)) goto erret;
2185            break;
2186        case 17:                   /* EMBL */
2187            if (! EMBLBlockAsnWrite((EMBLBlockPtr)pnt, aip, SEQDESC_embl)) goto erret;
2188            break;
2189     case 18:                   /* create-date */
2190         if (! DateAsnWrite((DatePtr)pnt, aip, SEQDESC_create_date)) goto erret;
2191         break;
2192     case 19:                   /* update-date */
2193         if (! DateAsnWrite((DatePtr)pnt, aip, SEQDESC_update_date)) goto erret;
2194         break;
2195        case 20:                   /* PRF */
2196            if (! PrfBlockAsnWrite((PrfBlockPtr)pnt, aip, SEQDESC_prf)) goto erret;
2197            break;
2198        case 21:                   /* PDB */
2199            if (! PdbBlockAsnWrite((PdbBlockPtr)pnt, aip, SEQDESC_pdb)) goto erret;
2200            break;
2201     case 22:                   /* Heterogen */
2202            if (! AsnWrite(aip, SEQDESC_het, dvp)) goto erret;
2203            break;
2204        case 23:                   /* BioSource */
2205            if (! BioSourceAsnWrite((BioSourcePtr)pnt, aip, SEQDESC_source)) goto erret;
2206            break;
2207        case 24:                   /* MolInfo */
2208            if (! MolInfoAsnWrite((MolInfoPtr)pnt, aip, SEQDESC_molinfo)) goto erret;
2209            break;
2210        case 25:                   /* ModelEvidenceSupport */
2211            if (! ModelEvidenceSupportAsnWrite((ModelEvidenceSupportPtr)pnt, aip, SEQDESC_modelev)) goto erret;
2212            break;
2213    }
2214     retval = TRUE;
2215 erret:
2216     AsnUnlinkType(orig);       /* unlink local tree */
2217     return retval;
2218 }
2219 
2220 /*****************************************************************************
2221 *
2222 *   SeqDescrAsnRead(aip, atp)
2223 *       atp is the current type (if identifier of a parent struct)
2224 *            assumption is readIdent has occurred
2225 *       if atp == NULL, then assumes it stands alone and read ident
2226 *            has not occurred.
2227 *
2228 *****************************************************************************/
SeqDescrAsnRead(AsnIoPtr aip,AsnTypePtr orig)2229 NLM_EXTERN ValNodePtr LIBCALL SeqDescrAsnRead (AsnIoPtr aip, AsnTypePtr orig)
2230 {
2231     DataVal av;
2232     AsnTypePtr atp;
2233     ValNodePtr anp, first=NULL, prev=NULL;
2234 
2235     if (! loaded)
2236     {
2237         if (! SeqAsnLoad())
2238             return first;
2239     }
2240 
2241     if (aip == NULL)
2242         return first;
2243 
2244     if (! ProgMon("Read SeqDescr"))
2245         return first;
2246 
2247     if (orig == NULL)           /* SeqDescr ::= (self contained) */
2248         atp = AsnReadId(aip, amp, SEQ_DESCR);
2249     else
2250         atp = AsnLinkType(orig, SEQ_DESCR);    /* link in local tree */
2251     if (atp == NULL) return first;
2252 
2253     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
2254 
2255     while ((atp = AsnReadId(aip, amp, atp)) == SEQ_DESCR_E)
2256     {
2257         anp = SeqDescAsnRead(aip, atp);
2258         if (anp == NULL) goto erret;
2259 
2260         if (first == NULL)
2261             first = anp;
2262         if (prev != NULL)
2263             prev->next = anp;
2264         prev = anp;
2265     }
2266     if (atp == NULL) goto erret;
2267 
2268     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct */
2269 ret:
2270     AsnUnlinkType(orig);       /* unlink local tree */
2271     return first;
2272 erret:
2273     aip->io_failure = TRUE;
2274     first = SeqDescrFree(first);
2275     goto ret;
2276 }
2277 
2278 /*****************************************************************************
2279 *
2280 *   SeqDescAsnRead(aip, atp)
2281 *       atp is the current type (if identifier of a parent struct)
2282 *            assumption is readIdent has occurred
2283 *       if atp == NULL, then assumes it stands alone and read ident
2284 *            has not occurred.
2285 *
2286 *****************************************************************************/
SeqDescAsnRead(AsnIoPtr aip,AsnTypePtr orig)2287 NLM_EXTERN SeqDescPtr LIBCALL SeqDescAsnRead (AsnIoPtr aip, AsnTypePtr orig)
2288 {
2289     DataVal av;
2290     AsnTypePtr atp;
2291     SeqDescPtr anp=NULL,tmp, tmp2;
2292     Uint1 choice;
2293     AsnReadFunc func;
2294 
2295     if (! loaded)
2296     {
2297         if (! SeqAsnLoad())
2298             return anp;
2299     }
2300 
2301     if (aip == NULL)
2302         return anp;
2303 
2304     if (orig == NULL)           /* SeqDesc ::= (self contained) */
2305         atp = AsnReadId(aip, amp, SEQDESC);
2306     else
2307         atp = AsnLinkType(orig, SEQDESC);    /* link in local tree */
2308     if (atp == NULL) return anp;
2309 
2310     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE value (nothing) */
2311     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* find the choice */
2312 
2313     func = NULL;
2314     choice = 0;
2315     if (atp == SEQDESC_mol_type)
2316     {
2317         choice = 1;
2318     }
2319     else if (atp == SEQDESC_modif)
2320     {
2321         choice = 2;
2322         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start struct */
2323         tmp = NULL;
2324         while ((atp = AsnReadId(aip, amp, atp)) == SEQDESC_modif_E)
2325         {
2326             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2327             tmp2 = ValNodeNew(tmp);
2328             if (tmp2 == NULL) goto erret;
2329             if (tmp == NULL)
2330                 tmp = tmp2;
2331             tmp2->data.intvalue = av.intvalue;
2332         }
2333         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct */
2334     }
2335     else if (atp == SEQDESC_method)
2336     {
2337         choice = 3;
2338     }
2339     else if (atp == SEQDESC_name)
2340     {
2341         choice = 4;
2342     }
2343     else if (atp == SEQDESC_title)
2344     {
2345         choice = 5;
2346     }
2347     else if (atp == SEQDESC_org)
2348     {
2349         choice = 6;
2350         func = (AsnReadFunc) OrgRefAsnRead;
2351     }
2352     else if (atp == SEQDESC_comment)
2353     {
2354         choice = 7;
2355     }
2356     else if (atp == SEQDESC_num)
2357     {
2358         choice = 8;
2359         func = (AsnReadFunc) NumberingAsnRead;
2360     }
2361     else if (atp == SEQDESC_maploc)
2362     {
2363         choice = 9;
2364         func = (AsnReadFunc) DbtagAsnRead;
2365     }
2366     else if (atp == SEQDESC_pir)
2367     {
2368         choice = 10;
2369         func = (AsnReadFunc) PirBlockAsnRead;
2370     }
2371     else if (atp == SEQDESC_genbank)
2372     {
2373         choice = 11;
2374         func = (AsnReadFunc) GBBlockAsnRead;
2375     }
2376     else if (atp == SEQDESC_pub)
2377     {
2378         choice = 12;
2379         func = (AsnReadFunc) PubdescAsnRead;
2380     }
2381     else if (atp == SEQDESC_region)
2382     {
2383         choice = 13;
2384     }
2385     else if (atp == SEQDESC_user)
2386     {
2387         choice = 14;
2388         func = (AsnReadFunc) UserObjectAsnRead;
2389     }
2390     else if (atp == SEQDESC_sp)
2391     {
2392         choice = 15;
2393         func = (AsnReadFunc) SPBlockAsnRead;
2394     }
2395     else if (atp == SEQDESC_dbxref)
2396     {
2397         choice = 16;
2398         func = (AsnReadFunc) DbtagAsnRead;
2399     }
2400     else if (atp == SEQDESC_embl)
2401     {
2402         choice = 17;
2403         func = (AsnReadFunc) EMBLBlockAsnRead;
2404     }
2405     else if (atp == SEQDESC_create_date)
2406     {
2407         choice = 18;
2408         func = (AsnReadFunc) DateAsnRead;
2409     }
2410     else if (atp == SEQDESC_update_date)
2411     {
2412         choice = 19;
2413         func = (AsnReadFunc) DateAsnRead;
2414     }
2415     else if (atp == SEQDESC_prf)
2416     {
2417         choice = 20;
2418         func = (AsnReadFunc) PrfBlockAsnRead;
2419     }
2420     else if (atp == SEQDESC_pdb)
2421     {
2422         choice = 21;
2423         func = (AsnReadFunc) PdbBlockAsnRead;
2424     }
2425     else if (atp == SEQDESC_het)
2426     {
2427         choice = 22;
2428     }
2429     else if (atp == SEQDESC_source)
2430     {
2431         choice = 23;
2432         func = (AsnReadFunc) BioSourceAsnRead;
2433     }
2434     else if (atp == SEQDESC_molinfo)
2435     {
2436         choice = 24;
2437         func = (AsnReadFunc) MolInfoAsnRead;
2438     }
2439     else if (atp == SEQDESC_modelev)
2440     {
2441         choice = 25;
2442         func = (AsnReadFunc) ModelEvidenceSupportAsnRead;
2443     }
2444 
2445     anp = SeqDescrNew(NULL);
2446     if (anp == NULL) goto erret;
2447     anp->choice = choice;
2448     if (func != NULL)
2449     {
2450         anp->data.ptrvalue = (* func)(aip, atp);
2451         if (anp->data.ptrvalue == NULL) goto erret;
2452     }
2453     else if (choice != 2)
2454     {
2455         if (AsnReadVal(aip, atp, &anp->data) <= 0) goto erret;
2456     }
2457     else
2458         anp->data.ptrvalue = (Pointer)tmp;
2459 
2460 ret:
2461     AsnUnlinkType(orig);       /* unlink local tree */
2462     return anp;
2463 erret:
2464     aip->io_failure = TRUE;
2465     anp = SeqDescFree(anp);
2466     goto ret;
2467 }
2468 
2469 /*****************************************************************************
2470 *
2471 *   NumberingFree(anp)
2472 *       Frees a chain of Numbering and associated data
2473 *
2474 *****************************************************************************/
NumberingFree(NumberingPtr anp)2475 NLM_EXTERN NumberingPtr LIBCALL NumberingFree (NumberingPtr anp)
2476 {
2477     Pointer pnt;
2478 
2479     if (anp == NULL)
2480         return anp;
2481 
2482     pnt = anp->data.ptrvalue;
2483     switch (anp->choice)
2484     {
2485         case 1:     /* num-cont */
2486             NumContFree((NumContPtr)pnt);
2487             break;
2488         case 2:      /* num-enum */
2489             NumEnumFree((NumEnumPtr)pnt);
2490             break;
2491         case 3:     /* ref, sources */
2492         case 4:      /* ref, aligns */
2493             if (pnt != NULL)
2494                 SeqAlignFree((SeqAlignPtr)pnt);
2495             break;
2496         case 5:     /* real */
2497             NumRealFree((NumRealPtr)pnt);
2498             break;
2499     }
2500 
2501     return (NumberingPtr)MemFree(anp);
2502 }
2503 /*****************************************************************************
2504 *
2505 *   NumberingAsnWrite(anp, aip, atp)
2506 *       atp is the current type (if identifier of a parent struct)
2507 *       if atp == NULL, then assumes it stands alone (Numbering ::=)
2508 *
2509 *****************************************************************************/
NumberingAsnWrite(NumberingPtr anp,AsnIoPtr aip,AsnTypePtr orig)2510 NLM_EXTERN Boolean LIBCALL NumberingAsnWrite (NumberingPtr anp, AsnIoPtr aip, AsnTypePtr orig)
2511 {
2512     DataVal av;
2513     AsnTypePtr atp;
2514     Pointer pnt;
2515     Boolean retval = FALSE;
2516 
2517     if (! loaded)
2518     {
2519         if (! SeqAsnLoad())
2520             return FALSE;
2521     }
2522 
2523     if (aip == NULL)
2524         return FALSE;
2525 
2526     atp = AsnLinkType(orig, NUMBERING);   /* link local tree */
2527     if (atp == NULL) return FALSE;
2528 
2529     if (anp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
2530 
2531     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
2532 
2533     av.ptrvalue = (Pointer)anp;
2534     if (! AsnWriteChoice(aip, atp, (Int2)anp->choice, &av)) goto erret;
2535 
2536     pnt = anp->data.ptrvalue;
2537     switch (anp->choice)
2538     {
2539         case 1:                   /* num-cont */
2540             if (! NumContAsnWrite((NumContPtr)pnt, aip, NUMBERING_cont)) goto erret;
2541             break;
2542         case 2:                   /* num-enum */
2543             if (! NumEnumAsnWrite((NumEnumPtr)pnt, aip, NUMBERING_enum)) goto erret;
2544             break;
2545         case 3:                   /* num-ref, sources */
2546         case 4:                   /* num-ref, aligns */
2547             if (! AsnOpenStruct(aip, NUMBERING_ref, pnt)) goto erret;
2548             av.intvalue = anp->choice - 2;
2549             if (! AsnWrite(aip, NUM_REF_type, &av)) goto erret;
2550             if (pnt != NULL)
2551             {
2552                 if (! SeqAlignAsnWrite((SeqAlignPtr)pnt, aip, NUM_REF_aligns)) goto erret;
2553             }
2554             if (! AsnCloseStruct(aip, NUMBERING_ref, pnt)) goto erret;
2555             break;
2556         case 5:                   /* real */
2557             if (! NumRealAsnWrite((NumRealPtr)pnt, aip, NUMBERING_real)) goto erret;
2558             break;
2559     }
2560     retval = TRUE;
2561 erret:
2562     AsnUnlinkType(orig);       /* unlink local tree */
2563     return retval;
2564 }
2565 /*****************************************************************************
2566 *
2567 *   NumberingAsnRead(aip, atp)
2568 *       atp is the current type (if identifier of a parent struct)
2569 *            assumption is readIdent has occurred
2570 *       if atp == NULL, then assumes it stands alone and read ident
2571 *            has not occurred.
2572 *
2573 *****************************************************************************/
NumberingAsnRead(AsnIoPtr aip,AsnTypePtr orig)2574 NLM_EXTERN NumberingPtr LIBCALL NumberingAsnRead (AsnIoPtr aip, AsnTypePtr orig)
2575 {
2576     DataVal av;
2577     AsnTypePtr atp;
2578     NumberingPtr anp = NULL;
2579 
2580     if (! loaded)
2581     {
2582         if (! SeqAsnLoad())
2583             return anp;
2584     }
2585 
2586     if (aip == NULL)
2587         return anp;
2588 
2589     if (orig == NULL)           /* Numbering ::= (self contained) */
2590         atp = AsnReadId(aip, amp, NUMBERING);
2591     else
2592         atp = AsnLinkType(orig, NUMBERING);    /* link in local tree */
2593     if (atp == NULL) return anp;
2594 
2595     anp = ValNodeNew(NULL);
2596     if (anp == NULL) goto erret;
2597 
2598     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the CHOICE */
2599 
2600     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* kind of choice */
2601     if (atp == NUMBERING_cont)
2602     {
2603         anp->choice = 1;
2604         anp->data.ptrvalue = (Pointer) NumContAsnRead(aip, atp);
2605     }
2606     else if (atp == NUMBERING_enum)
2607     {
2608         anp->choice = 2;
2609         anp->data.ptrvalue = (Pointer) NumEnumAsnRead(aip, atp);
2610     }
2611     else if (atp == NUMBERING_real)
2612     {
2613         anp->choice = 5;
2614         anp->data.ptrvalue = (Pointer) NumRealAsnRead(aip, atp);
2615     }
2616     else if (atp == NUMBERING_ref)
2617     {
2618         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* start struct */
2619         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
2620         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* type */
2621         if (av.intvalue == 1)
2622             anp->choice = 3;
2623         else if (av.intvalue == 2)
2624             anp->choice = 4;
2625         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
2626         if (atp == NUM_REF_aligns)
2627             anp->data.ptrvalue = (Pointer) SeqAlignAsnRead(aip, atp);
2628     }
2629     if (anp->data.ptrvalue == NULL) goto erret;
2630 ret:
2631     AsnUnlinkType(orig);       /* unlink local tree */
2632     return anp;
2633 erret:
2634     anp = NumberingFree(anp);
2635     goto ret;
2636 }
2637 
2638 /*****************************************************************************
2639 *
2640 *   NumContNew()
2641 *
2642 *****************************************************************************/
NumContNew(void)2643 NLM_EXTERN NumContPtr LIBCALL NumContNew (void)
2644 {
2645     NumContPtr ncp;
2646     ncp = (NumContPtr)MemNew(sizeof(NumCont));
2647     if (ncp == NULL) return NULL;
2648 
2649     ncp->ascending = TRUE;   /* default states */
2650     ncp->refnum = 1;
2651     return ncp;
2652 }
2653 
2654 /*****************************************************************************
2655 *
2656 *   NumContFree(ncp)
2657 *       Frees a NumCont and associated data
2658 *
2659 *****************************************************************************/
NumContFree(NumContPtr ncp)2660 NLM_EXTERN NumContPtr LIBCALL NumContFree (NumContPtr ncp)
2661 {
2662     if (ncp == NULL)
2663         return ncp;
2664 
2665     return (NumContPtr)MemFree(ncp);
2666 }
2667 /*****************************************************************************
2668 *
2669 *   NumContAsnWrite(ncp, aip, atp)
2670 *       atp is the current type (if identifier of a parent struct)
2671 *       if atp == NULL, then assumes it stands alone (NumCont ::=)
2672 *
2673 *****************************************************************************/
NumContAsnWrite(NumContPtr ncp,AsnIoPtr aip,AsnTypePtr orig)2674 NLM_EXTERN Boolean LIBCALL NumContAsnWrite (NumContPtr ncp, AsnIoPtr aip, AsnTypePtr orig)
2675 {
2676     DataVal av;
2677     AsnTypePtr atp;
2678     Boolean retval = FALSE;
2679 
2680     if (! loaded)
2681     {
2682         if (! SeqAsnLoad())
2683             return FALSE;
2684     }
2685 
2686     if (aip == NULL)
2687         return FALSE;
2688 
2689     atp = AsnLinkType(orig, NUM_CONT);   /* link local tree */
2690     if (atp == NULL) return FALSE;
2691 
2692     if (ncp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
2693 
2694     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
2695 
2696     if (! AsnOpenStruct(aip, atp, (Pointer)ncp)) goto erret;
2697     if (ncp->refnum != 1)
2698     {
2699         av.intvalue = ncp->refnum;
2700         if (! AsnWrite(aip, NUM_CONT_refnum, &av)) goto erret;
2701     }
2702     if (ncp->has_zero)
2703     {
2704         av.boolvalue = ncp->has_zero;
2705         if (! AsnWrite(aip, NUM_CONT_has_zero, &av)) goto erret;
2706     }
2707     if (! ncp->ascending)
2708     {
2709         av.boolvalue = ncp->ascending;
2710         if (! AsnWrite(aip, NUM_CONT_ascending, &av)) goto erret;
2711     }
2712 
2713     if (! AsnCloseStruct(aip, atp, (Pointer)ncp)) goto erret;
2714     retval = TRUE;
2715 erret:
2716     AsnUnlinkType(orig);       /* unlink local tree */
2717     return retval;
2718 }
2719 
2720 /*****************************************************************************
2721 *
2722 *   NumContAsnRead(aip, atp)
2723 *       atp is the current type (if identifier of a parent struct)
2724 *            assumption is readIdent has occurred
2725 *       if atp == NULL, then assumes it stands alone and read ident
2726 *            has not occurred.
2727 *
2728 *****************************************************************************/
NumContAsnRead(AsnIoPtr aip,AsnTypePtr orig)2729 NLM_EXTERN NumContPtr LIBCALL NumContAsnRead (AsnIoPtr aip, AsnTypePtr orig)
2730 {
2731     DataVal av;
2732     AsnTypePtr atp, oldatp;
2733     NumContPtr ncp=NULL;
2734 
2735     if (! loaded)
2736     {
2737         if (! SeqAsnLoad())
2738             return ncp;
2739     }
2740 
2741     if (aip == NULL)
2742         return ncp;
2743 
2744     if (orig == NULL)           /* NumCont ::= (self contained) */
2745         atp = AsnReadId(aip, amp, NUM_CONT);
2746     else
2747         atp = AsnLinkType(orig, NUM_CONT);    /* link in local tree */
2748     oldatp = atp;
2749     if (atp == NULL) return ncp;
2750 
2751     ncp = NumContNew();
2752     if (ncp == NULL) goto erret;
2753 
2754     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
2755 
2756     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
2757     {
2758         if (atp == NULL) goto erret;
2759         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2760         if (atp == NUM_CONT_refnum)
2761             ncp->refnum = av.intvalue;
2762         else if (atp == NUM_CONT_has_zero)
2763             ncp->has_zero = av.boolvalue;
2764         else if (atp == NUM_CONT_ascending)
2765             ncp->ascending = av.boolvalue;
2766     }
2767     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end struct */
2768 ret:
2769     AsnUnlinkType(orig);       /* unlink local tree */
2770     return ncp;
2771 erret:
2772     ncp = NumContFree(ncp);
2773     goto ret;
2774 }
2775 
2776 /*****************************************************************************
2777 *
2778 *   NumEnumNew()
2779 *
2780 *****************************************************************************/
NumEnumNew(void)2781 NLM_EXTERN NumEnumPtr LIBCALL NumEnumNew (void)
2782 {
2783     NumEnumPtr nep;
2784     nep = (NumEnumPtr)MemNew(sizeof(NumEnum));
2785     return nep;
2786 }
2787 
2788 /*****************************************************************************
2789 *
2790 *   NumEnumFree(nep)
2791 *       Frees a NumEnum and associated data
2792 *
2793 *****************************************************************************/
NumEnumFree(NumEnumPtr nep)2794 NLM_EXTERN NumEnumPtr LIBCALL NumEnumFree (NumEnumPtr nep)
2795 {
2796     if (nep == NULL)
2797         return nep;
2798 
2799     MemFree(nep->buf);
2800     MemFree(nep->names);
2801     return (NumEnumPtr)MemFree(nep);
2802 }
2803 
2804 /*****************************************************************************
2805 *
2806 *   NumEnumAsnWrite(nep, aip, atp)
2807 *       atp is the current type (if identifier of a parent struct)
2808 *       if atp == NULL, then assumes it stands alone (NumEnum ::=)
2809 *
2810 *****************************************************************************/
NumEnumAsnWrite(NumEnumPtr nep,AsnIoPtr aip,AsnTypePtr orig)2811 NLM_EXTERN Boolean LIBCALL NumEnumAsnWrite (NumEnumPtr nep, AsnIoPtr aip, AsnTypePtr orig)
2812 {
2813     DataVal av;
2814     AsnTypePtr atp;
2815     Int4 i, num;
2816     Boolean retval = FALSE;
2817 
2818     if (! loaded)
2819     {
2820         if (! SeqAsnLoad())
2821             return FALSE;
2822     }
2823 
2824     if (aip == NULL)
2825         return FALSE;
2826 
2827     atp = AsnLinkType(orig, NUM_ENUM);   /* link local tree */
2828     if (atp == NULL) return FALSE;
2829 
2830     if (nep == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
2831 
2832     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
2833 
2834     if (! AsnOpenStruct(aip, atp, (Pointer)nep)) goto erret;
2835 
2836     num = nep->num;
2837     av.intvalue = num;
2838     if (! AsnWrite(aip, NUM_ENUM_num, &av)) goto erret;
2839     if (! AsnOpenStruct(aip, NUM_ENUM_names, (Pointer)nep->names)) goto erret;
2840     for (i = 0; i < num; i++)
2841     {
2842         av.ptrvalue = (Pointer) nep->names[i];
2843         if (! AsnWrite(aip, NUM_ENUM_names_E, &av)) goto erret;
2844     }
2845     if (! AsnCloseStruct(aip, NUM_ENUM_names, (Pointer)nep->names)) goto erret;
2846 
2847     if (! AsnCloseStruct(aip, atp, (Pointer)nep)) goto erret;
2848     retval = TRUE;
2849 erret:
2850     AsnUnlinkType(orig);       /* unlink local tree */
2851     return retval;
2852 }
2853 
2854 /*****************************************************************************
2855 *
2856 *   NumEnumAsnRead(aip, atp)
2857 *       atp is the current type (if identifier of a parent struct)
2858 *            assumption is readIdent has occurred
2859 *       if atp == NULL, then assumes it stands alone and read ident
2860 *            has not occurred.
2861 *
2862 *****************************************************************************/
NumEnumAsnRead(AsnIoPtr aip,AsnTypePtr orig)2863 NLM_EXTERN NumEnumPtr LIBCALL NumEnumAsnRead (AsnIoPtr aip, AsnTypePtr orig)
2864 {
2865     DataVal av;
2866     AsnTypePtr atp;
2867     NumEnumPtr nep=NULL;
2868     Int4 num, i, total, used, oldtotal, j;
2869     CharPtr pnt, tmp, tmp2;
2870     CharPtr PNTR names2;
2871 
2872     if (! loaded)
2873     {
2874         if (! SeqAsnLoad())
2875             return nep;
2876     }
2877 
2878     if (aip == NULL)
2879         return nep;
2880 
2881     if (orig == NULL)           /* NumEnum ::= (self contained) */
2882         atp = AsnReadId(aip, amp, NUM_ENUM);
2883     else
2884         atp = AsnLinkType(orig, NUM_ENUM);    /* link in local tree */
2885     if (atp == NULL) return nep;
2886 
2887     nep = NumEnumNew();
2888     if (nep == NULL) goto erret;
2889 
2890     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
2891 
2892     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* get num */
2893     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2894     num = av.intvalue;
2895     nep->num = num;
2896     i = 0;
2897     total = num * 4;
2898     used = 0;
2899     nep->buf = (CharPtr)MemNew((size_t)total);   /* assume an average of 3 chars/name */
2900     if (nep->buf == NULL) goto erret;
2901     nep->names = (CharPtr PNTR)MemNew((size_t)(num * sizeof(CharPtr)));
2902     if (nep->names == NULL) goto erret;
2903     pnt = nep->buf;
2904     names2 = nep->names;
2905 
2906     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* name list */
2907     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2908 
2909     while ((atp = AsnReadId(aip, amp, atp)) == NUM_ENUM_names_E)
2910     {
2911         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
2912         tmp = (CharPtr)av.ptrvalue;
2913         used += StringLen(tmp) + 1;
2914         if (i == num)       /* out of pointers -- num is in ERROR */
2915         {
2916             num += 10;
2917             names2 = nep->names;
2918             nep->names = (CharPtr PNTR)MemNew((size_t)(num * sizeof(CharPtr)));
2919             if (nep->names == NULL)
2920             {
2921                 nep->names = names2;
2922                 goto erret;
2923             }
2924             MemCopy(nep->names, names2, (size_t)(i * sizeof(CharPtr)));
2925             MemFree(names2);
2926             names2 = nep->names;
2927         }
2928         if (used > total)     /* out of buffer space */
2929         {
2930             oldtotal = total;
2931             total += 100;
2932             tmp2 = nep->buf;
2933             nep->buf = (CharPtr)MemNew((size_t)total);
2934             if (nep->buf == NULL)
2935             {
2936                 nep->buf = tmp2;
2937                 goto erret;
2938             }
2939             MemCopy(nep->buf, tmp2, (size_t)oldtotal);
2940             MemFree(tmp2);
2941             pnt = nep->buf;
2942             for (j = 0; j < i; j++)
2943             {
2944                 names2[j] = pnt;
2945                 while (* pnt != '\0')
2946                     pnt++;
2947                 pnt++;
2948             }
2949         }
2950         names2[i] = pnt;
2951         pnt = StringMove(pnt, tmp);
2952         pnt++;   /* move past \0 */
2953         i++;
2954         MemFree(tmp);
2955     }
2956     if (atp == NULL) goto erret;
2957     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end struct names */
2958 
2959     nep->num = i;    /* record real count of names -- self correcting */
2960 
2961     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
2962     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct */
2963 ret:
2964     AsnUnlinkType(orig);       /* unlink local tree */
2965     return nep;
2966 erret:
2967     nep = NumEnumFree(nep);
2968     goto ret;
2969 }
2970 
2971 /*****************************************************************************
2972 *
2973 *   NumRealNew()
2974 *
2975 *****************************************************************************/
NumRealNew(void)2976 NLM_EXTERN NumRealPtr LIBCALL NumRealNew (void)
2977 {
2978     return (NumRealPtr)MemNew(sizeof(NumReal));
2979 }
2980 
2981 /*****************************************************************************
2982 *
2983 *   NumRealFree(nrp)
2984 *       Frees a NumReal and associated data
2985 *
2986 *****************************************************************************/
NumRealFree(NumRealPtr nrp)2987 NLM_EXTERN NumRealPtr LIBCALL NumRealFree (NumRealPtr nrp)
2988 {
2989     if (nrp == NULL)
2990         return nrp;
2991 
2992     MemFree(nrp->units);
2993     return (NumRealPtr)MemFree(nrp);
2994 }
2995 
2996 /*****************************************************************************
2997 *
2998 *   NumRealAsnWrite(nrp, aip, atp)
2999 *       atp is the current type (if identifier of a parent struct)
3000 *       if atp == NULL, then assumes it stands alone (NumReal ::=)
3001 *
3002 *****************************************************************************/
NumRealAsnWrite(NumRealPtr nrp,AsnIoPtr aip,AsnTypePtr orig)3003 NLM_EXTERN Boolean LIBCALL NumRealAsnWrite (NumRealPtr nrp, AsnIoPtr aip, AsnTypePtr orig)
3004 {
3005     DataVal av;
3006     AsnTypePtr atp;
3007     Boolean retval = FALSE;
3008 
3009     if (! loaded)
3010     {
3011         if (! SeqAsnLoad())
3012             return FALSE;
3013     }
3014 
3015     if (aip == NULL)
3016         return FALSE;
3017 
3018     atp = AsnLinkType(orig, NUM_REAL);   /* link local tree */
3019     if (atp == NULL) return FALSE;
3020 
3021     if (nrp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
3022 
3023     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
3024 
3025     if (! AsnOpenStruct(aip, atp, (Pointer)nrp)) goto erret;
3026 
3027     av.realvalue = nrp->a;
3028     if (! AsnWrite(aip, NUM_REAL_a, &av)) goto erret;
3029 
3030     av.realvalue = nrp->b;
3031     if (! AsnWrite(aip, NUM_REAL_b, &av)) goto erret;
3032 
3033     if (nrp->units != NULL)
3034     {
3035         av.ptrvalue = nrp->units;
3036         if (! AsnWrite(aip, NUM_REAL_units, &av)) goto erret;
3037     }
3038 
3039     if (! AsnCloseStruct(aip, atp, (Pointer)nrp)) goto erret;
3040     retval = TRUE;
3041 erret:
3042     AsnUnlinkType(orig);       /* unlink local tree */
3043     return retval;
3044 }
3045 
3046 /*****************************************************************************
3047 *
3048 *   NumRealAsnRead(aip, atp)
3049 *       atp is the current type (if identifier of a parent struct)
3050 *            assumption is readIdent has occurred
3051 *       if atp == NULL, then assumes it stands alone and read ident
3052 *            has not occurred.
3053 *
3054 *****************************************************************************/
NumRealAsnRead(AsnIoPtr aip,AsnTypePtr orig)3055 NLM_EXTERN NumRealPtr LIBCALL NumRealAsnRead (AsnIoPtr aip, AsnTypePtr orig)
3056 {
3057     DataVal av;
3058     AsnTypePtr atp, oldatp;
3059     NumRealPtr nrp=NULL;
3060 
3061     if (! loaded)
3062     {
3063         if (! SeqAsnLoad())
3064             return nrp;
3065     }
3066 
3067     if (aip == NULL)
3068         return nrp;
3069 
3070     if (orig == NULL)           /* NumReal ::= (self contained) */
3071         atp = AsnReadId(aip, amp, NUM_REAL);
3072     else
3073         atp = AsnLinkType(orig, NUM_REAL);    /* link in local tree */
3074     oldatp = atp;
3075     if (atp == NULL) return nrp;
3076 
3077     nrp = NumRealNew();
3078     if (nrp == NULL) goto erret;
3079 
3080     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
3081 
3082     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
3083     {
3084         if (atp == NULL) goto erret;
3085         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
3086         if (atp == NUM_REAL_a)
3087             nrp->a = av.realvalue;
3088         else if (atp == NUM_REAL_b)
3089             nrp->b = av.realvalue;
3090         else if (atp == NUM_REAL_units)
3091             nrp->units = (CharPtr)av.ptrvalue;
3092     }
3093     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end struct */
3094 ret:
3095     AsnUnlinkType(orig);       /* unlink local tree */
3096     return nrp;
3097 erret:
3098     nrp = NumRealFree(nrp);
3099     goto ret;
3100 }
3101 
3102 /*****************************************************************************
3103 *
3104 *   PubdescNew()
3105 *
3106 *****************************************************************************/
PubdescNew(void)3107 NLM_EXTERN PubdescPtr LIBCALL PubdescNew (void)
3108 {
3109     return (PubdescPtr)MemNew(sizeof(Pubdesc));
3110 }
3111 
3112 /*****************************************************************************
3113 *
3114 *   PubdescFree(pdp)
3115 *       Frees a Pubdesc and associated data
3116 *
3117 *****************************************************************************/
PubdescFree(PubdescPtr pdp)3118 NLM_EXTERN PubdescPtr LIBCALL PubdescFree (PubdescPtr pdp)
3119 {
3120     if (pdp == NULL)
3121         return pdp;
3122 
3123     PubEquivFree(pdp->pub);
3124     MemFree(pdp->name);
3125     MemFree(pdp->fig);
3126     NumberingFree(pdp->num);
3127     MemFree(pdp->maploc);
3128     MemFree(pdp->seq_raw);
3129     MemFree(pdp->comment);
3130     return (PubdescPtr)MemFree(pdp);
3131 }
3132 
3133 /*****************************************************************************
3134 *
3135 *   PubdescAsnWrite(pdp, aip, atp)
3136 *       atp is the current type (if identifier of a parent struct)
3137 *       if atp == NULL, then assumes it stands alone (Pubdesc ::=)
3138 *
3139 *****************************************************************************/
PubdescAsnWrite(PubdescPtr pdp,AsnIoPtr aip,AsnTypePtr orig)3140 NLM_EXTERN Boolean LIBCALL PubdescAsnWrite (PubdescPtr pdp, AsnIoPtr aip, AsnTypePtr orig)
3141 {
3142     DataVal av;
3143     AsnTypePtr atp;
3144     Boolean retval = FALSE;
3145 
3146     if (! loaded)
3147     {
3148         if (! SeqAsnLoad())
3149             return FALSE;
3150     }
3151 
3152     if (aip == NULL)
3153         return FALSE;
3154 
3155     atp = AsnLinkType(orig, PUBDESC);   /* link local tree */
3156     if (atp == NULL) return FALSE;
3157 
3158     if (pdp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
3159 
3160     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
3161 
3162     if (! AsnOpenStruct(aip, atp, (Pointer)pdp)) goto erret;
3163     if (! PubEquivAsnWrite(pdp->pub, aip, PUBDESC_pub)) goto erret;
3164 
3165     if (pdp->name != NULL)
3166     {
3167         av.ptrvalue = pdp->name;
3168         if (! AsnWrite(aip, PUBDESC_name, &av)) goto erret;
3169     }
3170     if (pdp->fig != NULL)
3171     {
3172         av.ptrvalue = pdp->fig;
3173         if (! AsnWrite(aip, PUBDESC_fig, &av)) goto erret;
3174     }
3175     if (pdp->num != NULL)
3176     {
3177         if (! NumberingAsnWrite(pdp->num, aip, PUBDESC_num)) goto erret;
3178     }
3179     if (pdp->numexc)
3180     {
3181         av.boolvalue = pdp->numexc;
3182         if (! AsnWrite(aip, PUBDESC_numexc, &av)) goto erret;
3183     }
3184     if (pdp->poly_a)
3185     {
3186         av.boolvalue = pdp->poly_a;
3187         if (! AsnWrite(aip, PUBDESC_poly_a, &av)) goto erret;
3188     }
3189     if (pdp->maploc != NULL)
3190     {
3191         av.ptrvalue = pdp->maploc;
3192         if (! AsnWrite(aip, PUBDESC_maploc, &av)) goto erret;
3193     }
3194     if (pdp->seq_raw != NULL)
3195     {
3196         av.ptrvalue = pdp->seq_raw;
3197         if (! AsnWrite(aip, PUBDESC_seq_raw, &av)) goto erret;
3198     }
3199     if (pdp->align_group)
3200     {
3201         av.intvalue = (Int4)pdp->align_group;
3202         if (! AsnWrite(aip, PUBDESC_align_group, &av)) goto erret;
3203     }
3204     if (pdp->comment != NULL)
3205     {
3206         av.ptrvalue = pdp->comment;
3207         if (! AsnWrite(aip, PUBDESC_comment, &av)) goto erret;
3208     }
3209     if (pdp->reftype)
3210     {
3211         av.intvalue = (Int4)(pdp->reftype);
3212         if (! AsnWrite(aip, PUBDESC_reftype, &av)) goto erret;
3213     }
3214 
3215     if (! AsnCloseStruct(aip, atp, (Pointer)pdp)) goto erret;
3216     retval = TRUE;
3217 erret:
3218     AsnUnlinkType(orig);       /* unlink local tree */
3219     return retval;
3220 }
3221 
3222 /*****************************************************************************
3223 *
3224 *   PubdescAsnRead(aip, atp)
3225 *       atp is the current type (if identifier of a parent struct)
3226 *            assumption is readIdent has occurred
3227 *       if atp == NULL, then assumes it stands alone and read ident
3228 *            has not occurred.
3229 *
3230 *****************************************************************************/
PubdescAsnRead(AsnIoPtr aip,AsnTypePtr orig)3231 NLM_EXTERN PubdescPtr LIBCALL PubdescAsnRead (AsnIoPtr aip, AsnTypePtr orig)
3232 {
3233     DataVal av;
3234     AsnTypePtr atp, oldatp;
3235     PubdescPtr pdp=NULL;
3236 
3237     if (! loaded)
3238     {
3239         if (! SeqAsnLoad())
3240             return pdp;
3241     }
3242 
3243     if (aip == NULL)
3244         return pdp;
3245 
3246     if (orig == NULL)           /* Pubdesc ::= (self contained) */
3247         atp = AsnReadId(aip, amp, PUBDESC);
3248     else
3249         atp = AsnLinkType(orig, PUBDESC);    /* link in local tree */
3250     oldatp = atp;
3251     if (atp == NULL) return pdp;
3252 
3253     pdp = PubdescNew();
3254     if (pdp == NULL) goto erret;
3255 
3256     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
3257 
3258     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
3259     {
3260         if (atp == NULL) goto erret;
3261         if (atp == PUBDESC_pub)
3262         {
3263             pdp->pub = PubEquivAsnRead(aip, atp);
3264             if (pdp->pub == NULL) goto erret;
3265         }
3266         else if (atp == PUBDESC_num)
3267         {
3268             pdp->num = NumberingAsnRead(aip, atp);
3269             if (pdp->num == NULL) goto erret;
3270         }
3271         else
3272         {
3273             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
3274             if (atp == PUBDESC_name)
3275                 pdp->name = (CharPtr)av.ptrvalue;
3276             else if (atp == PUBDESC_fig)
3277                 pdp->fig = (CharPtr)av.ptrvalue;
3278             else if (atp == PUBDESC_numexc)
3279                 pdp->numexc = av.boolvalue;
3280             else if (atp == PUBDESC_poly_a)
3281                 pdp->poly_a = av.boolvalue;
3282             else if (atp == PUBDESC_maploc)
3283                 pdp->maploc = (CharPtr)av.ptrvalue;
3284             else if (atp == PUBDESC_seq_raw)
3285                 pdp->seq_raw = (CharPtr)av.ptrvalue;
3286             else if (atp == PUBDESC_align_group)
3287                 pdp->align_group = (Uint1)av.intvalue;
3288             else if (atp == PUBDESC_comment)
3289                 pdp->comment = (CharPtr)av.ptrvalue;
3290             else if (atp == PUBDESC_reftype)
3291                 pdp->reftype = (Uint1)av.intvalue;
3292         }
3293     }
3294     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end struct */
3295 ret:
3296     AsnUnlinkType(orig);       /* unlink local tree */
3297     return pdp;
3298 erret:
3299     pdp = PubdescFree(pdp);
3300     goto ret;
3301 }
3302 
3303 /*****************************************************************************
3304 *
3305 *   SeqAnnotNew()
3306 *
3307 *****************************************************************************/
SeqAnnotNew(void)3308 NLM_EXTERN SeqAnnotPtr LIBCALL SeqAnnotNew (void)
3309 {
3310     return (SeqAnnotPtr)MemNew(sizeof(SeqAnnot));
3311 }
3312 
3313 /*****************************************************************************
3314 *
3315 *   SeqAnnotFree(sap)
3316 *       Frees a SeqAnnot and associated data
3317 *
3318 *****************************************************************************/
SeqAnnotFree(SeqAnnotPtr sap)3319 NLM_EXTERN SeqAnnotPtr LIBCALL SeqAnnotFree (SeqAnnotPtr sap)
3320 {
3321     SeqFeatPtr sfp, sfpnext;
3322     SeqAlignPtr sp, spnext;
3323     SeqGraphPtr sgp, sgpnext;
3324     SeqIdPtr sip;
3325     SeqLocPtr slp;
3326     SeqTablePtr stp;
3327 
3328     if (sap == NULL)
3329         return sap;
3330 
3331     ObjMgrDelete(OBJ_SEQANNOT, (Pointer)sap);
3332 
3333     AnnotIdSetFree(sap->id);
3334     MemFree(sap->name);
3335     AnnotDescrFree(sap->desc);
3336     switch (sap->type)
3337     {
3338         case 1:   /* ftable */
3339             sfp = (SeqFeatPtr)sap->data;
3340             while (sfp != NULL)
3341             {
3342                 sfpnext = sfp->next;
3343                 SeqFeatFree(sfp);
3344                 sfp = sfpnext;
3345             }
3346             break;
3347         case 2:   /* alignments */
3348             sp = (SeqAlignPtr)sap->data;
3349             while (sp != NULL)
3350             {
3351                 spnext = sp->next;
3352                 SeqAlignFree(sp);
3353                 sp = spnext;
3354             }
3355             break;
3356         case 3:
3357             sgp = (SeqGraphPtr)sap->data;
3358             while (sgp != NULL)
3359             {
3360                 sgpnext = sgp->next;
3361                 SeqGraphFree(sgp);
3362                 sgp = sgpnext;
3363             }
3364             break;
3365         case 4:    /* SeqIds */
3366             sip = (SeqIdPtr)sap->data;
3367             SeqIdSetFree(sip);
3368             break;
3369         case 5:   /* SeqLocs */
3370             slp = (SeqLocPtr)sap->data;
3371             SeqLocSetFree(slp);
3372             break;
3373         case 6:   /* SeqTable */
3374             stp = (SeqTablePtr)sap->data;
3375             SeqTableFree(stp);
3376             break;
3377 
3378     }
3379     return (SeqAnnotPtr)MemFree(sap);
3380 }
3381 
3382 /*****************************************************************************
3383 *
3384 *   SeqAnnotAsnWrite(sap, aip, atp)
3385 *       atp is the current type (if identifier of a parent struct)
3386 *       if atp == NULL, then assumes it stands alone (SeqAnnot ::=)
3387 *
3388 *****************************************************************************/
SeqAnnotAsnWrite(SeqAnnotPtr sap,AsnIoPtr aip,AsnTypePtr orig)3389 NLM_EXTERN Boolean LIBCALL SeqAnnotAsnWrite (SeqAnnotPtr sap, AsnIoPtr aip, AsnTypePtr orig)
3390 {
3391     return SeqAnnotAsnWriteExtra(sap, aip, orig, (ValNodePtr)NULL);
3392 }
3393 
3394 /*****************************************************************************
3395 *
3396 *   SeqAnnotAsnWriteExtra(sap, aip, atp, extras)
3397 *       atp is the current type (if identifier of a parent struct)
3398 *       if atp == NULL, then assumes it stands alone (SeqAnnot ::=)
3399 *       if extras != NULL
3400 *           assumes sap has no data ptr
3401 *           assumes extras is a ValNode chain pointing to SeqFeats from
3402 *              SeqEntryAsnOut()
3403 *       else
3404 *           ignores extras and writes the SeqAnnot
3405 *
3406 *****************************************************************************/
SeqAnnotAsnWriteExtra(SeqAnnotPtr sap,AsnIoPtr aip,AsnTypePtr orig,ValNodePtr extras)3407 static Boolean SeqAnnotAsnWriteExtra (SeqAnnotPtr sap, AsnIoPtr aip, AsnTypePtr orig,
3408                           ValNodePtr extras)
3409 {
3410     DataVal av;
3411     AsnTypePtr atp;
3412     Boolean retval = FALSE;
3413 
3414     if (! loaded)
3415     {
3416         if (! SeqAsnLoad())
3417             return FALSE;
3418     }
3419 
3420     if (aip == NULL)
3421         return FALSE;
3422 
3423     if (! ProgMon("Write SeqAnnot"))
3424         return FALSE;
3425 
3426     if ((aip->spec_version == 3 )  &&    /* ASN3 strip new value */
3427         (sap->type >3))
3428     {
3429         ErrPostEx(SEV_ERROR,0,0,"ASN3: SeqAnnot > 3 stripped");
3430         return TRUE;
3431     }
3432 
3433     atp = AsnLinkType(orig, SEQ_ANNOT);   /* link local tree */
3434     if (atp == NULL) return FALSE;
3435 
3436     if (sap == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
3437 
3438     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
3439 
3440     if (! AsnOpenStruct(aip, atp, (Pointer)sap)) goto erret;
3441 
3442     if (sap->id != NULL)
3443     {
3444         if (! AnnotIdSetAsnWrite(sap->id, aip, SEQ_ANNOT_id, SEQ_ANNOT_id_E)) goto erret;
3445     }
3446     if (sap->db)
3447     {
3448         av.intvalue = (Int4)(sap->db);
3449         if (! AsnWrite(aip, SEQ_ANNOT_db, &av)) goto erret;
3450     }
3451     if (sap->name != NULL)
3452     {
3453         av.ptrvalue = sap->name;
3454         if (! AsnWrite(aip, SEQ_ANNOT_name, &av)) goto erret;
3455     }
3456     if (sap->desc != NULL)
3457     {
3458         if (! AnnotDescrAsnWrite(sap->desc, aip, SEQ_ANNOT_desc)) goto erret;
3459     }
3460     av.ptrvalue = sap->data;
3461     if (! AsnWriteChoice(aip, SEQ_ANNOT_data, (Int2)sap->type, &av)) goto erret;
3462     switch (sap->type)
3463     {
3464         case 1:
3465         if (extras != NULL)  /* from SeqEntryAsnOut */
3466         {
3467         if (! SeqFeatSetAsnWriteExtra((SeqFeatPtr)sap->data, aip, SEQ_ANNOT_data_ftable,
3468                           SEQ_ANNOT_data_ftable_E, extras)) goto erret;
3469         }
3470         else
3471         {
3472         if (! SeqFeatSetAsnWrite((SeqFeatPtr)sap->data, aip, SEQ_ANNOT_data_ftable, SEQ_ANNOT_data_ftable_E)) goto erret;
3473         }
3474             break;
3475         case 2:
3476             if (! SeqAlignSetAsnWrite((SeqAlignPtr)sap->data, aip, SEQ_ANNOT_data_align, SEQ_ANNOT_data_align_E)) goto erret;
3477             break;
3478         case 3:
3479             if (! SeqGraphSetAsnWrite((SeqGraphPtr)sap->data, aip, SEQ_ANNOT_data_graph, SEQ_ANNOT_data_graph_E)) goto erret;
3480             break;
3481         case 4:
3482             if (! SeqIdSetAsnWrite((SeqIdPtr)sap->data, aip, SEQ_ANNOT_data_ids, SEQ_ANNOT_data_ids_E)) goto erret;
3483             break;
3484         case 5:
3485             if (! SeqLocSetAsnWrite((SeqLocPtr)sap->data, aip, SEQ_ANNOT_data_locs, SEQ_ANNOT_data_locs_E)) goto erret;
3486             break;
3487         case 6:
3488             if (! SeqTableAsnWrite((SeqTablePtr)sap->data, aip, SEQ_ANNOT_data_seq_table)) goto erret;
3489             break;
3490     }
3491 
3492     if (! AsnCloseStruct(aip, atp, (Pointer)sap)) goto erret;
3493     retval = TRUE;
3494 erret:
3495     AsnUnlinkType(orig);       /* unlink local tree */
3496     return retval;
3497 }
3498 
3499 /*****************************************************************************
3500 *
3501 *   SeqAnnotAsnRead(aip, atp)
3502 *       atp is the current type (if identifier of a parent struct)
3503 *            assumption is readIdent has occurred
3504 *       if atp == NULL, then assumes it stands alone and read ident
3505 *            has not occurred.
3506 *
3507 *****************************************************************************/
SeqAnnotAsnRead(AsnIoPtr aip,AsnTypePtr orig)3508 NLM_EXTERN SeqAnnotPtr LIBCALL SeqAnnotAsnRead (AsnIoPtr aip, AsnTypePtr orig)
3509 {
3510     DataVal av;
3511     AsnTypePtr atp;
3512     SeqAnnotPtr sap=NULL;
3513 
3514     if (! loaded)
3515     {
3516         if (! SeqAsnLoad())
3517             return sap;
3518     }
3519 
3520     if (aip == NULL)
3521         return sap;
3522 
3523     if (! ProgMon("Read SeqAnnot"))
3524         return sap;
3525 
3526     if (orig == NULL)           /* SeqAnnot ::= (self contained) */
3527         atp = AsnReadId(aip, amp, SEQ_ANNOT);
3528     else
3529         atp = AsnLinkType(orig, SEQ_ANNOT);    /* link in local tree */
3530     if (atp == NULL) return sap;
3531 
3532     sap = SeqAnnotNew();
3533     if (sap == NULL) goto erret;
3534 
3535     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
3536 
3537     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
3538     if (atp == SEQ_ANNOT_id)
3539     {
3540         sap->id = AnnotIdSetAsnRead(aip, atp, SEQ_ANNOT_id_E);
3541         if (sap->id == NULL) goto erret;
3542         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
3543     }
3544     if (atp == SEQ_ANNOT_db)
3545     {
3546         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
3547         sap->db = (Uint1)av.intvalue;
3548         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
3549     }
3550     if (atp == SEQ_ANNOT_name)
3551     {
3552         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
3553         sap->name = (CharPtr)av.ptrvalue;
3554         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
3555     }
3556     if (atp == SEQ_ANNOT_desc)
3557     {
3558         sap->desc = AnnotDescrAsnRead(aip, atp);
3559         if (sap->desc == NULL) goto erret;
3560         atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;
3561     }
3562 
3563     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* CHOICE */
3564     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;    /* type of choice */
3565     if (atp == SEQ_ANNOT_data_ftable)
3566     {
3567         sap->type = 1;
3568         sap->data = (Pointer) SeqFeatSetAsnRead(aip, atp, SEQ_ANNOT_data_ftable_E);
3569     }
3570     else if (atp == SEQ_ANNOT_data_align)
3571     {
3572         sap->type = 2;
3573         sap->data = (Pointer) SeqAlignSetAsnRead(aip, atp, SEQ_ANNOT_data_align_E);
3574     }
3575     else if (atp == SEQ_ANNOT_data_graph)
3576     {
3577         sap->type = 3;
3578         sap->data = (Pointer) SeqGraphSetAsnRead(aip, atp, SEQ_ANNOT_data_graph_E);
3579     }
3580     else if (atp == SEQ_ANNOT_data_ids)
3581     {
3582         sap->type = 4;
3583         sap->data = (Pointer) SeqIdSetAsnRead(aip, atp, SEQ_ANNOT_data_ids_E);
3584 
3585         if (aip->spec_version == 3)    /* ASN3 strip new value */
3586         {
3587             ErrPostEx(SEV_ERROR,0,0,"ASN3: SeqFeat.ids stripped");
3588             SeqIdSetFree((SeqIdPtr)(sap->data));
3589             sap->data = NULL;
3590             sap->type = 3;
3591         }
3592     }
3593     else if (atp == SEQ_ANNOT_data_locs)
3594     {
3595         sap->type = 5;
3596         sap->data = (Pointer) SeqLocSetAsnRead(aip, atp, SEQ_ANNOT_data_locs_E);
3597 
3598         if (aip->spec_version == 3)    /* ASN3 strip new value */
3599         {
3600             ErrPostEx(SEV_ERROR,0,0,"ASN3: SeqFeat.locs stripped");
3601             SeqLocSetFree((SeqLocPtr)(sap->data));
3602             sap->data = NULL;
3603             sap->type = 3;
3604         }
3605     }
3606     else if (atp == SEQ_ANNOT_data_seq_table)
3607     {
3608         sap->type = 6;
3609         sap->data = (Pointer) SeqTableAsnRead(aip, atp);
3610     }
3611 
3612     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;   /* end struct */
3613     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
3614 ret:
3615     AsnUnlinkType(orig);       /* unlink local tree */
3616     return sap;
3617 erret:
3618     aip->io_failure = TRUE;
3619     sap = SeqAnnotFree(sap);
3620     goto ret;
3621 }
3622 
3623 /*****************************************************************************
3624 *
3625 *   SeqAnnotSetAsnWrite(sap, aip, set, element)
3626 *       atp is the current type (if identifier of a parent struct)
3627 *       if atp == NULL, then assumes it stands alone (SeqAnnot ::=)
3628 *
3629 *****************************************************************************/
SeqAnnotSetAsnWrite(SeqAnnotPtr sap,AsnIoPtr aip,AsnTypePtr set,AsnTypePtr element)3630 NLM_EXTERN Boolean LIBCALL SeqAnnotSetAsnWrite (SeqAnnotPtr sap, AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
3631 {
3632     return SeqAnnotSetAsnWriteExtra(sap, aip, set, element, TRUE);
3633 }
3634 
3635 /*****************************************************************************
3636 *
3637 *   SeqAnnotSetExtraCheck(aip, set, element)
3638 *       atp is the current type (if identifier of a parent struct)
3639 *       if atp == NULL, then assumes it stands alone (SeqAnnot ::=)
3640 *       called to check for extra features from SeqEntryAsnOut when sap is NULL
3641 *
3642 *****************************************************************************/
SeqAnnotSetExtraCheck(AsnIoPtr aip,AsnTypePtr set,AsnTypePtr element)3643 NLM_EXTERN Boolean LIBCALL SeqAnnotSetExtraCheck (AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
3644 {
3645     return SeqAnnotSetAsnWriteExtra((SeqAnnotPtr)NULL, aip, set, element, FALSE);
3646 }
3647 
3648 /*****************************************************************************
3649 *
3650 *   SeqAnnotSetAsnWriteExtra(sap, aip, set, element, sap_not_null)
3651 *       atp is the current type (if identifier of a parent struct)
3652 *       if atp == NULL, then assumes it stands alone (SeqAnnot ::=)
3653 *
3654 *****************************************************************************/
SeqAnnotSetAsnWriteExtra(SeqAnnotPtr sap,AsnIoPtr aip,AsnTypePtr set,AsnTypePtr element,Boolean sap_not_null)3655 static Boolean SeqAnnotSetAsnWriteExtra (SeqAnnotPtr sap, AsnIoPtr aip, AsnTypePtr set,
3656                      AsnTypePtr element, Boolean sap_not_null)
3657 {
3658     AsnTypePtr atp;
3659     SeqAnnotPtr oldsap = NULL, tsap = NULL;
3660     Boolean retval = FALSE, had_extra = FALSE;
3661     AsnOptionPtr aopp;
3662     ValNodePtr extras = NULL;
3663     SeqAnnot sa;
3664 
3665     if (! loaded)
3666     {
3667         if (! SeqAsnLoad())
3668             return FALSE;
3669     }
3670 
3671     if (aip == NULL)
3672         return FALSE;
3673 
3674     aopp = AsnIoOptionGet(aip, OP_NCBIOBJSEQ, CHECK_EXTRA_FEAT, NULL);
3675     if (aopp != NULL)
3676     {
3677         had_extra = TRUE;
3678         extras = aopp->data.ptrvalue;
3679         tsap = &sa;
3680         MemSet(tsap, 0, sizeof(SeqAnnot));
3681         tsap->type = 1;  /* feature table */
3682     }
3683     else if ((sap == NULL) && (! sap_not_null))  /* nothing to write */
3684         return TRUE;
3685 
3686     atp = AsnLinkType(element, SEQ_ANNOT);   /* link local tree */
3687     if (atp == NULL) return FALSE;
3688 
3689     if (sap_not_null)
3690         oldsap = sap;
3691     else
3692         oldsap = tsap;
3693 
3694     if (oldsap == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
3695 
3696     if (! AsnOpenStruct(aip, set, (Pointer)oldsap)) goto erret;
3697 
3698     while (sap != NULL)
3699     {
3700         if (! SeqAnnotAsnWrite(sap, aip, atp)) goto erret;
3701         sap = sap->next;
3702     }
3703 
3704     if (tsap != NULL)
3705     {
3706     if (! SeqAnnotAsnWriteExtra(tsap, aip, atp, extras)) goto erret;
3707     }
3708 
3709     if (! AsnCloseStruct(aip, set, (Pointer)oldsap)) goto erret;
3710     retval = TRUE;
3711 erret:
3712     if (had_extra)
3713         AsnIoOptionFree(aip, OP_NCBIOBJSEQ, CHECK_EXTRA_FEAT);
3714     AsnUnlinkType(element);       /* unlink local tree */
3715     return retval;
3716 }
3717 
3718 /*****************************************************************************
3719 *
3720 *   SeqAnnotSetAsnRead(aip, set, element)
3721 *       atp is the current type (if identifier of a parent struct)
3722 *            assumption is readIdent has occurred
3723 *       if atp == NULL, then assumes it stands alone and read ident
3724 *            has not occurred.
3725 *
3726 *****************************************************************************/
SeqAnnotSetAsnRead(AsnIoPtr aip,AsnTypePtr set,AsnTypePtr element)3727 NLM_EXTERN SeqAnnotPtr LIBCALL SeqAnnotSetAsnRead (AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
3728 {
3729     DataVal av;
3730     AsnTypePtr atp;
3731     SeqAnnotPtr sap, first=NULL, curr;
3732 
3733     if (! loaded)
3734     {
3735         if (! SeqAsnLoad())
3736             return first;
3737     }
3738 
3739     if (aip == NULL)
3740         return first;
3741 
3742     first = NULL;
3743     curr = NULL;
3744     AsnLinkType(element, SEQ_ANNOT);    /* link in local tree */
3745 
3746     if (AsnReadVal(aip, set, &av) <= 0) goto erret;   /* read the start struct */
3747 
3748     atp = set;
3749 
3750     while ((atp = AsnReadId(aip, amp, atp)) != set)
3751     {
3752         if (atp == NULL) goto erret;
3753         sap = SeqAnnotAsnRead(aip, atp);
3754         if (sap != NULL) {
3755             if (first == NULL)
3756                 first = sap;
3757             else
3758                 curr->next = sap;
3759             curr = sap;
3760         }
3761     }
3762     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end SET OF */
3763     if (first == NULL)
3764         ErrPostEx(SEV_WARNING, 0,0, "Empty SET OF Seq-annot.  line %ld", (long) aip->linenumber);
3765 ret:
3766     AsnUnlinkType(element);       /* unlink local tree */
3767     return first;
3768 erret:
3769     while (first != NULL)
3770     {
3771         curr = first;
3772         first = curr->next;
3773         SeqAnnotFree(curr);
3774     }
3775     goto ret;
3776 }
3777 
3778 /*****************************************************************************
3779 *
3780 *   BioseqLoad()
3781 *       sets up a program to read/write and manipulate Bioseqs
3782 *
3783 *****************************************************************************/
BioseqLoad(void)3784 NLM_EXTERN Boolean LIBCALL BioseqLoad (void)
3785 {
3786     if (! SeqAsnLoad())
3787         return FALSE;
3788 
3789     if (SeqCodeSetLoad() == NULL)
3790     {
3791         ErrPostEx(SEV_ERROR, 0,0,  "SeqCodeSetLoad failed");
3792         return FALSE;
3793     }
3794 
3795     return TRUE;
3796 }
3797 
3798 /*****************************************************************************
3799 *
3800 *   SeqHistNew()
3801 *
3802 *****************************************************************************/
SeqHistNew(void)3803 NLM_EXTERN SeqHistPtr LIBCALL SeqHistNew (void)
3804 {
3805     SeqHistPtr shp;
3806     shp = (SeqHistPtr)MemNew(sizeof(SeqHist));
3807     return shp;
3808 }
3809 
3810 /*****************************************************************************
3811 *
3812 *   SeqHistFree(shp)
3813 *       Frees a SeqHist and associated data
3814 *
3815 *****************************************************************************/
SeqHistFree(SeqHistPtr shp)3816 NLM_EXTERN SeqHistPtr LIBCALL SeqHistFree (SeqHistPtr shp)
3817 {
3818     SeqAlignPtr sap, sapnext;
3819 
3820     if (shp == NULL)
3821         return shp;
3822 
3823     for (sap = shp->assembly; sap != NULL; sap = sapnext)
3824     {
3825         sapnext = sap->next;
3826         SeqAlignFree(sap);
3827     }
3828     DateFree(shp->replace_date);
3829     SeqIdSetFree(shp->replace_ids);
3830     DateFree(shp->replaced_by_date);
3831     SeqIdSetFree(shp->replaced_by_ids);
3832     DateFree(shp->deleted_date);
3833 
3834     return (SeqHistPtr)MemFree(shp);
3835 }
3836 
3837 /*****************************************************************************
3838 *
3839 *   SeqHistAsnWrite(shp, aip, atp)
3840 *       atp is the current type (if identifier of a parent struct)
3841 *       if atp == NULL, then assumes it stands alone (SeqHist ::=)
3842 *
3843 *****************************************************************************/
SeqHistAsnWrite(SeqHistPtr shp,AsnIoPtr aip,AsnTypePtr orig)3844 NLM_EXTERN Boolean LIBCALL SeqHistAsnWrite (SeqHistPtr shp, AsnIoPtr aip, AsnTypePtr orig)
3845 {
3846     DataVal dv;
3847     AsnTypePtr atp;
3848     Boolean retval = FALSE;
3849     SeqAlignPtr sap;
3850     Int2 choice, ctr = 0;
3851 
3852     if (! loaded)
3853     {
3854         if (! SeqAsnLoad())
3855             return FALSE;
3856     }
3857 
3858     if (aip == NULL)
3859         return FALSE;
3860 
3861     atp = AsnLinkType(orig, SEQ_HIST);   /* link local tree */
3862     if (atp == NULL) return FALSE;
3863 
3864     if (shp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
3865 
3866     if (! AsnOpenStruct(aip, atp, (Pointer)shp)) goto erret;
3867     if (shp->assembly != NULL)
3868     {
3869        if (! AsnOpenStruct(aip, SEQ_HIST_assembly, (Pointer)shp->assembly)) goto erret;
3870        sap = shp->assembly;
3871        while (sap != NULL)
3872        {
3873            if (! SeqAlignAsnWrite(sap, aip, SEQ_HIST_assembly_E)) goto erret;
3874            sap = sap->next;
3875             ctr++;
3876             if (ctr == 10)
3877             {
3878                 if (! ProgMon("Write SeqHist"))
3879                     goto erret;
3880                 ctr = 0;
3881             }
3882        }
3883        if (! AsnCloseStruct(aip, SEQ_HIST_assembly, (Pointer)shp->assembly)) goto erret;
3884     }
3885 
3886     if ((shp->replace_date != NULL) || (shp->replace_ids != NULL))
3887     {
3888        if (! AsnOpenStruct(aip, SEQ_HIST_replaces, (Pointer)shp)) goto erret;
3889         if (shp->replace_date != NULL)
3890         {
3891             if (! DateAsnWrite(shp->replace_date, aip, SEQ_HIST_REC_date))
3892                 goto erret;
3893         }
3894         if (shp->replace_ids != NULL)
3895         {
3896             if (! SeqIdSetAsnWrite(shp->replace_ids, aip, SEQ_HIST_REC_ids, SEQ_HIST_REC_ids_E))
3897                 goto erret;
3898         }
3899        if (! AsnCloseStruct(aip, SEQ_HIST_replaces, (Pointer)shp)) goto erret;
3900     }
3901 
3902     if ((shp->replaced_by_date != NULL) || (shp->replaced_by_ids != NULL))
3903     {
3904        if (! AsnOpenStruct(aip, SEQ_HIST_replaced_by, (Pointer)shp)) goto erret;
3905         if (shp->replaced_by_date != NULL)
3906         {
3907             if (! DateAsnWrite(shp->replaced_by_date, aip, SEQ_HIST_REC_date))
3908                 goto erret;
3909         }
3910         if (shp->replaced_by_ids != NULL)
3911         {
3912             if (! SeqIdSetAsnWrite(shp->replaced_by_ids, aip, SEQ_HIST_REC_ids, SEQ_HIST_REC_ids_E))
3913                 goto erret;
3914         }
3915        if (! AsnCloseStruct(aip, SEQ_HIST_replaced_by, (Pointer)shp)) goto erret;
3916     }
3917 
3918     if ((shp->deleted) || (shp->deleted_date != NULL))
3919     {
3920         dv.ptrvalue = (Pointer)shp;
3921         if (shp->deleted)
3922             choice = 0;
3923         else
3924             choice = 1;
3925         if (! AsnWriteChoice(aip, SEQ_HIST_deleted, choice, &dv)) goto erret;   /* CHOICE */
3926         if (shp->deleted)
3927         {
3928             dv.boolvalue = TRUE;
3929             if (! AsnWrite(aip, SEQ_HIST_deleted_bool, &dv)) goto erret;
3930         }
3931         else
3932         {
3933             if (! DateAsnWrite(shp->deleted_date, aip, SEQ_HIST_deleted_date))
3934                 goto erret;
3935         }
3936     }
3937     if (! AsnCloseStruct(aip, atp, (Pointer)shp)) goto erret;
3938     retval = TRUE;
3939 erret:
3940     AsnUnlinkType(orig);       /* unlink local tree */
3941     return retval;
3942 }
3943 
3944 /*****************************************************************************
3945 *
3946 *   SeqHistAsnRead(aip, atp)
3947 *       atp is the current type (if identifier of a parent struct)
3948 *            assumption is readIdent has occurred
3949 *       if atp == NULL, then assumes it stands alone and read ident
3950 *            has not occurred.
3951 *
3952 *****************************************************************************/
SeqHistAsnRead(AsnIoPtr aip,AsnTypePtr orig)3953 NLM_EXTERN SeqHistPtr LIBCALL SeqHistAsnRead (AsnIoPtr aip, AsnTypePtr orig)
3954 {
3955     DataVal dv;
3956     AsnTypePtr atp, oldatp;
3957     SeqHistPtr shp=NULL;
3958     SeqAlignPtr sap, saplast = NULL;
3959     Int2 in_replace = -1, ctr = 0;
3960     DatePtr date;
3961     SeqIdPtr sip;
3962 
3963     if (! loaded)
3964     {
3965         if (! SeqAsnLoad())
3966             return shp;
3967     }
3968 
3969     if (aip == NULL)
3970         return shp;
3971 
3972     if (orig == NULL)           /* SeqHist ::= (self contained) */
3973         atp = AsnReadId(aip, amp, SEQ_HIST);
3974     else
3975         atp = AsnLinkType(orig, SEQ_HIST);    /* link in local tree */
3976     oldatp = atp;
3977     if (atp == NULL) return shp;
3978 
3979     shp = SeqHistNew();
3980     if (shp == NULL) goto erret;
3981 
3982     if (AsnReadVal(aip, atp, &dv) <= 0) goto erret;   /* read the start struct */
3983 
3984     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
3985     {
3986         if (atp == NULL) goto erret;
3987         if (atp == SEQ_HIST_assembly_E)
3988         {
3989             sap = SeqAlignAsnRead(aip, atp);
3990             if (sap == NULL) goto erret;
3991             if (saplast == NULL)
3992                 shp->assembly = sap;
3993             else
3994                 saplast->next = sap;
3995             saplast = sap;
3996             ctr++;
3997             if (ctr == 10)
3998             {
3999                 if (! ProgMon("Read SeqHist"))
4000                     goto erret;
4001                 ctr = 0;
4002             }
4003         }
4004         else if (atp == SEQ_HIST_REC_date)
4005         {
4006             date = DateAsnRead(aip, atp);
4007             if (date == NULL) goto erret;
4008             if (in_replace == 1)
4009                 shp->replace_date = date;
4010             else
4011                 shp->replaced_by_date = date;
4012         }
4013         else if (atp == SEQ_HIST_REC_ids)
4014         {
4015             sip = SeqIdSetAsnRead(aip, atp, SEQ_HIST_REC_ids_E);
4016             if (sip == NULL) goto erret;
4017             if (in_replace == 1)
4018                 shp->replace_ids = sip;
4019             else
4020                 shp->replaced_by_ids = sip;
4021         }
4022         else if (atp == SEQ_HIST_deleted_date)
4023         {
4024             date = DateAsnRead(aip, atp);
4025             if (date == NULL) goto erret;
4026             shp->deleted_date = date;
4027         }
4028         else
4029         {
4030             if (AsnReadVal(aip, atp, &dv) <= 0) goto erret;
4031             if (atp == SEQ_HIST_replaces)
4032                 in_replace *= -1;
4033             else if (atp == SEQ_HIST_deleted_bool)
4034                 shp->deleted = dv.boolvalue;
4035         }
4036     }
4037     if (AsnReadVal(aip, atp, &dv) <= 0) goto erret;    /* end struct */
4038 ret:
4039     AsnUnlinkType(orig);       /* unlink local tree */
4040     return shp;
4041 erret:
4042     shp = SeqHistFree(shp);
4043     goto ret;
4044 }
4045 
4046 /*****************************************************************************
4047 *
4048 *   MolInfoNew()
4049 *
4050 *****************************************************************************/
MolInfoNew(void)4051 NLM_EXTERN MolInfoPtr LIBCALL MolInfoNew (void)
4052 {
4053     return (MolInfoPtr)MemNew(sizeof(MolInfo));
4054 }
4055 
4056 /*****************************************************************************
4057 *
4058 *   MolInfoFree(mip)
4059 *       Frees a MolInfo and associated data
4060 *
4061 *****************************************************************************/
MolInfoFree(MolInfoPtr mip)4062 NLM_EXTERN MolInfoPtr LIBCALL MolInfoFree (MolInfoPtr mip)
4063 {
4064   if (mip == NULL)
4065       return mip;
4066 
4067   MemFree(mip->techexp);
4068   MemFree(mip->gbmoltype);
4069 
4070   return (MolInfoPtr)MemFree(mip);
4071 }
4072 
4073 /*****************************************************************************
4074 *
4075 *   MolInfoAsnWrite(mip, aip, atp)
4076 *       atp is the current type (if identifier of a parent struct)
4077 *       if atp == NULL, then assumes it stands alone (MolInfo ::=)
4078 *
4079 *****************************************************************************/
MolInfoAsnWrite(MolInfoPtr mip,AsnIoPtr aip,AsnTypePtr orig)4080 NLM_EXTERN Boolean LIBCALL MolInfoAsnWrite (MolInfoPtr mip, AsnIoPtr aip, AsnTypePtr orig)
4081 {
4082     DataVal av;
4083     AsnTypePtr atp;
4084     Boolean retval = FALSE;
4085 
4086     if (! loaded)
4087     {
4088         if (! SeqAsnLoad())
4089             return FALSE;
4090     }
4091 
4092     if (aip == NULL)
4093         return FALSE;
4094 
4095     atp = AsnLinkType(orig, MOLINFO);   /* link local tree */
4096     if (atp == NULL) return FALSE;
4097 
4098     if (mip == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
4099 
4100     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
4101 
4102     if (! AsnOpenStruct(aip, atp, (Pointer)mip)) goto erret;
4103 
4104     if (mip->biomol)
4105     {
4106         av.intvalue = (Int4)mip->biomol;
4107         if (! AsnWrite(aip, MOLINFO_biomol, &av)) goto erret;
4108     }
4109 
4110     if (mip->tech)
4111     {
4112         av.intvalue = (Int4)mip->tech;
4113         if (! AsnWrite(aip, MOLINFO_tech, &av)) goto erret;
4114     }
4115 
4116     if (mip->techexp != NULL)
4117     {
4118         av.ptrvalue = (Pointer)mip->techexp;
4119         if (! AsnWrite(aip, MOLINFO_techexp, &av)) goto erret;
4120     }
4121 
4122     if (mip->completeness)
4123     {
4124         av.intvalue = (Int4)mip->completeness;
4125         if (! AsnWrite(aip, MOLINFO_completeness, &av)) goto erret;
4126     }
4127 
4128     if (mip->gbmoltype != NULL)
4129     {
4130         av.ptrvalue = (Pointer)mip->gbmoltype;
4131         if (! AsnWrite(aip, MOLINFO_gbmoltype, &av)) goto erret;
4132     }
4133 
4134     if (! AsnCloseStruct(aip, atp, (Pointer)mip)) goto erret;
4135     retval = TRUE;
4136 erret:
4137     AsnUnlinkType(orig);       /* unlink local tree */
4138     return retval;
4139 }
4140 
4141 /*****************************************************************************
4142 *
4143 *   MolInfoAsnRead(aip, atp)
4144 *       atp is the current type (if identifier of a parent struct)
4145 *            assumption is readIdent has occurred
4146 *       if atp == NULL, then assumes it stands alone and read ident
4147 *            has not occurred.
4148 *
4149 *****************************************************************************/
MolInfoAsnRead(AsnIoPtr aip,AsnTypePtr orig)4150 NLM_EXTERN MolInfoPtr LIBCALL MolInfoAsnRead (AsnIoPtr aip, AsnTypePtr orig)
4151 {
4152     DataVal av;
4153     AsnTypePtr atp, oldatp;
4154     MolInfoPtr mip=NULL;
4155 
4156     if (! loaded)
4157     {
4158         if (! SeqAsnLoad())
4159             return mip;
4160     }
4161 
4162     if (aip == NULL)
4163         return mip;
4164 
4165     if (orig == NULL)           /* MolInfo ::= (self contained) */
4166         atp = AsnReadId(aip, amp, MOLINFO);
4167     else
4168         atp = AsnLinkType(orig, MOLINFO);    /* link in local tree */
4169     oldatp = atp;
4170     if (atp == NULL) return mip;
4171 
4172     mip = MolInfoNew();
4173     if (mip == NULL) goto erret;
4174 
4175     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
4176 
4177     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
4178     {
4179         if (atp == NULL) goto erret;
4180         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
4181         if (atp == MOLINFO_biomol)
4182             mip->biomol = (Uint1)av.intvalue;
4183         else if (atp == MOLINFO_tech)
4184             mip->tech = (Uint1)av.intvalue;
4185         else if (atp == MOLINFO_techexp)
4186             mip->techexp = (CharPtr)av.ptrvalue;
4187         else if (atp == MOLINFO_completeness)
4188             mip->completeness = (Uint1)av.intvalue;
4189         else if (atp == MOLINFO_gbmoltype)
4190             mip->gbmoltype = (CharPtr)av.ptrvalue;
4191     }
4192     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end struct */
4193 ret:
4194     AsnUnlinkType(orig);       /* unlink local tree */
4195     return mip;
4196 erret:
4197     mip = MolInfoFree(mip);
4198     goto ret;
4199 }
4200 
4201 /*****************************************************************************
4202 *
4203 *   SeqLitNew()
4204 *
4205 *****************************************************************************/
SeqLitNew(void)4206 NLM_EXTERN SeqLitPtr LIBCALL SeqLitNew (void)
4207 {
4208     SeqLitPtr slp;
4209 
4210     slp = (SeqLitPtr)MemNew(sizeof(SeqLit));
4211     return slp;
4212 }
4213 
4214 /*****************************************************************************
4215 *
4216 *   SeqLitFree(slp)
4217 *       Frees a SeqLit and associated data
4218 *
4219 *****************************************************************************/
SeqLitFree(SeqLitPtr slp)4220 NLM_EXTERN SeqLitPtr LIBCALL SeqLitFree (SeqLitPtr slp)
4221 {
4222     if (slp == NULL)
4223         return slp;
4224 
4225     SeqDataFree(slp->seq_data, slp->seq_data_type);
4226     IntFuzzFree(slp->fuzz);
4227     return (SeqLitPtr)MemFree(slp);
4228 }
4229 /*****************************************************************************
4230 *
4231 *   SeqLitAsnWrite(slp, aip, atp)
4232 *       atp is the current type (if identifier of a parent struct)
4233 *       if atp == NULL, then assumes it stands alone (SeqLit ::=)
4234 *
4235 *****************************************************************************/
SeqLitAsnWrite(SeqLitPtr slp,AsnIoPtr aip,AsnTypePtr orig)4236 NLM_EXTERN Boolean LIBCALL SeqLitAsnWrite (SeqLitPtr slp, AsnIoPtr aip, AsnTypePtr orig)
4237 {
4238     DataVal av;
4239     AsnTypePtr atp;
4240     Boolean retval = FALSE;
4241 
4242     if (! loaded)
4243     {
4244         if (! SeqAsnLoad())
4245             return FALSE;
4246     }
4247 
4248     if (aip == NULL)
4249         return FALSE;
4250 
4251     atp = AsnLinkType(orig, SEQ_LITERAL);   /* link local tree */
4252     if (atp == NULL) return FALSE;
4253 
4254     if (slp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
4255 
4256     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
4257 
4258     if (! AsnOpenStruct(aip, atp, (Pointer)slp)) goto erret;
4259 
4260     av.intvalue = slp->length;
4261     if (! AsnWrite(aip, SEQ_LITERAL_length, &av)) goto erret;
4262 
4263     if (slp->fuzz != NULL)
4264     {
4265         if (! IntFuzzAsnWrite(slp->fuzz, aip, SEQ_LITERAL_fuzz)) goto erret;
4266     }
4267 
4268     if (slp->seq_data != NULL)
4269     {
4270         if (aip->type & ASNIO_XML)
4271     {
4272             if (! SeqDataAsnWriteXML(slp->seq_data, slp->seq_data_type, aip, SEQ_LITERAL_seq_data, slp->length)) goto erret;
4273     }
4274         else if (! SeqDataAsnWrite(slp->seq_data, slp->seq_data_type, aip, SEQ_LITERAL_seq_data)) goto erret;
4275     }
4276 
4277     if (! AsnCloseStruct(aip, atp, (Pointer)slp)) goto erret;
4278     retval = TRUE;
4279 erret:
4280     AsnUnlinkType(orig);       /* unlink local tree */
4281     return retval;
4282 }
4283 
4284 /*****************************************************************************
4285 *
4286 *   SeqLitAsnRead(aip, atp)
4287 *       atp is the current type (if identifier of a parent struct)
4288 *            assumption is readIdent has occurred
4289 *       if atp == NULL, then assumes it stands alone and read ident
4290 *            has not occurred.
4291 *
4292 *****************************************************************************/
SeqLitAsnRead(AsnIoPtr aip,AsnTypePtr orig)4293 NLM_EXTERN SeqLitPtr LIBCALL SeqLitAsnRead (AsnIoPtr aip, AsnTypePtr orig)
4294 {
4295     DataVal av;
4296     AsnTypePtr atp, oldatp;
4297     SeqLitPtr slp=NULL;
4298 
4299     if (! loaded)
4300     {
4301         if (! SeqAsnLoad())
4302             return slp;
4303     }
4304 
4305     if (aip == NULL)
4306         return slp;
4307 
4308     if (orig == NULL)           /* SeqLit ::= (self contained) */
4309         atp = AsnReadId(aip, amp, SEQ_LITERAL);
4310     else
4311         atp = AsnLinkType(orig, SEQ_LITERAL);    /* link in local tree */
4312     oldatp = atp;
4313     if (atp == NULL) return slp;
4314 
4315     slp = SeqLitNew();
4316     if (slp == NULL) goto erret;
4317 
4318     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
4319 
4320     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
4321     {
4322         if (atp == NULL) goto erret;
4323         if (atp == SEQ_LITERAL_length)
4324         {
4325             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
4326             slp->length = av.intvalue;
4327         }
4328         else if (atp == SEQ_LITERAL_fuzz)
4329         {
4330             slp->fuzz = IntFuzzAsnRead(aip, atp);
4331             if (slp->fuzz == NULL) goto erret;
4332         }
4333         else if (atp == SEQ_LITERAL_seq_data)
4334         {
4335             slp->seq_data = SeqDataAsnRead(aip, atp, &(slp->seq_data_type), slp->length);
4336             if (slp->seq_data == NULL) goto erret;
4337         }
4338     }
4339     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* end struct */
4340 ret:
4341     AsnUnlinkType(orig);       /* unlink local tree */
4342     return slp;
4343 erret:
4344     slp = SeqLitFree(slp);
4345     goto ret;
4346 }
4347 
4348 /*****************************************************************************
4349 *
4350 *   DeltaSeqFree(anp)
4351 *       Frees one DeltaSeq and associated data
4352 *
4353 *****************************************************************************/
DeltaSeqFree(DeltaSeqPtr dsp)4354 NLM_EXTERN DeltaSeqPtr LIBCALL DeltaSeqFree (DeltaSeqPtr dsp)
4355 {
4356     if (dsp == NULL)
4357         return dsp;
4358 
4359     switch (dsp->choice)
4360     {
4361         case 1:
4362             SeqLocFree((SeqLocPtr)(dsp->data.ptrvalue));
4363             break;
4364         case 2:
4365             SeqLitFree((SeqLitPtr)(dsp->data.ptrvalue));
4366             break;
4367     }
4368     return (DeltaSeqPtr)MemFree(dsp);
4369 }
4370 
4371 /*****************************************************************************
4372 *
4373 *   DeltaSeqAsnWrite(dsp, aip, atp)
4374 *       atp is the current type (if identifier of a parent struct)
4375 *       if atp == NULL, then assumes it stands alone (DeltaSeq ::=)
4376 *
4377 *****************************************************************************/
DeltaSeqAsnWrite(DeltaSeqPtr dsp,AsnIoPtr aip,AsnTypePtr orig)4378 NLM_EXTERN Boolean LIBCALL DeltaSeqAsnWrite (DeltaSeqPtr dsp, AsnIoPtr aip, AsnTypePtr orig)
4379 {
4380     DataVal av;
4381     AsnTypePtr atp;
4382     Boolean retval = FALSE;
4383 
4384     if (! loaded)
4385     {
4386         if (! SeqAsnLoad())
4387             return FALSE;
4388     }
4389 
4390     if (aip == NULL)
4391         return FALSE;
4392 
4393     atp = AsnLinkType(orig, DELTA_SEQ);   /* link local tree */
4394     if (atp == NULL)
4395         return FALSE;
4396 
4397     if (dsp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
4398 
4399     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
4400 
4401     av.ptrvalue = (Pointer)dsp;
4402     if (! AsnWriteChoice(aip, atp, (Int2)dsp->choice, &av))
4403         goto erret;
4404 
4405     switch (dsp->choice)
4406     {
4407         case 1:
4408             retval = SeqLocAsnWrite((SeqLocPtr)(dsp->data.ptrvalue), aip, DELTA_SEQ_loc);
4409             break;
4410         case 2:
4411             retval = SeqLitAsnWrite((SeqLitPtr)(dsp->data.ptrvalue), aip, DELTA_SEQ_literal);
4412             break;
4413     }
4414 erret:
4415     AsnUnlinkType(orig);       /* unlink local tree */
4416     return retval;
4417 }
4418 
4419 /*****************************************************************************
4420 *
4421 *   DeltaSeqAsnRead(aip, atp)
4422 *       atp is the current type (if identifier of a parent struct)
4423 *            assumption is readIdent has occurred
4424 *       if atp == NULL, then assumes it stands alone and read ident
4425 *            has not occurred.
4426 *
4427 *****************************************************************************/
DeltaSeqAsnRead(AsnIoPtr aip,AsnTypePtr orig)4428 NLM_EXTERN DeltaSeqPtr LIBCALL DeltaSeqAsnRead (AsnIoPtr aip, AsnTypePtr orig)
4429 {
4430     DataVal av;
4431     AsnTypePtr atp;
4432     DeltaSeqPtr dsp=NULL;
4433 
4434     if (! loaded)
4435     {
4436         if (! SeqAsnLoad())
4437             return dsp;
4438     }
4439 
4440     if (aip == NULL)
4441         return dsp;
4442 
4443     if (orig == NULL)           /* DeltaSeq ::= (self contained) */
4444         atp = AsnReadId(aip, amp, DELTA_SEQ);
4445     else
4446         atp = AsnLinkType(orig, DELTA_SEQ);    /* link in local tree */
4447     if (atp == NULL)
4448         return dsp;
4449 
4450     dsp = ValNodeNew(NULL);
4451     if (dsp == NULL)
4452         goto erret;
4453 
4454     if (AsnReadVal(aip, atp, &av) <= 0)  /* read the CHOICE value (nothing) */
4455         goto erret;
4456     atp = AsnReadId(aip, amp, atp);  /* find the choice */
4457     if (atp == NULL)
4458         goto erret;
4459 
4460     if (atp == DELTA_SEQ_loc)
4461     {
4462         dsp->choice = 1;
4463         dsp->data.ptrvalue = (Pointer) SeqLocAsnRead(aip, atp);
4464     }
4465     else if (atp == DELTA_SEQ_literal)
4466     {
4467         dsp->choice = 2;
4468         dsp->data.ptrvalue = (Pointer) SeqLitAsnRead(aip, atp);
4469     }
4470 
4471     if (dsp->data.ptrvalue == NULL) goto erret;
4472 ret:
4473     AsnUnlinkType(orig);       /* unlink local tree */
4474     return dsp;
4475 erret:
4476     dsp = DeltaSeqFree(dsp);
4477     goto ret;
4478 }
4479 
4480 /*****************************************************************************
4481 *
4482 *   DeltaSeqSetAsnWrite(dsp, aip, atp, set, element)
4483 *       atp is the current type (if identifier of a parent struct)
4484 *       if atp == NULL, then assumes it stands alone (DeltaSeqSet ::=)
4485 *
4486 *****************************************************************************/
DeltaSeqSetAsnWrite(DeltaSeqPtr dsp,AsnIoPtr aip,AsnTypePtr set,AsnTypePtr element)4487 NLM_EXTERN Boolean LIBCALL DeltaSeqSetAsnWrite (DeltaSeqPtr dsp, AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
4488 {
4489     AsnTypePtr atp2;
4490     DeltaSeqPtr olddsp;
4491     Boolean retval = FALSE;
4492 
4493     if (! loaded)
4494     {
4495         if (! SeqAsnLoad())
4496             return FALSE;
4497     }
4498 
4499     if (aip == NULL)
4500         return FALSE;
4501 
4502     atp2 = AsnLinkType(element, DELTA_SEQ);
4503     if (atp2 == NULL)
4504         return FALSE;
4505 
4506     if (dsp == NULL) { AsnNullValueMsg(aip, set); goto erret; }
4507 
4508     olddsp = dsp;
4509     if (! AsnOpenStruct(aip, set, (Pointer)olddsp))
4510         goto erret;
4511     while (dsp != NULL)
4512     {
4513         if (! DeltaSeqAsnWrite(dsp, aip, atp2))
4514             goto erret;
4515         dsp = dsp->next;
4516     }
4517     if (! AsnCloseStruct(aip, set, (Pointer)olddsp))
4518         goto erret;
4519     retval = TRUE;
4520 erret:
4521     AsnUnlinkType(element);
4522     return retval;
4523 }
4524 
4525 /*****************************************************************************
4526 *
4527 *   DeltaSeqSetAsnRead(aip, atp, set, element)
4528 *       atp is the current type (if identifier of a parent struct)
4529 *            assumption is readIdent has occurred
4530 *       if atp == NULL, then assumes it stands alone and read ident
4531 *            has not occurred.
4532 *
4533 *****************************************************************************/
DeltaSeqSetAsnRead(AsnIoPtr aip,AsnTypePtr set,AsnTypePtr element)4534 NLM_EXTERN DeltaSeqPtr LIBCALL DeltaSeqSetAsnRead (AsnIoPtr aip, AsnTypePtr set, AsnTypePtr element)
4535 {
4536     DataVal av;
4537     AsnTypePtr atp, atp2;
4538     DeltaSeqPtr dsp, first=NULL, prev;
4539 
4540     if (! loaded)
4541     {
4542         if (! SeqAsnLoad())
4543             return first;
4544     }
4545 
4546     if (aip == NULL)
4547         return first;
4548 
4549     prev = NULL;
4550 
4551     atp2 = AsnLinkType(element, DELTA_SEQ);
4552     if (atp2 == NULL)
4553         return first;
4554     atp = set;
4555     if (AsnReadVal(aip, atp, &av) <= 0)    /* read the START_STRUCT */
4556         goto erret;
4557     while ((atp = AsnReadId(aip, amp, atp)) != set)
4558     {
4559         if (atp == NULL)
4560             goto erret;
4561         dsp = DeltaSeqAsnRead(aip, atp2);
4562         if (dsp == NULL)
4563             goto erret;
4564         if (first == NULL)
4565             first = dsp;
4566         else
4567             prev->next = dsp;
4568         prev = dsp;
4569     }
4570     if (AsnReadVal(aip, atp, &av) <= 0)      /* read END_STRUCT */
4571         goto erret;
4572     if (first == NULL)
4573         ErrPostEx(SEV_WARNING, 0,0, "Empty SET OF Delta-seq.  line %ld", (long) aip->linenumber);
4574 ret:
4575     AsnUnlinkType(element);
4576     return first;
4577 erret:
4578     first = DeltaSeqSetFree(first);
4579     goto ret;
4580 }
4581 
4582 /*****************************************************************************
4583 *
4584 *   DeltaSeqSetFree(dsp)
4585 *
4586 *****************************************************************************/
DeltaSeqSetFree(DeltaSeqPtr dsp)4587 NLM_EXTERN DeltaSeqPtr LIBCALL DeltaSeqSetFree (DeltaSeqPtr dsp)
4588 {
4589     DeltaSeqPtr next;
4590 
4591     while (dsp != NULL)
4592     {
4593         next = dsp->next;
4594         DeltaSeqFree(dsp);
4595         dsp = next;
4596     }
4597     return dsp;
4598 }
4599 
4600 /*****************************************************************************
4601 *
4602 *   TextAnnotIdNew()
4603 *
4604 *****************************************************************************/
TextAnnotIdNew(void)4605 NLM_EXTERN TextAnnotIdPtr LIBCALL TextAnnotIdNew (void)
4606 {
4607     TextAnnotIdPtr taip;
4608 
4609     taip = (TextAnnotIdPtr)MemNew(sizeof(TextAnnotId));
4610     if (taip == NULL) return taip;
4611 
4612     taip->version = INT2_MIN;
4613     return taip;
4614 }
4615 
4616 /*****************************************************************************
4617 *
4618 *   TextAnnotIdFree(anp)
4619 *       Frees one TextAnnotId
4620 *
4621 *****************************************************************************/
TextAnnotIdFree(TextAnnotIdPtr taip)4622 NLM_EXTERN TextAnnotIdPtr LIBCALL TextAnnotIdFree (TextAnnotIdPtr taip)
4623 {
4624     if (taip == NULL)
4625         return taip;
4626 
4627     MemFree(taip->name);
4628     MemFree(taip->accession);
4629     MemFree(taip->release);
4630     return (TextAnnotIdPtr)MemFree(taip);
4631 }
4632 
4633 /*****************************************************************************
4634 *
4635 *   TextAnnotIdAsnWrite(taip, aip, atp)
4636 *       atp is the current type (if identifier of a parent struct)
4637 *       if atp == NULL, then assumes it stands alone (TextAnnotId ::=)
4638 *
4639 *****************************************************************************/
TextAnnotIdAsnWrite(TextAnnotIdPtr taip,AsnIoPtr aip,AsnTypePtr orig)4640 NLM_EXTERN Boolean LIBCALL TextAnnotIdAsnWrite (TextAnnotIdPtr taip, AsnIoPtr aip, AsnTypePtr orig)
4641 {
4642     DataVal av;
4643     AsnTypePtr atp;
4644     Boolean retval = FALSE;
4645 
4646     if (! loaded)
4647     {
4648         if (! SeqLocAsnLoad())
4649             return retval;
4650     }
4651 
4652     if (aip == NULL)
4653         return retval;
4654 
4655     atp = AsnLinkType(orig, TEXTANNOT_ID);   /* link local tree */
4656     if (atp == NULL)
4657         return retval;
4658 
4659     if (taip == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
4660 
4661     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
4662 
4663     if (! AsnOpenStruct(aip, atp, (Pointer)taip))
4664         goto erret;
4665     if (taip->name != NULL)
4666     {
4667         av.ptrvalue = taip->name;
4668         if (! AsnWrite(aip, TEXTANNOT_ID_name, &av))
4669             goto erret;
4670     }
4671     if (taip->accession != NULL)
4672     {
4673         av.ptrvalue = taip->accession;
4674         if (! AsnWrite(aip, TEXTANNOT_ID_accession, &av))
4675             goto erret;
4676     }
4677     if (taip->release != NULL)
4678     {
4679         av.ptrvalue = taip->release;
4680         if (! AsnWrite(aip, TEXTANNOT_ID_release, &av))
4681             goto erret;
4682     }
4683     if (taip->version != INT2_MIN)
4684     {
4685         av.intvalue = (Int4) taip->version;
4686         if (! AsnWrite(aip, TEXTANNOT_ID_version, &av))
4687             goto erret;
4688     }
4689     if (! AsnCloseStruct(aip, atp, (Pointer)taip))
4690         goto erret;
4691     retval = TRUE;
4692 erret:
4693     AsnUnlinkType(orig);       /* unlink local tree */
4694     return retval;
4695 }
4696 
4697 /*****************************************************************************
4698 *
4699 *   TextAnnotIdAsnRead(aip, atp)
4700 *       atp is the current type (if identifier of a parent struct)
4701 *            assumption is readIdent has occurred
4702 *       if atp == NULL, then assumes it stands alone and read ident
4703 *            has not occurred.
4704 *
4705 *****************************************************************************/
TextAnnotIdAsnRead(AsnIoPtr aip,AsnTypePtr orig)4706 NLM_EXTERN TextAnnotIdPtr LIBCALL TextAnnotIdAsnRead (AsnIoPtr aip, AsnTypePtr orig)
4707 {
4708     DataVal av;
4709     AsnTypePtr atp, oldtype;
4710     TextAnnotIdPtr taip=NULL;
4711 
4712     if (! loaded)
4713     {
4714         if (! SeqLocAsnLoad())
4715             return taip;
4716     }
4717 
4718     if (aip == NULL)
4719         return taip;
4720 
4721     if (orig == NULL)           /* TextAnnotId ::= (self contained) */
4722         atp = AsnReadId(aip, amp, TEXTANNOT_ID);
4723     else
4724         atp = AsnLinkType(orig, TEXTANNOT_ID);    /* link in local tree */
4725     oldtype = atp;
4726     if (atp == NULL)
4727         return taip;
4728 
4729     taip = TextAnnotIdNew();
4730     if (taip == NULL)
4731         goto erret;
4732 
4733     if ((AsnReadVal(aip, oldtype, &av)) <= 0)    /* read the START_STRUCT */
4734         goto erret;
4735     while ((atp = AsnReadId(aip, amp, atp)) != oldtype)
4736     {
4737         if (atp == NULL)
4738             goto erret;
4739         if ((AsnReadVal(aip, atp, &av)) <= 0)
4740             goto erret;
4741         if (atp == TEXTANNOT_ID_name)
4742             taip->name = (CharPtr)av.ptrvalue;
4743         else if (atp == TEXTANNOT_ID_accession)
4744             taip->accession = (CharPtr)av.ptrvalue;
4745         else if (atp == TEXTANNOT_ID_release)
4746             taip->release = (CharPtr)av.ptrvalue;
4747         else if (atp == TEXTANNOT_ID_version)
4748             taip->version = (Int2)av.intvalue;
4749         else
4750             goto erret;
4751     }
4752     if ((AsnReadVal(aip, atp, &av)) <= 0)      /* read END_STRUCT */
4753         goto erret;
4754 ret:
4755     AsnUnlinkType(orig);       /* unlink local tree */
4756     return taip;
4757 erret:
4758     taip = TextAnnotIdFree(taip);
4759     goto ret;
4760 }
4761 
4762 /*****************************************************************************
4763 *
4764 *   AnnotIdFree(anp)
4765 *       Frees one AnnotId and associated data
4766 *
4767 *****************************************************************************/
AnnotIdFree(AnnotIdPtr anp)4768 NLM_EXTERN AnnotIdPtr LIBCALL AnnotIdFree (AnnotIdPtr anp)
4769 {
4770     Pointer pnt;
4771 
4772     if (anp == NULL)
4773         return anp;
4774 
4775     pnt = anp->data.ptrvalue;
4776     switch (anp->choice)
4777     {
4778         case ANNOTID_LOCAL:      /* local */
4779             ObjectIdFree((ObjectIdPtr)pnt);
4780             break;
4781         case ANNOTID_NCBI:      /* ncbi */
4782             break;
4783         case ANNOTID_GENERAL:      /* general */
4784             DbtagFree((DbtagPtr)pnt);
4785             break;
4786         case ANNOTID_OTHER:      /* other */
4787             TextAnnotIdFree((TextAnnotIdPtr)pnt);
4788             break;
4789     }
4790     return (AnnotIdPtr)MemFree(anp);
4791 }
4792 
4793 /*****************************************************************************
4794 *
4795 *   AnnotIdAsnWrite(anp, aip, atp)
4796 *       atp is the current type (if identifier of a parent struct)
4797 *       if atp == NULL, then assumes it stands alone (AnnotId ::=)
4798 *
4799 *****************************************************************************/
AnnotIdAsnWrite(AnnotIdPtr anp,AsnIoPtr aip,AsnTypePtr orig)4800 NLM_EXTERN Boolean LIBCALL AnnotIdAsnWrite (AnnotIdPtr anp, AsnIoPtr aip, AsnTypePtr orig)
4801 {
4802     DataVal av;
4803     AsnTypePtr atp;
4804     Boolean retval = FALSE;
4805 
4806     if (! loaded)
4807     {
4808         if (! SeqAsnLoad())
4809             return retval;
4810     }
4811 
4812     if (aip == NULL)
4813         return retval;
4814 
4815     atp = AsnLinkType(orig, ANNOT_ID);   /* link local tree */
4816     if (atp == NULL)
4817         return retval;
4818 
4819     if (anp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
4820 
4821     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
4822 
4823     av.ptrvalue = (Pointer)anp;
4824     if (! AsnWriteChoice(aip, atp, (Int2)anp->choice, &av))
4825         goto erret;
4826 
4827     switch (anp->choice)
4828     {
4829         case ANNOTID_LOCAL:      /* local */
4830             retval = ObjectIdAsnWrite((ObjectIdPtr)(anp->data.ptrvalue), aip, ANNOT_ID_local);
4831             break;
4832         case ANNOTID_NCBI:      /* ncbi */
4833             av.intvalue = anp->data.intvalue;
4834             retval = AsnWrite(aip, ANNOT_ID_ncbi, &av);
4835             break;
4836         case ANNOTID_GENERAL:      /* general */
4837         if (aip->spec_version == 3 )    /* ASN3 strip new value */
4838         {
4839             ErrPostEx(SEV_ERROR,0,0,"ASN3: AnnotIdGeneral stripped");
4840         }
4841         else
4842             retval = DbtagAsnWrite((DbtagPtr)(anp->data.ptrvalue), aip, ANNOT_ID_general);
4843             break;
4844         case ANNOTID_OTHER:      /* other */
4845         if (aip->spec_version == 3 )    /* ASN3 strip new value */
4846         {
4847             ErrPostEx(SEV_ERROR,0,0,"ASN3: AnnotIdOther stripped");
4848         }
4849         else
4850             retval = TextAnnotIdAsnWrite((TextAnnotIdPtr)(anp->data.ptrvalue), aip, ANNOT_ID_other);
4851             break;
4852     }
4853 erret:
4854     AsnUnlinkType(orig);       /* unlink local tree */
4855     return retval;
4856 }
4857 
4858 /*****************************************************************************
4859 *
4860 *   AnnotIdAsnRead(aip, atp)
4861 *       atp is the current type (if identifier of a parent struct)
4862 *            assumption is readIdent has occurred
4863 *       if atp == NULL, then assumes it stands alone and read ident
4864 *            has not occurred.
4865 *
4866 *****************************************************************************/
AnnotIdAsnRead(AsnIoPtr aip,AsnTypePtr orig)4867 NLM_EXTERN AnnotIdPtr LIBCALL AnnotIdAsnRead (AsnIoPtr aip, AsnTypePtr orig)
4868 {
4869     DataVal av;
4870     AsnTypePtr atp;
4871     AnnotIdPtr anp=NULL;
4872 
4873     if (! loaded)
4874     {
4875         if (! SeqAsnLoad())
4876             return anp;
4877     }
4878 
4879     if (aip == NULL)
4880         return anp;
4881 
4882     if (orig == NULL)           /* AnnotId ::= (self contained) */
4883         atp = AsnReadId(aip, amp, ANNOT_ID);
4884     else
4885         atp = AsnLinkType(orig, ANNOT_ID);    /* link in local tree */
4886     if (atp == NULL)
4887         return anp;
4888 
4889     anp = ValNodeNew(NULL);
4890     if (anp == NULL)
4891         goto erret;
4892 
4893     if ( AsnReadVal(aip, atp, &av) <= 0)
4894         goto erret;    /* read the CHOICE value (nothing) */
4895     if ((atp = AsnReadId(aip, amp, atp)) == NULL)
4896         goto erret;  /* find the choice */
4897 
4898     if (atp == ANNOT_ID_local)
4899     {
4900         anp->choice = ANNOTID_LOCAL;
4901         anp->data.ptrvalue = (Pointer)ObjectIdAsnRead(aip, atp);
4902         if (anp->data.ptrvalue == NULL) goto erret;
4903     }
4904     else if (atp == ANNOT_ID_ncbi)
4905     {
4906         anp->choice = ANNOTID_NCBI;
4907         if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
4908         anp->data.intvalue = av.intvalue;
4909     }
4910     else if (atp == ANNOT_ID_general)
4911     {
4912         anp->choice = ANNOTID_GENERAL;
4913         anp->data.ptrvalue = (Pointer)DbtagAsnRead(aip, atp);
4914         if (anp->data.ptrvalue == NULL) goto erret;
4915     }
4916     else if (atp == ANNOT_ID_other)
4917     {
4918         anp->choice = ANNOTID_OTHER;
4919         anp->data.ptrvalue = (Pointer)TextAnnotIdAsnRead(aip, atp);
4920         if (anp->data.ptrvalue == NULL) goto erret;
4921     }
4922 ret:
4923     AsnUnlinkType(orig);       /* unlink local tree */
4924     return anp;
4925 erret:
4926     anp = AnnotIdFree(anp);
4927     goto ret;
4928 }
4929 
4930 /*****************************************************************************
4931 *
4932 *   AnnotIdSetFree()
4933 *       frees a chain of AnnotId's
4934 *
4935 *****************************************************************************/
AnnotIdSetFree(AnnotIdPtr anp)4936 NLM_EXTERN AnnotIdPtr LIBCALL AnnotIdSetFree (AnnotIdPtr anp)
4937 {
4938     AnnotIdPtr next;
4939 
4940     while (anp != NULL)
4941     {
4942         next = anp->next;
4943         AnnotIdFree(anp);
4944         anp = next;
4945     }
4946     return anp;
4947 }
4948 
4949 /*****************************************************************************
4950 *
4951 *   AnnotIdSetAsnRead(aip, settype, elementtype)
4952 *       read a set/seq of AnnotId's
4953 *
4954 *****************************************************************************/
AnnotIdSetAsnRead(AsnIoPtr aip,AsnTypePtr settype,AsnTypePtr elementtype)4955 NLM_EXTERN AnnotIdPtr LIBCALL AnnotIdSetAsnRead (AsnIoPtr aip, AsnTypePtr settype, AsnTypePtr elementtype)
4956 {
4957     DataVal av;
4958     AsnTypePtr atp, atp2;
4959     AnnotIdPtr first = NULL, curr = NULL, anp;
4960 
4961     if (! loaded)
4962     {
4963         if (! SeqAsnLoad())
4964             return first;
4965     }
4966 
4967     atp2 = AsnLinkType(elementtype, ANNOT_ID);
4968     if (atp2 == NULL)
4969         return first;
4970 
4971     if (AsnReadVal(aip, settype, &av) <= 0)   /* start struct */
4972         goto erret;
4973 
4974     atp = settype;
4975     while ((atp = AsnReadId(aip, amp, atp)) != settype)
4976     {
4977         if (atp == NULL)
4978             goto erret;
4979         anp = AnnotIdAsnRead(aip, atp2);
4980         if (anp == NULL)
4981             goto erret;
4982 
4983         if ((aip->spec_version == 3 ) &&   /* ASN3 strip new value */
4984             (anp->choice == ANNOTID_GENERAL))
4985         {
4986             ErrPostEx(SEV_ERROR,0,0,"ASN3: AnnotIdGeneral stripped");
4987             AnnotIdFree(anp);
4988         }
4989         else if ((aip->spec_version == 3 ) &&   /* ASN3 strip new value */
4990             (anp->choice == ANNOTID_OTHER))
4991         {
4992             ErrPostEx(SEV_ERROR,0,0,"ASN3: AnnotIdOther stripped");
4993             AnnotIdFree(anp);
4994         }
4995         else
4996         {
4997         if (first == NULL)
4998             first = anp;
4999         else
5000             curr->next = anp;
5001         curr = anp;
5002         }
5003     }
5004     if (AsnReadVal(aip, atp, &av) <= 0)    /* end struct */
5005         goto erret;
5006     if (first == NULL)
5007         ErrPostEx(SEV_WARNING, 0,0, "Empty SET OF Annot-id. line %ld", (long) aip->linenumber);
5008 ret:
5009     AsnUnlinkType(elementtype);
5010     return first;
5011 erret:
5012     first = AnnotIdSetFree(first);
5013     goto ret;
5014 }
5015 
5016 /*****************************************************************************
5017 *
5018 *   AnnotIdSetAsnWrite(anp, aip, settype, elementtype)
5019 *
5020 *****************************************************************************/
AnnotIdSetAsnWrite(AnnotIdPtr anp,AsnIoPtr aip,AsnTypePtr settype,AsnTypePtr elementtype)5021 NLM_EXTERN Boolean LIBCALL AnnotIdSetAsnWrite (AnnotIdPtr anp, AsnIoPtr aip, AsnTypePtr settype, AsnTypePtr elementtype)
5022 {
5023     AsnTypePtr atp2;
5024     AnnotIdPtr oldanp;
5025     Boolean retval = FALSE;
5026 
5027     if (! loaded)
5028     {
5029         if (! SeqAsnLoad())
5030             return retval;
5031     }
5032 
5033     if (aip == NULL)
5034         return retval;
5035 
5036     atp2 = AsnLinkType(elementtype, ANNOT_ID);
5037     if (atp2 == NULL)
5038         return retval;
5039 
5040     if (anp == NULL) { AsnNullValueMsg(aip, settype); goto erret; }
5041 
5042      oldanp = anp;
5043     if (! AsnOpenStruct(aip, settype, (Pointer)oldanp))
5044         goto erret;
5045     while (anp != NULL)
5046     {
5047         if (! AnnotIdAsnWrite(anp, aip, atp2))
5048             goto erret;
5049         anp = anp->next;
5050     }
5051     if (! AsnCloseStruct(aip, settype, (Pointer)oldanp))
5052         goto erret;
5053     retval = TRUE;
5054 erret:
5055     AsnUnlinkType(elementtype);
5056     return retval;
5057 }
5058 
5059 /*****************************************************************************
5060 *
5061 *   AnnotDescrFree(anp)
5062 *       Frees a chain of AnnotDescr and associated data
5063 *
5064 *****************************************************************************/
AnnotDescrFree(AnnotDescrPtr anp)5065 NLM_EXTERN AnnotDescrPtr LIBCALL AnnotDescrFree (AnnotDescrPtr anp)
5066 {
5067     AnnotDescrPtr next;
5068 
5069     while (anp != NULL)
5070     {
5071         next = anp->next;
5072         AnnotDescFree(anp);
5073         anp = next;
5074     }
5075     return anp;
5076 }
5077 
5078 /*****************************************************************************
5079 *
5080 *   AnnotDescrFree(anp)
5081 *       Frees a chain of AnnotDescr and associated data
5082 *
5083 *****************************************************************************/
AnnotDescFree(AnnotDescPtr anp)5084 NLM_EXTERN AnnotDescPtr LIBCALL AnnotDescFree (AnnotDescPtr anp)
5085 {
5086     Pointer pnt;
5087 
5088     if (anp == NULL) return anp;
5089 
5090     pnt = anp->data.ptrvalue;
5091     switch (anp->choice)
5092     {
5093         case Annot_descr_name:      /* name */
5094         case Annot_descr_title:      /* title */
5095         case Annot_descr_comment:      /* comment */
5096             MemFree(pnt);
5097             break;
5098         case Annot_descr_pub:      /* pub */
5099             PubdescFree((PubdescPtr)pnt);
5100             break;
5101         case Annot_descr_user:     /* user */
5102             UserObjectFree((UserObjectPtr)pnt);
5103             break;
5104         case Annot_descr_create_date:     /* create-date */
5105         case Annot_descr_update_date:     /* update-date */
5106             DateFree((DatePtr)pnt);
5107             break;
5108         case Annot_descr_src:     /* src */
5109             SeqIdFree((SeqIdPtr)pnt);
5110             break;
5111         case Annot_descr_align:     /* align */
5112             AlignDefFree((AlignDefPtr)pnt);
5113             break;
5114         case Annot_descr_region:  /* region */
5115             SeqLocFree((SeqLocPtr)pnt);
5116             break;
5117     }
5118     anp = (AnnotDescPtr)MemFree(anp);
5119     return anp;
5120 }
5121 
5122 /*****************************************************************************
5123 *
5124 *   AnnotDescrAsnWrite(anp, aip, atp)
5125 *       atp is the current type (if identifier of a parent struct)
5126 *       if atp == NULL, then assumes it stands alone (AnnotDescr ::=)
5127 *
5128 *****************************************************************************/
AnnotDescrAsnWrite(ValNodePtr anp,AsnIoPtr aip,AsnTypePtr orig)5129 NLM_EXTERN Boolean LIBCALL AnnotDescrAsnWrite (ValNodePtr anp, AsnIoPtr aip, AsnTypePtr orig)
5130 {
5131     AsnTypePtr atp;
5132     ValNodePtr oldanp;
5133     Boolean retval = FALSE;
5134 
5135     if (! loaded)
5136     {
5137         if (! SeqAsnLoad())
5138             return FALSE;
5139     }
5140 
5141     if (aip == NULL)
5142         return FALSE;
5143 
5144     atp = AsnLinkType(orig, ANNOT_DESCR);   /* link local tree */
5145     if (atp == NULL) return FALSE;
5146 
5147     if (anp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
5148 
5149     oldanp = anp;
5150     if (! AsnOpenStruct(aip, atp, (Pointer)oldanp)) goto erret; /* SET OF */
5151 
5152     while (anp != NULL)
5153     {
5154         if (! AnnotDescAsnWrite (anp, aip, ANNOT_DESCR_E)) goto erret;
5155         anp = anp->next;
5156     }
5157     if (! AsnCloseStruct(aip, atp, oldanp)) goto erret;
5158     retval = TRUE;
5159 erret:
5160     AsnUnlinkType(orig);       /* unlink local tree */
5161     return retval;
5162 }
5163 
5164 /*****************************************************************************
5165 *
5166 *   AnnotDescAsnWrite(anp, aip, orig)
5167 *       Writes one Seqdesc
5168 *
5169 *****************************************************************************/
AnnotDescAsnWrite(AnnotDescPtr anp,AsnIoPtr aip,AsnTypePtr orig)5170 NLM_EXTERN Boolean LIBCALL AnnotDescAsnWrite (AnnotDescPtr anp, AsnIoPtr aip, AsnTypePtr orig)
5171 {
5172     DataValPtr dvp;
5173     AsnTypePtr atp;
5174     Pointer pnt;
5175     Boolean retval = FALSE;
5176 
5177     if (! loaded)
5178     {
5179         if (! SeqAsnLoad())
5180             return FALSE;
5181     }
5182 
5183     if (aip == NULL)
5184         return FALSE;
5185 
5186     atp = AsnLinkType(orig, ANNOTDESC);   /* link local tree */
5187     if (atp == NULL) return FALSE;
5188 
5189     if (anp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
5190 
5191    dvp = &anp->data;
5192 
5193    if (! AsnWriteChoice(aip, atp, (Int2)anp->choice, dvp)) goto erret;
5194    pnt = anp->data.ptrvalue;
5195    switch (anp->choice)
5196    {
5197        case Annot_descr_name:                   /* name */
5198            if (! AsnWrite(aip, ANNOTDESC_name, dvp)) goto erret;
5199            break;
5200        case Annot_descr_title:                   /* title */
5201            if (! AsnWrite(aip, ANNOTDESC_title, dvp)) goto erret;
5202            break;
5203        case Annot_descr_comment:                   /* comment */
5204            if (! AsnWrite(aip, ANNOTDESC_comment, dvp)) goto erret;
5205            break;
5206        case Annot_descr_pub:                  /* pubdesc */
5207            if (! PubdescAsnWrite((PubdescPtr)pnt, aip, ANNOTDESC_pub)) goto erret;
5208            break;
5209        case Annot_descr_user:                   /* user */
5210            if (! UserObjectAsnWrite((UserObjectPtr)pnt, aip, ANNOTDESC_user)) goto erret;
5211            break;
5212     case Annot_descr_create_date:                   /* create-date */
5213         if (! DateAsnWrite((DatePtr)pnt, aip, ANNOTDESC_create_date)) goto erret;
5214         break;
5215     case Annot_descr_update_date:                   /* update-date */
5216         if (! DateAsnWrite((DatePtr)pnt, aip, ANNOTDESC_update_date)) goto erret;
5217         break;
5218        case Annot_descr_src:                   /* src */
5219            if (! SeqIdAsnWrite((SeqIdPtr)pnt, aip, ANNOTDESC_src)) goto erret;
5220            break;
5221        case Annot_descr_align:                   /* align */
5222         if (aip->spec_version == 3 )    /* ASN3 strip new value */
5223         {
5224             ErrPostEx(SEV_ERROR,0,0,"ASN3: AnnotDescrAlign stripped");
5225         }
5226         else
5227            if (! AlignDefAsnWrite((AlignDefPtr)pnt, aip, ANNOTDESC_align)) goto erret;
5228            break;
5229         case Annot_descr_region:                   /* region */
5230           if ((aip->spec_version == 0) || (aip->spec_version >= 6))
5231         {
5232            if (! SeqLocAsnWrite((SeqLocPtr)pnt, aip, ANNOTDESC_region)) goto erret;
5233         }
5234         else
5235         {
5236             ErrPostEx(SEV_ERROR,0,0,"ASN6: AnnotDescrRegion stripped");
5237         }
5238            break;
5239   }
5240     retval = TRUE;
5241 erret:
5242     AsnUnlinkType(orig);       /* unlink local tree */
5243     return retval;
5244 }
5245 
5246 /*****************************************************************************
5247 *
5248 *   AnnotDescrAsnRead(aip, atp)
5249 *       atp is the current type (if identifier of a parent struct)
5250 *            assumption is readIdent has occurred
5251 *       if atp == NULL, then assumes it stands alone and read ident
5252 *            has not occurred.
5253 *
5254 *****************************************************************************/
AnnotDescrAsnRead(AsnIoPtr aip,AsnTypePtr orig)5255 NLM_EXTERN ValNodePtr LIBCALL AnnotDescrAsnRead (AsnIoPtr aip, AsnTypePtr orig)
5256 {
5257     DataVal av;
5258     AsnTypePtr atp;
5259     ValNodePtr anp, first=NULL, prev=NULL;
5260 
5261     if (! loaded)
5262     {
5263         if (! SeqAsnLoad())
5264             return first;
5265     }
5266 
5267     if (aip == NULL)
5268         return first;
5269 
5270     if (! ProgMon("Read AnnotDescr"))
5271         return first;
5272 
5273     if (orig == NULL)           /* AnnotDescr ::= (self contained) */
5274         atp = AsnReadId(aip, amp, ANNOT_DESCR);
5275     else
5276         atp = AsnLinkType(orig, ANNOT_DESCR);    /* link in local tree */
5277     if (atp == NULL) return first;
5278 
5279     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* read the start struct */
5280 
5281     while ((atp = AsnReadId(aip, amp, atp)) == ANNOT_DESCR_E)
5282     {
5283         anp = AnnotDescAsnRead(aip, atp);
5284         if (anp == NULL) goto erret;
5285 
5286         if ((aip->spec_version == 3) && (anp->choice == Annot_descr_align))    /* ASN3 strip new value */
5287         {
5288             ErrPostEx(SEV_ERROR,0,0,"ASN3: AnnotDescrAlign stripped");
5289             AnnotDescFree(anp);
5290         }
5291         else
5292         {
5293         if (first == NULL)
5294             first = anp;
5295         if (prev != NULL)
5296             prev->next = anp;
5297         prev = anp;
5298         }
5299     }
5300     if (atp == NULL) goto erret;
5301 
5302     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;   /* end struct */
5303 ret:
5304     AsnUnlinkType(orig);       /* unlink local tree */
5305     return first;
5306 erret:
5307     first = AnnotDescrFree(first);
5308     goto ret;
5309 }
5310 
5311 /*****************************************************************************
5312 *
5313 *   AnnotDescAsnRead(aip, atp)
5314 *       atp is the current type (if identifier of a parent struct)
5315 *            assumption is readIdent has occurred
5316 *       if atp == NULL, then assumes it stands alone and read ident
5317 *            has not occurred.
5318 *
5319 *****************************************************************************/
AnnotDescAsnRead(AsnIoPtr aip,AsnTypePtr orig)5320 NLM_EXTERN AnnotDescPtr LIBCALL AnnotDescAsnRead (AsnIoPtr aip, AsnTypePtr orig)
5321 {
5322     DataVal av;
5323     AsnTypePtr atp;
5324     AnnotDescPtr anp=NULL;
5325     Uint1 choice;
5326     AsnReadFunc func;
5327 
5328     if (! loaded)
5329     {
5330         if (! SeqAsnLoad())
5331             return anp;
5332     }
5333 
5334     if (aip == NULL)
5335         return anp;
5336 
5337     if (orig == NULL)           /* AnnotDesc ::= (self contained) */
5338         atp = AsnReadId(aip, amp, ANNOTDESC);
5339     else
5340         atp = AsnLinkType(orig, ANNOTDESC);    /* link in local tree */
5341     if (atp == NULL) return anp;
5342 
5343     if (AsnReadVal(aip, atp, &av) <= 0) goto erret;    /* read the CHOICE value (nothing) */
5344     atp = AsnReadId(aip, amp, atp); if (atp == NULL) goto erret;  /* find the choice */
5345 
5346     func = NULL;
5347     choice = 0;
5348     if (atp == ANNOTDESC_name)
5349     {
5350         choice = Annot_descr_name;
5351     }
5352     else if (atp == ANNOTDESC_title)
5353     {
5354         choice = Annot_descr_title;
5355     }
5356     else if (atp == ANNOTDESC_comment)
5357     {
5358         choice = Annot_descr_comment;
5359     }
5360     else if (atp == ANNOTDESC_pub)
5361     {
5362         choice = Annot_descr_pub;
5363         func = (AsnReadFunc) PubdescAsnRead;
5364     }
5365     else if (atp == ANNOTDESC_user)
5366     {
5367         choice = Annot_descr_user;
5368         func = (AsnReadFunc) UserObjectAsnRead;
5369     }
5370     else if (atp == ANNOTDESC_create_date)
5371     {
5372         choice = Annot_descr_create_date;
5373         func = (AsnReadFunc) DateAsnRead;
5374     }
5375     else if (atp == ANNOTDESC_update_date)
5376     {
5377         choice = Annot_descr_update_date;
5378         func = (AsnReadFunc) DateAsnRead;
5379     }
5380     else if (atp == ANNOTDESC_src)
5381     {
5382         choice = Annot_descr_src;
5383         func = (AsnReadFunc) SeqIdAsnRead;
5384     }
5385     else if (atp == ANNOTDESC_align)
5386     {
5387         choice = Annot_descr_align;
5388         func = (AsnReadFunc) AlignDefAsnRead;
5389     }
5390     else if (atp == ANNOTDESC_region)
5391     {
5392         choice = Annot_descr_region;
5393         func = (AsnReadFunc) SeqLocAsnRead;
5394     }
5395 
5396     anp = AnnotDescrNew(NULL);
5397     if (anp == NULL) goto erret;
5398     anp->choice = choice;
5399     if (func != NULL)
5400     {
5401         anp->data.ptrvalue = (* func)(aip, atp);
5402         if (anp->data.ptrvalue == NULL) goto erret;
5403     }
5404     else
5405     {
5406         if (AsnReadVal(aip, atp, &anp->data) <= 0) goto erret;
5407     }
5408 
5409 ret:
5410     AsnUnlinkType(orig);       /* unlink local tree */
5411     return anp;
5412 erret:
5413     anp = AnnotDescFree(anp);
5414     goto ret;
5415 }
5416 
5417 /*****************************************************************************
5418 *
5419 *   AlignDefNew()
5420 *
5421 *****************************************************************************/
AlignDefNew(void)5422 NLM_EXTERN AlignDefPtr LIBCALL AlignDefNew (void)
5423 {
5424     return (AlignDefPtr)MemNew(sizeof(AlignDef));
5425 }
5426 
5427 /*****************************************************************************
5428 *
5429 *   AlignDefFree(adp)
5430 *       Frees one AlignDef and associated data
5431 *
5432 *****************************************************************************/
AlignDefFree(AlignDefPtr adp)5433 NLM_EXTERN AlignDefPtr LIBCALL AlignDefFree (AlignDefPtr adp)
5434 {
5435     if (adp == NULL)
5436         return adp;
5437     SeqIdSetFree(adp->ids);
5438     return (AlignDefPtr)MemFree(adp);
5439 }
5440 
5441 /*****************************************************************************
5442 *
5443 *   AlignDefAsnWrite(adp, aip, atp)
5444 *       atp is the current type (if identifier of a parent struct)
5445 *       if atp == NULL, then assumes it stands alone (AlignDef ::=)
5446 *
5447 *****************************************************************************/
AlignDefAsnWrite(AlignDefPtr adp,AsnIoPtr aip,AsnTypePtr orig)5448 NLM_EXTERN Boolean LIBCALL AlignDefAsnWrite (AlignDefPtr adp, AsnIoPtr aip, AsnTypePtr orig)
5449 {
5450     DataVal av;
5451     AsnTypePtr atp;
5452     Boolean retval = FALSE;
5453 
5454     if (! loaded)
5455     {
5456         if (! SeqAsnLoad())
5457             return retval;
5458     }
5459 
5460     if (aip == NULL)
5461         return retval;
5462 
5463     atp = AsnLinkType(orig, ALIGN_DEF);   /* link local tree */
5464     if (atp == NULL)
5465         return retval;
5466 
5467     if (adp == NULL) { AsnNullValueMsg(aip, atp); goto erret; }
5468 
5469     MemSet ((Pointer) (&av), 0, sizeof (DataVal));
5470 
5471     if (! AsnOpenStruct(aip, atp, (Pointer)adp))
5472         goto erret;
5473 
5474     av.intvalue = (Int4)(adp->align_type);
5475     retval = AsnWrite(aip, ALIGN_DEF_align_type, &av);
5476     if (! retval) goto erret;
5477 
5478     if (adp->ids != NULL)
5479     {
5480         retval = SeqIdSetAsnWrite(adp->ids, aip, ALIGN_DEF_ids, ALIGN_DEF_ids_E);
5481         if (! retval) goto erret;
5482     }
5483 
5484     retval = AsnCloseStruct(aip, atp, (Pointer)adp);
5485 
5486 erret:
5487     AsnUnlinkType(orig);       /* unlink local tree */
5488     return retval;
5489 }
5490 
5491 /*****************************************************************************
5492 *
5493 *   AlignDefAsnRead(aip, atp)
5494 *       atp is the current type (if identifier of a parent struct)
5495 *            assumption is readIdent has occurred
5496 *       if atp == NULL, then assumes it stands alone and read ident
5497 *            has not occurred.
5498 *
5499 *****************************************************************************/
AlignDefAsnRead(AsnIoPtr aip,AsnTypePtr orig)5500 NLM_EXTERN AlignDefPtr LIBCALL AlignDefAsnRead (AsnIoPtr aip, AsnTypePtr orig)
5501 {
5502     DataVal av;
5503     AsnTypePtr atp, oldatp;
5504     AlignDefPtr adp=NULL;
5505 
5506     if (! loaded)
5507     {
5508         if (! SeqAsnLoad())
5509             return adp;
5510     }
5511 
5512     if (aip == NULL)
5513         return adp;
5514 
5515     if (orig == NULL)           /* AlignDef ::= (self contained) */
5516         atp = AsnReadId(aip, amp, ALIGN_DEF);
5517     else
5518         atp = AsnLinkType(orig, ALIGN_DEF);    /* link in local tree */
5519     if (atp == NULL)
5520         return adp;
5521     oldatp = atp;
5522 
5523     adp = AlignDefNew();
5524     if (adp == NULL)
5525         goto erret;
5526 
5527     if ( AsnReadVal(aip, atp, &av) <= 0)
5528         goto erret;    /* read the open struct */
5529 
5530     while ((atp = AsnReadId(aip, amp, atp)) != oldatp)
5531     {
5532        if (atp == NULL) goto erret;
5533        if (atp == ALIGN_DEF_align_type)
5534         {
5535             if (AsnReadVal(aip, atp, &av) <= 0) goto erret;
5536             adp->align_type = (Uint1)(av.intvalue);
5537         }
5538         else if (atp == ALIGN_DEF_ids)
5539         {
5540             adp->ids = SeqIdSetAsnRead(aip, atp, ALIGN_DEF_ids_E);
5541         }
5542     }
5543 
5544     if ( AsnReadVal(aip, atp, &av) <= 0)
5545         goto erret;    /* read the close struct */
5546 
5547 ret:
5548     AsnUnlinkType(orig);       /* unlink local tree */
5549     return adp;
5550 erret:
5551     adp = AlignDefFree(adp);
5552     goto ret;
5553 }
5554 
5555 
5556