Debian Codegraph @

easel/miniapps/esl-weight.c

raw symbols SCIP
1 /* Assigns sequence weights to an MSA.
2 */
3 #include <esl_config.h>
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8
9 #include "easel.h"
10 #include "esl_getopts.h"
11 #include "esl_msa.h"
12 #include "esl_msafile.h"
13 #include "esl_msaweight.h"
14 #include "esl_random.h"
15
16 #define WGTOPTS "-g,-p,-b,-f"
17
18 static ESL_OPTIONS options[] = {
19 /* name type default env range toggles reqs incomp help docgroup*/
20 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL, NULL, "show brief help on version and usage", 1 },
21 { "-g", eslARG_NONE,"default",NULL, NULL,WGTOPTS,NULL, NULL, "Gerstein/Sonnhammer/Chothia tree weights", 1 },
22 { "-p", eslARG_NONE, FALSE, NULL, NULL,WGTOPTS,NULL, NULL, "Henikoff position-based weights", 1 },
23 { "-b", eslARG_NONE, FALSE, NULL, NULL,WGTOPTS,NULL, NULL, "Henikoff simple filter weights", 1 },
24 { "-f", eslARG_NONE, FALSE, NULL, NULL,WGTOPTS,NULL, NULL, "filter out seqs by fractional identity", 1 },
25 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL,NULL, NULL, "send output to file <f>, not stdout", 1 },
26 { "--id", eslARG_REAL, "0.62", NULL,"0<=x<=1", NULL,"-b", NULL, "for -b: set identity cutoff", 1 },
27 { "--idf", eslARG_REAL, "0.80", NULL,"0<=x<=1", NULL,"-f", NULL, "for -f: set identity cutoff", 1 },
28 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL,NULL, NULL, "specify that input file is in format <s>", 1 },
29 { "--amino", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--dna,--rna", "<msa file> contains protein alignments", 1 },
30 { "--dna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--rna", "<msa file> contains DNA alignments", 1 },
31 { "--rna", eslARG_NONE, FALSE, NULL, NULL, NULL,NULL,"--amino,--dna", "<msa file> contains RNA alignments", 1 },
32 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
33 };
34 static char usage[] = "[-options] <msa file>";
35 static char banner[] = "calculate sequence weights for an alignment";
36
37 static void
38 cmdline_failure(char *argv0, ESL_GETOPTS *go, char *format, ...)
39 {
40 va_list argp;
41
42 va_start(argp, format);
43 vfprintf(stderr, format, argp);
44 va_end(argp);
45 esl_usage(stdout, argv0, usage);
46 puts("\n options:");
47 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
48 /* printf("\nTo see more help on available options, do %s -h\n\n", argv0); */
49 exit(1);
50 }
51
52 static void
53 cmdline_help(char *argv0, ESL_GETOPTS *go)
54 {
55 esl_banner(stdout, argv0, banner);
56 esl_usage (stdout, argv0, usage);
57 puts("\n options:");
58 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
59 exit(0);
60 }
61
62 int
63 main(int argc, char **argv)
64 {
65 ESL_GETOPTS *go = NULL;
66 char *msafile = NULL;
67 int fmt = eslMSAFILE_UNKNOWN;
68 ESL_ALPHABET *abc = NULL;
69 ESL_MSAFILE *afp = NULL;
70 ESL_MSA *msa = NULL;
71 int status;
72 FILE *ofp; /* output stream */
73
74 /* Parse command line */
75 go = esl_getopts_Create(options);
76 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], go, "Failed to parse command line: %s\n", go->errbuf);
77 if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], go, "Error in app configuration: %s\n", go->errbuf);
78 if (esl_opt_GetBoolean(go, "-h") ) cmdline_help (argv[0], go);
79 if (esl_opt_ArgNumber(go) != 1) cmdline_failure(argv[0], go, "Incorrect number of command line arguments.\n");
80 msafile = esl_opt_GetArg(go, 1);
81
82 if (esl_opt_IsOn(go, "--informat")) {
83 fmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--informat"));
84 if (fmt == eslMSAFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --informat", esl_opt_GetString(go, "--informat"));
85 }
86
87 ofp = (esl_opt_GetString (go, "-o") == NULL ? stdout : fopen(esl_opt_GetString(go, "-o"), "w"));
88 if (! ofp) esl_fatal("Failed to open output file %s\n", esl_opt_GetString(go, "-o"));
89
90 if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
91 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
92 else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
93
94 if ((status = esl_msafile_Open(&abc, msafile, NULL, fmt, NULL, &afp)) != eslOK)
95 esl_msafile_OpenFailure(afp, status);
96
97
98 while ((status = esl_msafile_Read(afp, &msa)) != eslEOF)
99 {
100 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
101
102 if (esl_opt_GetBoolean(go, "-f"))
103 {
104 ESL_MSA *fmsa;
105 status = esl_msaweight_IDFilter(msa, esl_opt_GetReal(go, "--idf"), &fmsa);
106 esl_msafile_Write(ofp, fmsa, eslMSAFILE_STOCKHOLM);
107 if (fmsa != NULL) esl_msa_Destroy(fmsa);
108 }
109 else if (esl_opt_GetBoolean(go, "-g"))
110 {
111 status = esl_msaweight_GSC(msa);
112 esl_msafile_Write(ofp, msa, eslMSAFILE_STOCKHOLM);
113 }
114 else if (esl_opt_GetBoolean(go, "-p"))
115 {
116 status = esl_msaweight_PB(msa);
117 esl_msafile_Write(ofp, msa, eslMSAFILE_STOCKHOLM);
118 }
119 else if (esl_opt_GetBoolean(go, "-b"))
120 {
121 status = esl_msaweight_BLOSUM(msa, esl_opt_GetReal(go, "--id"));
122 esl_msafile_Write(ofp, msa, eslMSAFILE_STOCKHOLM);
123 }
124 else esl_fatal("internal error: no weighting algorithm selected");
125 if (status != eslOK) esl_fatal("Failed to calculate weights for msa %s", msa->name);
126
127 esl_msa_Destroy(msa);
128 }
129
130 esl_alphabet_Destroy(abc);
131 esl_msafile_Close(afp);
132 if (ofp != stdout) fclose(ofp);
133 esl_getopts_Destroy(go);
134 exit(0);
135 }
136