-
Notifications
You must be signed in to change notification settings - Fork 14
/
andi.c
278 lines (240 loc) · 6.68 KB
/
andi.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
/**
* @file
*
* This is the main file. It contains functions to parse the commandline arguments,
* read files etc.
*
* @brief The main file
* @author Fabian Klötzl
* @section License
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details at
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <errno.h>
#include "global.h"
#include "process.h"
#include "io.h"
#include "sequence.h"
#ifdef _OPENMP
#include <omp.h>
#endif
/* Global variables */
int FLAGS = 0;
int THREADS = 1;
double RANDOM_ANCHOR_PROP = 0.05;
void usage(void);
void version(void);
/**
* @brief The main function.
*
* The main function reads and parses the commandline arguments. Depending on
* the set flags it reads the input files and forwards the contained sequences
* to processing.
*/
int main( int argc, char *argv[]){
int c;
int version_flag = 0;
struct option long_options[] = {
{"version", no_argument, &version_flag, 1},
{"help", no_argument, NULL, 'h'},
{"raw", no_argument, NULL, 'r'},
{"verbose", no_argument, NULL, 'v'},
{"join", no_argument, NULL, 'j'},
{"low-memory", no_argument, NULL, 'm'},
{0,0,0,0}
};
#ifdef _OPENMP
// Use all available processors by default.
THREADS = omp_get_num_procs();
#endif
// parse arguments
while( 1 ){
int option_index = 0;
c = getopt_long( argc, argv, "jvhrt:p:m", long_options, &option_index);
if( c == -1){
break;
}
switch (c){
case 0:
break;
case 'h':
usage();
break;
case 'r':
FLAGS |= F_RAW;
break;
case 'v':
FLAGS |= FLAGS & F_VERBOSE ? F_EXTRA_VERBOSE : F_VERBOSE;
break;
case 'p':
{
errno = 0;
char *end;
double prop = strtod( optarg, &end);
if( errno || end == optarg || *end != '\0'){
warnx(
"Expected a floating point number for -p argument, but '%s' was given. "
"Skipping argument.", optarg
);
break;
}
if( prop < 0.0 || prop > 1.0 ){
warnx(
"A probability should be a value between 0 and 1; "
"Ignoring -p %f argument.", prop
);
break;
}
RANDOM_ANCHOR_PROP = prop;
}
break;
case 'm':
FLAGS |= F_LOW_MEMORY;
break;
case 'j':
FLAGS |= F_JOIN;
break;
case 't':
{
#ifdef _OPENMP
errno = 0;
char *end;
long unsigned int threads = strtoul( optarg, &end, 10);
if( errno || end == optarg || *end != '\0'){
warnx(
"Expected a number for -t argument, but '%s' was given. "
"Ignoring -t argument.", optarg
);
break;
}
if( threads > (long unsigned int) omp_get_num_procs() ){
warnx(
"The number of threads to be used, is greater then the number of available processors; "
"Ignoring -t %lu argument.", threads
);
break;
}
THREADS = threads;
#else
warnx("This version of andi was built without OpenMP and thus "
"does not support multi threading. Ignoring -t argument.");
#endif
}
break;
case '?': /* intentional fall-through */
default:
usage();
break;
}
}
if( version_flag ){
version();
}
argc -= optind;
argv += optind;
// at least one file name must be given
if( FLAGS & F_JOIN && argc == 0 ){
errx(1, "In join mode at least one filename needs to be supplied.");
}
dsa_t dsa;
if(dsa_init(&dsa)){
errx(errno,"Out of memory.");
}
FILE *in = NULL;
const char *name;
// parse all files
int minfiles = FLAGS & F_JOIN ? 2 : 1;
for( ; ; minfiles-- ){
if( !*argv){
if( minfiles <= 0) break;
// if no files are supplied, read from stdin
in = stdin;
name = "stdin";
} else {
name = *argv++;
in = fopen( name, "r");
if( !in) {
warn("%s", name);
continue;
}
}
if( FLAGS & F_JOIN){
read_fasta_join( in, &dsa, name);
} else {
read_fasta( in, &dsa);
}
fclose(in);
}
size_t n = dsa_size( &dsa);
if( FLAGS & F_VERBOSE){
fprintf( stderr, "Comparing %zu sequences\n", n);
fflush( stderr);
}
seq_t *sequences = dsa_data( &dsa);
// compute distance matrix
if( n >= 2){
calculate_distances(sequences, n);
} else {
warnx("I am truly sorry, but with less than two sequences (%zu given) there is nothing to compare.", n);
}
dsa_free( &dsa);
return 0;
}
/**
* Prints the usage to stdout and then exits successfully.
*/
void usage(void){
const char str[]= {
"Usage: andi [-jrv] [-p FLOAT] FILES...\n"
"\tFILES... can be any sequence of FASTA files. If no files are supplied, stdin is used instead.\n"
"Options:\n"
" -j, --join Treat all sequences from one file as a single genome\n"
" -m, --low-memory Use less memory at the cost of speed\n"
" -p <FLOAT> Significance of an anchor pair; default: 0.05\n"
" -r, --raw Calculates raw distances; default: Jukes-Cantor corrected\n"
" -v, --verbose Prints additional information\n"
#ifdef _OPENMP
" -t <INT> The number of threads to be used; by default, all available processors are used\n"
#endif
" -h, --help Display this help and exit\n"
" --version Output version information and acknowledgments\n"
};
printf("%s", str);
exit(EXIT_SUCCESS);
}
/**
* This function just prints the version string and then aborts
* the program. It conforms to the [GNU Coding Standard](http://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion).
*/
void version(void){
const char str[]= {
"andi " VERSION "\n"
"Copyright (C) 2014 Fabian Klötzl\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n\n"
"Acknowledgments:\n"
"1) Andi: Haubold, B. Klötzl, F. and Pfaffelhuber, P. (2014). "
"Fast and accurate estimation of evolutionary distances between closely related genomes\n"
"2) Algorithm: Ohlebusch, E. (2013). Bioinformatics Algorithms. Sequence Analysis, "
"Genome Rearrangements, and Phylogenetic Reconstruction. pp 118f.\n"
"3) SA construction: Mori, Y. (2005). Short description of improved two-stage suffix "
"sorting alorithm. http://homepage3.nifty.com/wpage/software/itssort.txt\n"
};
printf("%s", str);
exit(EXIT_SUCCESS);
}