-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmme.c
257 lines (214 loc) · 7.39 KB
/
hmme.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/************************************************************
* HMMER - Biological sequence analysis with HMMs
* Copyright 1992-1995 Sean R. Eddy
*
* This source code is distributed under the terms of the
* GNU General Public License. See the files COPYING and
* GNULICENSE for details.
*
************************************************************/
/* hmme.c
* Emit sequences from a model.
*
* Given an hmm, emit a number of sequences.
*
* Tue Nov 24 10:16:29 1992
* radical alterations Wed Jul 28 12:12:57 1993: hmme
* used to be "hmm evaluation" and did some pretty
* useless stuff.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#ifdef NEED_GETOPTH
#include <getopt.h>
#endif
#include "squid.h"
#include "states.h"
#include "externs.h"
#include "version.h"
#ifdef MEMDEBUG
#include "dbmalloc.h"
#endif
char Alphabet[MAXABET]; /* ACGT, for instance */
int Alphabet_size; /* 4 or 20 */
int Alphabet_type; /* kDNA, kRNA, or kAmino */
#define OPTIONS "bfhn:o:qr:s:B"
static char usage[] = "\
Usage: hmme [-options] <hmmfile>\n\
where available options are:\n\
-b : emit single best (most probable) sequence\n\
-f : write in FASTA format, not SELEX\n\
-h : print short help and version info\n\
-n <number> : emit <number> sequences\n\
-o <outfile> : save sequences to outfile\n\
-q : quiet - suppress verbose banner\n\
-r <rfile> : read random model from <rfile>\n\
-s <seed> : set seed for random()\n\
\n\
Experimental options:\n\
-B : like -b, but emit highest scoring sequence\n";
static char banner[] = "hmme -- sequence emission from a hidden Markov model";
int
main(int argc, char **argv)
{
char *hmmfile; /* HMM file to open */
FILE *hmmfp; /* opened hmm file pointer */
struct hmm_struc *hmm; /* the hidden Markov model */
int idx; /* counter over sequences */
char **rseqs; /* emitted sequences */
SQINFO *sqinfo; /* array of info for rseqs */
char **aseqs; /* alignment of emitted sequences */
AINFO ainfo; /* info for aseqs */
struct trace_s **tr; /* tracebacks from emitted seqs */
float score; /* score of emitted sequence */
float randomseq[MAXABET]; /* random sequence model, freqs */
char *randomfile;
int optc;
extern int optind;
extern char *optarg;
int emitnum; /* option: number of sequences to emit */
int seed;
char *outfile;
FILE *ofp;
int do_best;
int do_rd_best;
int be_quiet;
int do_fasta;
/***********************************************
* Parse the command line
***********************************************/
emitnum = 10;
seed = (int) time ((time_t *) NULL); /* default: "random" seed */
outfile = NULL;
do_best = FALSE;
do_rd_best = FALSE;
be_quiet = FALSE;
do_fasta = FALSE;
randomfile = NULL;
while ((optc = getopt(argc, argv, OPTIONS)) != -1)
switch (optc) {
case 'b': do_best = TRUE; break;
case 'f': do_fasta = TRUE; break;
case 'n': emitnum = atoi(optarg); break;
case 'o': outfile = optarg; break;
case 'q': be_quiet = TRUE; break;
case 'r': randomfile = optarg; break;
case 's': seed = atoi(optarg); break;
case 'B': do_rd_best = TRUE; break;
case 'h':
printf("%s\n version %s, %s\n\n%s\n", banner, RELEASE, RELEASEDATE, usage);
exit(0);
default:
fprintf(stderr, "Unrecognized option: -%c\n%s\n", optc, usage);
exit(1);
}
if (argc - optind != 1)
{ fprintf(stderr, "%s\n", usage); exit(1); }
hmmfile = argv[optind];
sre_srandom(seed);
if (outfile == NULL) ofp = stdout;
else if ((ofp = fopen(outfile, "w")) == NULL)
Die ("Failed to open output file %s", outfile);
/***********************************************
* Input of HMM (and sets alphabet)
***********************************************/
if ((hmmfp = fopen(hmmfile, "rb")) == NULL)
Die("failed to open HMM file %s for reading.", hmmfile);
if ((hmm = ReadHMM(hmmfp)) == NULL)
Die("failed to parse HMM file %s", hmmfile);
if (fclose(hmmfp) == EOF)
Die("file close failed!?");
if (randomfile == NULL) DefaultRandomModel(randomseq);
else ReadRandomModel(randomfile, randomseq);
/***********************************************
* Allocations
***********************************************/
if (do_best || do_rd_best) emitnum = 1;
if ((rseqs = (char **) malloc (emitnum * sizeof(char *))) == NULL ||
(sqinfo = (SQINFO *) malloc (emitnum * sizeof(SQINFO))) == NULL ||
(tr = (struct trace_s **) malloc (emitnum * sizeof(struct trace_s *))) == NULL)
Die("malloc failed");
/***********************************************
* Emit and align the sequences, then print 'em
***********************************************/
if (! be_quiet)
{
printf("%s\n", banner);
printf(" version %s, %s\n", RELEASE, RELEASEDATE);
puts("");
}
/* There are two versions of the best sequence code.
* Mine produces the best *state* sequence, then emits the most likely
* symbol sequence from it. Richard Durbin's version emits the
* most likely sequence relative to a random model.
*/
if (do_best || do_rd_best)
{
struct shmm_s *shmm;
if (do_rd_best)
RD_EmitBestSequence(hmm, randomseq, &(rseqs[0]), &(tr[0]));
else
EmitBestSequence(hmm, &(rseqs[0]), &(tr[0]));
shmm = AllocSearchHMM(hmm->M);
MakeSearchHMM(hmm, randomseq, shmm);
TraceScore(shmm, rseqs[0], tr[0], &score);
if (do_rd_best)
{
strcpy(sqinfo[0].name, "best-score");
sprintf(sqinfo[0].desc, "Best scoring sequence (score %.2f bits)", score);
sqinfo[0].flags = SQINFO_NAME | SQINFO_DESC;
}
else
{
strcpy(sqinfo[0].name, "most-likely");
sprintf(sqinfo[0].desc, "Most probable sequence (score %.2f bits)", score);
sqinfo[0].flags = SQINFO_NAME | SQINFO_DESC;
}
WriteSeq(ofp, kPearson, rseqs[0], &(sqinfo[0]));
FreeSequence(rseqs[0], &(sqinfo[0]));
FreeTrace(tr[0]);
FreeSearchHMM(shmm);
}
else
{
for (idx = 0; idx < emitnum; idx++)
if (! EmitSequence(hmm, &rseqs[idx], &tr[idx]))
Die("failed to emit sequence number %d", idx);
/* give them names */
for (idx = 0; idx < emitnum; idx++)
{
sqinfo[idx].flags = SQINFO_NAME;
sprintf(sqinfo[idx].name, "seq%d", idx);
}
if (do_fasta)
{
for (idx = 0; idx < emitnum; idx++)
WriteSeq(ofp, kPearson, rseqs[idx], &(sqinfo[idx]));
}
else /* SELEX alignment */
{
if (! Traces2Alignment(rseqs, sqinfo, emitnum, hmm->M, tr, FALSE, &aseqs, &ainfo))
Die("Traces2Alignment failed");
if (! WriteSELEX(ofp, aseqs, emitnum, &ainfo, 50))
Die("WriteSELEX failed");
FreeAlignment(aseqs, emitnum, &ainfo);
}
for (idx = 0; idx < emitnum; idx++)
{
FreeSequence(rseqs[idx], &(sqinfo[idx]));
FreeTrace(tr[idx]);
}
}
/***********************************************
* Exit
***********************************************/
if (outfile != NULL) fclose(ofp);
free(tr);
free(rseqs);
free(sqinfo);
FreeHMM(hmm);
return 0;
}