00001
00002
00003
00004
00005
00006
00007 #ifndef IMPDOMINO_COMB_STATE_H
00008 #define IMPDOMINO_COMB_STATE_H
00009
00010 #include "domino_config.h"
00011 #include <IMP/Particle.h>
00012 #include <IMP/Restraint.h>
00013 #include <vector>
00014 #include <sstream>
00015 #include <algorithm>
00016 #include "IMP/base_types.h"
00017 IMPDOMINO_BEGIN_INTERNAL_NAMESPACE
00018
00019
00020
00021
00022 inline unsigned int particle_index(Particle *p) {
00023 Model::ParticleIterator pit= p->get_model()->particles_begin();
00024 unsigned int ret=0;
00025 while (*pit != p) {
00026 ++pit;
00027 ++ret;
00028 IMP_INTERNAL_CHECK(pit != p->get_model()->particles_end(),
00029 "Particle not found");
00030 }
00031
00032
00033
00034
00035
00036
00037
00038
00039 return ret;
00040 }
00041 IMPDOMINO_END_INTERNAL_NAMESPACE
00042
00043 IMPDOMINO_BEGIN_NAMESPACE
00044
00045 IMPDOMINOEXPORT StringKey node_name_key();
00046
00047 typedef std::map<Particle *, unsigned int> CombData;
00048
00049
00050
00051
00052
00053 class IMPDOMINOEXPORT CombState
00054 {
00055 public:
00056
00057 CombState() {
00058 total_score_ = 0.0;
00059 }
00060
00061 CombState(const CombState &other){
00062 total_score_ = 0.0;
00063
00064 for(CombData::const_iterator it = other.data_.begin();
00065 it != other.data_.end(); it++) {
00066 data_[it->first]=it->second;
00067 }
00068 total_score_ = other.total_score_;
00069 }
00070 void add_data_item(Particle *p, unsigned int val) {
00071 IMP_INTERNAL_CHECK(data_.find(p) == data_.end(),
00072 "CombState::add_data_item the particle is already part"
00073 << "CombState : " << p <<std::endl);
00074 data_[p] = val;
00075 }
00076
00077 bool has_particle(Particle *p) {
00078 return data_.find(p) != data_.end();
00079 }
00080
00081 unsigned int get_state_num(Particle *p) const;
00082
00083 const std::string key() const {
00084 std::stringstream s;
00085
00086 for (CombData::const_iterator it = data_.begin();
00087 it != data_.end(); it++) {
00088 Particle *p = it->first;
00089 unsigned int p_index= internal::particle_index(p);
00090 IMP_INTERNAL_CHECK(data_.find(p) != data_.end(),
00091 "CombState::key particle with index " << p_index
00092 << " was not found ");
00093 s << p_index << ":" << it->second << "_";
00094 }
00095 return s.str();
00096
00097 }
00098 const std::string partial_key(const Particles *ps) const;
00099 CombState *get_partial(const Particles &ps) const;
00100
00101
00102
00103
00104
00105
00106
00107
00108 void update_total_score(float old_val, float new_val) {
00109 total_score_ += new_val - old_val;
00110 }
00111
00112
00113
00114
00115 void set_total_score(float val) {
00116 total_score_ = val;
00117 }
00118
00119 Float get_total_score() const {
00120 return total_score_;
00121 }
00122
00123
00124
00125
00126 bool is_part(const CombState &other) const {
00127 for (CombData::const_iterator it = other.data_.begin();
00128 it != other.data_.end(); it++) {
00129 if (data_.find(it->first) == data_.end()) {
00130 return false;
00131 }
00132 if ((data_.find(it->first))->second != it->second) {
00133 return false;
00134 }
00135 }
00136 return true;
00137
00138 }
00139
00140
00141 void show(std::ostream& out = std::cout) const;
00142
00143
00144
00145 void combine(CombState *other) {
00146 IMP_LOG(VERBOSE,"before combination the size is : " <<
00147 data_.size() << std::endl);
00148 for (CombData::const_iterator it = other->data_.begin();
00149 it != other->data_.end(); it++) {
00150 Particle *p = it->first;
00151 if (data_.find(p) == data_.end()) {
00152
00153 data_[p] = it->second;
00154 } else {
00155 std::stringstream error_message;
00156 IMP_INTERNAL_CHECK(data_[p] == it->second,
00157 " CombState::combine the state of particle with name :"
00158 << p->get_value(node_name_key()) << " is wrong"
00159 << " - expect ( " << it->second << " instead of "
00160 << data_[p] << " )"<<std::endl);
00161
00162 }
00163 }
00164 IMP_LOG(VERBOSE,"after combination the size is : "
00165 << data_.size() << std::endl);
00166 }
00167 const CombData * get_data() const {
00168 return &data_;
00169 }
00170 protected:
00171 CombData data_;
00172
00173 Float total_score_;
00174 };
00175
00176
00177 typedef std::map<std::string, CombState *> Combinations;
00178 typedef std::map<std::string, float> CombinationValues;
00179 typedef std::vector<CombState*> CombStates;
00180
00181 IMPDOMINO_END_NAMESPACE
00182
00183 #endif