bpp-core3  3.0.0
DataTable.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_DATATABLE_H
6 #define BPP_NUMERIC_DATATABLE_H
7 
8 
9 #include "../Clonable.h"
10 #include "../Text/TextTools.h"
11 #include "TableExceptions.h"
12 #include "VectorTools.h"
13 
14 // From the STL:
15 #include <string>
16 #include <vector>
17 #include <map>
18 
19 namespace bpp
20 {
29 class DataTable :
30  public virtual Clonable
31 {
32 protected:
33  size_t nRow_, nCol_;
34  std::vector< std::vector<std::string>> data_;
35  std::vector<std::string> rowNames_;
36  std::vector<std::string> colNames_;
37 
38 public:
45  DataTable(size_t nRow, size_t nCol);
46 
54  DataTable(size_t nRow, const std::vector<std::string>& colNames);
55 
61  DataTable(size_t nCol);
62 
69  DataTable(const std::vector<std::string>& colNames);
70 
71  DataTable(const DataTable& table);
72 
73  DataTable& operator=(const DataTable& table);
74 
75  DataTable* clone() const { return new DataTable(*this); }
76 
77  virtual ~DataTable();
78 
79 public:
86  std::string& operator()(size_t rowIndex, size_t colIndex);
87 
94  const std::string& operator()(size_t rowIndex, size_t colIndex) const;
95 
104  std::string& operator()(const std::string& rowName, const std::string& colName);
105 
114  const std::string& operator()(const std::string& rowName, const std::string& colName) const;
115 
124  std::string& operator()(const std::string& rowName, size_t colIndex);
125 
134  const std::string& operator()(const std::string& rowName, size_t colIndex) const;
135 
144  std::string& operator()(size_t rowIndex, const std::string& colName);
145 
154  const std::string& operator()(size_t rowIndex, const std::string& colName) const;
155 
165  size_t getNumberOfColumns() const { return nCol_; }
166 
174  void setColumnNames(const std::vector<std::string>& colNames);
181  std::vector<std::string> getColumnNames() const;
190  std::string getColumnName(size_t index) const;
191 
195  bool hasColumnNames() const { return colNames_.size() != 0; }
196 
202  std::vector<std::string>& getColumn(size_t index);
208  const std::vector<std::string>& getColumn(size_t index) const;
209 
216  std::vector<std::string>& getColumn(const std::string& colName);
223  const std::vector<std::string>& getColumn(const std::string& colName) const;
224 
231  bool hasColumn(const std::string& colName) const;
232 
239  void deleteColumn(size_t index);
240 
248  void deleteColumn(const std::string& colName);
249 
257  void addColumn(const std::vector<std::string>& newColumn);
267  void addColumn(const std::string& colName, const std::vector<std::string>& newColumn);
279  size_t getNumberOfRows() const { return nRow_; }
280 
288  void setRowNames(const std::vector<std::string>& rowNames);
289 
299  void setRowName(size_t rowIndex, const std::string& rowName);
300 
307  std::vector<std::string> getRowNames() const;
308 
315  bool hasRow(const std::string& rowName) const;
316 
325  std::string getRowName(size_t index) const;
326 
330  bool hasRowNames() const { return rowNames_.size() != 0; }
331 
337  std::vector<std::string> getRow(size_t index) const;
338 
345  std::vector<std::string> getRow(const std::string& rowName) const;
346 
353  void deleteRow(size_t index);
354 
362  void deleteRow(const std::string& rowName);
363 
371  void addRow(const std::vector<std::string>& newRow);
372 
382  void addRow(const std::string& rowName, const std::vector<std::string>& newRow);
383 
390  void setRow(const size_t rowIndex, const std::vector<std::string>& newRow);
391 
392 
395 public:
411  static std::unique_ptr<DataTable> read(std::istream& in, const std::string& sep = "\t", bool header = true, int rowNames = -1);
412 
421  static void write(const DataTable& data, std::ostream& out, const std::string& sep = "\t", bool alignHeaders = false);
422  static void write(const DataTable& data, bpp::OutputStream& out, const std::string& sep = "\t", bool alignHeaders = false);
423 };
424 } // end of namespace bpp.
425 #endif // BPP_NUMERIC_DATATABLE_H
std::vector< std::string > rowNames_
Definition: DataTable.h:35
virtual ~DataTable()
Definition: DataTable.cpp:92
std::vector< std::string > getColumnNames() const
Get the column names of this table.
Definition: DataTable.cpp:289
size_t nRow_
Definition: DataTable.h:33
std::string getRowName(size_t index) const
Get a given row name.
Definition: DataTable.cpp:266
This class corresponds to a &#39;dataset&#39;, i.e. a table with data by rows and variable by columns...
Definition: DataTable.h:29
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
Definition: DataTable.cpp:231
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
Definition: DataTable.cpp:392
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
Definition: DataTable.cpp:277
std::vector< std::vector< std::string > > data_
Definition: DataTable.h:34
std::vector< std::string > getRow(size_t index) const
Definition: DataTable.cpp:424
bool hasRowNames() const
Definition: DataTable.h:330
std::vector< std::string > & getColumn(size_t index)
Definition: DataTable.cpp:309
std::string getColumnName(size_t index) const
Get a given column name.
Definition: DataTable.cpp:296
DataTable * clone() const
Create a copy of this object and send a pointer to it.
Definition: DataTable.h:75
size_t getNumberOfRows() const
Definition: DataTable.h:279
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
Definition: DataTable.cpp:456
std::string & operator()(size_t rowIndex, size_t colIndex)
Definition: DataTable.cpp:99
void setRowName(size_t rowIndex, const std::string &rowName)
Set the row name of a row with a given index.
Definition: DataTable.cpp:245
void addRow(const std::vector< std::string > &newRow)
Add a new row.
Definition: DataTable.cpp:503
OutputStream interface.
Definition: OutputStream.h:29
static std::unique_ptr< DataTable > read(std::istream &in, const std::string &sep="\, bool header=true, int rowNames=-1)
Read a table form a stream in CSV-like format.
Definition: DataTable.cpp:554
bool hasColumnNames() const
Definition: DataTable.h:195
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:63
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
Definition: DataTable.cpp:18
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
Definition: DataTable.cpp:353
size_t nCol_
Definition: DataTable.h:33
void deleteColumn(size_t index)
Delete the given column.
Definition: DataTable.cpp:365
void setRow(const size_t rowIndex, const std::vector< std::string > &newRow)
Sets an existing with a given index.
Definition: DataTable.cpp:516
void deleteRow(size_t index)
Delete the given row.
Definition: DataTable.cpp:468
std::vector< std::string > colNames_
Definition: DataTable.h:36
size_t getNumberOfColumns() const
Definition: DataTable.h:165
DataTable & operator=(const DataTable &table)
Definition: DataTable.cpp:78
static void write(const DataTable &data, std::ostream &out, const std::string &sep="\, bool alignHeaders=false)
Write a DataTable object to stream in CVS-like format.
Definition: DataTable.cpp:624
std::vector< std::string > getRowNames() const
Get the row names of this table.
Definition: DataTable.cpp:259