bpp-core3  3.0.0
bpp::DataTable Class Reference

This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns. More...

#include <Bpp/Numeric/DataTable.h>

+ Inheritance diagram for bpp::DataTable:
+ Collaboration diagram for bpp::DataTable:

Public Member Functions

 DataTable (size_t nRow, size_t nCol)
 Build a new void DataTable object with nRow rows and nCol columns. More...
 
 DataTable (size_t nRow, const std::vector< std::string > &colNames)
 Build a new void DataTable object with nRow rows and named columns. More...
 
 DataTable (size_t nCol)
 Build a new void DataTable object with nCol columns. More...
 
 DataTable (const std::vector< std::string > &colNames)
 Build a new void DataTable object with named columns. More...
 
 DataTable (const DataTable &table)
 
DataTableoperator= (const DataTable &table)
 
DataTableclone () const
 Create a copy of this object and send a pointer to it. More...
 
virtual ~DataTable ()
 
std::string & operator() (size_t rowIndex, size_t colIndex)
 
const std::string & operator() (size_t rowIndex, size_t colIndex) const
 
std::string & operator() (const std::string &rowName, const std::string &colName)
 
const std::string & operator() (const std::string &rowName, const std::string &colName) const
 
std::string & operator() (const std::string &rowName, size_t colIndex)
 
const std::string & operator() (const std::string &rowName, size_t colIndex) const
 
std::string & operator() (size_t rowIndex, const std::string &colName)
 
const std::string & operator() (size_t rowIndex, const std::string &colName) const
 
Work on columns.
size_t getNumberOfColumns () const
 
void setColumnNames (const std::vector< std::string > &colNames)
 Set the column names of this table. More...
 
std::vector< std::string > getColumnNames () const
 Get the column names of this table. More...
 
std::string getColumnName (size_t index) const
 Get a given column name. More...
 
bool hasColumnNames () const
 
std::vector< std::string > & getColumn (size_t index)
 
const std::vector< std::string > & getColumn (size_t index) const
 
std::vector< std::string > & getColumn (const std::string &colName)
 
const std::vector< std::string > & getColumn (const std::string &colName) const
 
bool hasColumn (const std::string &colName) const
 Tell is a given column exists. More...
 
void deleteColumn (size_t index)
 Delete the given column. More...
 
void deleteColumn (const std::string &colName)
 Delete the given column. More...
 
void addColumn (const std::vector< std::string > &newColumn)
 Add a new column. More...
 
void addColumn (const std::string &colName, const std::vector< std::string > &newColumn)
 Add a new column. More...
 
Work on rows.
size_t getNumberOfRows () const
 
void setRowNames (const std::vector< std::string > &rowNames)
 Set the row names of this table. More...
 
void setRowName (size_t rowIndex, const std::string &rowName)
 Set the row name of a row with a given index. More...
 
std::vector< std::string > getRowNames () const
 Get the row names of this table. More...
 
bool hasRow (const std::string &rowName) const
 Tell is a given row exists. More...
 
std::string getRowName (size_t index) const
 Get a given row name. More...
 
bool hasRowNames () const
 
std::vector< std::string > getRow (size_t index) const
 
std::vector< std::string > getRow (const std::string &rowName) const
 
void deleteRow (size_t index)
 Delete the given row. More...
 
void deleteRow (const std::string &rowName)
 Delete the given row. More...
 
void addRow (const std::vector< std::string > &newRow)
 Add a new row. More...
 
void addRow (const std::string &rowName, const std::vector< std::string > &newRow)
 Add a new row. More...
 
void setRow (const size_t rowIndex, const std::vector< std::string > &newRow)
 Sets an existing with a given index. More...
 

Static Public Member Functions

static std::unique_ptr< DataTableread (std::istream &in, const std::string &sep="\, bool header=true, int rowNames=-1)
 Read a table form a stream in CSV-like format. More...
 
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. More...
 
static void write (const DataTable &data, bpp::OutputStream &out, const std::string &sep="\, bool alignHeaders=false)
 

Protected Attributes

size_t nRow_
 
size_t nCol_
 
std::vector< std::vector< std::string > > data_
 
std::vector< std::string > rowNames_
 
std::vector< std::string > colNames_
 

Detailed Description

This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns.

Data are stored as string objects, by column. A DataTable object is hence similar to a ColMatrix<string>.object. (NB: actually, ColMatrix does not exist yet...)

Definition at line 29 of file DataTable.h.

Constructor & Destructor Documentation

◆ DataTable() [1/5]

DataTable::DataTable ( size_t  nRow,
size_t  nCol 
)

Build a new void DataTable object with nRow rows and nCol columns.

Parameters
nRowThe number of rows of the DataTable.
nColThe number of columns of the DataTable.

Definition at line 18 of file DataTable.cpp.

References data_.

Referenced by clone().

◆ DataTable() [2/5]

DataTable::DataTable ( size_t  nRow,
const std::vector< std::string > &  colNames 
)

Build a new void DataTable object with nRow rows and named columns.

Parameters
nRowThe number of rows of the DataTable.
colNamesThe names of the columns of the DataTable.
Exceptions
DuplicatedTableColumnNameExceptionIf colnames contains identical names.

Definition at line 39 of file DataTable.cpp.

References data_, nCol_, and setColumnNames().

◆ DataTable() [3/5]

DataTable::DataTable ( size_t  nCol)

Build a new void DataTable object with nCol columns.

Parameters
nColThe number of columns of the DataTable.

Definition at line 31 of file DataTable.cpp.

◆ DataTable() [4/5]

DataTable::DataTable ( const std::vector< std::string > &  colNames)

Build a new void DataTable object with named columns.

Parameters
colNamesThe names of the columns of the DataTable.
Exceptions
DuplicatedTableColumnNameExceptionIf colnames contains identical names.

Definition at line 54 of file DataTable.cpp.

References setColumnNames().

◆ DataTable() [5/5]

DataTable::DataTable ( const DataTable table)

Definition at line 65 of file DataTable.cpp.

References colNames_, and rowNames_.

◆ ~DataTable()

DataTable::~DataTable ( )
virtual

Definition at line 92 of file DataTable.cpp.

Referenced by clone().

Member Function Documentation

◆ addColumn() [1/2]

void DataTable::addColumn ( const std::vector< std::string > &  newColumn)

Add a new column.

Parameters
newColumnThe new column values.
Exceptions
DimensionExceptionIf the number of values does not match the number of rows.
TableColumnNamesExceptionIf the table has row names.

Definition at line 392 of file DataTable.cpp.

References colNames_, data_, nCol_, and nRow_.

Referenced by hasColumnNames().

◆ addColumn() [2/2]

void DataTable::addColumn ( const std::string &  colName,
const std::vector< std::string > &  newColumn 
)

Add a new column.

Parameters
colNameThe name of the column.
newColumnThe new column values.
Exceptions
DimensionExceptionIf the number of values does not match the number of rows.
NoTableColumnNamesExceptionIf the table does not have row names.
DuplicatedTableColumnNameExceptionIf colName is already used.

Definition at line 402 of file DataTable.cpp.

References colNames_, data_, nCol_, and nRow_.

◆ addRow() [1/2]

void DataTable::addRow ( const std::vector< std::string > &  newRow)

Add a new row.

Parameters
newRowThe new row values.
Exceptions
DimensionExceptionIf the number of values does not match the number of columns.
TableRowNamesExceptionIf the table has column names.

Definition at line 503 of file DataTable.cpp.

References data_, nCol_, nRow_, and rowNames_.

Referenced by hasRowNames(), and read().

◆ addRow() [2/2]

void DataTable::addRow ( const std::string &  rowName,
const std::vector< std::string > &  newRow 
)

Add a new row.

Parameters
rowNameThe name of the row.
newRowThe new row values.
Exceptions
DimensionExceptionIf the number of values does not match the number of columns.
NoTableRowNamesExceptionIf the table does not have column names.
DuplicatedTableRowNameExceptionIf rowName is already used.

Definition at line 529 of file DataTable.cpp.

References data_, nCol_, nRow_, and rowNames_.

◆ clone()

DataTable* bpp::DataTable::clone ( ) const
inlinevirtual

Create a copy of this object and send a pointer to it.

Returns
A pointer toward the copy object.

Implements bpp::Clonable.

Definition at line 75 of file DataTable.h.

References DataTable(), operator()(), and ~DataTable().

◆ deleteColumn() [1/2]

void DataTable::deleteColumn ( size_t  index)

Delete the given column.

Parameters
indexThe index of the column.
Exceptions
IndexOutOfBoundsExceptionIf index is >= number of columns.

Definition at line 365 of file DataTable.cpp.

References colNames_, data_, and nCol_.

Referenced by hasColumnNames(), and read().

◆ deleteColumn() [2/2]

void DataTable::deleteColumn ( const std::string &  colName)

Delete the given column.

Parameters
colNameThe name of the column.
Exceptions
NoTableColumnNamesExceptionIf no column names are associated to this table.
TableColumnNameNotFoundExceptionIf colName do not match existing column names.

Definition at line 375 of file DataTable.cpp.

References colNames_, data_, nCol_, and bpp::VectorTools::which().

◆ deleteRow() [1/2]

void DataTable::deleteRow ( size_t  index)

Delete the given row.

Parameters
indexThe index of the row.
Exceptions
IndexOutOfBoundsExceptionIf index is >= number of row.

Definition at line 468 of file DataTable.cpp.

References data_, nCol_, nRow_, and rowNames_.

Referenced by hasRowNames().

◆ deleteRow() [2/2]

void DataTable::deleteRow ( const std::string &  rowName)

Delete the given row.

Parameters
rowNameThe name of the row.
Exceptions
NoTableRowNamesExceptionIf no row names are associated to this table.
TableRowNameNotFoundExceptionIf rowName do not match existing column names.

Definition at line 482 of file DataTable.cpp.

References data_, nCol_, nRow_, rowNames_, and bpp::VectorTools::which().

◆ getColumn() [1/4]

vector< string > & DataTable::getColumn ( size_t  index)
Returns
The values in the given column.
Parameters
indexThe index of the column.
Exceptions
IndexOutOfBoundsExceptionIf index is >= number of columns.

Definition at line 309 of file DataTable.cpp.

References data_, and nCol_.

Referenced by hasColumnNames(), and read().

◆ getColumn() [2/4]

const vector< string > & DataTable::getColumn ( size_t  index) const
Returns
The values in the given column.
Parameters
indexThe index of the column.
Exceptions
IndexOutOfBoundsExceptionIf index is >= number of columns.

Definition at line 316 of file DataTable.cpp.

References data_, and nCol_.

◆ getColumn() [3/4]

vector< string > & DataTable::getColumn ( const std::string &  colName)
Returns
The values in the given column.
Parameters
colNameThe name of the column.
Exceptions
NoTableColumnNamesExceptionIf no column names are associated to this table.
TableColumnNameNotFoundExceptionIf colName do not match existing column names.

Definition at line 323 of file DataTable.cpp.

References colNames_, data_, and bpp::VectorTools::which().

◆ getColumn() [4/4]

const vector< string > & DataTable::getColumn ( const std::string &  colName) const
Returns
The values in the given column.
Parameters
colNameThe name of the column.
Exceptions
NoTableColumnNamesExceptionIf no column names are associated to this table.
TableColumnNameNotFoundExceptionIf colName do not match existing column names.

Definition at line 338 of file DataTable.cpp.

References colNames_, data_, and bpp::VectorTools::which().

◆ getColumnName()

string DataTable::getColumnName ( size_t  index) const

Get a given column name.

Parameters
indexThe index of the column.
Returns
The column name associated to the given column.
Exceptions
NoTableColumnNamesExceptionIf no column names are associated to this table.
IndexOutOfBoundsExceptionIf index is >= number of columns.

Definition at line 296 of file DataTable.cpp.

References colNames_, and nCol_.

Referenced by getNumberOfColumns().

◆ getColumnNames()

vector< string > DataTable::getColumnNames ( ) const

Get the column names of this table.

Returns
The column names of this table.
Exceptions
NoTableColumnNamesExceptionIf no column names are associated to this table.

Definition at line 289 of file DataTable.cpp.

References colNames_.

Referenced by getNumberOfColumns(), and write().

◆ getNumberOfColumns()

size_t bpp::DataTable::getNumberOfColumns ( ) const
inline
Returns
The number of columns in this table.

Definition at line 165 of file DataTable.h.

References getColumnName(), getColumnNames(), nCol_, and setColumnNames().

Referenced by write().

◆ getNumberOfRows()

size_t bpp::DataTable::getNumberOfRows ( ) const
inline
Returns
The number of rows in this table.

Definition at line 279 of file DataTable.h.

References getRowName(), getRowNames(), hasRow(), nRow_, setRowName(), and setRowNames().

Referenced by write().

◆ getRow() [1/2]

vector< string > DataTable::getRow ( size_t  index) const
Returns
A vector which contains a copy in the given row.
Parameters
indexThe index of the row.
Exceptions
IndexOutOfBoundsExceptionIf index is >= number of rows.

Definition at line 424 of file DataTable.cpp.

References data_, nCol_, and nRow_.

Referenced by hasRowNames().

◆ getRow() [2/2]

vector< string > DataTable::getRow ( const std::string &  rowName) const
Returns
A vector which contains a copy in the given row.
Parameters
rowNameThe name of the row.
Exceptions
NoTableRowNamesExceptionIf no row names are associated to this table.
TableRowNameNotFoundExceptionIf rowName do not match existing row names.

Definition at line 436 of file DataTable.cpp.

References data_, nCol_, rowNames_, and bpp::VectorTools::which().

◆ getRowName()

string DataTable::getRowName ( size_t  index) const

Get a given row name.

Parameters
indexThe index of the row.
Returns
The row name associated to the given row.
Exceptions
NoTableRowNamesExceptionIf no row names are associated to this table.
IndexOutOfBoundsExceptionIf index is >= number of rows.

Definition at line 266 of file DataTable.cpp.

References nRow_, and rowNames_.

Referenced by getNumberOfRows(), and write().

◆ getRowNames()

vector< string > DataTable::getRowNames ( ) const

Get the row names of this table.

Returns
The row names of this table.
Exceptions
NoTableRowNamesExceptionIf no row names are associated to this table.

Definition at line 259 of file DataTable.cpp.

References rowNames_.

Referenced by getNumberOfRows().

◆ hasColumn()

bool DataTable::hasColumn ( const std::string &  colName) const

Tell is a given column exists.

Parameters
colNameThe name of the column to look for.
Returns
true if the column was found, false if not or if there are no column names.

Definition at line 353 of file DataTable.cpp.

References colNames_.

Referenced by hasColumnNames().

◆ hasColumnNames()

bool bpp::DataTable::hasColumnNames ( ) const
inline
Returns
true If column names are associated to this table.

Definition at line 195 of file DataTable.h.

References addColumn(), deleteColumn(), getColumn(), and hasColumn().

Referenced by write().

◆ hasRow()

bool DataTable::hasRow ( const std::string &  rowName) const

Tell is a given row exists.

Parameters
rowNameThe name of the row to look for.
Returns
true if the row was found, false if not or if there are no row names.

Definition at line 456 of file DataTable.cpp.

References rowNames_.

Referenced by getNumberOfRows().

◆ hasRowNames()

bool bpp::DataTable::hasRowNames ( ) const
inline
Returns
true If row names are associated to this table.

Definition at line 330 of file DataTable.h.

References addRow(), deleteRow(), getRow(), read(), setRow(), and write().

Referenced by read(), and write().

◆ operator()() [1/8]

string & DataTable::operator() ( size_t  rowIndex,
size_t  colIndex 
)
Returns
The element at a given position.
Parameters
rowIndexRow number.
colIndexColumn number.
Exceptions
IndexOutOfBoundsExceptionIf one of the index is greater or equal to the corresponding number of columns/rows.

Definition at line 99 of file DataTable.cpp.

References data_, and nCol_.

Referenced by clone().

◆ operator()() [2/8]

const string & DataTable::operator() ( size_t  rowIndex,
size_t  colIndex 
) const
Returns
The element at a given position.
Parameters
rowIndexRow number.
colIndexColumn number.
Exceptions
IndexOutOfBoundsExceptionIf one of the index is greater or equal to the corresponding number of columns/rows.

Definition at line 108 of file DataTable.cpp.

References data_, and nCol_.

◆ operator()() [3/8]

string & DataTable::operator() ( const std::string &  rowName,
const std::string &  colName 
)
Returns
The element at a given position.
Parameters
rowNameRow name.
colNameColumn name.
Exceptions
NoTableRowNamesExceptionIf the table does not have names associated to rows.
NoTableColumnNamesExceptionIf the table does not have names associated to columns.
TableNameNotFoundExceptionIf one of rowName or colName do not match existing names.

Definition at line 119 of file DataTable.cpp.

References colNames_, bpp::ElementNotFoundException< T >::getElement(), rowNames_, and bpp::VectorTools::which().

◆ operator()() [4/8]

const string & DataTable::operator() ( const std::string &  rowName,
const std::string &  colName 
) const
Returns
The element at a given position.
Parameters
rowNameRow name.
colNameColumn name.
Exceptions
NoTableRowNamesExceptionIf the table does not have names associated to rows.
NoTableColumnNamesExceptionIf the table does not have names associated to columns.
TableNameNotFoundExceptionIf one of rowName or colName do not match existing names.

Definition at line 137 of file DataTable.cpp.

References colNames_, bpp::ElementNotFoundException< T >::getElement(), rowNames_, and bpp::VectorTools::which().

◆ operator()() [5/8]

string & DataTable::operator() ( const std::string &  rowName,
size_t  colIndex 
)
Returns
The element at a given position.
Parameters
rowNameRow name.
colIndexColumn number.
Exceptions
NoTableRowNamesExceptionIf the table does not have names associated to rows.
IndexOutOfBoundsExceptionIf the index is greater or equal to the number of columns.
TableNameNotFoundExceptionIf rowName do not match existing names.

Definition at line 157 of file DataTable.cpp.

References bpp::ElementNotFoundException< T >::getElement(), nCol_, rowNames_, and bpp::VectorTools::which().

◆ operator()() [6/8]

const string & DataTable::operator() ( const std::string &  rowName,
size_t  colIndex 
) const
Returns
The element at a given position.
Parameters
rowNameRow name.
colIndexColumn number.
Exceptions
NoTableRowNamesExceptionIf the table does not have names associated to rows.
IndexOutOfBoundsExceptionIf the index is greater or equal to the number of columns.
TableNameNotFoundExceptionIf rowName do not match existing names.

Definition at line 174 of file DataTable.cpp.

References bpp::ElementNotFoundException< T >::getElement(), nCol_, rowNames_, and bpp::VectorTools::which().

◆ operator()() [7/8]

string & DataTable::operator() ( size_t  rowIndex,
const std::string &  colName 
)
Returns
The element at a given position.
Parameters
rowIndexRow number.
colNameColumn name.
Exceptions
IndexOutOfBoundsExceptionIf the index is greater or equal to the number of rows.
NoTableColumnNamesExceptionIf the table does not have names associated to columns.
TableNameNotFoundExceptionIf colName do not match existing names.

Definition at line 193 of file DataTable.cpp.

References colNames_, data_, bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().

◆ operator()() [8/8]

const string & DataTable::operator() ( size_t  rowIndex,
const std::string &  colName 
) const
Returns
The element at a given position.
Parameters
rowIndexRow number.
colNameColumn name.
Exceptions
IndexOutOfBoundsExceptionIf the index is greater or equal to the number of rows.
NoTableColumnNamesExceptionIf the table does not have names associated to columns.
TableNameNotFoundExceptionIf colName do not match existing names.

Definition at line 210 of file DataTable.cpp.

References colNames_, data_, bpp::ElementNotFoundException< T >::getElement(), and bpp::VectorTools::which().

◆ operator=()

DataTable & DataTable::operator= ( const DataTable table)

Definition at line 78 of file DataTable.cpp.

References colNames_, data_, nCol_, nRow_, and rowNames_.

◆ read()

unique_ptr< DataTable > DataTable::read ( std::istream &  in,
const std::string &  sep = "\t",
bool  header = true,
int  rowNames = -1 
)
static

Read a table form a stream in CSV-like format.

The number of rows is given by the second line in the file. By default, if the first line as one column less than the second one, the first line is taken as column names, and the first column as row names. Otherwise, no column names and no row names are specified, unless explicitely precised by the user.

Parameters
inThe input stream.
sepThe column delimiter.
headerTell if the first line must be used as column names, otherwise use default.
rowNamesUse a column as rowNames. If positive, use the specified column to compute rownames, otherwise use default;
Returns
A pointer toward a new DataTable object.

Definition at line 554 of file DataTable.cpp.

References addRow(), deleteColumn(), getColumn(), bpp::FileTools::getNextLine(), bpp::StringTokenizer::getTokens(), hasRowNames(), bpp::TextTools::isEmpty(), setColumnNames(), and setRowNames().

Referenced by hasRowNames().

◆ setColumnNames()

void DataTable::setColumnNames ( const std::vector< std::string > &  colNames)

Set the column names of this table.

Parameters
colNamesThe row names.
Exceptions
DimensionExceptionIf the number of names do not match the number of columns in the table.
DuplicatedTableColumnNameExceptionIf names are not unique.

Definition at line 277 of file DataTable.cpp.

References colNames_, bpp::VectorTools::isUnique(), and nCol_.

Referenced by DataTable(), getNumberOfColumns(), and read().

◆ setRow()

void DataTable::setRow ( const size_t  rowIndex,
const std::vector< std::string > &  newRow 
)

Sets an existing with a given index.

Parameters
rowIndexThe index of the row.
newRowThe new row values.

Definition at line 516 of file DataTable.cpp.

References data_, nCol_, and nRow_.

Referenced by hasRowNames().

◆ setRowName()

void DataTable::setRowName ( size_t  rowIndex,
const std::string &  rowName 
)

Set the row name of a row with a given index.

Parameters
rowIndexthe index of the row
rowNameThe row name.
Exceptions
DimensionExceptionIf the index does not match.
DuplicatedTableRowNameExceptionIf name is already given.

Definition at line 245 of file DataTable.cpp.

References bpp::VectorTools::contains(), nRow_, and rowNames_.

Referenced by getNumberOfRows().

◆ setRowNames()

void DataTable::setRowNames ( const std::vector< std::string > &  rowNames)

Set the row names of this table.

Parameters
rowNamesThe row names.
Exceptions
DimensionExceptionIf the number of names do not match the number of rows in the table.
DuplicatedTableRowNameExceptionIf names are not unique.

Definition at line 231 of file DataTable.cpp.

References bpp::VectorTools::isUnique(), nRow_, and rowNames_.

Referenced by getNumberOfRows(), and read().

◆ write() [1/2]

void DataTable::write ( const DataTable data,
std::ostream &  out,
const std::string &  sep = "\t",
bool  alignHeaders = false 
)
static

Write a DataTable object to stream in CVS-like format.

Parameters
dataThe table to write.
outThe output stream.
sepThe column delimiter.
alignHeadersIf true, add a delimiter before the first column header if there is row names.

Definition at line 624 of file DataTable.cpp.

References getColumnNames(), getNumberOfColumns(), getNumberOfRows(), getRowName(), hasColumnNames(), and hasRowNames().

Referenced by hasRowNames().

◆ write() [2/2]

void DataTable::write ( const DataTable data,
bpp::OutputStream out,
const std::string &  sep = "\t",
bool  alignHeaders = false 
)
static

Member Data Documentation

◆ colNames_

std::vector<std::string> bpp::DataTable::colNames_
protected

◆ data_

std::vector< std::vector<std::string> > bpp::DataTable::data_
protected

◆ nCol_

size_t bpp::DataTable::nCol_
protected

◆ nRow_

size_t bpp::DataTable::nRow_
protected

◆ rowNames_

std::vector<std::string> bpp::DataTable::rowNames_
protected

The documentation for this class was generated from the following files: