lumping2.h 13.6 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
26
27
#ifndef _SPEL_LUMPERJACK_H_
#define _SPEL_LUMPERJACK_H_

/*#include <boost/dynamic_bitset.hpp>*/
/*#include <Eigen/Jacobi>*/
/*#include <Eigen/IterativeLinearSolvers>*/
#include <deque>
#include <set>
#include <unordered_map>
#include <map>
28
/*#include "algebraic_genotype.h"*/
29
30
31
32
33
34
35
36
37
#include <iostream>
#include "chrono.h"

/*typedef boost::dynamic_bitset<> bitset;*/
/*typedef std::set<bitset> Partition;*/

/*typedef std::set<int> subset;*/
typedef std::vector<int> subset;

38
#if 0
39
40
41
42
43
44
45
46
47
48
49
#define _EPSILON (1.e-10)

namespace std {
    template <>
        struct less<double> {
            bool operator () (double a, double b) const
            {
                return a < b && abs(a - b) > _EPSILON;
            }
        };
}
50
#endif
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

#if 0
template <typename K, typename V, typename H=std::hash<K>, typename A=std::allocator<std::pair<const K, V>>>
using lumper_map = std::unordered_map<K, V, H, std::equal_to<K>, A>;
#else
template <typename K, typename V, typename C=std::less<K>, typename A=std::allocator<std::pair<const K, V>>>
using lumper_map = std::map<K, V, C, A>;
#endif

inline std::ostream& operator << (std::ostream& os, const subset& s)
{
    os << '{';
    auto cur = s.begin(), end = s.end();
    if (cur != end) {
        os << (*cur);
        for (++cur; cur != end; ++cur) {
            os << ' ' << (*cur);
        }
    }
    return os << '}';
}


inline std::ostream& operator << (std::ostream& os, const std::set<subset>& S)
{
    for (auto s: S) {
Damien Leroux's avatar
Damien Leroux committed
77
        os << s << " #" << s.size() << std::endl;
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
    }
    return os;
}

template <typename K>
std::ostream& operator << (std::ostream& os, const lumper_map<K, subset>& km)
{
    std::string sep = "";
    os << '{';
    for (const auto& kv: km) {
        os << sep << kv.first << " => " << kv.second;
        if (sep.size() == 0) { sep = ", "; }
    }
    return os << '}';
}

template <typename scalar_type>
struct exact_compare {
    typedef scalar_type key_type;
    bool operator () (const scalar_type& s1, const scalar_type& s2) const
    {
        return s1 < s2;
    }

    template <typename V, typename COMP, typename A>
        static V& get(lumper_map<key_type, V, COMP, A>& m, const scalar_type& k)
        {
            return m[k];
        }

    template <typename V, typename COMP, typename A>
        static lumper_map<key_type, V, COMP, A>
        postprocess(const lumper_map<key_type, V, COMP, A>& m) { return m; }
};


#define LUMP_BY_COLS

/*#define OLD_IMPL_WITH_ALGEBRAIC_GENOTYPES*/


/*struct fp_comp {*/
    /*bool operator () (fast_polynom p1, fast_polynom p2) const { return p1.value < p2.value; }*/
/*};*/

template <typename MATRIX_TYPE, typename LABEL_TYPE>
struct lumper {
    typedef LABEL_TYPE label_type;
    typedef typename MATRIX_TYPE::Scalar scalar_type;
    typedef lumper_map<scalar_type, subset> key_map;
    const MATRIX_TYPE& G;
    const std::vector<label_type>& labels;
    bool exact;
    chrono pol, cs, ck, tr, tm;

    lumper(const MATRIX_TYPE& x, const std::vector<label_type>& labs, bool e=false)
        : G(x), labels(labs), exact(e)
        , pol(), cs(), ck(), tr(), tm()
    {}

    scalar_type compute_sum(size_t s, const subset& B)
    {
        cs.start();
        scalar_type accum = scalar_type();
        /*fast_polynom accum = fast_polynom::zero;*/
        /*std::cout << "accum = " << accum << std::endl;*/
        /*for (size_t j = B.find_first(); j != subset::npos; j = B.find_next(j)) {*/
        for (int j: B) {
            /*std::cout << "accum + " << G.data(i, j);*/
#ifdef LUMP_BY_COLS
            accum += G(j, s);
#else
            accum += G(s, j);
#endif
            /*std::cout << " = " << accum << std::endl;*/
        }
        cs.stop();
        return accum;
    }

    key_map compute_keys(const subset& C, const subset& B)
        {
            ck.start();
            /*lumper_map<scalar_type, subset> ret;*/
            key_map ret;
            /*for (size_t i = C.find_first(); i != subset::npos; i = C.find_next(i)) {*/
            for (int i: C) {
                scalar_type accum = compute_sum(i, B);
                /*if (ret[accum].size() == 0) {*/
                    /*ret[accum].resize(G.column_labels.size());*/
                /*}*/
                /*ret[accum].set(i);*/
                ret[accum].push_back(i);
                /*std::cout << "added " << accum << ", " << i << " to ";*/
                /*auto kv = ret.find(accum);*/
                /*if (kv == ret.end()) {*/
                    /*std::cout << "BUG!" << std::endl;*/
                /*} else {*/
                    /*std::cout << kv->first << ", " << kv->second << std::endl;*/
                /*}*/
            }
#if 0
            std::cout << std::endl << "Subsets " << C << " vs " << B << std::endl;
            for (auto kv: ret) {
                std::cout << kv.first << std::endl << "    " << kv.second << std::endl;
            }
#endif
            ck.stop();
            return ret;
        }

    std::vector<subset> partition_on_labels()
    {
        pol.start();
        std::vector<subset> ret;
        lumper_map<label_type, subset> tmp;
        for (size_t i = 0; i < labels.size(); ++i) {
            auto& l = labels[i];
            /*if (tmp[l].size() == 0) {*/
                /*tmp[l].resize(G.column_labels.size());*/
            /*}*/
            /*tmp[l].set(i);*/
            tmp[l]./*insert*/push_back(i);
        }
        ret.reserve(tmp.size());
        for (auto& kv: tmp) {
            /*std::cout << kv.first << "\t" << kv.second << std::endl;*/
            ret.push_back(kv.second);
        }
        pol.stop();
        return ret;
    }


    std::set<subset> try_refine(std::set<subset>& P)
    {
        tr.start();
        /*std::vector<const subset*> stack;*/
        std::deque<const subset*> stack;
        for (auto& x: P) {
            stack.push_back(&x);
        }
        while (stack.size()) {
221
            subset C = *stack.back();
222
223
224
            /*const subset& C = *stack.front();*/
            /*std::cout << "** " << C << std::endl;*/
            /*std::cout << '.' << std::flush;*/
Damien Leroux's avatar
Damien Leroux committed
225
            MSG_DEBUG("TACKLING " << C);
226
227
228
229
230
231
232
233
234
235
            stack.pop_back();
            /*stack.pop_front();*/
            if (C.size() <= 1) {
                continue;
            }
            for (auto& B: P) {
                /*if (B.size() <= 1) {*/
                    /*continue;*/
                /*}*/
                key_map subsets = compute_keys(C, B);
Damien Leroux's avatar
Damien Leroux committed
236
//                 MSG_DEBUG("KEYS: " << subsets);
237
238
239
240
241
242
                /*size_t size = 0;*/
                /*while (size != subsets.size()) {*/
                    /*size = subsets.size();*/
                /*}*/
                /* MARK */
                if (subsets.size() > 1) {
Damien Leroux's avatar
Damien Leroux committed
243
                    MSG_DEBUG("WE HAVE A SPLIT " << labels[C.front()] << '/' << labels[B.front()] << ": " << subsets);
244
245
246
247
                    P.erase(C);
                    for (auto& s: subsets) {
                        auto io = P.insert(s.second);
                        if (!io.second) {
Damien Leroux's avatar
Damien Leroux committed
248
                            MSG_DEBUG("PROUT SUBSET ALREADY IN P " << (*io.first));
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
                        }
                        if (io.first->size() > 1) {
                            stack.push_back(&*io.first);
                        }
                    }
                    break;
                }
            }
        }
        tr.stop();
        /*std::cout << std::endl;*/
        /*std::cout << P.size() << std::endl;*/
        /*std::cout << P << std::endl;*/
        /*MSG_DEBUG("P.size() = " << P.size());*/
        /*MSG_DEBUG("P = " << P);*/
        /*MSG_DEBUG("Lumping time: ck=" << ck.accum << " cs=" << cs.accum << " pol=" << pol.accum << " tr=" << tr.accum);*/
        return P;
    }


    std::set<subset> try_harder(std::set<subset>& P)
    {
        tr.start();
        /*std::vector<const subset*> stack;*/
        std::deque<const subset*> stack;
        for (auto& x: P) {
            stack.push_back(&x);
        }
        while (stack.size()) {
            const subset& C = *stack.back();
            /*const subset& C = *stack.front();*/
            /*std::cout << "** " << C << std::endl;*/
            /*std::cout << '.' << std::flush;*/
            /*MSG_DEBUG("TACKLING " << C);*/
            stack.pop_back();
            /*stack.pop_front();*/
            if (C.size() <= 1) {
                continue;
            }
            for (auto& B: P) {
                /*if (labels[B.front()] != labels[C.front()]) {*/
                    /*MSG_DEBUG("SKIP TEST BETWEEN " << C << " AND " << B);*/
                    /*continue;*/
                /*}*/
                /*if (B.size() <= 1) {*/
                    /*continue;*/
                /*}*/
                key_map subsets = compute_keys(C, B);
                /*MSG_DEBUG("KEYS: " << subsets);*/
                /*size_t size = 0;*/
                /*while (size != subsets.size()) {*/
                    /*size = subsets.size();*/
                /*}*/
                /* MARK */
                if (subsets.size() > 1) {
Damien Leroux's avatar
Damien Leroux committed
304
                    /*MSG_DEBUG("KEYS " << labels[subsets.begin()->second.front()] << ": " << subsets);*/
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
348
349
350
351
352
353
354
355
                    P.erase(C);
                    for (auto& s: subsets) {
                        auto io = P.insert(s.second);
                        if (!io.second) {
                            /*MSG_DEBUG("PROUT SUBSET ALREADY IN P " << (*io.first));*/
                        }
                        if (io.first->size() > 1) {
                            stack.push_back(&*io.first);
                        }
                    }
                    break;
                }
            }
        }
        tr.stop();
        /*std::cout << std::endl;*/
        /*std::cout << P.size() << std::endl;*/
        /*std::cout << P << std::endl;*/
        /*MSG_DEBUG("P.size() = " << P.size());*/
        /*MSG_DEBUG("P = " << P);*/
        /*MSG_DEBUG("Lumping time: ck=" << ck.accum << " cs=" << cs.accum << " pol=" << pol.accum << " tr=" << tr.accum);*/
        return P;
    }


    std::set<subset> refine_all()
    {
        std::vector<subset> tmp = partition_on_labels();
        std::set<subset> P(tmp.begin(), tmp.end());
        return try_refine(P);
    }

    MATRIX_TYPE to_matrix(const std::set<subset>& P, std::vector<label_type>& reduced_labels)
    {
        tm.start();
        reduced_labels.clear();
        reduced_labels.reserve(P.size());
        for (auto& S: P) {
            reduced_labels.push_back(labels[*S.begin()]);
        }
        MATRIX_TYPE ret(reduced_labels.size(), reduced_labels.size());
        size_t i = 0;
        size_t j;
        for (auto& C: P) {
            j = 0;
            for (auto& B: P) {
                /*algebraic_genotype accum = compute_sum(*C.begin(), B);*/
                /*accum.type = algebraic_genotype::Type::Genotype;*/
                /*accum.genotype = {reduced_labels[B.front()], reduced_labels[C.front()]};*/
                scalar_type accum = compute_sum(*C.begin(), B);

Damien Leroux's avatar
Damien Leroux committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#ifdef LUMP_BY_COLS
                ret(j, i) = accum;
#else
                ret(i, j) = accum;
#endif
                ++j;
            }
            ++i;
        }
        tm.stop();
        /*MSG_DEBUG("Created lumped matrix in " << tm.accum << " seconds");*/
        return ret;
    }


371
#if 0
Damien Leroux's avatar
Damien Leroux committed
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    scalar_type compute_sum(size_t s, const subset& B, const VectorXd& abundances)
    {
        cs.start();
        scalar_type accum = scalar_type();
        /*fast_polynom accum = fast_polynom::zero;*/
        /*std::cout << "accum = " << accum << std::endl;*/
        /*for (size_t j = B.find_first(); j != subset::npos; j = B.find_next(j)) {*/
        for (int j: B) {
            /*std::cout << "accum + " << G.data(i, j);*/
#ifdef LUMP_BY_COLS
            accum += G(j, s);
#else
            accum += G(s, j);
#endif
            /*std::cout << " = " << accum << std::endl;*/
        }
        cs.stop();
        return accum;
    }


    MATRIX_TYPE to_matrix(const std::set<subset>& P, const VectorXd& abundances, std::vector<label_type>& reduced_labels)
    {
        tm.start();
        reduced_labels.clear();
        reduced_labels.reserve(P.size());
        for (auto& S: P) {
            reduced_labels.push_back(labels[*S.begin()]);
        }
        MATRIX_TYPE ret(reduced_labels.size(), reduced_labels.size());
        size_t i = 0;
        size_t j;
        for (auto& C: P) {
            j = 0;
            for (auto& B: P) {
                /*algebraic_genotype accum = compute_sum(*C.begin(), B);*/
                /*accum.type = algebraic_genotype::Type::Genotype;*/
                /*accum.genotype = {reduced_labels[B.front()], reduced_labels[C.front()]};*/
                scalar_type accum = compute_sum(*C.begin(), B, abundances);

412
413
414
415
416
417
418
419
420
421
422
423
424
#ifdef LUMP_BY_COLS
                ret(j, i) = accum;
#else
                ret(i, j) = accum;
#endif
                ++j;
            }
            ++i;
        }
        tm.stop();
        /*MSG_DEBUG("Created lumped matrix in " << tm.accum << " seconds");*/
        return ret;
    }
425
#endif
426
427
428
429
430
};


#endif