NCBI C Toolkit Cross Reference

C/network/taxon1/taxon2/txcdproc.c


  1 /*   txcdproc.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:  txcdproc.c
 27 *
 28 * Author:  Vladimir Soussov
 29 *
 30 * Version Creation Date:   07/15/97
 31 *
 32 * $Revision: 1.18 $
 33 *
 34 * File Description: 
 35 *       API for Taxonomy service
 36 *
 37 * Modifications:  
 38 * --------------------------------------------------------------------------
 39 * Date     Name        Description of modification
 40 * -------  ----------  -----------------------------------------------------
 41 *
 42 * ==========================================================================
 43 *
 44 *
 45 * RCS Modification History:
 46 * $Log: txcdproc.c,v $
 47 * Revision 1.18  2007/09/14 18:28:47  soussov
 48 * fixes bug in init function (it now calls NetFini instead of TaxArchFini
 49 *
 50 * Revision 1.17  2006/07/13 17:14:49  bollin
 51 * use Uint4 instead of Uint2 for itemID values
 52 *
 53 * Revision 1.16  2003/11/04 17:13:58  soussov
 54 * adds NetFini() call if service can not start properly
 55 *
 56 * Revision 1.15  2003/06/12 16:46:25  soussov
 57 * changes severity for AsnRead failures
 58 *
 59 * Revision 1.14  2003/03/06 16:30:32  kans
 60 * tdp->org needed a cast to (OrgRefPtr) due to Mac compiler complaint
 61 *
 62 * Revision 1.13  2003/03/05 21:32:00  soussov
 63 * new lookup procedure
 64 *
 65 * Revision 1.12  2001/09/28 15:53:15  soussov
 66 * tax1e_maxTaxId() added
 67 *
 68 * Revision 1.11  2001/09/18 16:55:38  soussov
 69 * switching to the new ASN
 70 *
 71 * Revision 1.10  2001/09/18 14:42:26  soussov
 72 * Fixes error processing in case of reconnection
 73 *
 74 * Revision 1.9  2001/09/17 22:27:34  soussov
 75 * ErrPost call added if nothing was read from the service
 76 *
 77 * Revision 1.8  2001/06/13 21:23:19  soussov
 78 * makes purify happy
 79 *
 80 * Revision 1.7  1999/12/20 17:05:16  soussov
 81 * taxid4gi added
 82 *
 83 * Revision 1.6  1999/10/21 19:20:57  soussov
 84 * time tracking added
 85 *
 86 * Revision 1.5  1998/07/27 16:54:04  soussov
 87 * bug in txc_getChildren fixed
 88 *
 89 * Revision 1.4  1998/06/11 20:42:11  soussov
 90 * kill some warnings
 91 *
 92 * Revision 1.3  1998/04/01 17:34:02  soussov
 93 * changed tp include <>
 94 *
 95 * Revision 1.2  1998/03/31 22:54:42  kans
 96 * codewarrior can tell null from 0, and unsigned charptr needed cast to CharPtr
 97 *
 98 * Revision 1.1  1998/02/10 20:12:02  soussov
 99 * taxon2 related soft
100 *
101  *
102 */
103 
104 /** for ErrPostEx() ****/
105 
106 static char *this_module = "txcdproc";
107 
108 #ifdef THIS_MODULE
109 #undef THIS_MODULE
110 #endif
111 
112 #define THIS_MODULE this_module
113 static char *this_file = __FILE__;
114 #define THIS_FILE this_file
115 
116 #include <ncbinet.h>
117 #include <objall.h>
118 #include <objfeat.h>
119 #include <objtax1.h>
120 #include <txclient.h>
121 #include <taxinc.h>
122 #include <time.h>
123 
124 #define TAXARCH_SERV_RETRIES 4
125 
126 static Taxon1RespPtr NetTaxArchReadAsn PROTO((void));
127 static Boolean ReestablishNetTaxArch PROTO((void));
128 static Boolean NetInit PROTO((void));
129 static Boolean ForceNetInit PROTO((void));
130 static Boolean NetFini PROTO((void));
131 static Boolean GenericReestablishNet PROTO((CharPtr svcName, Boolean showErrs));
132 static Boolean TaxServFini (void);
133 
134 static NI_HandPtr svcp = NULL;
135 static AsnIoPtr   asnin = NULL;
136 static AsnIoPtr   asnout = NULL;
137 static Boolean num_attached = 0;
138 static Boolean reallyFinal = TRUE;
139 static NI_DispatcherPtr dispatcher;
140 static Boolean (*myNetInit) PROTO((void));
141 
142 static int tax_track= 0;
143 static clock_t tax_time1;
144 
145 static void report_service_error(CharPtr proc_name, Taxon1RespPtr taxbp)
146 {
147     if(taxbp == NULL) {
148         ErrPostEx(SEV_ERROR, 0, 0, "%s got NULL responce from service", proc_name);
149     }
150     else if(taxbp->choice == Taxon1Resp_error) {
151         Taxon1ErrorPtr tep= taxbp->data.ptrvalue;
152         if(tep != NULL) {
153             ErrPostEx(tep->level, 0, 0, "%s got %s", proc_name, tep->msg? tep->msg : "Empty message");
154         }
155     }
156 }
157 
158 /*****************************************************************************
159 *
160 *   NetTaxArchReadAsn ()
161 *
162 *****************************************************************************/
163 
164 static Taxon1RespPtr NetTaxArchReadAsn(void)
165 {
166     Taxon1RespPtr taxbp;
167     short erract;
168     ErrDesc err;
169 
170     ErrGetOpts(&erract, NULL);
171     ErrSetOpts(ERR_CONTINUE, 0);
172     ErrFetch(&err); /* clear any pending error */
173 
174     taxbp = Taxon1RespAsnRead(asnin, NULL);
175 
176     if ((taxbp == NULL) || ErrFetch(&err))
177     {
178         ErrPostEx (SEV_WARNING, 1, 1, "Null message read from server");
179     }
180     ErrSetOpts(erract, 0);
181 
182     return taxbp;
183 }
184 
185 /*****************************************************************************
186 *
187 *   txc_connect2server()
188 *
189 *   all arguments are optional just to keep os interface
190 *****************************************************************************/
191 
192 static Boolean TaxServInit(void)
193 {
194     Taxon1ReqPtr taxrp;
195     Taxon1RespPtr taxbp;
196 
197     myNetInit = TaxServInit;
198 
199     if (!NetInit()) return FALSE;
200 
201     svcp= NI_GenericGetService(dispatcher, NULL, "TAXONOMY", "TaxService", TRUE);
202 
203     if (svcp == NULL) {
204         ErrPostEx(SEV_ERROR, 0, 0, "NI_ServiceGet [%s] (%s)", ni_errlist[ni_errno], ni_errtext);
205         NetFini();
206         return FALSE;
207     }
208 
209     asnin= svcp->raip;
210     asnout= svcp->waip;
211 
212     if(getenv("TAX_TRACK_TIME") != NULL) {
213         tax_track= 1;
214     }
215 
216     /**********************************************************/
217 
218     taxrp= ValNodeNew(NULL);
219     taxrp->choice = Taxon1Req_init;
220     if(tax_track) tax_time1= clock();
221     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
222     AsnIoReset(asnout);
223     Taxon1ReqFree (taxrp);
224 
225     if((taxbp = NetTaxArchReadAsn()) == NULL) {
226         NetFini();
227         return FALSE;
228     }
229     else  {
230         taxbp->data.ptrvalue = NULL;
231         Taxon1RespFree (taxbp);
232         if(tax_track) {
233             double diff= (double)(clock() - tax_time1)/CLOCKS_PER_SEC;
234             printf("tax1_init() %f\n", diff);
235         }
236             
237         return TRUE;
238     }
239 }
240 
241 Boolean txc_connect2Server(CharPtr server, CharPtr user, CharPtr passwd, CharPtr module)
242 {
243     return TaxServInit();
244 }
245 
246 
247 /*****************************************************************************
248 *
249 *   Tax0Fini ()
250 *
251 *****************************************************************************/
252 
253 static Boolean s_TaxArchFini(void)
254 {
255     Boolean retval = TRUE;
256     Taxon1ReqPtr taxrp;
257     Taxon1RespPtr taxbp;
258 
259     
260     if (asnout != NULL && asnin != NULL)  {
261         taxrp = ValNodeNew(NULL);
262         taxrp->choice = Taxon1Req_fini;
263         Taxon1ReqAsnWrite (taxrp, asnout, NULL);
264         AsnIoReset(asnout);
265         Taxon1ReqFree (taxrp);
266 
267         if ((taxbp = NetTaxArchReadAsn()) == NULL) {
268             retval = FALSE;
269         }
270         else {
271             taxbp->data.ptrvalue = NULL;
272             Taxon1RespFree (taxbp);
273         }
274     }
275 
276     NetFini();
277 
278     return retval;
279 }
280 
281 /* the only thing done here is to suppress errors */
282 
283 static Boolean TaxServFini (void)
284 {
285     short erract;
286     ErrDesc err;
287     Boolean retval;
288 
289     ErrGetOpts(&erract, NULL);
290     ErrSetOpts(ERR_IGNORE, 0);
291     ErrFetch(&err);
292 
293     retval = s_TaxArchFini();
294 
295     ErrSetOpts(erract, 0);
296     ErrFetch(&err);
297 
298     return retval;
299 }
300 
301 void txc_close(void)
302 {
303     TaxServFini();
304 }
305 
306 
307 static Taxon1NamePtr s_findname(CharPtr sname)
308 {
309     Taxon1ReqPtr taxrp;
310     Taxon1RespPtr taxbp;
311     Taxon1NamePtr tnp;
312 
313     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
314     taxrp->choice= Taxon1Req_findname;
315     taxrp->data.ptrvalue= sname;
316     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
317     AsnIoReset(asnout);
318     taxrp->data.ptrvalue= NULL;
319     Taxon1ReqFree(taxrp);
320 
321     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
322     
323     if(taxbp->choice != Taxon1Resp_findname) {
324         report_service_error("tax_findByName", taxbp);
325         Taxon1RespFree(taxbp);
326         return NULL;
327     }
328 
329     tnp= taxbp->data.ptrvalue;
330     taxbp->data.ptrvalue= NULL;
331     Taxon1RespFree(taxbp);
332 
333     return tnp;
334 }
335 
336 static Taxon1NamePtr findname(CharPtr sname)
337 {
338     Int4 i;
339     short erract;
340     ErrDesc err;
341     Taxon1NamePtr tnp = NULL;
342 
343     if(tax_track) tax_time1= clock();
344     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
345         ErrGetOpts(&erract, NULL);
346         ErrSetOpts(ERR_IGNORE, 0);
347         ErrFetch(&err);
348 
349         tnp = s_findname(sname);
350 
351         ErrSetOpts(erract, 0);
352         if (!ErrFetch(&err))  break; /* success */
353         
354         if(!ReestablishNetTaxArch()) break;
355     }
356     if(tax_track) {
357         double diff= (double)(clock() - tax_time1)/CLOCKS_PER_SEC;
358         printf("tax1_findByName() %f\n", diff);
359     }
360         
361 
362     return(tnp);
363 }
364 
365 Int4 tax_findByName(CharPtr sname, int mode, TaxNamePtr* res_name)
366 {
367     TaxNamePtr o_name;
368     Int4 i, n= 0;
369     Taxon1NamePtr tnp, tnp_list= ((sname == NULL) || (*sname == '\0'))? NULL : findname(sname);
370     
371 
372     if((res_name == NULL) || (tnp_list == NULL)) return 0;
373     for(tnp= tnp_list; tnp != NULL; tnp= tnp->next) n++;
374 
375     *res_name= o_name= MemNew(n*sizeof(TaxName));
376     if(o_name == NULL) {
377         for(i= 0; i < n; i++) {
378             tnp= tnp_list->next;
379             Taxon1NameFree(tnp_list);
380             tnp_list= tnp;
381         }
382         return 0;
383     }
384     
385     for(i= 0; i < n; i++) {
386         tnp= tnp_list->next;
387         o_name[i].tax_id= tnp_list->taxid;
388         o_name[i].class_cde= tnp_list->cde;
389         o_name[i].name_txt= tnp_list->oname;
390         o_name[i].unique_name= tnp_list->uname;
391         MemFree(tnp_list);
392         tnp_list= tnp;
393     }
394 
395     return n;
396 }
397                 
398 static Taxon1NamePtr s_getorgnames(Int4 tax_id)
399 {
400     Taxon1ReqPtr taxrp;
401     Taxon1RespPtr taxbp;
402     Taxon1NamePtr tnp;
403 
404     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
405     taxrp->choice= Taxon1Req_getorgnames;
406     taxrp->data.intvalue= tax_id;
407     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
408     AsnIoReset(asnout);
409     Taxon1ReqFree(taxrp);
410 
411     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
412     
413     if(taxbp->choice != Taxon1Resp_getorgnames) {
414         report_service_error("tax_getOrgNames", taxbp);
415         Taxon1RespFree(taxbp);
416         return NULL;
417     }
418 
419     tnp= taxbp->data.ptrvalue;
420     taxbp->data.ptrvalue= NULL;
421     Taxon1RespFree(taxbp);
422 
423     return tnp;
424 }
425 
426 
427 static Taxon1NamePtr getorgnames(Int4 tax_id)
428 {
429     Int4 i;
430     short erract;
431     ErrDesc err;
432     Taxon1NamePtr tnp = NULL;
433 
434     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
435         ErrGetOpts(&erract, NULL);
436         ErrSetOpts(ERR_IGNORE, 0);
437         ErrFetch(&err);
438 
439         tnp = s_getorgnames(tax_id);
440 
441         ErrSetOpts(erract, 0);
442         if (!ErrFetch(&err))  break; /* success */
443         
444         if(!ReestablishNetTaxArch()) break;
445     }
446 
447     return(tnp);
448 }
449 
450 Int4 tax_getOrgNames(Int4 tax_id, TaxNamePtr* res_name)
451 {
452     TaxNamePtr o_name;
453     Int4 i, n= 0;
454     Taxon1NamePtr tnp, tnp_list= (tax_id > 0)? getorgnames(tax_id) : NULL;
455     
456 
457     if((res_name == NULL) || (tnp_list == NULL)) return 0;
458     for(tnp= tnp_list; tnp != NULL; tnp= tnp->next) n++;
459 
460     *res_name= o_name= MemNew(n*sizeof(TaxName));
461     if(o_name == NULL) {
462         for(i= 0; i < n; i++) {
463             tnp= tnp_list->next;
464             Taxon1NameFree(tnp_list);
465             tnp_list= tnp;
466         }
467         return 0;
468     }
469     
470     for(i= 0; i < n; i++) {
471         tnp= tnp_list->next;
472         o_name[i].tax_id= tnp_list->taxid;
473         o_name[i].class_cde= tnp_list->cde;
474         o_name[i].name_txt= tnp_list->oname;
475         o_name[i].unique_name= tnp_list->uname;
476         MemFree(tnp_list);
477         tnp_list= tnp;
478     }
479 
480     return n;
481 }
482 
483 static Int4 s_getdesignator(CharPtr sname)
484 {
485     Taxon1ReqPtr taxrp;
486     Taxon1RespPtr taxbp;
487     Int4 tax_id;
488 
489     if((taxrp= ValNodeNew(NULL)) == NULL) return 0;
490     taxrp->choice= Taxon1Req_getdesignator;
491     taxrp->data.ptrvalue= sname;
492     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
493     AsnIoReset(asnout);
494     taxrp->data.ptrvalue= NULL;
495     Taxon1ReqFree(taxrp);
496 
497     if((taxbp= NetTaxArchReadAsn()) == NULL) return 0;
498     
499     if(taxbp->choice != Taxon1Resp_getdesignator) {
500         report_service_error("tax_getDesignator", taxbp);
501         Taxon1RespFree(taxbp);
502         return 0;
503     }
504 
505     tax_id= taxbp->data.intvalue;
506     Taxon1RespFree(taxbp);
507 
508     return tax_id;
509 }
510 
511 static Int4 getdesignator(CharPtr sname)
512 {
513     Int4 i;
514     short erract;
515     ErrDesc err;
516     Int4 tax_id= 0;
517 
518     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
519         ErrGetOpts(&erract, NULL);
520         ErrSetOpts(ERR_IGNORE, 0);
521         ErrFetch(&err);
522 
523         tax_id = s_getdesignator(sname);
524 
525         ErrSetOpts(erract, 0);
526         if (!ErrFetch(&err))  break; /* success */
527         
528         if(!ReestablishNetTaxArch()) break;
529     }
530 
531     return tax_id;
532 }
533 
534 Int4 tax_getDesignator(char* sname)
535 {
536     return getdesignator(sname);
537 }
538 
539 static Int4 s_getunique(CharPtr sname)
540 {
541     Taxon1ReqPtr taxrp;
542     Taxon1RespPtr taxbp;
543     Int4 tax_id;
544 
545     if((taxrp= ValNodeNew(NULL)) == NULL) return 0;
546     taxrp->choice= Taxon1Req_getunique;
547     taxrp->data.ptrvalue= sname;
548     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
549     AsnIoReset(asnout);
550     taxrp->data.ptrvalue= NULL;
551     Taxon1ReqFree(taxrp);
552 
553     if((taxbp= NetTaxArchReadAsn()) == NULL) return 0;
554     
555     if(taxbp->choice != Taxon1Resp_getunique) {
556         report_service_error("tax_uniqueName", taxbp);
557         Taxon1RespFree(taxbp);
558         return 0;
559     }
560 
561     tax_id= taxbp->data.intvalue;
562     Taxon1RespFree(taxbp);
563 
564     return tax_id;
565 }
566 
567 static Int4 getunique(CharPtr sname)
568 {
569     Int4 i;
570     short erract;
571     ErrDesc err;
572     Int4 tax_id= 0;
573 
574     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
575         ErrGetOpts(&erract, NULL);
576         ErrSetOpts(ERR_IGNORE, 0);
577         ErrFetch(&err);
578 
579         tax_id = s_getunique(sname);
580 
581         ErrSetOpts(erract, 0);
582         if (!ErrFetch(&err))  break; /* success */
583         
584         if(!ReestablishNetTaxArch()) break;
585     }
586 
587     return tax_id;
588 }
589 
590 Int4 tax_uniqueName(CharPtr sname, Int4 id)
591 {
592     Int4 tax_id= getunique(sname);
593     return (tax_id == id)? 0 : tax_id;
594 }
595 
596 static Int4 s_getidbyorg(OrgRefPtr orgRef)
597 {
598     Taxon1ReqPtr taxrp;
599     Taxon1RespPtr taxbp;
600     Int4 tax_id;
601 
602     if((taxrp= ValNodeNew(NULL)) == NULL) return 0;
603     taxrp->choice= Taxon1Req_getidbyorg;
604     taxrp->data.ptrvalue= orgRef;
605     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
606     AsnIoReset(asnout);
607     taxrp->data.ptrvalue= NULL;
608     Taxon1ReqFree(taxrp);
609 
610     if((taxbp= NetTaxArchReadAsn()) == NULL) return 0;
611     
612     if(taxbp->choice != Taxon1Resp_getidbyorg) {
613         report_service_error("txc_getTaxIdByOrgRef", taxbp);
614         Taxon1RespFree(taxbp);
615         return 0;
616     }
617 
618     tax_id= taxbp->data.intvalue;
619     Taxon1RespFree(taxbp);
620 
621     return tax_id;
622 }
623 
624 static Int4 getidbyorg(OrgRefPtr orgRef)
625 {
626     Int4 i;
627     short erract;
628     ErrDesc err;
629     Int4 tax_id= 0;
630 
631     if(tax_track) tax_time1= clock();
632     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
633         ErrGetOpts(&erract, NULL);
634         ErrSetOpts(ERR_IGNORE, 0);
635         ErrFetch(&err);
636 
637         tax_id = s_getidbyorg(orgRef);
638 
639         ErrSetOpts(erract, 0);
640         if (!ErrFetch(&err))  break; /* success */
641         
642         if(!ReestablishNetTaxArch()) break;
643     }
644 
645     if(tax_track) {
646         double diff= (double)(clock() - tax_time1)/CLOCKS_PER_SEC;
647         printf("tax1_getTaxIdByOrgRef() %f\n", diff);
648     }
649     
650 
651     return tax_id;
652 }
653 
654 Int4 txc_getTaxIdByOrgRef(OrgRefPtr orgRef)
655 {
656     return getidbyorg(orgRef);
657 }
658 
659 Int4 tax_getIdByName(char* sname, char* qualif, Int1 id)
660 {
661     OrgRefPtr orgRef= OrgRefNew();
662     Int4 tax_id;
663     
664     orgRef->taxname= sname;
665     if(qualif != NULL) {
666         orgRef->orgname= OrgNameNew();
667         orgRef->orgname->mod= OrgModNew();
668         orgRef->orgname->mod->subtype= id;
669         orgRef->orgname->mod->subname= qualif;
670     }
671     
672     tax_id= getidbyorg(orgRef);
673     orgRef->taxname= NULL;
674     if(qualif != NULL) {
675         orgRef->orgname->mod->subname=NULL;
676     }
677     OrgRefFree(orgRef);
678     return tax_id;
679 }
680 
681 static Taxon1NamePtr s_getlineage(Int4 tax_id)
682 {
683     Taxon1ReqPtr taxrp;
684     Taxon1RespPtr taxbp;
685     Taxon1NamePtr tnp;
686 
687     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
688     taxrp->choice= Taxon1Req_taxalineage;
689     taxrp->data.intvalue= tax_id;
690     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
691     AsnIoReset(asnout);
692     Taxon1ReqFree(taxrp);
693 
694     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
695     
696     if(taxbp->choice != Taxon1Resp_taxalineage) {
697         report_service_error("txc_getLineage", taxbp);
698         Taxon1RespFree(taxbp);
699         return NULL;
700     }
701 
702     tnp= taxbp->data.ptrvalue;
703     taxbp->data.ptrvalue= NULL;
704     Taxon1RespFree(taxbp);
705 
706     return tnp;
707 }
708 
709 
710 static Taxon1NamePtr getlineage(Int4 tax_id)
711 {
712     Int4 i;
713     short erract;
714     ErrDesc err;
715     Taxon1NamePtr tnp = NULL;
716 
717     if(tax_track) tax_time1= clock();
718     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
719         ErrGetOpts(&erract, NULL);
720         ErrSetOpts(ERR_IGNORE, 0);
721         ErrFetch(&err);
722 
723         tnp = s_getlineage(tax_id);
724 
725         ErrSetOpts(erract, 0);
726         if (!ErrFetch(&err))  break; /* success */
727         
728         if(!ReestablishNetTaxArch()) break;
729     }
730 
731     if(tax_track) {
732         double diff= (double)(clock() - tax_time1)/CLOCKS_PER_SEC;
733         printf("tax1_getLineage() %f\n", diff);
734     }
735 
736     return(tnp);
737 }
738 
739 TXC_TreeNodePtr* txc_getLineage(Int4 lin_id, Int4Ptr lin_len)
740 {
741     TXC_TreeNodePtr* lin;
742     Int4 n= 0, i, k, k1;
743     Taxon1NamePtr tnp, tnp_list= (lin_id > 0)? getlineage(lin_id) : NULL;
744 
745     if(lin_len != NULL) *lin_len= 0;
746 
747     if(tnp_list == NULL) return NULL;
748 
749     for(tnp= tnp_list; tnp != NULL; tnp= tnp->next) n++;
750 
751     if((lin= MemNew(n*sizeof(TXC_TreeNodePtr))) == NULL) {
752         /* no memory, just free tnp_list */
753         for(i= 0; i < n; i++) {
754             tnp= tnp_list->next;
755             Taxon1NameFree(tnp_list);
756             tnp_list= tnp;
757         }
758         return NULL;
759     }
760 
761     for(i= 0; i < n; i++) {
762         tnp= tnp_list->next;
763         k= StringLen(tnp_list->oname);
764         k1= tnp_list->uname? (StringLen(tnp_list->uname) + 2) : 0;
765         if((lin[i]= MemNew(k+k1+10)) != NULL) {
766             lin[i]->tax_id= tnp_list->taxid;
767             lin[i]->flags= tnp_list->cde | TXC_STHIDE;
768             memcpy(lin[i]->node_label, tnp_list->oname, k+1);
769             if(k1) {
770                 memcpy(lin[i]->node_label + (k+1), tnp_list->uname, k1 - 1);
771                 lin[i]->flags^= TXC_STHIDE;
772             }
773         }
774         Taxon1NameFree(tnp_list);
775         tnp_list= tnp;
776     }
777             
778     if(lin_len != NULL) *lin_len= n;
779     return lin;
780 }
781                 
782 static Taxon1NamePtr s_getchildren(Int4 tax_id)
783 {
784     Taxon1ReqPtr taxrp;
785     Taxon1RespPtr taxbp;
786     Taxon1NamePtr tnp;
787 
788     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
789     taxrp->choice= Taxon1Req_taxachildren;
790     taxrp->data.intvalue= tax_id;
791     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
792     AsnIoReset(asnout);
793     Taxon1ReqFree(taxrp);
794 
795     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
796     
797     if(taxbp->choice != Taxon1Resp_taxachildren) {
798         report_service_error("txc_getChildren", taxbp);
799         Taxon1RespFree(taxbp);
800         return NULL;
801     }
802 
803     tnp= taxbp->data.ptrvalue;
804     taxbp->data.ptrvalue= NULL;
805     Taxon1RespFree(taxbp);
806 
807     return tnp;
808 }
809 
810 
811 static Taxon1NamePtr getchildren(Int4 tax_id)
812 {
813     Int4 i;
814     short erract;
815     ErrDesc err;
816     Taxon1NamePtr tnp = NULL;
817 
818     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
819         ErrGetOpts(&erract, NULL);
820         ErrSetOpts(ERR_IGNORE, 0);
821         ErrFetch(&err);
822 
823         tnp = s_getchildren(tax_id);
824 
825         ErrSetOpts(erract, 0);
826         if (!ErrFetch(&err))  break; /* success */
827         
828         if(!ReestablishNetTaxArch()) break;
829     }
830 
831     return(tnp);
832 }
833 
834 TXC_TreeNodePtr* txc_getChildren(Int4 node_id, Int4Ptr nof_children)
835 {
836     TXC_TreeNodePtr* lin;
837     Int4 n= 0, i, k, k1;
838     Taxon1NamePtr tnp, tnp_list= getchildren(node_id);
839 
840     if(nof_children != NULL) *nof_children= 0;
841 
842     if(tnp_list == NULL) return NULL;
843 
844     for(tnp= tnp_list; tnp != NULL; tnp= tnp->next) n++;
845 
846     if((lin= MemNew(n*sizeof(TXC_TreeNodePtr))) == NULL) {
847         /* no memory, just free tnp_list */
848         for(i= 0; i < n; i++) {
849             tnp= tnp_list->next;
850             Taxon1NameFree(tnp_list);
851             tnp_list= tnp;
852         }
853         return NULL;
854     }
855 
856     for(i= 0; i < n; i++) {
857         tnp= tnp_list->next;
858         if(tnp_list->cde == 0) {
859             k= 0;
860             k1= 0;
861         }
862         else {
863             k= StringLen(tnp_list->oname);
864             k1= tnp_list->uname? (StringLen(tnp_list->uname) + 2) : 0;
865         }
866         if((lin[i]= MemNew(k+k1+10)) != NULL) {
867             lin[i]->tax_id= tnp_list->taxid;
868             lin[i]->flags= k? (tnp_list->cde | TXC_STHIDE) : 0;
869             if(k) {
870                 memcpy(lin[i]->node_label, tnp_list->oname, k+1);
871                 if(k1) {
872                     memcpy(lin[i]->node_label + (k+1), tnp_list->uname, k1 - 1);
873                     lin[i]->flags^= TXC_STHIDE;
874                 }
875             }
876             
877         }
878         Taxon1NameFree(tnp_list);
879         tnp_list= tnp;
880     }
881             
882     if(nof_children != NULL) *nof_children= n;
883     return lin;
884 }
885 
886 static Taxon1InfoPtr s_getcde(void)
887 {
888     Taxon1ReqPtr taxrp;
889     Taxon1RespPtr taxbp;
890     Taxon1InfoPtr tnp;
891 
892     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
893     taxrp->choice= Taxon1Req_getcde;
894     taxrp->data.ptrvalue= NULL;
895     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
896     AsnIoReset(asnout);
897     Taxon1ReqFree(taxrp);
898 
899     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
900     
901     if(taxbp->choice != Taxon1Resp_getcde) {
902         report_service_error("txc_loadNameClasses", taxbp);
903         Taxon1RespFree(taxbp);
904         return NULL;
905     }
906 
907     tnp= taxbp->data.ptrvalue;
908     taxbp->data.ptrvalue= NULL;
909     Taxon1RespFree(taxbp);
910 
911     return tnp;
912 }
913 
914 
915 static Taxon1InfoPtr getcde(void)
916 {
917     Int4 i;
918     short erract;
919     ErrDesc err;
920     Taxon1InfoPtr tnp = NULL;
921 
922     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
923         ErrGetOpts(&erract, NULL);
924         ErrSetOpts(ERR_IGNORE, 0);
925         ErrFetch(&err);
926 
927         tnp = s_getcde();
928 
929         ErrSetOpts(erract, 0);
930         if (!ErrFetch(&err))  break; /* success */
931         
932         if(!ReestablishNetTaxArch()) break;
933     }
934 
935     return(tnp);
936 }
937 
938 Boolean txc_loadNameClasses(void)
939 {
940     Taxon1InfoPtr tnp, tnp_list= getcde();
941 
942     if(tnp_list == NULL) return FALSE;
943 
944     while(tnp_list != NULL) {
945         tax_addNameClass(tnp_list->ival1, tnp_list->sval, tnp_list->ival2);
946         tnp= tnp_list->next;
947         Taxon1InfoFree(tnp_list);
948         tnp_list= tnp;
949     }
950     return TRUE;
951 }
952 
953 
954 static Taxon1InfoPtr s_getranks(void)
955 {
956     Taxon1ReqPtr taxrp;
957     Taxon1RespPtr taxbp;
958     Taxon1InfoPtr tnp;
959 
960     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
961     taxrp->choice= Taxon1Req_getranks;
962     taxrp->data.ptrvalue= NULL;
963     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
964     AsnIoReset(asnout);
965     Taxon1ReqFree(taxrp);
966 
967     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
968     
969     if(taxbp->choice != Taxon1Resp_getranks) {
970         report_service_error("txc_loadRanks", taxbp);
971         Taxon1RespFree(taxbp);
972         return NULL;
973     }
974 
975     tnp= taxbp->data.ptrvalue;
976     taxbp->data.ptrvalue= NULL;
977     Taxon1RespFree(taxbp);
978 
979     return tnp;
980 }
981 
982 
983 static Taxon1InfoPtr getranks(void)
984 {
985     Int4 i;
986     short erract;
987     ErrDesc err;
988     Taxon1InfoPtr tnp = NULL;
989 
990     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
991         ErrGetOpts(&erract, NULL);
992         ErrSetOpts(ERR_IGNORE, 0);
993         ErrFetch(&err);
994 
995         tnp = s_getranks();
996 
997         ErrSetOpts(erract, 0);
998         if (!ErrFetch(&err))  break; /* success */
999         
1000         if(!ReestablishNetTaxArch()) break;
1001     }
1002 
1003     return(tnp);
1004 }
1005 
1006 Boolean txc_loadRanks(void)
1007 {
1008     Taxon1InfoPtr tnp, tnp_list= getranks();
1009 
1010     if(tnp_list == NULL) return FALSE;
1011 
1012     while(tnp_list != NULL) {
1013         tax_addRank(tnp_list->ival1, tnp_list->sval);
1014         tnp= tnp_list->next;
1015         Taxon1InfoFree(tnp_list);
1016         tnp_list= tnp;
1017     }
1018     return TRUE;
1019 }
1020 
1021 static Taxon1InfoPtr s_getdivs(void)
1022 {
1023     Taxon1ReqPtr taxrp;
1024     Taxon1RespPtr taxbp;
1025     Taxon1InfoPtr tnp;
1026 
1027     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
1028     taxrp->choice= Taxon1Req_getdivs;
1029     taxrp->data.ptrvalue= NULL;
1030     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
1031     AsnIoReset(asnout);
1032     Taxon1ReqFree(taxrp);
1033 
1034     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
1035     
1036     if(taxbp->choice != Taxon1Resp_getdivs) {
1037         report_service_error("txc_loadDivisions", taxbp);
1038         Taxon1RespFree(taxbp);
1039         return NULL;
1040     }
1041 
1042     tnp= taxbp->data.ptrvalue;
1043     taxbp->data.ptrvalue= NULL;
1044     Taxon1RespFree(taxbp);
1045 
1046     return tnp;
1047 }
1048 
1049 
1050 static Taxon1InfoPtr getdivs(void)
1051 {
1052     Int4 i;
1053     short erract;
1054     ErrDesc err;
1055     Taxon1InfoPtr tnp = NULL;
1056 
1057     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
1058         ErrGetOpts(&erract, NULL);
1059         ErrSetOpts(ERR_IGNORE, 0);
1060         ErrFetch(&err);
1061 
1062         tnp = s_getdivs();
1063 
1064         ErrSetOpts(erract, 0);
1065         if (!ErrFetch(&err))  break; /* success */
1066         
1067         if(!ReestablishNetTaxArch()) break;
1068     }
1069 
1070     return(tnp);
1071 }
1072 
1073 Boolean txc_loadDivisions(void)
1074 {
1075     Taxon1InfoPtr tnp, tnp_list= getdivs();
1076     Uint4 w;
1077     int i;
1078     unsigned char buff[8];
1079 
1080     if(tnp_list == NULL) return FALSE;
1081 
1082     while(tnp_list != NULL) {
1083         w= tnp_list->ival2;
1084         for(i= 0; i < 3; i++) {
1085             buff[i]= (w >> (8*(3-i))) & 0xFF;
1086         }
1087         buff[3]= w & 0xFF;
1088         buff[4]= '\0';
1089         tax_addDivision(tnp_list->ival1, (CharPtr) buff, tnp_list->sval);
1090         tnp= tnp_list->next;
1091         Taxon1InfoFree(tnp_list);
1092         tnp_list= tnp;
1093     }
1094     return TRUE;
1095 }
1096 
1097 static Taxon1InfoPtr s_getgcs(void)
1098 {
1099     Taxon1ReqPtr taxrp;
1100     Taxon1RespPtr taxbp;
1101     Taxon1InfoPtr tnp;
1102 
1103     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
1104     taxrp->choice= Taxon1Req_getgcs;
1105     taxrp->data.ptrvalue= NULL;
1106     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
1107     AsnIoReset(asnout);
1108     Taxon1ReqFree(taxrp);
1109 
1110     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
1111     
1112     if(taxbp->choice != Taxon1Resp_getgcs) {
1113         report_service_error("txc_loadGCs", taxbp);
1114         Taxon1RespFree(taxbp);
1115         return NULL;
1116     }
1117 
1118     tnp= taxbp->data.ptrvalue;
1119     taxbp->data.ptrvalue= NULL;
1120     Taxon1RespFree(taxbp);
1121 
1122     return tnp;
1123 }
1124 
1125 
1126 static Taxon1InfoPtr getgcs(void)
1127 {
1128     Int4 i;
1129     short erract;
1130     ErrDesc err;
1131     Taxon1InfoPtr tnp = NULL;
1132 
1133     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
1134         ErrGetOpts(&erract, NULL);
1135         ErrSetOpts(ERR_IGNORE, 0);
1136         ErrFetch(&err);
1137 
1138         tnp = s_getgcs();
1139 
1140         ErrSetOpts(erract, 0);
1141         if (!ErrFetch(&err))  break; /* success */
1142         
1143         if(!ReestablishNetTaxArch()) break;
1144     }
1145 
1146     return(tnp);
1147 }
1148 
1149 Boolean txc_loadGCs(void)
1150 {
1151     Taxon1InfoPtr tnp, tnp_list= getgcs();
1152 
1153     if(tnp_list == NULL) return FALSE;
1154 
1155     while(tnp_list != NULL) {
1156         tax_addGC(tnp_list->ival1, tnp_list->sval);
1157         tnp= tnp_list->next;
1158         Taxon1InfoFree(tnp_list);
1159         tnp_list= tnp;
1160     }
1161     return TRUE;
1162 }
1163 
1164 
1165 /*****************************************************************************
1166 *
1167 *   ReestablishNetTaxArch ()
1168 *
1169 *****************************************************************************/
1170 
1171 static Boolean ReestablishNetTaxArch(void)
1172 {
1173     return GenericReestablishNet("Taxon2", FALSE);
1174 }
1175 
1176 /*****************************************************************************
1177 *
1178 *   GenericReestablishNet ()
1179 *
1180 *****************************************************************************/
1181 
1182 static Boolean GenericReestablishNet(CharPtr svcName, Boolean showErrs)
1183 {
1184     MonitorPtr mon = NULL;
1185     Boolean retval;
1186     CharPtr buf;
1187 
1188     buf = MemNew(2 * StrLen(svcName) + 60);
1189 
1190     if (showErrs) {
1191         sprintf (buf, "Re-establishing %s Service", svcName);
1192         mon = MonitorStrNew(buf, 40);
1193         sprintf (buf, "Requesting %s service", svcName);
1194         MonitorStrValue(mon, buf);
1195     }
1196     NetFini();
1197     retval = TRUE;
1198 
1199     if (!myNetInit())
1200     {
1201         if(mon) {
1202             sprintf (buf, "%s get failed; re-contacting dispatcher", svcName);
1203             MonitorStrValue(mon, buf);
1204         }
1205         retval = FALSE;
1206         if (ForceNetInit())
1207         { /* successfully established contact w/dispatcher */
1208             if(mon) {
1209                 sprintf (buf, "%s get failed; re-requesting %s service",
1210                          svcName, svcName);
1211                 MonitorStrValue(mon, buf);
1212             }
1213             retval = myNetInit();
1214         }
1215         else {
1216             ErrPostEx(SEV_ERROR, 1,2, "Unable to re-contact dispatcher");
1217             if (showErrs) {
1218                 ErrShow();
1219             }
1220         }
1221     }
1222 
1223     if(mon) MonitorFree(mon);
1224 
1225     if (! retval )
1226     {
1227         /*sprintf (buf, "Unable to re-establish %s service", svcName);*/
1228         ErrPostEx(SEV_ERROR, 1, 3, "Unable to re-establish %s service", svcName);
1229         if (showErrs) {
1230             ErrShow();
1231         }
1232     }
1233 
1234     MemFree(buf);
1235     return retval;
1236 }
1237 
1238 /*****************************************************************************
1239 *
1240 *   NetInit ()
1241 *
1242 *****************************************************************************/
1243 
1244 static Boolean NetInit(void)
1245 {
1246     if (num_attached++ > 0)
1247         return TRUE;
1248 
1249     return ((dispatcher = NI_GenericInit(NULL, NULL, TRUE, NULL, 0)) != NULL);
1250 }
1251 
1252 
1253 /*****************************************************************************
1254 *
1255 *   ForceNetInit ()
1256 *
1257 *****************************************************************************/
1258 
1259 static Boolean ForceNetInit(void)
1260 {
1261     Boolean retval;
1262 
1263     reallyFinal = FALSE;
1264     num_attached = 0; /* force re-attempt to contact dispatcher */
1265     retval = NetInit();
1266     reallyFinal = TRUE;
1267 
1268     return retval;
1269 }
1270 
1271 /*****************************************************************************
1272 *
1273 *   NetFini ()
1274 *
1275 *****************************************************************************/
1276 
1277 static Boolean NetFini(void)
1278 {
1279    if (num_attached > 0)
1280         num_attached--;
1281 
1282     if (num_attached == 0)
1283     {
1284         NI_ServiceDisconnect(svcp);
1285         svcp = NULL;
1286         NI_EndServices (dispatcher);
1287         dispatcher = NULL;
1288     }
1289 
1290     return TRUE;
1291 }
1292 
1293 
1294 static _subspecPtr s_SSget(Int4 tax_id, _subspecPtr ss)
1295 {
1296     Taxon1ReqPtr taxrp;
1297     Taxon1RespPtr taxbp;
1298     Taxon1InfoPtr tax_info;
1299 
1300     if((taxrp= ValNodeNew(NULL)) == NULL) return NULL;
1301     taxrp->choice= Taxon1Req_getorgmod;
1302     taxrp->data.ptrvalue= tax_info= Taxon1InfoNew();
1303     if(tax_info == NULL) {
1304         MemFree(taxrp);
1305         return NULL;
1306     }
1307     tax_info->ival1= tax_id;
1308     if(ss != NULL) {
1309         tax_info->ival2= ss->stype;
1310         tax_info->sval= ss->sname;
1311     }
1312     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
1313     AsnIoReset(asnout);
1314     tax_info->sval= NULL;
1315     Taxon1ReqFree(taxrp);
1316 
1317     if((taxbp= NetTaxArchReadAsn()) == NULL) return NULL;
1318     
1319     if(taxbp->choice != Taxon1Resp_getorgmod) {
1320         report_service_error("tax_SSget", taxbp);
1321         Taxon1RespFree(taxbp);
1322         return NULL;
1323     }
1324 
1325     if(ss != NULL) {
1326         tax_info= taxbp->data.ptrvalue;
1327         if(tax_info != NULL) {      
1328             ss->r_id= tax_info->ival1;
1329             ss->rtype= tax_info->ival2;
1330             ss->rname= tax_info->sval;
1331             tax_info->sval= NULL;
1332         }
1333         else ss= NULL;
1334         Taxon1RespFree(taxbp);
1335         return ss;
1336     }
1337     
1338     Taxon1RespFree(taxbp);
1339 
1340     return NULL;
1341 }
1342 
1343 _subspecPtr tax_SSget(Int4 tax_id, _subspecPtr ss)
1344 {
1345     Int4 i;
1346     short erract;
1347     ErrDesc err;
1348     _subspecPtr res= NULL;
1349 
1350     if(tax_track) tax_time1= clock();
1351     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
1352         ErrGetOpts(&erract, NULL);
1353         ErrSetOpts(ERR_IGNORE, 0);
1354         ErrFetch(&err);
1355 
1356         res= s_SSget(tax_id, ss);
1357 
1358         ErrSetOpts(erract, 0);
1359         if (!ErrFetch(&err))  break; /* success */
1360         
1361         if(!ReestablishNetTaxArch()) break;
1362     }
1363     if(tax_track) {
1364         double diff= (double)(clock() - tax_time1)/CLOCKS_PER_SEC;
1365         printf("tax1_SSget() %f\n", diff);
1366     }
1367 
1368     return res;
1369 }
1370 
1371 static Int4 s_getTaxId4GI(Int4 gi)
1372 {
1373     Taxon1ReqPtr taxrp;
1374     Taxon1RespPtr taxbp;
1375     Int4 tax_id;
1376 
1377     if((taxrp= ValNodeNew(NULL)) == NULL) return 0;
1378     taxrp->choice= Taxon1Req_id4gi;
1379     taxrp->data.intvalue= gi;
1380     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
1381     AsnIoReset(asnout);
1382     Taxon1ReqFree(taxrp);
1383 
1384     if((taxbp= NetTaxArchReadAsn()) == NULL) return 0;
1385     
1386     if(taxbp->choice != Taxon1Resp_id4gi) {
1387         report_service_error("tax_getTaxId4GI", taxbp);
1388         Taxon1RespFree(taxbp);
1389         return 0;
1390     }
1391 
1392     tax_id= taxbp->data.intvalue;
1393     Taxon1RespFree(taxbp);
1394 
1395     return tax_id;
1396 }
1397 
1398 static Int4 getTaxId4GI(Int4 gi)
1399 {
1400     Int4 i;
1401     short erract;
1402     ErrDesc err;
1403     Int4 tax_id= 0;
1404 
1405     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
1406         ErrGetOpts(&erract, NULL);
1407         ErrSetOpts(ERR_IGNORE, 0);
1408         ErrFetch(&err);
1409 
1410         tax_id = s_getTaxId4GI(gi);
1411 
1412         ErrSetOpts(erract, 0);
1413         if (!ErrFetch(&err))  break; /* success */
1414         
1415         if(!ReestablishNetTaxArch()) break;
1416     }
1417 
1418     return tax_id;
1419 }
1420 
1421 Int4 tax_getTaxId4GI(Int4 gi)
1422 {
1423     return getTaxId4GI(gi);
1424 }
1425 
1426 static Int4 s_maxTaxId()
1427 {
1428     Taxon1ReqPtr taxrp;
1429     Taxon1RespPtr taxbp;
1430     Int4 tax_id;
1431 
1432     if((taxrp= ValNodeNew(NULL)) == NULL) return 0;
1433     taxrp->choice= Taxon1Req_maxtaxid;
1434     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
1435     AsnIoReset(asnout);
1436     Taxon1ReqFree(taxrp);
1437 
1438     if((taxbp= NetTaxArchReadAsn()) == NULL) return 0;
1439     
1440     if(taxbp->choice != Taxon1Resp_maxtaxid) {
1441         report_service_error("tax1e_maxTaxId", taxbp);
1442         Taxon1RespFree(taxbp);
1443         return 0;
1444     }
1445 
1446     tax_id= taxbp->data.intvalue;
1447     Taxon1RespFree(taxbp);
1448 
1449     return tax_id;
1450 }
1451 
1452 Int4 tax1e_maxTaxId()
1453 {
1454     Int4 i;
1455     short erract;
1456     ErrDesc err;
1457     Int4 tax_id= 0;
1458 
1459     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
1460         ErrGetOpts(&erract, NULL);
1461         ErrSetOpts(ERR_IGNORE, 0);
1462         ErrFetch(&err);
1463 
1464         tax_id = s_maxTaxId();
1465 
1466         ErrSetOpts(erract, 0);
1467         if (!ErrFetch(&err))  break; /* success */
1468         
1469         if(!ReestablishNetTaxArch()) break;
1470     }
1471 
1472     return tax_id;
1473 }
1474 
1475 static OrgRefPtr s_findbyorg(OrgRefPtr orgRef)
1476 {
1477     Taxon1ReqPtr taxrp;
1478     Taxon1RespPtr taxbp;
1479     Taxon1DataPtr tdp;
1480 
1481     if((taxrp= ValNodeNew(NULL)) == NULL) return 0;
1482     taxrp->choice= Taxon1Req_lookup;
1483     taxrp->data.ptrvalue= orgRef;
1484     Taxon1ReqAsnWrite(taxrp, asnout, NULL);
1485     AsnIoReset(asnout);
1486     taxrp->data.ptrvalue= NULL;
1487     Taxon1ReqFree(taxrp);
1488 
1489     if((taxbp= NetTaxArchReadAsn()) == NULL) return 0;
1490     
1491     if(taxbp->choice != Taxon1Resp_lookup) {
1492         report_service_error("txc_getTaxIdByOrgRef", taxbp);
1493         Taxon1RespFree(taxbp);
1494         return NULL;
1495     }
1496     
1497     tdp= taxbp->data.ptrvalue;
1498     orgRef= (OrgRefPtr) tdp->org;
1499     tdp->org= NULL;
1500     Taxon1RespFree(taxbp);
1501     
1502     return orgRef;
1503 }
1504 
1505 Int4 txc_findByOrg(OrgRefPtr inp_orgRef, OrgModPtr* hitName)
1506 {
1507     Int4 i;
1508     short erract;
1509     ErrDesc err;
1510     OrgRefPtr orp= NULL;
1511     Int4 tax_id= 0;
1512 
1513     if(tax_track) tax_time1= clock();
1514     for (i= TAXARCH_SERV_RETRIES; i >= 0; --i) {
1515         ErrGetOpts(&erract, NULL);
1516         ErrSetOpts(ERR_IGNORE, 0);
1517         ErrFetch(&err);
1518 
1519         orp = s_findbyorg(inp_orgRef);
1520 
1521         ErrSetOpts(erract, 0);
1522         if (!ErrFetch(&err))  break; /* success */
1523         
1524         if(!ReestablishNetTaxArch()) break;
1525     }
1526     
1527     if(tax_track) {
1528         double diff= (double)(clock() - tax_time1)/CLOCKS_PER_SEC;
1529         printf("tax1_getTaxIdByOrgRef() %f\n", diff);
1530     }
1531     
1532     if(orp && orp->db) {
1533         ValNodePtr vnp= orp->db;
1534         DbtagPtr dbtag= vnp->data.ptrvalue;
1535         ObjectIdPtr object_id= dbtag->tag;
1536         tax_id= object_id->id;
1537         
1538         if(hitName) {
1539             if(orp->orgname) {
1540                 *hitName= orp->orgname->mod;
1541                 orp->orgname->mod= NULL;
1542             }
1543             else *hitName= NULL;
1544         }
1545     }
1546 
1547     if(orp) OrgRefFree(orp);
1548 
1549     return tax_id;
1550 }
1551 
1552 

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

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