1 /* hmmconvert: converting profile HMM files to HMMER3 HMM format.
2  *
3  * SRE, Thu Oct 16 08:57:43 2008 [janelia] [Enigma MCMXC a.D.]
4  */
5 #include "p7_config.h"
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "easel.h"
12 #include "esl_alphabet.h"
13 #include "esl_getopts.h"
14 
15 #include "hmmer.h"
16 
17 static ESL_OPTIONS options[] = {
18   /* name           type      default  env  range     toggles      reqs   incomp  help   docgroup*/
19   { "-h",        eslARG_NONE,   FALSE, NULL, NULL,      NULL,       NULL,    NULL, "show brief help on version and usage",                             0 },
20   { "-a",        eslARG_NONE,"default",NULL, NULL, "-a,-b,-2",      NULL,    NULL, "ascii:  output models in HMMER3 ASCII format",                     0 },
21   { "-b",        eslARG_NONE,   FALSE, NULL, NULL, "-a,-b,-2",      NULL,    NULL, "binary: output models in HMMER3 binary format",                    0 },
22   { "-2",        eslARG_NONE,   FALSE, NULL, NULL, "-a,-b,-2",      NULL,    NULL, "HMMER2: output backward compatible HMMER2 ASCII format (ls mode)", 0 },
23   { "--outfmt",  eslARG_STRING, NULL,  NULL, NULL,      NULL,       NULL,    "-2", "choose output legacy 3.x file formats by name, such as '3/a'",     0 },
24   {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
25 };
26 static char usage[]  = "[-options] <hmmfile>";
27 static char banner[] = "convert profile file to a HMMER format";
28 
29 
30 int
main(int argc,char ** argv)31 main(int argc, char **argv)
32 {
33   ESL_GETOPTS   *go      = p7_CreateDefaultApp(options, 1, argc, argv, banner, usage);
34   ESL_ALPHABET  *abc     = NULL;
35   char          *hmmfile = esl_opt_GetArg(go, 1);
36   P7_HMMFILE    *hfp     = NULL;
37   P7_HMM        *hmm     = NULL;
38   FILE          *ofp     = stdout;
39   char          *outfmt  = esl_opt_GetString(go, "--outfmt");
40   int            fmtcode = -1;	/* -1 = write the current default format */
41   int            status;
42   char           errbuf[eslERRBUFSIZE];
43 
44   if (outfmt != NULL) {
45     if      (strcmp(outfmt, "3/a") == 0) fmtcode = p7_HMMFILE_3a;
46     else if (strcmp(outfmt, "3/b") == 0) fmtcode = p7_HMMFILE_3b;
47     else if (strcmp(outfmt, "3/c") == 0) fmtcode = p7_HMMFILE_3c;
48     else if (strcmp(outfmt, "3/d") == 0) fmtcode = p7_HMMFILE_3d;
49     else if (strcmp(outfmt, "3/e") == 0) fmtcode = p7_HMMFILE_3e;
50     else if (strcmp(outfmt, "3/f") == 0) fmtcode = p7_HMMFILE_3f;
51     else    p7_Fail("No such 3.x output format code %s.\n", outfmt);
52   }
53 
54   status = p7_hmmfile_OpenE(hmmfile, NULL, &hfp, errbuf);
55   if      (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
56   else if (status == eslEFORMAT)   p7_Fail("File format problem in trying to open HMM file %s.\n%s\n",                hmmfile, errbuf);
57   else if (status != eslOK)        p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n",                       status, hmmfile, errbuf);
58 
59   while ((status = p7_hmmfile_Read(hfp, &abc, &hmm)) == eslOK)
60     {
61       if      (esl_opt_GetBoolean(go, "-a") == TRUE) p7_hmmfile_WriteASCII (ofp, fmtcode, hmm);
62       else if (esl_opt_GetBoolean(go, "-b") == TRUE) p7_hmmfile_WriteBinary(ofp, fmtcode, hmm);
63       else if (esl_opt_GetBoolean(go, "-2") == TRUE) p7_h2io_WriteASCII    (ofp, hmm);
64 
65       p7_hmm_Destroy(hmm);
66     }
67   if      (status == eslEFORMAT)   p7_Fail("bad file format in HMM file %s",             hmmfile);
68   else if (status == eslEINCOMPAT) p7_Fail("HMM file %s contains different alphabets",   hmmfile);
69   else if (status != eslEOF)       p7_Fail("Unexpected error in reading HMMs from %s",   hmmfile);
70 
71   p7_hmmfile_Close(hfp);
72   esl_alphabet_Destroy(abc);
73   esl_getopts_Destroy(go);
74   return 0;
75 }
76 
77 
78