bpp-core  2.2.0
DataTable.h
Go to the documentation of this file.
1 //
2 // File: DataTable.h
3 // Created by: Julien Dutheil
4 // Created on: Aug 2005
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #ifndef _DataTable_H_
41 #define _DataTable_H_
42 
43 #include "VectorTools.h"
44 #include "DataTableExceptions.h"
45 #include "../Exceptions.h"
46 #include "../Text/TextTools.h"
47 #include "../Clonable.h"
48 
49 // From the STL:
50 #include <string>
51 #include <vector>
52 #include <map>
53 
54 namespace bpp
55 {
64 class DataTable :
65  public Clonable
66 {
67 protected:
68  size_t nRow_, nCol_;
69  std::vector< std::vector<std::string> > data_;
70  std::vector<std::string>* rowNames_;
71  std::vector<std::string>* colNames_;
72 
73 public:
80  DataTable(size_t nRow, size_t nCol);
81 
87  DataTable(size_t nCol);
88 
95  DataTable(const std::vector<std::string>& colNames) throw (DuplicatedTableColumnNameException);
96 
97  DataTable(const DataTable& table);
98 
99  DataTable& operator=(const DataTable& table);
100 
101  DataTable* clone() const { return new DataTable(*this); }
102 
103  virtual ~DataTable();
104 
105 public:
112  std::string& operator()(size_t rowIndex, size_t colIndex) throw (IndexOutOfBoundsException);
113 
120  const std::string& operator()(size_t rowIndex, size_t colIndex) const throw (IndexOutOfBoundsException);
121 
130  std::string& operator()(const std::string& rowName, const std::string& colName)
132 
141  const std::string& operator()(const std::string& rowName, const std::string& colName) const
143 
152  std::string& operator()(const std::string& rowName, size_t colIndex)
154 
163  const std::string& operator()(const std::string& rowName, size_t colIndex) const
165 
174  std::string& operator()(size_t rowIndex, const std::string& colName)
176 
185  const std::string& operator()(size_t rowIndex, const std::string& colName) const
187 
197  size_t getNumberOfColumns() const { return nCol_; }
198 
206  void setColumnNames(const std::vector<std::string>& colNames) throw (DimensionException, DuplicatedTableColumnNameException);
213  std::vector<std::string> getColumnNames() const throw (NoTableColumnNamesException);
222  std::string getColumnName(size_t index) const throw (NoTableColumnNamesException, IndexOutOfBoundsException);
223 
227  bool hasColumnNames() const { return colNames_ != 0; }
228 
234  std::vector<std::string>& getColumn(size_t index) throw (IndexOutOfBoundsException);
240  const std::vector<std::string>& getColumn(size_t index) const throw (IndexOutOfBoundsException);
241 
248  std::vector<std::string>& getColumn(const std::string& colName) throw (NoTableColumnNamesException, TableColumnNameNotFoundException);
255  const std::vector<std::string>& getColumn(const std::string& colName) const throw (NoTableColumnNamesException, TableColumnNameNotFoundException);
256 
263  bool hasColumn(const std::string& colName) const;
264 
271  void deleteColumn(size_t index) throw (IndexOutOfBoundsException);
272 
280  void deleteColumn(const std::string& colName) throw (NoTableColumnNamesException, TableColumnNameNotFoundException);
281 
289  void addColumn(const std::vector<std::string>& newColumn) throw (DimensionException, TableColumnNamesException);
299  void addColumn(const std::string& colName, const std::vector<std::string>& newColumn) throw (DimensionException, NoTableColumnNamesException, DuplicatedTableColumnNameException);
311  size_t getNumberOfRows() const { return nRow_; }
312 
320  void setRowNames(const std::vector<std::string>& rowNames) throw (DimensionException, DuplicatedTableRowNameException);
321 
328  std::vector<std::string> getRowNames() const throw (NoTableRowNamesException);
329 
336  bool hasRow(const std::string& rowName) const;
337 
346  std::string getRowName(size_t index) const throw (NoTableRowNamesException, IndexOutOfBoundsException);
347 
351  bool hasRowNames() const { return rowNames_ != 0; }
352 
358  std::vector<std::string> getRow(size_t index) const throw (IndexOutOfBoundsException);
359 
366  std::vector<std::string> getRow(const std::string& rowName) const throw (NoTableRowNamesException, TableRowNameNotFoundException);
367 
374  void deleteRow(size_t index) throw (IndexOutOfBoundsException);
375 
383  void deleteRow(const std::string& rowName) throw (NoTableRowNamesException, TableRowNameNotFoundException);
384 
392  void addRow(const std::vector<std::string>& newRow) throw (DimensionException, TableRowNamesException);
402  void addRow(const std::string& rowName, const std::vector<std::string>& newRow) throw (DimensionException, NoTableRowNamesException, DuplicatedTableRowNameException);
405 public:
421  static DataTable* read(std::istream& in, const std::string& sep = "\t", bool header = true, int rowNames = -1)
423 
432  static void write(const DataTable& data, std::ostream& out, const std::string& sep = "\t", bool alignHeaders = false);
433  static void write(const DataTable& data, bpp::OutputStream& out, const std::string& sep = "\t", bool alignHeaders = false);
434 };
435 } // end of namespace bpp.
436 
437 #endif // _DataTable_H_
438 
virtual ~DataTable()
Definition: DataTable.cpp:116
std::vector< std::string > getColumnNames() const
Get the column names of this table.
Definition: DataTable.cpp:316
size_t nRow_
Definition: DataTable.h:68
This class allows to perform a correspondence analysis.
std::string getRowName(size_t index) const
Get a given row name.
Definition: DataTable.cpp:290
Exception thrown when a given column name is not found is a DataTable object.
Exception thrown when attempting to duplicate a row name.
std::vector< std::string > * rowNames_
Definition: DataTable.h:70
Exception thrown when trying to retrieve a row by its name and no row names have been specified...
STL namespace.
This class corresponds to a &#39;dataset&#39;, i.e. a table with data by rows and variable by columns...
Definition: DataTable.h:64
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
Definition: DataTable.cpp:266
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
Definition: DataTable.cpp:425
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
Definition: DataTable.cpp:301
std::vector< std::vector< std::string > > data_
Definition: DataTable.h:69
std::vector< std::string > getRow(size_t index) const
Definition: DataTable.cpp:459
Exception thrown when a given row name is not found is a DataTable object.
bool hasRowNames() const
Definition: DataTable.h:351
std::vector< std::string > & getColumn(size_t index)
Definition: DataTable.cpp:336
std::string getColumnName(size_t index) const
Get a given column name.
Definition: DataTable.cpp:323
DataTable * clone() const
Create a copy of this object and send a pointer to it.
Definition: DataTable.h:101
size_t getNumberOfRows() const
Definition: DataTable.h:311
Excpetion thrown when attempting to duplicate a column name.
Exception thrown when trying to retrieve a column by its name and no column names have been specified...
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
Definition: DataTable.cpp:493
std::string & operator()(size_t rowIndex, size_t colIndex)
Definition: DataTable.cpp:128
std::vector< std::string > * colNames_
Definition: DataTable.h:71
void addRow(const std::vector< std::string > &newRow)
Add a new row.
Definition: DataTable.cpp:542
OutputStream interface.
Definition: OutputStream.h:64
bool hasColumnNames() const
Definition: DataTable.h:227
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:99
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
Definition: DataTable.cpp:51
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
Definition: DataTable.cpp:384
size_t nCol_
Definition: DataTable.h:68
void deleteColumn(size_t index)
Delete the given column.
Definition: DataTable.cpp:396
static 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:582
Index out of bounds exception class.
Definition: Exceptions.h:298
Exception thrown when a given name is not found is a DataTable object.
void deleteRow(size_t index)
Delete the given row.
Definition: DataTable.cpp:505
Exception thrown when a dimension problem occured.
General exception class dealing with column names.
General exception class dealing with row names.
size_t getNumberOfColumns() const
Definition: DataTable.h:197
DataTable & operator=(const DataTable &table)
Definition: DataTable.cpp:96
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:653
std::vector< std::string > getRowNames() const
Get the row names of this table.
Definition: DataTable.cpp:283