1 /*   asn2gb.c
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *            National Center for Biotechnology Information (NCBI)
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government do not place any restriction on its use or reproduction.
13 *  We would, however, appreciate having the NCBI and the author cited in
14 *  any work or product based on this material
15 *
16 *  Although all reasonable efforts have been taken to ensure the accuracy
17 *  and reliability of the software and data, the NLM and the U.S.
18 *  Government do not and cannot warrant the performance or results that
19 *  may be obtained by using this software or data. The NLM and the U.S.
20 *  Government disclaim all warranties, express or implied, including
21 *  warranties of performance, merchantability or fitness for any particular
22 *  purpose.
23 *
24 * ===========================================================================
25 *
26 * File Name:  asn2gb.c
27 *
28 * Author:  Karl Sirotkin, Tom Madden, Tatiana Tatusov, Jonathan Kans
29 *
30 * Version Creation Date:   10/21/98
31 *
32 * $Revision: 6.242 $
33 *
34 * File Description:  New GenBank flatfile generator application
35 *
36 * Modifications:
37 * --------------------------------------------------------------------------
38 * ==========================================================================
39 */
40 
41 #include <ncbi.h>
42 #include <objall.h>
43 #include <objsset.h>
44 #include <objsub.h>
45 #include <objfdef.h>
46 #include <objgbseq.h>
47 #include <objtseq.h>
48 #include <sequtil.h>
49 #include <sqnutils.h>
50 #include <explore.h>
51 #include <gather.h>
52 #include <toasn3.h>
53 #include <asn2gnbp.h>
54 #include <connect/ncbi_gnutls.h>
55 
56 /* asn2gnbi.h needed to test PUBSEQGetAccnVer in accpubseq.c */
57 #include <asn2gnbi.h>
58 
59 #define ASN2GB_APP_VER "18.0"
60 
61 CharPtr ASN2GB_APPLICATION = ASN2GB_APP_VER;
62 
SaveSeqEntry(SeqEntryPtr sep,CharPtr filename)63 static void SaveSeqEntry (
64   SeqEntryPtr sep,
65   CharPtr filename
66 )
67 
68 {
69   AsnIoPtr  aop;
70 
71   if (sep == NULL) return;
72   aop = AsnIoOpen (filename, "w");
73   if (aop != NULL) {
74     SeqEntryAsnWrite (sep, aop, NULL);
75   }
76   AsnIoClose (aop);
77 }
78 
SaveAsn2gnbk(SeqEntryPtr sep,CharPtr filename,FmtType format,ModType mode,StlType style,FlgType flags,LckType locks,CstType custom)79 static void SaveAsn2gnbk (
80   SeqEntryPtr sep,
81   CharPtr filename,
82   FmtType format,
83   ModType mode,
84   StlType style,
85   FlgType flags,
86   LckType locks,
87   CstType custom
88 )
89 
90 {
91   FILE  *fp;
92 
93   if (sep == NULL) return;
94   fp = FileOpen (filename, "w");
95   if (fp != NULL) {
96     SeqEntryToGnbk (sep, NULL, format, mode, style, flags, locks, custom, NULL, fp);
97   }
98   FileClose (fp);
99 }
100 
GetFirstGoodBioseq(BioseqPtr bsp,Pointer userdata)101 static void GetFirstGoodBioseq (
102   BioseqPtr bsp,
103   Pointer userdata
104 )
105 
106 {
107   BioseqPtr PNTR bspp;
108 
109   bspp = (BioseqPtr PNTR) userdata;
110   if (*bspp != NULL) return;
111   *bspp = bsp;
112 }
113 
114 NLM_EXTERN void AsnPrintNewLine PROTO((AsnIoPtr aip));
115 
SaveTinySeqs(BioseqPtr bsp,Pointer userdata)116 static void SaveTinySeqs (
117   BioseqPtr bsp,
118   Pointer userdata
119 )
120 
121 {
122   AsnIoPtr  aip;
123   TSeqPtr   tsp;
124 
125   if (bsp == NULL) return;
126   aip = (AsnIoPtr) userdata;
127 
128   tsp = BioseqToTSeq (bsp);
129   if (tsp == NULL) return;
130 
131   TSeqAsnWrite (tsp, aip, NULL);
132   AsnPrintNewLine (aip);
133   AsnIoFlush (aip);
134 
135   TSeqFree (tsp);
136 }
137 
SaveTinyStreams(BioseqPtr bsp,Pointer userdata)138 static void SaveTinyStreams (
139   BioseqPtr bsp,
140   Pointer userdata
141 )
142 
143 {
144   AsnIoPtr  aip;
145 
146   if (bsp == NULL) return;
147   aip = (AsnIoPtr) userdata;
148 
149   BioseqAsnWriteAsTSeq (bsp, aip, NULL);
150   AsnPrintNewLine (aip);
151   AsnIoFlush (aip);
152 }
153 
154 #ifdef INTERNAL_NCBI_ASN2GB
155 static CharPtr dirsubfetchproc = "DirSubBioseqFetch";
156 
157 static CharPtr dirsubfetchcmd = NULL;
158 
159 extern Pointer ReadFromDirSub (CharPtr accn, Uint2Ptr datatype, Uint2Ptr entityID);
ReadFromDirSub(CharPtr accn,Uint2Ptr datatype,Uint2Ptr entityID)160 extern Pointer ReadFromDirSub (CharPtr accn, Uint2Ptr datatype, Uint2Ptr entityID)
161 
162 {
163   Char     cmmd [256];
164   Pointer  dataptr;
165   FILE*    fp;
166   Char     path [PATH_MAX];
167 
168   if (datatype != NULL) {
169     *datatype = 0;
170   }
171   if (entityID != NULL) {
172     *entityID = 0;
173   }
174   if (StringHasNoText (accn)) return NULL;
175 
176   if (dirsubfetchcmd == NULL) {
177     if (GetAppParam ("SEQUIN", "DIRSUB", "FETCHSCRIPT", NULL, cmmd, sizeof (cmmd))) {
178     	dirsubfetchcmd = StringSaveNoNull (cmmd);
179     }
180   }
181   if (dirsubfetchcmd == NULL) return NULL;
182 
183   TmpNam (path);
184 
185 #ifdef OS_UNIX
186   sprintf (cmmd, "csh %s %s > %s", dirsubfetchcmd, accn, path);
187   system (cmmd);
188 #endif
189 #ifdef OS_MSWIN
190   sprintf (cmmd, "%s %s -o %s", dirsubfetchcmd, accn, path);
191   system (cmmd);
192 #endif
193 
194   fp = FileOpen (path, "r");
195   if (fp == NULL) {
196     FileRemove (path);
197     return NULL;
198   }
199   dataptr = ReadAsnFastaOrFlatFile (fp, datatype, entityID, FALSE, FALSE, TRUE, FALSE);
200   FileClose (fp);
201   FileRemove (path);
202   return dataptr;
203 }
204 
205 
DirSubBioseqFetchFunc(Pointer data)206 static Int2 LIBCALLBACK DirSubBioseqFetchFunc (Pointer data)
207 
208 {
209   BioseqPtr         bsp;
210   Char              cmmd [256];
211   Pointer           dataptr;
212   Uint2             datatype;
213   Uint2             entityID;
214   FILE*             fp;
215   OMProcControlPtr  ompcp;
216   ObjMgrProcPtr     ompp;
217   Char              path [PATH_MAX];
218   SeqEntryPtr       sep = NULL;
219   SeqIdPtr          sip;
220   TextSeqIdPtr      tsip;
221 
222   ompcp = (OMProcControlPtr) data;
223   if (ompcp == NULL) return OM_MSG_RET_ERROR;
224   ompp = ompcp->proc;
225   if (ompp == NULL) return OM_MSG_RET_ERROR;
226   sip = (SeqIdPtr) ompcp->input_data;
227   if (sip == NULL) return OM_MSG_RET_ERROR;
228 
229   if (sip->choice != SEQID_GENBANK) return OM_MSG_RET_ERROR;
230   tsip = (TextSeqIdPtr) sip->data.ptrvalue;
231   if (tsip == NULL || StringHasNoText (tsip->accession)) return OM_MSG_RET_ERROR;
232 
233   if (dirsubfetchcmd == NULL) {
234     if (GetAppParam ("SEQUIN", "DIRSUB", "FETCHSCRIPT", NULL, cmmd, sizeof (cmmd))) {
235     	dirsubfetchcmd = StringSaveNoNull (cmmd);
236     }
237   }
238   if (dirsubfetchcmd == NULL) return OM_MSG_RET_ERROR;
239 
240   TmpNam (path);
241 
242 #ifdef OS_UNIX
243   sprintf (cmmd, "csh %s %s > %s", dirsubfetchcmd, tsip->accession, path);
244   system (cmmd);
245 #endif
246 #ifdef OS_MSWIN
247   sprintf (cmmd, "%s %s -o %s", dirsubfetchcmd, tsip->accession, path);
248   system (cmmd);
249 #endif
250 
251   fp = FileOpen (path, "r");
252   if (fp == NULL) {
253     FileRemove (path);
254     return OM_MSG_RET_ERROR;
255   }
256   dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID, FALSE, FALSE, TRUE, FALSE);
257   FileClose (fp);
258   FileRemove (path);
259 
260   if (dataptr == NULL) return OM_MSG_RET_OK;
261 
262   sep = GetTopSeqEntryForEntityID (entityID);
263   if (sep == NULL) return OM_MSG_RET_ERROR;
264   bsp = BioseqFindInSeqEntry (sip, sep);
265   ompcp->output_data = (Pointer) bsp;
266   ompcp->output_entityID = ObjMgrGetEntityIDForChoice (sep);
267   return OM_MSG_RET_DONE;
268 }
269 
DirSubFetchEnable(void)270 static Boolean DirSubFetchEnable (void)
271 
272 {
273   ObjMgrProcLoad (OMPROC_FETCH, dirsubfetchproc, dirsubfetchproc,
274                   OBJ_SEQID, 0, OBJ_BIOSEQ, 0, NULL,
275                   DirSubBioseqFetchFunc, PROC_PRIORITY_DEFAULT);
276   return TRUE;
277 }
278 
279 static CharPtr smartfetchproc = "SmartBioseqFetch";
280 
281 static CharPtr smartfetchcmd = NULL;
282 
283 extern Pointer ReadFromSmart (CharPtr accn, Uint2Ptr datatype, Uint2Ptr entityID);
ReadFromSmart(CharPtr accn,Uint2Ptr datatype,Uint2Ptr entityID)284 extern Pointer ReadFromSmart (CharPtr accn, Uint2Ptr datatype, Uint2Ptr entityID)
285 
286 {
287   Char     cmmd [256];
288   Pointer  dataptr;
289   FILE*    fp;
290   Char     path [PATH_MAX];
291 
292   if (datatype != NULL) {
293     *datatype = 0;
294   }
295   if (entityID != NULL) {
296     *entityID = 0;
297   }
298   if (StringHasNoText (accn)) return NULL;
299 
300   if (smartfetchcmd == NULL) {
301     if (GetAppParam ("SEQUIN", "SMART", "FETCHSCRIPT", NULL, cmmd, sizeof (cmmd))) {
302     	smartfetchcmd = StringSaveNoNull (cmmd);
303     }
304   }
305   if (smartfetchcmd == NULL) return NULL;
306 
307   TmpNam (path);
308 
309 #ifdef OS_UNIX
310   sprintf (cmmd, "csh %s %s > %s", smartfetchcmd, accn, path);
311   system (cmmd);
312 #endif
313 #ifdef OS_MSWIN
314   sprintf (cmmd, "%s %s -o %s", smartfetchcmd, accn, path);
315   system (cmmd);
316 #endif
317 
318   fp = FileOpen (path, "r");
319   if (fp == NULL) {
320     FileRemove (path);
321     return NULL;
322   }
323   dataptr = ReadAsnFastaOrFlatFile (fp, datatype, entityID, FALSE, FALSE, TRUE, FALSE);
324   FileClose (fp);
325   FileRemove (path);
326   return dataptr;
327 }
328 
329 
SmartBioseqFetchFunc(Pointer data)330 static Int2 LIBCALLBACK SmartBioseqFetchFunc (Pointer data)
331 
332 {
333   BioseqPtr         bsp;
334   Char              cmmd [256];
335   Pointer           dataptr;
336   Uint2             datatype;
337   Uint2             entityID;
338   FILE*             fp;
339   OMProcControlPtr  ompcp;
340   ObjMgrProcPtr     ompp;
341   Char              path [PATH_MAX];
342   SeqEntryPtr       sep = NULL;
343   SeqIdPtr          sip;
344   TextSeqIdPtr      tsip;
345 
346   ompcp = (OMProcControlPtr) data;
347   if (ompcp == NULL) return OM_MSG_RET_ERROR;
348   ompp = ompcp->proc;
349   if (ompp == NULL) return OM_MSG_RET_ERROR;
350   sip = (SeqIdPtr) ompcp->input_data;
351   if (sip == NULL) return OM_MSG_RET_ERROR;
352 
353   if (sip->choice != SEQID_GENBANK) return OM_MSG_RET_ERROR;
354   tsip = (TextSeqIdPtr) sip->data.ptrvalue;
355   if (tsip == NULL || StringHasNoText (tsip->accession)) return OM_MSG_RET_ERROR;
356 
357   if (smartfetchcmd == NULL) {
358     if (GetAppParam ("SEQUIN", "SMART", "FETCHSCRIPT", NULL, cmmd, sizeof (cmmd))) {
359     	smartfetchcmd = StringSaveNoNull (cmmd);
360     }
361   }
362   if (smartfetchcmd == NULL) return OM_MSG_RET_ERROR;
363 
364   TmpNam (path);
365 
366 #ifdef OS_UNIX
367   sprintf (cmmd, "csh %s %s > %s", smartfetchcmd, tsip->accession, path);
368   system (cmmd);
369 #endif
370 #ifdef OS_MSWIN
371   sprintf (cmmd, "%s %s -o %s", smartfetchcmd, tsip->accession, path);
372   system (cmmd);
373 #endif
374 
375   fp = FileOpen (path, "r");
376   if (fp == NULL) {
377     FileRemove (path);
378     return OM_MSG_RET_ERROR;
379   }
380   dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID, FALSE, FALSE, TRUE, FALSE);
381   FileClose (fp);
382   FileRemove (path);
383 
384   if (dataptr == NULL) return OM_MSG_RET_OK;
385 
386   sep = GetTopSeqEntryForEntityID (entityID);
387   if (sep == NULL) return OM_MSG_RET_ERROR;
388   bsp = BioseqFindInSeqEntry (sip, sep);
389   ompcp->output_data = (Pointer) bsp;
390   ompcp->output_entityID = ObjMgrGetEntityIDForChoice (sep);
391   return OM_MSG_RET_DONE;
392 }
393 
SmartFetchEnable(void)394 static Boolean SmartFetchEnable (void)
395 
396 {
397   ObjMgrProcLoad (OMPROC_FETCH, smartfetchproc, smartfetchproc,
398                   OBJ_SEQID, 0, OBJ_BIOSEQ, 0, NULL,
399                   SmartBioseqFetchFunc, PROC_PRIORITY_DEFAULT);
400   return TRUE;
401 }
402 
403 static CharPtr tpasmartfetchproc = "TPASmartBioseqFetch";
404 
405 static CharPtr tpasmartfetchcmd = NULL;
406 
407 extern Pointer ReadFromTPASmart (CharPtr accn, Uint2Ptr datatype, Uint2Ptr entityID);
ReadFromTPASmart(CharPtr accn,Uint2Ptr datatype,Uint2Ptr entityID)408 extern Pointer ReadFromTPASmart (CharPtr accn, Uint2Ptr datatype, Uint2Ptr entityID)
409 
410 {
411   Char     cmmd [256];
412   Pointer  dataptr;
413   FILE*    fp;
414   Char     path [PATH_MAX];
415 
416   if (datatype != NULL) {
417     *datatype = 0;
418   }
419   if (entityID != NULL) {
420     *entityID = 0;
421   }
422   if (StringHasNoText (accn)) return NULL;
423 
424   if (tpasmartfetchcmd == NULL) {
425     if (GetAppParam ("SEQUIN", "TPASMART", "FETCHSCRIPT", NULL, cmmd, sizeof (cmmd))) {
426     	tpasmartfetchcmd = StringSaveNoNull (cmmd);
427     }
428   }
429   if (tpasmartfetchcmd == NULL) return NULL;
430 
431   TmpNam (path);
432 
433 #ifdef OS_UNIX
434   sprintf (cmmd, "csh %s %s > %s", tpasmartfetchcmd, accn, path);
435   system (cmmd);
436 #endif
437 #ifdef OS_MSWIN
438   sprintf (cmmd, "%s %s -o %s", tpasmartfetchcmd, accn, path);
439   system (cmmd);
440 #endif
441 
442   fp = FileOpen (path, "r");
443   if (fp == NULL) {
444     FileRemove (path);
445     return NULL;
446   }
447   dataptr = ReadAsnFastaOrFlatFile (fp, datatype, entityID, FALSE, FALSE, TRUE, FALSE);
448   FileClose (fp);
449   FileRemove (path);
450   return dataptr;
451 }
452 
453 
TPASmartBioseqFetchFunc(Pointer data)454 static Int2 LIBCALLBACK TPASmartBioseqFetchFunc (Pointer data)
455 
456 {
457   BioseqPtr         bsp;
458   Char              cmmd [256];
459   Pointer           dataptr;
460   Uint2             datatype;
461   Uint2             entityID;
462   FILE*             fp;
463   OMProcControlPtr  ompcp;
464   ObjMgrProcPtr     ompp;
465   Char              path [PATH_MAX];
466   SeqEntryPtr       sep = NULL;
467   SeqIdPtr          sip;
468   TextSeqIdPtr      tsip;
469 
470   ompcp = (OMProcControlPtr) data;
471   if (ompcp == NULL) return OM_MSG_RET_ERROR;
472   ompp = ompcp->proc;
473   if (ompp == NULL) return OM_MSG_RET_ERROR;
474   sip = (SeqIdPtr) ompcp->input_data;
475   if (sip == NULL) return OM_MSG_RET_ERROR;
476 
477   if (sip->choice != SEQID_TPG) return OM_MSG_RET_ERROR;
478   tsip = (TextSeqIdPtr) sip->data.ptrvalue;
479   if (tsip == NULL || StringHasNoText (tsip->accession)) return OM_MSG_RET_ERROR;
480 
481   if (tpasmartfetchcmd == NULL) {
482     if (GetAppParam ("SEQUIN", "TPASMART", "FETCHSCRIPT", NULL, cmmd, sizeof (cmmd))) {
483     	tpasmartfetchcmd = StringSaveNoNull (cmmd);
484     }
485   }
486   if (tpasmartfetchcmd == NULL) return OM_MSG_RET_ERROR;
487 
488   TmpNam (path);
489 
490 #ifdef OS_UNIX
491   sprintf (cmmd, "csh %s %s > %s", tpasmartfetchcmd, tsip->accession, path);
492   system (cmmd);
493 #endif
494 #ifdef OS_MSWIN
495   sprintf (cmmd, "%s %s -o %s", tpasmartfetchcmd, tsip->accession, path);
496   system (cmmd);
497 #endif
498 
499   fp = FileOpen (path, "r");
500   if (fp == NULL) {
501     FileRemove (path);
502     return OM_MSG_RET_ERROR;
503   }
504   dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID, FALSE, FALSE, TRUE, FALSE);
505   FileClose (fp);
506   FileRemove (path);
507 
508   if (dataptr == NULL) return OM_MSG_RET_OK;
509 
510   sep = GetTopSeqEntryForEntityID (entityID);
511   if (sep == NULL) return OM_MSG_RET_ERROR;
512   bsp = BioseqFindInSeqEntry (sip, sep);
513   ompcp->output_data = (Pointer) bsp;
514   ompcp->output_entityID = ObjMgrGetEntityIDForChoice (sep);
515   return OM_MSG_RET_DONE;
516 }
517 
TPASmartFetchEnable(void)518 static Boolean TPASmartFetchEnable (void)
519 
520 {
521   ObjMgrProcLoad (OMPROC_FETCH, tpasmartfetchproc, tpasmartfetchproc,
522                   OBJ_SEQID, 0, OBJ_BIOSEQ, 0, NULL,
523                   TPASmartBioseqFetchFunc, PROC_PRIORITY_DEFAULT);
524   return TRUE;
525 }
526 #endif
527 
HandleSingleRecord(CharPtr inputFile,CharPtr outputFile,FmtType format,FmtType altformat,ModType mode,StlType style,FlgType flags,LckType locks,CstType custom,XtraPtr extra,Int2 type,Boolean binary,Boolean compressed,Int4 from,Int4 to,Uint1 strand,Uint4 itemID,Boolean do_tiny_seq,Boolean do_fasta_stream)528 static Int2 HandleSingleRecord (
529   CharPtr inputFile,
530   CharPtr outputFile,
531   FmtType format,
532   FmtType altformat,
533   ModType mode,
534   StlType style,
535   FlgType flags,
536   LckType locks,
537   CstType custom,
538   XtraPtr extra,
539   Int2 type,
540   Boolean binary,
541   Boolean compressed,
542   Int4 from,
543   Int4 to,
544   Uint1 strand,
545   Uint4 itemID,
546   Boolean do_tiny_seq,
547   Boolean do_fasta_stream
548 )
549 
550 {
551   AsnIoPtr      aip;
552   BioseqPtr     bsp;
553   BioseqSetPtr  bssp;
554   Pointer       dataptr = NULL;
555   Uint2         datatype = 0;
556   Uint2         entityID;
557   FILE          *fp;
558   FILE          *ofp = NULL;
559   ObjMgrPtr     omp;
560   SeqEntryPtr   sep;
561   SeqFeatPtr    sfp;
562   SeqInt        sint;
563   SeqLocPtr     slp = NULL;
564   ValNode       vn;
565 
566   if (type == 1) {
567     fp = FileOpen (inputFile, "r");
568     if (fp == NULL) {
569       Message (MSG_POSTERR, "FileOpen failed for input file '%s'", inputFile);
570       return 1;
571     }
572 
573     dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE);
574 
575     FileClose (fp);
576 
577     entityID = ObjMgrRegister (datatype, dataptr);
578 
579   } else if (type >= 2 && type <= 5) {
580     aip = AsnIoOpen (inputFile, binary? "rb" : "r");
581     if (aip == NULL) {
582       Message (MSG_POSTERR, "AsnIoOpen failed for input file '%s'", inputFile);
583       return 1;
584     }
585 
586     SeqMgrHoldIndexing (TRUE);
587     switch (type) {
588       case 2 :
589         dataptr = (Pointer) SeqEntryAsnRead (aip, NULL);
590         datatype = OBJ_SEQENTRY;
591         break;
592       case 3 :
593         dataptr = (Pointer) BioseqAsnRead (aip, NULL);
594         datatype = OBJ_BIOSEQ;
595         break;
596       case 4 :
597         dataptr = (Pointer) BioseqSetAsnRead (aip, NULL);
598         datatype = OBJ_BIOSEQSET;
599         break;
600       case 5 :
601         dataptr = (Pointer) SeqSubmitAsnRead (aip, NULL);
602         datatype = OBJ_SEQSUB;
603         break;
604       default :
605         break;
606     }
607     SeqMgrHoldIndexing (FALSE);
608 
609     AsnIoClose (aip);
610 
611     entityID = ObjMgrRegister (datatype, dataptr);
612 
613   } else {
614     Message (MSG_POSTERR, "Input format type '%d' unrecognized", (int) type);
615     return 1;
616   }
617 
618   if (dataptr == NULL) {
619     Message (MSG_POSTERR, "Data read failed for input file '%s'", inputFile);
620     return 1;
621   }
622 
623   if (datatype == OBJ_SEQSUB || datatype == OBJ_SEQENTRY ||
624         datatype == OBJ_BIOSEQ || datatype == OBJ_BIOSEQSET) {
625 
626 /*
627 #ifdef WIN_MAC
628 #if __profile__
629     ProfilerSetStatus (TRUE);
630 #endif
631 #endif
632 */
633 
634     entityID = SeqMgrIndexFeatures (entityID, NULL);
635 
636 /*
637 #ifdef WIN_MAC
638 #if __profile__
639     ProfilerSetStatus (FALSE);
640 #endif
641 #endif
642 */
643 
644     sep = GetTopSeqEntryForEntityID (entityID);
645 
646     if (sep == NULL) {
647       sep = SeqEntryNew ();
648       if (sep != NULL) {
649         if (datatype == OBJ_BIOSEQ) {
650           bsp = (BioseqPtr) dataptr;
651           sep->choice = 1;
652           sep->data.ptrvalue = bsp;
653           SeqMgrSeqEntry (SM_BIOSEQ, (Pointer) bsp, sep);
654         } else if (datatype == OBJ_BIOSEQSET) {
655           bssp = (BioseqSetPtr) dataptr;
656           sep->choice = 2;
657           sep->data.ptrvalue = bssp;
658           SeqMgrSeqEntry (SM_BIOSEQSET, (Pointer) bssp, sep);
659         } else {
660           sep = SeqEntryFree (sep);
661         }
662       }
663       sep = GetTopSeqEntryForEntityID (entityID);
664     }
665 
666     if (sep != NULL) {
667       if (extra == NULL || extra->gbseq == NULL) {
668         FileRemove (outputFile);
669 #ifdef WIN_MAC
670         FileCreate (outputFile, "TEXT", "ttxt");
671 #endif
672         ofp = FileOpen (outputFile, "w");
673       }
674 
675       if ((from > 0 && to > 0) || strand == Seq_strand_minus) {
676         bsp = NULL;
677         if (format == GENPEPT_FMT) {
678           VisitSequencesInSep (sep, (Pointer) &bsp, VISIT_PROTS, GetFirstGoodBioseq);
679         } else {
680           VisitSequencesInSep (sep, (Pointer) &bsp, VISIT_NUCS, GetFirstGoodBioseq);
681         }
682         if (bsp != NULL) {
683           if (strand == Seq_strand_minus && from == 0 && to == 0) {
684             from = 1;
685             to = bsp->length;
686           }
687           if (from < 0) {
688             from = 1;
689           } else if (from > bsp->length) {
690             from = bsp->length;
691           }
692           if (to < 0) {
693             to = 1;
694           } else if (to > bsp->length) {
695             to = bsp->length;
696           }
697           MemSet ((Pointer) &vn, 0, sizeof (ValNode));
698           MemSet ((Pointer) &sint, 0, sizeof (SeqInt));
699           sint.from = from - 1;
700           sint.to = to - 1;
701           sint.strand = strand;
702           sint.id = SeqIdFindBest (bsp->id, 0);
703           vn.choice = SEQLOC_INT;
704           vn.data.ptrvalue = (Pointer) &sint;
705           slp = &vn;
706         }
707       } else if (itemID > 0) {
708         sfp = SeqMgrGetDesiredFeature (entityID, 0, itemID, 0, NULL, NULL);
709         if (sfp != NULL) {
710           slp = sfp->location;
711         }
712       }
713 
714       if (do_tiny_seq) {
715         aip = AsnIoNew (ASNIO_TEXT_OUT | ASNIO_XML, ofp, NULL, NULL, NULL);
716         VisitBioseqsInSep (sep, (Pointer) aip, SaveTinySeqs);
717         AsnIoFree (aip, FALSE);
718       } else if (do_fasta_stream) {
719         aip = AsnIoNew (ASNIO_TEXT_OUT | ASNIO_XML, ofp, NULL, NULL, NULL);
720         VisitBioseqsInSep (sep, (Pointer) aip, SaveTinyStreams);
721         AsnIoFree (aip, FALSE);
722       } else {
723         SeqEntryToGnbk (sep, slp, format, mode, style, flags, locks, custom, extra, ofp);
724         if (altformat != 0) {
725           SeqEntryToGnbk (sep, slp, altformat, mode, style, flags, locks, custom, extra, ofp);
726         }
727       }
728       if (ofp != NULL) {
729         FileClose (ofp);
730       }
731     }
732   } else {
733     Message (MSG_POSTERR, "Datatype %d not recognized", (int) datatype);
734   }
735 
736   omp = ObjMgrGet ();
737   ObjMgrReapOne (omp);
738   SeqMgrClearBioseqIndex ();
739   ObjMgrFreeCache (0);
740   FreeSeqIdGiCache ();
741 
742   SeqEntrySetScope (NULL);
743 
744   ObjMgrFree (datatype, dataptr);
745 
746   return 0;
747 }
748 
HandleCatenatedRecord(CharPtr inputFile,CharPtr outputFile,FmtType format,FmtType altformat,ModType mode,StlType style,FlgType flags,LckType locks,CstType custom,XtraPtr extra,Int2 type,Boolean binary,Boolean compressed,Int4 from,Int4 to,Uint1 strand,Uint4 itemID,Boolean do_tiny_seq,Boolean do_fasta_stream)749 static Int2 HandleCatenatedRecord (
750   CharPtr inputFile,
751   CharPtr outputFile,
752   FmtType format,
753   FmtType altformat,
754   ModType mode,
755   StlType style,
756   FlgType flags,
757   LckType locks,
758   CstType custom,
759   XtraPtr extra,
760   Int2 type,
761   Boolean binary,
762   Boolean compressed,
763   Int4 from,
764   Int4 to,
765   Uint1 strand,
766   Uint4 itemID,
767   Boolean do_tiny_seq,
768   Boolean do_fasta_stream
769 )
770 
771 {
772   AsnIoPtr     aip;
773   BioseqPtr    bsp;
774   Pointer      dataptr = NULL;
775   Uint2        datatype = 0;
776   Uint2        entityID = 0;
777   FILE         *fp;
778   FILE         *ofp = NULL;
779   ObjMgrPtr    omp;
780   SeqEntryPtr  sep;
781   SeqFeatPtr   sfp;
782   SeqInt       sint;
783   SeqLocPtr    slp = NULL;
784   ValNode      vn;
785 
786   fp = FileOpen (inputFile, "r");
787   if (fp == NULL) {
788     Message (MSG_POSTERR, "FileOpen failed for input file '%s'", inputFile);
789     return 1;
790   }
791 
792   SeqMgrHoldIndexing (TRUE);
793   dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE);
794   SeqMgrHoldIndexing (FALSE);
795 
796   if (extra == NULL || extra->gbseq == NULL) {
797     FileRemove (outputFile);
798 #ifdef WIN_MAC
799     FileCreate (outputFile, "TEXT", "ttxt");
800 #endif
801     ofp = FileOpen (outputFile, "w");
802   }
803 
804   while (dataptr != NULL) {
805 
806     entityID = ObjMgrRegister (datatype, dataptr);
807     sep = GetTopSeqEntryForEntityID (entityID);
808 
809 
810     if (sep != NULL) {
811       if ((from > 0 && to > 0) || strand == Seq_strand_minus) {
812         bsp = NULL;
813         if (format == GENPEPT_FMT) {
814           VisitSequencesInSep (sep, (Pointer) &bsp, VISIT_PROTS, GetFirstGoodBioseq);
815         } else {
816           VisitSequencesInSep (sep, (Pointer) &bsp, VISIT_NUCS, GetFirstGoodBioseq);
817         }
818         if (bsp != NULL) {
819           if (strand == Seq_strand_minus && from == 0 && to == 0) {
820             from = 1;
821             to = bsp->length;
822           }
823           if (from < 0) {
824             from = 1;
825           } else if (from > bsp->length) {
826             from = bsp->length;
827           }
828           if (to < 0) {
829             to = 1;
830           } else if (to > bsp->length) {
831             to = bsp->length;
832           }
833           MemSet ((Pointer) &vn, 0, sizeof (ValNode));
834           MemSet ((Pointer) &sint, 0, sizeof (SeqInt));
835           sint.from = from - 1;
836           sint.to = to - 1;
837           sint.strand = strand;
838           sint.id = SeqIdFindBest (bsp->id, 0);
839           vn.choice = SEQLOC_INT;
840           vn.data.ptrvalue = (Pointer) &sint;
841           slp = &vn;
842         }
843       } else if (itemID > 0) {
844         sfp = SeqMgrGetDesiredFeature (entityID, 0, itemID, 0, NULL, NULL);
845         if (sfp != NULL) {
846           slp = sfp->location;
847         }
848       }
849 
850       if (do_tiny_seq) {
851         aip = AsnIoNew (ASNIO_TEXT_OUT | ASNIO_XML, ofp, NULL, NULL, NULL);
852         VisitBioseqsInSep (sep, (Pointer) aip, SaveTinySeqs);
853         AsnIoFree (aip, FALSE);
854       } else if (do_fasta_stream) {
855         aip = AsnIoNew (ASNIO_TEXT_OUT | ASNIO_XML, ofp, NULL, NULL, NULL);
856         VisitBioseqsInSep (sep, (Pointer) aip, SaveTinyStreams);
857         AsnIoFree (aip, FALSE);
858       } else {
859         SeqEntryToGnbk (sep, slp, format, mode, style, flags, locks, custom, extra, ofp);
860         if (altformat != 0) {
861           SeqEntryToGnbk (sep, slp, altformat, mode, style, flags, locks, custom, extra, ofp);
862         }
863       }
864     }
865 
866     ObjMgrFree (datatype, dataptr);
867 
868     omp = ObjMgrGet ();
869     ObjMgrReapOne (omp);
870     SeqMgrClearBioseqIndex ();
871     ObjMgrFreeCache (0);
872     FreeSeqIdGiCache ();
873 
874     SeqEntrySetScope (NULL);
875 
876     SeqMgrHoldIndexing (TRUE);
877     dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE);
878     SeqMgrHoldIndexing (FALSE);
879   }
880 
881   if (ofp != NULL) {
882     FileClose (ofp);
883   }
884 
885   FileClose (fp);
886 
887   return 0;
888 }
889 
890 typedef struct hasgidata {
891   Int4     gi;
892   CharPtr  accn;
893   Boolean  found;
894 } HasGiData, PNTR HasGiPtr;
895 
LookForGi(SeqEntryPtr sep,Pointer mydata,Int4 index,Int2 indent)896 static void LookForGi (
897   SeqEntryPtr sep,
898   Pointer mydata,
899   Int4 index,
900   Int2 indent
901 )
902 
903 {
904   BioseqPtr     bsp;
905   HasGiPtr      hgp;
906   SeqIdPtr      sip;
907   TextSeqIdPtr  tsip;
908 
909   if (sep == NULL) return;
910   if (! IS_Bioseq (sep)) return;
911   bsp = (BioseqPtr) sep->data.ptrvalue;
912   if (bsp == NULL) return;
913   hgp = (HasGiPtr) mydata;
914   if (hgp == NULL) return;
915   for (sip = bsp->id; sip != NULL; sip = sip->next) {
916     switch (sip->choice) {
917       case SEQID_GI :
918         if (sip->data.intvalue == hgp->gi) {
919           hgp->found = TRUE;
920           return;
921         }
922         break;
923       case SEQID_GENBANK :
924       case SEQID_EMBL :
925       case SEQID_PIR :
926       case SEQID_SWISSPROT :
927       case SEQID_OTHER :
928       case SEQID_DDBJ :
929       case SEQID_PRF :
930       case SEQID_TPG :
931       case SEQID_TPE :
932       case SEQID_TPD :
933       case SEQID_GPIPE :
934         tsip = (TextSeqIdPtr) sip->data.ptrvalue;
935         if (tsip != NULL && hgp->accn!= NULL &&
936             StringICmp (tsip->accession, hgp->accn) == 0) {
937           hgp->found = TRUE;
938           return;
939         }
940         break;
941       default :
942         break;
943     }
944   }
945 }
946 
SeqEntryHasGi(SeqEntryPtr sep,CharPtr accn)947 static Boolean SeqEntryHasGi (
948   SeqEntryPtr sep,
949   CharPtr accn
950 )
951 
952 {
953   HasGiData  hgd;
954   long int   val;
955 
956   if (sep == NULL || StringHasNoText (accn)) return FALSE;
957   MemSet ((Pointer) &hgd, 0, sizeof (HasGiData));
958   if (sscanf (accn, "%ld", &val) == 1) {
959     hgd.gi = (Int4) val;
960   } else {
961     hgd.accn = accn;
962   }
963   hgd.found = FALSE;
964   SeqEntryExplore (sep, (Pointer) (&hgd), LookForGi);
965   return hgd.found;
966 }
967 
FreeUnpubAffil(PubdescPtr pdp,Pointer userdata)968 static void FreeUnpubAffil (
969   PubdescPtr pdp,
970   Pointer userdata
971 )
972 
973 {
974   AuthListPtr  alp;
975   CitGenPtr    cgp;
976   ValNodePtr   vnp;
977 
978   if (pdp == NULL) return;
979   for (vnp = pdp->pub; vnp != NULL; vnp = vnp->next) {
980     if (vnp->choice != PUB_Gen) continue;
981     cgp = (CitGenPtr) vnp->data.ptrvalue;
982     if (cgp == NULL) continue;
983     if (cgp->cit != NULL) {
984       if (StringNICmp (cgp->cit, "submitted", 8) == 0 ||
985                        StringNICmp (cgp->cit, "unpublished", 11) == 0 ||
986                        StringNICmp (cgp->cit, "in press", 8) == 0 ||
987                        StringNICmp (cgp->cit, "to be published", 15) == 0) {
988         cgp->cit = MemFree (cgp->cit);
989         cgp->cit = StringSave ("Unpublished");
990       }
991     }
992     alp = cgp->authors;
993     if (alp == NULL) continue;
994     alp->affil = AffilFree (alp->affil);
995   }
996 }
997 
LookForRefSeq(BioseqPtr bsp,Pointer userdata)998 static void LookForRefSeq (
999   BioseqPtr bsp,
1000   Pointer userdata
1001 )
1002 
1003 {
1004   BoolPtr   hasRefseqP;
1005   SeqIdPtr  sip;
1006 
1007   hasRefseqP = (BoolPtr) userdata;
1008   if (*hasRefseqP) return;
1009   for (sip = bsp->id; sip != NULL; sip = sip->next) {
1010     if (sip->choice == SEQID_OTHER) {
1011       *hasRefseqP = TRUE;
1012       return;
1013     }
1014   }
1015 }
1016 
1017 static CharPtr fffmt [] = {
1018   "",
1019   "genbank",
1020   "embl",
1021   "genbank",
1022   "embl",
1023   "ftable",
1024   NULL
1025 };
1026 
1027 static CharPtr ffmod [] = {
1028   "",
1029   "release",
1030   "entrez",
1031   "gbench",
1032   "dump",
1033   NULL
1034 };
1035 
1036 static CharPtr ffstl [] = {
1037   "",
1038   "normal",
1039   "segment",
1040   "master",
1041   "contig",
1042   NULL
1043 };
1044 
1045 static CharPtr ffvew [] = {
1046   "",
1047   "nuc",
1048   "nuc",
1049   "prot",
1050   "prot",
1051   "nuc",
1052   NULL
1053 };
1054 
ReportDiffs(CharPtr path1,CharPtr path2,CharPtr path3,FILE * fp,CharPtr ffdiff,Boolean useFfdiff)1055 static void ReportDiffs (
1056   CharPtr path1,
1057   CharPtr path2,
1058   CharPtr path3,
1059   FILE* fp,
1060   CharPtr ffdiff,
1061   Boolean useFfdiff
1062 )
1063 
1064 {
1065   Char    buf [256];
1066   Char    cmmd [256];
1067   size_t  ct;
1068   FILE    *fpo;
1069 
1070   if (useFfdiff) {
1071     sprintf (cmmd, "%s -o %s -n %s -d reports", ffdiff, path1, path2);
1072     system (cmmd);
1073 
1074     sprintf (cmmd, "rm %s; rm %s", path1, path2);
1075     system (cmmd);
1076   } else {
1077     sprintf (cmmd, "sort %s | uniq -c > %s.suc; rm %s", path1, path1, path1);
1078     system (cmmd);
1079 
1080     sprintf (cmmd, "sort %s | uniq -c > %s.suc; rm %s", path2, path2, path2);
1081     system (cmmd);
1082 
1083     sprintf (cmmd, "diff %s.suc %s.suc > %s", path1, path2, path3);
1084     system (cmmd);
1085 
1086     sprintf (cmmd, "cat %s", path3);
1087     fpo = popen (cmmd, "r");
1088     if (fpo != NULL) {
1089       while ((ct = fread (buf, 1, sizeof (buf), fpo)) > 0) {
1090         fwrite (buf, 1, ct, fp);
1091         fflush (fp);
1092       }
1093       pclose (fpo);
1094     }
1095 
1096     sprintf (cmmd, "rm %s.suc; rm %s.suc", path1, path2);
1097     system (cmmd);
1098   }
1099 }
1100 
CompareFlatFiles(CharPtr path1,CharPtr path2,CharPtr path3,SeqEntryPtr sep,FILE * fp,FmtType format,FmtType altformat,ModType mode,StlType style,FlgType flags,LckType locks,CstType custom,XtraPtr extra,Int2 batch,CharPtr ffdiff,CharPtr asn2flat,Boolean useFfdiff)1101 static void CompareFlatFiles (
1102   CharPtr path1,
1103   CharPtr path2,
1104   CharPtr path3,
1105   SeqEntryPtr sep,
1106   FILE* fp,
1107   FmtType format,
1108   FmtType altformat,
1109   ModType mode,
1110   StlType style,
1111   FlgType flags,
1112   LckType locks,
1113   CstType custom,
1114   XtraPtr extra,
1115   Int2 batch,
1116   CharPtr ffdiff,
1117   CharPtr asn2flat,
1118   Boolean useFfdiff
1119 )
1120 
1121 {
1122 #ifdef OS_UNIX
1123   AsnIoPtr     aip;
1124   Char         arguments [128];
1125   BioseqPtr    bsp;
1126   Char         buf [256];
1127   Char         cmmd [256];
1128   size_t       ct;
1129   int          diff;
1130   FILE         *fpo;
1131   SeqEntryPtr  fsep;
1132 
1133   if (sep == NULL) return;
1134 
1135   if (batch == 1) {
1136 
1137     SeqEntryToGnbk (sep, NULL, format, mode, style, flags, locks, custom, extra, fp);
1138     if (altformat != 0) {
1139       SeqEntryToGnbk (sep, NULL, altformat, mode, style, flags, locks, custom, extra, fp);
1140     }
1141     return; /* just make report, nothing to diff */
1142 
1143   } else if (batch == 2) {
1144 
1145 #ifdef ASN2GNBK_SUPPRESS_UNPUB_AFFIL
1146     VisitPubdescsInSep (sep, NULL, FreeUnpubAffil);
1147 #endif
1148 
1149     SaveAsn2gnbk (sep, path1, format, SEQUIN_MODE, style, flags, locks, custom);
1150     SaveAsn2gnbk (sep, path2, format, RELEASE_MODE, style, flags, locks, custom);
1151 
1152     ReportDiffs (path1, path2, path3, fp, ffdiff, useFfdiff);
1153 
1154   } else if (batch == 3) {
1155 
1156 #ifdef ASN2GNBK_SUPPRESS_UNPUB_AFFIL
1157     VisitPubdescsInSep (sep, NULL, FreeUnpubAffil);
1158 #endif
1159 
1160     SaveAsn2gnbk (sep, path1, format, mode, style, flags, locks, custom);
1161     SeriousSeqEntryCleanupBulk (sep);
1162     SaveAsn2gnbk (sep, path2, format, mode, style, flags, locks, custom);
1163 
1164     ReportDiffs (path1, path2, path3, fp, ffdiff, useFfdiff);
1165 
1166   } else if (batch == 4) {
1167 
1168     aip = AsnIoOpen (path3, "w");
1169     if (aip == NULL) return;
1170 
1171     SeqEntryAsnWrite (sep, aip, NULL);
1172     AsnIoClose (aip);
1173 
1174     fsep = FindNthBioseq (sep, 1);
1175     if (fsep == NULL || fsep->choice != 1) return;
1176     bsp = (BioseqPtr) fsep->data.ptrvalue;
1177     if (bsp == NULL) return;
1178     SeqIdWrite (bsp->id, buf, PRINTID_FASTA_LONG, sizeof (buf));
1179 
1180     arguments [0] = '\0';
1181     sprintf (arguments, "-format %s -mode %s -style %s -view %s -nocleanup",
1182              fffmt [(int) format], ffmod [(int) mode], ffstl [(int) style], ffvew [(int) format]);
1183 
1184     sprintf (cmmd, "%s %s -i %s -o %s", asn2flat, arguments, path3, path1);
1185     system (cmmd);
1186 
1187     arguments [0] = '\0';
1188     sprintf (arguments, "-format %s -mode %s -style %s -view %s",
1189              fffmt [(int) format], ffmod [(int) mode], ffstl [(int) style], ffvew [(int) format]);
1190 
1191     sprintf (cmmd, "%s %s -i %s -o %s", asn2flat, arguments, path3, path2);
1192     system (cmmd);
1193 
1194     ReportDiffs (path1, path2, path3, fp, ffdiff, useFfdiff);
1195 
1196   } else if (batch == 5) {
1197 
1198     SaveAsn2gnbk (sep, path1, format, mode, style, flags, locks, custom);
1199 
1200     aip = AsnIoOpen (path3, "w");
1201     if (aip == NULL) return;
1202 
1203     SeqEntryAsnWrite (sep, aip, NULL);
1204     AsnIoClose (aip);
1205 
1206     fsep = FindNthBioseq (sep, 1);
1207     if (fsep == NULL || fsep->choice != 1) return;
1208     bsp = (BioseqPtr) fsep->data.ptrvalue;
1209     if (bsp == NULL) return;
1210     SeqIdWrite (bsp->id, buf, PRINTID_FASTA_LONG, sizeof (buf));
1211 
1212     arguments [0] = '\0';
1213     sprintf (arguments, "-format %s -mode %s -style %s -view %s",
1214              fffmt [(int) format], ffmod [(int) mode], ffstl [(int) style], ffvew [(int) format]);
1215 
1216     sprintf (cmmd, "%s %s -i %s -o %s", asn2flat, arguments, path3, path2);
1217     system (cmmd);
1218 
1219     ReportDiffs (path1, path2, path3, fp, ffdiff, useFfdiff);
1220 
1221   } else if (batch == 6) {
1222 
1223 #ifdef ASN2GNBK_SUPPRESS_UNPUB_AFFIL
1224     VisitPubdescsInSep (sep, NULL, FreeUnpubAffil);
1225 #endif
1226 
1227     SaveAsn2gnbk (sep, path1, format, ENTREZ_MODE, style, (flags | 1), locks, custom);
1228     SaveAsn2gnbk (sep, path2, format, ENTREZ_MODE, style, (flags | 1 | 262144), locks, custom);
1229 
1230     ReportDiffs (path1, path2, path3, fp, ffdiff, useFfdiff);
1231 
1232   } else if (batch == 7) {
1233 
1234     aip = AsnIoOpen (path3, "w");
1235     if (aip == NULL) return;
1236 
1237     SeqEntryAsnWrite (sep, aip, NULL);
1238     AsnIoClose (aip);
1239 
1240     if (FindNucBioseq (sep) != NULL) {
1241 
1242       sprintf (cmmd, "./oldasn2gb -i %s -o %s", path3, path1);
1243       system (cmmd);
1244 
1245       sprintf (cmmd, "./newasn2gb -i %s -o %s", path3, path2);
1246       system (cmmd);
1247 
1248     } else {
1249 
1250       sprintf (cmmd, "./oldasn2gb -f p -i %s -o %s", path3, path1);
1251       system (cmmd);
1252 
1253       sprintf (cmmd, "./newasn2gb -f p -i %s -o %s", path3, path2);
1254       system (cmmd);
1255 
1256     }
1257 
1258     sprintf (cmmd, "diff -b %s %s > %s", path1, path2, path3);
1259     diff = system (cmmd);
1260 
1261     fsep = FindNthBioseq (sep, 1);
1262     if (fsep == NULL || fsep->choice != 1) return;
1263     bsp = (BioseqPtr) fsep->data.ptrvalue;
1264     if (bsp == NULL) return;
1265     SeqIdWrite (bsp->id, buf, PRINTID_FASTA_LONG, sizeof (buf));
1266 
1267     if (diff > 0) {
1268       sprintf (cmmd, "cat %s", path3);
1269       fpo = popen (cmmd, "r");
1270       if (fpo != NULL) {
1271         fprintf (fp, "\nasn2gb difference in %s\n", buf);
1272         fflush (fp);
1273         while ((ct = fread (buf, 1, sizeof (buf), fpo)) > 0) {
1274           fwrite (buf, 1, ct, fp);
1275           fflush (fp);
1276         }
1277         pclose (fpo);
1278       }
1279     }
1280   }
1281 
1282 #else
1283 
1284   SeqEntryToGnbk (sep, NULL, format, mode, style, flags, locks, custom, extra, fp);
1285   if (altformat != 0) {
1286     SeqEntryToGnbk (sep, NULL, altformat, mode, style, flags, locks, custom, extra, fp);
1287   }
1288 #endif
1289 }
1290 
CheckOrder(SeqFeatPtr sfp,Pointer userdata)1291 static void CheckOrder (
1292   SeqFeatPtr sfp,
1293   Pointer userdata
1294 )
1295 
1296 {
1297 #ifdef ASN2GNBK_IGNORE_OUT_OF_ORDER
1298   BoolPtr    bp;
1299   BioseqPtr  bsp;
1300 #endif
1301 #ifdef ASN2GNBK_REPAIR_OUT_OF_ORDER
1302   BioseqPtr  bsp;
1303   SeqLocPtr  gslp;
1304   Boolean    hasNulls;
1305   Boolean    noLeft;
1306   Boolean    noRight;
1307 #endif
1308 
1309   /* ignore order of bonds in heterogen features from PDB */
1310 
1311   if (sfp->data.choice == SEQFEAT_HET) return;
1312 
1313 #ifdef ASN2GNBK_IGNORE_OUT_OF_ORDER
1314   bsp = BioseqFindFromSeqLoc (sfp->location);
1315   if (bsp != NULL && SeqLocBadSortOrder (bsp, sfp->location)) {
1316     bp = (BoolPtr) userdata;
1317     *bp = TRUE;
1318   }
1319 #endif
1320 #ifdef ASN2GNBK_REPAIR_OUT_OF_ORDER
1321   bsp = BioseqFindFromSeqLoc (sfp->location);
1322   if (bsp != NULL && SeqLocBadSortOrder (bsp, sfp->location)) {
1323     hasNulls = LocationHasNullsBetween (sfp->location);
1324     gslp = SeqLocMerge (bsp, sfp->location, NULL, FALSE, FALSE, hasNulls);
1325     if (gslp != NULL) {
1326       CheckSeqLocForPartial (sfp->location, &noLeft, &noRight);
1327       sfp->location = SeqLocFree (sfp->location);
1328       sfp->location = gslp;
1329       if (bsp->repr == Seq_repr_seg) {
1330         gslp = SegLocToParts (bsp, sfp->location);
1331         sfp->location = SeqLocFree (sfp->location);
1332         sfp->location = gslp;
1333       }
1334       FreeAllFuzz (sfp->location);
1335       SetSeqLocPartial (sfp->location, noLeft, noRight);
1336     }
1337   }
1338 #endif
1339 }
1340 
HandleMultipleRecords(CharPtr inputFile,CharPtr outputFile,FmtType format,FmtType altformat,ModType mode,StlType style,FlgType flags,LckType locks,CstType custom,XtraPtr extra,Int2 type,Int2 batch,Boolean binary,Boolean compressed,Boolean propOK,CharPtr ffdiff,CharPtr asn2flat,CharPtr accn,FILE * logfp)1341 static Int2 HandleMultipleRecords (
1342   CharPtr inputFile,
1343   CharPtr outputFile,
1344   FmtType format,
1345   FmtType altformat,
1346   ModType mode,
1347   StlType style,
1348   FlgType flags,
1349   LckType locks,
1350   CstType custom,
1351   XtraPtr extra,
1352   Int2 type,
1353   Int2 batch,
1354   Boolean binary,
1355   Boolean compressed,
1356   Boolean propOK,
1357   CharPtr ffdiff,
1358   CharPtr asn2flat,
1359   CharPtr accn,
1360   FILE *logfp
1361 )
1362 
1363 {
1364   AsnIoPtr        aip;
1365   AsnModulePtr    amp;
1366   AsnTypePtr      atp, atp_bss, atp_desc, atp_sbp, atp_se = NULL, atp_ssp;
1367   Boolean         atp_se_seen = FALSE;
1368   BioseqPtr       bsp;
1369   BioseqSetPtr    bssp;
1370   Char            buf [41];
1371   Char            cmmd [256];
1372   CitSubPtr       csp = NULL;
1373   SeqDescrPtr     descr = NULL;
1374   FILE            *fp;
1375   SeqEntryPtr     fsep;
1376   Boolean         hasgi;
1377   Boolean         hasRefSeq;
1378   Boolean         io_failure = FALSE;
1379   Char            longest [41];
1380   Int4            numrecords = 0;
1381   FILE            *ofp = NULL;
1382   ObjMgrPtr       omp;
1383   Boolean         outOfOrder;
1384   ObjValNode      ovn;
1385   Char            path1 [PATH_MAX];
1386   Char            path2 [PATH_MAX];
1387   Char            path3 [PATH_MAX];
1388   Pubdesc         pd;
1389   SubmitBlockPtr  sbp = NULL;
1390   SeqEntryPtr     sep;
1391   time_t          starttime, stoptime, worsttime;
1392   SeqDescrPtr     subcit = NULL;
1393   FILE            *tfp;
1394   Boolean         useFfdiff;
1395   ValNode         vn;
1396 #ifdef OS_UNIX
1397   CharPtr         gzcatprog;
1398   int             ret;
1399   Boolean         usedPopen = FALSE;
1400 #endif
1401 
1402   if (StringHasNoText (inputFile)) return 1;
1403 
1404 #ifndef OS_UNIX
1405   if (compressed) {
1406     Message (MSG_POSTERR, "Can only decompress on-the-fly on UNIX machines");
1407     return 1;
1408   }
1409 #endif
1410 
1411   amp = AsnAllModPtr ();
1412   if (amp == NULL) {
1413     Message (MSG_POSTERR, "Unable to load AsnAllModPtr");
1414     return 1;
1415   }
1416 
1417   atp_ssp = AsnFind ("Seq-submit");
1418   if (atp_ssp == NULL) {
1419     Message (MSG_POSTERR, "Unable to find ASN.1 type Seq-submit");
1420     return 1;
1421   }
1422 
1423   atp_sbp = AsnFind ("Seq-submit.sub");
1424   if (atp_sbp == NULL) {
1425     Message (MSG_POSTERR, "Unable to find ASN.1 type Seq-submit.sub");
1426     return 1;
1427   }
1428 
1429   atp_bss = AsnFind ("Bioseq-set");
1430   if (atp_bss == NULL) {
1431     Message (MSG_POSTERR, "Unable to find ASN.1 type Bioseq-set");
1432     return 1;
1433   }
1434 
1435   atp_desc = AsnFind ("Bioseq-set.descr");
1436   if (atp_desc == NULL) {
1437     Message (MSG_POSTERR, "Unable to find ASN.1 type Bioseq-set.descr");
1438     return 1;
1439   }
1440 
1441   if (type == 4) {
1442     atp_se = AsnFind ("Bioseq-set.seq-set.E");
1443     if (atp_se == NULL) {
1444       Message (MSG_POSTERR, "Unable to find ASN.1 type Bioseq-set.seq-set.E");
1445       return 1;
1446     }
1447   } else if (type == 5) {
1448     /*
1449     atp_se = AsnFind ("Seq-submit.data.entrys.E");
1450     if (atp_se == NULL) {
1451       Message (MSG_POSTERR, "Unable to find ASN.1 type Seq-submit.data.entrys.E");
1452       return 1;
1453     }
1454     */
1455     /* current use has genbank set containing batch, so iterate genbank set within Seq-submit */
1456     atp_se = AsnFind ("Bioseq-set.seq-set.E");
1457     if (atp_se == NULL) {
1458       Message (MSG_POSTERR, "Unable to find ASN.1 type Bioseq-set.seq-set.E");
1459       return 1;
1460     }
1461   } else {
1462     Message (MSG_POSTERR, "Batch processing type not set properly");
1463     return 1;
1464   }
1465 
1466   if (atp_se == NULL) {
1467     Message (MSG_POSTERR, "Unable to find ASN.1 type for atp_se");
1468     return 1;
1469   }
1470 
1471 #ifdef OS_UNIX
1472   if (compressed) {
1473     gzcatprog = getenv ("NCBI_UNCOMPRESS_BINARY");
1474     if (gzcatprog != NULL) {
1475       sprintf (cmmd, "%s %s", gzcatprog, inputFile);
1476     } else {
1477       ret = system ("gzcat -h >/dev/null 2>&1");
1478       if (ret == 0) {
1479         sprintf (cmmd, "gzcat %s", inputFile);
1480       } else if (ret == -1) {
1481         Message (MSG_POSTERR, "Unable to fork or exec gzcat in ScanBioseqSetRelease");
1482         return 1;
1483       } else {
1484         ret = system ("zcat -h >/dev/null 2>&1");
1485         if (ret == 0) {
1486           sprintf (cmmd, "zcat %s", inputFile);
1487         } else if (ret == -1) {
1488           Message (MSG_POSTERR, "Unable to fork or exec zcat in ScanBioseqSetRelease");
1489           return 1;
1490         } else {
1491           Message (MSG_POSTERR, "Unable to find zcat or gzcat in ScanBioseqSetRelease - please edit your PATH environment variable");
1492           return 1;
1493         }
1494       }
1495     }
1496     fp = popen (cmmd, /* binary? "rb" : */ "r");
1497     usedPopen = TRUE;
1498   } else {
1499     fp = FileOpen (inputFile, binary? "rb" : "r");
1500   }
1501 #else
1502   fp = FileOpen (inputFile, binary? "rb" : "r");
1503 #endif
1504   if (fp == NULL) {
1505     Message (MSG_POSTERR, "FileOpen failed for input file '%s'", inputFile);
1506     return 1;
1507   }
1508 
1509   aip = AsnIoNew (binary? ASNIO_BIN_IN : ASNIO_TEXT_IN, fp, NULL, NULL, NULL);
1510   if (aip == NULL) {
1511     Message (MSG_POSTERR, "AsnIoNew failed for input file '%s'", inputFile);
1512     return 1;
1513   }
1514 
1515   if ((batch == 1 || batch == 4 || batch == 5 || batch == 7 || format != GENBANK_FMT) &&
1516       (extra == NULL || extra->gbseq == NULL)) {
1517     ofp = FileOpen (outputFile, "w");
1518     if (ofp == NULL) {
1519       AsnIoClose (aip);
1520       Message (MSG_POSTERR, "FileOpen failed for output file '%s'", outputFile);
1521       return 1;
1522     }
1523   }
1524 
1525   TmpNam (path1);
1526   tfp = FileOpen (path1, "w");
1527   fprintf (tfp, "\n");
1528   FileClose (tfp);
1529 
1530   TmpNam (path2);
1531   tfp = FileOpen (path2, "w");
1532   fprintf (tfp, "\n");
1533   FileClose (tfp);
1534 
1535   TmpNam (path3);
1536   tfp = FileOpen (path3, "w");
1537   fprintf (tfp, "\n");
1538   FileClose (tfp);
1539 
1540   if (type == 4) {
1541     atp = atp_bss;
1542   } else if (type == 5) {
1543     atp = atp_ssp;
1544   } else {
1545     Message (MSG_POSTERR, "Batch processing type not set properly");
1546     return 1;
1547   }
1548 
1549   longest [0] = '\0';
1550   worsttime = 0;
1551 
1552   while ((! io_failure) && (atp = AsnReadId (aip, amp, atp)) != NULL) {
1553     if (aip->io_failure) {
1554       io_failure = TRUE;
1555       aip->io_failure = FALSE;
1556     }
1557     if (atp == atp_se) {
1558       atp_se_seen = TRUE;
1559 
1560       SeqMgrHoldIndexing (TRUE);
1561       sep = SeqEntryAsnRead (aip, atp);
1562       SeqMgrHoldIndexing (FALSE);
1563 
1564       /* propagate descriptors from the top-level set */
1565 
1566       if (propOK && descr != NULL && sep != NULL && sep->data.ptrvalue != NULL) {
1567         if (sep->choice == 1) {
1568           bsp = (BioseqPtr) sep->data.ptrvalue;
1569           ValNodeLink (&(bsp->descr),
1570                        AsnIoMemCopy ((Pointer) descr,
1571                                      (AsnReadFunc) SeqDescrAsnRead,
1572                                      (AsnWriteFunc) SeqDescrAsnWrite));
1573         } else if (sep->choice == 2) {
1574           bssp = (BioseqSetPtr) sep->data.ptrvalue;
1575           ValNodeLink (&(bssp->descr),
1576                        AsnIoMemCopy ((Pointer) descr,
1577                                      (AsnReadFunc) SeqDescrAsnRead,
1578                                      (AsnWriteFunc) SeqDescrAsnWrite));
1579         }
1580       }
1581 
1582       /* propagate submission citation as descriptor onto each Seq-entry */
1583 
1584       if (subcit != NULL && sep != NULL && sep->data.ptrvalue != NULL) {
1585         if (sep->choice == 1) {
1586           bsp = (BioseqPtr) sep->data.ptrvalue;
1587           ValNodeLink (&(bsp->descr),
1588                        AsnIoMemCopy ((Pointer) subcit,
1589                                      (AsnReadFunc) SeqDescrAsnRead,
1590                                      (AsnWriteFunc) SeqDescrAsnWrite));
1591         } else if (sep->choice == 2) {
1592           bssp = (BioseqSetPtr) sep->data.ptrvalue;
1593           ValNodeLink (&(bssp->descr),
1594                        AsnIoMemCopy ((Pointer) subcit,
1595                                      (AsnReadFunc) SeqDescrAsnRead,
1596                                      (AsnWriteFunc) SeqDescrAsnWrite));
1597         }
1598       }
1599 
1600       fsep = FindNthBioseq (sep, 1);
1601       if (fsep != NULL && fsep->choice == 1) {
1602         bsp = (BioseqPtr) fsep->data.ptrvalue;
1603         if (bsp != NULL) {
1604           SeqIdWrite (bsp->id, buf, PRINTID_FASTA_LONG, sizeof (buf));
1605 #ifdef OS_UNIX
1606           if (batch != 1) {
1607             printf ("%s\n", buf);
1608             fflush (stdout);
1609             if (batch != 4 && batch != 5) {
1610               if (ofp != NULL) {
1611                 fprintf (ofp, "%s\n", buf);
1612                 fflush (ofp);
1613               }
1614             }
1615           }
1616 #endif
1617           if (logfp != NULL) {
1618             fprintf (logfp, "%s\n", buf);
1619             fflush (logfp);
1620           }
1621         }
1622       }
1623 
1624       hasgi = SeqEntryHasGi (sep, accn);
1625       if (hasgi) {
1626         sprintf (buf, "%s.before", accn);
1627         SaveSeqEntry (sep, buf);
1628         sprintf (buf, "%s.gbff.before", accn);
1629         SaveAsn2gnbk (sep, buf, format, SEQUIN_MODE, NORMAL_STYLE, 0, 0, 0);
1630         if (ofp != NULL) {
1631           FileClose (ofp);
1632         }
1633         AsnIoClose (aip);
1634         return 0;
1635       }
1636       outOfOrder = FALSE;
1637 #ifdef ASN2GNBK_IGNORE_OUT_OF_ORDER
1638       VisitFeaturesInSep (sep, (Pointer) &outOfOrder, CheckOrder);
1639 #endif
1640 #ifdef ASN2GNBK_REPAIR_OUT_OF_ORDER
1641       VisitFeaturesInSep (sep, (Pointer) &outOfOrder, CheckOrder);
1642 #endif
1643       if ((! outOfOrder) && StringHasNoText (accn)) {
1644         if ((format != GENPEPT_FMT && SeqEntryHasNucs (sep)) ||
1645            (format == GENPEPT_FMT && SeqEntryHasProts (sep))) {
1646 
1647           hasRefSeq = FALSE;
1648           VisitBioseqsInSep (sep, (Pointer) &hasRefSeq, LookForRefSeq);
1649           if (hasRefSeq) {
1650             if (batch != 1 && format == GENBANK_FMT && ofp == NULL &&
1651                 (extra == NULL || extra->gbseq == NULL)) {
1652               ofp = FileOpen (outputFile, "w");
1653               if (ofp == NULL) {
1654                 ofp = stdout;
1655               }
1656             }
1657           }
1658 
1659           starttime = GetSecs ();
1660           useFfdiff = (Boolean) (format == GENBANK_FMT && (! hasRefSeq));
1661           CompareFlatFiles (path1, path2, path3, sep, ofp,
1662                             format, altformat, mode, style, flags, locks,
1663                             custom, extra, batch, ffdiff, asn2flat, useFfdiff);
1664           stoptime = GetSecs ();
1665           if (stoptime - starttime > worsttime) {
1666             worsttime = stoptime - starttime;
1667             StringCpy (longest, buf);
1668           }
1669           numrecords++;
1670         }
1671       }
1672       SeqEntryFree (sep);
1673 
1674       omp = ObjMgrGet ();
1675       ObjMgrReapOne (omp);
1676       SeqMgrClearBioseqIndex ();
1677       ObjMgrFreeCache (0);
1678       FreeSeqIdGiCache ();
1679 
1680       SeqEntrySetScope (NULL);
1681 
1682     } else if (atp == atp_desc && (! atp_se_seen)) {
1683       descr = SeqDescrAsnRead (aip, atp);
1684     } else if (atp == atp_sbp) {
1685       sbp = SubmitBlockAsnRead (aip, atp);
1686       if (sbp != NULL) {
1687         csp = sbp->cit;
1688         if (csp != NULL) {
1689           MemSet ((Pointer) &ovn, 0, sizeof (ObjValNode));
1690           MemSet ((Pointer) &pd, 0, sizeof (Pubdesc));
1691           MemSet ((Pointer) &vn, 0, sizeof (ValNode));
1692           vn.choice = PUB_Sub;
1693           vn.data.ptrvalue = (Pointer) csp;
1694           vn.next = NULL;
1695           pd.pub = &vn;
1696           ovn.vn.choice = Seq_descr_pub;
1697           ovn.vn.data.ptrvalue = (Pointer) &pd;
1698           ovn.vn.next = NULL;
1699           ovn.vn.extended = 1;
1700           subcit = (SeqDescrPtr) &ovn;
1701         }
1702       }
1703     } else {
1704       AsnReadVal (aip, atp, NULL);
1705     }
1706 
1707     if (aip->io_failure) {
1708       io_failure = TRUE;
1709       aip->io_failure = FALSE;
1710     }
1711   }
1712 
1713   if (aip->io_failure) {
1714     io_failure = TRUE;
1715   }
1716 
1717   if (io_failure) {
1718     Message (MSG_POSTERR, "Asn io_failure for input file '%s'", inputFile);
1719   }
1720 
1721   if (ofp != NULL) {
1722     FileClose (ofp);
1723   }
1724 
1725   AsnIoFree (aip, FALSE);
1726 
1727   SeqDescrFree (descr);
1728   SubmitBlockFree (sbp);
1729 
1730 #ifdef OS_UNIX
1731   if (usedPopen) {
1732     pclose (fp);
1733   } else {
1734     FileClose (fp);
1735   }
1736 #else
1737   FileClose (fp);
1738 #endif
1739 
1740   if (logfp != NULL && (! StringHasNoText (longest))) {
1741     fprintf (logfp, "Longest processing time %ld seconds on %s\n",
1742              (long) worsttime, longest);
1743     fprintf (logfp, "Total number of records %ld\n", (long) numrecords);
1744     fflush (logfp);
1745   }
1746 
1747   sprintf (cmmd, "rm %s; rm %s; rm %s", path1, path2, path3);
1748   system (cmmd);
1749 
1750   if (io_failure) return 1;
1751   return 0;
1752 }
1753 
1754 #include <lsqfetch.h>
1755 #include <pmfapi.h>
1756 #ifdef INTERNAL_NCBI_ASN2GB
1757 #include <accpubseq.h>
1758 #endif
1759 
ProcessOneSeqEntry(SeqEntryPtr sep,CharPtr outputFile,FmtType format,FmtType altformat,ModType mode,StlType style,FlgType flags,LckType locks,CstType custom,XtraPtr extra,Int4 from,Int4 to,Uint1 strand,Uint4 itemID,Boolean do_tiny_seq,Boolean do_fasta_stream)1760 static void ProcessOneSeqEntry (
1761   SeqEntryPtr sep,
1762   CharPtr outputFile,
1763   FmtType format,
1764   FmtType altformat,
1765   ModType mode,
1766   StlType style,
1767   FlgType flags,
1768   LckType locks,
1769   CstType custom,
1770   XtraPtr extra,
1771   Int4 from,
1772   Int4 to,
1773   Uint1 strand,
1774   Uint4 itemID,
1775   Boolean do_tiny_seq,
1776   Boolean do_fasta_stream
1777 )
1778 
1779 
1780 {
1781   AsnIoPtr    aip;
1782   BioseqPtr   bsp;
1783   Uint2       entityID;
1784   FILE        *ofp = NULL;
1785   SeqFeatPtr  sfp;
1786   SeqInt      sint;
1787   SeqLocPtr   slp = NULL;
1788   ValNode     vn;
1789 
1790   if (sep == NULL) return;
1791 
1792   if (extra == NULL || extra->gbseq == NULL) {
1793     FileRemove (outputFile);
1794 #ifdef WIN_MAC
1795     FileCreate (outputFile, "TEXT", "ttxt");
1796 #endif
1797     ofp = FileOpen (outputFile, "w");
1798   }
1799 
1800   if ((from > 0 && to > 0) || strand == Seq_strand_minus) {
1801     bsp = NULL;
1802     if (format == GENPEPT_FMT) {
1803       VisitSequencesInSep (sep, (Pointer) &bsp, VISIT_PROTS, GetFirstGoodBioseq);
1804     } else {
1805       VisitSequencesInSep (sep, (Pointer) &bsp, VISIT_NUCS, GetFirstGoodBioseq);
1806     }
1807     if (bsp != NULL) {
1808       if (strand == Seq_strand_minus && from == 0 && to == 0) {
1809         from = 1;
1810         to = bsp->length;
1811       }
1812       if (from < 0) {
1813         from = 1;
1814       } else if (from > bsp->length) {
1815         from = bsp->length;
1816       }
1817       if (to < 0) {
1818         to = 1;
1819       } else if (to > bsp->length) {
1820         to = bsp->length;
1821       }
1822       MemSet ((Pointer) &vn, 0, sizeof (ValNode));
1823       MemSet ((Pointer) &sint, 0, sizeof (SeqInt));
1824       sint.from = from - 1;
1825       sint.to = to - 1;
1826       sint.strand = strand;
1827       sint.id = SeqIdFindBest (bsp->id, 0);
1828       vn.choice = SEQLOC_INT;
1829       vn.data.ptrvalue = (Pointer) &sint;
1830       slp = &vn;
1831     }
1832   } else if (itemID > 0) {
1833     bsp = NULL;
1834     VisitBioseqsInSep (sep, (Pointer) &bsp, GetFirstGoodBioseq);
1835     if (bsp != NULL) {
1836       entityID = ObjMgrGetEntityIDForPointer (bsp);
1837       sfp = SeqMgrGetDesiredFeature (entityID, 0, itemID, 0, NULL, NULL);
1838       if (sfp != NULL) {
1839         slp = sfp->location;
1840       }
1841     }
1842   }
1843 
1844   if (do_tiny_seq) {
1845     aip = AsnIoNew (ASNIO_TEXT_OUT | ASNIO_XML, ofp, NULL, NULL, NULL);
1846     VisitBioseqsInSep (sep, (Pointer) aip, SaveTinySeqs);
1847     AsnIoFree (aip, FALSE);
1848   } else if (do_fasta_stream) {
1849     aip = AsnIoNew (ASNIO_TEXT_OUT | ASNIO_XML, ofp, NULL, NULL, NULL);
1850     VisitBioseqsInSep (sep, (Pointer) aip, SaveTinyStreams);
1851     AsnIoFree (aip, FALSE);
1852   } else {
1853     SeqEntryToGnbk (sep, slp, format, mode, style, flags, locks, custom, extra, ofp);
1854     if (altformat != 0) {
1855       SeqEntryToGnbk (sep, slp, altformat, mode, style, flags, locks, custom, extra, ofp);
1856     }
1857   }
1858   if (ofp != NULL) {
1859     FileClose (ofp);
1860   }
1861 }
1862 
SeqEntryFromAccnOrGi(CharPtr str)1863 static SeqEntryPtr SeqEntryFromAccnOrGi (
1864   CharPtr str
1865 )
1866 
1867 {
1868   CharPtr      accn;
1869   BioseqPtr    bsp;
1870   Char         buf [64];
1871   Int4         flags = 0;
1872   Int2         retcode = 0;
1873   SeqEntryPtr  sep = NULL;
1874   SeqIdPtr     sip;
1875   CharPtr      tmp1 = NULL;
1876   CharPtr      tmp2 = NULL;
1877   long int     val;
1878 #ifdef INTERNAL_NCBI_ASN2GB
1879   Int4         uid;
1880 #endif
1881 
1882   if (StringHasNoText (str)) return NULL;
1883   StringNCpy_0 (buf, str, sizeof (buf));
1884   TrimSpacesAroundString (buf);
1885 
1886   accn = buf;
1887   tmp1 = StringChr (accn, ',');
1888   if (tmp1 != NULL) {
1889     *tmp1 = '\0';
1890     tmp1++;
1891     tmp2 = StringChr (tmp1, ',');
1892     if (tmp2 != NULL) {
1893       *tmp2 = '\0';
1894       tmp2++;
1895       if (StringDoesHaveText (tmp2) && sscanf (tmp2, "%ld", &val) == 1) {
1896         flags = (Int4) val;
1897       }
1898     }
1899     if (StringDoesHaveText (tmp1) && sscanf (tmp1, "%ld", &val) == 1) {
1900       retcode = (Int2) val;
1901     }
1902   }
1903 
1904 #ifdef INTERNAL_NCBI_ASN2GB
1905   /* temporary code to test PUBSEQGetAccnVer in accpubseq.c */
1906 
1907   if (*accn == '*') {
1908     Char buf [64];
1909     accn++;
1910     if (sscanf (accn, "%ld", &val) == 1) {
1911       uid = (Int4) val;
1912       if (GetAccnVerFromServer (uid, buf)) {
1913         Message (MSG_POST, "GetAccnVerFromServer returned %s", buf);
1914       } else {
1915         Message (MSG_POST, "GetAccnVerFromServer failed");
1916       }
1917     }
1918     return NULL;
1919   }
1920 #endif
1921 
1922   sip = SeqIdFromPubSeqString (accn);
1923   sep = PubSeqSynchronousQueryId (sip, retcode, flags);
1924 
1925   if (sep != NULL) {
1926     bsp = BioseqFind (sip);
1927     if (bsp != NULL) {
1928       sep = SeqMgrGetSeqEntryForData ((Pointer) bsp);
1929     }
1930   }
1931   sip = SeqIdFree (sip);
1932   return sep;
1933 }
1934 
MarkLocalAnnots(SeqAnnotPtr sap,Pointer userdata)1935 static void MarkLocalAnnots (
1936   SeqAnnotPtr sap,
1937   Pointer userdata
1938 )
1939 
1940 {
1941   if (sap == NULL) return;
1942 
1943   if (StringNICmp (sap->name, "Annot:", 6) != 0) {
1944     sap->idx.deleteme = TRUE;
1945   }
1946 }
1947 
PubSeqRemoteLock(SeqIdPtr sip,Pointer remotedata)1948 static ValNodePtr PubSeqRemoteLock (
1949   SeqIdPtr sip,
1950   Pointer remotedata
1951 )
1952 
1953 {
1954   BioseqPtr    bsp;
1955   SeqAnnotPtr  sap = NULL;
1956   SeqEntryPtr  sep = NULL;
1957   ValNodePtr   vnp = NULL;
1958 
1959   if (sip == NULL) return NULL;
1960   sep = PubSeqSynchronousQueryId (sip, 1, -1);
1961 
1962   if (sep != NULL && IS_Bioseq (sep)) {
1963     bsp = (BioseqPtr) sep->data.ptrvalue;
1964     if (bsp != NULL) {
1965       AssignIDsInEntity (0, OBJ_SEQENTRY, (Pointer) sep);
1966       VisitAnnotsInSep (sep, NULL, MarkLocalAnnots);
1967       DeleteMarkedObjects (0, OBJ_BIOSEQ, (Pointer) bsp);
1968       sap = bsp->annot;
1969       bsp->annot = NULL;
1970     }
1971   }
1972   SeqEntryFree (sep);
1973 
1974   if (sap == NULL) return NULL;
1975 
1976   bsp = (BioseqPtr) MemNew (sizeof (Bioseq));
1977   if (bsp == NULL) return NULL;
1978   bsp->annot = sap;
1979 
1980   vnp = ValNodeNew (NULL);
1981   if (vnp == NULL) return NULL;
1982 
1983   vnp->data.ptrvalue = (Pointer) bsp;
1984 
1985   return vnp;
1986 }
1987 
PubSeqRemoteFree(ValNodePtr vnp,Pointer remotedata)1988 static void PubSeqRemoteFree (
1989   ValNodePtr vnp,
1990   Pointer remotedata
1991 )
1992 
1993 {
1994   ValNodeFreeData (vnp);
1995 }
1996 
1997 /* Args structure contains command-line arguments */
1998 
1999 typedef enum {
2000   i_argInputFile  = 0,
2001   o_argOutputFile,
2002   f_argFormat,
2003   m_argMode,
2004   s_argStyle,
2005   g_argFlags,
2006   h_argLock,
2007   u_argCustom,
2008   a_argType,
2009   t_argBatch,
2010   b_argBinary,
2011   c_argCompressed,
2012   p_argPropagate,
2013   l_argLogFile,
2014   r_argRemote,
2015   A_argAccession,
2016   F_argFarFeats,
2017 #ifdef OS_UNIX
2018   q_argFfDiff,
2019   n_argAsn2Flat,
2020   j_argFrom,
2021   k_argTo,
2022   d_argStrand,
2023   y_argItemID,
2024 #ifdef INTERNAL_NCBI_ASN2GB
2025   H_argAccessHUP,
2026 #endif
2027 #ifdef ENABLE_ARG_X
2028   x_argAccnToSave,
2029 #endif
2030 #endif
2031 } Arguments;
2032 
2033 Args myargs [] = {
2034   {"Input File Name", "stdin", NULL, NULL,
2035     FALSE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
2036   {"Output File Name", "stdout", NULL, NULL,
2037     FALSE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
2038   {"Format (b GenBank, e EMBL, p GenPept, t Feature Table, x INSDSet)", "b", NULL, NULL,
2039     FALSE, 'f', ARG_STRING, 0.0, 0, NULL},
2040   {"Mode (r Release, e Entrez, s Sequin, d Dump)", "s", NULL, NULL,
2041     FALSE, 'm', ARG_STRING, 0.0, 0, NULL},
2042   {"Style (n Normal, s Segment, m Master, c Contig)", "n", NULL, NULL,
2043     FALSE, 's', ARG_STRING, 0.0, 0, NULL},
2044   {"Bit Flags (1 HTML, 2 XML, 4 ContigFeats, 8 ContigSrcs, 16 FarTransl)", "0", NULL, NULL,
2045     FALSE, 'g', ARG_INT, 0.0, 0, NULL},
2046   {"Lock/Lookup Flags (8 LockProd, 16 LookupComp, 64 LookupProd)", "0", NULL, NULL,
2047     FALSE, 'h', ARG_INT, 0.0, 0, NULL},
2048   {"Custom Flags (4 HideFeats, 1792 HideRefs, 8192 HideSources, 262144 HideTranslation)", "0", NULL, NULL,
2049     FALSE, 'u', ARG_INT, 0.0, 0, NULL},
2050   {"ASN.1 Type\n"
2051    "      Single Record: a Any, e Seq-entry, b Bioseq, s Bioseq-set, m Seq-submit, q Catenated\n"
2052    "      Release File: t Batch Bioseq-set, u Batch Seq-submit\n", "a", NULL, NULL,
2053     TRUE, 'a', ARG_STRING, 0.0, 0, NULL},
2054   {"Batch\n"
2055    "      1 Report\n"
2056    "      2 Sequin/Release\n"
2057    "      3 asn2gb SSEC/nocleanup\n"
2058    "      4 asn2flat BSEC/nocleanup\n"
2059    "      5 asn2gb/asn2flat\n"
2060    "      6 asn2gb NEW dbxref/OLD dbxref\n"
2061    "      7 oldasn2gb/newasn2gb", "0", "0", "7",
2062     FALSE, 't', ARG_INT, 0.0, 0, NULL},
2063   {"Input File is Binary", "F", NULL, NULL,
2064     TRUE, 'b', ARG_BOOLEAN, 0.0, 0, NULL},
2065   {"Batch File is Compressed", "F", NULL, NULL,
2066     TRUE, 'c', ARG_BOOLEAN, 0.0, 0, NULL},
2067   {"Propagate Top Descriptors", "F", NULL, NULL,
2068     TRUE, 'p', ARG_BOOLEAN, 0.0, 0, NULL},
2069   {"Log file", NULL, NULL, NULL,
2070     TRUE, 'l', ARG_FILE_OUT, 0.0, 0, NULL},
2071   {"Remote Fetching", "F", NULL, NULL,
2072     TRUE, 'r', ARG_BOOLEAN, 0.0, 0, NULL},
2073   {"Accession to Fetch (or Accession,retcode,flags where flags -1 fetches external features)", NULL, NULL, NULL,
2074     TRUE, 'A', ARG_STRING, 0.0, 0, NULL},
2075   {"Remote Annotation Fetch Test (use -A Accession,0,-1 instead)", "F", NULL, NULL,
2076     TRUE, 'F', ARG_BOOLEAN, 0.0, 0, NULL},
2077 #ifdef OS_UNIX
2078 #ifdef PROC_I80X86
2079   {"Ffdiff Executable", "ffdiff", NULL, NULL,
2080     TRUE, 'q', ARG_FILE_IN, 0.0, 0, NULL},
2081   {"Asn2Flat Executable", "asn2flat", NULL, NULL,
2082     TRUE, 'n', ARG_FILE_IN, 0.0, 0, NULL},
2083 #else
2084   {"Ffdiff Executable", "/netopt/genbank/subtool/bin/ffdiff", NULL, NULL,
2085     TRUE, 'q', ARG_FILE_IN, 0.0, 0, NULL},
2086   {"Asn2Flat Executable", "asn2flat", NULL, NULL,
2087     TRUE, 'n', ARG_FILE_IN, 0.0, 0, NULL},
2088 #endif
2089   {"SeqLoc From", "0", NULL, NULL,
2090     TRUE, 'j', ARG_INT, 0.0, 0, NULL},
2091   {"SeqLoc To", "0", NULL, NULL,
2092     TRUE, 'k', ARG_INT, 0.0, 0, NULL},
2093   {"SeqLoc Minus Strand", "F", NULL, NULL,
2094     TRUE, 'd', ARG_BOOLEAN, 0.0, 0, NULL},
2095   {"Feature itemID", "0", NULL, NULL,
2096     TRUE, 'y', ARG_INT, 0.0, 0, NULL},
2097 #ifdef INTERNAL_NCBI_ASN2GB
2098   {"Internal Access to HUP", "F", NULL, NULL,
2099     TRUE, 'H', ARG_BOOLEAN, 0.0, 0, NULL},
2100 #endif
2101 #ifdef ENABLE_ARG_X
2102   {"Accession to Extract", NULL, NULL, NULL,
2103     TRUE, 'x', ARG_STRING, 0.0, 0, NULL},
2104 #endif
2105 #endif
2106 };
2107 
2108 
2109 #define HTML_XML_ASN_MASK (CREATE_HTML_FLATFILE | CREATE_XML_GBSEQ_FILE | CREATE_ASN_GBSEQ_FILE)
2110 
Main(void)2111 Int2 Main (
2112   void
2113 )
2114 
2115 {
2116   CharPtr      accn = NULL;
2117   CharPtr      accntofetch = NULL;
2118   AsnIoPtr     aip = NULL;
2119   FmtType      altformat = (FmtType) 0;
2120   Char         app [64];
2121   CharPtr      asn2flat = NULL;
2122   AsnTypePtr   atp = NULL;
2123   Int2         batch = 0;
2124   Boolean      binary = FALSE;
2125   Boolean      catenated = FALSE;
2126   Boolean      compressed = FALSE;
2127   CstType      custom;
2128   Boolean      do_gbseq = FALSE;
2129   Boolean      do_insdseq = FALSE;
2130   Boolean      do_tiny_seq = FALSE;
2131   Boolean      do_fasta_stream = FALSE;
2132   XtraPtr      extra = NULL;
2133   Boolean      farfeats = FALSE;
2134   CharPtr      ffdiff = NULL;
2135   FlgType      flags;
2136   FmtType      format = GENBANK_FMT;
2137   Int4         from = 0;
2138   GBSeq        gbsq;
2139   GBSet        gbst;
2140 #ifdef INTERNAL_NCBI_ASN2GB
2141   Boolean      hup = FALSE;
2142 #endif
2143   Uint4        itemID = 0;
2144   LckType      locks;
2145   CharPtr      logfile = NULL;
2146   FILE         *logfp = NULL;
2147   ModType      mode = SEQUIN_MODE;
2148   Boolean      propOK = FALSE;
2149   Boolean      remote = FALSE;
2150   Int2         rsult = 0;
2151   time_t       runtime, starttime, stoptime;
2152   SeqEntryPtr  sep;
2153   CharPtr      str;
2154   Uint1        strand = Seq_strand_plus;
2155   StlType      style = NORMAL_STYLE;
2156   Int4         to = 0;
2157   Int2         type = 0;
2158   Char         xmlbuf [128];
2159   XtraBlock    xtra;
2160 
2161   /* standard setup */
2162 
2163   ErrSetFatalLevel (SEV_MAX);
2164   ErrClearOptFlags (EO_SHOW_USERSTR);
2165   ErrSetLogfile ("stderr", ELOG_APPEND);
2166   UseLocalAsnloadDataAndErrMsg ();
2167   ErrPathReset ();
2168 
2169   SOCK_SetupSSL(NcbiSetupGnuTls);
2170 
2171   if (! AllObjLoad ()) {
2172     Message (MSG_POSTERR, "AllObjLoad failed");
2173     return 1;
2174   }
2175   if (! SubmitAsnLoad ()) {
2176     Message (MSG_POSTERR, "SubmitAsnLoad failed");
2177     return 1;
2178   }
2179   if (! FeatDefSetLoad ()) {
2180     Message (MSG_POSTERR, "FeatDefSetLoad failed");
2181     return 1;
2182   }
2183   if (! SeqCodeSetLoad ()) {
2184     Message (MSG_POSTERR, "SeqCodeSetLoad failed");
2185     return 1;
2186   }
2187   if (! GeneticCodeTableLoad ()) {
2188     Message (MSG_POSTERR, "GeneticCodeTableLoad failed");
2189     return 1;
2190   }
2191 
2192   /* process command line arguments */
2193 
2194   sprintf (app, "asn2gb %s", ASN2GB_APPLICATION);
2195   if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
2196     return 0;
2197   }
2198 
2199   if (myargs [b_argBinary].intvalue) {
2200     binary = TRUE;
2201   } else {
2202     binary = FALSE;
2203   }
2204 
2205   if (myargs [c_argCompressed].intvalue) {
2206     compressed = TRUE;
2207   } else {
2208     compressed = FALSE;
2209   }
2210 
2211   if (myargs [p_argPropagate].intvalue) {
2212     propOK = TRUE;
2213   } else {
2214     propOK = FALSE;
2215   }
2216 
2217   str = myargs [f_argFormat].strvalue;
2218   if (StringICmp (str, "bp") == 0 || StringICmp (str, "pb") == 0) {
2219     format = GENBANK_FMT;
2220     altformat = GENPEPT_FMT;
2221 
2222   } else if (StringICmp (str, "b") == 0) {
2223     format = GENBANK_FMT;
2224   } else if (StringICmp (str, "e") == 0) {
2225     format = EMBL_FMT;
2226   } else if (StringICmp (str, "p") == 0) {
2227     format = GENPEPT_FMT;
2228   } else if (StringICmp (str, "t") == 0) {
2229     format = FTABLE_FMT;
2230 
2231   } else if (StringICmp (str, "q") == 0) {
2232     do_gbseq = TRUE;
2233     format = GENBANK_FMT;
2234   } else if (StringICmp (str, "r") == 0) {
2235     do_gbseq = TRUE;
2236     format = GENPEPT_FMT;
2237 
2238   } else if (StringICmp (str, "xz") == 0 || StringICmp (str, "zx") == 0) {
2239     do_gbseq = TRUE;
2240     do_insdseq = TRUE;
2241     format = GENBANK_FMT;
2242     altformat = GENPEPT_FMT;
2243 
2244   } else if (StringICmp (str, "x") == 0) {
2245     do_gbseq = TRUE;
2246     do_insdseq = TRUE;
2247     format = GENBANK_FMT;
2248   } else if (StringCmp (str, "y") == 0) {
2249     do_tiny_seq = TRUE;
2250     format = GENBANK_FMT;
2251   } else if (StringCmp (str, "Y") == 0) {
2252     do_fasta_stream = TRUE;
2253     format = GENBANK_FMT;
2254   } else if (StringICmp (str, "z") == 0) {
2255     do_gbseq = TRUE;
2256     do_insdseq = TRUE;
2257     format = GENPEPT_FMT;
2258   } else {
2259     format = GENBANK_FMT;
2260   }
2261 
2262   str = myargs [m_argMode].strvalue;
2263   if (StringICmp (str, "r") == 0) {
2264     mode = RELEASE_MODE;
2265   } else if (StringICmp (str, "e") == 0) {
2266     mode = ENTREZ_MODE;
2267   } else if (StringICmp (str, "s") == 0) {
2268     mode = SEQUIN_MODE;
2269   } else if (StringICmp (str, "d") == 0) {
2270     mode = DUMP_MODE;
2271   } else {
2272     mode = SEQUIN_MODE;
2273   }
2274 
2275   str = myargs [s_argStyle].strvalue;
2276   if (StringICmp (str, "n") == 0) {
2277     style = NORMAL_STYLE;
2278   } else if (StringICmp (str, "s") == 0) {
2279     style = SEGMENT_STYLE;
2280   } else if (StringICmp (str, "m") == 0) {
2281     style = MASTER_STYLE;
2282   } else if (StringICmp (str, "c") == 0) {
2283     style = CONTIG_STYLE;
2284   } else {
2285     style = NORMAL_STYLE;
2286   }
2287 
2288   MemSet ((Pointer) &xtra, 0, sizeof (XtraBlock));
2289 
2290   flags = (FlgType) myargs [g_argFlags].intvalue;
2291 
2292   locks = (LckType) myargs [h_argLock].intvalue;
2293 
2294   custom = (CstType) myargs [u_argCustom].intvalue;
2295 
2296   str = myargs [a_argType].strvalue;
2297   if (StringICmp (str, "a") == 0) {
2298     type = 1;
2299   } else if (StringICmp (str, "e") == 0) {
2300     type = 2;
2301   } else if (StringICmp (str, "b") == 0) {
2302     type = 3;
2303   } else if (StringICmp (str, "s") == 0) {
2304     type = 4;
2305   } else if (StringICmp (str, "m") == 0) {
2306     type = 5;
2307   } else if (StringICmp (str, "q") == 0) {
2308     catenated = TRUE;
2309     type = 1;
2310   } else if (StringICmp (str, "t") == 0) {
2311     batch = 1;
2312     type = 4;
2313   } else if (StringICmp (str, "u") == 0) {
2314     batch = 1;
2315     type = 5;
2316   } else {
2317     type = 1;
2318   }
2319 
2320   if (myargs [t_argBatch].intvalue > 0) {
2321     batch = (Int2) myargs [t_argBatch].intvalue;
2322   }
2323 
2324   if ((binary || compressed) && batch == 0) {
2325     if (type == 1) {
2326       Message (MSG_FATAL, "-b or -c cannot be used without -t or -a");
2327       return 1;
2328     }
2329   }
2330 
2331   remote = (Boolean) myargs [r_argRemote].intvalue;
2332 
2333   accntofetch = (CharPtr) myargs [A_argAccession].strvalue;
2334   if (StringDoesHaveText (accntofetch)) {
2335     remote = TRUE;
2336   }
2337   farfeats = myargs [F_argFarFeats].intvalue;
2338 
2339 #ifdef INTERNAL_NCBI_ASN2GB
2340   hup = myargs [H_argAccessHUP].intvalue;
2341 #endif
2342 
2343   if (remote) {
2344 #ifdef INTERNAL_NCBI_ASN2GB
2345     if (hup) {
2346       DirSubFetchEnable ();
2347       SmartFetchEnable ();
2348       TPASmartFetchEnable ();
2349     }
2350 
2351     if (! PUBSEQBioseqFetchEnable ("asn2gb", FALSE)) {
2352       Message (MSG_POSTERR, "PUBSEQBioseqFetchEnable failed");
2353       return 1;
2354     }
2355 #else
2356     PubSeqFetchEnable ();
2357     if (farfeats) {
2358       xtra.remotelock = PubSeqRemoteLock;
2359       xtra.remotefree = PubSeqRemoteFree;
2360     }
2361 #endif
2362     PubMedFetchEnable ();
2363     LocalSeqFetchInit (FALSE);
2364   }
2365 
2366   logfile = (CharPtr) myargs [l_argLogFile].strvalue;
2367   if (! StringHasNoText (logfile)) {
2368     logfp = FileOpen (logfile, "w");
2369   }
2370 
2371 #ifdef OS_UNIX
2372   ffdiff = myargs [q_argFfDiff].strvalue;
2373   asn2flat = myargs [n_argAsn2Flat].strvalue;
2374 
2375   from = myargs [j_argFrom].intvalue;
2376   to = myargs [k_argTo].intvalue;
2377   if (myargs [d_argStrand].intvalue) {
2378     strand = Seq_strand_minus;
2379   } else {
2380     strand = Seq_strand_plus;
2381   }
2382   itemID = myargs [y_argItemID].intvalue;
2383 
2384 #ifdef ENABLE_ARG_X
2385   if (! StringHasNoText (myargs [x_argAccnToSave].strvalue)) {
2386     accn = myargs [x_argAccnToSave].strvalue;
2387   }
2388 #endif
2389 #endif
2390 
2391   if (GetAppParam ("NCBI", "SETTINGS", "XMLPREFIX", NULL, xmlbuf, sizeof (xmlbuf))) {
2392     AsnSetXMLmodulePrefix (StringSave (xmlbuf));
2393   }
2394 
2395   if (do_gbseq) {
2396     if (! objgbseqAsnLoad ()) {
2397       Message (MSG_POSTERR, "objgbseqAsnLoad failed");
2398       return 1;
2399     }
2400     if (! objinsdseqAsnLoad ()) {
2401       Message (MSG_POSTERR, "objinsdseqAsnLoad failed");
2402       return 1;
2403     }
2404     MemSet ((Pointer) &gbsq, 0, sizeof (GBSeq));
2405     xtra.gbseq = &gbsq;
2406     if ((flags & HTML_XML_ASN_MASK) == CREATE_ASN_GBSEQ_FILE) {
2407       aip = AsnIoOpen (myargs [o_argOutputFile].strvalue, "w");
2408     } else {
2409       aip = AsnIoOpen (myargs [o_argOutputFile].strvalue, "wx");
2410     }
2411     if (aip == NULL) {
2412       Message (MSG_POSTERR, "AsnIoOpen failed");
2413       return 1;
2414     }
2415     xtra.aip = aip;
2416     if ((Boolean) (((flags & PRODUCE_OLD_GBSEQ) != 0)) || ((custom & OLD_GBSEQ_XML) != 0)) {
2417       do_insdseq = FALSE;
2418     }
2419     if (do_insdseq) {
2420       atp = AsnLinkType (NULL, AsnFind ("INSDSet"));
2421       xtra.atp = AsnLinkType (NULL, AsnFind ("INSDSet.E"));
2422     } else {
2423       atp = AsnLinkType (NULL, AsnFind ("GBSet"));
2424       xtra.atp = AsnLinkType (NULL, AsnFind ("GBSet.E"));
2425       flags |= PRODUCE_OLD_GBSEQ;
2426     }
2427     if (atp == NULL || xtra.atp == NULL) {
2428       Message (MSG_POSTERR, "AsnLinkType or AsnFind failed");
2429       return 1;
2430     }
2431     MemSet ((Pointer) &gbst, 0, sizeof (GBSet));
2432     AsnOpenStruct (aip, atp, (Pointer) &gbst);
2433   }
2434 
2435   extra = &xtra;
2436 
2437   starttime = GetSecs ();
2438 
2439   if (StringDoesHaveText (accntofetch)) {
2440 
2441     if (remote) {
2442       sep = SeqEntryFromAccnOrGi (accntofetch);
2443       if (sep != NULL) {
2444         ProcessOneSeqEntry (sep, myargs [o_argOutputFile].strvalue,
2445                             format, altformat, mode, style, flags, locks,
2446                             custom, extra, from, to, strand, itemID,
2447                             do_tiny_seq, do_fasta_stream);
2448         SeqEntryFree (sep);
2449       }
2450     }
2451 
2452   } else if (batch != 0 || accn != NULL) {
2453 
2454     rsult = HandleMultipleRecords (myargs [i_argInputFile].strvalue,
2455                                    myargs [o_argOutputFile].strvalue,
2456                                    format, altformat, mode, style, flags, locks,
2457                                    custom, extra, type, batch, binary, compressed,
2458                                    propOK, ffdiff, asn2flat, accn, logfp);
2459   } else if (catenated) {
2460 
2461     rsult = HandleCatenatedRecord (myargs [i_argInputFile].strvalue,
2462                                 myargs [o_argOutputFile].strvalue,
2463                                 format, altformat, mode, style, flags, locks,
2464                                 custom, extra, type, binary, compressed,
2465                                 from, to, strand, itemID, do_tiny_seq, do_fasta_stream);
2466   } else {
2467 
2468     rsult = HandleSingleRecord (myargs [i_argInputFile].strvalue,
2469                                 myargs [o_argOutputFile].strvalue,
2470                                 format, altformat, mode, style, flags, locks,
2471                                 custom, extra, type, binary, compressed,
2472                                 from, to, strand, itemID, do_tiny_seq, do_fasta_stream);
2473   }
2474 
2475   if (aip != NULL) {
2476     AsnCloseStruct (aip, atp, NULL);
2477     AsnPrintNewLine (aip);
2478     AsnIoClose (aip);
2479   }
2480 
2481   stoptime = GetSecs ();
2482   runtime = stoptime - starttime;
2483   if (logfp != NULL) {
2484     fprintf (logfp, "Finished in %ld seconds\n", (long) runtime);
2485     FileClose (logfp);
2486   }
2487 
2488   if (remote) {
2489     LocalSeqFetchDisable ();
2490     PubMedFetchDisable ();
2491 #ifdef INTERNAL_NCBI_ASN2GB
2492     PUBSEQBioseqFetchDisable ();
2493 #else
2494     PubSeqFetchDisable ();
2495 #endif
2496   }
2497 
2498   return rsult;
2499 }
2500 
2501