Debian Codegraph @

hmmer/src/hmmalign.c

raw symbols SCIP
1 /* hmmalign: align sequences to a profile HMM
2 */
3 #include <p7_config.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "easel.h"
10 #include "esl_alphabet.h"
11 #include "esl_getopts.h"
12 #include "esl_msa.h"
13 #include "esl_msafile.h"
14 #include "esl_sq.h"
15 #include "esl_sqio.h"
16 #include "esl_vectorops.h"
17
18 #include "hmmer.h"
19
20 static int map_alignment(const char *msafile, const P7_HMM *hmm, ESL_SQ ***ret_sq, P7_TRACE ***ret_tr, int *ret_ntot);
21
22
23 #define ALPHOPTS "--amino,--dna,--rna" /* Exclusive options for alphabet choice */
24
25 static ESL_OPTIONS options[] = {
26 /* name type default env range toggles reqs incomp help docgroup*/
27 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 1 },
28 { "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output alignment to file <f>, not stdout", 1 },
29
30 { "--mapali", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, NULL, "include alignment in file <f> (same ali that HMM came from)", 2 },
31 { "--trim", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "trim terminal tails of nonaligned residues from alignment", 2 },
32 { "--amino", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "assert <seqfile>, <hmmfile> both protein: no autodetection", 2 },
33 { "--dna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "assert <seqfile>, <hmmfile> both DNA: no autodetection", 2 },
34 { "--rna", eslARG_NONE, FALSE, NULL, NULL, ALPHOPTS, NULL, NULL, "assert <seqfile>, <hmmfile> both RNA: no autodetection", 2 },
35 { "--informat", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "assert <seqfile> is in format <s>: no autodetection", 2 },
36 { "--outformat", eslARG_STRING, "Stockholm", NULL, NULL, NULL, NULL, NULL, "output alignment in format <s>", 2 },
37 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
38 };
39
40 static char usage[] = "[-options] <hmmfile> <seqfile>";
41 static char banner[] = "align sequences to a profile HMM";
42
43
44
45
46 static void
47 cmdline_failure(char *argv0, char *format, ...)
48 {
49 va_list argp;
50 printf("\nERROR: ");
51 va_start(argp, format);
52 vfprintf(stdout, format, argp);
53 va_end(argp);
54 esl_usage(stdout, argv0, usage);
55 printf("\nTo see more help on available options, do %s -h\n\n", argv0);
56 exit(1);
57 }
58
59 static void
60 cmdline_help(char *argv0, ESL_GETOPTS *go)
61 {
62 p7_banner (stdout, argv0, banner);
63 esl_usage (stdout, argv0, usage);
64 puts("\nBasic options:");
65 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
66 puts("\nLess common options:");
67 esl_opt_DisplayHelp(stdout, go, 2, 2, 80);
68 puts("\nSequence input formats include: FASTA, EMBL, GenBank, UniProt");
69 puts("Alignment output formats include: Stockholm, Pfam, A2M, PSIBLAST\n");
70 exit(0);
71 }
72
73
74
75 int
76 main(int argc, char **argv)
77 {
78 ESL_GETOPTS *go = NULL; /* application configuration */
79 char *hmmfile = NULL; /* HMM file name */
80 char *seqfile = NULL; /* sequence file name */
81 char *mapfile = NULL; /* optional mapped MSA file name */
82 int infmt = eslSQFILE_UNKNOWN;
83 int outfmt = eslMSAFILE_STOCKHOLM;
84 P7_HMMFILE *hfp = NULL; /* open HMM file */
85 ESL_SQFILE *sqfp = NULL; /* open sequence file */
86 char *outfile = NULL; /* output filename */
87 FILE *ofp = stdout; /* output stream */
88 ESL_SQ **sq = NULL; /* array of sequences */
89 void *p = NULL; /* tmp ptr for reallocation */
90 int nseq = 0; /* # of sequences in <seqfile> */
91 int mapseq = 0; /* # of sequences in mapped MSA */
92 int totseq = 0; /* # of seqs in all sources */
93 ESL_ALPHABET *abc = NULL; /* alphabet (set from the HMM file)*/
94 P7_HMM *hmm = NULL;
95 P7_TRACE **tr = NULL; /* array of tracebacks */
96 ESL_MSA *msa = NULL; /* resulting multiple alignment */
97 int msaopts = 0; /* flags to p7_tracealign_Seqs() */
98 int idx; /* counter over seqs, traces */
99 int status; /* easel/hmmer return code */
100 char errbuf[eslERRBUFSIZE];
101
102 /* Parse the command line
103 */
104 go = esl_getopts_Create(options);
105 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf);
106 if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], "Error in configuration: %s\n", go->errbuf);
107 if (esl_opt_GetBoolean(go, "-h") ) cmdline_help (argv[0], go);
108 if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
109
110 hmmfile = esl_opt_GetArg(go, 1);
111 seqfile = esl_opt_GetArg(go, 2);
112
113 if (strcmp(hmmfile, "-") == 0 && strcmp(seqfile, "-") == 0)
114 cmdline_failure(argv[0], "Either <hmmfile> or <seqfile> may be '-' (to read from stdin), but not both.\n");
115
116 msaopts |= p7_ALL_CONSENSUS_COLS; /* default as of 3.1 */
117 if (esl_opt_GetBoolean(go, "--trim")) msaopts |= p7_TRIM;
118
119 /* If caller declared an input format, decode it
120 */
121 if (esl_opt_IsOn(go, "--informat")) {
122 infmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat"));
123 if (infmt == eslSQFILE_UNKNOWN) cmdline_failure(argv[0], "%s is not a recognized input sequence file format\n", esl_opt_GetString(go, "--informat"));
124 }
125
126 /* Determine output alignment file format */
127 outfmt = esl_msafile_EncodeFormat(esl_opt_GetString(go, "--outformat"));
128 if (outfmt == eslMSAFILE_UNKNOWN) cmdline_failure(argv[0], "%s is not a recognized output MSA file format\n", esl_opt_GetString(go, "--outformat"));
129
130 /* Open output stream */
131 if ( (outfile = esl_opt_GetString(go, "-o")) != NULL)
132 {
133 if ((ofp = fopen(outfile, "w")) == NULL)
134 cmdline_failure(argv[0], "failed to open -o output file %s for writing\n", outfile);
135 }
136
137
138 /* If caller forced an alphabet on us, create the one the caller wants
139 */
140 if (esl_opt_GetBoolean(go, "--amino")) abc = esl_alphabet_Create(eslAMINO);
141 else if (esl_opt_GetBoolean(go, "--dna")) abc = esl_alphabet_Create(eslDNA);
142 else if (esl_opt_GetBoolean(go, "--rna")) abc = esl_alphabet_Create(eslRNA);
143
144 /* Read one HMM, and make sure there's only one.
145 */
146 status = p7_hmmfile_Open(hmmfile, NULL, &hfp, errbuf);
147 if (status == eslENOTFOUND) p7_Fail("File existence/permissions problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
148 else if (status == eslEFORMAT) p7_Fail("File format problem in trying to open HMM file %s.\n%s\n", hmmfile, errbuf);
149 else if (status != eslOK) p7_Fail("Unexpected error %d in opening HMM file %s.\n%s\n", status, hmmfile, errbuf);
150
151 status = p7_hmmfile_Read(hfp, &abc, &hmm);
152 if (status == eslEFORMAT) p7_Fail("Bad file format in HMM file %s:\n%s\n", hfp->fname, hfp->errbuf);
153 else if (status == eslEINCOMPAT) p7_Fail("HMM in %s is not in the expected %s alphabet\n", hfp->fname, esl_abc_DecodeType(abc->type));
154 else if (status == eslEOF) p7_Fail("Empty HMM file %s? No HMM data found.\n", hfp->fname);
155 else if (status != eslOK) p7_Fail("Unexpected error in reading HMMs from %s\n", hfp->fname);
156
157 status = p7_hmmfile_Read(hfp, &abc, NULL);
158 if (status != eslEOF) p7_Fail("HMM file %s does not contain just one HMM\n", hfp->fname);
159 p7_hmmfile_Close(hfp);
160
161
162 /* We're going to build up two arrays: sequences and traces.
163 * If --mapali option is chosen, the first set of sequences/traces is from the provided alignment
164 */
165 if ( (mapfile = esl_opt_GetString(go, "--mapali")) != NULL)
166 {
167 map_alignment(mapfile, hmm, &sq, &tr, &mapseq);
168 }
169 totseq = mapseq;
170
171 /* Read digital sequences into an array (possibly concat'ed onto mapped seqs)
172 */
173 status = esl_sqfile_OpenDigital(abc, seqfile, infmt, NULL, &sqfp);
174 if (status == eslENOTFOUND) p7_Fail("Failed to open sequence file %s for reading\n", seqfile);
175 else if (status == eslEFORMAT) p7_Fail("Sequence file %s is empty or misformatted\n", seqfile);
176 else if (status != eslOK) p7_Fail("Unexpected error %d opening sequence file %s\n", status, seqfile);
177
178 ESL_RALLOC(sq, p, sizeof(ESL_SQ *) * (totseq + 1));
179 sq[totseq] = esl_sq_CreateDigital(abc);
180 nseq = 0;
181 while ((status = esl_sqio_Read(sqfp, sq[totseq+nseq])) == eslOK)
182 {
183 nseq++;
184 ESL_RALLOC(sq, p, sizeof(ESL_SQ *) * (totseq+nseq+1));
185 sq[totseq+nseq] = esl_sq_CreateDigital(abc);
186 }
187 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s):\n%s\n",
188 sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
189 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s", status, sqfp->filename);
190 esl_sqfile_Close(sqfp);
191 totseq += nseq;
192
193
194 /* Remaining initializations, including trace array allocation
195 */
196 ESL_RALLOC(tr, p, sizeof(P7_TRACE *) * totseq);
197 for (idx = mapseq; idx < totseq; idx++)
198 tr[idx] = p7_trace_CreateWithPP();
199
200 p7_tracealign_computeTraces(hmm, sq, mapseq, totseq - mapseq, tr);
201
202 p7_tracealign_Seqs(sq, tr, totseq, hmm->M, msaopts, hmm, &msa);
203
204 esl_msafile_Write(ofp, msa, outfmt);
205
206 for (idx = 0; idx <= totseq; idx++) esl_sq_Destroy(sq[idx]); /* including sq[nseq] because we overallocated */
207 for (idx = 0; idx < totseq; idx++) p7_trace_Destroy(tr[idx]);
208 free(sq);
209 free(tr);
210 esl_msa_Destroy(msa);
211 p7_hmm_Destroy(hmm);
212 if (ofp != stdout) fclose(ofp);
213 esl_alphabet_Destroy(abc);
214 esl_getopts_Destroy(go);
215 return eslOK;
216
217 ERROR:
218 return status;
219 }
220
221
222
223 /*****************************************************************
224 * Internal functions used by main and API
225 *****************************************************************/
226
227 static int
228 map_alignment(const char *msafile, const P7_HMM *hmm, ESL_SQ ***ret_sq, P7_TRACE ***ret_tr, int *ret_ntot)
229 {
230 ESL_SQ **sq = NULL;
231 P7_TRACE **tr = NULL;
232 ESL_MSAFILE *afp = NULL;
233 ESL_MSA *msa = NULL;
234 ESL_ALPHABET *abc = (ESL_ALPHABET *) hmm->abc; /* removing const'ness to make compiler happy. Safe. */
235 int *matassign = NULL;
236 uint32_t chksum = 0;
237 int i,k;
238 int status;
239
240 status = esl_msafile_Open(&abc, msafile, NULL, eslMSAFILE_UNKNOWN, NULL, &afp);
241 if (status != eslOK) esl_msafile_OpenFailure(afp, status);
242
243 status = esl_msafile_Read(afp, &msa);
244 if (status != eslOK) esl_msafile_ReadFailure(afp, status);
245
246 if (! (hmm->flags & p7H_CHKSUM) ) esl_fatal("HMM has no checksum. --mapali unreliable without it.");
247 if (! (hmm->flags & p7H_MAP) ) esl_fatal("HMM has no map. --mapali can't work without it.");
248 esl_msa_Checksum(msa, &chksum);
249 if (hmm->checksum != chksum) esl_fatal("--mapali MSA %s isn't same as the one HMM came from (checksum mismatch)", msafile);
250
251 ESL_ALLOC(sq, sizeof(ESL_SQ *) * msa->nseq);
252 ESL_ALLOC(tr, sizeof(P7_TRACE *) * msa->nseq);
253 ESL_ALLOC(matassign, sizeof(int) * (msa->alen + 1));
254
255 esl_vec_ISet(matassign, msa->alen+1, 0);
256 for (k = 1; k <= hmm->M; k++) matassign[hmm->map[k]] = 1;
257
258 p7_trace_FauxFromMSA(msa, matassign, p7_DEFAULT, tr);
259
260 /* The 'faux' core traces constructed by FauxFromMSA() may contain
261 * D->I and I->D transitions. They may *only* now be passed to
262 * p7_tracealign_Seqs(), which can deal with these 'illegal'
263 * transitions, in order to exactly reproduce the input --mapali
264 * alignment.
265 */
266
267 for (i = 0; i < msa->nseq; i++)
268 esl_sq_FetchFromMSA(msa, i, &(sq[i]));
269
270 *ret_ntot = msa->nseq;
271 *ret_tr = tr;
272 *ret_sq = sq;
273
274 esl_msafile_Close(afp);
275 esl_msa_Destroy(msa);
276 free(matassign);
277 return eslOK;
278
279 ERROR:
280 *ret_ntot = 0;
281 *ret_tr = NULL;
282 *ret_sq = NULL;
283 if (afp != NULL) esl_msafile_Close(afp);
284 if (msa != NULL) esl_msa_Destroy(msa);
285 if (matassign != NULL) free(matassign);
286 return status;
287 }
288