basic_data.cc 11.9 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/>.
 */

18
19
20
21
22
23
24
25
//#include "input.h"
//#include <mutex>
#include "eigen.h"
#include "input.h"
//#include "data/geno_matrix.h"
#include "cache/md5.h"
#include "computations/base.h"
#include "cache2.h"
Damien Leroux's avatar
Damien Leroux committed
26
#include "computations/basic_data.h"
27
#include "io/output_impl.h"
Damien Leroux's avatar
Damien Leroux committed
28
29


30
31
32
33
collection<std::string>
all_traits()
{
    collection<std::string> ret;
34
35
    for (auto& kv: active_settings->linked_pops) {
        ret.emplace_back(kv.first);
36
37
38
39
40
    }
    return ret;
}


Damien Leroux's avatar
Damien Leroux committed
41
42
43
44
45
46
47
48
49
50
51
52
53
std::vector<std::pair<const chromosome*, double>>
selected_qtls()
{
    std::vector<std::pair<const chromosome*, double>> ret;
    for (auto& qc: active_settings->working_set) {
        for (auto& l: qc.qtl) {
            ret.push_back({qc.chr, l});
        }
    }
    return ret;
}


54
#if 0
Damien Leroux's avatar
Damien Leroux committed
55
56
57
58
59
60
61
62
63
64
population_value
population_by_name(const std::string& name)
{
    for (auto& kv: active_settings->populations) {
        if (kv.first == name) {
            return &kv.second;
        }
    }
    return NULL;
}
65
#endif
Damien Leroux's avatar
Damien Leroux committed
66

67
68
69
70
collection<population_value> all_populations()
{
	collection<population_value> ret;
	ret.reserve(active_settings->populations.size());
71
72
73
	for (auto& p: active_settings->populations) {
		/*ret.push_back({&kv.second});*/
        ret.push_back(p.get());
74
75
76
	}
	return ret;
}
Damien Leroux's avatar
Damien Leroux committed
77
78
79
80
81
82
83
84

std::vector<double>
selected_qtls_on_chromosome(qtl_chromosome_value chr)
{
    return chr->qtl;
}


85
#if 0
Damien Leroux's avatar
Damien Leroux committed
86
87
88
generation_value
generation_by_name(const std::string& name)
{
89
90
91
    /* FIXME */
    return NULL;
    /*return active_settings->design->generation[name];*/
Damien Leroux's avatar
Damien Leroux committed
92
93
94
95
96
97
}


generation_value
qtl_generation(const population_value& pop)
{
98
99
100
    /* FIXME */
    return NULL;
    /*return generation_by_name(pop->qtl_generation_name);*/
Damien Leroux's avatar
Damien Leroux committed
101
}
102
#else
Damien Leroux's avatar
Damien Leroux committed
103

104
generation_value
105
generation_by_name(const population_value& pop, const std::string& /*name*/)
106
{
Damien Leroux's avatar
Damien Leroux committed
107
    return pop->gen.get();
108
109
110
111
112
}

generation_value
qtl_generation(const population_value& pop)
{
Damien Leroux's avatar
Damien Leroux committed
113
    return pop->gen.get();
114
115
116
}

#endif
Damien Leroux's avatar
Damien Leroux committed
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

chromosome_value
chromosome_by_name(const std::string& name)
{
    for (auto& chr: active_settings->map) {
        if (chr.name == name) {
            return &chr;
        }
    }
    return NULL;
}


qtl_chromosome_value
qtl_chromosome_by_name(const std::string& name)
{
    for (auto& chr: active_settings->working_set) {
        if (chr.chr->name == name) {
            return &chr;
        }
    }
    return NULL;
}


collection<std::string>
143
all_observed_generation_names(population_value /*pop*/)
Damien Leroux's avatar
Damien Leroux committed
144
145
{
    collection<std::string> ret;
146
147
148
149
    /* FIXME */
    /*for (auto& kv: pop->marker_observation_filenames) {*/
        /*ret.emplace_back(kv.first);*/
    /*}*/
Damien Leroux's avatar
Damien Leroux committed
150
151
152
153
    return ret;
}


154
#if 0
155
/* DEPRECAITIDE */
Damien Leroux's avatar
Damien Leroux committed
156
157
158
159
160
std::vector<char>
marker_observations(population_value pop, chromosome_value chr, generation_value gen, int ind)
{
    return pop->get_observed_mark(gen->name)
            .observations
161
            .get_obs(chr->raw.marker_name.begin(), chr->raw.marker_name.end(), ind);
Damien Leroux's avatar
Damien Leroux committed
162
163
164
165
166
167
168
169
170
}


collection<double>
test_loci(chromosome_value chr)
{
    std::vector<double> steps = compute_steps(chr->marker_locus, active_settings->step);
    return collection<double>(steps.begin(), steps.end());
}
171
172
173
174
175
#endif

value<std::vector<double>>
test_loci(chromosome_value chr)
{
176
    return compute_steps(chr->condensed.marker_locus, active_settings->step);
177
}
Damien Leroux's avatar
Damien Leroux committed
178
179


180
#if 0
Damien Leroux's avatar
Damien Leroux committed
181
segment_computer_t
Damien Leroux's avatar
Damien Leroux committed
182
183
genoprob_computer(population_value pop, generation_value gen, qtl_chromosome_value qtl_chr)
{
Damien Leroux's avatar
Damien Leroux committed
184
185
186
    /*return gen->segment_computer(qtl_chr, 0, active_settings->step);*/
    segment_computer_t ret(*gen, qtl_chr, active_settings->step, 0.);
    return ret;
Damien Leroux's avatar
Damien Leroux committed
187
188
189
190
191
192
}


marker_observation_spec
marker_observation_specifications(generation_value gen)
{
193
    return active_settings->marker_observation_specs[gen->name];
Damien Leroux's avatar
Damien Leroux committed
194
}
195
#endif
Damien Leroux's avatar
Damien Leroux committed
196

197
198
199
200
201
/*observation_vectors_type*/
/*get_observation_vectors(generation_value gen)*/
/*{*/
        /*return gen->observation_vectors(marker_observation_specifications(gen));*/
/*}*/
Damien Leroux's avatar
Damien Leroux committed
202
203


204
205
206
207
208
/*pedigree_type*/
/*pedigree(population_value pop, generation_value gen, int ind)*/
/*{*/
    /*return pop->get_pedigree(gen, ind);*/
/*}*/
Damien Leroux's avatar
Damien Leroux committed
209
210
211
212
213


range<int>
individual_range(population_value pop)
{
214
215
216
217
218
219
220
221
222
223
    /*auto it = pop->families.find(pop->qtl_generation_name);*/
    /*if (it == pop->families.end()) {*/
        /*return {0, 0, 0};*/
    /*}*/
    return {0, (int) pop->size(), 1};
    /*auto it = pop->observed_mark.find(pop->qtl_generation_name);*/
    /*if (it == pop->observed_mark.end()) {*/
        /*return {0, 0, 0};*/
    /*}*/
    /*return {0, (int)it->second.observations.n_obs, 1};*/
Damien Leroux's avatar
Damien Leroux committed
224
225
226
}


227
MatrixXd
228
229
population_marker_obs(const context_key& ck, int ind)
{
230
    /*return {ck->pop, ck->chr, ck->gen, ind};*/
231
232
    /*return active_settings->LV(ck->chr->name, ck->pop->qtl_generation_name, ind);*/
    return ck->pop->get_LV(ck->chr->name, ind);
233
234
235
}

#if 0
236
MatrixXd
237
238
239
240
241
population_marker_obs(population_value pop, chromosome_value chr, int i, const pop_mgo_data* pmd)
{
    return {pop, chr, pmd->qtl_gen, i};
}

242
MatrixXd
243
244
245
246
247
248
population_marker_obs(population_value pop, chromosome_value chr, const pedigree_type& ped, const pop_mgo_data* pmd)
{
    /*generation_value qtl_gen = generation_by_name(pop->qtl_generation_name);*/
    /*collection<std::string> aogn = all_observed_generation_names(pop);*/
    /*collection<generation_value> aog = make_collection(generation_by_name, aogn);*/
    /*collection<observation_vectors_type> aov = make_collection(get_observation_vectors, aog);*/
249
    MatrixXd mgo(chr->marker_locus.size(), pmd->qtl_gen);
250
251
252
253
    for (size_t i = 0; i < pmd->aogn.size(); ++i) {
        auto mo = make_value<Mem>(marker_observations, pop, chr, *pmd->aog[i], ped[i]);
        mgo[*pmd->aog[i]]
            = (*pmd->aog[i])->raw_observations(*pmd->aov[i], *mo);
Damien Leroux's avatar
Damien Leroux committed
254
    }
255
    pmd->qtl_gen->update_locus_vectors(mgo);
Damien Leroux's avatar
Damien Leroux committed
256
257
    return mgo;
}
258
#endif
Damien Leroux's avatar
Damien Leroux committed
259

260
261
262
263
264
265
266
267
268
269
270
stpom_data
compute_state_to_parental_origin_haplo(const context_key& ck, const locus_key& lk);

value<stpom_data>
get_stpom_data(const context_key& ck, const locus_key& lk)
{
    return make_value<Sync|Mem>(compute_state_to_parental_origin_haplo,
                                value<context_key>{ck}, value<locus_key>{lk});
}


271
272
273
274
275
276
277
278
int
get_n_parents(const context_key& ck, const locus_key& lk)
{
    return (int) get_stpom_data(ck, lk)->row_labels.size();
}



Damien Leroux's avatar
Damien Leroux committed
279
280
281
MatrixXb
get_contrast_groups(generation_value gen, const locus_key& lk)
{
282
283
    /* FIXME! */
#if 1
Damien Leroux's avatar
Damien Leroux committed
284
285
286
287
    context_key ck(new context_key_struc(gen));
    value<stpom_data> sd = get_stpom_data(ck, lk->parent);
    MatrixXb ret(2, sd->haplo1.innerSize());
    MatrixXb collapse = MatrixXb::Ones(sd->haplo1.outerSize(), 1);
288
289
290
    /*MSG_DEBUG((*sd));*/
    /*MSG_DEBUG(MATRIX_SIZE(ret));*/
    /*MSG_DEBUG(MATRIX_SIZE(collapse) << std::endl << collapse);*/
Damien Leroux's avatar
Damien Leroux committed
291
292
293
294
    (void)*sd;
    ret.row(0) = (sd->haplo1 * collapse).transpose();
    ret.row(1) = (sd->haplo2 * collapse).transpose();
    /*MSG_DEBUG("CONTRAST GROUPS " << gen->name << std::endl << ret);*/
295
296
297
#else
    MatrixXb ret;
#endif
Damien Leroux's avatar
Damien Leroux committed
298
299
300
301
302
303
    return ret;
}




Damien Leroux's avatar
Damien Leroux committed
304
labelled_matrix<MatrixXd, std::vector<char>, label_type>
305
306
307
308
309
compute_state_to_parental_origin(const context_key& ck, const locus_key& lk)
{
    value<stpom_data>
        sd = get_stpom_data(ck, lk);

Damien Leroux's avatar
Damien Leroux committed
310
    labelled_matrix<MatrixXd, std::vector<char>, label_type>
311
312
313
        ret(sd->row_labels, sd->col_labels);

    ret.data = sd->haplo1.cast<double>() + sd->haplo2.cast<double>();
Damien Leroux's avatar
Damien Leroux committed
314
    /*MSG_DEBUG("stfopom " << MATRIX_SIZE(ret));*/
315
316
317
318
    return ret;
}


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
labelled_matrix<MatrixXd, std::vector<char>, label_type>
compute_state_to_dominance(const context_key& ck, const locus_key& lk)
{
    auto stdom = compute_state_to_parental_origin(ck, lk);
    std::map<std::vector<char>, VectorXd> dominance_vectors;
    int nrows = stdom.rows();
    for (int r1 = 0; r1 < nrows; ++r1) {
        for (int r2 = r1 + 1; r2 < nrows; ++r2) {
            VectorXd prod = (stdom.data.row(r1).array() * stdom.data.row(r2).array()).matrix();
            if (prod.sum() > 0) {
                std::vector<char> label;
                label.push_back('(');
                label.insert(label.end(), stdom.row_labels[r1].begin(), stdom.row_labels[r1].end());
                label.push_back('/');
                label.insert(label.end(), stdom.row_labels[r2].begin(), stdom.row_labels[r2].end());
                label.push_back(')');
                dominance_vectors[label] = prod;
            }
        }
    }

    stdom.row_labels.clear();
    stdom.row_labels.reserve(dominance_vectors.size());
    stdom.data.resize(dominance_vectors.size(), stdom.data.cols());
    for (const auto& lv: dominance_vectors) {
        stdom.data.row(stdom.row_labels.size()) = lv.second;
        stdom.row_labels.emplace_back(lv.first);
    }

348
    /*MSG_DEBUG("Computed stdom matrix" << std::endl << stdom);*/
349
350
351
352
353

    return stdom;
}


354
355
356
stpom_data
compute_state_to_parental_origin_haplo(const context_key& ck, const locus_key& lk)
{
357
358
359
    /*const impl::generation_rs* gen = ck->gen;*/
    /* FIXME! */
    /*const impl::generation_rs* gen = NULL;*/
Damien Leroux's avatar
Damien Leroux committed
360
    const geno_matrix* gen = ck->pop ? ck->pop->gen.get() : ck->gen;
361
    stpom_data sd;
362
363
    /*MSG_DEBUG("Computing stfopom order=" << lk->depth() << " gen@" << gen);*/
    /*MSG_DEBUG_INDENT;*/
364
    /*MSG_DEBUG((*gen));*/
365
366
    if (!(lk && lk->locus != locus_key_struc::no_locus)) {
        std::set<char> parents;
Damien Leroux's avatar
Damien Leroux committed
367
368
369
370
        const std::vector<label_type>& ap_labels = gen->get_unique_labels();
        for (const label_type& ap: ap_labels) {
            parents.insert(ap.first());
            parents.insert(ap.second());
371
372
373
374
375
376
377
378
379
380
381
382
383
384
        }
        std::vector<std::vector<char>> parent_labels;
        std::map<char, int> parent_index;
        parent_labels.reserve(parents.size());
        for (char c: parents) {
            parent_labels.push_back({c});
            size_t sz = parent_index.size();
            parent_index[c] = sz;
        }
        sd.row_labels.assign(parent_labels.begin(), parent_labels.end());
        sd.col_labels.assign(ap_labels.begin(), ap_labels.end());
        sd.haplo1 = MatrixXb::Zero(parent_labels.size(), ap_labels.size());
        sd.haplo2 = MatrixXb::Zero(parent_labels.size(), ap_labels.size());
        for (size_t i = 0; i < ap_labels.size(); ++i) {
Damien Leroux's avatar
Damien Leroux committed
385
386
            sd.haplo1(parent_index[ap_labels[i].first()], i) = 1;
            sd.haplo2(parent_index[ap_labels[i].second()], i) = 1;
387
        }
388
389
        /*MSG_DEBUG("prout");*/
        /*MSG_DEBUG(sd);*/
390
391
392
393
    } else {
        value<stpom_data> first = get_stpom_data(ck, NULL);
        value<stpom_data> pred = get_stpom_data(ck, lk->parent);

Damien Leroux's avatar
Damien Leroux committed
394
        sd.col_labels.reserve(gen->size()
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
                              * pred->col_labels.size());
        sd.row_labels.reserve(pred->row_labels.size()
                              * first->row_labels.size());

        for (auto& pred_par_lab: pred->row_labels) {
            for (auto& row: first->row_labels) {
                sd.row_labels.push_back(pred_par_lab);
                sd.row_labels.back().push_back(row.front());
            }
        }

        size_t sz = gen->get_unique_labels().size();
        for (size_t i = 0; i < sz; ++i) {
            sd.col_labels.insert(sd.col_labels.end(), pred->col_labels.begin(), pred->col_labels.end());
        }

        /* NB : c'est d'une puissance... */

        sd.haplo1 = kroneckerProduct(pred->haplo1, first->haplo1);
        sd.haplo2 = kroneckerProduct(pred->haplo2, first->haplo2);
    }

417
418
    /*MSG_DEBUG("stfopom " << lk);*/
    /*MSG_DEBUG(sd);*/
419

420
    /*MSG_DEBUG_DEDENT;*/
421
    /*MSG_QUEUE_FLUSH();*/
422
423
424

    return sd;
}