Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
SpatialGeneticSampleLoader.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __GENETICSLOADER_H_INCLUDED__
10#define __GENETICSLOADER_H_INCLUDED__
11
12#include <fstream>
13#include <iostream>
14#include <map>
15#include <sstream>
16#include <string>
17#include <vector>
18
19#include "Allele.hpp"
20#include "DiploidIndividual.hpp"
21#include "SpatialGeneticSample.hpp"
22#include "marker_traits.hpp"
23
24using std::cerr;
25using std::ifstream;
26using std::string;
27
28namespace quetzal::format
29{
30namespace genetics
31{
32
33template <typename Space, typename Marker> class Loader
34{
35 public:
36 using coord_type = Space;
37 using marker_type = Marker;
38 using allelic_state_type = typename marker_type::value_type;
42
43 SpatialGeneticSample<coord_type, individual_type> read(std::string const &path)
44 {
45 if (!is_readable(path))
46 {
47 throw std::string("In GeneticsLoader::read : unable to read file");
48 }
49 analyze(path);
50 return m_sample;
51 }
52
53 private:
54 const unsigned int m_ploidy = 2;
55 std::vector<std::string> m_loci_names;
57
58 bool is_readable(std::string const &path) const
59 {
60 ifstream file(path.c_str());
61 return !file.fail();
62 }
63
64 // String extractions, dependant on file structure
65 void analyze(std::string const &path)
66 {
67 std::ifstream file(path);
68 std::string line;
69 unsigned int current_line = 0;
70
71 while (std::getline(file, line))
72 {
73 if (current_line == 0)
74 {
75 m_loci_names = read_loci_names(erase_coordinates(line));
76 }
77 else
78 {
79 auto x = read_coordinates(line);
80 auto ind = read_individual(erase_coordinates(line));
81 m_sample.add(x, ind);
82 }
83 ++current_line;
84 }
85 }
86
87 std::string &erase_coordinates(std::string &line) const
88 {
89 for (int i = 0; i <= 1; ++i)
90 {
91 std::size_t found = line.find(' ');
92 line.erase(0, ++found);
93 }
94 return line;
95 }
96
97 std::vector<std::string> read_loci_names(std::string const &line) const
98 {
99 std::vector<std::string> names;
100 std::istringstream iss(line);
101 std::string elem;
102 while (std::getline(iss, elem, ' '))
103 {
104 names.push_back(elem);
105 }
106 assert(!names.empty());
107 return names;
108 }
109
110 coord_type read_coordinates(std::string const &line) const
111 {
112 double lat = 0.;
113 double lon = 0.;
114 std::istringstream iss(line);
115 string elem;
116 unsigned int pos = 0;
117 while (std::getline(iss, elem, ' ') && pos <= 1)
118 {
119 if (pos == 0)
120 {
121 lat = std::stod(elem);
122 }
123 else if (pos == 1)
124 {
125 lon = std::stod(elem);
126 }
127 ++pos;
128 }
129 coord_type x(lat, lon);
130 return x;
131 }
132
133 individual_type read_individual(std::string const &line) const
134 {
135
136 std::istringstream iss(line);
137 string elem;
138
139 individual_type individual;
140
141 std::pair<allele_type, allele_type> alleles;
142
143 unsigned int i_locus = 0;
144 unsigned int i_allele = 0;
145 while (std::getline(iss, elem, ' '))
146 {
147
148 if (i_allele == 0)
149 {
150
151 alleles.first = std::stod(elem);
152 ++i_allele;
153 }
154 else if (i_allele == 1)
155 {
156
157 alleles.second = std::stod(elem);
158 std::string locus_name = m_loci_names.at(i_locus);
159 individual.add(locus_name, alleles);
160
161 i_allele = 0;
162 ++i_locus;
163 }
164 }
165 return individual;
166 }
167};
168
169} // namespace genetics
170} // namespace quetzal::format
171
172#endif
Definition Allele.hpp:16
Definition DiploidIndividual.hpp:22
Definition SpatialGeneticSampleLoader.hpp:34
Definition SpatialGeneticSample.hpp:25
Parsers and generators for input/output.
Definition io.hpp:23