forked from wltrimbl/FGS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_lib.c
executable file
·362 lines (274 loc) · 9.03 KB
/
util_lib.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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#if !defined(__APPLE__)
#include <malloc.h>
#endif
#include <string.h>
double log2(double a){
return log(a)/log(2);
}
double **dmatrix(int num_row, int num_col){
int i, j;
double **m;
m=(double **) malloc(num_row * sizeof(double*));
if (!m) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure for points to rows in dmatrix()");
exit(EXIT_FAILURE);
}
for(i=0; i<num_row; i++) {
m[i]=(double *) malloc(num_col * sizeof(double));
if (!m[i]) {
fprintf(stderr, "%s %d %s\n", "ERROR: Allocation failure for the row ", i, " in dmatrix()");
exit(EXIT_FAILURE);
}
for(j=0; j<num_col; j++){
m[i][j] = 0.0;
}
}
return m;
}
int **imatrix(int num_row, int num_col){
int i,j;
int **m;
m=(int **) malloc(num_row * sizeof(int*));
if (!m) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure for points to rows in imatrix()");
exit(EXIT_FAILURE);
}
for(i=0; i<num_row; i++) {
m[i]=(int *) malloc(num_col * sizeof(int));
if (!m[i]) {
fprintf(stderr, "%s %d %s\n", "ERROR: Allocation failure for the row ", i ," in imatrix()");
exit(EXIT_FAILURE);
}
for(j=0; j<num_col; j++){
m[i][j] = 0;
}
}
return m;
}
double *dvector(int nh){
int j;
double *v;
v=(double *)malloc(nh * sizeof(double));
if (!v) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure in dvector()");
exit(EXIT_FAILURE);
}
for(j=0; j<nh; j++){
v[j] = 0.0;
}
return v;
}
int *ivector(int nh){
int j;
int *v;
v=(int *)malloc(nh * sizeof(int));
if (!v) {
fprintf(stderr, "%s\n", "ERROR: Allocation failure in ivector()");
exit(EXIT_FAILURE);
}
for(j=0; j<nh; j++){
v[j] = 0;
}
return v;
}
void free_dvector(double *v){
free(v);
}
void free_ivector(int *v){
free(v);
}
void free_dmatrix(double **m, int num_row){
int i;
for(i=num_row-1; i>=0; i--)
free(m[i]);
free(m);
}
void free_imatrix(int **m,int num_row){
int i;
for(i=num_row-1; i>=0; i--)
free(m[i]);
free(m);
}
int tr2int (char *tr){
int result=0;
if (strcmp(tr, "MM")==0){ result = 0; }
else if (strcmp(tr, "MI")==0){ result = 1; }
else if (strcmp(tr, "MD")==0){ result = 2; }
else if (strcmp(tr, "II")==0){ result = 3; }
else if (strcmp(tr, "IM")==0){ result = 4; }
else if (strcmp(tr, "DD")==0){ result = 5; }
else if (strcmp(tr, "DM")==0){ result = 6; }
else if (strcmp(tr, "GE")==0){ result = 7; }
else if (strcmp(tr, "GG")==0){ result = 8; }
else if (strcmp(tr, "ER")==0){ result = 9; }
else if (strcmp(tr, "RS")==0){ result = 10;}
else if (strcmp(tr, "RR")==0){ result = 11;}
else if (strcmp(tr, "ES")==0){ result = 12;} /* ES: E+ -> S+, E- -> S- */
else if (strcmp(tr, "ES1")==0){ result = 13;} /* ES1: E+ -> S-, E- -> S+ */
else { fprintf(stderr, "%s\n", "ERROR: corrupt training data");
exit(EXIT_FAILURE);}
return result;
}
int nt2int (char nt){
int result;
if (nt == 'A' || nt == 'a'){ result = 0; }
else if (nt == 'C' || nt == 'c'){ result = 1; }
else if (nt == 'G' || nt == 'g'){ result = 2; }
else if (nt == 'T' || nt == 't'){ result = 3; }
else { result = 4; }
return result;
}
int nt2int_rc (char nt){
int result;
if (nt == 'A' || nt == 'a'){ result = 3; }
else if (nt == 'C' || nt == 'c'){ result = 2; }
else if (nt == 'G' || nt == 'g'){ result = 1; }
else if (nt == 'T' || nt == 't'){ result = 0; }
else { result = 4; }
return result;
}
int nt2int_rc_indel (char nt){
int result;
if (nt == 'A' ){ result = 3; }
else if (nt == 'C' ){ result = 2; }
else if (nt == 'G' ){ result = 1; }
else if (nt == 'T' ){ result = 0; }
else if (nt == 'a' ){ result = 8; }
else if (nt == 'c' ){ result = 7; }
else if (nt == 'g' ){ result = 6; }
else if (nt == 't' ){ result = 5; }
else if (nt == 'n' ){ result = 9; }
else if (nt == 'x' ){ result = 10;}
else { result = 4; }
return result;
}
int trinucleotide (char a, char b, char c){
int freq_id;
if (a == 'A' || a == 'a'){ freq_id = 0;}
else if (a == 'C' || a == 'c'){ freq_id = 16;}
else if (a == 'G' || a == 'g'){ freq_id = 32;}
else if (a == 'T' || a == 't'){ freq_id = 48;}
else { freq_id = 0;}
if (b == 'A' || b == 'a'){ freq_id += 0;}
else if (b == 'C' || b == 'c'){ freq_id += 4;}
else if (b == 'G' || b == 'g'){ freq_id += 8;}
else if (b == 'T' || b == 't'){ freq_id += 12;}
else {freq_id = 0;}
if (c == 'A' || c == 'a'){ freq_id += 0;}
else if (c == 'C' || c == 'c'){ freq_id += 1;}
else if (c == 'G' || c == 'g'){ freq_id += 2;}
else if (c == 'T' || c == 't'){ freq_id += 3;}
else {freq_id = 0;}
return freq_id;
}
int trinucleotide_pep (char a, char b, char c){
int freq_id;
if (a == 'A' || a == 'a'){ freq_id = 0;}
else if (a == 'C' || a == 'c'){ freq_id = 16;}
else if (a == 'G' || a == 'g'){ freq_id = 32;}
else if (a == 'T' || a == 't'){ freq_id = 48;}
else { freq_id = 64;}
if (freq_id <64){
if (b == 'A' || b == 'a'){ freq_id += 0;}
else if (b == 'C' || b == 'c'){ freq_id += 4;}
else if (b == 'G' || b == 'g'){ freq_id += 8;}
else if (b == 'T' || b == 't'){ freq_id += 12;}
else {freq_id = 64;}
}
if (freq_id < 64){
if (c == 'A' || c == 'a'){ freq_id += 0;}
else if (c == 'C' || c == 'c'){ freq_id += 1;}
else if (c == 'G' || c == 'g'){ freq_id += 2;}
else if (c == 'T' || c == 't'){ freq_id += 3;}
else {freq_id = 64;}
}
return freq_id;
}
void get_rc_dna(char *dna, char *dna1){
char codon[5] = {'A', 'C', 'G', 'T', 'N'};
int i;
int dna_len = strlen(dna);
for (i=0; i<dna_len; i++){
dna1[dna_len-i-1] =codon[nt2int_rc(dna[i])];
}
}
void get_rc_dna_indel(char *dna, char *dna1){
char codon[11] = {'A', 'C', 'G', 'T', 'N', 'a', 'c', 'g', 't', 'n', 'x'};
int i;
int dna_len = strlen(dna);
for (i=0; i<dna_len; i++){
dna1[dna_len-i-1] =codon[nt2int_rc_indel(dna[i])];
}
}
void get_protein(char *dna, char *protein, int strand){
int i;
char codon_code[65] = {'K','N','K','N',
'T','T','T','T',
'R','S','R','S',
'I','I','M','I',
'Q','H','Q','H',
'P','P','P','P',
'R','R','R','R',
'L','L','L','L',
'E','D','E','D',
'A','A','A','A',
'G','G','G','G',
'V','V','V','V',
'*','Y','*','Y',
'S','S','S','S',
'*','C','W','C',
'L','F','L','F', 'X'};
char anti_codon_code[65] = {'F','V','L','I',
'C','G','R','S',
'S','A','P','T',
'Y','D','H','N',
'L','V','L','M',
'W','G','R','R',
'S','A','P','T',
'*','E','Q','K',
'F','V','L','I',
'C','G','R','S',
'S','A','P','T',
'Y','D','H','N',
'L','V','L','I',
'*','G','R','R',
'S','A','P','T',
'*','E','Q','K','X'};
int dna_len = strlen(dna);
int protein_len = dna_len/3;
if (strand ==1){
for (i=0; i<dna_len; i+=3){
protein[i/3] = codon_code[trinucleotide_pep(dna[i], dna[i+1], dna[i+2])];
}
}else{
if (dna_len % 3 == 2){
dna_len -= 2;
}else if (dna_len % 3 == 1){
dna_len -= 1;
}
for (i=0; i<dna_len; i+=3){
protein[(dna_len-i)/3-1] = anti_codon_code[trinucleotide_pep(dna[i], dna[i+1], dna[i+2])];
protein_len --;
}
}
}
void print_usage(){
printf("%s", "USAGE: ./FragGeneScan.pl -s [seq_file_name] -o [output_file_name] -w [1 or 0] -t [train_file_name]\n");
printf("%s", " [seq_file_name]: sequence file name including the full path\n");
printf("%s", " [output_file_name]: output file name including the full path\n");
printf("%s", " [1 or 0]: 1 if the sequence file has complete genomic sequences\n");
printf("%s", " 0 if the sequence file has short sequence reads\n");
printf("%s", " [train_file_name]: file name that contains model parameters; this file should be in the \"train\" directory\n");
printf("%s", " Note that four files containing model parameters already exist in the \"train\" directory\n");
printf("%s", " [complete] for complete genomic sequences or short sequence reads without sequencing error\n");
printf("%s", " [sanger_5] for Sanger sequencing reads with about 0.5% error rate\n");
printf("%s", " [sanger_10] for Sanger sequencing reads with about 1% error rate\n");
printf("%s", " [454_5] for 454 pyrosequencing reads with about 0.5% error rate\n");
printf("%s", " [454_10] for 454 pyrosequencing reads with about 1% error rate\n");
printf("%s", " [454_30] for 454 pyrosequencing reads with about 3% error rate\n");
printf("%s", " [illumina_5] for Illumina sequencing reads with about 0.5% error rate\n");
printf("%s", " [illumina_10] for Illumina sequencing reads with about 1% error rate\n\n");
}