bpp-core3  3.0.0
DataTable.h
Go to the documentation of this file.
1 //
2 // File: DataTable.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2005-08-07 00:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_DATATABLE_H
42 #define BPP_NUMERIC_DATATABLE_H
43 
44 
45 #include "../Clonable.h"
46 #include "../Text/TextTools.h"
47 #include "TableExceptions.h"
48 #include "VectorTools.h"
49 
50 // From the STL:
51 #include <string>
52 #include <vector>
53 #include <map>
54 
55 namespace bpp
56 {
65 class DataTable :
66  public Clonable
67 {
68 protected:
69  size_t nRow_, nCol_;
70  std::vector< std::vector<std::string> > data_;
71  std::vector<std::string>* rowNames_;
72  std::vector<std::string>* colNames_;
73 
74 public:
81  DataTable(size_t nRow, size_t nCol);
82 
90  DataTable(size_t nRow, const std::vector<std::string>& colNames);
91 
97  DataTable(size_t nCol);
98 
105  DataTable(const std::vector<std::string>& colNames);
106 
107  DataTable(const DataTable& table);
108 
109  DataTable& operator=(const DataTable& table);
110 
111  DataTable* clone() const { return new DataTable(*this); }
112 
113  virtual ~DataTable();
114 
115 public:
122  std::string& operator()(size_t rowIndex, size_t colIndex);
123 
130  const std::string& operator()(size_t rowIndex, size_t colIndex) const;
131 
140  std::string& operator()(const std::string& rowName, const std::string& colName);
141 
150  const std::string& operator()(const std::string& rowName, const std::string& colName) const;
151 
160  std::string& operator()(const std::string& rowName, size_t colIndex);
161 
170  const std::string& operator()(const std::string& rowName, size_t colIndex) const;
171 
180  std::string& operator()(size_t rowIndex, const std::string& colName);
181 
190  const std::string& operator()(size_t rowIndex, const std::string& colName) const;
191 
201  size_t getNumberOfColumns() const { return nCol_; }
202 
210  void setColumnNames(const std::vector<std::string>& colNames);
217  std::vector<std::string> getColumnNames() const;
226  std::string getColumnName(size_t index) const;
227 
231  bool hasColumnNames() const { return colNames_ != 0; }
232 
238  std::vector<std::string>& getColumn(size_t index);
244  const std::vector<std::string>& getColumn(size_t index) const;
245 
252  std::vector<std::string>& getColumn(const std::string& colName);
259  const std::vector<std::string>& getColumn(const std::string& colName) const;
260 
267  bool hasColumn(const std::string& colName) const;
268 
275  void deleteColumn(size_t index);
276 
284  void deleteColumn(const std::string& colName);
285 
293  void addColumn(const std::vector<std::string>& newColumn);
303  void addColumn(const std::string& colName, const std::vector<std::string>& newColumn);
315  size_t getNumberOfRows() const { return nRow_; }
316 
324  void setRowNames(const std::vector<std::string>& rowNames);
325 
335  void setRowName(size_t rowIndex, const std::string& rowName);
336 
343  std::vector<std::string> getRowNames() const;
344 
351  bool hasRow(const std::string& rowName) const;
352 
361  std::string getRowName(size_t index) const;
362 
366  bool hasRowNames() const { return rowNames_ != 0; }
367 
373  std::vector<std::string> getRow(size_t index) const;
374 
381  std::vector<std::string> getRow(const std::string& rowName) const;
382 
389  void deleteRow(size_t index);
390 
398  void deleteRow(const std::string& rowName);
399 
407  void addRow(const std::vector<std::string>& newRow);
408 
419  void addRow(const std::string& rowName, const std::vector<std::string>& newRow);
420 
428  void setRow(const size_t rowIndex, const std::vector<std::string>& newRow);
429 
430 
433 public:
449  static DataTable* read(std::istream& in, const std::string& sep = "\t", bool header = true, int rowNames = -1);
450 
459  static void write(const DataTable& data, std::ostream& out, const std::string& sep = "\t", bool alignHeaders = false);
460  static void write(const DataTable& data, bpp::OutputStream& out, const std::string& sep = "\t", bool alignHeaders = false);
461 };
462 } // end of namespace bpp.
463 #endif // BPP_NUMERIC_DATATABLE_H
The Clonable interface (allow an object to be cloned).
Definition: Clonable.h:103
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns.
Definition: DataTable.h:67
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
Definition: DataTable.cpp:506
std::vector< std::string > * rowNames_
Definition: DataTable.h:71
void deleteRow(size_t index)
Delete the given row.
Definition: DataTable.cpp:518
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
Definition: DataTable.cpp:53
void deleteColumn(size_t index)
Delete the given column.
Definition: DataTable.cpp:415
void setRow(const size_t rowIndex, const std::vector< std::string > &newRow)
Sets an existing with a given index.
Definition: DataTable.cpp:566
bool hasRowNames() const
Definition: DataTable.h:366
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
Definition: DataTable.cpp:442
std::string & operator()(size_t rowIndex, size_t colIndex)
Definition: DataTable.cpp:145
size_t nCol_
Definition: DataTable.h:69
void setRowName(size_t rowIndex, const std::string &rowName)
Set the row name of a row with a given index.
Definition: DataTable.cpp:293
std::vector< std::string > * colNames_
Definition: DataTable.h:72
DataTable * clone() const
Create a copy of this object and send a pointer to it.
Definition: DataTable.h:111
std::vector< std::vector< std::string > > data_
Definition: DataTable.h:70
std::vector< std::string > getRowNames() const
Get the row names of this table.
Definition: DataTable.cpp:307
size_t getNumberOfColumns() const
Definition: DataTable.h:201
virtual ~DataTable()
Definition: DataTable.cpp:133
std::string getColumnName(size_t index) const
Get a given column name.
Definition: DataTable.cpp:346
bool hasColumnNames() const
Definition: DataTable.h:231
std::vector< std::string > getColumnNames() const
Get the column names of this table.
Definition: DataTable.cpp:339
std::vector< std::string > getRow(size_t index) const
Definition: DataTable.cpp:474
std::vector< std::string > & getColumn(size_t index)
Definition: DataTable.cpp:359
static DataTable * read(std::istream &in, const std::string &sep="\t", bool header=true, int rowNames=-1)
Read a table form a stream in CSV-like format.
Definition: DataTable.cpp:604
static void write(const DataTable &data, std::ostream &out, const std::string &sep="\t", bool alignHeaders=false)
Write a DataTable object to stream in CVS-like format.
Definition: DataTable.cpp:674
size_t nRow_
Definition: DataTable.h:69
DataTable & operator=(const DataTable &table)
Definition: DataTable.cpp:113
void addRow(const std::vector< std::string > &newRow)
Add a new row.
Definition: DataTable.cpp:553
std::string getRowName(size_t index) const
Get a given row name.
Definition: DataTable.cpp:314
size_t getNumberOfRows() const
Definition: DataTable.h:315
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
Definition: DataTable.cpp:325
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
Definition: DataTable.cpp:403
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
Definition: DataTable.cpp:277
OutputStream interface.
Definition: OutputStream.h:67