Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CoordinateConstraint.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
12 
13 // Unit headers
15 
16 // Package Headers
24 #include <core/pose/Pose.hh>
25 #include <core/pose/util.hh>
26 #include <core/id/AtomID.hh>
27 
29 
30 // Utility Headers
31 #include <basic/Tracer.hh>
32 
33 
34 // C++ Headers
35 #include <cstdlib>
36 #include <iostream>
37 // AUTO-REMOVED #include <utility>
38 
39 #include <core/id/NamedAtomID.hh>
41 #include <utility/vector1.hh>
42 
43 
44 
45 static basic::Tracer tr("core.io.constraints");
46 
47 namespace core {
48 namespace scoring {
49 namespace constraints {
50 
53 
56  atom_( id::BOGUS_ATOM_ID ),
57  fixed_atom_( id::BOGUS_ATOM_ID ),
58  func_( NULL ) {}
59 
60 ///c-tor
62  AtomID const & a1,
63  AtomID const & fixed_atom_in,
64  Vector const & xyz_target_in,
65  FuncOP func,
66  ScoreType scotype
67 ):
68  Constraint( scotype ),
69  atom_(a1),
70  fixed_atom_( fixed_atom_in ),
71  xyz_target_( xyz_target_in ),
72  func_( func )
73 {}
74 
76 
79  return "CoordinateConstraint";
80 }
81 
84  return new CoordinateConstraint( *this );
85 }
86 
87 
88 void CoordinateConstraint::show( std::ostream& out ) const
89 {
90  out << "CoordinateConstraint ("
91  << atom_.atomno() << "," << atom_.rsd() << "-"
92  << fixed_atom_.atomno() << "," << fixed_atom_.rsd() << ")" << std::endl;
93  func_->show( out );
94 }
95 
96 void CoordinateConstraint::show_def( std::ostream& out, pose::Pose const& pose ) const
97 {
98  out << type() << " " << pose::atom_id_to_named_atom_id( atom_, pose ) << " " << pose::atom_id_to_named_atom_id(fixed_atom_, pose ) << " ";
99  out << xyz_target_.x() << " " << xyz_target_.y() << " " << xyz_target_.z() << " " ;
100  func_->show_definition( out );
101 }
102 
103 
104 Size
106  std::ostream & out,
107  pose::Pose const & pose,
108  Size verbose_level,
109  Real threshold
110 ) const{
111  if ( verbose_level > 80 ) {
112  out << "CoordConstraint ("
113  << pose.residue_type(atom_.rsd() ).atom_name( atom_.atomno() )
114  << ":" << atom_.atomno() << "," << atom_.rsd() << " ) ";
115  }
116  return func_->show_violations( out, dist( pose ), verbose_level, threshold );
117 }
118 
119 
120 /// @brief Copies the data from this Constraint into a new object and returns an OP
121 /// atoms are mapped to atoms with the same name in dest pose ( e.g. for switch from centroid to fullatom )
122 /// if a sequence_mapping is present it is used to map residue numbers .. NULL = identity mapping
123 /// to the new object. Intended to be implemented by derived classes.
125  id::NamedAtomID atom1( atom_id_to_named_atom_id( atom(1), src ) );
126  id::NamedAtomID atom2( atom_id_to_named_atom_id( atom(2), src ) );
127 
128  if ( smap ) {
129  atom1.rsd() = (*smap)[ atom(1).rsd() ];
130  atom2.rsd() = (*smap)[ atom(2).rsd() ];
131  }
132 
133  //get AtomIDs for target pose
134  id::AtomID id1( named_atom_id_to_atom_id( atom1, dest, false /*raise exception*/ ) );
135  id::AtomID id2( named_atom_id_to_atom_id( atom2, dest, false /*raise exception*/ ) );
136 
137  if ( id1.valid() && id2.valid() ) {
138  return new CoordinateConstraint( id1, id2, xyz_target_, func_, score_type() );
139  } else {
140  return NULL;
141  }
142 }
143 
144 ///
145 Real
147  Vector const & xyz
148 ) const
149 {
150  return func( xyz.distance( xyz_target_ ) );
151 }
152 
153 ///
154 void
155 CoordinateConstraint::score( XYZ_Func const & xyz, EnergyMap const &, EnergyMap & emap ) const
156 {
157  emap[ this->score_type() ] += non_virtual_score( xyz( atom_ ) );
158 }
159 
160 // atom deriv
161 void
163  AtomID const & atom,
164  XYZ_Func const & xyz,
165  Vector & F1,
166  Vector & F2,
167  EnergyMap const & weights
168 ) const
169 {
170  if ( atom != atom_ ) return;
171 
172  Vector const & xyz1( xyz( atom_ ) ), xyz2( xyz_target_ );
173 
174  Vector const f2( xyz1 - xyz2 );
175  Real const dist( f2.length() ), deriv( dfunc( dist ) );
176  if ( deriv != 0.0 && dist != 0.0 ) {
177  Vector const f1( xyz1.cross( xyz2 ) );
178  // jk: double F1 and F2 because the target is fixed
179  // (matches deriv_check, and minimizes faster)
180  // rhiju: No, JK, this isn't working...
181  F1 += ( ( deriv / dist ) * f1 ) * weights[ this->score_type() ];
182  F2 += ( ( deriv / dist ) * f2 ) * weights[ this->score_type() ];
183  }
184 }
185 
186 
187 
188 ///
189 Size
191 {
192  return 2;
193 }
194 
195 ///
196 core::id::AtomID const &
198 {
199  switch( n ) {
200  case 1:
201  return atom_;
202  case 2:
203  return fixed_atom_;
204  default:
205  utility_exit_with_message( "CoordinateConstraint::atom() bad argument" );
206  }
207  return atom_;
208 }
209 
210 
211 Real
212 CoordinateConstraint::dist( pose::Pose const & pose ) const {
213  conformation::Conformation const & conformation( pose.conformation() );
214  Vector const & xyz( conformation.xyz( atom_ ) );
215  return xyz.distance( xyz_target_ );
216 }
217 
220 {
221 
222  if ( seqmap[atom_.rsd()] != 0 && seqmap[fixed_atom_.rsd()] != 0 ) {
223  AtomID remap_a( atom_.atomno(), seqmap[atom_.rsd()] ),
224  remap_fa( fixed_atom_.atomno(), seqmap[fixed_atom_.rsd()] );
225  return ConstraintOP( new CoordinateConstraint( remap_a, remap_fa, xyz_target_, this->func_, score_type() ) );
226  } else {
227  return NULL;
228  }
229 
230 }
231 
232 void
234  conformation::Conformation const & conformation( pose.conformation() );
235  xyz_target_ = conformation.xyz( atom_ );
236 }
237 
238 ///@details one line definition "CoordinateConstraint Atom1_Name Atom1_ResNum Atom2_Name Atom2_ResNum Atom1_target_X_coordinate Atom1_target_Y_coordinate Atom1_target_Z_coordinate Func_Type Func_Def"
239 void
241  std::istream& data,
242  core::pose::Pose const& pose,
243  FuncFactory const& func_factory
244 ) {
245  Size res1, res2;
246  std::string tempres1, tempres2;
247  std::string name1, name2;
248  std::string func_type;
250 
251  data
252  >> name1 >> tempres1
253  >> name2 >> tempres2
254  >> xyz_target_.x()
255  >> xyz_target_.y()
256  >> xyz_target_.z()
257  >> func_type;
258 
259  ConstraintIO::parse_residue( pose, tempres1, res1 );
260  ConstraintIO::parse_residue( pose, tempres2, res2 );
261 
262  tr.Debug << "read: " << name1 << " " << name2 << " " << res1 << " " << res2 << " func: " << func_type << std::endl;
263  if ( res1 > pose.total_residue() || res2 > pose.total_residue() ) {
264  tr.Warning << "ignored constraint (no such atom in pose!)"
265  << name1 << " " << name2 << " " << res1 << " " << res2 << std::endl;
266  data.setstate( std::ios_base::failbit );
267  return;
268  }
269 
270  atom_ = named_atom_id_to_atom_id( id::NamedAtomID( name1, res1 ), pose );
271  fixed_atom_ = named_atom_id_to_atom_id( id::NamedAtomID( name2, res2 ), pose );
272  if ( atom_.atomno() == 0 || fixed_atom_.atomno() == 0 ) {
273  tr.Warning << "Error reading atoms: read in atom names("
274  << name1 << "," << name2 << "), "
275  << "and found AtomIDs (" << atom_ << "," << fixed_atom_ << ")" << std::endl;
276  data.setstate( std::ios_base::failbit );
277  return;
278  }
279 
280  func_ = func_factory.new_func( func_type );
281  func_->read_data( data );
282 
283  if ( data.good() ) {
284  //chu skip the rest of line since this is a single line defintion.
285  while( data.good() && (data.get() != '\n') ) {}
286  if ( !data.good() ) data.setstate( std::ios_base::eofbit );
287  }
288 
289  if ( tr.Debug.visible() ) {
290  func_->show_definition( std::cout );
291  std::cout << std::endl;
292  }
293 } // read_def
294 
295 bool
297 {
298  if( !dynamic_cast< CoordinateConstraint const * > ( &other_cst ) ) return false;
299 
300  CoordinateConstraint const & other( static_cast< CoordinateConstraint const & > (other_cst) );
301 
302  if( atom_ != other.atom_ ) return false;
303  if( fixed_atom_ != other.fixed_atom_ ) return false;
304  if( func_ != other.func_ ) return false;
305  if(xyz_target_ != other.xyz_target_ ) return false;
306  if( this->score_type() != other.score_type() ) return false;
307 
308  return true;
309 }
310 
311 
312 // functions
313 Real
314 CoordinateConstraint::func( Real const theta ) const
315 {
316  return func_->func( theta );
317 }
318 
319 // deriv
320 Real
321 CoordinateConstraint::dfunc( Real const theta ) const
322 {
323  return func_->dfunc( theta );
324 }
325 
326 
327 } // constraints
328 } // scoring
329 } // core