NCBI C Toolkit Cross Reference

C/access/qblastapi.c


  1 /*   qblastapi.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:  qblastapi.c
 27 *
 28 * Author:  Jonathan Kans
 29 *
 30 * Version Creation Date:   6/28/00
 31 *
 32 * $Revision: 1.33 $
 33 *
 34 * File Description: 
 35 *
 36 * Modifications:  
 37 * --------------------------------------------------------------------------
 38 *
 39 * $Log: qblastapi.c,v $
 40 * Revision 1.33  2006/07/13 17:03:51  bollin
 41 * resolved compiler warnings
 42 *
 43 * Revision 1.32  2005/11/28 17:49:55  coulouri
 44 * correction to previous commit; define snprintf for windows
 45 *
 46 * Revision 1.31  2005/11/28 15:32:11  coulouri
 47 * do not overflow string buffer if BioseqGetTitle() returns NULL
 48 *
 49 * Revision 1.30  2004/02/23 15:30:02  lavr
 50 * New (last) parameter "how" added in CONN_Write() API call
 51 *
 52 * Revision 1.29  2004/02/20 17:13:01  madden
 53 * Doubled some timeouts
 54 *
 55 * Revision 1.28  2003/11/14 16:23:08  dondosha
 56 * If query id not local, append title to id to get defline in BLASTGetQuerySummary
 57 *
 58 * Revision 1.27  2003/11/12 21:19:03  kans
 59 * changed query_number to query_num - fixing typo
 60 *
 61 * Revision 1.26  2003/11/12 20:49:20  coulouri
 62 * Replace code accidentally removed between 1.24 and 1.25 in BlastGetQueryBioseqByRIDEx
 63 *
 64 * Revision 1.25  2003/11/03 20:50:53  madden
 65 * Fix problem with content length, remove call to QblastTestRID, add BLASTGetBOByRIDEx
 66 *
 67 * Revision 1.24  2003/01/17 20:40:29  madden
 68 * Put QUERY_NUMBER back in URL for fetching Seqannot
 69 *
 70 * Revision 1.23  2003/01/15 22:04:49  madden
 71 * Moved BLASTGetSeqAnnotByRIDEx from internal
 72 *
 73 * Revision 1.22  2003/01/02 15:06:48  boemker
 74 * Wrote BLASTGetQueryBioseqByRIDEx that extends behavior of
 75 * BLASTGetQueryBioseqByRID by returning the Bioseq for a particular query
 76 * (rather than always returning the Bioseq for the first query.  Modified
 77 * BLASTGetQueryBioseqByRID to use BLASTGetQueryBioseqByRIDEx.  Wrote
 78 * BLASTGetQuerySummary to return information about a query.
 79 *
 80 * Revision 1.21  2002/11/08 22:36:17  kans
 81 * in synchronous query, loop on == eIO_Timeout instead of != eIO_Success
 82 *
 83 * Revision 1.20  2002/08/07 18:45:15  lavr
 84 * Change from deprecated to current EIO_ReadMethod enums
 85 *
 86 * Revision 1.19  2002/07/24 21:09:56  kans
 87 * reverted ncbi URL
 88 *
 89 * Revision 1.18  2002/07/23 16:45:21  kans
 90 * changed www.ncbi.nlm.nih.gov to www.ncbi.nih.gov
 91 *
 92 * Revision 1.17  2002/04/12 14:27:35  madden
 93 * Check RID for internal jobs, send to appropriate place
 94 *
 95 * Revision 1.16  2001/02/25 21:50:41  kans
 96 * changes from Uint4 to size_t due to connection type changes
 97 *
 98 * Revision 1.15  2001/02/21 22:07:34  lavr
 99 * Changes for use new CONN interface
100 *
101 * Revision 1.14  2000/12/06 17:53:31  madden
102 * make QBBioseqToFasta non-static, fix UMR
103 *
104 * Revision 1.13  2000/12/04 18:07:31  madden
105 * BLASTGetBOByRID uses Blast.cgi
106 *
107 * Revision 1.12  2000/11/07 21:54:52  shavirin
108 * Added new function BLASTGetQueryBioseqByRID().
109 *
110 * Revision 1.10  2000/08/30 14:55:08  kans
111 * parentheses around cast of currtime - curr->initialTime in printf
112 *
113 * Revision 1.9  2000/07/19 15:16:12  shavirin
114 * Added new function CharPtr BLASTGetBOByRID(CharPtr RID);
115 *
116 *
117 * ==========================================================================
118 */
119 
120 #include <ncbi.h>
121 #include <urlquery.h>
122 #include <qblastapi.h>
123 #include <tofasta.h>
124 #include <sqnutils.h>
125 
126 #ifdef OS_MAC
127 #include <Events.h>
128 #endif
129 
130 #ifdef _MSC_VER
131 #define strcasecmp _stricmp
132 #define strdup _strdup
133 #define snprintf _snprintf
134 #endif
135 
136 static CharPtr ReadALine (CharPtr str, size_t size, FILE *fp)
137 
138 {
139   Char     ch;
140   CharPtr  ptr;
141   CharPtr  rsult;
142 
143   if (str == NULL || size < 1 || fp == NULL) return NULL;
144   *str = '\0';
145   rsult = fgets (str, size, fp);
146   if (rsult != NULL) {
147     ptr = str;
148     ch = *ptr;
149     while (ch != '\0' && ch != '\n' && ch != '\r') {
150       ptr++;
151       ch = *ptr;
152     }
153     *ptr = '\0';
154   }
155   return rsult;
156 }
157 
158 NLM_EXTERN CONN QBlastOpenConnection (
159   void
160 )
161 
162 {
163  return QUERY_OpenUrlQuery ("www.ncbi.nlm.nih.gov", 80, "/blast/Blast.cgi",
164                              NULL, "qblastapp", 30, eMIME_T_Application,
165                              eMIME_WwwForm, eENCOD_Url, 0);
166 }
167 
168 NLM_EXTERN EIO_Status QBlastWaitForReply (
169   CONN conn
170 )
171 
172 {
173   time_t           currtime, starttime;
174   time_t           max = 0;
175   EIO_Status       status;
176   STimeout         timeout;
177 #ifdef OS_MAC
178   EventRecord      currEvent;
179 #endif
180 
181   if (conn == NULL) return eIO_Unknown;
182 
183 #ifdef OS_MAC
184   timeout.sec = 0;
185   timeout.usec = 0;
186 #else
187   timeout.sec = 100;
188   timeout.usec = 0;
189 #endif
190 
191   starttime = GetSecs ();
192   while ((status = CONN_Wait (conn, eIO_Read, &timeout)) == eIO_Timeout && max < 300) {
193     currtime = GetSecs ();
194     max = currtime - starttime;
195 #ifdef OS_MAC
196     WaitNextEvent (0, &currEvent, 0, NULL);
197 #endif
198   }
199 
200   return status;
201 }
202 
203 typedef struct QBQueueTag {
204   Char                rid [64];
205   Char                seqid [41];
206   time_t              estTime;
207   time_t              initialTime;
208   time_t              postedTime;
209   Int2                secondsToWait;
210   QBlastResultProc    resultproc;
211   QBlastAnnounceProc  announceproc;
212   Nlm_VoidPtr         userdata;
213   QUEUE               connqueue;
214   Boolean             ready;
215   Boolean             fetching;
216   Boolean             done;
217   struct QBQueueTag*  next;
218 } QBlastQueue, PNTR QBQueuePtr;
219 
220 static Boolean LIBCALLBACK ThirdQBlastCallback (
221   CONN conn,
222   Nlm_VoidPtr userdata,
223   EIO_Status  status
224 )
225 
226 {
227   QBQueuePtr  cqp;
228   FILE        *fp;
229   Char        path [PATH_MAX];
230 
231   cqp = (QBQueuePtr) userdata;
232 
233   TmpNam (path);
234   fp = FileOpen (path, "w");
235   QUERY_CopyResultsToFile (conn, fp);
236   FileClose (fp);
237 
238   cqp->resultproc (path, cqp->userdata, cqp->rid, cqp->seqid, TRUE);
239   cqp->done = TRUE;
240 
241   FileRemove (path);
242 
243   return TRUE;
244 }
245 
246 static Boolean LIBCALLBACK SecondQBlastCallback (
247   CONN conn,
248   Nlm_VoidPtr userdata,
249   EIO_Status  status
250 )
251 
252 {
253   QBQueuePtr  cqp;
254   FILE        *fp;
255   Char        line [256];
256   Char        path [PATH_MAX];
257   CharPtr     sttus;
258   CharPtr     str;
259   Boolean     waiting = FALSE;
260 
261   /* look for waiting, failure, or success */
262 
263   cqp = (QBQueuePtr) userdata;
264 
265   TmpNam (path);
266   fp = FileOpen (path, "w");
267   QUERY_CopyResultsToFile (conn, fp);
268   FileClose (fp);
269 
270   fp = FileOpen (path, "r");
271   str = ReadALine (line, sizeof (line), fp);
272   while (str != NULL) {
273     if (! StringHasNoText (line)) {
274       sttus = StringStr (line, "Status=");
275       if (sttus != NULL) {
276         sttus += 7;
277         if (StringStr (sttus, "WAITING") != NULL) {
278           waiting = TRUE;
279         } else if (StringStr (sttus, "READY") != NULL) {
280           cqp->ready = TRUE;
281         /* } else if (StringStr (sttus, "FAILED") != NULL) {
282           cqp->done = TRUE; */
283         }
284       }
285     }
286     str = ReadALine (line, sizeof (line), fp);
287   }
288   FileClose (fp);
289 
290   if (cqp->done) {
291     cqp->resultproc (NULL, cqp->userdata, cqp->rid, cqp->seqid, FALSE);
292   }
293 
294   FileRemove (path);
295 
296   return TRUE;
297 }
298 
299 static Boolean LIBCALLBACK FirstQBlastCallback (
300   CONN conn,
301   Nlm_VoidPtr userdata,
302   EIO_Status  status
303 )
304 
305 {
306   QBQueuePtr  cqp;
307   FILE        *fp;
308   Char        line [256];
309   Char        path [PATH_MAX];
310   CharPtr     ptr;
311   Char        rid [64];
312   time_t      rtoe;
313   CharPtr     str;
314   long int    val;
315 
316   /* read rID or failure message */
317 
318   cqp = (QBQueuePtr) userdata;
319 
320   TmpNam (path);
321   fp = FileOpen (path, "w");
322   QUERY_CopyResultsToFile (conn, fp);
323   FileClose (fp);
324 
325   rid [0] = '\0';
326   rtoe = 15;
327 
328   fp = FileOpen (path, "r");
329   str = ReadALine (line, sizeof (line), fp);
330   while (str != NULL) {
331     if (! StringHasNoText (line)) {
332       ptr = StringStr (line, "RID = ");
333       if (ptr != NULL) {
334         ptr += 6;
335         StringNCpy_0 (rid, ptr, sizeof (rid));
336       }
337       ptr = StringStr (line, "RTOE = ");
338       if (ptr != NULL) {
339         ptr += 7;
340         if (sscanf (ptr, "%ld", &val) == 1) {
341           rtoe = (time_t) val;
342         }
343       }
344     }
345     str = ReadALine (line, sizeof (line), fp);
346   }
347   FileClose (fp);
348 
349   FileRemove (path);
350 
351   if (! StringHasNoText (rid)) {
352     StringNCpy_0 (cqp->rid, rid, sizeof (cqp->rid));
353     cqp->estTime = (time_t) rtoe;
354     cqp->secondsToWait = (Int2) rtoe + 2;
355     if (cqp->secondsToWait > 15) {
356       cqp->secondsToWait = 15;
357     }
358     if (cqp->announceproc != NULL) {
359       cqp->announceproc (rid, cqp->seqid, (Int2) val);
360     }
361 
362   } else {
363 
364     cqp->done = TRUE;
365     if (cqp->resultproc != NULL) {
366       cqp->resultproc (NULL, cqp->userdata, cqp->rid, cqp->seqid, FALSE);
367     }
368   }
369 
370   return TRUE;
371 }
372 
373 static void QBlast_AddToQueue (
374   QBQUEUE * queue,
375   QBlastResultProc resultproc,
376   QBlastAnnounceProc announceproc,
377   Nlm_VoidPtr userdata,
378   CONN conn,
379   BioseqPtr bsp,
380   CharPtr rid
381 )
382 
383 {
384   QBQueuePtr       cqp;
385   QBQueuePtr PNTR  qptr;
386   QBQueuePtr       tmp;
387 
388   if (queue == NULL || resultproc == NULL || conn == NULL) return;
389 
390   /* allocate queue element */
391 
392   cqp = (QBQueuePtr) MemNew (sizeof (QBlastQueue));
393   if (cqp == NULL) return;
394 
395   cqp->rid [0] = '\0';
396   if (rid != NULL) {
397     StringNCpy_0 (cqp->rid, rid, sizeof (cqp->rid));
398   }
399   cqp->seqid [0] = '\0';
400   if (bsp != NULL) {
401     SeqIdWrite (bsp->id, cqp->seqid, PRINTID_FASTA_LONG, 40);
402   }
403 
404   cqp->estTime = 0;
405   cqp->initialTime = GetSecs ();
406   cqp->postedTime = cqp->initialTime;
407   cqp->secondsToWait = 15;
408   cqp->resultproc = resultproc;
409   cqp->announceproc = announceproc;
410   cqp->userdata = userdata;
411   cqp->connqueue = NULL;
412   cqp->ready = FALSE;
413   cqp->fetching = FALSE;
414   cqp->done = FALSE;
415 
416   /* add to polling queue */
417 
418   qptr = (QBQueuePtr PNTR) queue;
419   if (qptr != NULL) {
420     if (*qptr != NULL) {
421       tmp = *qptr;
422       if (tmp != NULL) {
423         while (tmp->next != NULL) {
424           tmp = tmp->next;
425         }
426         tmp->next = cqp;
427       }
428     } else {
429       *qptr = cqp;
430     }
431   }
432 
433   /* queue the request for a rID */
434 
435   if (bsp != NULL) {
436 
437     QUERY_AddToQueue (&(cqp->connqueue), conn, FirstQBlastCallback, (Pointer) cqp, TRUE);
438 
439   } else if (rid != NULL) {
440 
441     QUERY_AddToQueue (&(cqp->connqueue), conn, SecondQBlastCallback, (Pointer) cqp, TRUE);
442 
443   }
444 }
445 
446 /* print just the sequence with no punctuation */
447 
448 static Boolean QBlastFastaFileFunc (BioseqPtr bsp, Int2 key, CharPtr buf,
449                                     Uint4 buflen, Pointer data)
450 {
451   FILE * fp;
452   CharPtr bufr;
453   size_t len, bufsize;
454   size_t src_read, dst_written;
455 
456   fp = (FILE *)data;
457 
458   switch (key) {
459     case FASTA_ID:
460       len = StringLen (buf);
461       if (len > 0) {
462         bufsize = len * 3 + 2;
463         bufr = MemNew (bufsize);
464         if (bufr != NULL) {
465           URL_Encode (buf, len, &src_read, bufr, bufsize, &dst_written);
466           fprintf (fp, "%s", "%3E");
467           fprintf(fp, "%s+", bufr);
468         }
469         MemFree (bufr);
470       }
471       /* fprintf(fp, ">%s ", buf); */
472       break;
473     case FASTA_DEFLINE:
474       len = StringLen (buf);
475       if (len > 0) {
476         bufsize = len * 3 + 2;
477         bufr = MemNew (bufsize);
478         if (bufr != NULL) {
479           URL_Encode (buf, len, &src_read, bufr, bufsize, &dst_written);
480           fprintf(fp, "%s", bufr);
481           fprintf (fp, "%s", "%0D%0A");
482         }
483         MemFree (bufr);
484       }
485       /* fprintf(fp, "%s\n", buf); */
486       break;
487     case FASTA_SEQLINE:
488       fprintf(fp, "%s", buf); /* no newline */
489       break;
490     case FASTA_EOS:   /* end of sequence */
491       break;
492     default:
493       break;
494   }
495   return TRUE;
496 }
497 
498 #define QB_FASTA_BUFFER_LEN 400
499 
500 Boolean QBBioseqToFasta (BioseqPtr bsp, FILE *fp, Boolean is_na)
501 
502 {
503   MyFsa mfa;
504   Char buf [QB_FASTA_BUFFER_LEN + 1];
505 
506   if (bsp == NULL || fp == NULL) return FALSE;
507 
508   MemSet(&mfa, 0, sizeof(MyFsa));
509 
510   mfa.buf = buf;
511   mfa.buflen = QB_FASTA_BUFFER_LEN;
512   mfa.seqlen = 80;
513   mfa.mydata = (Pointer)fp;
514   mfa.myfunc = QBlastFastaFileFunc;
515   mfa.bad_asn1 = FALSE;
516   mfa.order = 0;
517   mfa.accession = NULL;
518   mfa.organism = NULL;
519   mfa.do_virtual = FALSE;
520   mfa.tech = 0;
521   mfa.no_sequence = FALSE;
522   mfa.formatdb = FALSE;
523 
524   return BioseqToFastaX (bsp, &mfa, is_na);
525 }
526 
527 NLM_EXTERN Boolean QBlastAsynchronousRequest (
528   CharPtr database,
529   CharPtr program,
530   BioseqPtr bsp,
531   QBQUEUE* queue,
532   QBlastResultProc resultproc,
533   QBlastAnnounceProc announceproc,
534   VoidPtr userdata
535 )
536 
537 {
538   CONN    conn;
539   FILE    *fp;
540   size_t  n_written;
541   Char    path [PATH_MAX];
542   Char    str [256];
543 
544   if (bsp == NULL || queue == NULL || resultproc == NULL) return FALSE;
545 
546   if (StringHasNoText (database)) {
547     database = "nr";
548   }
549   if (StringHasNoText (program)) {
550     program = "blastn";
551   }
552   sprintf (str, "DATABASE=%s&PROGRAM=%s&CMD=Put&QUERY_BELIEVE_DEFLINE=yes&QUERY=",
553            database, program);
554   conn = QBlastOpenConnection ();
555   if (conn == NULL) return FALSE;
556 
557   TmpNam (path);
558 
559   fp = FileOpen (path, "w");
560   QBBioseqToFasta (bsp, fp, ISA_na (bsp->mol));
561   FileClose (fp);
562 
563   fp = FileOpen (path, "r");
564   CONN_Write (conn, (const void *) str, StringLen (str),
565               &n_written, eIO_WritePersist);
566   QUERY_CopyFileToQuery (conn, fp);
567   FileClose (fp);
568 
569   QUERY_SendQuery (conn);
570 
571   QBlast_AddToQueue (queue, resultproc, announceproc, userdata, conn, bsp, NULL);
572 
573   FileRemove (path);
574 
575   return TRUE;
576 }
577 
578 static void QBlast_RemoveFromQueue (
579   QBQUEUE* queue, QBQueuePtr freeme
580 )
581 
582 {
583   QBQueuePtr       curr;
584   QBQueuePtr       next;
585   QBQueuePtr PNTR  prev;
586   QBQueuePtr PNTR  qptr;
587 
588   qptr = (QBQueuePtr PNTR) queue;
589   if (qptr == NULL || *qptr == NULL || freeme == NULL) return;
590 
591   prev = qptr;
592   curr = *qptr;
593 
594   while (curr != NULL) {
595     next = curr->next;
596     if (curr == freeme) {
597       *(prev) = next;
598       curr->next = NULL;
599       MemFree (curr);
600     } else {
601       prev = &(curr->next);
602     }
603     curr = next;
604   }
605 }
606 
607 NLM_EXTERN Int4 QBlastCheckQueue (
608   QBQUEUE* queue
609 )
610 
611 {
612   CONN             conn;
613   Nlm_Int4         count = 0;
614   QBQueuePtr       curr;
615   time_t           currtime;
616   QBQueuePtr       next;
617   size_t           n_written;
618   QBQueuePtr PNTR  qptr;
619   Char             str [80];
620 
621   qptr = (QBQueuePtr PNTR) queue;
622   if (qptr == NULL || *qptr == NULL) return 0;
623 
624   curr = *qptr;
625 
626   /* first loop through actual url requests, call first-third callbacks */
627 
628   while (curr != NULL) {
629     next = curr->next;
630 
631     /* check for return of rID, WAITING, of SUCCESS message */
632 
633     QUERY_CheckQueue (&(curr->connqueue));
634 
635     if (curr->done) {
636       QBlast_RemoveFromQueue (queue, curr);
637     }
638 
639     curr = next;
640   }
641 
642   /* now send new url queries if appropriate */
643 
644   curr = *qptr;
645   if (curr == NULL) return 0;
646 
647   while (curr != NULL) {
648     next = curr->next;
649 
650     if (curr->done) {
651       QBlast_RemoveFromQueue (queue, curr);
652 
653     } else {
654 
655       if ((! curr->fetching) && (! curr->ready) && curr->connqueue == NULL) {
656         currtime = GetSecs ();
657         if (currtime - curr->postedTime < curr->secondsToWait) {
658         } else if (! StringHasNoText (curr->rid)) {
659 
660           /* estimated wait time has expired, so queue another check */
661 
662           if (curr->secondsToWait < 60) {
663             curr->secondsToWait *= 2;
664           }
665           curr->postedTime = GetSecs ();
666 
667           sprintf (str, "RID=%s&CMD=Get\n", curr->rid);
668           conn = QBlastOpenConnection ();
669           if (conn != NULL) {
670             CONN_Write (conn, (const void *) str, StringLen (str),
671                         &n_written, eIO_WritePersist);
672             QUERY_SendQuery (conn);
673             QUERY_AddToQueue (&(curr->connqueue), conn, SecondQBlastCallback, (Pointer) curr, TRUE);
674           } else {
675             ErrPostEx (SEV_ERROR, 0, 0, "Unable to open QBlast connection");
676           }
677         }
678       }
679 
680       count++;
681     }
682 
683     if ((! curr->done) && curr->ready && (! curr->fetching) && curr->connqueue == NULL) {
684       curr->fetching = TRUE;
685       sprintf (str, "CMD=Get&RID=%s&FORMAT_OBJECT=Alignment&FORMAT_TYPE=ASN.1\n", curr->rid);
686       conn = QBlastOpenConnection ();
687       if (conn != NULL) {
688         CONN_Write (conn, (const void *) str, StringLen (str),
689                     &n_written, eIO_WritePersist);
690         QUERY_SendQuery (conn);
691         QUERY_AddToQueue (&(curr->connqueue), conn, ThirdQBlastCallback, (Pointer) curr, TRUE);
692       } else {
693         ErrPostEx (SEV_ERROR, 0, 0, "Unable to open QBlast connection");
694       }
695     }
696 
697     curr = next;
698   }
699 
700   return count;
701 }
702 
703 NLM_EXTERN Boolean QBlastCheckRequest (
704   CharPtr rid,
705   QBQUEUE* queue,
706   QBlastResultProc resultproc,
707   QBlastAnnounceProc announceproc,
708   VoidPtr userdata
709 )
710 
711 {
712   CONN    conn;
713   size_t  n_written;
714   Char    str [256];
715 
716   if (StringHasNoText (rid) || queue == NULL || resultproc == NULL) return FALSE;
717 
718   sprintf (str, "RID=%s&CMD=Get\n", rid);
719   conn = QBlastOpenConnection ();
720   if (conn == NULL) return FALSE;
721   CONN_Write (conn, (const void *) str, StringLen (str),
722               &n_written, eIO_WritePersist);
723   QUERY_SendQuery (conn);
724 
725   QBlast_AddToQueue (queue, resultproc, announceproc, userdata, conn, NULL, rid);
726 
727   return TRUE;
728 }
729 
730 NLM_EXTERN void QBlastCloseQueue (
731   QBQUEUE* queue
732 )
733 
734 {
735   QBQueuePtr       curr;
736   QBQueuePtr       next;
737   QBQueuePtr PNTR  qptr;
738 
739   qptr = (QBQueuePtr PNTR) queue;
740   if (qptr == NULL || *qptr == NULL) return;
741 
742   curr = *qptr;
743 
744   while (curr != NULL) {
745     next = curr->next;
746 
747     if (curr->connqueue != NULL) {
748       QUERY_CloseQueue (&(curr->connqueue));
749     }
750     QBlast_RemoveFromQueue (queue, curr);
751 
752     curr = next;
753   }
754 }
755 
756 NLM_EXTERN Int4 PrintQBlastQueue (
757   QBQUEUE* queue,
758   FILE *fp
759 )
760 
761 {
762   Nlm_Int4         count = 0;
763   QBQueuePtr       curr;
764   time_t           currtime;
765   QBQueuePtr       next;
766   QBQueuePtr PNTR  qptr;
767 
768   qptr = (QBQueuePtr PNTR) queue;
769   if (qptr == NULL || *qptr == NULL || fp == NULL) return 0;
770 
771   currtime = GetSecs ();
772   curr = *qptr;
773 
774   while (curr != NULL) {
775     next = curr->next;
776 
777     if (! curr->done) {
778 
779       fprintf (fp, "%s\t%s\t%d\t%d\n", curr->rid, curr->seqid,
780                (int) (currtime - curr->initialTime), (int) curr->estTime);
781       count++;
782     }
783 
784     curr = next;
785   }
786 
787   return count;
788 }
789 
790 static void LIBCALLBACK AsnIoErrorFunc(Int2 type, CharPtr message)
791 {
792     return;
793 }
794 
795 /* Function to get SeqAnnot for RID. We suupose, that search already
796    finished and results are exists on the Qblast repository */
797 
798 NLM_EXTERN SeqAnnotPtr BLASTGetSeqAnnotByRID(CharPtr RID)
799 {
800     return BLASTGetSeqAnnotByRIDEx(RID, NULL, 0, NULL, 0);
801 }
802 
803 /*
804         Returns a SeqAnnot for a given RID.  
805         If host_machine and host_path are set to NULL and host_port to zero
806         Default servers will be used.
807 
808         if query_number is set to zero all will be returned, otherwise the
809         one-based offset query will be returned.
810  */
811 NLM_EXTERN SeqAnnotPtr BLASTGetSeqAnnotByRIDEx(CharPtr RID,
812                                                Nlm_CharPtr host_machine, 
813                                                Nlm_Uint2 host_port, 
814                                                Nlm_CharPtr host_path,
815                                                Int4 query_number)
816 {
817     Char         query_string[256];
818     CONN         conn;
819     SeqAnnotPtr  annot;
820     size_t       n_written;
821     Int4         length;
822     AsnIoConnPtr aicp;
823     EIO_Status   status;
824 
825     sprintf(query_string,
826             "FORMAT_TYPE=ASN.1&CMD=Get&RID=%s&QUERY_NUMBER=%ld&FORMAT_OBJECT=Alignment", 
827             RID,
828             (long) query_number);
829 
830     conn = QUERY_OpenUrlQuery ((host_machine == NULL) ? "www.ncbi.nlm.nih.gov" : host_machine, 
831                                 (host_port <= 0) ? 80 : host_port, 
832                                 (host_path == NULL) ? "/blast/Blast.cgi" : host_path, 
833                                 NULL, "BLASTGetSeqAnnotByRIDEx()", 
834                                240, eMIME_T_Application, 
835                                eMIME_WwwForm, eENCOD_Url, 0);
836     
837     length = StringLen(query_string);
838     status = CONN_Write (conn, query_string, length,
839                          &n_written, eIO_WritePersist);
840     QUERY_SendQuery (conn);
841     
842     aicp = QUERY_AsnIoConnOpen ("r", conn);
843     AsnIoSetErrorMsg(aicp->aip, AsnIoErrorFunc);
844 
845     annot = SeqAnnotAsnRead (aicp->aip, NULL);
846     QUERY_AsnIoConnClose (aicp);
847 
848     CONN_Close(conn);
849     return annot;
850 }
851 
852 /* Function to get Bioseq for RID. We suupose, that search already
853    finished and results are exists on the Qblast repository */
854 
855 NLM_EXTERN BioseqPtr BLASTGetQueryBioseqByRID(CharPtr RID)
856 {
857         return BLASTGetQueryBioseqByRIDEx(RID, 0);
858 }
859 
860 NLM_EXTERN BioseqPtr BLASTGetQueryBioseqByRIDEx(CharPtr RID, int query_num)
861 {
862     Char         query_string[256];
863     CONN         conn;
864     BioseqPtr    bsp;
865     size_t       n_written;
866     Int4         length;
867     AsnIoConnPtr aicp;
868     EIO_Status   status;
869 
870     sprintf(query_string,
871             "FORMAT_TYPE=ASN.1&CMD=Get&RID=%s&QUERY_NUMBER=%ld&FORMAT_OBJECT=Bioseq", 
872             RID,
873             (long) query_num);
874     
875     conn = QUERY_OpenUrlQuery ("www.ncbi.nlm.nih.gov", 80, 
876                                "/blast/Blast.cgi", NULL, 
877                                "BLASTGetQueryBioseqByRIDEx()", 
878                                60, eMIME_T_Application, 
879                                eMIME_WwwForm, eENCOD_Url, 0);
880 
881     length = StringLen(query_string);
882     status = CONN_Write (conn, query_string, length,
883                          &n_written, eIO_WritePersist);
884     QUERY_SendQuery (conn);
885     
886     aicp = QUERY_AsnIoConnOpen ("r", conn);
887     AsnIoSetErrorMsg(aicp->aip, AsnIoErrorFunc);
888 
889     bsp = BioseqAsnRead (aicp->aip, NULL);
890     QUERY_AsnIoConnClose (aicp);
891 
892     CONN_Close(conn);
893     return bsp;
894 }
895 
896 NLM_EXTERN Nlm_Boolean BLASTGetQuerySummary(CharPtr RID, Int4 query_number,
897     CharPtr *defline, Int4Ptr query_length)
898 {
899     BioseqPtr bsp = BLASTGetQueryBioseqByRIDEx(RID, query_number);
900     if(!bsp)
901         return FALSE;
902     if (bsp->id->choice == SEQID_LOCAL) {
903        *defline = StringSave(BioseqGetTitle(bsp));
904     } else {
905 #define SEQID_LENGTH 255
906        Int4 id_length;
907        Char tmp[SEQID_LENGTH+1];
908        Int4 title_length = StringLen(BioseqGetTitle(bsp));
909        SeqIdWrite(bsp->id, tmp, PRINTID_FASTA_LONG, SEQID_LENGTH);
910        id_length = StringLen(tmp);
911        title_length += id_length + 3;
912        *defline = (CharPtr) MemNew(title_length*sizeof(Char));
913        snprintf(*defline, title_length, "%s %s", tmp, BioseqGetTitle(bsp));
914     }
915     *query_length = BioseqGetLen(bsp);
916     BioseqFree(bsp);
917     return TRUE;
918 }
919 
920 
921 /* Function to get BlastObject for RID. We suupose, that search already
922    finished and results are exists on the Qblast repository. Blast Object
923    ASN.1 will be returned as CharPtr buffer*/
924 /*
925  * retrive blast object
926  */
927 NLM_EXTERN CharPtr BLASTGetBOByRIDEx(CharPtr RID,
928                                      Nlm_CharPtr host_machine,
929                                      Nlm_Uint2 host_port,
930                                      Nlm_CharPtr host_path)
931 {
932     Char         query_string[256];
933     CONN         conn;
934     size_t       n_written;
935     size_t       bytes;
936     Int4         length, buff_len, new_size;
937     EIO_Status   status;
938     CharPtr      in_buff;
939     
940     sprintf(query_string, "CMD=Get&RID=%s&FORMAT_OBJECT=BlastObject&FORMAT_TYPE=ASN.1", RID);
941 
942     conn = QUERY_OpenUrlQuery ((host_machine == NULL) ? "www.ncbi.nlm.nih.gov" : host_machine,
943                                 (host_port <= 0) ? 80 : host_port,
944                                 (host_path == NULL) ? "/blast/Blast.cgi" : host_path,
945                                 NULL,
946                                "BLASTGetBOByRIDEx()", 
947                                60, eMIME_T_Application, 
948                                eMIME_WwwForm, eENCOD_Url, 0);
949 
950     length = StringLen(query_string);
951     status = CONN_Write (conn, query_string, length,
952                          &n_written, eIO_WritePersist);
953     QUERY_SendQuery (conn);
954     
955     new_size = 1024;   
956     if((in_buff = (CharPtr)MemNew(new_size)) == NULL) {
957         ErrPostEx(SEV_ERROR, 0, 0, "Error in allocating memory\n");
958         return NULL;
959     }
960 
961     buff_len = 0;
962     while ((status = CONN_Read(conn, in_buff + buff_len, 
963                                1024, &bytes, eIO_ReadPlain)) == eIO_Success) {
964         if(bytes == 0) 
965             break;
966 
967         new_size += bytes;
968         buff_len += bytes;
969         
970         if ((in_buff = (CharPtr)Realloc(in_buff, new_size)) == NULL) {
971             ErrPostEx(SEV_ERROR, 0, 0, "Error in reallocating memory\n");
972             return NULL;
973         }
974     }
975     
976     in_buff[buff_len] = NULLB;
977     
978     CONN_Close(conn);
979     return in_buff;
980 }
981 
982 NLM_EXTERN CharPtr BLASTGetBOByRID(CharPtr RID)
983 {
984         return BLASTGetBOByRIDEx(RID, NULL, 0, NULL);
985 }
986 

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.