1 /*  $Id: mmdb_pubstruct.c,v 6.4 2001/05/25 01:42:10 kimelman Exp $
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:  $Id: mmdb_pubstruct.c,v 6.4 2001/05/25 01:42:10 kimelman Exp $
27 *
28 * File Description: Defines MMDB access using sybase retrival.
29 *
30 * Modifications:
31 * --------------------------------------------------------------------------
32 * Date     Name        Description of modification
33 * -------  ----------  -----------------------------------------------------
34 *
35 * $Log: mmdb_pubstruct.c,v $
36 * Revision 6.4  2001/05/25 01:42:10  kimelman
37 * mmdb2livemmdb
38 *
39 * Revision 6.3  1999/12/01 23:27:26  kimelman
40 * added guards to avoid repeatable connect/disconnect
41 *
42 * Revision 6.2  1999/08/02 19:50:51  kimelman
43 * keep connection alive foor mmdbsrv & vastsrv sessions
44 *
45 * Revision 6.1  1999/05/17 21:29:20  kimelman
46 * extracted version of pubstruct specific implementations of MMDBBiostrucGET & Co..
47 *
48 *
49 * ==========================================================================
50 */
51 
52 /*********************************************************************************/
53 /* This file abstracts the calls to Network Entrez that were previously          */
54 /* embedded in mmdbapi1.c  Linking with this file forces sysbase retrival        */
55 /********   NOTE - .mmdbrc (UNIX) mmdb.ini (Win) mmdb.cnf (Mac) required   *******/
56 /* containing paths to database and index file name:                         ******
57  *
58  * [MMDB]
59  * PubStruct  = PUBSEQ_OS:PubStruct=anyone:allowed
60  *
61  */
62 
63 #include <ncbi.h>
64 #include <mmdbapi1.h>
65 #include <mmdbdata.h>
66 #include <mmdblocl.h>
67 #include <accentr.h>
68 #include <accutils.h>
69 #include "PubStructAsn.h"
70 #include <assert.h>
71 
72 
73 static int  pubstruct_init=0;
74 static Char pubstruct[PATH_MAX];
75 static ps_handle_t db = NULL;
76 
77 /**************************************************************
78  *  PUBLIC INTERFACE
79  **************************************************************/
80 Boolean  LIBCALL
MMDB2liveMMDB(DocUid * mmdb,Int4Ptr live,CharPtr pdb)81 MMDB2liveMMDB(DocUid *mmdb,Int4Ptr live,CharPtr pdb)
82 {
83   Boolean b;
84   if (!db && pubstruct[0]==0) {
85     strcpy(pubstruct,"BACH:PubStruct=anyone,allowed");
86   }
87   MMDBInit();
88   b = PubStruct_mmdb2livemmdb(db,*mmdb,mmdb,live,pdb);
89   MMDBFini();
90   return b;
91 }
92 
93 DocUid  LIBCALL
MMDBEvalPDB(CharPtr str)94 MMDBEvalPDB (CharPtr str)
95 {
96   Char eval[10];
97   DocUid id=0;
98 
99   StringNCpy(eval,  str,  4);
100   eval[4] = '\0';
101   StrUpper(eval);
102   { /* let's be paranoid */
103     int i,is_mmdb=1;
104     for(i=0;i<4;i++)if(eval[i]>='A') { is_mmdb=0; break; }
105     if(is_mmdb)
106       id=atol(eval);
107   }
108   MMDBInit();
109   id = PubStruct_pdb2mmdb1(db, eval);
110   MMDBFini();
111   return  id;
112 }
113 
114 BiostrucPtr LIBCALL
MMDBBiostrucGet(DocUid uid,Int4 mdlLvl,Int4 maxModels)115 MMDBBiostrucGet (DocUid uid, Int4 mdlLvl, Int4 maxModels)
116 {
117   extern WWWInfoPtr 	info; /* mmdbsrv:info */
118 
119   AsnIoPtr    aip = NULL;
120   BiostrucPtr pbs = NULL;
121   int         index;
122   char       *item;
123   Int4        sat_key   = 0;
124   Int4        state     = 0;
125   /*   char       *dbserver = (pubstruct[0]?pubstruct:NULL); */
126 
127   MMDBInit();
128 
129   /*
130    * THE following block should be defined ONLY for server which show up internal blob revisions in DB
131    */
132 #ifdef INTERNAL_SERVER
133   /* printf("MMDBBiostrucGet_pubstruct(%s,%d)\n",dbserver,uid); */
134   if(WWWFindName(info, "sat_key") >=0)
135     {
136       index = WWWFindName(info, "sat_key");
137       item  = WWWGetValueByIndex(info,index);
138       sat_key=atol(item);
139       if (sat_key<=0) {
140         ErrPostEx(SEV_ERROR,0,0, "cant convert Pubstruct..Struct.acc (sat_key) <%s> into integer",item);
141         return NULL;
142       }
143     }
144   if (WWWFindName(info, "state"))
145     {
146       index = WWWFindName(info, "state");
147       item = WWWGetValueByIndex(info,index);
148       state=-1;
149       state=atol(item);
150       if (state<0) {
151         ErrPostEx(SEV_ERROR,0,0, "cant convert Pubstruct..Struct.state<%s> into integer",item);
152         return NULL;
153       }
154     }
155 #endif
156 
157   if(state>0 && sat_key==0) {
158     sat_key = PubStruct_lookup1(db,uid, state);
159   }
160   if(sat_key>0)
161     aip = PubStruct_readasn1(db, sat_key);
162   else if (state==0)
163     aip = PubStruct_viewasn1(db, uid);
164 
165   if(aip)
166     {
167       pbs = BiostrucAsnGet(aip, NULL,  mdlLvl,  maxModels);
168       PubStruct_closeasn (aip,1);
169     }
170   MMDBFini();
171   return pbs;
172 }
173 
174 Boolean LIBCALL
MMDBInit(void)175 MMDBInit (void)
176 {
177   pubstruct_init++;
178   if(db)
179     return TRUE;
180   if(pubstruct_init==1)
181     {
182       GetAppParam("mmdb", "MMDB", "PubStruct", "", pubstruct, sizeof(pubstruct));
183       if (pubstruct[0]==0) {
184         strcpy(pubstruct,"PUBSEQ_OS:PubStruct=anyone,allowed");
185       }
186     }
187   db = PubStruct_connect(pubstruct);
188   if(!db)
189     {
190       ErrPostEx(SEV_ERROR,0,0, "cant connect to Pubstruct (%s)",pubstruct);
191       return FALSE;
192     }
193   return TRUE;
194 }
195 
196 void LIBCALL
MMDBFini(void)197 MMDBFini (void)
198 {
199   pubstruct_init--;
200   if(pubstruct_init>0)
201     return;
202   if(db)
203     {
204       PubStruct_disconnect(db);
205       db = NULL;
206     }
207   return;
208 }
209 
210 CharPtr LIBCALL
MMDB_configuration(void)211 MMDB_configuration(void)
212 {
213   return "Version:\t$Id: mmdb_pubstruct.c,v 6.4 2001/05/25 01:42:10 kimelman Exp $\nConfiguration: PubStruct" ;
214 }
215