Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SICFast.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 // :noTabs=false:tabSize=4:indentSize=4:
4 
6 
9 
10 #include <basic/options/keys/sicdock.OptionKeys.gen.hh>
11 #include <basic/options/option.hh>
12 #include <basic/options/option_macros.hh>
13 #include <numeric/constants.hh>
14 #include <numeric/xyz.functions.hh>
15 #include <numeric/xyz.io.hh>
16 #include <ObjexxFCL/format.hh>
17 #include <ObjexxFCL/string.functions.hh>
18 #include <utility/io/ozstream.hh>
19 #include <utility/string_util.hh>
20 #include <core/id/AtomID_Map.hh>
21 #include <core/scoring/sasa.hh>
23 #include <core/pose/util.hh>
24 
25 namespace protocols {
26 namespace sic_dock {
27 
28 using platform::Size;
29 using platform::Real;
30 using std::string;
31 using utility::vector1;
32 using ObjexxFCL::fmt::I;
33 using ObjexxFCL::fmt::F;
34 using ObjexxFCL::fmt::RJ;
35 using numeric::min;
36 using numeric::max;
37 using std::cout;
38 using std::cerr;
39 using std::endl;
42 
43 template<typename T> inline T sqr(T x) { return x*x; }
44 
45 
47  using namespace ObjexxFCL::fmt;
48  std::ofstream o(fn.c_str());
49  for(Size i = 1; i <= p.size(); ++i) {
50  std::string rn = "VIZ"; o<<"HETATM"<<I(5,i)<<' '<<" CA "<<' '<<rn<<' '<<"A"<<I(4,i)<<" "<<F(8,3,p[i].x())<<F(8,3,p[i].y())<<F(8,3,p[i].z())<<F(6,2,1.0)<<F(6,2,1.0)<<'\n';
51  } o.close();
52 }
53 
54 
55 inline double dist_score( double const & sqdist, double const & start, double const & stop ) {
56  if( sqdist > stop*stop ) {
57  return 0.0;
58  } else if( sqdist < start*start ) {
59  return 1.0;
60  } else {
61  double dist = sqrt( sqdist );
62  return (stop-dist)/(stop-start);
63  //return sqr(1.0 - sqr( (dist - start) / (stop - start) ) );
64  }
65 }
66 
68  CLD(clash_dis),
69  CLD2(sqr(CLD)),
70  BIN(CLD*basic::options::option[basic::options::OptionKeys::sicdock::hash_2D_vs_3D]()),
71  h1_(new xyzStripeHashPose(CLD+0.01)),
72  h2_(new xyzStripeHashPose(CLD+0.01))
73 {}
74 
76  CLD(basic::options::option[basic::options::OptionKeys::sicdock::clash_dis]()),
77  CLD2(sqr(CLD)),
78  BIN(CLD*basic::options::option[basic::options::OptionKeys::sicdock::hash_2D_vs_3D]()),
79  h1_(new xyzStripeHashPose(CLD+0.01)),
80  h2_(new xyzStripeHashPose(CLD+0.01))
81 {}
82 
84  if(h1_) delete h1_;
85  if(h2_) delete h2_;
86 }
87 
88 
89 void
91  core::pose::Pose const & pose
92 ) {
93  init(pose,pose);
94 }
95 
96 void
98  core::pose::Pose const & pose,
99  core::id::AtomID_Map<platform::Real> const & clash_atoms
100 ){
101  init(pose,pose,clash_atoms,clash_atoms);
102 }
103 
104 void
106  core::pose::Pose const & pose1,
107  core::pose::Pose const & pose2
108 ){
109  using core::id::AtomID;
110  core::id::AtomID_Map<platform::Real> clashmap1,clashmap2;
111  core::pose::initialize_atomid_map( clashmap1,pose1,-1.0);
112  core::pose::initialize_atomid_map( clashmap2,pose2,-1.0);
113  for(Size i = 1; i <= pose1.n_residue(); ++i) {
114  for(int j = 1; j <= ((pose1.residue(i).name3()=="GLY")?4:5); ++j)
115  clashmap1[AtomID(j,i)] = pose1.residue(i).atom_type(j).lj_radius();
116  }
117  for(Size i = 1; i <= pose2.n_residue(); ++i) {
118  for(int j = 1; j <= ((pose2.residue(i).name3()=="GLY")?4:5); ++j)
119  clashmap2[AtomID(j,i)] = pose2.residue(i).atom_type(j).lj_radius();
120  }
121  init(pose1,pose2,clashmap1,clashmap2);
122 }
123 
124 void
126  core::pose::Pose const & pose1,
127  core::pose::Pose const & pose2,
128  core::id::AtomID_Map<platform::Real> const & clash_atoms1,
129  core::id::AtomID_Map<platform::Real> const & clash_atoms2
130 ){
131  using core::id::AtomID;
132  h1_->init_with_pose(pose1,clash_atoms1);
133  h2_->init_with_pose(pose2,clash_atoms2);
134  w1_ = cb_weights_from_pose(pose1);
135  w2_ = cb_weights_from_pose(pose2);
136 }
137 
138 
139 inline
140 bool
142  vector1<Vec> const & pb,
143  vector1<Vec> const & pa,
144  double & xmx, double & xmn,
145  double & ymx, double & ymn
146 ){
147  // get bounds for plane hashes
148  double xmx1=-9e9,xmn1=9e9,ymx1=-9e9,ymn1=9e9;
149  xmx=-9e9,xmn=9e9,ymx=-9e9,ymn=9e9;
150  for(vector1<Vec>::const_iterator ia = pb.begin(); ia != pb.end(); ++ia) {
151  xmx1 = max(xmx1,ia->x()); xmn1 = min(xmn1,ia->x());
152  ymx1 = max(ymx1,ia->y()); ymn1 = min(ymn1,ia->y());
153  }
154  for(vector1<Vec>::const_iterator ib = pa.begin(); ib != pa.end(); ++ib) {
155  xmx = max(xmx,ib->x()); xmn = min(xmn,ib->x());
156  ymx = max(ymx,ib->y()); ymn = min(ymn,ib->y());
157  }
158  xmx = min(xmx,xmx1); xmn = max(xmn,xmn1);
159  ymx = min(ymx,ymx1); ymn = max(ymn,ymn1);
160  if(ymn > ymx || xmn > xmx) return false; //utility_exit_with_message("slide disjoint, maybe something wrong.");
161  return true;
162 }
163 
164 
165 inline
166 void
168  vector1<Vec> const & pb,
169  vector1<Vec> const & pa,
170  double const & xmx,
171  double const & xmn,
172  double const & ymx,
173  double const & ymn,
174  double const & BIN,
175  ObjexxFCL::FArray2D<Vec> & ha,
176  ObjexxFCL::FArray2D<Vec> & hb,
177  int & xlb,
178  int & ylb,
179  int & xub,
180  int & yub
181 ){
182  xlb = (int)(xmn/BIN)-2; xub = (int)(xmx/BIN+0.999999999)+2; // one extra on each side for correctness,
183  ylb = (int)(ymn/BIN)-2; yub = (int)(ymx/BIN+0.999999999)+2; // and one extra for outside atoms
184  ha.dimension(xub-xlb+1,yub-ylb+1,Vec(0,0,-9e9));
185  hb.dimension(xub-xlb+1,yub-ylb+1,Vec(0,0, 9e9));
186  int const xsize = xub-xlb+1;
187  int const ysize = yub-ylb+1;
188  for(vector1<Vec>::const_iterator ia = pb.begin(); ia != pb.end(); ++ia) {
189  int const ix = (int)((ia->x()/BIN)-xlb+0.999999999);
190  int const iy = (int)((ia->y()/BIN)-ylb+0.999999999);
191  if( ix < 1 || ix > xsize || iy < 1 || iy > ysize ) continue;
192  if( ha(ix,iy).z() < ia->z() ) ha(ix,iy) = *ia;
193  // bool const test = !( ix < 1 || ix > xsize || iy < 1 || iy > ysize) && ha(ix,iy).z() < ia->z();
194  // ha(ix,iy) = test ? *ia : ha(ix,iy);
195  }
196  for(vector1<Vec>::const_iterator ib = pa.begin(); ib != pa.end(); ++ib) {
197  int const ix = (int)((ib->x()/BIN)-xlb+0.999999999);
198  int const iy = (int)((ib->y()/BIN)-ylb+0.999999999);
199  if( ix < 1 || ix > xsize || iy < 1 || iy > ysize ) continue;
200  if( hb(ix,iy).z() > ib->z() ) hb(ix,iy) = *ib;
201  // bool const test = !( ix < 1 || ix > xsize || iy < 1 || iy > ysize ) && hb(ix,iy).z() > ib->z();
202  // hb(ix,iy) = test ? *ib : hb(ix,iy);
203  }
204 
205 }
206 
207 inline
208 double
210  int const & xlb,
211  int const & ylb,
212  int const & xub,
213  int const & yub,
214  ObjexxFCL::FArray2D<Vec> const & ha,
215  ObjexxFCL::FArray2D<Vec> const & hb,
216  double const & clashdis2
217 ){
218  int const xsize=xub-xlb+1, ysize=yub-ylb+1;
219  double m = 9e9;
220  for(int i = 1; i <= xsize; ++i) { // skip 1 and N because they contain outside atoms (faster than clashcheck?)
221  for(int j = 1; j <= ysize; ++j) {
222  for(int k = -1; k <= 1; ++k) {
223  if(i+k < 1 || i+k > xsize) continue;
224  for(int l = -1; l <= 1; ++l) {
225  if(j+l < 1 || j+l > ysize) continue;
226  double const xa1=ha(i,j).x(),ya1=ha(i,j).y(),xb1=hb(i+k,j+l).x(),yb1=hb(i+k,j+l).y(),d21=(xa1-xb1)*(xa1-xb1)+(ya1-yb1)*(ya1-yb1);
227  if(d21<clashdis2){ double const dz=hb(i+k,j+l).z()-ha(i,j).z()-sqrt(clashdis2-d21); if(dz<m) m=dz; }
228  }
229  }
230  }
231  }
232  return m;
233 }
234 
236  Vec const dof;
239  int contacts;
240  CorrectionVisitor(Vec const & dof_in, Real const & clash_dis_sq_in) : dof(dof_in),clash_dis_sq(clash_dis_sq_in),correction(9e9),contacts(0) {}
242  double const dxy2 = dof.cross(v-c).length_squared();
243  if( dxy2 < clash_dis_sq ){
244  double const dz = dof.dot(v) - dof.dot(c) - sqrt(clash_dis_sq-dxy2);
245  correction = min(dz,correction);
246  contacts++;
247  }
248  }
249 };
250 
251 inline
252 double
254  xyzStripeHashPose *xh,
255  core::kinematics::Stub const & xform_to_struct2_start,
256  vector1<Vec> const & pa,
257  Vec const & ori,
258  double const & clash_dis_sq,
259  double const & mindis_approx
260 ){
261  double mindis = mindis_approx;
262  Mat Rori = rotation_matrix_degrees( (ori.z() < -0.99999) ? Vec(1,0,0) : (Vec(0,0,1)+ori.normalized())/2.0 , 180.0 );
263  Vec hash_ori = xform_to_struct2_start.M.transposed() * ori;
264  while(true){
265  CorrectionVisitor visitor(hash_ori,clash_dis_sq);
266  for(vector1<Vec>::const_iterator ipa = pa.begin(); ipa != pa.end(); ++ipa) {
267  Vec const v = xform_to_struct2_start.global2local(Rori*((*ipa)-Vec(0,0,mindis)));
268  xh->visit_lax(v,visitor);
269  }
270  if(visitor.contacts == 0){
271  mindis = 9e9;
272  break;
273  }
274  mindis += visitor.correction;
275  if( fabs(visitor.correction) < 0.001 ) break;
276  }
277  return mindis; // now fixed
278 }
279 
280 double
282  core::kinematics::Stub const & xa,
283  core::kinematics::Stub const & xb,
284  Vec ori
285 ) const {
286  ori.normalize();
287 
288  // get rotated points
289  utility::vector1<Vec> pa(h1_->natom()), pb(h2_->natom());
290  utility::vector1<Vec>::iterator ipa(pa.begin()),ipb(pb.begin());
291  for(xyzStripeHashPose::const_iterator i = h1_->begin(); i != h1_->end(); ++i,++ipa) *ipa = xa.local2global(*i-h1_->translation());
292  for(xyzStripeHashPose::const_iterator i = h2_->begin(); i != h2_->end(); ++i,++ipb) *ipb = xb.local2global(*i-h2_->translation());
293 
294  double xmx,xmn,ymx,ymn;
295  int xlb,ylb,xub,yub;
296  ObjexxFCL::FArray2D<Vec> ha,hb; // 2D hashes
297 
298  // rotate points, should merge with above
299  Mat rot = rotation_matrix_degrees( (ori.z() < -0.99999) ? Vec(1,0,0) : (Vec(0,0,1)+ori)/2.0 , 180.0 );
300  for(vector1<Vec>::iterator ia = pb.begin(); ia != pb.end(); ++ia) *ia = rot*(*ia);
301  for(vector1<Vec>::iterator ib = pa.begin(); ib != pa.end(); ++ib) *ib = rot*(*ib);
302 
303  if( ! get_bounds_intersection(pb,pa,xmx,xmn,ymx,ymn) ) return 9e9;
304 
305  fill_plane_hash(pb,pa,xmx,xmn,ymx,ymn,BIN,ha,hb,xlb,ylb,xub,yub);
306 
307  double const mindis_approx = get_mindis_with_plane_hashes(xlb,ylb,xub,yub,ha,hb,CLD2);
308  if( fabs(mindis_approx) > 9e8 ) return 9e9;
309 
310  double const mindis = refine_mindis_with_xyzHash(h2_,xb,pa,ori,CLD2,mindis_approx);
311  if( fabs(mindis) > 9e8 ) return 9e9;
312 
313  return -mindis;
314 }
315 
316 
317 
318 
319 } // namespace sic_dock
320 } // namespace protocols