main.cc 4.51 KB
Newer Older
Damien Leroux's avatar
Damien Leroux committed
1
#include "pedigree.h"
2
3
#include "bayes/factor_var3.h"
#include "bayes/output.h"
Damien Leroux's avatar
Damien Leroux committed
4
#include "cli.h"
5
#include "dispatch.h"
Damien Leroux's avatar
Damien Leroux committed
6
7
8
9
10
11

void print_usage();


int main(int argc, const char** argv)
{
12
    msg_handler_t::debug_enabled() = false;
13
14
15
16
17
18
19
    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
20
21
22
    if (!settings) {
        print_usage();
    } else {
23
24
25
26
27
28
        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]);
        }

29
        /*
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        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;
50
        */
51
52
53
54
55
56
57
58

        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
59
        /*MSG_DEBUG("MARKER NAMES " << settings->marker_names);*/
60
61
62
63
64
65

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

        if (settings->is_master()) {
66
            if (do_the_job(settings, "compute-alleles-per-marker")) {
67
68
69
70
71
                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) {
72
                    ifile result(settings->job_filename("compute-alleles-per-marker", mark));
73
74
75
76
77
78
79
80
                    /*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());
81
                {
82
                    ofile unafs(settings->job_filename("unique_n_alleles", 0));
83
84
                    rw_base()(unafs, settings->unique_n_alleles);
                }
85
                MSG_DEBUG("Computed n_alleles: " << settings->unique_n_alleles);
86

Damien Leroux's avatar
Damien Leroux committed
87
88
89
                if (do_the_job(settings, "compute-factor-graphs")
                        && do_the_job(settings, "compute-LV")
                        && do_the_job(settings, "collect-LV")) {
90
                    ifile output(settings->job_filename("output", 0));
Damien Leroux's avatar
Damien Leroux committed
91
92
93
94
95
                    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();
                }
96
            }
97
98
99
            /*do_the_job(settings, "dummy-one");*/
            /*do_the_job(settings, "dummy");*/
            /*do_the_job(settings, "dummy-one");*/
100
            settings->cleanup_job_files();
101
        } else {
102
            (void) do_the_job(settings, settings->job_name);
103
104
        }

Damien Leroux's avatar
Damien Leroux committed
105
106
107
108
109
110
        delete settings;
    }
    return 0;
}