home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / network / blast2 / client / blastcl2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-05  |  11.6 KB  |  410 lines  |  [TEXT/R*ch]

  1. /*
  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: blastcli.c
  27. *
  28. * Author:  Roman L. Tatusov, Jonathan Epstein, Tom Madden
  29. *
  30. * Version Creation Date:   06/16/95
  31. *
  32. * $Revision: 4.0 $
  33. *
  34. * File Description: 
  35. *       Simulates "traditional" BLAST output
  36. *
  37. * Modifications:  
  38. * --------------------------------------------------------------------------
  39. * Date     Name        Description of modification
  40. * -------  ----------  -----------------------------------------------------
  41. *
  42. * ==========================================================================
  43. *
  44. *
  45. * RCS Modification History:
  46. * $Log: blastcl2.c,v $
  47.  * Revision 4.0  1995/07/26  13:55:34  ostell
  48.  * force revision to 4.0
  49.  *
  50.  * Revision 1.6  1995/07/25  15:02:28  madden
  51.  * Error messages returned from server printed out.
  52.  *
  53.  * Revision 1.5  1995/07/24  17:34:02  madden
  54.  * Changed HitData to BLAST0Result
  55.  *
  56.  * Revision 1.4  1995/07/12  17:45:25  madden
  57.  * Call BlastBioseq with new argument to perform masking.
  58.  *
  59.  * Revision 1.3  1995/06/23  22:14:00  madden
  60.  * sixth argument in BlastBioseq call is now zero, rather than NULL.
  61.  *
  62.  * Revision 1.2  1995/06/22  17:08:16  madden
  63.  * Added "output" argument to call to BlastBioseq.
  64.  *
  65.  * Revision 1.1  1995/06/16  11:26:33  epstein
  66.  * Initial revision
  67.  *
  68.  * Revision 1.16  95/05/17  17:59:18  epstein
  69.  * add RCS log revision history
  70.  * 
  71. */
  72.  
  73. #include <sequtil.h>
  74. #include <prtutil.h>
  75. #include <tofasta.h>
  76. #include <netblap2.h>
  77.  
  78. #define MAX_SEQ_LEN 20000
  79.  
  80. /* find the last nucleotide bioseq in the bioseqset */
  81. static void FindNuc(SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
  82. {
  83.     BioseqPtr PNTR bp;
  84.     BioseqPtr local_bsp;
  85.  
  86.     bp = (BioseqPtr PNTR) data;
  87.     if (IS_Bioseq(sep))
  88.     {
  89.         local_bsp = (BioseqPtr) sep->data.ptrvalue;
  90.         if (ISA_na(local_bsp->mol))
  91.           *bp = local_bsp;
  92.     }
  93. }
  94.  
  95. /* find the last protein bioseq in the bioseqset */
  96. static void FindProt(SeqEntryPtr sep, Pointer data, Int4 index, Int2 indent)
  97. {
  98.     BioseqPtr PNTR bp;
  99.     BioseqPtr local_bsp;
  100.  
  101.     bp = (BioseqPtr PNTR) data;
  102.     if (IS_Bioseq(sep))
  103.     {
  104.         local_bsp = (BioseqPtr) sep->data.ptrvalue;
  105.         if (ISA_aa(local_bsp->mol))
  106.           *bp = local_bsp;
  107.     }
  108. }
  109.  
  110.  
  111. static Boolean LIBCALLBACK
  112. callback (BLAST0ResponsePtr brp)
  113. {
  114.     return FALSE;
  115. }
  116.  
  117.  
  118. static Int2 blast(BioseqPtr bsp, CharPtr blast_program, CharPtr blast_database,
  119. CharPtr cmd_options, CharPtr outfile, StdPrintOptionsPtr Spop, Boolean filter, Boolean standalone)
  120. {
  121.     BLAST0PrefacePtr preface;
  122.     BLAST0RequestPtr blreqp;
  123.     BLAST0ResponsePtr brp, brp1, blastResponse;
  124.     CharPtr ptr;
  125.     BLAST0ResultPtr result;
  126.     BLAST0StatusPtr status;
  127.     SeqAnnotPtr sap;
  128.     FILE *outputfile;
  129.     ValNodePtr vnp;
  130.  
  131.     if (bsp == NULL) {
  132.         ErrPostEx(SEV_FATAL, 0, 0, "Couldn't read sequences");
  133.         return (5);
  134.     }
  135. #ifdef MAX_SEQ_LEN
  136.     if (bsp != NULL && bsp->length > MAX_SEQ_LEN) {
  137.         ErrPostEx(SEV_FATAL, 0, 0, "Cannot process sequences > %d base pairs (this sequence is %d bp long)", MAX_SEQ_LEN, bsp->length);
  138.         return (4);
  139.     }
  140. #endif
  141.     if (! BlastInit(standalone ? "blasttest" : "blastcl2", FALSE)) {
  142.         ErrPostEx(SEV_FATAL, 0, 0, "Unable to initialize BLAST service");
  143.         return (1);
  144.     }
  145.     brp = NULL;
  146.     result = BlastBioseq(bsp, blast_program, blast_database, cmd_options, &brp, NULL, 0, callback);
  147.     if (result != NULL) 
  148.     {
  149. #ifdef DEBUG
  150.         AsnIoPtr aip;
  151.  
  152.         aip = AsnIoOpen("blastout.asn", "w");
  153.         BLAST0ResultAsnWrite(result, aip, NULL);
  154.         AsnIoClose(aip);
  155. #endif /* DEBUG */
  156.         outputfile = FileOpen (outfile, "w");
  157.         if (outputfile != NULL) 
  158.         {
  159.             if (Spop != NULL)
  160.             {
  161.                 ptr = FormatResultWithTemplate(result, Spop);
  162.                 if (ptr != NULL)
  163.                 {
  164.                     fprintf(outputfile, "%s", ptr);
  165.                     ptr = MemFree(ptr);
  166.                 }
  167.             }
  168.             else
  169.             {
  170.                 TraditionalBlastOutput(result, brp, blast_program, outputfile);
  171.                 BLAST0ResultFree(result);
  172.                 FileClose (outputfile);
  173.             }
  174.             while (brp)
  175.             {
  176.                 brp1 = brp;
  177.                 brp = brp->next;
  178.                 brp1->next = NULL;
  179.                 BLAST0ResponseFree(brp1);
  180.             }
  181.         } 
  182.         else 
  183.         {
  184.             BlastFini();
  185.             ErrPostEx(SEV_FATAL, 0, 0, "FileOpen failed");
  186.             return (3);
  187.         }
  188.     }
  189.     else
  190.     {
  191.         if (brp)
  192.         {
  193.             if (brp->choice == BLAST0Response_status)
  194.             {
  195.                 blreqp = ValNodeNew(NULL);
  196.                 blreqp->choice = BLAST0Response_usage_info;
  197.                 blreqp->data.ptrvalue = StringSave(blast_program);
  198.                 blastResponse = SubmitInfoRequest(blreqp);
  199.                 preface = blastResponse->data.ptrvalue;
  200.                 vnp = preface->prog_usage;
  201.                 print_usage(stderr, vnp);
  202.                 status = brp->data.ptrvalue;
  203.                 fprintf(stderr, "\nFATAL:  %s\n", status->reason);
  204.                 fprintf(stderr, "\nEXIT CODE %ld\n", (long) status->code);
  205.             }
  206.             else
  207.             {
  208.                 brp1 = brp->next;
  209.                 while (brp1)
  210.                 {
  211.                      if (brp1->choice == BLAST0Response_status)
  212.                      {
  213.                     status = brp1->data.ptrvalue;
  214.                     fprintf(stderr, "\nFATAL:  %s\n", status->reason);
  215.                     fprintf(stderr, "\nEXIT CODE %ld\n", (long) status->code);
  216.                      }
  217.                      brp1 = brp1->next;
  218.                 }
  219.             }
  220.         }
  221.         
  222.         else
  223.         {
  224.             ErrPostEx(SEV_FATAL, 0, 0, "BLAST search failed");
  225.             return (3);
  226.         }
  227.     }
  228.  
  229.     BlastFini();
  230.     return 0;
  231. }
  232.  
  233. #define NUMARG 7
  234.  
  235. static Args myargs [NUMARG] = {
  236.   { "Program Name (blastn,blastp,tblasn,blastx)", "", NULL, NULL, FALSE, 'p', ARG_STRING, 0.0, 0, NULL},
  237.   { "Database", "nr", NULL, NULL, FALSE, 'd', ARG_STRING, 0.0, 0, NULL},
  238.   { "Query File", "stdin", NULL, NULL, FALSE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
  239.   { "Output File", "stdout", NULL, NULL, FALSE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
  240.   { "Print Template", NULL, NULL, NULL, TRUE, 't', ARG_STRING, 0.0, 0, NULL} ,
  241.   { "BLAST options (enclosed in double quotes, separated by spaces)", NULL, NULL, NULL, TRUE, 'b', ARG_STRING, 0.0, 0, NULL} ,
  242.   { "Standalone mode", "F", NULL, NULL, TRUE, 's', ARG_BOOLEAN, 0.0,0,NULL}
  243. };
  244.  
  245. /*********************************************************************
  246. *    "main" function to call blast for the client.  The first "main"
  247. *    uses GetArgs and will run on Mac's and PC's; will use a 
  248. *    Print Template if that is given as an argument; and (eventually)
  249. *    will accept multiple fasta sequences from a single file.
  250. *********************************************************************/
  251. #if !defined(OLD_BLAST)
  252. Int2 Main (void)
  253.  
  254. {
  255.     Boolean filter=FALSE;
  256.         CharPtr blast_program;
  257.         CharPtr blast_database;
  258.         CharPtr blast_inputfile;
  259.         CharPtr blast_outputfile;
  260.         CharPtr blast_params;
  261.         CharPtr print_template = NULL;
  262.         Boolean isprot = FALSE;
  263.     BioseqPtr bsp;
  264.     SeqEntryPtr sep;
  265.     StdPrintOptionsPtr Spop = NULL;
  266.     FILE *fp;
  267.  
  268.     if (! GetArgs ("blastcli", NUMARG, myargs)) 
  269.     {
  270.         exit(1);
  271.     }
  272.  
  273.     blast_program = myargs [0].strvalue;
  274.  
  275.     if (strcmp(blast_program, "blastp") == 0 ) {
  276.         isprot = TRUE;
  277.     } else if (strcmp(blast_program, "blastx") == 0) {
  278.         isprot = FALSE;
  279.     } else if (strcmp(blast_program, "blastn") == 0) {
  280.         isprot = FALSE;
  281.     } else if (strcmp(blast_program, "tblastn") == 0) {
  282.         isprot = TRUE;
  283.     } else if (strcmp(blast_program, "tblastx") == 0) {
  284.         isprot = FALSE;
  285.     } else {
  286.         ErrPostEx(SEV_FATAL, 0, 0, "blast: Bad name for BLAST program: \"%s\"\n", blast_program);
  287.         exit(1);
  288.     }
  289.     blast_database = myargs [1].strvalue;
  290.     blast_inputfile = myargs [2].strvalue;
  291.     blast_outputfile = myargs [3].strvalue;
  292.     print_template = myargs [4].strvalue;
  293.     if (myargs [5].intvalue)
  294.         filter = TRUE;
  295.     blast_params = NULL;
  296.  
  297.     if ((fp = FileOpen(blast_inputfile, "r")) == NULL)
  298.     {
  299.         ErrPostEx(SEV_FATAL, 0, 0, "blast: Unable to open input file %s\n", blast_inputfile);
  300.         exit(1);
  301.     }
  302.  
  303. /* Initialize Print Template stuff */
  304.     if (print_template != NULL)
  305.     {
  306.                 if (! PrintTemplateSetLoad (print_template))
  307.                 {
  308.             ErrPostEx(SEV_FATAL, 0, 0, "blast: Unable to open print_template file %s\n", print_template);
  309.             exit(1);
  310.                 }
  311.                 Spop = StdPrintOptionsNew(NULL);
  312.                 if (Spop)
  313.                 {
  314.                         Spop->linelength = 0;
  315.                         Spop->indent = "";
  316.                         Spop->newline = "\n";
  317.                         Spop->tab = "\t";
  318.                 }
  319.                 else
  320.                 {
  321.                         ErrPostEx(SEV_FATAL, 0, 0, "blast: StdPrintOptionsNew failed");
  322.             exit(1);
  323.                 }
  324.     }
  325. /* Next task, change this to accept multiple fasta entries. */
  326.  
  327.     sep = FastaToSeqEntry(fp, !isprot);
  328.     FileClose(fp);
  329.     bsp = NULL;
  330.     SeqEntryExplore(sep, &bsp, isprot? FindProt : FindNuc);
  331.     if (bsp == NULL)
  332.     {
  333.        ErrPostEx(SEV_FATAL, 0, 0, "Unable to obtain bioseq\n");
  334.        exit (2);
  335.     }
  336.     blast_params = StringSave(myargs[5].strvalue);
  337.     return (blast(bsp, blast_program, blast_database, blast_params, blast_outputfile, Spop, filter, myargs[6].intvalue));
  338. }
  339. #else
  340.  
  341. void main(int argc, char *argv[])
  342. {
  343.         CharPtr blast_program;
  344.         CharPtr blast_database;
  345.         CharPtr blast_inputfile;
  346.         CharPtr blast_params;
  347.         int isprot = FALSE;
  348.     BioseqPtr bsp;
  349.     SeqEntryPtr sep;
  350.     FILE *fp;
  351.     int parmLen = 0;
  352.     int arg;
  353.  
  354.     if (argc < 4) {
  355.         fprintf (stderr, "Usage: %s {blastn,blastp,tblasn,blastx} database queryfile [options]\n", argv[0]);
  356.         exit(1);
  357.     }
  358.     blast_program = argv[1];
  359.  
  360.     if (strcmp(blast_program, "blastp") == 0 ) {
  361.         isprot = TRUE;
  362.     } else if (strcmp(blast_program, "blastx") == 0) {
  363.         isprot = FALSE;
  364.     } else if (strcmp(blast_program, "blastn") == 0) {
  365.         isprot = FALSE;
  366.     } else if (strcmp(blast_program, "tblastn") == 0) {
  367.         isprot = TRUE;
  368.     } else if (strcmp(blast_program, "tblastx") == 0) {
  369.         isprot = FALSE;
  370.     } else {
  371.         fprintf(stderr, "%s: Bad name for BLAST program: \"%s\"\n", argv[0], blast_program);
  372.         exit(1);
  373.     }
  374.     blast_database = argv[2];
  375.     blast_inputfile = argv[3];
  376.     if ((fp = FileOpen(blast_inputfile, "r")) == NULL)
  377.     {
  378.         fprintf(stderr, "%s: Unable to open input file %s\n", argv[0],
  379.             blast_inputfile);
  380.         exit(1);
  381.     }
  382.     sep = FastaToSeqEntry(fp, !isprot);
  383.     FileClose(fp);
  384.     bsp = NULL;
  385.     SeqEntryExplore(sep, &bsp, isprot? FindProt : FindNuc);
  386.     if (bsp == NULL)
  387.     {
  388.        fprintf (stderr, "Unable to obtain bioseq\n");
  389.        exit (2);
  390.     }
  391.     for (arg = 4; arg < argc; arg++)
  392.     {
  393.         parmLen += StrLen(argv[arg]) + 1;
  394.     }
  395.     blast_params = MemNew(parmLen);
  396.     parmLen = 0;
  397.     for (arg = 4; arg < argc; arg++)
  398.     {
  399.         StrCpy(&blast_params[parmLen], argv[arg]);
  400.         parmLen += StrLen(argv[arg]);
  401.         if (arg < argc - 1)
  402.         {
  403.         blast_params[parmLen++] = ' ';
  404.         }
  405.     }
  406.     exit (blast(bsp, blast_program, blast_database, blast_params, "stdout", NULL, FALSE, FALSE));
  407. }
  408. #endif
  409.  
  410.