Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KofNConstraint.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file
11 /// @brief meta constraint where N constraints declared
12 /// @brief only the lowest K are evaluated
13 /// @author
14 
15 
18 
20 
21 #include <platform/types.hh>
22 #include <core/types.hh>
24 // AUTO-REMOVED #include <core/conformation/signals/LengthEvent.fwd.hh>
25 #include <core/id/AtomID.fwd.hh>
26 #include <core/id/AtomID.hh>
28 // AUTO-REMOVED #include <core/id/SequenceMapping.hh>
30 #include <core/pose/Pose.fwd.hh>
48 #include <utility/down_cast.hh>
49 #include <utility/exit.hh>
50 #include <utility/vector1.fwd.hh>
51 #include <utility/vector1.hh>
52 #include <utility/vector1_bool.hh>
53 #include <utility/vectorL.fwd.hh>
54 #include <utility/vectorL.hh>
55 #include <utility/vectorL_Selector.hh>
56 #include <utility/vectorL_bool.hh>
57 #include <utility/factory/WidgetRegistrator.hh>
58 #include <utility/pointer/ReferenceCount.fwd.hh>
59 #include <utility/pointer/ReferenceCount.hh>
60 #include <utility/pointer/access_ptr.fwd.hh>
61 #include <utility/pointer/access_ptr.hh>
62 #include <utility/pointer/owning_ptr.functions.hh>
63 #include <utility/pointer/owning_ptr.fwd.hh>
64 #include <utility/pointer/owning_ptr.hh>
65 #include <numeric/xyzVector.fwd.hh>
66 #include <numeric/random/random.fwd.hh>
67 #include <algorithm>
68 #include <cassert>
69 #include <cstddef>
70 #include <iosfwd>
71 #include <iostream>
72 #include <limits>
73 #include <map>
74 #include <sstream>
75 #include <string>
76 #include <vector>
77 
78 
79 namespace core {
80 namespace scoring {
81 namespace constraints {
82 
83 ////////////////////////////////////////////////////////////////////////////////////////////////////
84 /// @brief Constructor
86  K_ = K;
88  assert ( member_constraints().size() == 0 );
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////////////////////////
92 /// @brief Constructor
94  K_ = K;
96  assert ( member_constraints().size() > 0 );
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////////////////////////
100 void
102 {
103  cst_score_types_.clear();
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////////////////////////
113 /// @brief ScoreFunction, scores all member constraints; reports the lowest k
114 void
115 KofNConstraint::score( XYZ_Func const & xyz_func, EnergyMap const & weights, EnergyMap & emap ) const {
116  //std::cout << "scoring K of N constraint..." << std::endl;
117 
118  if (K_ == 0) {
119  return; // ? warning msg here?
120  }
121  runtime_assert( K_ <= member_constraints().size() ); // bomb out if K<N
122 
123  cutoff_cst_score_ = 1000000;
124  active_constraints_.clear();
125 
126  utility::vector1<core::Real> all_scores;
127  utility::vector1<EnergyMap> tmp_EMaps;
128 
129  // step 1 score
130  for( ConstraintCOPs::const_iterator member_it = member_constraints().begin(); member_it != member_constraints().end(); member_it++) {
131  EnergyMap emap_i;
132  (*member_it)->score(xyz_func, weights, emap_i);
133 
134  tmp_EMaps.push_back( emap_i );
135  all_scores.push_back( calculate_total_cst_score( weights, emap_i ) );
136  }
137 
138  // step 2 choose cutoff
139  // what if there is a tie?
140  // allow more than K_ to be active <-- this will mess up scores but derivatives will be ok
142  std::sort( sort_scores.begin(), sort_scores.end() );
143  cutoff_cst_score_ = sort_scores[ K_ ];
144 
145  for (core::Size i=1; i<=all_scores.size(); ++i) {
146  if( all_scores[i] <= cutoff_cst_score_ ){
147  active_constraints_.push_back( member_constraints()[i] );
148 
149  emap[constant_constraint] += tmp_EMaps[i][constant_constraint];
150  emap[coordinate_constraint] += tmp_EMaps[i][coordinate_constraint];
151  emap[atom_pair_constraint] += tmp_EMaps[i][atom_pair_constraint];
152  emap[angle_constraint] += tmp_EMaps[i][angle_constraint];
153  emap[dihedral_constraint] += tmp_EMaps[i][dihedral_constraint];
154  emap[backbone_stub_constraint] += tmp_EMaps[i][backbone_stub_constraint];
155  }
156  }
157 } //score
158 
159 
160 /// @brief helper function to accumulate all constraint scores into one number
163 
165  emap[constant_constraint] * weights[constant_constraint] +
167  emap[atom_pair_constraint] * weights[atom_pair_constraint] +
168  emap[angle_constraint] * weights[angle_constraint] +
169  emap[dihedral_constraint] * weights[dihedral_constraint] +
171 
172  return total_score;
173 }
174 
175 
178  ConstraintCOPs new_csts;
179  for( ConstraintCOPs::const_iterator cst_it = member_constraints_.begin(); cst_it != member_constraints_.end(); ++cst_it ){
180  ConstraintOP new_cst = (*cst_it)->remap_resid( seqmap );
181  if( new_cst ) new_csts.push_back( new_cst );
182  }
183  if( new_csts.size() > 0 ){
184  return ConstraintOP( new KofNConstraint( new_csts ) );
185  }
186  else return NULL;
187 }
188 
189 
190 /// @brief function to minimize lowest scoring member constraint
191 void
193  AtomID const & atom,
194  XYZ_Func const & xyz,
195  Vector & F1,
196  Vector & F2,
197  EnergyMap const & weights
198 ) const {
199  if (K_ == 0) {
200  return; // warning msg?
201  }
202  runtime_assert( active_constraints_.size() != 0 );
203  for (core::Size i=1; i<=active_constraints_.size(); ++i) {
204  active_constraints_[i]->fill_f1_f2(atom, xyz, F1, F2, weights);
205  }
206 }
207 
208 void
209 KofNConstraint::show( std::ostream& out) const
210 {
211  out << "KofNConstraint active constraints (K=" << K_ << "):" << std::endl;
212  for (core::Size i=1; i<=active_constraints_.size(); ++i) {
213  active_constraints_[i]->show(out);
214  }
215  out << "KofNConstraint containing the following " << member_constraints().size() << " constraints: " << std::endl;
216  for( ConstraintCOPs::const_iterator cst_it = member_constraints().begin(); cst_it != member_constraints().end(); cst_it++){
217  (*cst_it)->show(out);
218  }
219  out << " ...all member constraints of this KofNConstraint shown." << std::endl;
220 }
221 
224  return active_constraints_;
225 }
226 
227 Size
228 KofNConstraint::show_violations( std::ostream& out, pose::Pose const& pose, Size verbose_level, Real threshold ) const {
229  if (K_ == 0) { return 0; }
230 
231  Size total_viol( 0 );
232  if ( verbose_level >=80 )
233  out << type() << " " << K_ << " of " << member_constraints().size() << " ";
234 
235  for (core::Size i=1; i<=active_constraints_.size(); ++i) {
236  total_viol += active_constraints_[i]->show_violations( out, pose, verbose_level, threshold );
237  }
238 
239  return total_viol;
240 }
241 
242 
243 void
244 KofNConstraint::read_def( std::istream& data, core::pose::Pose const& pose, FuncFactory const& func_factory ) {
245  data >> K_;
246  ConstraintOP constr;
247  while( ( constr = ConstraintIO::read_individual_constraint_new( data, pose, func_factory ) ) != 0) {
249  }
250  std::cout << "Read K of N constraints! K = " << K_ << " N = " << member_constraints().size() << std::endl;
251 }
252 
253 // void
254 // KofNConstraint::read_def(
255 // std::istream& data,
256 // core::pose::Pose const& pose,
257 // FuncFactory const& func_factory
258 // ) {
259 // Size res1, res2;
260 // std::string name1, name2;
261 // std::string func_type;
262 // std::string type;
263 
264 // data
265 // >> name1 >> res1
266 // >> name2 >> res2
267 // >> func_type;
268 
269 // tr.Debug << "read: " << name1 << " " << name2 << " " << res1 << " " << res2 << " func: " << func_type << std::endl;
270 // if ( res1 > pose.total_residue() || res2 > pose.total_residue() ) {
271 // tr.Warning << "ignored constraint (no such atom in pose!)"
272 // << name1 << " " << name2 << " " << res1 << " " << res2 << std::endl;
273 // data.setstate( std::ios_base::failbit );
274 // return;
275 // }
276 
277 // atom1_ = id::AtomID( id::NamedAtomID( name1, res1 ), pose );
278 // atom2_ = id::AtomID( id::NamedAtomID( name2, res2 ), pose );
279 // if ( atom1_.atomno() == 0 || atom2_.atomno() == 0 ) {
280 // tr.Warning << "Error reading atoms: read in atom names("
281 // << name1 << "," << name2 << "), "
282 // << "and found AtomIDs (" << atom1_ << "," << atom2_ << ")" << std::endl;
283 // data.setstate( std::ios_base::failbit );
284 // return;
285 // }
286 
287 // func_ = func_factory.new_func( func_type );
288 // func_->read_data( data );
289 
290 // if ( tr.Debug.visible() ) {
291 // func_->show_definition( std::cout );
292 // std::cout << std::endl;
293 // }
294 // } // parse_ambigous_constraint
295 
296 
297 
298 
299 } //constraints
300 } //scoring
301 } //core