initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / src / hmmfetch.c
1 /************************************************************
2  * HMMER - Biological sequence analysis with profile HMMs
3  * Copyright (C) 1992-1999 Washington University School of Medicine
4  * All Rights Reserved
5  * 
6  *     This source code is distributed under the terms of the
7  *     GNU General Public License. See the files COPYING and LICENSE
8  *     for details.
9  ************************************************************/
10
11 /* hmmfetch.c
12  * SRE, Wed Aug  5 14:26:51 1998 [St. Louis]
13  * 
14  * Recover a specific HMM file from an HMM database, using
15  * an SSI index (created with hmmindex).
16  * 
17  * CVS $Id: hmmfetch.c,v 1.1.1.1 2005/03/22 08:34:14 cmzmasek Exp $
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "squid.h"
25 #include "config.h"
26 #include "structs.h"
27 #include "funcs.h"
28 #include "version.h"
29
30 #include "globals.h"
31
32 static char banner[] = "hmmfetch -- retrieve specific HMM from an HMM database";
33
34 static char usage[] = "\
35 Usage: hmmfetch [-options] <hmmfile> <HMM name>\n\
36 Available options are:\n\
37   -h             : print short usage and version info, then exit\n\
38   -n             : interpret <HMM name> instead as an HMM number\n\
39 ";
40
41 static char experts[] = "\
42 ";
43
44 static struct opt_s OPTIONS[] = {
45    { "-h", TRUE, sqdARG_NONE  },
46    { "-n", TRUE, sqdARG_NONE  },
47 };
48 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
49
50
51 int
52 main(int argc, char **argv)
53 {
54   char    *hmmfile;             /* HMM file to open                */
55   char    *key;                 /* HMM name to retrieve            */
56   HMMFILE *hmmfp;               /* opened hmm file pointer         */
57   struct plan7_s *hmm;          /* a hidden Markov model           */
58
59   char *optname;                /* name of option found by Getopt() */
60   char *optarg;                 /* argument found by Getopt()       */
61   int   optind;                 /* index in argv[]                  */
62
63   int   by_number;              /* fetch by number, not name        */
64   int   nhmm;                   /* hmm number */
65
66   /***********************************************
67    * Parse the command line
68    ***********************************************/
69   
70   by_number = FALSE;
71
72   while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage,
73                 &optind, &optname, &optarg))
74     {
75       if      (strcmp(optname, "-n") == 0) by_number = TRUE;
76       else if (strcmp(optname, "-h") == 0)
77         {
78           Banner(stdout, banner);
79           puts(usage);
80           puts(experts);
81           exit(0);
82         }
83     }
84
85   if (argc - optind != 2) Die("Incorrect number of arguments.\n%s\n", usage);
86   hmmfile = argv[optind++];
87   key     = argv[optind++];
88
89   /***********************************************
90    * Open HMM file, make sure SSI index exists
91    ***********************************************/
92
93   if ((hmmfp = HMMFileOpen(hmmfile, "HMMERDB")) == NULL)
94     Die("failed to open HMM file %s for reading.", hmmfile);
95   if (hmmfp->ssi == NULL)
96     Die("There is no SSI index for %s; you need to use hmmindex on it.", hmmfile);
97
98   /***********************************************
99    * find key in hmmfile; get HMM; show as ASCII
100    ***********************************************/
101
102   if (by_number) {
103     if (! IsInt(key)) Die("%s does not appear to be a number.", key);
104     nhmm = atoi(key);
105     if (! HMMFilePositionByIndex(hmmfp, nhmm)) 
106       Die("failed to position %s to HMM #%d", hmmfile, nhmm);
107   } else {
108     if (! HMMFilePositionByName(hmmfp, key))
109       Die("No such hmm %s in HMM file %s\n", key, hmmfile);
110   }
111
112   if (! HMMFileRead(hmmfp, &hmm))
113     Die("Unexpected end of HMM file");
114   if (hmm == NULL) 
115     Die("HMM file %s may be corrupt or in incorrect format; parse failed", hmmfile);
116
117   WriteAscHMM(stdout, hmm);
118
119   FreePlan7(hmm);
120   HMMFileClose(hmmfp);
121
122   /***********************************************
123    * Exit
124    ***********************************************/
125
126   SqdClean();
127   return 0;
128 }
129
130