// Copyright 2009, Andreas Biegert #ifndef CS_PROFILE_COLUMN_H_ #define CS_PROFILE_COLUMN_H_ namespace cs { template class ProfileColumn { public: ProfileColumn(); explicit ProfileColumn(const double &a); explicit ProfileColumn(const double *a); ProfileColumn(const ProfileColumn &rhs); ~ProfileColumn(); ProfileColumn & operator=(const ProfileColumn &rhs); inline double & operator[](const size_t i); inline const double & operator[](const size_t i) const; inline size_t size() const; private: double *v; }; template ProfileColumn::ProfileColumn() : v(new double[Abc::kSizeAny]) {} template ProfileColumn::ProfileColumn(const double& a) : v(new double[Abc::kSizeAny]) { for(size_t i=0; i ProfileColumn::ProfileColumn(const double *a) : v(new double[Abc::kSizeAny]) { for(size_t i=0; i ProfileColumn::ProfileColumn(const ProfileColumn &rhs) : v(new double[Abc::kSizeAny]) { for(size_t i=0; i ProfileColumn & ProfileColumn::operator=(const ProfileColumn &rhs) { if (this != &rhs) for (size_t i=0; i inline double & ProfileColumn::operator[](const size_t i) { #ifdef CHECKBOUNDS if (i<0 || i>=nn) { throw Exception("Profile column subscript out of bounds"); } #endif return v[i]; } template inline const double & ProfileColumn::operator[](const size_t i) const { #ifdef CHECKBOUNDS if (i<0 || i>=nn) { throw Exception("Profile column subscript out of bounds"); } #endif return v[i]; } template inline size_t ProfileColumn::size() const { return Abc::kSizeAny; } template ProfileColumn::~ProfileColumn() { if (v != NULL) delete[] (v); } // Assigns given constant value or default to all entries in vector template inline void Assign(ProfileColumn& v, double val) { for (size_t i = 0; i < Abc::kSizeAny; ++i) v[i] = val; } // Prints profile column in human-readable format for debugging. template std::ostream& operator<< (std::ostream& out, const ProfileColumn& col) { for (size_t a = 0; a < Abc::kSizeAny; ++a) out << strprintf(" %c \t", Abc::kIntToChar[a]); out << std::endl; for (size_t a = 0; a < Abc::kSizeAny; ++a) out << strprintf("%6.4f\t", col[a]); out << std::endl; return out; } // Normalizes all profile column to fixed value. Iff 'incl_any' is true, // normalization also includes value of ANY letter template inline void Normalize(ProfileColumn& col, double val, bool incl_any = false) { const size_t abc_size = incl_any ? Abc::kSizeAny : Abc::kSize; double sum = 0; for (size_t a = 0; a < abc_size; ++a) sum += col[a]; double fac = val / sum; for (size_t a = 0; a < abc_size; ++a) col[a] *= fac; } // Calculates entropy of given profile column using logarithm base 2. template inline double Entropy(const ProfileColumn& col) { double rv = 0.0; for (size_t a = 0; a < Abc::kSize; ++a) if (col[a] > FLT_MIN) rv -= col[a] * log2(col[a]); return rv; } } // namespace cs #endif // CS_PROFILE_COLUMN_H_