summaryrefslogtreecommitdiff
path: root/network/id1arch/accid1.c
blob: fa8b070feb73f4b6579dfe8f3f737350a7cccc85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <seqmgr.h>
#include  <id1arch.h>

#define ID1BFS_DISABLE 0     /* ID1BioseqFetch not in use (default) */
#define ID1BFS_INIT    1     /* ID1BioseqFetchEnable called, but not ID1Init */
#define ID1BFS_READY   2     /* ID1Init has been called */

#define ID1_NUM_RETRY 5

static CharPtr procname[3] = {
        "ID1BioseqFetch",
        "ID1SeqIdForGI",
        "ID1GIForSeqId" };
static Int2 LIBCALLBACK s_ID1BioseqFetchFunc PROTO((Pointer data));
static Int2 LIBCALLBACK s_ID1SeqIdForGIFunc PROTO((Pointer data));
static Int2 LIBCALLBACK s_ID1GIForSeqIdFunc PROTO((Pointer data));

typedef struct ID1Fetch_struct {
        Uint1 	state;
        CharPtr app_name;
	Uint2 	ctr;        /* counts iterations of enable disable */
} ID1FetchStruct, PNTR ID1FetchStructPtr;


Boolean 
ID1Init(void)
{
	return ID1ArchInit();
}

void 
ID1Fini(void)
{
	ID1ArchFini();
}


/**** Look Up a Uid from a SeqId using ID1 lookup ****/
Int4 
ID1FindSeqId(SeqIdPtr sip)
{
	return ID1ArchGIGet(sip);
}

/**** Look Up the source SeqId given a GI ****************/
SeqIdPtr
ID1SeqIdForGI(Int4 gi)
{
	return ID1ArchSeqIdsGet(gi,NULL);
}


SeqEntryPtr 
ID1SeqEntryGet(Int4 gi, Int2 retcode)
{
	SeqEntryPtr sep = NULL;
	ErrStrId errorID;
	char errorString[32];
	Int4 status=0,i;
   
	if (!gi) 
		return NULL;
	sprintf(errorString, "(ID1SeqEntryGet: %d)",gi);
        errorID = Nlm_ErrUserInstall(errorString, 0);

	for(i=0;i<ID1_NUM_RETRY;i++){
		if((sep=ID1ArchSeqEntryGet(gi,NULL,0,&status,retcode)) != NULL){
			break;
		} else {
			if(status & GI_IS_OVERRIDEN /** override **/){
				ErrPostEx(SEV_INFO,0,0,"Sequence is withdrawn");
				goto DONE;
			} else if(status &  GI_IS_CONFIDENTIAL /*** confidential ***/){
				ErrPostEx(SEV_INFO,0,0,"Sequence is not yet available");
                                goto DONE;
			} else {
				ErrPostEx(SEV_INFO,0,0,"Network failure... Reconecting");
			}
		}
	}
	if(sep==NULL){
		ErrPostEx(SEV_WARNING,0,0,"Unable to get the sequence ");
	}

DONE:
	Nlm_ErrUserDelete(errorID);
	return sep;
}

Boolean 
ID1BioseqFetchEnable(CharPtr progname, Boolean now)
{
	Boolean result;
	ID1FetchStructPtr ifsp;
	ObjMgrPtr omp;
	ObjMgrProcPtr ompp;

	/* check if already enabled ***/

        omp = ObjMgrGet();
        ompp = ObjMgrProcFind(omp, 0, procname[0], OMPROC_FETCH);
        if(ompp != NULL){   /* already initialized */
		ifsp = (ID1FetchStructPtr)(ompp->procdata);
	} else {
		ifsp = MemNew(sizeof(ID1FetchStruct));
		ifsp->app_name = StringSave(progname);

                ObjMgrProcLoad(OMPROC_FETCH,procname[0],procname[0],OBJ_SEQID,0,OBJ_BIOSEQ,0,
                        (Pointer)ifsp,s_ID1BioseqFetchFunc, PROC_PRIORITY_DEFAULT);

		ObjMgrProcLoad(OMPROC_FETCH, procname[1],procname[1],OBJ_SEQID,SEQID_GI,OBJ_SEQID,0,
                        (Pointer)ifsp,s_ID1SeqIdForGIFunc,PROC_PRIORITY_DEFAULT);

                ObjMgrProcLoad(OMPROC_FETCH, procname[2],procname[2],OBJ_SEQID,0,OBJ_SEQID,SEQID_GI,
                        (Pointer)ifsp,s_ID1GIForSeqIdFunc,PROC_PRIORITY_DEFAULT);
	}
	ifsp->ctr++;    /* count number of enables */
	if(ifsp->state == ID1BFS_READY)  /* nothing more to do */
		return TRUE;
        if(now){
                if ((result = ID1Init())==FALSE){
                        return result;
                }
                ifsp->state = ID1BFS_READY;
        } else {
		ifsp->state = ID1BFS_INIT;
	}
        return TRUE;
}
void
ID1BioseqFetchDisable(void)
{
	ObjMgrPtr		omp;
	ObjMgrProcPtr		ompp;
	ID1FetchStructPtr	ifsp;

	omp = ObjMgrGet();
	ompp = ObjMgrProcFind(omp,0,procname[0],OMPROC_FETCH);
	if(ompp == NULL)   /* not initialized */
		return;

	ifsp = (ID1FetchStructPtr)(ompp->procdata);
	if(!ifsp || !ifsp->ctr)   /* no enables active */
		return;
	ifsp->ctr--;
	if(ifsp->ctr)   /* connection still pending */
		return;
	if(ifsp->state == ID1BFS_READY)
		ID1Fini();
	ifsp->state = ID1BFS_DISABLE;
        return;
}
/**** Static Functions *****/

/*****************************************************************************
*
*   s_ID1SeqIdForGIFunc(data)
*       callback for ID1SeqIdForGI
*
*****************************************************************************/
static Int2 LIBCALLBACK
s_ID1SeqIdForGIFunc(Pointer data)
{
	OMProcControlPtr ompcp;
	ObjMgrProcPtr ompp;
	ID1FetchStructPtr ifsp;
	SeqIdPtr sip, sip2;

	ompcp = (OMProcControlPtr)data;
	ompp = ompcp->proc;
	ifsp = ompp->procdata;
	if(!ifsp || ifsp->state == ID1BFS_DISABLE){ /* shut off */
		return OM_MSG_RET_OK;    /* not done, go on to next */
        } else if(ifsp->state == ID1BFS_INIT){
                if (!ID1Init())
			return OM_MSG_RET_ERROR;
                ifsp->state = ID1BFS_READY;
        }

        if(ifsp->state != ID1BFS_READY)
		return OM_MSG_RET_ERROR;

        sip = (SeqIdPtr)(ompcp->input_data);
        if(sip == NULL || sip->choice != SEQID_GI)
		return OM_MSG_RET_ERROR;

        if((sip2 = ID1SeqIdForGI(sip->data.intvalue))==NULL)
		return OM_MSG_RET_OK;

        ompcp->output_data = (Pointer)sip2; /* not registering right now */
        return OM_MSG_RET_DONE;   /* data found, don't call further functions */
}

/*****************************************************************************
*
*   s_ID1GIForSeqIdFunc(data)
*       callback for ID1GIForSeqId
*
*****************************************************************************/
static Int2 LIBCALLBACK
s_ID1GIForSeqIdFunc (Pointer data)
{
	OMProcControlPtr	ompcp;
	ObjMgrProcPtr		ompp;
	ID1FetchStructPtr	ifsp;
	SeqIdPtr		sip, sip2;
	Int4			gi;

	ompcp = (OMProcControlPtr)data;
	ompp = ompcp->proc;
        ifsp = ompp->procdata;

	if(!ifsp || ifsp->state == ID1BFS_DISABLE){  /* shut off */
		return OM_MSG_RET_OK;    /* not done, go on to next */
	} else if(ifsp->state == ID1BFS_INIT) {
                if (!ID1Init())
			return OM_MSG_RET_ERROR;
		ifsp->state = ID1BFS_READY;
        }

        if(ifsp->state != ID1BFS_READY)
		return OM_MSG_RET_ERROR;

        if((sip = (SeqIdPtr)ompcp->input_data) == NULL)
		return OM_MSG_RET_ERROR;

	if (sip->choice == SEQID_GI)
		 return OM_MSG_RET_ERROR;

	if((gi = ID1FindSeqId(sip))<=1)
		return OM_MSG_RET_OK;

	sip2 = ValNodeNew(NULL);
	sip2->choice = SEQID_GI;
	sip2->data.intvalue = gi;

        ompcp->output_data = (Pointer)sip2; /* not registering right now */

        return OM_MSG_RET_DONE;   /* data found, don't call further functions */
}

/*****************************************************************************
*
*   s_ID1BioseqFetchFunc(data)
*       callback for ID1SeqEntryGet
*
*****************************************************************************/

static Int2 LIBCALLBACK
s_ID1BioseqFetchFunc(Pointer data)
{
	OMProcControlPtr	ompcp;
	ObjMgrProcPtr		ompp;
	OMUserDataPtr		omdp;
	ID1FetchStructPtr	ifsp;
	SeqEntryPtr		sep;
	BioseqPtr		bsp;
	SeqIdPtr		sip;
	Int4			gi=0;
	Int2			retcode=0;

	ompcp = (OMProcControlPtr)data;
	ompp = ompcp->proc;
        ifsp = ompp->procdata;

	if(!ifsp || ifsp->state == ID1BFS_DISABLE){  /* shut off */
		return OM_MSG_RET_OK;    /* not done, go on to next */
	} else if(ifsp->state == ID1BFS_INIT) {
                if (!ID1Init())
			return OM_MSG_RET_ERROR;
		ifsp->state = ID1BFS_READY;
        }

        if(ifsp->state != ID1BFS_READY)
		return OM_MSG_RET_ERROR;

	/* check for cached gi */
        if (ompcp->input_entityID){
                omdp = ObjMgrGetUserData(ompcp->input_entityID,ompp->procid, OMPROC_FETCH, 0);
                if (omdp != NULL) {
			gi = omdp->userdata.intvalue;
		}
        }
        if(!gi){          /* not cached, try input id */
                sip = (SeqIdPtr)(ompcp->input_data);
                if (sip == NULL)
                        return OM_MSG_RET_ERROR;

                if(sip->choice != SEQID_GI){
			gi = ID1FindSeqId(sip);
		} else {
                        gi = sip->data.intvalue;
		}
        }

        if(!gi) return OM_MSG_RET_OK;  /* no error but call next proc */

        sep = ID1SeqEntryGet(gi,0);/*** always get a whole blob in anticipation of following requests ***/
        if(sep == NULL) 
		return OM_MSG_RET_ERROR;

        sip = (SeqIdPtr)(ompcp->input_data);
        bsp = BioseqFindInSeqEntry(sip, sep);
        ompcp->output_data = (Pointer)bsp;
        ompcp->output_entityID = ObjMgrGetEntityIDForChoice(sep);

	/* store the cache info */
        omdp = ObjMgrAddUserData(ompcp->output_entityID, ompp->procid, OMPROC_FETCH, 0);
        omdp->userdata.intvalue = gi;

        return OM_MSG_RET_DONE;   /* data found, don't call further functions */
}