read_trait.cc 3 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
#include "input.h"
damien's avatar
damien committed
19
#include "input/read_trait.h"
20
//#include "error.h"
21
#include "output_impl.h"
22
23
24


static
25
bool read_value(file& is, double& value)
26
27
28
29
30
31
32
33
34
35
36
{
    std::string tmp;
    is >> tmp;
    if (tmp == "-" || tmp == "NA") {
        return false;
    }
    std::stringstream(tmp) >> value;
    /*std::cout << "read " << tmp << " => " << value << std::endl;*/
    return true;
}

37
38

namespace read_data {
39
std::vector<trait> read_trait(file& is, const std::vector<std::string>& filter_names)
40
{
41
42
43
    if (!is.good()) {
        throw input_exception(is, "couldn't read from file");
    }
44
45
    std::vector<trait> ret;
    std::string s;
46
    /*double d;*/
47
    MSG_DEBUG("Reading traits amongst " << filter_names);
48
49
    is >> std::noskipws;
    while (!is.eof()) {
50
        std::string name = read_star_name(is);
51
        ret.emplace_back();
52
53
        ret.back().name = name;
//        MSG_DEBUG("Reading single_trait " << ret.back()->name);
54
        size_t counter = 0;
55
        std::vector<double> values;
56
        while (!(is.eof()||ws_opt_nl(is))) {
57
58
59
            /*is >> d;*/
            double val;
            if (read_value(is, val)) {
60
                values.push_back(val);
61
62
63
64
                ret.back().good_indices.push_back(counter);
            }
            ++counter;
        }
65
        if (ret.back().good_indices.size() == 0 || (!!filter_names.size() && std::find(filter_names.begin(), filter_names.end(), name) == filter_names.end())) {
66
            ret.pop_back();
67
68
69
70
71
72
        } else {
            ret.back().dim_names = {ret.back().name};
            ret.back().values.resize(values.size(), 1);
            ret.back().values = Eigen::Map<Eigen::Matrix<double, -1, -1, Eigen::ColMajor>>(values.data(), values.size(), 1);
            ret.back().raw = ret.back().values;
            ret.back().P = MatrixXd::Ones(1, 1);
73
        }
74
       MSG_DEBUG("... read " << counter << " values (" << (counter - ret.back().values.size()) << " missing)");
75
    }
76
    MSG_DEBUG("all traits " << ret);
77
78
79
80
    return ret;
}
} /* namespace read_data */

81
#ifdef TEST_TRAIT
82
/*#include "file.h"*/
83
84
85
86

int main(int argc, char** argv)
{
    for (int i = 1; i < argc; ++i) {
87
        ifile ifs(argv[i]);
88
89
90
91
92
93
94
        std::cout << read_data::read_trait(ifs) << std::endl;
    }
    return 0;
}

#endif