-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsa_main.c
358 lines (345 loc) · 11.4 KB
/
msa_main.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
#include "msa.h"
#include "io.h"
#include "function.h"
#include <linux/limits.h>
#define REPORTCOSTS 1
#if REPORTCOSTS
#include <time.h>
#endif
#define VERSION "0.4.4"
#define SHOWVERSION reporterr( "%s (%s, %d-bit) Version " VERSION "\n\n", "MSA align", (seq_type == 1) ? "nuc" : ((seq_type == 0) ? "unknown" : "aa"), sizeof(int *) * 8 )
// #define FILESAVE
#define MIN(X, Y) ((X) > (Y) ? (Y) : (X))
static int alignmode, calcsp, simplycheck, profilealignthread;
void print_help_message()
{
reporterr("WMSA alignment Version %s ", VERSION);
#ifdef enablemultithread
reporterr("(Multi-thread mode)");
#else
reporterr("(Single-thread mode)");
#endif
reporterr(" help: \n");
reporterr("== Common ==\n");
reporterr("-i: input file name\n");
reporterr("-o: output file name\n");
reporterr("-D, -P: The sequence is DNA/RNA(-D) or Protein(-P)\n");
reporterr("-T: use threads in cd-hit and staralign\n");
reporterr("-p: Calcuate SP Scores after alignment\n");
reporterr("-d: print debug info on profilealign, staralign and cd-hit*\n");
reporterr("-s: do not simply check the result is ok\n");
reporterr("-v: only print version\n");
reporterr("-t: temp folder name\n");
reporterr("== MAFFT common arguments ==\n");
reporterr("-V, -f, -S: ppenalty_dist, penalty, nmax_shift\n");
reporterr("-z, -w: fftthreshold, fftWinsize\n");
reporterr("-b: Band in alignment, if not provided, use auto detected band\n");
reporterr("-B: BLOSUM??\n");
reporterr("=== profilealign arguments ===\n");
reporterr("-A, -F: Use Simply lign(-A) or FFT align(-F) on profile alignment\n");
reporterr("-x: use threads in profilealign, default is min(10, staralign_threads)\n");
reporterr("== CD-HIT arugments ==\n");
reporterr("-c: threshold on cd-hit to cluster\n");
reporterr("-M: max memory for CD-HIT\n");
}
void print_version()
{
reporterr("WMSA verison %s ", VERSION);
#ifdef enablemultithread
reporterr("(Multi-thread mode)\n");
#else
reporterr("(Single-thread mode)\n");
#endif
}
void arguments(int argc, char *argv[])
{
if(argc == 1)
{
print_help_message();
exit(0);
}
int c;
cdhitsim = NOTKNOWNDOUBLE;
ppenalty = NOTKNOWNINT;
ppenalty_dist = NOTKNOWNINT;
fftthreshold = NOTKNOWNINT;
fftWinSize = NOTKNOWNINT;
alignband = NOTKNOWNINT;
threads = 1;
nmax_shift = 1;
seq_type = 0;
alignmode = 1;
calcsp = 0;
maxmemory = 0; // In cd-hit, maxmemory=0 is unlimited
printdebug = 0;
BLOSUM = 62;
simplycheck = 1;
profilealignthread = NOTKNOWNINT;
tmpinthisdir = 0;
while(--argc > 0 && (*++ argv)[0] == '-' )
{
while ( (c = *++ argv[0]) )
{
switch( c )
{
// I/O
case 'i':
inputfile = *++ argv;
-- argc;
goto nextoption;
case 'o':
outputfile = *++ argv;
-- argc;
goto nextoption;
case 't':
readtmpdir = *++ argv;
-- argc;
goto nextoption;
// Sequence type
case 'P':
seq_type = 2;
goto nextoption;
case 'D':
seq_type = 1;
goto nextoption;
// MAFFT arguments
case 'V':
ppenalty_dist = myatof( *++ argv, c );
-- argc;
goto nextoption;
case 'f':
ppenalty = myatof( *++ argv, c );
-- argc;
goto nextoption;
case 'z':
fftthreshold = myatoi( *++ argv, c );
-- argc;
goto nextoption;
case 'w':
fftWinSize = myatoi( *++ argv, c );
-- argc;
goto nextoption;
case 'b':
alignband = myatoi(*++ argv, c);
-- argc;
goto nextoption;
case 'T':
threads = myatoi(*++ argv, c);
-- argc;
goto nextoption;
case 'S':
nmax_shift = myatoi(*++ argv, c);
-- argc;
goto nextoption;
case 'B':
BLOSUM = myatoi(*++ argv, c);
-- argc;
goto nextoption;
case 'A':
alignmode = 0;
break;
case 'F':
alignmode = 1;
break;
case 'd':
printdebug = 1;
break;
case 'x':
profilealignthread = myatoi(*++ argv, c);
-- argc;
goto nextoption;
// CD-HIT arguments
case 'c':
cdhitsim = myatof( *++ argv, c );
if(cdhitsim >= 1.0 || cdhitsim <= 0.0)
{
reporterr("Warning: cd-hit-sim is out of (0, 1), will ignore. \n");
cdhitsim = NOTKNOWNDOUBLE;
}
-- argc;
goto nextoption;
case 'M':
maxmemory = myatoi(*++ argv, c);
-- argc;
goto nextoption;
// common
case 'p':
calcsp = 1;
break;
case 's':
simplycheck = 0;
break;
case 'H':
case '?':
print_help_message();
exit(0);
case 'v':
print_version();
exit(0);
default:
reporterr( "Illegal option %c. Please type wmsa -H or -? to get help.\n", c );
argc = 0;
break;
}
}
nextoption:
;
}
if( argc != 0 )
{
reporterr( "Please type wmsa -H or -? to get help.\n" );
exit( 1 );
}
}
int main(int argc, char **argv)
{
arguments(argc, argv);
FILE *input = fopen(inputfile, "rb"), *cmdd, *order;
seq_type = getnumlenandtype(input, seq_type);
reporterr("This file has %d sequences.\n", seq_num);
/* Part -1: strip gap */
char **aseq, **bseq, **strip_name;
int *nlen;
int i, j, k;
aseq = AllocateCharMtx(seq_num, seq_maxlen + 10);
bseq = AllocateCharMtx(seq_num, seq_maxlen + 10);
strip_name = AllocateCharMtx(seq_num, B + 1);
nlen = AllocateIntVec(seq_num + 1);
readData(input, strip_name, nlen, aseq, seq_num);
fclose(input);
if(seq_num < 2)
{
reporterr("Warning: only %d sequence(s) found.\n", seq_num);
return 0;
}
for(i = 0; i < seq_num; ++ i) gapfilter_oneseq(bseq[i], aseq[i]);
cmdd = fopen(outputfile, "w");
writeData(cmdd, seq_num, strip_name, nlen, bseq);
FreeCharMtx(aseq);
FreeCharMtx(bseq);
FreeCharMtx(strip_name);
FreeIntVec(nlen);
fclose(cmdd);
#if REPORTCOSTS
time_t starttime;
starttime = time(NULL);
#endif
/* Part 0: process program path && make temp folder */
// get the program folder
char *programfolder = AllocateCharVec(PATH_MAX + 100);
programfolder = get_exe_path(programfolder, PATH_MAX + 100);
if(printdebug) puts(programfolder);
// now the value programfolder is the right value, and the last place of the folder name is not '/'.
if(readtmpdir != NULL)
{
if(tmpinthisdir) sprintf(tmpdir, "./%s/", readtmpdir);
else sprintf(tmpdir, "%s/", readtmpdir);
}
if(maketmpfolder())
{
reporterr("Warning: cannot make folder %s\n", tmpdir);
}
/* Part 1: cd-hit process input */
reporterr("Clustering...");
clustercommand(outputfile, programfolder);
reporterr("done. \n");
/*
Saved file to tmpdir/tmp;
the clusters is tmpdir/tmp_%d.clstr
*/
/* Part 2: for each cluster, run staralign to align */
int cluster_seq = calcuate_cluster_sequences();
reporterr("CD-HIT spilted this file to %d cluster(s).\n", cluster_seq);
/* Part 2.1: read the center file */
int *center_len;
char **center_name = NULL, **center_seq = NULL;
/* Part 2.2: staralign on the code && write center sequence order file */
reporterr("Clusteralign...\n");
// first check the existance of this program
checkBLASTalign(programfolder, "submafft/staralign");
for(i = 0; i < cluster_seq; ++ i)
{
sprintf(cmdstr2, "%stmp_%d.clstr", tmpdir, i);
sprintf(cmdstr3, "%stmp_%d.center", tmpdir, i);
BLASTaligncommand(cmdstr3, cmdstr2, programfolder, "submafft/staralign");
reporterr("\rSTEP %d / %d", i + 1, cluster_seq);
}
/* Part 2.3: Write center sequence order file */
sprintf(cmdstr, "%scluster_order", tmpdir);
cmdd = fopen(cmdstr, "w");
for(i = 0; i < cluster_seq; ++ i)
{
fprintf(cmdd, "%stmp_%d.clstr.res\n", tmpdir, i);
}
fclose(cmdd);
reporterr("\ndone. \n");
if(cluster_seq == 1)
{
/* no need to do part 3 */
sprintf(cmdstr, "mv %stmp_0.clstr.res %s", tmpdir, outputfile);
if(movefile()) reporterr("Moving file failed\n");
}
else
{
/* Part 3: profile-profile align */
if(profilealignthread != NOTKNOWNINT) threads = profilealignthread;
else threads = MIN(threads, 10);
reporterr("profile merging... ");
checkprofilealign(programfolder, "submafft/profilealign");
sprintf(cmdstr3, "%scluster_order", tmpdir);
sprintf(cmdstr2, "%stmp", tmpdir);
profilealigncommand(cmdstr3, cmdstr2, alignmode, programfolder, "submafft/profilealign");
reporterr("done. \n");
}
#if REPORTCOSTS
reporterr( "\nmsa align, real = %f min\n", (float)(time(NULL) - starttime)/60.0 );
#endif
reporterr("\n\nDone.\n");
SHOWVERSION;
if(simplycheck && ! calcsp)
{
input = fopen(outputfile, "rb");
if(input == NULL)
{
reporterr("ERROR: result file is not exist!!\n");
return 1;
}
if(fgetc(input) == EOF)
{
reporterr("ERROR: result file is empty. please check your arugments and try again.\n");
reporterr("It may coursed by small fftWinSize (-w) or sequences is not the high similarity.\n");
return 1;
}
}
if(calcsp)
{
/* SP Scores calcuation */
if(seq_type == 2) reporterr("Info: Using BLOSUM62 to calcuate the SP Scores\n");
else if(seq_type == 1) reporterr("Info: Using trans/trans model to calcuate the SP Scores\n");
input = fopen(outputfile, "rb");
getnumlenandtype(input, seq_type);
reporterr("Info: The final file has %d sequences.\n", seq_num);
center_name = AllocateCharMtx(seq_num, seq_maxlen + 10);
center_len = AllocateIntVec(seq_num);
center_seq = AllocateCharMtx(seq_num, seq_maxlen + 10);
readData(input, center_name, center_len, center_seq, seq_num);
fclose(input);
double sp = 0.0;
if(ppenalty == NOTKNOWNINT) ppenalty = (int)(-1530.0 * 600 / 1000 + 0.5); // by MAFFT
for(i = 0; i < seq_num; ++ i)
for(j = i + 1; j < seq_num; ++ j)
{
sp += naivepairscore11(center_seq[i], center_seq[j], ppenalty, seq_type) / 600.0;
}
reporterr("SP Score is %f\n", sp);
FreeCharMtx(center_name);
FreeCharMtx(center_seq);
FreeIntVec(center_len);
}
#ifndef FILESAVE
/* Part 4: remove useless files */
cleantmpfile();
#endif
free(programfolder);
return 0;
}