read_settings.cc 65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Spell-QTL  Software suite for the QTL analysis of modern datasets.
 * Copyright (C) 2016,2017  Damien Leroux <damien.leroux@inra.fr>, Sylvain Jasson <sylvain.jasson@inra.fr>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Damien Leroux's avatar
Damien Leroux committed
18
19
20
21
22
23
24
25
26
#include "input.h"
#include "settings.h"
#include <set>
#include <map>
#include <algorithm>
#include "banner.h"
#include "labelled_matrix.h"
#include "cache2.h"
#include "model.h"
Damien Leroux's avatar
WIP.    
Damien Leroux committed
27
#include "computations/probabilities.h"
28

Damien Leroux's avatar
Damien Leroux committed
29
30
31
32
33
34
35
36
37
#include <regex>

#include <cerrno>  /* for errno */
#include <cstring> /* for strerror */

extern "C" {
#include <ftw.h>
}

38
#define OUT(__x__) do { msg_handler_t::cout() << __x__; } while (0)
Damien Leroux's avatar
Damien Leroux committed
39

40
/*#include "computations.h"*/
Damien Leroux's avatar
Damien Leroux committed
41

42
43
/*namespace read_data {*/
/*settings_t* read_settings(std::istream&) {*/
44
#if 0
45
46
47
48
    if (!is.good()) {
        throw input_exception(is, "couldn't read from file");
    }
    return check_consistency(settings_dtd.parse(is));
49
#endif
50
51
52
    /*return NULL;*/
/*}*/
/*}*/
53
54


55
56
57
static const char* prg_name = NULL;


58
59
60
61
62
63
64
template <typename T>
T to(const std::string& x)
{
    std::stringstream s(x);
    T ret;
    s >> ret;
    return ret;
65
}
66
67
68
69


#define VCCI std::vector<const char*>::iterator
#define CALLBACK_ARGS settings_t*& target, VCCI& ai, VCCI& aj
Damien Leroux's avatar
Damien Leroux committed
70
#define SAFE_IGNORE_CALLBACK_ARGS do { (void)target; (void)ai; (void)aj; } while(0)
71
72
73
74

typedef std::function<void(CALLBACK_ARGS)> argument_parser_callback_type;


Damien Leroux's avatar
Damien Leroux committed
75
76
extern int MAGIC8_ORDER;
extern int RIL_ORDER;
77

78
79
80
81
82
locus_probabilities_type
locus_probabilities(const context_key& ck, const locus_key& lk,
                    int ind,
                    const std::vector<double>& loci);

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
struct default_value_display {
    std::string str;

    default_value_display()
        : str()
    {}

    default_value_display(bool req)
        : str()
    {
        std::stringstream ss;
        ss << WHITE << (req ? "MANDATORY" : "Optional") << NORMAL;
        str = ss.str();
    }

    default_value_display(const char* s)
        : str()
    {
        std::stringstream ss;
        ss << "Default: " << WHITE << s << NORMAL;
        str = ss.str();
    }

    default_value_display(default_value_display&& dvd)
        : str(std::forward<std::string>(dvd.str))
    {}

    default_value_display(const default_value_display& dvd)
        : str(dvd.str)
    {}

    template <typename C, typename X>
    default_value_display(X C::* x)
        : str()
    {
        C s;
        std::stringstream ss;
        ss << "Default: " << WHITE << model_print::to_string(s.*x) << NORMAL;
        str = ss.str();
    }

    template <typename CX, typename X, typename CY, typename Y>
    default_value_display(X CX::* x, Y CY::* y)
        : str()
    {
        CX cx;
        CY cy;
        std::stringstream ss;
        ss << "Default: " << WHITE << model_print::to_string(cx.*x) << NORMAL << ", " << WHITE << model_print::to_string(cy.*y) << NORMAL;
        str = ss.str();
    }
134
135
136
137
138
139
140
141
142

    template <typename K>
    default_value_display(const K* var)
        : str()
    {
        std::stringstream ss;
        ss << "Default: " << WHITE << model_print::to_string(*var) << NORMAL;
        str = ss.str();
    }
143
144
145
146
};



147
148
struct argument {
    std::vector<std::string> names;
149
    std::vector<std::string> parameters;
150
    std::string description;
Damien Leroux's avatar
Damien Leroux committed
151
    bool hidden;
152
    default_value_display dvd;
153
    argument_parser_callback_type callback;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

    argument(std::vector<std::string>&& n,
             std::vector<std::string>&& p,
             std::string&& d,
             bool h,
             default_value_display&& dv,
             argument_parser_callback_type c)
        : names(std::forward<std::vector<std::string>>(n))
        , parameters(std::forward<std::vector<std::string>>(p))
        , description(std::forward<std::string>(d))
        , hidden(h)
        , dvd(std::forward<default_value_display>(dv))
        , callback(std::forward<argument_parser_callback_type>(c))
    {}

169
170
    void install(std::map<std::string, argument_parser_callback_type>& amap,
                 std::map<std::string, int>& pmap) const
171
172
173
    {
        for (auto& n: names) {
            amap[n] = callback;
174
            pmap[n] = parameters.size();
175
176
177
178
179
180
181
182
183
184
185
186
187
        }
    }
};


settings_t* ensure(settings_t*& target)
{
    if (target == NULL) {
        target = new settings_t();
    }
    return target;
}

188
189
190
191
192
193
194
195
196
void destroy(settings_t*& target)
{
    if (target != NULL) {
        delete target;
        target = NULL;
    }
}


Damien Leroux's avatar
Damien Leroux committed
197
198
199
200
201
202
203
204
struct argument_section {
    std::string title;
    std::string description;
    bool hidden;
    std::vector<argument> arguments;
};

typedef std::vector<argument_section> argument_section_list_t;
205
206
207

std::string last_pop_name = "";

208
209
#if 0
pop_data_type& last_pop(settings_t* target)
210
211
212
213
214
215
{
    if (last_pop_name == "") {
        MSG_ERROR("No population has been defined yet.", "Use '-p pop_name qtl_gen traits_file' before any other population-related argument");
    }
    return target->populations[last_pop_name];
}
216
#endif
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
void
read_locus_list(std::string& s, settings_t* target)
{
    std::map<const chromosome*, std::vector<double>>& ll = target->estimation_loci;
    std::string token;
    auto i = s.cbegin();
    auto j = s.cend();
    size_t locus_end, colon;
    while (i != j) {
        locus_end = s.find_first_of(',');
        if (locus_end == std::string::npos) {
            locus_end = j - i;
        }
        colon = s.find_first_of(':');
        if (colon == std::string::npos) {
            colon = j - i;
        }
        if (locus_end <= colon) {
            MSG_ERROR("Invalid locus around \"" << std::string(i, i + locus_end) << "\": Locus format is chromosome_name:position", "Specify loci in the correct format");
        }
        std::string name(i, i + colon);
        const chromosome* chr = target->find_chromosome(name);
        if (!chr) {
            MSG_ERROR("Chromosome \"" << name << "\" doesn't exist.", "Use correct chromosome names");
        } else {
            double locus = to<double>(std::string(i + colon + 1, i + locus_end));
            ll[chr].push_back(locus);
        }
        i += locus_end;
        i += (i != j);  /* skip comma if not at end */
    }
    for (auto& kv: ll) {
        std::sort(kv.second.begin(), kv.second.end());
    }
}


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
void
read_threshold_values(const std::string& s, settings_t* target)
{
    std::map<std::string, double>& ql = target->qtl_thresholds;
    std::string token;
    auto i = s.cbegin();
    auto j = s.cend();
    size_t locus_end, colon;
    while (i != j) {
        locus_end = s.find_first_of(',');
        if (locus_end == std::string::npos) {
            locus_end = j - i;
        }
        colon = s.find_first_of('=');
        if (colon == std::string::npos) {
            colon = j - i;
        }
        if (locus_end <= colon) {
273
            MSG_ERROR("Invalid single_trait threshold around \"" << std::string(i, i + locus_end) << "\": Trait threshold format is trait_name=value", "Specify single_trait thresholds in the correct format");
274
275
276
277
278
279
280
281
282
        }
        std::string name(i, i + colon);
        ql[name] = to<double>(std::string(i + colon + 1, i + locus_end));
        i += locus_end;
        i += (i != j);  /* skip comma if not at end */
    }
}


283
284
std::vector<std::string>
cut(const std::string& s, int width)
285
286
287
288
289
{
    std::vector<std::string> ret;
    auto i = s.begin();
    auto z = s.end();
    while (i != z) {
290
        auto j = z;
291
292
        if ((z - i) >= width) {
            j = i + width;
293
#if 0
294
295
296
297
        } else {
            ret.push_back(std::string(i, z));
            return ret;
            /*j = z;*/
298
#endif
299
        }
300
301
302
        auto last = j;
        auto k = i;
        for (; k != j; ++k) {
303
304
            if (*k == ' ') {
                last = k;
305
306
307
            } else if (*k == '\n') {
                last = k;
                break;
308
309
            }
        }
310
311
312
        if (k == z) {
            last = z;
        }
313
        ret.push_back(std::string(i, last));
314
        /*OUT(ret.back() << std::endl);*/
315
316
317
318
319
        i = last + (last != z);
    }
    return ret;
}

320
settings_t* read_settings(const std::string& filename, ifile& is);
321
design_type* read_design(const std::string& filename, std::istream& is);
322
/*format_specification_t* read_format(const std::string& filename, std::istream& is);*/
323
void read_format(std::map<std::string, marker_observation_spec>& settings, const std::string& filename, std::istream& is);
324

Damien Leroux's avatar
Damien Leroux committed
325
void print_usage_impl(bool show_hidden, const argument_section_list_t& aslt)
326
{
Damien Leroux's avatar
WIP.    
Damien Leroux committed
327
    int max_cols = msg_handler_t::termcols();
Damien Leroux's avatar
Damien Leroux committed
328
329
330
331
332
    int opt_width = 0;
    int doc_width = 0;
    if (max_cols <= 0) {
        max_cols = 80;
    }
333
    settings_t tmp;
Damien Leroux's avatar
Damien Leroux committed
334
335
    OUT("Usage: " << WHITE << prg_name << " [arguments...]" << NORMAL << std::endl);
    OUT("Arguments:" << std::endl);
336
    for (auto& kv: aslt) {
Damien Leroux's avatar
Damien Leroux committed
337
338
339
340
341
342
343
        if (kv.hidden && !show_hidden) {
            continue;
        }
        for (auto& a: kv.arguments) {
            if (a.hidden && !show_hidden) {
                continue;
            }
Damien Leroux's avatar
Damien Leroux committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
            auto ni = a.names.begin(), nj = a.names.end();
            int width = 0;
            if (ni != nj) {
                width = ni->size();
                for (++ni; ni != nj; ++ni) {
                    width += 1 + ni->size();
                }
            }
            for (auto& par: a.parameters) {
                width += 3 + par.size();
            }
            if (width > opt_width) {
                opt_width = width;
            }
        }
    }
    opt_width += 2;
    doc_width = max_cols - opt_width;
    for (auto& kv: aslt) {
Damien Leroux's avatar
Damien Leroux committed
363
364
365
366
367
368
369
370
371
372
373
374
375
        if (kv.hidden && !show_hidden) {
            continue;
        }
        OUT(std::endl << CYAN <<  "* " << kv.title << NORMAL << std::endl);
        if (kv.description.size() > 0) {
            for (const auto& line: cut(kv.description, max_cols - 2)) {
                OUT("  " << line << std::endl);
            }
        }
        for (auto& a: kv.arguments) {
            if (a.hidden && !show_hidden) {
                continue;
            }
376
377
378
379
380
381
382
383
            auto ni = a.names.begin(), nj = a.names.end();
            std::stringstream s;
            s << WHITE << (*ni);
            ++ni;
            for (; ni != nj; ++ni) { s << NORMAL << ',' << WHITE << (*ni); }
            s << NORMAL;
            for (auto& par: a.parameters) { s << " <" << YELLOW << par << NORMAL << '>'; }
            /* find max length under 30 without cutting words */
Damien Leroux's avatar
Damien Leroux committed
384
            std::vector<std::string> lines = cut(a.description, doc_width);
385
            auto i = lines.begin(), j = lines.end();
Damien Leroux's avatar
Damien Leroux committed
386
387
            OUT(std::setw(opt_width) <<  "" << (*i));
            OUT('\r' << s.str() << std::endl);
388
            for (++i; i != j; ++i) {
Damien Leroux's avatar
Damien Leroux committed
389
                OUT(std::setw(opt_width) << "" << (*i) << std::endl);
390
            }
391
392
393
394
395
396
397
398
399
            if (a.dvd.str.size()) {
                lines = cut(a.dvd.str, doc_width);
                i = lines.begin();
                j = lines.end();
                OUT(std::setw(opt_width) <<  "" << (*i) << std::endl);
                for (++i; i != j; ++i) {
                    OUT(std::setw(opt_width) << "" << (*i) << std::endl);
                }
            }
400
401
402
        }
    }
}
403

Damien Leroux's avatar
Damien Leroux committed
404
void print_usage(bool);
405

406
407
#define is_hex(_c) (((_c) >= 'a' && (_c) <= 'f') || ((_c) >= '0' && (_c) <= '9') || ((_c) >= 'A' && (_c) <= 'F'))

Damien Leroux's avatar
Damien Leroux committed
408
extern "C" {
409
int clear_cache(const char *fpath, const struct stat *, int typeflag, struct FTW*)
Damien Leroux's avatar
Damien Leroux committed
410
{
Damien Leroux's avatar
Damien Leroux committed
411
412
    /* FIXME check for some magic bytes or something before deleting! */
    static std::regex is_cache_file("^.*/[a-f0-9]{1,16}$");
Damien Leroux's avatar
Damien Leroux committed
413
414
    /*std::string f(fpath);*/
    if (typeflag == FTW_F) {  /* ignore all but regular files */
Damien Leroux's avatar
Damien Leroux committed
415
        /*MSG_DEBUG("on " << fpath);*/
Damien Leroux's avatar
Damien Leroux committed
416
417
418
419
        if (std::regex_match(fpath, fpath + strlen(fpath), is_cache_file)) {
            /*remove(fpath);*/
            if (remove(fpath) != 0) {
                MSG_WARNING("Couldn't remove file \"" << fpath << "\": " << strerror(errno));
Damien Leroux's avatar
Damien Leroux committed
420
421
            /*} else {*/
                /*MSG_DEBUG("Deleted cache file <" << fpath << '>');*/
Damien Leroux's avatar
Damien Leroux committed
422
423
            }
        }
424
425
426
427
428
429
430
    } else if (typeflag == FTW_D) {
        size_t x = strlen(fpath) - 3;
        if (fpath[x] == '/' && is_hex(fpath[x + 1]) && is_hex(fpath[x + 2])) {
            if (remove(fpath) != 0) {
                MSG_WARNING("Couldn't remove directory \"" << fpath << "\": " << strerror(errno));
            }
        }
Damien Leroux's avatar
Damien Leroux committed
431
432
433
434
    }
    return 0;
}
}
435
436
437
438

static
argument_section_list_t
arguments = {
Damien Leroux's avatar
Damien Leroux committed
439
    {"Advanced (NOT INTENDED FOR USERS!)", "", true, {
440
#if 0
Damien Leroux's avatar
Damien Leroux committed
441
442
443
444
        {{"-Dd", "--dump-design"},
            {},
            "[DEBUG] Dump breeding design and exit",
            true,
445
            {},
Damien Leroux's avatar
Damien Leroux committed
446
447
            [](CALLBACK_ARGS)
            {
Damien Leroux's avatar
Damien Leroux committed
448
449
450
451
452
                OUT((*ensure(target)->design) << std::endl);
                exit(0);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
        {{"-Dg", "--dump-generation"},
453
            {"gen"},
Damien Leroux's avatar
Damien Leroux committed
454
455
            "[DEBUG] Dump one generation and exit",
            true,
456
            {},
Damien Leroux's avatar
Damien Leroux committed
457
458
459
460
461
462
463
464
465
466
467
468
469
            [](CALLBACK_ARGS)
            {
                std::string name = *++ai;
                auto it = ensure(target)->design->generation.find(name);
                if (it == ensure(target)->design->generation.end()) {
                    MSG_ERROR("Generation " << name << " not found.", "");
                    exit(0);
                }
                const generation_rs* gen = it->second;
                OUT((*gen) << std::endl);
                exit(0);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
470
#endif
Damien Leroux's avatar
WIP.    
Damien Leroux committed
471
472
473
474
475
476
477
478
479
480
        {{"-Dpop", "--dump-POP"},
            {},
            "[DEBUG] Dump parental origin probabilities and exit",
            true,
            {},
            [](CALLBACK_ARGS)
            {
                ensure(target);
                active_settings = target;

481
482
                /* TODO: FIXME */
#if 0
Damien Leroux's avatar
WIP.    
Damien Leroux committed
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
                msg_handler_t::check(true);
                active_settings->finalize();
                active_settings->set_title("Checking the validity of the configuration");
                if (!active_settings->sanity_check()) {
                    exit(-1);
                }
                msg_handler_t::check(true);
                locus_key lk;
                Eigen::IOFormat fmt(StreamPrecision, DontAlignCols, "\t", "\n", "", "", "", "");
                for (const chromosome& c: active_settings->map) {
                    auto loci = compute_steps(c.condensed.marker_locus, target->step);
                    for (const auto& kv: active_settings->populations) {
                        const auto& pop = kv.second;
                        context_key ck(new context_key_struc(&pop, &c, loci));
                        auto vck = as_value(ck);
498
499
500
                        /*collection<multi_generation_observations>*/
                            /*vmgo = make_collection<Disk>(population_marker_obs,*/
                                                         /*vck, range<int>(0, ck->pop->size(), 1));*/
Damien Leroux's avatar
WIP.    
Damien Leroux committed
501
502
                        collection<locus_probabilities_type>
                            alp = make_collection<Disk>(locus_probabilities,
503
                                    vck, as_value(lk), range<int>(0, ck->pop->size(), 1), as_value(ck->loci));
Damien Leroux's avatar
WIP.    
Damien Leroux committed
504
505
506
                        OUT("CHROMOSOME " << c.name << " POPULATION " << kv.first << std::endl);
                        int i = 0;
                        for (const auto& pm: alp) {
507
                            OUT('#' << (++i) << std::endl << pm->transpose() << std::endl);
Damien Leroux's avatar
WIP.    
Damien Leroux committed
508
509
510
                        }
                    }
                }
511
#endif
Damien Leroux's avatar
WIP.    
Damien Leroux committed
512
513
514
                exit(0);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
515
#if 0
Damien Leroux's avatar
Damien Leroux committed
516
        {{"-Dstfopom", "--dump-stfopom"},
517
            {"gen", "order"},
Damien Leroux's avatar
Damien Leroux committed
518
519
            "[DEBUG] Dump ST(fo)POM",
            true,
520
            {},
Damien Leroux's avatar
Damien Leroux committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
            [](CALLBACK_ARGS)
            {
                std::string name = *++ai;
                auto it = ensure(target)->design->generation.find(name);
                if (it == ensure(target)->design->generation.end()) {
                    MSG_ERROR("Generation " << name << " not found.", "");
                    exit(0);
                }
                int order = to<int>(*++ai);
                locus_key lk(new locus_key_struc());
                population pop;
                pop.qtl_generation_name = name;
                chromosome chr;
                active_settings = target;  /* because context_key_struc needs it :( */
                context_key ck(new context_key_struc(&pop, &chr, std::vector<double>()));
                int o = order + 1;
                int unit = o % 10;

                while (order > 0) {
                    lk = lk + (double) (order--);
                }

                auto sd = get_stpom_data(ck, lk);
                auto stfopom = compute_state_to_parental_origin(ck, lk);

                OUT("State to " << o << (unit == 1 ? "st" : unit == 2 ? "nd" : unit == 3 ? "rd" : "th") << " order parental origin matrix" << std::endl);
                OUT("Per-haplotype matrices" << std::endl << sd << std::endl);
                OUT("Full matrix" << std::endl << stfopom << std::endl);

Damien Leroux's avatar
Damien Leroux committed
550
551
552
                exit(0);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
553
#endif
Damien Leroux's avatar
Damien Leroux committed
554
555
556
557
        {{"--verbose-help"},
            {},
            "Display COMPLETE usage and exit",
            true,
558
            {},
Damien Leroux's avatar
Damien Leroux committed
559
560
561
562
563
564
565
            [](CALLBACK_ARGS)
            {
                print_usage(true);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
    }},
    {"Miscellaneous", "", false, {
Damien Leroux's avatar
Damien Leroux committed
566
567
568
569
        {{"-c", "--counters"},
            {},
            "Display counters and timers when exiting",
            true,
570
            {"not displayed"},
Damien Leroux's avatar
Damien Leroux committed
571
572
573
574
575
576
            [](CALLBACK_ARGS)
            {
                chrono::display() = true;
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

Damien Leroux's avatar
Damien Leroux committed
577
578
579
580
        {{"-v", "--version"},
            {},
            "Display version and exit",
            false,
581
            {},
Damien Leroux's avatar
Damien Leroux committed
582
583
            [](CALLBACK_ARGS)
            {
584
                OUT(BANNER << std::endl << std::endl);
Damien Leroux's avatar
Damien Leroux committed
585
586
587
588
589
590
591
592
                exit(0);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-h", "--help"},
            {},
            "Display usage and exit",
            false,
593
            {},
Damien Leroux's avatar
Damien Leroux committed
594
595
596
597
598
599
600
601
602
603
            [](CALLBACK_ARGS)
            {
                print_usage(false);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-n", "--name"},
            {"name"},
            "User-friendly name for this configuration",
            false,
604
            {true},
Damien Leroux's avatar
Damien Leroux committed
605
606
607
608
609
610
611
612
613
614
            [](CALLBACK_ARGS)
            {
                ensure(target)->name = *++ai;
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-N", "--notes"},
            {"text"},
            "Optional free text",
            false,
615
            {false},
Damien Leroux's avatar
Damien Leroux committed
616
617
618
619
620
621
622
623
624
625
            [](CALLBACK_ARGS)
            {
                ensure(target)->notes = *++ai;
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-wd", "--work-directory"},
            {"path"},
            "Path to directory for cache files and outputs",
            false,
626
            {&settings_t::work_directory},
Damien Leroux's avatar
Damien Leroux committed
627
628
            [](CALLBACK_ARGS) {
                ensure(target)->work_directory = *++ai;
629
630
631
632
                if (!check_file(ensure(target)->work_directory, true, true, false)) {
                    ensure_directories_exist(ensure(target)->work_directory);
                    check_file(ensure(target)->work_directory, true, true, true);
                }
Damien Leroux's avatar
Damien Leroux committed
633
634
635
636
637
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-P", "--parallel"},
            {"n_cores"},
638
            "Setup parallel computations (number of cores to use or 'auto')",
Damien Leroux's avatar
Damien Leroux committed
639
            false,
640
            {&settings_t::parallel},
Damien Leroux's avatar
Damien Leroux committed
641
642
643
644
645
646
            [](CALLBACK_ARGS) {
                /*ensure(target)->parallel = to<int>(*++ai);*/
                ensure(target)->set_parallel(*++ai);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//        {{"-i", "--input-XML"},
//            {"path"},
//            "Read configuration from this XML file",
//            false,
//            {},
//            [](CALLBACK_ARGS) {
//                std::string filename = *++ai;
//                if (target) {
//                    MSG_WARNING("Reading configuration from XML overrides previous command-line arguments");
//                    delete target;
//                }
//                check_file(filename, false, false);
//                MSG_INFO("Processing " << filename);
//                ifile ifs(filename);
//                target = read_settings(filename, ifs);
//                if (!target) {
//                    MSG_ERROR("Couldn't load XML file " << filename, "");
//                }
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
//
//        {{"-o", "--output-XML"},
//            {"path"},
//            "Output configuration to this XML file",
//            false,
//            {},
//            [](CALLBACK_ARGS) {
//                const char* path = *++ai;
//                if (std::string("-") == path) {
//                    OUT((*ensure(target)));
//                } else {
//                    ofile o(path);
//                    o << (*ensure(target));
//                }
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
Damien Leroux's avatar
Damien Leroux committed
683
684
685
686
687

        {{"--clean"},
            {},
            "Clears all cached files",
            false,
688
            {},
Damien Leroux's avatar
Damien Leroux committed
689
690
691
692
693
694
            [](CALLBACK_ARGS) {
                const std::string& wd = ensure(target)->work_directory;
                if (wd == "") {
                    return;
                }
                MSG_DEBUG("Clearing all files in work directory " << wd);
695
                std::string cache_path = MESSAGE(wd << '/'<< ensure(target)->name << ".cache");
696
                nftw(cache_path.c_str(), clear_cache, 10, FTW_DEPTH);
Damien Leroux's avatar
Damien Leroux committed
697
698
699
700
701
702
703
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-a", "--ansi"},
            {},
            "Use ANSI escape sequences to display colors and realtime progress information at the top of the terminal",
            false,
704
            {"enabled if output on a terminal"},
Damien Leroux's avatar
Damien Leroux committed
705
706
707
708
709
710
711
712
            [](CALLBACK_ARGS) {
                msg_handler_t::set_color(1);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
        {{"-na", "--no-ansi"},
            {},
            "Don't use ANSI escape sequences, don't display colors or realtime progress information",
            false,
713
            {"see -a"},
Damien Leroux's avatar
Damien Leroux committed
714
715
716
717
            [](CALLBACK_ARGS) {
                msg_handler_t::set_color(0);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
718

Damien Leroux's avatar
Damien Leroux committed
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
        {{"-rj", "--join-radius"},
            {"distance"},
            "Specify the maximum distance from a selected locus to compute joint probabilities. TODO: fix this doc.",
            false,
            {&settings_t::Rjoin},
            [](CALLBACK_ARGS)
            {
                ensure(target)->Rjoin = to<double>(*++ai);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-rs", "--skip-radius"},
            {"distance"},
            "Specify the maximum distance from a selected locus to skip tests. TODO: fix this doc.",
            false,
            {&settings_t::Rskip},
            [](CALLBACK_ARGS)
            {
                ensure(target)->Rskip = to<double>(*++ai);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},


Damien Leroux's avatar
Damien Leroux committed
742
#if 0
743
744
745
746
747
748
749
750
        {{"-ro", "--RIL-order"},
            {"order"},
            "Specify the precision for RIL populations (order of the Taylor series).",
            false,
            {&RIL_ORDER},
            [](CALLBACK_ARGS)
            {
                int order = to<int>(*++ai);
751
752
                if (order <= 1 || order > 25) {
                    MSG_ERROR("Invalid value for order: " << order, "The order parameter must be an integer between 2 and 25, both included.");
753
754
755
756
757
                } else {
                    RIL_ORDER = order;
                }
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
758
759
760
761
762
763
764
765
766

        {{"-m8o", "--MAGIC-8-order"},
            {"order"},
            "Specify the precision for MAGIC-8 populations (order of the Taylor series).",
            false,
            {&MAGIC8_ORDER},
            [](CALLBACK_ARGS)
            {
                int order = to<int>(*++ai);
767
768
                if (order <= 1 || order > 25) {
                    MSG_ERROR("Invalid value for order: " << order, "The order parameter must be an integer between 2 and 25, both included.");
769
770
771
772
773
                } else {
                    MAGIC8_ORDER = order;
                }
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
Damien Leroux's avatar
Damien Leroux committed
774
#endif
Damien Leroux's avatar
Damien Leroux committed
775
776
    }},

777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//    {"Input files", "The following are used to specify the various input files. They are ALL compulsory.", false, {
//        {{"-gm", "--genetic-map"},
//            {"path"},
//            "Path to the genetic map file",
//            false,
//            {true},
//            [](CALLBACK_ARGS)
//            {
//                ensure(target)->map_filename = *++ai;
//                ifile ifs(ensure(target)->map_filename);
//                ensure(target)->map = read_data::read_map(ifs);
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
//
//#if 0
//        {{"-bds", "--breeding-design"},
//            {"path"},
//            "Path to the breeding design specification file",
//            false,
//            {true},
//            [](CALLBACK_ARGS)
//            {
//                std::string filename = *++ai;
//                check_file(filename, false, false);
//                /*ensure(target)->design_filename = *++ai;*/
//                ifile ifs(filename);
//                ensure(target)->design = read_design(filename, ifs);
//                if (ensure(target)->design) {
//                    ensure(target)->design->filename = filename;
//                } else {
//                    MSG_ERROR("Couldn't read breeding design file", "");
//                }
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
//
//        {{"-ot", "--observation-type"},
//            {"type"},
//            "Specify how to interpret the observation data.\nIf type is 'line'" /* TODO */,
//            false,
//            {&settings_t::observation_type},
//            [](CALLBACK_ARGS)
//            {
//                /* TODO */
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
//
//        {{"-mos", "--marker-observations-spec"},
//            {"path"},
//            "Path to the marker observation specification file",
//            false,
//            {true},
//            [](CALLBACK_ARGS)
//            {
//                std::string filename = *++ai;
//                check_file(filename, false, false);
//                /*ensure(target)->marker_observation_specs_filename = *++ai;*/
//                ifile ifs(filename);
//                /*ensure(target)->marker_observation_specs = read_format(filename, ifs);*/
//                read_format(ensure(target)->marker_observation_specs, filename, ifs);
//                ensure(target)->marker_observation_specs_filenames.push_back(filename);
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
//#endif
//    }},
Damien Leroux's avatar
Damien Leroux committed
841

842
    {"Input datasets", "The following specify the datasets you want processed.\nA dataset specification starts with argument -p, followed by one or more arguments -m.\nArguments -l, -e, and -z are non-requisite and may appear anywhere after -p.", false, {
843
844
845
846
847
848
849
850
851
852
853
854
855
        {{"-gm", "--genetic-map"},
            {"path"},
            "Path to the genetic map file",
            false,
            {true},
            [](CALLBACK_ARGS)
            {
                ensure(target)->map_filename = *++ai;
                ifile ifs(ensure(target)->map_filename);
                ensure(target)->map = read_data::read_map(ifs);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

Damien Leroux's avatar
Damien Leroux committed
856
        {{"-p", "--population"},
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
         {"QTL generation name", "data file path"},
         "Specify a new population (dataset) to work on.",
         false,
         {true},
         [](CALLBACK_ARGS)
         {
             std::string qtl_gen_name = *++ai;
             std::string observed_traits_filename = *++ai;
             ensure(target)->datasets.emplace_back(qtl_gen_name, observed_traits_filename);
             SAFE_IGNORE_CALLBACK_ARGS;
         }},

//        {{"-c", "--covariable"},
//         {"QTL generation name", "data file path"},
//         "Specify a new covariable or set of covariables to use as static column(s) in the model. The data file format is the same as the traits file format.",
//         false,
//         {true},
//         [](CALLBACK_ARGS)
//         {
//             std::string qtl_gen_name = *++ai;
//             std::string observed_traits_filename = *++ai;
//             ensure(target)->covariables.emplace_back(qtl_gen_name, observed_traits_filename);
//             SAFE_IGNORE_CALLBACK_ARGS;
//         }},
881
#if 0
Damien Leroux's avatar
Damien Leroux committed
882
        {{"-m", "--observed-markers"},
883
884
            {"gen:format", "path"},
            "Path to the marker observations file of generation 'gen' with given format for the last declared population",
Damien Leroux's avatar
Damien Leroux committed
885
            false,
886
            {true},
Damien Leroux's avatar
Damien Leroux committed
887
888
889
890
            [](CALLBACK_ARGS)
            {
                population& p = last_pop(ensure(target));
                std::string gen_and_format = *++ai;
891
                std::string filename(*++ai);
Damien Leroux's avatar
Damien Leroux committed
892
893
894
                size_t colon = gen_and_format.find_first_of(':');
                if (colon == std::string::npos) {
                    MSG_ERROR("No format was specified for the observations of generation " << gen_and_format << " in population " << p.name, "Specify observations format name along with the generation");
895
                    return;
Damien Leroux's avatar
Damien Leroux committed
896
897
898
                }
                std::string gen_name(gen_and_format.cbegin(), gen_and_format.cbegin() + colon);
                std::string format_name(gen_and_format.cbegin() + 1 + colon, gen_and_format.cend());
Damien Leroux's avatar
Damien Leroux committed
899
900
901
902
903
904
905
906
907
                if (!ensure(target)->design->generation[gen_name]) {
                    MSG_ERROR("Generation '" << gen_name << "' isn't defined.", "Specify a generation name that is defined in the breeding design XML file");
                }
                if (0) {
                    const auto& mos = ensure(target)->marker_observation_specs;
                    if (mos.find(format_name) == mos.end()) {
                        MSG_ERROR("Marker observation format '" << format_name << "' isn't defined.", "");
                    }
                }
Damien Leroux's avatar
Damien Leroux committed
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
                size_t colon1 = filename.find_first_of(':');
                int start = -1;
                int end = -1;
                if (colon1 != std::string::npos) {
                    size_t colon2 = filename.find_first_of(':', colon1 + 1);
                    if (colon2 != std::string::npos) {
                        std::stringstream s1(filename.substr(colon1 + 1, colon2 - colon1 - 1));
                        std::stringstream s2(filename.substr(colon2 + 1));
                        s1 >> start;
                        s2 >> end;
                    } else {
                        std::stringstream s1(filename.substr(colon1 + 1));
                        s1 >> start;
                        end = start;
                    }
                    filename.resize(colon1);
                }
Damien Leroux's avatar
Damien Leroux committed
925
                if (check_file(filename, false, false)) {
926
                    ifile ifs(filename);
Damien Leroux's avatar
Damien Leroux committed
927
928
929
                    p.observed_mark[gen_name] = {filename, format_name, 0,
                        read_data::read_marker(ifs, start, end), {}};
                }
Damien Leroux's avatar
Damien Leroux committed
930
931
932
933
934
935
936
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-z", "--noise"},
            {"value"},
            "Noise value for the marker observations in [0,0.5]",
            false,
937
            {&population::noise},
Damien Leroux's avatar
Damien Leroux committed
938
939
940
941
942
943
944
945
946
947
            [](CALLBACK_ARGS)
            {
                last_pop(ensure(target)).noise = to<double>(*++ai);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

        {{"-e", "--pedigree"},
            {"path"},
            "Path to the pedigree file for the last declared population",
            false,
948
            {false},
Damien Leroux's avatar
Damien Leroux committed
949
950
951
            [](CALLBACK_ARGS)
            {
                std::string filename(*++ai);
952
                ifile ifs(filename);
Damien Leroux's avatar
Damien Leroux committed
953
954
955
956
                last_pop(ensure(target)).pedigree
                    = read_pedigree(ensure(target)->design, filename, ifs);
                SAFE_IGNORE_CALLBACK_ARGS;
            }},
957
#endif
958
959
960
961
962
963
964
965
966
967
968
969
970
//        {{"-l", "--ld"},
//            {"generation_name", "path"},
//            "Path to the LD file for the last declared population",
//            false,
//            {false},
//            [](CALLBACK_ARGS)
//            {
//                std::string qtl_gen(*++ai);
//                std::string filename(*++ai);
//                ifile ifs(filename);
//                read_ld(ensure(target), qtl_gen, filename, ifs);
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
Damien Leroux's avatar
Damien Leroux committed
971
972
973
974
975
976
977
    }},

    {"Model options", "The following configures the construction of the linear model.", false, {
        {{"connected"},
            {},
            "Select connected mode",
            false,
978
            {"disconnected"},
Damien Leroux's avatar
Damien Leroux committed
979
980
981
982
983
984
            [](CALLBACK_ARGS)
            {
                ensure(target)->connected = true;
                SAFE_IGNORE_CALLBACK_ARGS;
            }},

985
986
987
988
989
990
991
992
993
994
//        {{"max-order"},
//            {"order"},
//            "Maximum block order allowed in the model",
//            false,
//            {1},
//            [](CALLBACK_ARGS)
//            {
//                ensure(target)->max_order = to<size_t>(*++ai);
//                SAFE_IGNORE_CALLBACK_ARGS;
//            }},
995

996
997
998
999
1000
//        {{"epistasis"},
//            {},
//            "Detect epistasis",
//            false,
//            {"don't detect epistasis"},