-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_hmm.c
executable file
·384 lines (326 loc) · 9.77 KB
/
run_hmm.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include "hmm.h"
#include "util_lib.h"
int main (int argc, char **argv){
int i, j, c, num_seq;
HMM my_hmm;
char *obs_seq = 0;
char *obs_head = 0;
TRAIN train;
int wholegenome=-1;
int format=0;
FILE *fp_out, *fp_aa, *fp_dna, *fp;
char hmm_file[4096] = "";
char aa_file[4096] = "";
char seq_file[4096] = "";
char out_file[4096] = "";
char dna_file[4096] = "";
char train_file[4096] = "";
char mstate_file[4096] = "";
char rstate_file[4096] = "";
char nstate_file[4096] = "";
char sstate_file[4096] = "";
char pstate_file[4096] = "";
char s1state_file[4096] = ""; /* stop codon of gene in - stand */
char p1state_file[4096] = "";
char dstate_file[4096] = "";
char train_dir[4096] = "";
char new_train_dir[4096] = "";
int count=0;
char mystring[1000] = ""; /* input buffer */
int mylongbuffer_len = 5000000;
//char mylongbuffer[5000000] = "";
char * mylongbuffer = 0;
int data_len;
int *obs_seq_len = 0;
int bp_count=0; /* count the length of each line in input file */
/* read command line argument */
if (argc <= 8){
fprintf(stderr, "ERROR: You missed some parameters for input\n");
print_usage();
exit(EXIT_FAILURE);
}
while ((c=getopt(argc, argv, "fs:o:w:t:d:q")) != -1){
switch (c){
case 's':
strcpy(seq_file, optarg);
if (strcmp(seq_file, "-") != 0 ) {
if (access(seq_file, F_OK)==-1){
fprintf(stderr, "ERROR: Sequence file [%s] does not exist\n", seq_file);
print_usage();
exit(EXIT_FAILURE);
}
}
break;
case 'w':
wholegenome = atoi(optarg);
if (wholegenome != 0 && wholegenome != 1){
fprintf(stderr, "ERROR: An incorrect value for the option -w was entered\n");
print_usage();
exit(EXIT_FAILURE);
}
break;
case 'o':
strcpy(out_file, optarg);
break;
case 't':
strcpy(train_file, optarg);
break;
case 'f':
format = 1;
break;
case 'q':
printf("inhibit .fnn for output");
format = 2;
break;
case 'd':
strcpy(new_train_dir, optarg);
break;
}
}
if (strlen(new_train_dir) == 0) {
strncpy(train_dir, argv[0], strlen(argv[0])-12);
strcat(train_dir, "train/");
} else {
strcpy(train_dir, new_train_dir);
strcat(train_dir, "/");
}
printf("train_dir=%s\n", train_dir);
strcpy(hmm_file, train_dir);
strcat(hmm_file, train_file);
strcpy(mstate_file, train_dir);
strcat(mstate_file, "gene");
strcpy(rstate_file, train_dir);
strcat(rstate_file, "rgene");
strcpy(nstate_file, train_dir);
strcat(nstate_file, "noncoding");
strcpy(sstate_file, train_dir);
strcat(sstate_file, "start");
strcpy(pstate_file, train_dir);
strcat(pstate_file, "stop");
strcpy(s1state_file, train_dir);
strcat(s1state_file, "stop1");
strcpy(p1state_file, train_dir);
strcat(p1state_file, "start1");
strcpy(dstate_file, train_dir);
strcat(dstate_file, "pwm");
if (access(hmm_file, F_OK)==-1){
fprintf(stderr, "ERROR: The file for model parameters [%s] does not exist\n", hmm_file);
print_usage();
exit(EXIT_FAILURE);
}
/* check whether the specified files exist */
if (access(mstate_file, F_OK)==-1){
fprintf(stderr, "Forward prob. file [%s] does not exist\n", mstate_file);
exit(1);
}
if (access(rstate_file, F_OK)==-1){
fprintf(stderr, "Backward prob. file [%s] does not exist\n", rstate_file);
exit(1);
}
if (access(nstate_file, F_OK)==-1){
fprintf(stderr, "noncoding prob. file [%s] does not exist\n", nstate_file);
exit(1);
}
if (access(sstate_file, F_OK)==-1){
fprintf(stderr, "start prob. file [%s] does not exist\n", sstate_file);
exit(1);
}
if (access(pstate_file, F_OK)==-1){
fprintf(stderr, "stop prob. file [%s] does not exist\n", pstate_file);
exit(1);
}
if (access(s1state_file, F_OK)==-1){
fprintf(stderr, "start1 prob. file [%s] does not exist\n", s1state_file);
exit(1);
}
if (access(p1state_file, F_OK)==-1){
fprintf(stderr, "stop1 prob. file [%s] does not exist\n", p1state_file);
exit(1);
}
if (access(dstate_file, F_OK)==-1){
fprintf(stderr, "pwm dist. file [%s] does not exist\n", dstate_file);
exit(1);
}
if (access(hmm_file, F_OK)==-1){
fprintf(stderr, "hmm file [%s] does not exist\n", hmm_file);
exit(1);
}
/* read all initial model */
my_hmm.N=NUM_STATE;
get_train_from_file(hmm_file, &my_hmm, mstate_file, rstate_file, nstate_file, sstate_file, pstate_file,s1state_file, p1state_file, dstate_file, &train);
/* create output file name */
strcpy(aa_file, out_file);
strcat(aa_file, ".faa");
strcpy(dna_file, out_file);
strcat(dna_file, ".ffn");
remove (out_file);
remove (aa_file);
remove (dna_file);
fp_aa = fopen (aa_file , "w");
if(fp_aa==0 ){fprintf(stderr, "error opening aa file %s\n", aa_file); exit(EXIT_FAILURE);}
fp_out = fopen (out_file , "w");
if(fp_out ==0) {fprintf(stderr, "error opening out file %s\n", out_file); exit(EXIT_FAILURE);}
fp_dna = fopen (dna_file , "w");
if(fp_dna ==0) {fprintf(stderr, "error opening dna file %s\n", dna_file); exit(EXIT_FAILURE);}
if (strcmp(seq_file, "-") == 0 ) {
fp = stdin;
mylongbuffer = (char *)malloc(mylongbuffer_len);
obs_seq = (char *)malloc(mylongbuffer_len);
obs_seq[0] = '\0';
while ( fgets (mylongbuffer , mylongbuffer_len , fp) ){
//need to defined obs_seq and obs_head
data_len = strlen(mylongbuffer);
if ( data_len >= mylongbuffer_len -1) {
fprintf(stderr, "error: buffer full, use stdin only for short sequences \n");
exit(EXIT_FAILURE);
}
//fprintf(stdout, "reading: %s\n", mylongbuffer);
while (mylongbuffer[data_len-1] == 10 || mylongbuffer[data_len-1] == 13 ) {
mylongbuffer[data_len-1] = '\0';
data_len--;
if (data_len == '\0') {
fprintf(stderr, "error: header or sequence too short\n");
exit(EXIT_FAILURE);
}
}
if (mylongbuffer[0] == '>'){
//count++;
if (obs_seq[0] != '\0') {
// process previous sequence
//fprintf(stdout, "give: \"%s\"\n", obs_head);
//fprintf(stdout, "give: \"%s\"\n", obs_seq);
get_prob_from_cg(&my_hmm, &train, obs_seq);
if (strlen(obs_seq)>70){
viterbi(&my_hmm, obs_seq, fp_out, fp_aa, fp_dna, obs_head, wholegenome, format);
}
free(obs_head);
obs_head = 0;
obs_seq[0] = '\0';
}
obs_head = (char *)malloc(data_len+1);
memcpy(obs_head, mylongbuffer, data_len);
obs_head[data_len] = '\0';
} else {
strcat(obs_seq, mylongbuffer);
}
}
// last sequence
if (obs_seq != 0) {
get_prob_from_cg(&my_hmm, &train, obs_seq);
if (strlen(obs_seq)>70){
viterbi(&my_hmm, obs_seq, fp_out, fp_aa, fp_dna, obs_head, wholegenome, format);
}
free(obs_head);
obs_head = 0;
free(obs_seq);
obs_seq = 0;
}
} else {
/* Parse input, count records, store in int num_seq */
fp = fopen (seq_file, "r");
while ( fgets (mystring , sizeof mystring , fp) ){
if (mystring[0] == '>'){
count++;
}
}
num_seq = count;
obs_seq_len = (int *)malloc(num_seq * sizeof(int));
if (!obs_seq_len) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure for obs_seq_len");
exit(EXIT_FAILURE);
}
printf("no. of seqs: %d\n", num_seq);
/* Parse input, count record length, store in obs_seq_len */
i = 0;
count = 0;
rewind(fp);
while ( fgets (mystring , sizeof mystring , fp) ){
if (mystring[0] == '>'){
if (i>0){
obs_seq_len[count] = i;
count++;
}
i = 0;
}else{
bp_count = strlen(mystring)-1;
while(mystring[bp_count-1] == 10 || mystring[bp_count-1]==13){
bp_count --;
}
i += bp_count;
}
}
obs_seq_len[count] = i;
/* Parse input, read data, store in obs_head and obs_seq fields and call viterbi */
count=-1;
rewind(fp);
j = 0;
obs_head = (char *)malloc(sizeof(mystring));
if (!obs_head) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure for obs_head");
exit(EXIT_FAILURE);
}
memset(obs_head, 0, (bp_count+1) * sizeof(char));
obs_seq = (char *)malloc(obs_seq_len[0] * sizeof(char) + 1);
if (!obs_seq) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure for obs_seq");
exit(EXIT_FAILURE);
}
while ( fgets (mystring , sizeof mystring , fp) ){
if (mystring[0] == '>'){
if(strlen(mystring) == (sizeof mystring) -1) {
fprintf(stderr, "ERROR: FASTA header too long! %d bytes\n", (int)strlen(mystring));
exit(EXIT_FAILURE);
}
if (count>=0 && j>0){ // process previous sequence
get_prob_from_cg(&my_hmm, &train, obs_seq);
if (strlen(obs_seq)>70){
viterbi(&my_hmm, obs_seq, fp_out, fp_aa, fp_dna, obs_head, wholegenome, format);
}
}
bp_count = strlen(mystring)-1; // handle fasta header
while(mystring[bp_count-1] == 10 || mystring[bp_count-1]==13){
bp_count --;
}
memset(obs_head, 0, (bp_count+1) * sizeof(char));
memcpy(obs_head, mystring, bp_count);
if (count == -1 || (count>=0 && j>0) ){ // allocate and initially populate obs_seq
count++;
obs_seq = (char *)malloc(obs_seq_len[count] * sizeof(char) + 1);
if (!obs_seq) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure for obs_seq");
exit(EXIT_FAILURE);
}
memset(obs_seq, 0, obs_seq_len[count] * sizeof(char) + 1);
}
j = 0;
}else{ // append to obs_seq
bp_count = strlen(mystring)-1;
while(mystring[bp_count-1] == 10 || mystring[bp_count-1]==13){
bp_count --;
}
memcpy(obs_seq+j, mystring, bp_count);
j += bp_count;
}
}
if (count>=0){ // take care of final sequence
get_prob_from_cg(&my_hmm, &train, obs_seq);
if (strlen(obs_seq)>70){
viterbi(&my_hmm, obs_seq, fp_out, fp_aa, fp_dna, obs_head, wholegenome, format);
}
}
}
free(obs_seq_len);
free(obs_head);
free(obs_seq);
fclose(fp_out);
fclose(fp_aa);
fclose(fp_dna);
fclose(fp);
return(0);
}