main.cc 4.62 KB
Newer Older
Damien Leroux's avatar
Damien Leroux committed
1
#include "pedigree.h"
2
/*#include "bayes/factor_var4.h"*/
3
#include "bayes/output.h"
Damien Leroux's avatar
Damien Leroux committed
4
#include "cli.h"
5
#include "dispatch.h"
6
#include <fenv.h>
Damien Leroux's avatar
Damien Leroux committed
7
8
9
10
11
12

void print_usage();


int main(int argc, const char** argv)
{
13
14
    feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);

15
    msg_handler_t::set_color(true);
16
    msg_handler_t::debug_enabled() = false;
17
18
19
20
21
22
23
    bn_settings_t* settings = NULL;
    try {
         settings = bn_settings_t::from_args(argc, argv);
    } catch (file::error& fe) {
        MSG_ERROR("An error happened while reading input files. " << fe.what(), "");
        return -1;
    }
Damien Leroux's avatar
Damien Leroux committed
24
25
26
    if (!settings) {
        print_usage();
    } else {
27
28
29
30
31
32
        std::map<std::string, ObservationDomain> obs_gen;
        for (const auto& kv: settings->observed_mark) {
            obs_gen[kv.first] = settings->marker_observation_specs[kv.second.format_name].domain;
            MSG_DEBUG("OBSERVED GEN " << kv.first << ": " << obs_gen[kv.first]);
        }

33
        /*
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        size_t n_alleles = 1;
        for (const auto& kv: settings->marker_observation_specs) {
            if (kv.second.domain == ODAllele) {
                if (n_alleles == 1) {
                    n_alleles = kv.second.domain_size;
                } else {
                    MSG_ERROR("Multiple and inconsistent formats (domain: allele)", "Consolidate your data and use at most ONE format per domain (alleles/ancestors) for your marker observations");
                }
            }
            settings->compiled_obs_specs[kv.first] = kv.second.compile();
        }
        for (const auto& kvfmt: settings->compiled_obs_specs) {
            MSG_DEBUG("COMPILED FORMAT " << kvfmt.first);
            for (const auto& kvscore: kvfmt.second) {
                MSG_DEBUG("  score '" << kvscore.first << "': " << kvscore.second.transpose());
            }
        }

        pedigree_bayesian_network bn = make_bn(pedigree, obs_gen, n_alleles, settings->noise, settings->tolerance);
        settings->bn = &bn;
54
        */
55
56
57
58
59
60
61
62

        std::set<std::string> tmp;
        for (const auto& obs: settings->observed_mark) {
            for (const auto& kv: obs.second.observations.data) {
                tmp.insert(kv.first);
            }
        }
        settings->marker_names.assign(tmp.begin(), tmp.end());
Damien Leroux's avatar
Damien Leroux committed
63
        /*MSG_DEBUG("MARKER NAMES " << settings->marker_names);*/
64
65
66
67
68
69

        for (size_t mn = 0; mn < settings->marker_names.size(); ++mn) {
            settings->marker_index[settings->marker_names[mn]] = mn;
        }

        if (settings->is_master()) {
70
            if (do_the_job(settings, "compute-alleles-per-marker")) {
71
72
73
74
75
                size_t n_mark = settings->count_markers();
                settings->alleles_per_marker.clear();
                settings->alleles_per_marker.resize(n_mark);
                std::set<size_t> unique_n_alleles;
                for (size_t mark = 0; mark < n_mark; ++mark) {
76
                    ifile result(settings->job_filename("compute-alleles-per-marker", mark));
77
78
79
80
81
82
83
84
                    /*auto& apm = settings->alleles_per_marker[mark];*/
                    std::set<char> temp;
                    rw_base()(result, temp);
                    unique_n_alleles.insert(temp.size());
                    /*MSG_DEBUG("Read an allele count of " << settings->alleles_per_marker[mark].size());*/
                    MSG_DEBUG("Read an allele count of " << temp.size());
                }
                settings->unique_n_alleles.assign(unique_n_alleles.begin(), unique_n_alleles.end());
85
                {
86
                    ofile unafs(settings->job_filename("unique_n_alleles", 0));
87
88
                    rw_base()(unafs, settings->unique_n_alleles);
                }
89
                MSG_DEBUG("Computed n_alleles: " << settings->unique_n_alleles);
90

Damien Leroux's avatar
Damien Leroux committed
91
92
93
                if (do_the_job(settings, "compute-factor-graphs")
                        && do_the_job(settings, "compute-LV")
                        && do_the_job(settings, "collect-LV")) {
94
                    ifile output(settings->job_filename("output", 0));
Damien Leroux's avatar
Damien Leroux committed
95
96
97
98
99
                    std::string filename;
                    rw_base() (output, filename);
                    CREATE_MESSAGE(msg_channel::Out, MESSAGE(std::endl << std::endl << GREEN << "Marker data for population `" << settings->pop_name << "' written in file " << WHITE << filename << NORMAL << std::endl));
                    MSG_QUEUE_FLUSH();
                }
100
            }
101
102
103
            /*do_the_job(settings, "dummy-one");*/
            /*do_the_job(settings, "dummy");*/
            /*do_the_job(settings, "dummy-one");*/
104
            settings->cleanup_job_files();
105
        } else {
106
            (void) do_the_job(settings, settings->job_name);
107
108
        }

Damien Leroux's avatar
Damien Leroux committed
109
110
111
112
113
114
        delete settings;
    }
    return 0;
}