initial commit
[jalview.git] / forester / archive / RIO / others / hmmer / src / hmmindex.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 /* hmmindex.c
12  * SRE, Wed Aug  5 11:05:03 1998 [St. Louis]
13  * 
14  * Create an SSI index file for an HMM database.
15  * 
16  * CVS $Id: hmmindex.c,v 1.1.1.1 2005/03/22 08:34:03 cmzmasek Exp $
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "squid.h"
24 #include "config.h"
25 #include "structs.h"
26 #include "funcs.h"
27 #include "version.h"
28 #include "globals.h"
29 #include "ssi.h"
30
31 static char banner[] = "hmmindex -- create SSI index for an HMM database";
32
33 static char usage[] = "\
34 Usage: hmmindex [-options] <hmmfile>\n\
35 Available options are:\n\
36   -h             : print short usage and version info, then exit\n\
37 ";
38
39 static char experts[] = "\
40 ";
41
42 static struct opt_s OPTIONS[] = {
43    { "-h", TRUE, sqdARG_NONE  },
44 };
45 #define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))
46
47 int
48 main(int argc, char **argv)
49 {
50   char    *hmmfile;             /* HMM file to open                */
51   SSIINDEX *ssi;                /* SSI index in memory             */
52   char    *ssifile;             /* name of SSI index on disk       */
53   HMMFILE *hmmfp;               /* opened hmm file pointer         */
54   struct plan7_s     *hmm;      /* a hidden Markov model           */
55   int     idx, nhmm;            /* counter over HMMs               */
56   int     npri, nsec;           /* # of names, accessions          */
57   int     fh;                   /* file handle                     */
58   int     status;               /* return status from SSI call     */
59
60   char *optname;                /* name of option found by Getopt() */
61   char *optarg;                 /* argument found by Getopt()       */
62   int   optind;                 /* index in argv[]                  */
63
64   /***********************************************
65    * Parse the command line
66    ***********************************************/
67
68   while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage,
69                 &optind, &optname, &optarg))
70     {
71       if (strcmp(optname, "-h")        == 0)
72         {
73           Banner(stdout, banner);
74           puts(usage);
75           puts(experts);
76           exit(0);
77         }
78     }
79
80   if (argc - optind != 1) Die("Incorrect number of arguments.\n%s\n", usage);
81   hmmfile = argv[optind++];
82
83   /***********************************************
84    * Open our input HMM file, make sure all is well with the output SSI filename
85    ***********************************************/
86
87   if ((hmmfp = HMMFileOpen(hmmfile, NULL)) == NULL)
88     Die("failed to open HMM file %s for reading.", hmmfile);
89   if (hmmfp->ssi != NULL)
90     Die("SSI index already exists for %s.\nPlease delete it first.", hmmfile);
91   
92   ssifile = MallocOrDie(strlen(hmmfile) + 5);
93   sprintf(ssifile, "%s%s", hmmfile, ".ssi");
94   if (FileExists(ssifile))   /* shouldn't happen */
95     Die("An SSI file %s already exists; please delete it first", ssifile);
96
97   if ((ssi = SSICreateIndex(hmmfp->mode)) == NULL)
98     Die("Failed to initialize the SSI index structure");
99   if (SSIAddFileToIndex(ssi, hmmfile, hmmfp->is_binary, &fh) != 0)
100     Die("SSIAddFileToIndex() failed");
101
102   /*********************************************** 
103    * Show the banner
104    ***********************************************/
105
106   Banner(stdout, banner);
107   printf("HMM file:                 %s\n", hmmfile);
108   if (hmmfp->mode == SSI_OFFSET_I64) 
109     printf("Index file mode:          64-bit (large HMM file)\n");
110   printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n");
111
112   /***********************************************
113    * Get offsets and names for every model; store in keylist
114    ***********************************************/
115
116   printf("Determining offsets for %s, please be patient...\n", hmmfile);
117
118   nhmm = npri = nsec = 0;
119   while (HMMFileRead(hmmfp, &hmm)) 
120     {   
121       if (hmm == NULL) 
122         Die("HMM file %s may be corrupt or in incorrect format; parse failed", hmmfile);
123
124                                 /* record name of HMM as the primary retrieval key */
125       status = SSIAddPrimaryKeyToIndex(ssi, hmm->name, fh, &(hmmfp->offset), NULL, 0);
126       if (status != 0) Die("SSIAddPrimaryKeyToIndex() failed");
127       npri++;
128
129                                 /* record accession of HMM as a secondary retrieval key */
130       if (hmm->flags & PLAN7_ACC) {
131         status = SSIAddSecondaryKeyToIndex(ssi, hmm->acc, hmm->name);
132         if (status != 0) Die("SSIAddSecondaryKeyToIndex() failed");
133         nsec++;
134       }
135
136       nhmm++;
137       FreePlan7(hmm);
138     }
139   HMMFileClose(hmmfp);
140
141   /***********************************************
142    * Output the SSI file
143    ***********************************************/
144
145   status = SSIWriteIndex(ssifile, ssi);
146   if (status != 0) Die("SSIWriteIndex() failed");
147
148   printf("Complete.\n");
149   printf("HMM file:       %s\n", hmmfile);
150   printf("SSI index:      %s\n", ssifile);
151   printf("# of HMMS:      %d\n", nhmm);
152   printf("HMM names:      %d\n", npri);
153   printf("HMM accessions: %d\n", nsec);
154
155
156   /***********************************************
157    * Exit
158    ***********************************************/
159
160   free(ssifile);
161   SSIFreeIndex(ssi);
162   SqdClean();
163   return 0;
164 }
165
166