Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ImplicitFastClashCheck.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 src/core/scoring/ImplicitFastClashCheck.cc
11 /// @brief does implicit fast clash checking WRT the provided pose
12 /// @author Will Sheffler (will@sheffler.me)
13 
16 #include <core/pose/Pose.hh>
17 #include <core/id/AtomID.hh>
18 #include <ObjexxFCL/format.hh>
19 #include <numeric/xyz.functions.hh>
20 
21 #include <core/kinematics/Stub.hh>
22 #include <utility/vector1.hh>
23 #include <utility/io/ozstream.hh>
24 
25 
26 namespace protocols {
27 namespace scoring {
28 
29 /// @details Auto-generated virtual destructor
31 
32 using core::pose::Pose;
33 using core::Size;
34 using core::Real;
35 using core::id::AtomID;
37 using namespace numeric;
38 using utility::vector1;
41 
43  pose_ = new core::pose::Pose(pose_in);
45  poses.push_back(pose_in);
46  init_clash_check( poses, clash_dis, ignore );
47 }
48 
50  pose_ = new core::pose::Pose(pose_in);
52  poses.push_back(pose_in);
54  init_clash_check( poses, clash_dis, ignore );
55 }
56 
58  utility::vector1<core::pose::Pose> const & poses_in,
59  core::Real clash_dis,
61 ){
62  pose_ = new core::pose::Pose(poses_in[1]);
63  init_clash_check(poses_in,clash_dis,ignore);
64 }
65 
66 
68  using numeric::min;
69  using numeric::max;
70  using numeric::square;
71  typedef numeric::xyzTriple< Size > CubeDim; // Cube dimensions
72  typedef numeric::xyzTriple< Size > CubeKey; // Cube index-triple key
73 
74  neighbor_cutoff_ = neighbor_cutoff;
75  neighbor_cutoff_sq_ = ( neighbor_cutoff*neighbor_cutoff);
76  // points_.reserve((pose_->n_residue()-ignore.size())*5);
77  // resno_ .reserve((pose_->n_residue()-ignore.size())*5);
78  // atomno_.reserve((pose_->n_residue()-ignore.size())*5);
79  for(utility::vector1<Pose>::const_iterator pi = poses.begin(); pi != poses.end(); ++pi) {
80  for(Size i = 0; i < pi->n_residue(); ++i) {
81  if( std::find(ignore.begin(),ignore.end(),i+1) != ignore.end() ) continue;
82  //Size const natom = min(5ul,pi->residue(i+1).nheavyatoms());
83  Size const natom = pi->residue(i+1).nheavyatoms();
84  for(Size j = 1; j <= natom; ++j) {
85  // TODO could check for point redundance here
86  points_.push_back( pi->xyz(AtomID(j,i+1)) );
87  resno_ .push_back( i+1 );
88  atomno_.push_back( j );
89  }
90  }
91  }
92 
93  Vec bbu( points_[ 1 ] ); // Lower and upper corners of bounding box
94  bbl_ = bbu;
95  for ( Size ii = 2; ii <= points_.size(); ++ii ) { bbl_.min( points_[ ii ] ); bbu.max( points_[ ii ] ); }
96  bbl_ -= 10 * std::numeric_limits< Real >::epsilon();
97  bbu += 10 * std::numeric_limits< Real >::epsilon();
98  Real const side( neighbor_cutoff );
99  assert( side > Real( 0 ) );
100  side_inv_ = ( Real( 1 ) / side );
101  cube_dim_ = CubeDim( // Cube dimensions
102  Size( std::ceil( ( bbu.x() - bbl_.x() ) * side_inv_ ) ), // Test that ceil values == Size values
103  Size( std::ceil( ( bbu.y() - bbl_.y() ) * side_inv_ ) ),
104  Size( std::ceil( ( bbu.z() - bbl_.z() ) * side_inv_ ) )
105  );
106 
107  cubes_.dimension( cube_dim_.x(), cube_dim_.y(), cube_dim_.z() );
108 
109  for ( Size i = 1; i <= points_.size(); ++i ) {
110  Vec const pp( points_[ i ]);
111  CubeKey const cube_key(
112  Size( ( pp.x() - bbl_.x() ) * side_inv_ ) + 1,
113  Size( ( pp.y() - bbl_.y() ) * side_inv_ ) + 1,
114  Size( ( pp.z() - bbl_.z() ) * side_inv_ ) + 1
115  );
116  assert( cube_key.x() <= cube_dim_.x() );
117  assert( cube_key.y() <= cube_dim_.y() );
118  assert( cube_key.z() <= cube_dim_.z() );
119  Size i_index = cubes_.index( cube_key.x(), cube_key.y(), cube_key.z() );
120  cubes_[ i_index ].push_back( i );
121  }
122 }
123 
124 bool ImplicitFastClashCheck::clash_check(Vec const & pp ) const {
125  Size const icx( Size( ( pp.x() - bbl_.x() ) * side_inv_ ) + 1 );
126  Size const icy( Size( ( pp.y() - bbl_.y() ) * side_inv_ ) + 1 );
127  Size const icz( Size( ( pp.z() - bbl_.z() ) * side_inv_ ) + 1 );
128  for ( Size ix = max( icx, Size( 2 ) ) - 1, ixe = min( icx + 1, cube_dim_.x() ); ix <= ixe; ++ix ) {
129  for ( Size iy = max( icy, Size( 2 ) ) - 1, iye = min( icy + 1, cube_dim_.y() ); iy <= iye; ++iy ) {
130  for ( Size iz = max( icz, Size( 2 ) ) - 1, ize = min( icz + 1, cube_dim_.z() ); iz <= ize; ++iz ) {
131  Size cube_index = cubes_.index( ix, iy, iz );
132  if ( cubes_[ cube_index ].size() != 0 ) { // Cube exists
133  for ( vector1<unsigned int>::const_iterator ia = cubes_[ cube_index ].begin(), iae = cubes_[ cube_index ].end(); ia != iae; ++ia ) {
134  Vec const j( points_[*ia] );
135  Real const d_sq( pp.distance_squared( j ) );
136  if ( d_sq <= neighbor_cutoff_sq_ ) {
137  // std::cerr << "CLASHCHECK_FAIL_" << d_sq << "_" << resno_[*ia] << "_" << atomno_[*ia] << " ";
138  return false;
139  }
140  }
141  }
142  }
143  }
144  }
145  return true;
146 }
147 
149  platform::uint count = 0;
150  Size const icx( Size( ( pp.x() - bbl_.x() ) * side_inv_ ) + 1 );
151  Size const icy( Size( ( pp.y() - bbl_.y() ) * side_inv_ ) + 1 );
152  Size const icz( Size( ( pp.z() - bbl_.z() ) * side_inv_ ) + 1 );
153  for ( Size ix = max( icx, Size( 2 ) ) - 1, ixe = min( icx + 1, cube_dim_.x() ); ix <= ixe; ++ix ) {
154  for ( Size iy = max( icy, Size( 2 ) ) - 1, iye = min( icy + 1, cube_dim_.y() ); iy <= iye; ++iy ) {
155  for ( Size iz = max( icz, Size( 2 ) ) - 1, ize = min( icz + 1, cube_dim_.z() ); iz <= ize; ++iz ) {
156  Size cube_index = cubes_.index( ix, iy, iz );
157  if ( cubes_[ cube_index ].size() != 0 ) { // Cube exists
158  for ( vector1<unsigned int>::const_iterator ia = cubes_[ cube_index ].begin(), iae = cubes_[ cube_index ].end(); ia != iae; ++ia ) {
159  Vec const j( points_[*ia] );
160  Real const d_sq( pp.distance_squared( j ) );
161  if ( d_sq <= neighbor_cutoff_sq_ ) {
162  ++count;
163  }
164  }
165  }
166  }
167  }
168  }
169  return count;
170 }
171 
172 bool ImplicitFastClashCheck::clash_check(Vec const & pp, Size resno ) const {
173  Size const icx( Size( ( pp.x() - bbl_.x() ) * side_inv_ ) + 1 );
174  Size const icy( Size( ( pp.y() - bbl_.y() ) * side_inv_ ) + 1 );
175  Size const icz( Size( ( pp.z() - bbl_.z() ) * side_inv_ ) + 1 );
176  for ( Size ix = max( icx, Size( 2 ) ) - 1, ixe = min( icx + 1, cube_dim_.x() ); ix <= ixe; ++ix ) {
177  for ( Size iy = max( icy, Size( 2 ) ) - 1, iye = min( icy + 1, cube_dim_.y() ); iy <= iye; ++iy ) {
178  for ( Size iz = max( icz, Size( 2 ) ) - 1, ize = min( icz + 1, cube_dim_.z() ); iz <= ize; ++iz ) {
179  Size cube_index = cubes_.index( ix, iy, iz );
180  if ( cubes_[ cube_index ].size() != 0 ) { // Cube exists
181  for ( vector1<unsigned int>::const_iterator ia = cubes_[ cube_index ].begin(), iae = cubes_[ cube_index ].end(); ia != iae; ++ia ) {
182  Vec const j( points_[*ia] );
183  // TR << "resno " << resno << " resno_[*ia] " << resno_[*ia] << std::endl;
184  // if(resno_[*ia]==27 && atomno_[*ia]==1) {
185  // std::cout << "clash_check 27 1 " << j.x() << " " << j.y() << " " << j.z() << " " << pp.x() << " " << pp.y() << " " << pp.z() << std::endl;
186  // }
187  if( resno == resno_[*ia] && (atomno_[*ia]==2 || atomno_[*ia]==5) ) {
188  // std::cout << "ignoring same res clash " << resno_[*ia] << " " << atomno_[*ia] << std::endl;
189  continue; // ignore if same res && CA or CB
190  }
191  Real const d_sq( pp.distance_squared( j ) );
192  // if(resno_[*ia]==27 && atomno_[*ia]==1) {
193  // std::cout << "clash_check 27 1 " << d_sq << std::endl;
194  // }
195  if ( d_sq <= neighbor_cutoff_sq_ ) {
196  return false;
197  // if( resno == resno_[*ia] ) TR << "clash from same res with res/atm " << resno_[*ia] << " / " << atomno_[*ia] << std::endl;
198  // if( resno != resno_[*ia] ) return false;
199  // if(sqrt(d_sq)+0.5 <= neighbor_cutoff_) return false;
200  }
201  }
202  }
203  }
204  }
205  }
206  return true;
207 }
208 
209 // bool ImplicitFastClashCheck::clash_check(Pose const & pose, Size refrsd) const {
210 // Stub stubl(pose_->xyz(AtomID(2,1)),pose_->xyz(AtomID(2,2)),pose_->xyz(AtomID(2,3)));
211 // Stub stub1(pose .xyz(AtomID(2,1)),pose .xyz(AtomID(2,2)),pose .xyz(AtomID(2,3)));
212 // for(Size i = 9; i <= pose.residue(refrsd).nheavyatoms(); ++i) {
213 // if( ! clash_check( stubl.local2global(stub1.global2local(pose.xyz(AtomID(i,refrsd+0*pose_->n_residue())))) ) ) return false;
214 // }
215 // return true;
216 // }
217 
218 // bool ImplicitFastClashCheck::clash_check(Stub const & stub, Vec pos) const {
219 // // Stub stubl(pose_->xyz(AtomID(2,1)),pose_->xyz(AtomID(2,2)),pose_->xyz(AtomID(2,3)));
220 // // return clash_check( stubl.local2global(stub.global2local(pos)) );
221 // return clash_check( stub.local2global(pos) );
222 // }
223 
224 bool ImplicitFastClashCheck::clash_check_trimer(Pose const & pose, Size refrsd) const {
225  Stub stubl(pose_->xyz(AtomID(2,1)),pose_->xyz(AtomID(2,2)),pose_->xyz(AtomID(2,3)));
226  Stub stub1(pose .xyz(AtomID(2,1)),pose .xyz(AtomID(2,2)),pose .xyz(AtomID(2,3)));
227  for(Size i = 1; i <= pose.residue(refrsd).nheavyatoms(); ++i) {
228  if(i > 9) if( ! clash_check( stubl.local2global(stub1.global2local(pose.xyz(AtomID(i,refrsd+0*pose_->n_residue())))) ) ) return false;
229  if( ! clash_check( stubl.local2global(stub1.global2local(pose.xyz(AtomID(i,refrsd+1*pose_->n_residue())))) ) ) return false;
230  if( ! clash_check( stubl.local2global(stub1.global2local(pose.xyz(AtomID(i,refrsd+2*pose_->n_residue())))) ) ) return false;
231  }
232  Vec cen = stubl.local2global(stub1.global2local(Vec(0,0,0)));
233  Vec axs = stubl.local2global(stub1.global2local(Vec(0,0,1)));
234  axs = axs - cen;
235  Mat rot = numeric::rotation_matrix_degrees(axs,120.0);
236  for(vector1<Vec>::const_iterator i = points_.begin(); i != points_.end(); ++i) {
237  if( ! clash_check( rot*(*i-cen)+cen ) ) return false;
238  }
239  return true;
240 }
241 
242 void ImplicitFastClashCheck::dump_debug_pdb( utility::io::ozstream & out, core::kinematics::Stub const & stub, char chain ) const {
243  using namespace ObjexxFCL::fmt;
244  for(Size i = 1; i <= points_.size(); ++i) {
245  numeric::xyzVector<Real> p = stub.global2local(points_[i]);
246  std::string rname = pose_->residue(resno_[i]).name3();
247  std::string aname = pose_->residue(resno_[i]).atom_name(atomno_[i]);
248  out << "HETATM" + I( 5, i ) + " "+aname+" "+rname+" "+chain
249  + I( 4, resno_[i] ) + " "
250  + F( 8, 3, p.x() )
251  + F( 8, 3, p.y() )
252  + F( 8, 3, p.z() )
253  + F( 6, 2, 0.0 ) + ' '
254  + F( 5, 2, 0.0);
255  out << "" << std::endl;
256  }
257 }
258 
259 void ImplicitFastClashCheck::dump_debug_pdb( std::string const & fname, core::kinematics::Stub const & stub, char chain ) const {
260  utility::io::ozstream out(fname);
261  dump_debug_pdb(out,stub,chain);
262 }
263 
265 
266  bool bclash = true;
268  for( i = points_.begin(); i != points_.end(); ++i ) {
269  Real const d_sq( pp.distance_squared( *i ) );
270  if ( d_sq <= neighbor_cutoff_sq_ ) {
271  bclash = false;
272  return true;
273  }
274  }
275  return false;
276 
277  bool clash = clash_check(pp);
278 
279  if(clash != bclash) utility_exit_with_message("clash check test fails!!!");
280 
281  return clash;
282 }
283 
284 
285 } // namespace scoring {
286 } // namespace