33template <
typename Space,
typename Marker>
class Loader
36 using coord_type = Space;
37 using marker_type = Marker;
38 using allelic_state_type =
typename marker_type::value_type;
45 if (!is_readable(path))
47 throw std::string(
"In GeneticsLoader::read : unable to read file");
54 const unsigned int m_ploidy = 2;
55 std::vector<std::string> m_loci_names;
58 bool is_readable(std::string
const &path)
const
60 ifstream file(path.c_str());
65 void analyze(std::string
const &path)
67 std::ifstream file(path);
69 unsigned int current_line = 0;
71 while (std::getline(file, line))
73 if (current_line == 0)
75 m_loci_names = read_loci_names(erase_coordinates(line));
79 auto x = read_coordinates(line);
80 auto ind = read_individual(erase_coordinates(line));
87 std::string &erase_coordinates(std::string &line)
const
89 for (
int i = 0; i <= 1; ++i)
91 std::size_t found = line.find(
' ');
92 line.erase(0, ++found);
97 std::vector<std::string> read_loci_names(std::string
const &line)
const
99 std::vector<std::string> names;
100 std::istringstream iss(line);
102 while (std::getline(iss, elem,
' '))
104 names.push_back(elem);
106 assert(!names.empty());
110 coord_type read_coordinates(std::string
const &line)
const
114 std::istringstream iss(line);
116 unsigned int pos = 0;
117 while (std::getline(iss, elem,
' ') && pos <= 1)
121 lat = std::stod(elem);
125 lon = std::stod(elem);
129 coord_type x(lat, lon);
136 std::istringstream iss(line);
141 std::pair<allele_type, allele_type> alleles;
143 unsigned int i_locus = 0;
144 unsigned int i_allele = 0;
145 while (std::getline(iss, elem,
' '))
151 alleles.first = std::stod(elem);
154 else if (i_allele == 1)
157 alleles.second = std::stod(elem);
158 std::string locus_name = m_loci_names.at(i_locus);
159 individual.add(locus_name, alleles);