LocARNA-1.9.2
|
00001 #ifndef LOCARNA_RIBOSUM_HH 00002 #define LOCARNA_RIBOSUM_HH 00003 00004 #ifdef HAVE_CONFIG_H 00005 #include <config.h> 00006 #endif 00007 00008 #include <cstdlib> 00009 #include <string> 00010 #include <fstream> 00011 #include <math.h> 00012 00013 #include "matrix.hh" 00014 #include "alphabet.hh" 00015 00016 namespace LocARNA { 00017 00022 class Ribosum { 00023 public: 00024 typedef Matrix<double> matrix_t; 00025 protected: 00026 typedef Alphabet<std::string> alphabet_type; 00027 00028 std::string name; 00029 matrix_t bm; 00030 matrix_t am; 00031 00032 00033 alphabet_type basename_alphabet; 00034 alphabet_type arcname_alphabet; 00035 00036 Alphabet<char> 00037 char_basename_alphabet; 00038 00039 protected: 00049 std::istream & 00050 read_matrix(std::istream &in, 00051 matrix_t &mat, 00052 const alphabet_type &names) const; 00053 00063 std::ostream & 00064 write_matrix(std::ostream &out, 00065 const matrix_t &mat, 00066 const alphabet_type &alph) const; 00067 00069 Ribosum(); 00070 00073 void 00074 read_ribosum(std::istream &in); 00075 00076 protected: 00078 Alphabet<char> 00079 make_char_alphabet() const; 00080 00087 void 00088 set_basename_alphabet(const std::string a[]) { 00089 basename_alphabet = 00090 alphabet_type(std::vector<std::string>(&a[0], &a[4])); 00091 char_basename_alphabet = make_char_alphabet(); 00092 } 00093 00100 void 00101 set_arcname_alphabet(const std::string a[]) { 00102 arcname_alphabet = 00103 alphabet_type(std::vector<std::string>(&a[0], &a[16])); 00104 } 00105 00106 public: 00112 explicit Ribosum(const std::string &filename); 00113 00115 virtual ~Ribosum(); 00116 00121 const matrix_t & 00122 get_basematch_scores() const { 00123 return bm; 00124 } 00125 00130 const matrix_t & 00131 get_arcmatch_scores() const { 00132 return am; 00133 } 00134 00137 const alphabet_type & 00138 string_alphabet() const { 00139 return basename_alphabet; 00140 } 00141 00144 const Alphabet<char> & 00145 alphabet() const { 00146 return char_basename_alphabet; 00147 } 00148 00151 const std::string & 00152 get_name() const { 00153 return name; 00154 } 00155 00161 double 00162 basematch_score(char i, char j) const { 00163 return bm(alphabet().idx(i), alphabet().idx(j)); 00164 } 00165 00174 double 00175 arcmatch_score(char i, char j, char k, char l) const { 00176 return am(alphabet().idx(i) * 4 + alphabet().idx(j), 00177 alphabet().idx(k) * 4 + alphabet().idx(l)); 00178 } 00179 00180 friend std::ostream & 00181 operator<<(std::ostream &out, const Ribosum &ribosum); 00182 }; 00183 00190 class RibosumFreq : public Ribosum { 00191 public: 00201 explicit RibosumFreq(const std::string &filename); 00202 00203 protected: 00204 RibosumFreq(); 00205 00206 matrix_t base_probs_; 00207 matrix_t base_nonstruct_probs_; 00208 00209 matrix_t basepair_probs_; 00210 matrix_t basematch_probs_; 00211 matrix_t arcmatch_probs_; 00212 00213 public: 00217 double 00218 base_prob(char i) const { 00219 return base_probs_(alphabet().idx(i), 0); 00220 } 00221 00226 double 00227 base_nonstruct_prob(char i) const { 00228 return base_nonstruct_probs_(alphabet().idx(i), 0); 00229 } 00230 00233 const matrix_t & 00234 get_base_probs() const { 00235 return base_probs_; 00236 } 00237 00241 const matrix_t & 00242 get_base_nonstruct_probs() const { 00243 return base_nonstruct_probs_; 00244 } 00245 00250 double 00251 basepair_prob(char i, char j) const { 00252 return basepair_probs_(alphabet().idx(i), alphabet().idx(j)); 00253 } 00254 00257 const matrix_t & 00258 get_basepair_probs() const { 00259 return basepair_probs_; 00260 } 00261 00266 double 00267 basematch_prob(char i, char j) const { 00268 return basematch_probs_(alphabet().idx(i), alphabet().idx(j)); 00269 } 00270 00273 const matrix_t & 00274 get_basematch_probs() const { 00275 return basematch_probs_; 00276 } 00277 00286 double 00287 arcmatch_prob(char i, char j, char k, char l) const { 00288 return arcmatch_probs_(alphabet().idx(i) * 4 + alphabet().idx(j), 00289 alphabet().idx(k) * 4 + alphabet().idx(l)); 00290 } 00291 00294 const matrix_t & 00295 get_arcmatch_probs() const { 00296 return arcmatch_probs_; 00297 } 00298 00302 double 00303 base_unpaired_prob(char i) const; 00304 00318 double 00319 basematch_score_corrected(char i, char j) const; 00320 00323 void 00324 print_basematch_scores_corrected(std::ostream &out) const; 00325 00335 void 00336 read_matrix(std::istream &in, 00337 const std::string &header, 00338 matrix_t &mat, 00339 size_t xdim, 00340 size_t ydim); 00341 00349 void 00350 write_ICC_code(std::ostream &out, const std::string &ribname) const; 00351 00361 std::ostream & 00362 write_matrix(std::ostream &out, 00363 const std::string &name, 00364 const Matrix<double> &mat) const; 00365 00366 friend std::ostream & 00367 operator<<(std::ostream &out, const RibosumFreq &ribosum); 00368 00369 private: 00370 void 00371 write_CC_matrix(std::ostream &out, 00372 const std::string &ribname, 00373 const std::string &matname, 00374 int x, 00375 int y, 00376 const Ribosum::matrix_t &m) const; 00377 00378 void 00379 read_frequencies(std::istream &in); 00380 }; 00381 00382 } // end namespace LocARNA 00383 00384 #endif // LOCARNA_RIBOSUM_HH