|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_CONTAINERS_MATRIX_READER_H_
2 #define GENESIS_UTILS_CONTAINERS_MATRIX_READER_H_
98 return skip_first_col_;
103 return skip_first_row_;
108 skip_first_col_ = value;
114 skip_first_row_ = value;
130 convert_value_ = functor;
143 std::vector<T> table;
147 if( ! input_stream ) {
152 if( skip_first_row_ ) {
156 while( input_stream ) {
157 auto const line = reader_.
parse_line( input_stream );
161 auto len = line.size();
162 if( len > 0 && skip_first_col_ ) {
179 }
else if( cols != len ) {
180 throw std::runtime_error(
"Matrix has different line lengths." );
184 if( convert_value_ ) {
185 for(
size_t i = first; i < line.size(); ++i ) {
186 table.push_back( convert_value_( line[i] ) );
189 for(
size_t i = first; i < line.size(); ++i ) {
191 table.push_back( convert_from_string<T>( line[i] ));
193 throw std::runtime_error(
196 "Cannot parse value \"" + line[i] +
"\" into Matrix. "
204 if( table.size() == 0 ) {
210 if( table.size() % cols != 0 ) {
213 throw std::runtime_error(
"Matrix is not rectangluar." );
217 size_t const rows = table.size() / cols;
218 return Matrix<T>( rows, cols, std::move(table) );
227 bool skip_first_row_ =
false;
228 bool skip_first_col_ =
false;
232 std::function<T( std::string
const& )> convert_value_;
239 #endif // include guard
bool skip_first_col() const
bool skip_first_row() const
std::vector< std::string > parse_line(utils::InputStream &input_stream) const
Parse one line of the CSV data and return it.
CsvReader & separator_chars(std::string const &chars)
Set the chars used to separate fields of the CSV data.
MatrixReader & convert_value_functor(std::function< T(std::string const &)> functor)
MatrixReader(CsvReader const &reader)
MatrixReader(std::string const &separator="\t")
Provides some valuable additions to STD.
CsvReader const & csv_reader() const
std::string to_string(GenomeLocus const &locus)
Provides some commonly used string utility functions.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Matrix< T > read(std::shared_ptr< BaseInputSource > source) const
MatrixReader & operator=(MatrixReader const &)=default
MatrixReader & skip_first_row(bool value)
Read Comma/Character Separated Values (CSV) data and other delimiter-separated formats.
MatrixReader & skip_first_col(bool value)