Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PocketGrid.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 protocols/pockets/PocketGrid.cc
11 /// @brief protocols::pockets::PocketGrid functions
12 /// @author David Johnson
13 /// @author Ragul Gowthaman
14 
16 
17 // Core Headers
18 #include <basic/options/option.hh>
19 #include <basic/options/keys/OptionKeys.hh>
20 #include <basic/options/keys/fingerprint.OptionKeys.gen.hh>
21 // AUTO-REMOVED #include <core/scoring/ScoreType.hh>
23 // AUTO-REMOVED #include <core/scoring/EnergyMap.hh>
25 // AUTO-REMOVED #include <core/scoring/constraints/ConstraintSet.hh>
26 #include <core/pose/PDBInfo.hh>
27 #include <core/pose/Pose.hh>
30 #include <basic/Tracer.hh>
31 #include <ObjexxFCL/string.functions.hh>
32 #include <basic/options/keys/pocket_grid.OptionKeys.gen.hh>
33 #include <basic/options/keys/out.OptionKeys.gen.hh>
34 #include <utility/io/ozstream.hh>
35 #include <utility/io/izstream.hh>
36 #include <numeric/xyz.functions.fwd.hh>
37 
38 // Utility Headers
39 #include <string>
40 #include <algorithm>
41 #include <iomanip>
42 #include <ostream>
43 #include <sstream>
44 #include <cmath>
45 #include <map>
46 
47 // Eigen header for SVD support
48 #include <Eigen/SVD>
49 
50 #include <utility/vector1.hh>
51 #include <numeric/xyz.functions.hh>
52 
53 //Auto Headers
54 #include <numeric/random/random.fwd.hh>
55 
56 namespace protocols {
57 namespace pockets {
58 
59 static basic::Tracer TR("core.grid.Pockets.PocketGrid");
60 
61 // Visual Studio is more particular about implicit upconversion than gcc
62 #ifdef _WIN32
63 core::Real floor(core::Size x) {
64  return std::floor((double) x);
65 }
66 
68  return std::pow((double) x, (double) y);
69 }
70 
71 core::Real sqrt(core::Size x) {
72  return std::sqrt((double) x);
73 }
74 #endif
75 
77  Cxyz point;
78  point.x=x;
79  minX=x;
80  maxX=x;
81  point.y=y;
82  minY=y;
83  maxY=y;
84  point.z=z;
85  minZ=z;
86  maxZ=z;
87  points_.push_back(point);
88  target=false;
89  count_=1;
90  step=step_;
91 }
92 
94  Cxyz point;
95  point.x=x;
96  point.y=y;
97  point.z=z;
98  points_.push_back(point);
99 }
100 
101 
103  count_ = old.count_;
104  points_ = old.points_;
105  target = old.target;
106  maxX = old.maxX; maxY = old.maxY; maxZ = old.maxZ;
107  minX = old.minX; minY = old.minY; minZ = old.minZ;
108  step = old.step;
109 }
110 
111 
112 bool PCluster::isClose(PCluster const & c2) const{
113  if (minX>c2.maxX+1) return false;
114  if ((int)maxX<(int)c2.minX-1) return false;
115  if (minY>c2.maxY+1) return false;
116  if ((int)maxY<(int)c2.minY-1) return false;
117  if (minZ>c2.maxZ+1) return false;
118  if ((int)maxZ<(int)c2.minZ-1) return false;
119  return true;
120 }
121 
123  if (c2.points_.size()>100 &&points_.size()>100){
124  std::list<Cxyz>::iterator i = points_.begin();
125  int icount=0;
126  while (i!=points_.end()){
127  std::list<Cxyz>::iterator j = c2.points_.begin();
128  int ictend=std::min(500,(int)points_.size()-icount);
129  for (int ict=0; ict<ictend; ++i, ++ict){
130  int jcount=0;
131  while (j!=c2.points_.end()){
132  int jctend=std::min(500,(int)c2.points_.size()-jcount);
133  for (int jct=0; jct<jctend; ++j, ++jct){
134  if (abs((int) i->x - (int) j->x)<=1)
135  if (abs((int) i->y - (int) j->y)<=1)
136  if (abs((int) i->z - (int) j->z)<=1){
137  points_.splice(points_.end(), c2.points_);
138  minX=std::min(minX, c2.minX);
139  minY=std::min(minY, c2.minY);
140  minZ=std::min(minZ, c2.minZ);
141  maxX=std::max(maxX, c2.maxX);
142  maxY=std::max(maxY, c2.maxY);
143  maxZ=std::max(maxZ, c2.maxZ);
144  return true;
145  }
146  }
147  jcount+=jctend;
148  }
149  }
150  icount+=ictend;
151  }
152 
153  }else{
154 
155  for (std::list<Cxyz>::iterator i = points_.begin(); i!=points_.end(); ++i){
156  for (std::list<Cxyz>::iterator j = c2.points_.begin(); j!=c2.points_.end(); ++j){
157  if (abs((int) i->x - (int) j->x)<=1)
158  if (abs((int) i->y - (int) j->y)<=1)
159  if (abs((int) i->z - (int) j->z)<=1){
160 
161  minX=std::min(minX, c2.minX);
162  minY=std::min(minY, c2.minY);
163  minZ=std::min(minZ, c2.minZ);
164  maxX=std::max(maxX, c2.maxX);
165  maxY=std::max(maxY, c2.maxY);
166  maxZ=std::max(maxZ, c2.maxZ);
167  points_.splice(points_.end(), c2.points_);
168  return true;
169  }
170  }
171  }
172  }
173  return false;
174 }
175 
177  clusters_.clear();
178 }
179 
181  if (this != &old){
182  clusters_ = old.clusters_;
183  }
184  return *this;
185 }
186 
188  clusters_.clear();
189 }
190 
191 std::_List_iterator<protocols::pockets::PCluster> PClusterSet::add(core::Size x, core::Size y, core::Size z, core::Real step){
192  PCluster tmp(x,y,z, step);
193  clusters_.push_front(tmp);
194  return clusters_.begin();
195 }
196 
197 void PClusterSet::join(std::list<PCluster>::iterator c1, std::list<PCluster>::iterator c2){
198  c1->points_.splice(c1->points_.end(), c2->points_);
199  clusters_.erase(c2);
200 }
201 
203  for (std::list<PCluster>::iterator i = clusters_.begin(); i!=clusters_.end(); ++i){
204  for (std::list<PCluster>::iterator j = i; j!=clusters_.end(); ++j){
205  if (i==j) continue;
206  if (j->isClose(*i)) {
207  if (j->testNeighbor(*i)) {
208  std::list<PCluster>::iterator oldI = i;
209  i--;
210  clusters_.erase(oldI);
211  break;
212  }
213  }
214  }
215  }
216 }
217 
218 core::Real PClusterSet::getLargestClusterSize( core::Real const & stepSize, core::Real const & minClusterSize ){
219  core::Real largest_size=0.;
220  for (std::list<PCluster>::iterator i = clusters_.begin(); i!=clusters_.end(); ++i){
221  if (!i->isTarget()) continue;
222  if (i->size()>largest_size){
223  largest_size=i->size();
224  }
225  }
226  largest_size = largest_size*pow(stepSize, 3)-minClusterSize;
227  if (largest_size > 0) return largest_size;
228 
229  return 0;
230 }
231 
232 core::Real PClusterSet::getNetClusterSize( core::Real const & stepSize, core::Real const & minClusterSize ){
233  core::Real total_size=0.;
234  for (std::list<PCluster>::iterator i = clusters_.begin(); i!=clusters_.end(); ++i){
235  if (!i->isTarget()) continue;
236  if (i->size()*pow(stepSize, 3) >minClusterSize){
237  total_size+=i->size()*pow(stepSize, 3)-minClusterSize;
238  }
239  }
240  if (total_size>0) return total_size;
241 
242  return 0;
243 }
244 
246  grid_.clear();
247  pockets_.clear();
248  clusters_.clear();
249 }
250 
252  clear();
253  grid_.resize(xdim_);
254  pockets_.resize(xdim_);
255  for (core::Size tx=0;tx<xdim_;tx++){
256  grid_[tx].resize(ydim_);
257  pockets_[tx].resize(ydim_);
258  for (core::Size ty=0;ty<ydim_;ty++){
259  grid_[tx][ty].resize(zdim_, EMPTY);
260  pockets_[tx][ty].resize(zdim_, EMPTY);
261  }
262  }
263 }
264 
265 
267 
268  using namespace basic::options;
269  spacing_=option[ OptionKeys::pocket_grid::pocket_grid_spacing ]();
270  if (option[ OptionKeys::pocket_grid::pocket_grid_size ]()>0){
271  size_x_ = option[ OptionKeys::pocket_grid::pocket_grid_size ]();
272  size_y_ = option[ OptionKeys::pocket_grid::pocket_grid_size ]();
273  size_z_ = option[ OptionKeys::pocket_grid::pocket_grid_size ]();
274  }else{
275  size_x_ = option[ OptionKeys::pocket_grid::pocket_grid_size_x ]();
276  size_y_ = option[ OptionKeys::pocket_grid::pocket_grid_size_y ]();
277  size_z_ = option[ OptionKeys::pocket_grid::pocket_grid_size_z ]();
278  }
279  restrictSize_ = option[ OptionKeys::pocket_grid::pocket_restrict_size ]();
280  maxLen_ = option[ OptionKeys::pocket_grid::pocket_max_spacing ]();
281  limit_x_ = size_x_*2;
282  limit_y_ = size_y_*2;
283  limit_z_ = size_z_*2;
284  probe_rad_=option[ OptionKeys::pocket_grid::pocket_probe_radius ]();
285  side_chains_only_=option[ OptionKeys::pocket_grid::pocket_side ]();
286  markpsp_=option[ OptionKeys::pocket_grid::pocket_psp ]();
287  marksps_=option[ OptionKeys::pocket_grid::pocket_sps ]();
288  surf_score_=option[ OptionKeys::pocket_grid::pocket_surface_score ]();
289  surf_dist_=option[ OptionKeys::pocket_grid::pocket_surface_dist ]();
290  bur_score_=option[ OptionKeys::pocket_grid::pocket_buried_score ]();
291  bur_dist_=option[ OptionKeys::pocket_grid::pocket_buried_dist ]();
292  minPockSize_=option[ OptionKeys::pocket_grid::pocket_min_size ]();
293  maxPockSize_=option[ OptionKeys::pocket_grid::pocket_max_size ]();
294  rot_mat_ = numeric::x_rotation_matrix_degrees((core::Real)0);
295 
296 }
297 
298 
300  utility::pointer::ReferenceCount()
301 {
302 
304 
305  xdim_ = gr.xdim_; ydim_ = gr.ydim_; zdim_ = gr.zdim_;
306  xcorn_ = gr.xcorn_; ycorn_ = gr.ycorn_; zcorn_ = gr.zcorn_;
307  stepSize_ = gr.stepSize_;
308  markpsp_ = gr.markpsp_;
309  marksps_ = gr.marksps_;
310  tag_=gr.tag_;
311  pdbno_=gr.pdbno_;
313 
314  grid_.resize(xdim_);
315  pockets_.resize(xdim_);
316  for (core::Size tx=0;tx<xdim_;tx++){
317  grid_[tx].resize(ydim_);
318  pockets_[tx].resize(ydim_);
319  for (core::Size ty=0;ty<ydim_;ty++){
320  grid_[tx][ty].resize(zdim_);
321  pockets_[tx][ty].resize(zdim_);
322  for (core::Size tz=0;tz<zdim_;tz++){
323  grid_[tx][ty][tz] = gr.grid_[tx][ty][tz];
324  pockets_[tx][ty][tz] = gr.pockets_[tx][ty][tz];
325  }
326  }
327  }
328 
329 }
330 
332 
334  if (this != &gr){
335 
336  xdim_ = gr.xdim_; ydim_ = gr.ydim_; zdim_ = gr.zdim_;
337  xcorn_ = gr.xcorn_; ycorn_ = gr.ycorn_; zcorn_ = gr.zcorn_;
338  stepSize_ = gr.stepSize_;
339  markpsp_ = gr.markpsp_;
340  marksps_ = gr.marksps_;
341  tag_=gr.tag_;
342  pdbno_=gr.pdbno_;
344 
345  grid_.resize(xdim_);
346  pockets_.resize(xdim_);
347  for (core::Size tx=0;tx<xdim_;tx++){
348  grid_[tx].resize(ydim_);
349  pockets_[tx].resize(ydim_);
350  for (core::Size ty=0;ty<ydim_;ty++){
351  grid_[tx][ty].resize(zdim_);
352  pockets_[tx][ty].resize(zdim_);
353  for (core::Size tz=0;tz<zdim_;tz++){
354  grid_[tx][ty][tz] = gr.grid_[tx][ty][tz];
355  pockets_[tx][ty][tz] = gr.pockets_[tx][ty][tz];
356  }
357  }
358  }
359  }
360  return *this;
361 }
362 
363 
366 }
367 
371  using namespace basic::options;
372  tag_=option[ OptionKeys::out::output_tag ]();
373 }
374 
375 PocketGrid::PocketGrid( std::vector< core::conformation::ResidueOP > const & central_rsds ) {
378  using namespace basic::options;
379  tag_=option[ OptionKeys::out::output_tag ]();
380 }
381 
383  core::Real x,y,z;
384  x = (int) (numeric::random::uniform() *89 +1);
385  y = (int) (numeric::random::uniform() *89 +1);
386  z = (int) (numeric::random::uniform() *89 +1);
387  rot_mat_ = numeric::x_rotation_matrix_degrees(x);
388  rot_mat_ = rot_mat_ * numeric::y_rotation_matrix_degrees(y);
389  rot_mat_ = rot_mat_ * numeric::z_rotation_matrix_degrees(z);
390 }
391 
393  rot_mat_ = numeric::x_rotation_matrix_degrees((core::Real)0);
394 }
395 
397  numeric::xyzVector<core::Real> coord(x,y,z);
398  coord = rot_mat_ * coord;
399  return coord;
400 }
401 
402 void PocketGrid::initialize( core::Real const & xc, core::Real const & yc, core::Real const & zc, core::Real x, core::Real y, core::Real z, core::Real const & stepSize, bool psp, bool sps){
403  markpsp_=psp;
404  marksps_=sps;
405  stepSize_=stepSize;
406  xdim_=2*(core::Size)ceil(x/stepSize_)+1;
407  ydim_=2*(core::Size)ceil(y/stepSize_)+1;
408  zdim_=2*(core::Size)ceil(z/stepSize_)+1;
409  init();
410  recenter(xc,yc,zc);
411  pdbno_=0;
412 }
413 
414 PocketGrid::PocketGrid( core::Real const & xc, core::Real const & yc, core::Real const & zc, core::Real x, core::Real y, core::Real z, core::Real const & stepSize ){
416  initialize(xc, yc, zc, x, y, z, stepSize, markpsp_, marksps_);
417  using namespace basic::options;
418  tag_=option[ OptionKeys::out::output_tag ]();
419 }
420 
423  initialize(xc, yc, zc, x, y, z, spacing_, markpsp_, marksps_);
424  using namespace basic::options;
425  tag_=option[ OptionKeys::out::output_tag ]();
426 }
427 
428 PocketGrid::PocketGrid( core::Real const & xc, core::Real const & yc, core::Real const & zc, core::Real x, core::Real y, core::Real z, core::Real const & stepSize, bool psp, bool sps){
430  initialize(xc, yc, zc, x, y, z, stepSize, psp, sps);
431  using namespace basic::options;
432  tag_=option[ OptionKeys::out::output_tag ]();
433 }
434 
435 void PocketGrid::initialize (core::Real const & xc, core::Real const & yc, core::Real const & zc, core::Real x, core::Real const & stepSize, bool psp, bool sps){
436  markpsp_=psp;
437  marksps_=sps;
438  stepSize_=stepSize;
439  xdim_=2*(core::Size)ceil(x/stepSize_)+1;
440  ydim_=2*(core::Size)ceil(x/stepSize_)+1;
441  zdim_=2*(core::Size)ceil(x/stepSize_)+1;
442  init();
443  recenter(xc,yc,zc);
444  pdbno_=0;
445 }
446 
447 PocketGrid::PocketGrid (core::Real const & xc, core::Real const & yc, core::Real const & zc, core::Real x, core::Real const & stepSize, bool psp, bool sps){
449  initialize(xc, yc, zc, x, stepSize, psp, sps);
450  using namespace basic::options;
451  tag_=option[ OptionKeys::out::output_tag ]();
452 }
453 
454 void PocketGrid::initialize (core::Vector const & center, core::Real x, core::Real const & stepSize, bool psp, bool sps){
455  markpsp_=psp;
456  marksps_=sps;
457  stepSize_=stepSize;
458  core::Real xc=center(1);
459  core::Real yc=center(2);
460  core::Real zc=center(3);
461  xdim_=2*(core::Size)ceil(x/stepSize_)+1;
462  ydim_=2*(core::Size)ceil(x/stepSize_)+1;
463  zdim_=2*(core::Size)ceil(x/stepSize_)+1;
464  init();
465  recenter(xc,yc,zc);
466  pdbno_=0;
467 }
468 
469 PocketGrid::PocketGrid (core::Vector const & center, core::Real x, core::Real const & stepSize, bool psp, bool sps){
471  initialize(center, x, stepSize, psp, sps);
472  using namespace basic::options;
473  tag_=option[ OptionKeys::out::output_tag ]();
474 }
475 
476 void PocketGrid::initialize (core::Vector const & center, core::Real const & x, core::Real const & y, core::Real const & z, core::Real const & stepSize, bool psp, bool sps){
477  markpsp_=psp;
478  marksps_=sps;
479  stepSize_=stepSize;
480  xdim_=2*(core::Size)ceil(x/stepSize_)+1;
481  ydim_=2*(core::Size)ceil(y/stepSize_)+1;
482  zdim_=2*(core::Size)ceil(z/stepSize_)+1;
483  init();
484  recenter(center);
485  pdbno_=0;
486 }
487 
488 void PocketGrid::initialize (core::conformation::Residue const & central_rsd, core::Real const & x, core::Real const & y, core::Real const & z, core::Real const & stepSize, bool psp, bool sps){
489  markpsp_=psp;
490  marksps_=sps;
491  stepSize_=stepSize;
492  xdim_=2*(core::Size)ceil(x/stepSize_)+1;
493  ydim_=2*(core::Size)ceil(y/stepSize_)+1;
494  zdim_=2*(core::Size)ceil(z/stepSize_)+1;
495  init();
496  recenter(central_rsd);
497  pdbno_=0;
498 }
499 
500 void PocketGrid::initialize (std::vector< core::conformation::ResidueOP > const & central_rsds, core::Real const & x, core::Real const & y, core::Real const & z, core::Real const & stepSize, bool psp, bool sps){
501  markpsp_=psp;
502  marksps_=sps;
503  stepSize_=stepSize;
504  xdim_=2*(core::Size)ceil(x/stepSize_)+1;
505  ydim_=2*(core::Size)ceil(y/stepSize_)+1;
506  zdim_=2*(core::Size)ceil(z/stepSize_)+1;
507  init();
508  recenter(central_rsds);
509  pdbno_=0;
510 }
511 
512 PocketGrid::PocketGrid (core::Vector const & center, core::Real const & x, core::Real const & y, core::Real const & z, core::Real const & stepSize, bool psp, bool sps){
514  initialize(center, x, y, z, stepSize, psp, sps);
515  using namespace basic::options;
516  tag_=option[ OptionKeys::out::output_tag ]();
517 }
518 
519 void PocketGrid::recenter(core::Real const & xc, core::Real const & yc, core::Real const & zc){
520  init();
521  clusters_.clear();
522  numeric::xyzVector<core::Real> rpoint = rotatePoint(xc, yc, zc);
523  xcorn_=rpoint.x()-((xdim_-1)/2*stepSize_);
524  ycorn_=rpoint.y()-((ydim_-1)/2*stepSize_);
525  zcorn_=rpoint.z()-((zdim_-1)/2*stepSize_);
526 }
527 
529 
530  assert( central_rsd.is_protein() );
531  core::Vector center;
532  center(1)=0.0;
533  center(2)=0.0;
534  center(3)=0.0;
535  core::Size count=0;
536  for(Size i = 1, i_end = central_rsd.nheavyatoms(); i <= i_end; ++i) {
537  if (central_rsd.atom(i).type()<18||central_rsd.atom(i).type()>21){
538  center(1)+=central_rsd.atom(i).xyz()(1);
539  center(2)+=central_rsd.atom(i).xyz()(2);
540  center(3)+=central_rsd.atom(i).xyz()(3);
541  count++;
542  }
543  }
544  if (count) {
545  center(1)/=count;
546  center(2)/=count;
547  center(3)/=count;
548  }
549  recenter(center(1), center(2), center(3));
550 
551 }
552 
553 void PocketGrid::recenter( std::vector< core::conformation::ResidueOP > const & central_rsds ){
554  core::Vector center;
555  center(1)=0.0;
556  center(2)=0.0;
557  center(3)=0.0;
558  core::Size count=0;
559  int sz = central_rsds.size();
560  assert (sz>0);
561  for (int rnum = 0; rnum < sz; ++rnum){
562  core::conformation::ResidueOP central_rsd = central_rsds[rnum];
563 
564  assert( central_rsd->is_protein() );
565  for(Size i = 1, i_end = central_rsd->nheavyatoms(); i <= i_end; ++i) {
566  if (central_rsd->atom(i).type()<18||central_rsd->atom(i).type()>21){
567  center(1)+=central_rsd->atom(i).xyz()(1);
568  center(2)+=central_rsd->atom(i).xyz()(2);
569  center(3)+=central_rsd->atom(i).xyz()(3);
570  count++;
571  }
572  }
573  }
574  if (count) {
575  center(1)/=count;
576  center(2)/=count;
577  center(3)/=count;
578  }
579  recenter(center(1), center(2), center(3));
580 
581 }
582 void PocketGrid::recenter(core::Vector const & center){
583  recenter(center(1), center(2), center(3));
584 }
585 
586 std::vector< core::conformation::ResidueOP > PocketGrid::getRelaxResidues( core::pose::Pose const & input_pose, std::string const & resids ) {
587  std::vector< core::conformation::ResidueOP > residues;
588 
589  const std::string & delimiters = ",";
590  // Skip delimiters at beginning.
591  std::string::size_type lastPos = resids.find_first_not_of(delimiters, 0);
592 
593  // Find first "non-delimiter".
594  std::string::size_type pos = resids.find_first_of(delimiters, lastPos);
595 
596  while ( std::string::npos != pos || std::string::npos != lastPos ) {
597  std::string const & resid = resids.substr( lastPos, pos - lastPos );
598  lastPos = resids.find_first_not_of(delimiters, pos);
599  pos = resids.find_first_of(delimiters, lastPos);
600 
601  int central_relax_pdb_number;
602  char chain = ' ';
603  std::size_t fpos( resid.find(':') );
604  if ( fpos != std::string::npos ) {
605  central_relax_pdb_number = ObjexxFCL::int_of( resid.substr(0,fpos) );
606  if (fpos != resid.size()-1 ) {
607  chain = resid[ fpos+1 ];
608  }
609  } else {
610  central_relax_pdb_number = ObjexxFCL::int_of( resid );
611  }
612 
613  for ( int j = 1, resnum = input_pose.total_residue(); j <= resnum; ++j ) {
614  if ( input_pose.pdb_info()->number(j) == central_relax_pdb_number ) {
615  if (chain != ' '){
616  if ( input_pose.pdb_info()->chain(j) == chain ) {
617  residues.push_back(input_pose.conformation().residue(j).clone());
618  continue;
619  }
620  }else{
621  residues.push_back(input_pose.conformation().residue(j).clone());
622  continue;
623  }
624  }
625  }
626  }
627  return residues;
628 }
629 
631  thr=(core::Size)std::floor((core::Real)(thr)/stepSize_+.5);
632  core::Size thr2=(core::Size)std::floor(((core::Real)(thr)/stepSize_)/sqrt(3.)+.5);
633  core::Size max1=(core::Size)std::floor(max/stepSize_+.5);
634  core::Size max2=(core::Size)std::floor((max/stepSize_)/sqrt(3.)+.5);
635  newSearch(thr, thr2, max1, max2);
636 }
637 
639  thr=(core::Size)std::floor((core::Real)(thr)/stepSize_+.5);
640  core::Size thr2=(core::Size)std::floor(((core::Real)(thr)/stepSize_)/sqrt(2.)+.5);
641  core::Size max1=(core::Size)std::floor(max/stepSize_+.5);
642  core::Size max2=(core::Size)std::floor((max/stepSize_)/sqrt(2.)+.5);
643  newSearch(thr, thr2, max1, max2, true);
644 }
645 
647  thr=(core::Size)std::floor((core::Real)(thr)/stepSize_+.5);
648  core::Size thr2=(core::Size)std::floor(((core::Real)(thr)/stepSize_)/sqrt(2.)+.5);
649  core::Size max1=(core::Size)std::floor(max/stepSize_+.5);
650  core::Size max2=(core::Size)std::floor((max/stepSize_)/sqrt(2.)+.5);
651  newSearch(thr, thr2, max1, max2, false, true);
652 }
653 
655  if (grid_[x][y][z]!=TP_POCKET && grid_[x][y][z]!=POCKET ){
656  return false;
657  }
658  if (pockets_[x][y][z]!=EMPTY) return false;
659 
660  grid_[x][y][z]=TP_POCKET;
661  pockets_[x][y][z]=TP_POCKET;
662  bool touching=false;
663  core::Size x1,y1,z1;
664  if (x==0) x1=0;
665  else x1=x-1;
666  if (y==0) y1=0;
667  else y1=y-1;
668  if (z==0) z1=0;
669  else z1=z-1;
670  for(core::Size x2=x1; x2<std::min(xdim_, x+2); ++x2){
671  for(core::Size y2=y1; y2<std::min(ydim_, y+2); ++y2){
672  for(core::Size z2=z1; z2<std::min(zdim_, z+2); ++z2){
673  touching=touching || fill(x2,y2,z2);
674  }}}
675  return touching;
676 }
677 
679  bool touching=false;
680  if ((x==0) || (y==0)||(z==0)) return true;
681  if ((x==xdim_-1) || (y==ydim_-1)||(z==zdim_-1)) return true;
682  if (grid_[x-1][y][z]==EMPTY)return true;
683  if (grid_[x+1][y][z]==EMPTY)return true;
684  if (grid_[x][y-1][z]==EMPTY)return true;
685  if (grid_[x][y+1][z]==EMPTY)return true;
686  if (grid_[x][y][z-1]==EMPTY)return true;
687  if (grid_[x][y][z+1]==EMPTY)return true;
688 
689  return touching;
690 }
691 
693  if (x!=0) if (grid_[x-1][y][z]==EMPTY) return true;
694  if (y!=0) if (grid_[x][y-1][z]==EMPTY) return true;
695  if (z!=0) if (grid_[x][y][z-1]==EMPTY) return true;
696  if ((x!=xdim_-1)) if (grid_[x+1][y][z]==EMPTY)return true;
697  if ((y!=ydim_-1)) if (grid_[x][y+1][z]==EMPTY)return true;
698  if ((z!=zdim_-1)) if (grid_[x][y][z+1]==EMPTY)return true;
699  return false;
700 }
701 
703  if (x!=0) {
704  if (grid_[x-1][y][z]==HSURFACE) return true;
705  if (grid_[x-1][y][z]==PSURFACE) return true;
706  if (grid_[x-1][y][z]==T_SURFACE) return true;
707  }
708  if (y!=0) {
709  if (grid_[x][y-1][z]==HSURFACE) return true;
710  if (grid_[x][y-1][z]==PSURFACE) return true;
711  if (grid_[x][y-1][z]==T_SURFACE) return true;
712  }
713  if (z!=0) {
714  if (grid_[x][y][z-1]==HSURFACE) return true;
715  if (grid_[x][y][z-1]==PSURFACE) return true;
716  if (grid_[x][y][z-1]==T_SURFACE) return true;
717  }
718  if ((x!=xdim_-1)) {
719  if (grid_[x+1][y][z]==HSURFACE) return true;
720  if (grid_[x+1][y][z]==PSURFACE) return true;
721  if (grid_[x+1][y][z]==T_SURFACE) return true;
722  }
723  if ((y!=ydim_-1)) {
724  if (grid_[x][y+1][z]==HSURFACE) return true;
725  if (grid_[x][y+1][z]==PSURFACE) return true;
726  if (grid_[x][y+1][z]==T_SURFACE) return true;
727  }
728  if ((z!=zdim_-1)) {
729  if (grid_[x][y][z+1]==HSURFACE) return true;
730  if (grid_[x][y][z+1]==PSURFACE) return true;
731  if (grid_[x][y][z+1]==T_SURFACE) return true;
732  }
733  return false;
734 }
735 
736 
737 bool PocketGrid::touchesSurface(core::Size x, core::Size y,core::Size z, bool polar, bool either) const {
738  bool touching=false;
739  if (polar){
740  if (x!=0) if (grid_[x-1][y][z]==PSURFACE)return true;
741  if (x!=xdim_-1) if (grid_[x+1][y][z]==PSURFACE)return true;
742  if (y!=0) if (grid_[x][y-1][z]==PSURFACE)return true;
743  if (y!=ydim_-1) if (grid_[x][y+1][z]==PSURFACE)return true;
744  if (z!=0) if (grid_[x][y][z-1]==PSURFACE)return true;
745  if (z!=zdim_-1) if (grid_[x][y][z+1]==PSURFACE)return true;
746  }else{
747  if (x!=0) if (grid_[x-1][y][z]==HSURFACE)return true;
748  if (x!=xdim_-1) if (grid_[x+1][y][z]==HSURFACE)return true;
749  if (y!=0) if (grid_[x][y-1][z]==HSURFACE)return true;
750  if (y!=ydim_-1) if (grid_[x][y+1][z]==HSURFACE)return true;
751  if (z!=0) if (grid_[x][y][z-1]==HSURFACE)return true;
752  if (z!=zdim_-1) if (grid_[x][y][z+1]==HSURFACE)return true;
753  }
754  if (either){
755  if (x!=0) if (grid_[x-1][y][z]==PSURFACE)return true;
756  if (x!=xdim_-1) if (grid_[x+1][y][z]==PSURFACE)return true;
757  if (y!=0) if (grid_[x][y-1][z]==PSURFACE)return true;
758  if (y!=ydim_-1) if (grid_[x][y+1][z]==PSURFACE)return true;
759  if (z!=0) if (grid_[x][y][z-1]==PSURFACE)return true;
760  if (z!=zdim_-1) if (grid_[x][y][z+1]==PSURFACE)return true;
761  if (x!=0) if (grid_[x-1][y][z]==HSURFACE)return true;
762  if (x!=xdim_-1) if (grid_[x+1][y][z]==HSURFACE)return true;
763  if (y!=0) if (grid_[x][y-1][z]==HSURFACE)return true;
764  if (y!=ydim_-1) if (grid_[x][y+1][z]==HSURFACE)return true;
765  if (z!=0) if (grid_[x][y][z-1]==HSURFACE)return true;
766  if (z!=zdim_-1) if (grid_[x][y][z+1]==HSURFACE)return true;
767  }
768  return touching;
769 }
770 
772  for(core::Size x=0; x<xdim_; ++x){
773  for(core::Size y=0; y<ydim_; ++y){
774  for(core::Size z=0; z<zdim_; ++z){
775  pockets_[x][y][z]=EMPTY;
776  }}}
777  for(core::Size x=0; x<xdim_; ++x){
778  for(core::Size y=0; y<ydim_; ++y){
779  for(core::Size z=0; z<zdim_; ++z){
780  if (grid_[x][y][z]==TP_POCKET){
781  pockets_[x][y][z]=TP_POCKET;
782  core::Size x1,y1,z1;
783  if (x==0) x1=0;
784  else x1=x-1;
785  if (y==0) y1=0;
786  else y1=y-1;
787  if (z==0) z1=0;
788  else z1=z-1;
789  for(core::Size x2=x1; x2<std::min(xdim_, x+2); ++x2){
790  for(core::Size y2=y1; y2<std::min(ydim_, y+2); ++y2){
791  for(core::Size z2=z1; z2<std::min(zdim_, z+2); ++z2){
792  fill(x2,y2,z2);
793  }}}
794  }
795  }}}
796 }
797 
798 
799 void PocketGrid::newSearch(core::Size thr1, core::Size thr2, core::Size max1, core::Size max2, bool psp, bool sps){
800  //vector of deltas to add to x,y,z. Index % 3 = 0 for x deltas, 1 for y, 2 for z
801  std::vector<int> deltas;
802  int dirs=7;
803  deltas.push_back(0);deltas.push_back(0);deltas.push_back(1);
804  deltas.push_back(0);deltas.push_back(1);deltas.push_back(0);
805  deltas.push_back(1);deltas.push_back(0);deltas.push_back(0);
806  deltas.push_back(1);deltas.push_back(1);deltas.push_back(1);
807  deltas.push_back(-1);deltas.push_back(1);deltas.push_back(1);
808  deltas.push_back(-1);deltas.push_back(-1);deltas.push_back(1);
809  deltas.push_back(1);deltas.push_back(-1);deltas.push_back(1);
810 
811  core::Size thr;
812  core::Size max;
813  //iterate over the entire grid
814  for (core::Size cx=0; cx<xdim_;cx++){
815  for (core::Size cy=0; cy<ydim_;cy++){
816  for (core::Size cz=0; cz<zdim_;cz++){
817  bool tar_surf=false;
818 
819  //if a point is a surface, or pocket and doind psp searching, or solvent and doing a sps search, search in all directions from that point.
820  if ((!sps&&(grid_[cx][cy][cz]==HSURFACE || grid_[cx][cy][cz]==PSURFACE || grid_[cx][cy][cz]==T_SURFACE||(grid_[cx][cy][cz]==POCKET&&psp)||(grid_[cx][cy][cz]==TP_POCKET&&psp)||(grid_[cx][cy][cz]==POCKET&&sps)))||(grid_[cx][cy][cz]==EMPTY&&sps)){
821  if (grid_[cx][cy][cz]==T_SURFACE||(grid_[cx][cy][cz]==TP_POCKET&&psp)) tar_surf=true;
822  //iterate over the searching directions, get the index associated with x
823  for (int i=0;i<dirs*3;i+=3){
824  if (i<=6) {
825  thr=thr1;
826  max=max1;
827  }
828  else {
829  thr=thr2;
830  max=max2;
831  }
832  bool t_surf=tar_surf;
833  int x;
834  int y;
835  int z;
836  int count;
837  bool marked=true;
838  //starting at that point, search outward in the direction being searched until max distance
839  for (x=(int)cx+deltas[i],y=(int)cy+deltas[i+1],z=(int)cz+deltas[i+2], count=0; count<(int)max; x+=deltas[i],y+=deltas[i+1],z+=deltas[i+2], count++){
840  if (x<0 || x>=(int)xdim_) break;
841  if (y<0 || y>=(int)ydim_) break;
842  if (z<0 || z>=(int)zdim_) break;
843 
844  //Just keep going for EMPTY (solvent)
845  if (grid_[x][y][z]==EMPTY&&!sps) {
846  marked=false;
847  continue;
848  }
849 
850  //if not doing psp, treat pockets as EMPTY coz they are still solvent
851  else if((grid_[x][y][z]==POCKET&&!psp) || (grid_[x][y][z]==TP_POCKET&&!psp)) {
852  if (sps) {
853  marked=false;
854  }
855  continue;
856  }
857 
858  //if it's a surface, we want to go back to the starting point and fill the solvents in as pockets
859  else if (sps&&(grid_[x][y][z]==HSURFACE || grid_[x][y][z]==PSURFACE || grid_[x][y][z]==T_SURFACE)) break;
860  else if ((!sps&&(grid_[x][y][z]==HSURFACE || grid_[x][y][z]==PSURFACE || grid_[x][y][z]==T_SURFACE || (grid_[x][y][z]==POCKET&&psp) || (grid_[x][y][z]==TP_POCKET&&psp))) || (grid_[x][y][z]==EMPTY&&sps)){
861 
862  //if there is no EMPTY, then everything has been marked already. No need to go further.
863  if (marked) break;
864 
865 
866  if (grid_[x][y][z]==T_SURFACE||(grid_[x][y][z]==TP_POCKET&&psp)) t_surf=true;
867  if ((count>(int)thr && !psp)||psp||sps){
868  for (int c=count;c>0;c--){
869  if (grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]==HSURFACE || grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]] == PSURFACE || grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]==T_SURFACE) std::cout<<"MAJOR ERROR, overwriting surface with pocket\n";
870  if (sps){
871  grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]=EMPTY;
872  }
873  else if (markpsp_||!psp){
874  if (t_surf) grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]=TP_POCKET;
875  else if (grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]!=TP_POCKET) grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]=POCKET;
876  }else{
877  if (grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]==EMPTY) grid_[x-c*deltas[i]][y-c*deltas[i+1]][z-c*deltas[i+2]]=PSP;
878  }
879  }
880  }
881  break;
882  }
883  //if it's not a surface, pocket, or solvent then can't be a pocket. Stop searching this direction
884  else break;
885  }
886 
887  }
888 
889  }
890  }
891  }}
892 
893 }
894 
895 
896 void PocketGrid::mark(core::Vector const & center, core::Real const & vdWd, core::Real const & buffer, bool polar, bool targetResi){
897  mark(center(1),center(2),center(3), vdWd, buffer, polar, targetResi);
898 }
899 
900 void PocketGrid::mark(core::Real x, core::Real y, core::Real z, core::Real const & vdWd, core::Real const & buffer, bool polar, bool targetResi){
901  x-=xcorn_;
902  y-=ycorn_;
903  z-=zcorn_;
904  int xcen=(int)floor(x/stepSize_+.5);
905  int ycen=(int)floor(y/stepSize_+.5);
906  int zcen=(int)floor(z/stepSize_+.5);
907  int minX,maxX;
908  int minY,maxY;
909  int minZ,maxZ;
910  core::Real radius=(vdWd+buffer)/stepSize_;
911  core::Real vdW=vdWd/stepSize_;
912 
913  minX=int(std::max(ceil(((core::Real)(xcen)-radius)-.5), 0.));
914  maxX=int(std::min(floor(((core::Real)(xcen)+radius)+.5), (core::Real)(xdim_)-1.));
915  minY=int(std::max(ceil(((core::Real)(ycen)-radius)-.5), 0.));
916  maxY=int(std::min(floor(((core::Real)(ycen)+radius)+.5), (core::Real)(ydim_)-1.));
917  minZ=int(std::max(ceil(((core::Real)(zcen)-radius)-.5), 0.));
918  maxZ=int(std::min(floor(((core::Real)(zcen)+radius)+.5), (core::Real)(zdim_)-1.));
919 
920  for (int xIter=minX; xIter<=maxX; ++xIter){
921  for (int yIter=minY; yIter<=maxY; ++yIter){
922  int centerZ=std::max(minZ, zcen);
923  for (int zIter=centerZ;zIter <= maxZ; ++zIter){
924  if (pow((xIter-xcen),2)+pow((yIter-ycen),2)+pow((zIter-zcen),2)>pow(radius, 2)) continue;
925  if (pow((xIter-xcen),2)+pow((yIter-ycen),2)+pow((zIter-zcen),2)>pow(vdW, 2)){
926  if (grid_[xIter][yIter][zIter]!=PROTEIN &&grid_[xIter][yIter][zIter]!=TARGET) {
927  if (targetResi){
928  grid_[xIter][yIter][zIter]=T_SURFACE;
929  }else if (polar) { if (grid_[xIter][yIter][zIter]!=HSURFACE) grid_[xIter][yIter][zIter]=PSURFACE;
930  }else{grid_[xIter][yIter][zIter]=HSURFACE;
931  }
932  }else if (targetResi) grid_[xIter][yIter][zIter]=TARGET;
933  }else{
934  if (targetResi) {
935  grid_[xIter][yIter][zIter]=TARGET;
936  }else{
937  if (grid_[xIter][yIter][zIter]!=TARGET) grid_[xIter][yIter][zIter]=PROTEIN;
938  }
939  }
940  }
941 
942  centerZ=std::min(maxZ, zcen-1);
943  for (int zIter=centerZ;zIter >= minZ; --zIter){
944  if (pow(xIter-xcen,2)+pow(yIter-ycen,2)+pow(zIter-zcen,2)>pow(radius, 2)) continue;
945  if (pow(xIter-xcen,2)+pow(yIter-ycen,2)+pow(zIter-zcen,2)>pow(vdW, 2)){
946  if (grid_[xIter][yIter][zIter]!=PROTEIN &&grid_[xIter][yIter][zIter]!=TARGET) {
947  if (targetResi){
948  grid_[xIter][yIter][zIter]=T_SURFACE;
949  }else if (polar) { if (grid_[xIter][yIter][zIter]!=HSURFACE) grid_[xIter][yIter][zIter]=PSURFACE;
950  }else{grid_[xIter][yIter][zIter]=HSURFACE;
951  }
952  }else if (targetResi) grid_[xIter][yIter][zIter]=TARGET;
953 
954  }else{
955  if (targetResi) {
956  grid_[xIter][yIter][zIter]=TARGET;
957  }else{
958  grid_[xIter][yIter][zIter]=PROTEIN;
959  }
960  }
961  }
962  }
963  }
964 
965 }
966 
968  core::Size tmp=minsize;
969  tmp++;
970 }
971 
973  for (core::Size z=0; z<zdim_;z++){
974  for (core::Size x=0;x<xdim_;x++){
975  for (core::Size y=0;y<ydim_;y++){
976  std::cout << pockets_[x][y][z];
977  }
978  std::cout<<std::endl;
979  }
980  std::cout<<std::endl;
981  }
982 }
983 
985  std::stringstream filename;
986  filename<<tag_<<"pocket"<<pdbno_<<".pdb";
987  pdbno_++;
988  dumpGridToFile( filename.str() );
989 }
990 
991 
992 void PocketGrid::dumpGridToFile( std::string const & output_filename ){
993 
994  utility::io::ozstream outPDB_stream;
995  outPDB_stream.open(output_filename, std::ios::out);
996  int counter=1;
997  int counter2=1;
998 
999  core::Size x,y,z;
1000  for (x=0;x<(xdim_); x++){
1001  for (y=0;y<(ydim_); y++){
1002  for (z=0;z<(zdim_); z++){
1003  std::string concatenated_pdb_info;
1004  concatenated_pdb_info += "ATOM ";
1005  std::stringstream tmp;
1006  tmp<<counter2;
1007  if (counter2<10) concatenated_pdb_info += " ";
1008  else if (counter2<100) concatenated_pdb_info += " ";
1009  else if (counter2<1000) concatenated_pdb_info += " ";
1010  else if (counter2<10000) concatenated_pdb_info += " ";
1011  concatenated_pdb_info += tmp.str()+" ";
1012  if (grid_[x][y][z]==EMPTY) {
1013  continue;
1014  }
1015  if (grid_[x][y][z]==POCKET) {
1016  continue;
1017  }
1018  if (grid_[x][y][z]==PO_SURF){
1019  continue;
1020  }
1021  if (grid_[x][y][z]==PO_BURIED) {
1022  continue;
1023  }
1024  if (grid_[x][y][z]==HSURFACE) {
1025  continue;
1026  }
1027  if (grid_[x][y][z]==PSURFACE) {
1028  continue;
1029  }
1030  if (grid_[x][y][z]==TP_POCKET) {
1031  continue;
1032  }
1033  if (grid_[x][y][z]==TP_SURF) {
1034  continue;
1035  }
1036  if (grid_[x][y][z]==TP_BURIED) {
1037  continue;
1038  }
1039  if (grid_[x][y][z]==PO_EDGE) {
1040  continue;
1041  }
1042  if (grid_[x][y][z]==TP_EDGE) {
1043  continue;
1044  }
1045  if (grid_[x][y][z]==T_SURFACE) {
1046  concatenated_pdb_info += "TS TS ";
1047  }
1048  if (grid_[x][y][z]==TARGET) {
1049  continue;
1050  }
1051  if (grid_[x][y][z]==PROTEIN) {
1052  continue;
1053  }
1054  if (grid_[x][y][z]==PSP) {
1055  continue;
1056  }
1057 
1058  tmp.str(std::string());
1059  tmp<<" "<<std::setw(8)<<std::fixed<<std::setprecision(3)<<x*stepSize_+xcorn_<<std::setw(8)<<y*stepSize_+ycorn_<<std::setw(8)<<z*stepSize_+zcorn_<<std::endl;
1060  concatenated_pdb_info += tmp.str();
1061  counter++;
1062  outPDB_stream<<concatenated_pdb_info;
1063  }}}
1064 
1065  int clustNo=1;
1066  bool smallPocket;
1067  for (std::list<PCluster>::iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1068  if (cit->points_.size()*pow(stepSize_,3)<minPockSize_) smallPocket=true;
1069  else smallPocket=false;
1070  for (std::list<PCluster::Cxyz>::iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1071  std::string concatenated_pdb_info;
1072  concatenated_pdb_info += "ATOM ";
1073  std::stringstream tmp;
1074  tmp<<counter2;
1075  if (counter2<10) concatenated_pdb_info += " ";
1076  else if (counter2<100) concatenated_pdb_info += " ";
1077  else if (counter2<1000) concatenated_pdb_info += " ";
1078  else if (counter2<10000) concatenated_pdb_info += " ";
1079  else concatenated_pdb_info += "";
1080  concatenated_pdb_info += tmp.str()+" ";
1081  if (smallPocket){
1082  if (grid_[pit->x][pit->y][pit->z]==TP_POCKET) concatenated_pdb_info += "STP STP ";
1083  if (grid_[pit->x][pit->y][pit->z]==TP_SURF) concatenated_pdb_info += "STS STS ";
1084  if (grid_[pit->x][pit->y][pit->z]==TP_BURIED) concatenated_pdb_info += "STB STB ";
1085  if (grid_[pit->x][pit->y][pit->z]==TP_EDGE) concatenated_pdb_info += "STE STE ";
1086  }else{
1087  if (grid_[pit->x][pit->y][pit->z]==TP_POCKET) concatenated_pdb_info += "TP TP ";
1088  if (grid_[pit->x][pit->y][pit->z]==TP_SURF) concatenated_pdb_info += "TPS TPS ";
1089  if (grid_[pit->x][pit->y][pit->z]==TP_BURIED) concatenated_pdb_info += "TPB TPB ";
1090  if (grid_[pit->x][pit->y][pit->z]==TP_EDGE) concatenated_pdb_info += "TPE TPE ";
1091  }
1092  if (grid_[pit->x][pit->y][pit->z]==POCKET) concatenated_pdb_info += "PC PC ";
1093  if (grid_[pit->x][pit->y][pit->z]==PO_SURF) concatenated_pdb_info += "PCS PCS ";
1094  if (grid_[pit->x][pit->y][pit->z]==PO_BURIED) concatenated_pdb_info += "PCB PCB ";
1095  if (grid_[pit->x][pit->y][pit->z]==PO_EDGE) concatenated_pdb_info += "PCE PCE ";
1096 
1097  tmp.str(std::string());
1098  tmp<<clustNo;
1099  if (clustNo<10) concatenated_pdb_info += " ";
1100  else if (clustNo<100) concatenated_pdb_info += " ";
1101  else if (clustNo<1000) concatenated_pdb_info += " ";
1102  concatenated_pdb_info += tmp.str()+" ";
1103  tmp.str(std::string());
1104  tmp<<" "<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pit->x*stepSize_+xcorn_<<std::setw(8)<<pit->y*stepSize_+ycorn_<<std::setw(8)<<pit->z*stepSize_+zcorn_<<std::endl;
1105  concatenated_pdb_info += tmp.str();
1106  counter2++;
1107  outPDB_stream<<concatenated_pdb_info;
1108 
1109  }
1110  clustNo++;
1111  }
1112  outPDB_stream.close();
1113  outPDB_stream.clear();
1114 
1115 }
1116 
1118 
1119  //vector of deltas to add to x,y,z. Index % 3 = 0 for x deltas, 1 for y, 2 for z
1120  std::vector<int> deltas;
1121  int dirs=13;
1122  deltas.push_back(0);deltas.push_back(-1);deltas.push_back(-1);
1123  deltas.push_back(0);deltas.push_back(-1);deltas.push_back(0);
1124  deltas.push_back(0);deltas.push_back(-1);deltas.push_back(1);
1125  deltas.push_back(0);deltas.push_back(0);deltas.push_back(-1);
1126  //deltas.push_back(0);deltas.push_back(1);deltas.push_back(-1);
1127  deltas.push_back(-1);deltas.push_back(-1);deltas.push_back(-1);
1128  deltas.push_back(-1);deltas.push_back(-1);deltas.push_back(0);
1129  deltas.push_back(-1);deltas.push_back(-1);deltas.push_back(1);
1130  deltas.push_back(-1);deltas.push_back(0);deltas.push_back(-1);
1131  deltas.push_back(-1);deltas.push_back(0);deltas.push_back(0);
1132  deltas.push_back(-1);deltas.push_back(0);deltas.push_back(1);
1133  deltas.push_back(-1);deltas.push_back(1);deltas.push_back(-1);
1134  deltas.push_back(-1);deltas.push_back(1);deltas.push_back(0);
1135  deltas.push_back(-1);deltas.push_back(1);deltas.push_back(1);
1136 
1137  // a grid of pointers back to clusters. Non-clustered points point to end()
1138  std::vector < std::vector < std::vector <std::list<PCluster>::iterator> > > cluster_grid;
1139  cluster_grid.resize(xdim_);
1140  for (core::Size tx=0;tx<xdim_;tx++){
1141  cluster_grid[tx].resize(ydim_);
1142  for (core::Size ty=0;ty<ydim_;ty++){
1143  cluster_grid[tx][ty].resize(zdim_, clusters_.clusters_.end());
1144  }
1145  }
1146 
1147  // Search the grid for pockets
1148  core::Size x,y,z;
1149  for (x=0;x<(xdim_); x++){
1150  for (y=0;y<(ydim_); y++){
1151  for (z=0;z<(zdim_); z++){
1152  // If you find a pocket, check if its neighbors are already part of a cluster
1153  if (grid_[x][y][z]==TP_POCKET || grid_[x][y][z]==POCKET || grid_[x][y][z]==PO_EDGE || grid_[x][y][z]==TP_EDGE || grid_[x][y][z]==PO_BURIED || grid_[x][y][z]==TP_BURIED) {
1154  int found=0;
1155  for (int i=0;i<dirs*3;i+=3){
1156  int cx = (int)x+deltas[i];
1157  int cy = (int)y+deltas[i+1];
1158  int cz = (int)z+deltas[i+2];
1159  if (cx<0 || cy<0 || cz<0 || cx>=(int)xdim_ || cy>=(int)ydim_ || cz>=(int)zdim_) continue;
1160 
1161  // If a neighbor is found...
1162  if (cluster_grid[cx][cy][cz]!=clusters_.clusters_.end()){
1163  // If this is the first neighbor found, add it to that cluster
1164  if (!found){
1165  found=1;
1166  cluster_grid[x][y][z]=cluster_grid[cx][cy][cz];
1167  cluster_grid[x][y][z]->add(x,y,z);
1168  }else{
1169  // If this cluster differs from the one that you just added, merge them
1170  if (cluster_grid[cx][cy][cz]!=cluster_grid[x][y][z]){
1171  std::list<PCluster>::iterator old = cluster_grid[cx][cy][cz];
1172  for (std::list<PCluster::Cxyz>::iterator pit=old->points_.begin(); pit != old->points_.end(); ++pit){
1173  cluster_grid[pit->x][pit->y][pit->z]=cluster_grid[x][y][z];
1174  }
1175  clusters_.join(cluster_grid[x][y][z], old);
1176  }
1177  }
1178  }
1179  }
1180  // If none of the neighbors are in a cluster, create a new cluster
1181  if (!found){
1182  cluster_grid[x][y][z]=clusters_.add(x,y,z, stepSize_);
1183  }
1184  }
1185  }
1186  }
1187  }
1188 }
1189 
1190 
1191 
1192 void PocketGrid::markPocketDepth(core::Real const & surf_d, core::Real const & bur_d){
1193  core::Size x,y,z;
1194  for (x=0;x<(xdim_); x++){
1195  for (y=0;y<(ydim_); y++){
1196  for (z=0;z<(zdim_); z++){
1197  if (grid_[x][y][z]==TP_POCKET || grid_[x][y][z]==POCKET) markDepth(x,y,z, surf_d, bur_d);
1198  }}}
1199 }
1200 
1201 void PocketGrid::markEdgeDepth(core::Real const & surf_d, core::Real const & bur_d ){
1202  for (std::list<PCluster>::iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1203  for (std::list<PCluster::Cxyz>::iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1204  if (grid_[pit->x][pit->y][pit->z]==TP_EDGE || grid_[pit->x][pit->y][pit->z]==PO_EDGE) {
1205  bool surf=markOneEdgeDepth(pit->x,pit->y,pit->z, surf_d, bur_d, cit->isTarget());
1206  if (surf){
1207  std::list<PCluster::Cxyz>::iterator tmp = pit;
1208  pit--;
1209  cit->points_.erase(tmp);
1210  }
1211  }
1212  }
1213  }
1214  }
1215 
1216  void PocketGrid::markDepth(core::Size x, core::Size y, core::Size z, core::Real const & surf_d, core::Real const & bur_d){
1217  core::Size ssteps=(core::Size)ceil(surf_d/stepSize_);
1218  core::Size st;
1219  core::Size en;
1220  if (ssteps>x) {
1221  st=0;
1222  if (grid_[x][y][z]==POCKET){
1223  grid_[x][y][z]=PO_EDGE;
1224  }else grid_[x][y][z]=TP_EDGE;
1225  //clusters_.add(x,y,z, stepSize_);
1226  return;
1227  }
1228  else st=x-ssteps;
1229  if (x+ssteps>=xdim_) {
1230  en=xdim_;
1231  if (grid_[x][y][z]==POCKET){
1232  grid_[x][y][z]=PO_EDGE;
1233  }else grid_[x][y][z]=TP_EDGE;
1234  //clusters_.add(x,y,z, stepSize_);
1235  return ;
1236  }
1237  else en=x+ssteps+1;
1238  for (core::Size i=st;i<en;i++){
1239  if (grid_[i][y][z]==EMPTY) {
1240  if (grid_[x][y][z]==POCKET){
1241  grid_[x][y][z]=PO_SURF;
1242  }else grid_[x][y][z]=TP_SURF;
1243  return ;
1244  }
1245  }
1246  if (ssteps>y) {
1247  st=0;
1248  if (grid_[x][y][z]==POCKET){
1249  grid_[x][y][z]=PO_EDGE;
1250  }else grid_[x][y][z]=TP_EDGE;
1251  //clusters_.add(x,y,z, stepSize_);
1252  return ;
1253  }
1254  else st=y-ssteps;
1255  if (y+ssteps>=ydim_) {
1256  en=ydim_;
1257  if (grid_[x][y][z]==POCKET){
1258  grid_[x][y][z]=PO_EDGE;
1259  }else grid_[x][y][z]=TP_EDGE;
1260  //clusters_.add(x,y,z, stepSize_);
1261  return ;
1262  }
1263  else en=y+ssteps+1;
1264  for (core::Size i=st;i<en;i++){
1265  if (grid_[x][i][z]==EMPTY) {
1266  if (grid_[x][y][z]==POCKET){
1267  grid_[x][y][z]=PO_SURF;
1268  }else grid_[x][y][z]=TP_SURF;
1269  return ;
1270  }
1271  }
1272  if (ssteps>z) {
1273  st=0;
1274  if (grid_[x][y][z]==POCKET){
1275  grid_[x][y][z]=PO_EDGE;
1276  }else grid_[x][y][z]=TP_EDGE;
1277  //clusters_.add(x,y,z, stepSize_);
1278  return ;
1279  }
1280  else st=z-ssteps;
1281  if (z+ssteps>=zdim_) {
1282  en=zdim_;
1283  if (grid_[x][y][z]==POCKET){
1284  grid_[x][y][z]=PO_EDGE;
1285  }else grid_[x][y][z]=TP_EDGE;
1286  //clusters_.add(x,y,z, stepSize_);
1287  return ;
1288  }
1289  else en=z+ssteps+1;
1290  for (core::Size i=st;i<en;i++){
1291  if (grid_[x][y][i]==EMPTY) {
1292  if (grid_[x][y][z]==POCKET){
1293  grid_[x][y][z]=PO_SURF;
1294  }else grid_[x][y][z]=TP_SURF;
1295  return ;
1296  }
1297  }
1298 
1299  ssteps=(core::Size)ceil(bur_d/stepSize_);
1300  if (ssteps>x) st=0;
1301  else st=x-ssteps;
1302  if (x+ssteps>=xdim_) en=xdim_;
1303  else en=x+ssteps+1;
1304  for (core::Size i=st;i<en;i++){
1305  if (grid_[i][y][z]==HSURFACE || grid_[i][y][z]==PSURFACE || grid_[i][y][z]==PSURFACE || grid_[i][y][z]==T_SURFACE) {
1306  if (grid_[x][y][z]==POCKET){
1307  grid_[x][y][z]=PO_BURIED;
1308  }else grid_[x][y][z]=TP_BURIED;
1309  //clusters_.add(x,y,z, stepSize_);
1310  return ;
1311  }
1312  }
1313  if (ssteps>y) st=0;
1314  else st=y-ssteps;
1315  if (y+ssteps>=ydim_) en=ydim_;
1316  else en=y+ssteps+1;
1317  for (core::Size i=st;i<en;i++){
1318  if (grid_[x][i][z]==HSURFACE || grid_[x][i][z]==PSURFACE || grid_[x][i][z]==T_SURFACE) {
1319  if (grid_[x][y][z]==POCKET){
1320  grid_[x][y][z]=PO_BURIED;
1321  }else grid_[x][y][z]=TP_BURIED;
1322  //clusters_.add(x,y,z, stepSize_);
1323  return ;
1324  }
1325  }
1326  if (ssteps>z) st=0;
1327  else st=z-ssteps;
1328  if (z+ssteps>=zdim_) en=zdim_;
1329  else en=z+ssteps+1;
1330  for (core::Size i=st;i<en;i++){
1331  if (grid_[x][y][i]==HSURFACE || grid_[x][y][i]==PSURFACE || grid_[x][y][i]==T_SURFACE) {
1332  if (grid_[x][y][z]==POCKET){
1333  grid_[x][y][z]=PO_BURIED;
1334  }else grid_[x][y][z]=TP_BURIED;
1335  //clusters_.add(x,y,z, stepSize_);
1336  return ;
1337  }
1338  }
1339  //clusters_.add(x,y,z, stepSize_);
1340  }
1341 
1342  bool PocketGrid::markOneEdgeDepth(core::Size x, core::Size y, core::Size z, core::Real const & surf_d, core::Real const & bur_d, bool isTarget){
1343  core::Size xst;
1344  core::Size xen;
1345  core::Size yst;
1346  core::Size yen;
1347  core::Size zst;
1348  core::Size zen;
1349  core::Size ssteps=(core::Size)ceil(surf_d/stepSize_);
1350  if (ssteps>x)
1351  xst=0;
1352  else xst=x-ssteps;
1353  if (x+ssteps>=xdim_)
1354  xen=xdim_;
1355  else xen=x+ssteps+1;
1356  if (ssteps>y)
1357  yst=0;
1358  else yst=y-ssteps;
1359  if (y+ssteps>=ydim_)
1360  yen=ydim_;
1361  else yen=y+ssteps+1;
1362  if (ssteps>z)
1363  zst=0;
1364  else zst=z-ssteps;
1365  if (z+ssteps>=zdim_)
1366  zen=zdim_;
1367  else zen=z+ssteps+1;
1368  for (core::Size i=xst;i<xen;i++){
1369  if (grid_[i][y][z]==EMPTY) {
1370  if (grid_[x][y][z]==PO_EDGE||!isTarget){
1371  grid_[x][y][z]=PO_SURF;
1372  }else grid_[x][y][z]=TP_SURF;
1373  return true;
1374  }
1375  }
1376  for (core::Size i=yst;i<yen;i++){
1377  if (grid_[x][i][z]==EMPTY) {
1378  if (grid_[x][y][z]==PO_EDGE||!isTarget){
1379  grid_[x][y][z]=PO_SURF;
1380  }else grid_[x][y][z]=TP_SURF;
1381  return true;
1382  }
1383  }
1384  for (core::Size i=zst;i<zen;i++){
1385  if (grid_[x][y][i]==EMPTY) {
1386  if (grid_[x][y][z]==PO_EDGE||!isTarget){
1387  grid_[x][y][z]=PO_SURF;
1388  }else grid_[x][y][z]=TP_SURF;
1389  return true;
1390  }
1391  }
1392 
1393  ssteps=(core::Size)ceil(bur_d/stepSize_);
1394  if (ssteps>x)
1395  xst=0;
1396  else xst=x-ssteps;
1397  if (x+ssteps>=xdim_)
1398  xen=xdim_;
1399  else xen=x+ssteps+1;
1400  if (ssteps>y)
1401  yst=0;
1402  else yst=y-ssteps;
1403  if (y+ssteps>=ydim_)
1404  yen=ydim_;
1405  else yen=y+ssteps+1;
1406  if (ssteps>z)
1407  zst=0;
1408  else zst=z-ssteps;
1409  if (z+ssteps>=zdim_)
1410  zen=zdim_;
1411  else zen=z+ssteps+1;
1412  for (core::Size i=xst;i<xen;i++){
1413  if (grid_[i][y][z]==HSURFACE || grid_[i][y][z]==PSURFACE ) {
1414  if (grid_[x][y][z]==POCKET||!isTarget){
1415  grid_[x][y][z]=PO_BURIED;
1416  }else grid_[x][y][z]=TP_BURIED;
1417  return false;
1418  }
1419  }
1420  for (core::Size i=yst;i<yen;i++){
1421  if (grid_[x][i][z]==HSURFACE || grid_[x][i][z]==PSURFACE ) {
1422  if (grid_[x][y][z]==POCKET||!isTarget){
1423  grid_[x][y][z]=PO_BURIED;
1424  }else grid_[x][y][z]=TP_BURIED;
1425  return false;
1426  }
1427  }
1428 
1429  for (core::Size i=zst;i<zen;i++){
1430  if (grid_[x][y][i]==HSURFACE || grid_[x][y][i]==PSURFACE ) {
1431  if (grid_[x][y][z]==POCKET||!isTarget){
1432  grid_[x][y][z]=PO_BURIED;
1433  }else grid_[x][y][z]=TP_BURIED;
1434  return false;
1435  }
1436  }
1437 
1438  if (grid_[x][y][z]==PO_EDGE||!isTarget){
1439  grid_[x][y][z]=POCKET;
1440  }else grid_[x][y][z]=TP_POCKET;
1441  return false;
1442 
1443  }
1444 
1445  core::Real PocketGrid::targetPocketVolume(core::Real const & surf_sc, core::Real const & bur_sc) const {
1446  core::Real vol=0;
1447  core::Size x,y,z;
1448  for (x=0;x<(xdim_); x++){
1449  for (y=0;y<(ydim_); y++){
1450  for (z=0;z<(zdim_); z++){
1451  if (grid_[x][y][z]==TP_POCKET) vol+=1;
1452  if (grid_[x][y][z]==TP_SURF) vol+=surf_sc;
1453  if (grid_[x][y][z]==TP_BURIED) vol+=bur_sc;
1454  }}}
1455  return vol*pow(stepSize_,3);
1456  }
1457 
1459  core::Real vol=0;
1460  core::Size x,y,z;
1461  for (x=0;x<(xdim_); x++){
1462  for (y=0;y<(ydim_); y++){
1463  for (z=0;z<(zdim_); z++){
1464  if (grid_[x][y][z]==TP_POCKET) {
1465  if (touchesSolvent(x,y,z)) vol+=1;
1466  }
1467  }}}
1468  vol*=pow(stepSize_,2);
1469  return vol;
1470 
1471 
1472  }
1473 
1475  core::Real vol=0;
1476  core::Size x,y,z;
1477  for (x=0;x<(xdim_); x++){
1478  for (y=0;y<(ydim_); y++){
1479  for (z=0;z<(zdim_); z++){
1480  if (grid_[x][y][z]==TP_POCKET) {
1481  if (touchesSurface(x,y,z, true, true)) vol+=1;
1482  }
1483  }}}
1484  vol*=pow(stepSize_,2);
1485  return vol;
1486 
1487 
1488  }
1489 
1491  core::Real vol=0;
1492  core::Size x,y,z;
1493  for (x=0;x<(xdim_); x++){
1494  for (y=0;y<(ydim_); y++){
1495  for (z=0;z<(zdim_); z++){
1496  if (grid_[x][y][z]==TP_POCKET) {
1497  if (touchesSurface(x,y,z, false)) vol+=1;
1498  }
1499  }}}
1500  vol*=pow(stepSize_,2);
1501  return vol;
1502 
1503 
1504  }
1505 
1507  core::Real vol=0;
1508  core::Size x,y,z;
1509  for (x=0;x<(xdim_); x++){
1510  for (y=0;y<(ydim_); y++){
1511  for (z=0;z<(zdim_); z++){
1512  if (grid_[x][y][z]==TP_POCKET) {
1513  if (touchesSurface(x,y,z, true)) vol+=1;
1514  }
1515  }}}
1516  vol*=pow(stepSize_,2);
1517  return vol;
1518 
1519 
1520  }
1521 
1523  core::Real vol=0;
1524  core::Size x,y,z;
1525  for (x=0;x<(xdim_); x++){
1526  for (y=0;y<(ydim_); y++){
1527  for (z=0;z<(zdim_); z++){
1528  if (grid_[x][y][z]==TP_POCKET) {
1529  if (!touchesSS(x,y,z)){
1530  core::Size protTouch=touchesPS(x,y,z);
1531  if (protTouch>0){
1532  vol+=3*protTouch;
1533  }else{
1534  vol+=1;
1535  }
1536  }
1537  }
1538  }}}
1539  vol*=pow(stepSize_,2);
1540  return vol;
1541 
1542 
1543  }
1544 
1546 // clusters_.findClusters();
1547  clusterPockets();
1548  //mark target clusters
1549  for (std::list<PCluster>::iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1550  for (std::list<PCluster::Cxyz>::iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1551  for (int x = -1; x <2; x++){
1552  for (int y = -1; y <2; y++){
1553  for (int z = -1; z <2; z++){
1554  if (x==-1) if (pit->x == 0) continue;
1555  if (y==-1) if (pit->y == 0) continue;
1556  if (z==-1) if (pit->z == 0) continue;
1557  if (x==1) if (pit->x == xdim_-1) continue;
1558  if (y==1) if (pit->y == ydim_-1) continue;
1559  if (z==1) if (pit->z == zdim_-1) continue;
1560  if (grid_[pit->x+x][pit->y+y][pit->z+z]==T_SURFACE) {
1561  cit->target=true;
1562  x=2;y=2;z=2;
1563  pit=cit->points_.end();
1564  --pit;
1565  }
1566  }}}
1567  }
1568  }
1569 
1570  //Change target clusters to target types on grid
1571  for (std::list<PCluster>::iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1572  if (!cit->isTarget() ) {
1573  for (std::list<PCluster::Cxyz>::iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1574  if (grid_[pit->x][pit->y][pit->z]==TP_POCKET) grid_[pit->x][pit->y][pit->z]=POCKET;
1575  if (grid_[pit->x][pit->y][pit->z]==TP_SURF) grid_[pit->x][pit->y][pit->z]=PO_SURF;
1576  if (grid_[pit->x][pit->y][pit->z]==TP_BURIED) grid_[pit->x][pit->y][pit->z]=PO_BURIED;
1577  if (grid_[pit->x][pit->y][pit->z]==TP_EDGE) grid_[pit->x][pit->y][pit->z]=PO_EDGE;
1578  }
1579  continue;
1580  }
1581 
1582  for (std::list<PCluster::Cxyz>::iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1583  if (grid_[pit->x][pit->y][pit->z]==POCKET) grid_[pit->x][pit->y][pit->z]=TP_POCKET;
1584  if (grid_[pit->x][pit->y][pit->z]==PO_SURF) grid_[pit->x][pit->y][pit->z]=TP_SURF;
1585  if (grid_[pit->x][pit->y][pit->z]==PO_BURIED) grid_[pit->x][pit->y][pit->z]=TP_BURIED;
1586  if (grid_[pit->x][pit->y][pit->z]==PO_EDGE) grid_[pit->x][pit->y][pit->z]=TP_EDGE;
1587  }
1588  }
1589  }
1590 
1593  }
1594 
1596  if (maxPockSize_){
1599  }
1600 
1601 
1602  bool PocketGrid::isTooSmall() const {
1603  core::Size x,y,z;
1604  if (xdim_==0||ydim_==0||zdim_==0) return false;
1605  if ( grid_[0][0][0] == TP_POCKET) return true;
1606  if ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_POCKET) return true;
1607  if ( grid_[0][0][0] == TP_SURF) return true;
1608  if ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_SURF) return true;
1609  if ( grid_[0][0][0] == TP_BURIED) return true;
1610  if ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_BURIED) return true;
1611  if ( grid_[0][0][0] == TP_EDGE) return true;
1612  if ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_EDGE) return true;
1613 
1614  for (x=0, y=0, z=0; x<xdim_ || y<ydim_ || z<zdim_;x++,y++,z++){
1615  if (x==xdim_) x--;
1616  if (y==ydim_) y--;
1617  if (z==zdim_) z--;
1618  if ( grid_[0][y][z] == TP_POCKET) return true;
1619  if ( grid_[x][0][z] == TP_POCKET) return true;
1620  if ( grid_[x][y][0] == TP_POCKET) return true;
1621  if ( grid_[xdim_-1][y][z] == TP_POCKET) return true;
1622  if ( grid_[x][ydim_-1][z] == TP_POCKET) return true;
1623  if ( grid_[x][y][zdim_-1] == TP_POCKET) return true;
1624  if ( grid_[0][y][z] == TP_SURF) return true;
1625  if ( grid_[x][0][z] == TP_SURF) return true;
1626  if ( grid_[x][y][0] == TP_SURF) return true;
1627  if ( grid_[xdim_-1][y][z] == TP_SURF) return true;
1628  if ( grid_[x][ydim_-1][z] == TP_SURF) return true;
1629  if ( grid_[x][y][zdim_-1] == TP_SURF) return true;
1630  if ( grid_[0][y][z] == TP_BURIED) return true;
1631  if ( grid_[x][0][z] == TP_BURIED) return true;
1632  if ( grid_[x][y][0] == TP_BURIED) return true;
1633  if ( grid_[xdim_-1][y][z] == TP_BURIED) return true;
1634  if ( grid_[x][ydim_-1][z] == TP_BURIED) return true;
1635  if ( grid_[x][y][zdim_-1] == TP_BURIED) return true;
1636  if ( grid_[0][y][z] == TP_EDGE) return true;
1637  if ( grid_[x][0][z] == TP_EDGE) return true;
1638  if ( grid_[x][y][0] == TP_EDGE) return true;
1639  if ( grid_[xdim_-1][y][z] == TP_EDGE) return true;
1640  if ( grid_[x][ydim_-1][z] == TP_EDGE) return true;
1641  if ( grid_[x][y][zdim_-1] == TP_EDGE) return true;
1642  }
1643 
1644 
1645  return false;
1646  }
1647 
1649  core::Vector dims;
1650  dims(1)=0;
1651  dims(2)=0;
1652  dims(3)=0;
1653  core::Size x,y,z;
1654  if (xdim_==0||ydim_==0||zdim_==0) return dims;
1655  if (( grid_[0][0][0] == TP_POCKET) || ( grid_[0][0][0] == TP_SURF) || ( grid_[0][0][0] == TP_BURIED) || ( grid_[0][0][0] == TP_EDGE) || ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_POCKET) || ( grid_[xdim_-1][ydim_-1][zdim_-1]) || ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_BURIED) || ( grid_[xdim_-1][ydim_-1][zdim_-1] == TP_EDGE)) {
1656  dims(1)=1;
1657  dims(2)=1;
1658  dims(3)=1;
1659  return dims;
1660  }
1661 
1662  for (x=0, y=0, z=0; x<xdim_ || y<ydim_ || z<zdim_;x++,y++,z++){
1663  if (x==xdim_) x--;
1664  if (y==ydim_) y--;
1665  if (z==zdim_) z--;
1666  if (( grid_[0][y][z] == TP_POCKET) || ( grid_[0][y][z] == TP_BURIED) || ( grid_[0][y][z] == TP_EDGE) || ( grid_[xdim_-1][y][z] == TP_POCKET) || ( grid_[xdim_-1][y][z] == TP_SURF) || ( grid_[xdim_-1][y][z] == TP_BURIED) || ( grid_[xdim_-1][y][z] == TP_EDGE)){
1667  dims(1)=1;
1668  }
1669  if (( grid_[x][0][z] == TP_POCKET) || ( grid_[x][0][z] == TP_SURF) || ( grid_[x][0][z] == TP_BURIED) || ( grid_[x][0][z] == TP_EDGE) || ( grid_[x][ydim_-1][z] == TP_POCKET) || ( grid_[x][ydim_-1][z] == TP_SURF) || ( grid_[x][ydim_-1][z] == TP_BURIED) || ( grid_[x][ydim_-1][z] == TP_EDGE)) {
1670  dims(2)=1;
1671  }
1672  if (( grid_[x][y][0] == TP_POCKET) || ( grid_[x][y][0] == TP_SURF) || ( grid_[x][y][0] == TP_BURIED) || ( grid_[x][y][0] == TP_EDGE) || ( grid_[x][y][zdim_-1] == TP_POCKET) || ( grid_[x][y][zdim_-1] == TP_SURF) || ( grid_[x][y][zdim_-1] == TP_BURIED) || ( grid_[x][y][zdim_-1] == TP_EDGE)){
1673  dims(3)=1;
1674  }
1675  }
1676 
1677 
1678  return dims;
1679  }
1680 
1682  core::scoring::constraints::ConformationXYZ const xyz_func( inPose.conformation() );
1683  return autoexpanding_pocket_eval(central_rsd, xyz_func, inPose.total_residue());
1684  }
1685 
1686  bool PocketGrid::autoexpanding_pocket_eval( std::vector< core::conformation::ResidueOP > const & central_rsds, core::scoring::constraints::XYZ_Func const & xyz_func, Size const total_residues ) {
1687  return autoexpanding_pocket_eval(*central_rsds[0], xyz_func, total_residues);
1688 }
1689 
1690  bool PocketGrid::autoexpanding_pocket_eval( std::vector< core::conformation::ResidueOP > const & central_rsds, core::pose::Pose const & inPose ) {
1691  core::scoring::constraints::ConformationXYZ const xyz_func( inPose.conformation() );
1692  return autoexpanding_pocket_eval(*central_rsds[0], xyz_func, inPose.total_residue());
1693  }
1694 
1695  bool PocketGrid::autoexpanding_pocket_eval( core::conformation::Residue const & central_rsd, core::scoring::constraints::XYZ_Func const & xyz_func, Size const total_residues ) {
1696 
1697  bool too_small=true;
1698  while (too_small){
1699  recenter( central_rsd );
1700 
1701  for ( Size j = 1, resnum = total_residues; j <= resnum; ++j ) {
1702  core::conformation::Residue const & rsd( xyz_func.residue(j) );
1703  bool target=false;
1704  if ( j == central_rsd.seqpos() ) target=true;
1705  core::Size total_atoms(0);
1706  using namespace basic::options;
1707  if (option[ OptionKeys::fingerprint::include_hydrogens ]()){
1708  total_atoms = rsd.natoms();
1709  } else {
1710  total_atoms = rsd.nheavyatoms();
1711  }
1712  for(Size i = 1, i_end = total_atoms; i <= i_end; ++i) {
1713  bool target_res=target;
1714  if (side_chains_only_){
1715  if ((central_rsd.atom(i).type()>=18)&&(central_rsd.atom(i).type()<=21)){
1716  target_res=false;
1717  }
1718  }
1719  numeric::xyzVector<core::Real> rpoint = rotatePoint(rsd.atom(i).xyz().x(),rsd.atom(i).xyz().y(),rsd.atom(i).xyz().z());
1720  mark(rpoint, rsd.atom_type(i).lj_radius(),probe_rad_, rsd.is_polar(), target_res);
1721  }
1722  }
1723 
1724  findPockets(0, maxLen_);
1725  if (markpsp_){
1726  findPSP(0,maxLen_);
1727  }
1728 
1729  if (marksps_){
1730  findSPS(0,ceil((xdim_+ydim_+zdim_)/stepSize_));
1731  }
1733 
1734  findClusters();
1735 
1736  // Code to check edge of grid goes here
1737  if (isTooSmall()){
1739  if ((size_x_ + dims(1) > limit_x_) || (size_y_ + dims(2) > limit_y_) ||(size_z_ + dims(3) > limit_z_)){
1740  return false;
1741  }
1742  size_x_ = size_x_ + dims(1);
1743  size_y_ = size_y_ + dims(2);
1744  size_z_ = size_z_ + dims(3);
1746  }else{
1747  too_small=false;
1748  }
1749  }
1750 
1752  return true;
1753  }
1754 
1756  core::scoring::constraints::ConformationXYZ const xyz_func( inPose.conformation() );
1757  return DARC_autoexpanding_pocket_eval(central_rsd, xyz_func, inPose.total_residue(), grid_center);
1758  }
1759 
1761  bool too_small=true;
1762  while (too_small){
1763  recenter( grid_center.x(), grid_center.y(), grid_center.z() );
1764  for ( Size j = 1, resnum = total_residues; j <= resnum; ++j ) {
1765  core::conformation::Residue const & rsd( xyz_func.residue(j) );
1766  bool target=false;
1767  if ( j == central_rsd.seqpos() ) target=true;
1768  core::Size total_atoms(0);
1769  using namespace basic::options;
1770  if (option[ OptionKeys::fingerprint::include_hydrogens ]()){
1771  total_atoms = rsd.natoms();
1772  } else {
1773  total_atoms = rsd.nheavyatoms();
1774  }
1775  for(Size i = 1, i_end = total_atoms; i <= i_end; ++i) {
1776  bool target_res=target;
1777  if (side_chains_only_){
1778  if ((central_rsd.atom(i).type()>=18)&&(central_rsd.atom(i).type()<=21)){
1779  target_res=false;
1780  }
1781  }
1782  numeric::xyzVector<core::Real> rpoint = rotatePoint(rsd.atom(i).xyz().x(),rsd.atom(i).xyz().y(),rsd.atom(i).xyz().z());
1783  mark(rpoint, rsd.atom_type(i).lj_radius(),probe_rad_, rsd.is_polar(), target_res);
1784  }
1785  }
1786  findPockets(0, maxLen_);
1787  if (markpsp_){
1788  findPSP(0,maxLen_);
1789  }
1790  if (marksps_){
1791  findSPS(0,ceil((xdim_+ydim_+zdim_)/stepSize_));
1792  }
1794  findClusters();
1795  // Code to check edge of grid goes here
1796  if (isTooSmall()){
1798  if ((size_x_ + dims(1) > limit_x_) || (size_y_ + dims(2) > limit_y_) ||(size_z_ + dims(3) > limit_z_)){
1799  return false;
1800  }
1801  size_x_ = size_x_ + dims(1);
1802  size_y_ = size_y_ + dims(2);
1803  size_z_ = size_z_ + dims(3);
1804  initialize(grid_center.x(), grid_center.y(), grid_center.z(), size_x_, size_y_, size_z_, spacing_, markpsp_, marksps_);
1805  }else{
1806  too_small=false;
1807  }
1808  }
1810  return true;
1811  }
1812 
1814  core::scoring::constraints::ConformationXYZ const xyz_func( inPose.conformation() );
1815  return DARC_pocket_eval(central_rsd, xyz_func, inPose.total_residue(), grid_center);
1816  }
1817 
1818  bool PocketGrid::DARC_pocket_eval( core::conformation::Residue const & central_rsd, core::scoring::constraints::XYZ_Func const & xyz_func, Size const total_residues, numeric::xyzVector<core::Real> grid_center ) {
1819  //bool too_small=true;
1820  //while (too_small){
1821  //recenter( grid_center.x(), grid_center.y(), grid_center.z() );
1822  for ( Size j = 1, resnum = total_residues; j <= resnum; ++j ) {
1823  core::conformation::Residue const & rsd( xyz_func.residue(j) );
1824  bool target=false;
1825  if ( j == central_rsd.seqpos() ) target=true;
1826  core::Size total_atoms(0);
1827  using namespace basic::options;
1828  if (option[ OptionKeys::fingerprint::include_hydrogens ]()){
1829  total_atoms = rsd.natoms();
1830  } else {
1831  total_atoms = rsd.nheavyatoms();
1832  }
1833  for(Size i = 1, i_end = total_atoms; i <= i_end; ++i) {
1834  bool target_res=target;
1835  if (side_chains_only_){
1836  if ((central_rsd.atom(i).type()>=18)&&(central_rsd.atom(i).type()<=21)){
1837  target_res=false;
1838  }
1839  }
1840  numeric::xyzVector<core::Real> rpoint = rotatePoint(rsd.atom(i).xyz().x(),rsd.atom(i).xyz().y(),rsd.atom(i).xyz().z());
1841  mark(rpoint, rsd.atom_type(i).lj_radius(),probe_rad_, rsd.is_polar(), target_res);
1842  }
1843  }
1844  findPockets(0, maxLen_);
1845  if (markpsp_){
1846  findPSP(0,maxLen_);
1847  }
1848  if (marksps_){
1849  findSPS(0,ceil((xdim_+ydim_+zdim_)/stepSize_));
1850  }
1852  findClusters();
1853  /* Code to check edge of grid goes here
1854  if (isTooSmall()){
1855  core::Vector dims=whatIsTooSmall();
1856  if ((size_x_ + dims(1) > limit_x_) || (size_y_ + dims(2) > limit_y_) ||(size_z_ + dims(3) > limit_z_)){
1857  return false;
1858  }
1859  size_x_ = size_x_ + dims(1);
1860  size_y_ = size_y_ + dims(2);
1861  size_z_ = size_z_ + dims(3);
1862  initialize(grid_center.x(), grid_center.y(), grid_center.z(), size_x_, size_y_, size_z_, spacing_, markpsp_, marksps_);
1863  }else{
1864  too_small=false;
1865  }
1866  }*/
1868  return true;
1869  }
1870 
1871 
1873 
1874  // generate the pocket
1875  autoexpanding_pocket_eval( pose.conformation().residue(central_seqpos), pose );
1876 
1877  // get the pocket CoM and number of pocket points
1878  core::Size num_pocket_points=0;
1879  core::Real CoM_x=0.;
1880  core::Real CoM_y=0.;
1881  core::Real CoM_z=0.;
1882  for (std::list<PCluster>::const_iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1883  if (cit->points_.size()*pow(stepSize_,3)<minPockSize_) continue; // this is a smallpocket
1884  for (std::list<PCluster::Cxyz>::const_iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1885  if ( (grid_[pit->x][pit->y][pit->z]==TP_POCKET) || (grid_[pit->x][pit->y][pit->z]==TP_SURF) || (grid_[pit->x][pit->y][pit->z]==TP_EDGE) || (grid_[pit->x][pit->y][pit->z]==TP_BURIED) ) {
1886  CoM_x += pit->x*stepSize_+xcorn_;
1887  CoM_y += pit->y*stepSize_+ycorn_;
1888  CoM_z += pit->z*stepSize_+zcorn_;
1889  ++num_pocket_points;
1890  }
1891  }
1892  }
1893  CoM_x/=num_pocket_points;
1894  CoM_y/=num_pocket_points;
1895  CoM_z/=num_pocket_points;
1896 
1897  Eigen::MatrixXf pocket_coors_matrix(num_pocket_points,3);
1898  core::Size pocket_inx(0);
1899  // put pocket points into a matrix for Eigen, subtracting out the CoM so they'll be centered at zero
1900  for (std::list<PCluster>::const_iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1901  if (cit->points_.size()*pow(stepSize_,3)<minPockSize_) continue; // this is a smallpocket
1902  for (std::list<PCluster::Cxyz>::const_iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1903  if ( (grid_[pit->x][pit->y][pit->z]==TP_POCKET) || (grid_[pit->x][pit->y][pit->z]==TP_SURF) || (grid_[pit->x][pit->y][pit->z]==TP_EDGE) || (grid_[pit->x][pit->y][pit->z]==TP_BURIED) ) {
1904  // add this pocket point to the Eigen matrix (subtracting out the pocket CoM)
1905  pocket_coors_matrix(pocket_inx,0) = pit->x*stepSize_+xcorn_ - CoM_x;
1906  pocket_coors_matrix(pocket_inx,1) = pit->y*stepSize_+ycorn_ - CoM_y;
1907  pocket_coors_matrix(pocket_inx,2) = pit->z*stepSize_+zcorn_ - CoM_z;
1908  ++pocket_inx;
1909  }
1910  }
1911  }
1912 
1913  // do the SVD calculation
1914  Eigen::JacobiSVD<Eigen::MatrixXf> svd(pocket_coors_matrix, Eigen::ComputeFullU | Eigen::ComputeFullV );
1915  svd.computeV();
1916  Eigen::MatrixXf svd_result(3,3);
1917  svd_result = svd.matrixV();
1918  // std::cout << "Matrix V is: " << std::endl << svd.matrixV() << std::endl;
1919  // MatrixXf transformed = A * svd.matrixV();
1920 
1921  // setup a couple null operations
1922  numeric::xyzMatrix<core::Real> identity_mat;
1923  identity_mat.to_identity();
1925  zero_vec.zero();
1926 
1927  // setup the translation vector that will move the pose such that the pocket will be centered at zero
1929  trans_vec.x() = -CoM_x;
1930  trans_vec.y() = -CoM_y;
1931  trans_vec.z() = -CoM_z;
1932 
1933  // setup the rotation matrix that will rotate the pose to align the pocket PCA to the axes
1935  rot_mat.xx()=svd_result(0,0);
1936  rot_mat.xy()=svd_result(0,1);
1937  rot_mat.xz()=svd_result(0,2);
1938  rot_mat.yx()=svd_result(1,0);
1939  rot_mat.yy()=svd_result(1,1);
1940  rot_mat.yz()=svd_result(1,2);
1941  rot_mat.zx()=svd_result(2,0);
1942  rot_mat.zy()=svd_result(2,1);
1943  rot_mat.zz()=svd_result(2,2);
1944 
1945  // first do the translation (with no rotation), then do the rotation (with no translation)
1946  pose.apply_transform_Rx_plus_v(identity_mat, trans_vec);
1947  pose.apply_transform_Rx_plus_v(rot_mat, zero_vec);
1948 
1949  // jk DEBUG CODE
1950  /*
1951  // transform then print the original pocket using this transform,
1952  // to ensure PCA works and see how different pocket is when it's recalculated
1953  utility::io::ozstream outPDB_stream;
1954  outPDB_stream.open("transformed_starting_grid.pdb", std::ios::out);
1955  int counter=1;
1956  int counter2=1;
1957  for (std::list<PCluster>::const_iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
1958  if (cit->points_.size()*pow(stepSize_,3)<minPockSize_) continue; // this is a smallpocket
1959  int clustNo=1;
1960  for (std::list<PCluster::Cxyz>::const_iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
1961  if ( (grid_[pit->x][pit->y][pit->z]==TP_POCKET) || (grid_[pit->x][pit->y][pit->z]==TP_SURF) || (grid_[pit->x][pit->y][pit->z]==TP_EDGE) || (grid_[pit->x][pit->y][pit->z]==TP_BURIED) ) {
1962  numeric::xyzVector<core::Real> orig_coors;
1963  orig_coors.x() = pit->x*stepSize_+xcorn_;
1964  orig_coors.y() = pit->y*stepSize_+ycorn_;
1965  orig_coors.z() = pit->z*stepSize_+zcorn_;
1966 
1967  orig_coors += trans_vec;
1968  numeric::xyzVector<core::Real> new_coors = rot_mat * orig_coors;
1969 
1970  bool smallPocket;
1971  std::string concatenated_pdb_info;
1972  concatenated_pdb_info += "ATOM ";
1973  std::stringstream tmp;
1974  tmp<<counter2;
1975  if (counter2<10) concatenated_pdb_info += " ";
1976  else if (counter2<100) concatenated_pdb_info += " ";
1977  else if (counter2<1000) concatenated_pdb_info += " ";
1978  else if (counter2<10000) concatenated_pdb_info += " ";
1979  else concatenated_pdb_info += "";
1980  concatenated_pdb_info += tmp.str()+" ";
1981  if (smallPocket){
1982  if (grid_[pit->x][pit->y][pit->z]==TP_POCKET) concatenated_pdb_info += "STP STP ";
1983  if (grid_[pit->x][pit->y][pit->z]==TP_SURF) concatenated_pdb_info += "STS STS ";
1984  if (grid_[pit->x][pit->y][pit->z]==TP_BURIED) concatenated_pdb_info += "STB STB ";
1985  if (grid_[pit->x][pit->y][pit->z]==TP_EDGE) concatenated_pdb_info += "STE STE ";
1986  }else{
1987  if (grid_[pit->x][pit->y][pit->z]==TP_POCKET) concatenated_pdb_info += "TP TP ";
1988  if (grid_[pit->x][pit->y][pit->z]==TP_SURF) concatenated_pdb_info += "TPS TPS ";
1989  if (grid_[pit->x][pit->y][pit->z]==TP_BURIED) concatenated_pdb_info += "TPB TPB ";
1990  if (grid_[pit->x][pit->y][pit->z]==TP_EDGE) concatenated_pdb_info += "TPE TPE ";
1991  }
1992  if (grid_[pit->x][pit->y][pit->z]==POCKET) concatenated_pdb_info += "PC PC ";
1993  if (grid_[pit->x][pit->y][pit->z]==PO_SURF) concatenated_pdb_info += "PCS PCS ";
1994  if (grid_[pit->x][pit->y][pit->z]==PO_BURIED) concatenated_pdb_info += "PCB PCB ";
1995  if (grid_[pit->x][pit->y][pit->z]==PO_EDGE) concatenated_pdb_info += "PCE PCE ";
1996  tmp.str(std::string());
1997  tmp<<clustNo;
1998  if (clustNo<10) concatenated_pdb_info += " ";
1999  else if (clustNo<100) concatenated_pdb_info += " ";
2000  else if (clustNo<1000) concatenated_pdb_info += " ";
2001  concatenated_pdb_info += tmp.str()+" ";
2002  tmp.str(std::string());
2003  tmp<<" "<<std::setw(8)<<std::fixed<<std::setprecision(3)<<new_coors.x()<<std::setw(8)<<new_coors.y()<<std::setw(8)<<new_coors.z()<<std::endl;
2004  concatenated_pdb_info += tmp.str();
2005  counter2++;
2006  outPDB_stream<<concatenated_pdb_info;
2007  }
2008  }
2009  clustNo++;
2010  }
2011  outPDB_stream.close();
2012  outPDB_stream.clear();
2013  */
2014 
2015  // clear the pocket (to be regenerated outside this function)
2016  init();
2018 
2019  return;
2020 }
2021 
2022 
2023 
2024 core::Real PocketGrid::get_pocket_distance( PocketGrid const & template_pocket, std::string const & comparison_pdbname ) const {
2025 
2026 
2027  bool write_comparison_pdb = false;
2028  // note: string comparison will return non-zero if the strings are NOT equal
2029  if ( comparison_pdbname.compare("") ) {
2030  write_comparison_pdb = true;
2031  }
2032 
2033  utility::io::ozstream comparisonPDB_stream;
2034  if ( write_comparison_pdb ) comparisonPDB_stream.open(comparison_pdbname, std::ios::out);
2035 
2036  core::Real const full_match_weight = 1.;
2037  core::Real const partial_match_weight = 0.25;
2038  core::Real const mismatch_weight = 0.;
2039  core::Real match_score = 0.;
2040  core::Size output_res_num = 1.;
2041 
2042  // Loop over all points in the template pocket
2043  core::Size template_num_points = 0;
2044  for (std::list<PCluster>::const_iterator cit=template_pocket.clusters_.clusters_.begin(); cit != template_pocket.clusters_.clusters_.end(); ++cit){
2045  if (cit->points_.size()*pow(template_pocket.stepSize_,3)<template_pocket.minPockSize_) continue; // this is a smallpocket
2046  for (std::list<PCluster::Cxyz>::const_iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
2047  if ( (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_POCKET) || (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_SURF) || (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_EDGE) || (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_BURIED) ) {
2048 
2049  ++template_num_points;
2050 
2051  // For this template pocket point, get the cartesian coors and convert to comparison's grid
2052  core::Real const template_x = pit->x*template_pocket.stepSize_+template_pocket.xcorn_;
2053  core::Real const template_y = pit->y*template_pocket.stepSize_+template_pocket.ycorn_;
2054  core::Real const template_z = pit->z*template_pocket.stepSize_+template_pocket.zcorn_;
2055 
2056  core::Size const self_x_index = floor( ( ( template_x - xcorn_ ) / stepSize_ ) + 0.5 );
2057  core::Size const self_y_index = floor( ( ( template_y - ycorn_ ) / stepSize_ ) + 0.5 );
2058  core::Size const self_z_index = floor( ( ( template_z - zcorn_ ) / stepSize_ ) + 0.5 );
2059 
2060  //Check to see if template point is within comparison's grid range
2061  if ( ( self_x_index < xdim_ ) && ( self_y_index < ydim_ ) && ( self_z_index < zdim_ ) ) {
2062  // note: no need to check that self_index >= 0 since it's of type "Size"
2063  // if ( ( self_x_index >= 0 ) && ( self_x_index < xdim_ ) && ( self_y_index >= 0 ) && ( self_y_index < ydim_ ) && ( self_z_index >= 0 ) && ( self_z_index < zdim_ ) ) {
2064 
2065  // jk note: later, we could actually match sub-pocket types - will this give better results??
2066  //See if template point is pocket for comparison
2067  if ( ( grid_[self_x_index][self_y_index][self_z_index] == TP_POCKET ) || ( grid_[self_x_index][self_y_index][self_z_index] == TP_SURF ) || ( grid_[self_x_index][self_y_index][self_z_index] == TP_EDGE ) || ( grid_[self_x_index][self_y_index][self_z_index] == TP_BURIED ) ) {
2068 
2069  core::Real curr_weight = partial_match_weight;
2070  if ( ( grid_[self_x_index][self_y_index][self_z_index] == TP_POCKET ) && (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_POCKET) ) {
2071  curr_weight = full_match_weight;
2072  } else if ( ( grid_[self_x_index][self_y_index][self_z_index] == TP_SURF ) && (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_SURF) ) {
2073  curr_weight = full_match_weight;
2074  } else if ( ( grid_[self_x_index][self_y_index][self_z_index] == TP_EDGE ) && (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_EDGE) ) {
2075  curr_weight = full_match_weight;
2076  } else if ( ( grid_[self_x_index][self_y_index][self_z_index] == TP_BURIED ) && (template_pocket.grid_[pit->x][pit->y][pit->z]==template_pocket.TP_BURIED) ) {
2077  curr_weight = full_match_weight;
2078  }
2079  match_score += curr_weight;
2080 
2081  // KK output_res_num needed
2082  if ( write_comparison_pdb ) comparisonPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C MPO A "<<std::setw(8)<<output_res_num<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_x<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_y<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_z<<std::endl;
2083  } else {
2084  match_score += mismatch_weight;
2085  // KK output_res_num needed
2086  if ( write_comparison_pdb ) comparisonPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C NPO A "<<std::setw(8)<<output_res_num<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_x<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_y<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_z<<std::endl;
2087  }
2088  } else {
2089  //Score as mismatch if it's out of comparison's grid
2090  match_score += mismatch_weight;
2091  // KK output_res_num needed
2092  if ( write_comparison_pdb ) comparisonPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C NPO A "<<std::setw(8)<<output_res_num<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_x<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_y<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_z<<std::endl;
2093  }
2094  output_res_num++;
2095  }
2096 
2097  }
2098  }
2099 
2100  if ( write_comparison_pdb ) {
2101  comparisonPDB_stream.close();
2102  comparisonPDB_stream.clear();
2103  }
2104 
2105 
2106  core::Size self_num_points = 0;
2107  for (std::list<PCluster>::const_iterator cit=clusters_.clusters_.begin(); cit != clusters_.clusters_.end(); ++cit){
2108  if (cit->points_.size()*pow(stepSize_,3)<minPockSize_) continue; // this is a smallpocket
2109  for (std::list<PCluster::Cxyz>::const_iterator pit=cit->points_.begin(); pit != cit->points_.end(); ++pit){
2110  if ( (grid_[pit->x][pit->y][pit->z]==TP_POCKET) || (grid_[pit->x][pit->y][pit->z]==TP_SURF) || (grid_[pit->x][pit->y][pit->z]==TP_EDGE) || (grid_[pit->x][pit->y][pit->z]==TP_BURIED) ) {
2111  ++self_num_points;
2112  }
2113  }
2114  }
2115 
2116  // note: non-trivial to average number of points in each pocket, since we don't know how many points in the
2117  match_score = 2. * match_score / ( template_num_points + self_num_points );
2118  TR << "PocketGrid match score = " << match_score << std::endl;
2119 
2120  return 1. - sqrt( match_score );
2121 }
2122 
2123 EggshellGrid::EggshellGrid( const PocketGrid& ext_grd, std::list< numeric::xyzVector<core::Real> > const & eggshell_coord_list):
2124  PocketGrid( ext_grd)
2125 {
2126  init();
2127  extra_coord_list_.clear();
2128  using namespace basic::options;
2129  core::Real const extra_dist = option[ OptionKeys::pocket_grid::extra_eggshell_dist ];
2130  core::Size searchxmin, searchxmax, searchymin, searchymax, searchzmin, searchzmax, xx,yy,zz;
2131  numeric::xyzVector<core::Real> extra_coord;
2132  for (core::Size x=0;x<(ext_grd.xdim_); x++){
2133  for (core::Size y=0;y<(ext_grd.ydim_); y++){
2134  for (core::Size z=0;z<(ext_grd.zdim_); z++){
2135  if ( (ext_grd.grid_[x][y][z] == ext_grd.HSURFACE) || (ext_grd.grid_[x][y][z] == ext_grd.PSURFACE) || (ext_grd.grid_[x][y][z] == ext_grd.T_SURFACE) ){
2136  if (x != 0) {
2137  searchxmin=x-1;
2138  }else{
2139  searchxmin=x;
2140  }
2141  if (x != ext_grd.xdim_-1) {
2142  searchxmax=x+1;
2143  }else{
2144  searchxmax=x;
2145  }
2146  if (y != 0) {
2147  searchymin=y-1;
2148  }else{
2149  searchymin=y;
2150  }
2151  if (y != ext_grd.ydim_-1) {
2152  searchymax=y+1;
2153  }else{
2154  searchymax=y;
2155  }
2156  if (z != 0) {
2157  searchzmin=z-1;
2158  }else{
2159  searchzmin=z;
2160  }
2161  if (z != ext_grd.zdim_-1) {
2162  searchzmax=z+1;
2163  }else{
2164  searchzmax=z;
2165  }
2166  bool found = false;
2167  for (xx = searchxmin; xx<=searchxmax; ++xx){
2168  for (yy = searchymin; yy<=searchymax; ++yy){
2169  for (zz = searchzmin; zz<=searchzmax; ++zz){
2170  if (xx==x && yy==y && zz==z) continue;
2171  if ( ( ext_grd.grid_[xx][yy][zz]==ext_grd.EMPTY ) || (ext_grd.grid_[xx][yy][zz] == ext_grd.TP_POCKET) || (ext_grd.grid_[xx][yy][zz] == ext_grd.TP_EDGE) || (ext_grd.grid_[xx][yy][zz] == ext_grd.TP_BURIED) || (ext_grd.grid_[xx][yy][zz] == ext_grd.TP_SURF) || (ext_grd.grid_[xx][yy][zz] == ext_grd.POCKET) || (ext_grd.grid_[xx][yy][zz] == ext_grd.PO_BURIED) || (ext_grd.grid_[xx][yy][zz] == ext_grd.PO_SURF) || (ext_grd.grid_[xx][yy][zz] == ext_grd.PO_EDGE) ) {
2172  extra_coord.x() = (x*ext_grd.stepSize_+ext_grd.xcorn_);
2173  extra_coord.y() = (y*ext_grd.stepSize_+ext_grd.ycorn_);
2174  extra_coord.z() = (z*ext_grd.stepSize_+ext_grd.zcorn_);
2175  found = true;
2176  bool nearby = false;
2177  for (std::list< numeric::xyzVector<core::Real> >::const_iterator eg = eggshell_coord_list.begin(); eg != eggshell_coord_list.end(); ++eg) {
2178  if (extra_coord == *eg) {
2179  nearby = false;
2180  break;
2181  }
2182  if ( extra_coord.distance(*eg) < extra_dist ) {
2183  nearby = true;
2184  }
2185  }
2186  if (nearby){
2187  extra_coord_list_.push_back(extra_coord);
2188  }
2189  }
2190  if (found) break;
2191  }
2192  if (found) break;
2193  }
2194  if (found) break;
2195  }
2196  }
2197  }
2198  }
2199  }
2200 
2201 //Print grid points into a PDB file
2202  using namespace basic::options;
2203  if (option[ OptionKeys::pocket_grid::print_grid ]()){
2204  write_grid_to_pdb(ext_grd, "ext_grd.pdb");
2205  }
2206  return;
2207 
2208 }
2209 
2211  // initialize member data using PocketGrid
2212  PocketGrid( gr )
2213 {
2214  init();
2215 
2216  eggshell_coord_list_.clear();
2217  // use Grid to fill in Eggshell grid
2218  numeric::xyzVector<core::Real> grid_coord;
2219  core::Size searchxmin, searchxmax, searchymin, searchymax, searchzmin, searchzmax, xx,yy,zz;
2220  for (core::Size x=0;x<(gr.xdim_); x++){
2221  for (core::Size y=0;y<(gr.ydim_); y++){
2222  for (core::Size z=0;z<(gr.zdim_); z++){
2223  if ( (gr.grid_[x][y][z] == gr.HSURFACE) || (gr.grid_[x][y][z] == gr.PSURFACE) || (gr.grid_[x][y][z] == gr.T_SURFACE) /*||(gr.grid_[x][y][z]==gr.TP_SURF)*/ ) {
2224  if (x != 0) {
2225  searchxmin=x-1;
2226  }else{
2227  searchxmin=x;
2228  }
2229  if (x != gr.xdim_-1) {
2230  searchxmax=x+1;
2231  }else{
2232  searchxmax=x;
2233  }
2234  if (y != 0) {
2235  searchymin=y-1;
2236  }else{
2237  searchymin=y;
2238  }
2239  if (y != gr.ydim_-1) {
2240  searchymax=y+1;
2241  }else{
2242  searchymax=y;
2243  }
2244  if (z != 0) {
2245  searchzmin=z-1;
2246  }else{
2247  searchzmin=z;
2248  }
2249  if (z != gr.zdim_-1) {
2250  searchzmax=z+1;
2251  }else{
2252  searchzmax=z;
2253  }
2254  bool found=false;
2255  for (xx = searchxmin; (xx<=searchxmax) && ! found; xx++){
2256  for (yy = searchymin; (yy<=searchymax) && ! found; yy++){
2257  for (zz = searchzmin; (zz<=searchzmax) && ! found; zz++){
2258  if (xx==x && yy==y && zz==z) continue;
2259  if ( (gr.grid_[xx][yy][zz] == gr.TP_POCKET) || (gr.grid_[xx][yy][zz] == gr.TP_EDGE) || (gr.grid_[xx][yy][zz] == gr.TP_BURIED) ) {
2260  found = true;
2261  grid_[x][y][z] = EGGSHELL;
2262  grid_coord.x() = (x*gr.stepSize_+gr.xcorn_);
2263  grid_coord.y() = (y*gr.stepSize_+gr.ycorn_);
2264  grid_coord.z() = (z*gr.stepSize_+gr.zcorn_);
2265  eggshell_coord_list_.push_back(grid_coord);
2266  }
2267  }
2268  }
2269  }
2270  }
2271  }
2272  }
2273  }
2274 
2275  //set eggshell CoM from eggshell_coord_list
2277 
2278  //Print grid points into a PDB file
2279  using namespace basic::options;
2280  if (option[ OptionKeys::pocket_grid::print_grid ]()){
2281  write_grid_to_pdb(gr, "egg_grd.pdb");
2282  }
2283  return;
2284 
2285 }
2286 
2288 
2289  // NOTE: THIS FUNCTION DOESN'T FULLY SETUP THE GRID OR extra_coord_list_
2290  // HERE WE ONLY PROVIDE MINIMAL FUNCTIONALITY TO DO EGGSHELL VS EGGSHELL COMPARISONS....
2291 
2292  utility::io::izstream instream;
2293  instream.open(fname, std::ios::in);
2294 
2295  // Read stepSize_ xdim_ ydim_ zdim_ xcorn_ ycorn_ zcorn_
2296  instream >> stepSize_ >> xdim_ >> ydim_ >> zdim_ >> xcorn_ >> ycorn_ >> zcorn_;
2297 
2298  // Reset the grid for the new dimensions
2299  init();
2300  eggshell_coord_list_.clear();
2301  extra_coord_list_.clear();
2302 
2303  // Read the grid points which should be marked EGGSHELL, use these to push onto eggshell_coord_list_
2304  // Format is x y z
2305  core::Size x, y, z;
2306  while ( ! instream.eof() ) {
2307  instream >> x >> y >> z;
2308  grid_[x][y][z] = EGGSHELL;
2309  numeric::xyzVector<core::Real> grid_coord;
2310  grid_coord.x() = x*stepSize_+xcorn_;
2311  grid_coord.y() = y*stepSize_+ycorn_;
2312  grid_coord.z() = z*stepSize_+zcorn_;
2313  eggshell_coord_list_.push_back(grid_coord);
2314  }
2315 
2316  instream.close();
2317  instream.clear();
2318 
2319 }
2320 
2321 
2322 void EggshellGrid::dump_eggshell( std::string const & fname ) const {
2323 
2324  // NOTE: THIS FUNCTION DOESN'T WRITE THE WHOLE GRID OR extra_coord_list_
2325  // HERE WE ONLY PROVIDE MINIMAL FUNCTIONALITY TO DO EGGSHELL VS EGGSHELL COMPARISONS....
2326 
2327  utility::io::ozstream outstream;
2328  outstream.open(fname, std::ios::out);
2329 
2330  // Print stepSize_ xdim_ ydim_ zdim_ xcorn_ ycorn_ zcorn_
2331  outstream << stepSize_ << " " << xdim_ << " " << ydim_ << " " << zdim_ << " " << xcorn_ << " " << ycorn_ << " " << zcorn_ << std::endl;
2332 
2333  // Need to print each grid point if it's of type EGGSHELL along with the corresponding cartesian coors ( for eggshell_coord_list_ )
2334  for (core::Size x=0;x<xdim_; x++) {
2335  for (core::Size y=0;y<ydim_; y++) {
2336  for (core::Size z=0;z<zdim_; z++) {
2337  if ( ( grid_[x][y][z]==EGGSHELL ) ) {
2338  outstream << x << " " << y << " " << z << std::endl;
2339  // outstream << x << " " << y << " " << z << " " << x*stepSize_+xcorn_ << " " << y*stepSize_+ycorn_ << " " << z*stepSize_+zcorn_ << std::endl;
2340  }
2341  }
2342  }
2343  }
2344 
2345  outstream.close();
2346  outstream.clear();
2347 
2348 }
2349 
2350 void EggshellGrid::write_eggshell_to_pdb( std::string const & output_pdbname ) const {
2351 
2352  utility::io::ozstream outPDB_stream;
2353  outPDB_stream.open(output_pdbname, std::ios::out);
2354  for (std::list< numeric::xyzVector<core::Real> >::const_iterator pd = eggshell_coord_list_.begin(); pd != eggshell_coord_list_.end(); ++pd) {
2355  outPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C EGG A 1 "<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pd->x()<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pd->y()<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pd->z()<<std::endl;
2356  }
2357  for (std::list< numeric::xyzVector<core::Real> >::const_iterator pd = extra_coord_list_.begin(); pd != extra_coord_list_.end(); ++pd) {
2358  outPDB_stream<<"HETATM "<<std::setw(2)<<1<<" O EXT B 1 "<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pd->x()<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pd->y()<<std::setw(8)<<std::fixed<<std::setprecision(3)<<pd->z()<<std::endl;
2359  }
2360  outPDB_stream.close();
2361  outPDB_stream.clear();
2362 
2363 }
2364 
2365 void EggshellGrid::write_grid_to_pdb( const PocketGrid& gr, std::string const & output_pdbname ) const {
2366 
2367  utility::io::ozstream outPDB_stream;
2368  outPDB_stream.open(output_pdbname, std::ios::out);
2369 
2370  for (core::Size x=0;x<(gr.xdim_); x++){
2371  for (core::Size y=0;y<(gr.ydim_); y++){
2372  for (core::Size z=0;z<(gr.zdim_); z++){
2373  outPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C GRD A 1 "<<std::setw(8)<<std::fixed<<std::setprecision(3)<<x*gr.stepSize_+gr.xcorn_<<std::setw(8)<<std::fixed<<std::setprecision(3)<<y*gr.stepSize_+gr.ycorn_<<std::setw(8)<<std::fixed<<std::setprecision(3)<<z*gr.stepSize_+gr.zcorn_<<std::endl;
2374  }
2375  }
2376  }
2377  outPDB_stream.close();
2378  outPDB_stream.clear();
2379 
2380  return;
2381 
2382 }
2383 
2385 
2386  numeric::xyzVector<core::Real> pocketshell_CoM(0.);
2387  for (std::list< numeric::xyzVector<core::Real> >::const_iterator pd = pocketshell_coord_list.begin(); pd != pocketshell_coord_list.end(); ++pd) {
2388  pocketshell_CoM.x() += pd->x();
2389  pocketshell_CoM.y() += pd->y();
2390  pocketshell_CoM.z() += pd->z();
2391  }
2392  pocketshell_CoM /= pocketshell_coord_list.size();
2393  return pocketshell_CoM;
2394  }
2395 
2396 core::Real EggshellGrid::get_eggshell_distance( EggshellGrid const & template_eggshell, std::string const & comparison_pdbname ) const {
2397 
2398  TR << "My CoM is " << eggshell_CoM_.x() << " " << eggshell_CoM_.y() << " " << eggshell_CoM_.z() << std::endl;
2399  TR << "Template CoM is " << template_eggshell.eggshell_CoM_.x() << " " << template_eggshell.eggshell_CoM_.y() << " " << template_eggshell.eggshell_CoM_.z() << std::endl;
2400 
2401  bool write_comparison_pdb = false;
2402  // note: string comparison will return non-zero if the strings are NOT equal
2403  if ( comparison_pdbname.compare("") ) {
2404  write_comparison_pdb = true;
2405  }
2406 
2407  utility::io::ozstream comparisonPDB_stream;
2408  if ( write_comparison_pdb ) comparisonPDB_stream.open(comparison_pdbname, std::ios::out);
2409 
2410  core::Real const match_weight = 1.;
2411  core::Real const mismatch_weight = 0.;
2412  core::Real match_score = 0.;
2413  core::Size output_res_num = 1.;
2414 
2415  // Loop over all points in the template eggshell
2416  for (std::list< numeric::xyzVector<core::Real> >::const_iterator pd = template_eggshell.eggshell_coord_list().begin(); pd != template_eggshell.eggshell_coord_list().end(); ++pd) {
2417 
2418  // For this template eggshell point, get the cartesian coors and convert to comparison's grid
2419  core::Real const template_x = pd->x();
2420  core::Real const template_y = pd->y();
2421  core::Real const template_z = pd->z();
2422  core::Size const self_x_index = floor( ( ( template_x - xcorn_ ) / stepSize_ ) + 0.5 );
2423  core::Size const self_y_index = floor( ( ( template_y - ycorn_ ) / stepSize_ ) + 0.5 );
2424  core::Size const self_z_index = floor( ( ( template_z - zcorn_ ) / stepSize_ ) + 0.5 );
2425 
2426  //Check to see if template point is within comparison's grid range
2427  if ( ( self_x_index < xdim_ ) && ( self_y_index < ydim_ ) && ( self_z_index < zdim_ ) ) {
2428  // note: no need to check that self_index >= 0 since it's of type "Size"
2429  // if ( ( self_x_index >= 0 ) && ( self_x_index < xdim_ ) && ( self_y_index >= 0 ) && ( self_y_index < ydim_ ) && ( self_z_index >= 0 ) && ( self_z_index < zdim_ ) ) {
2430  //See if template point is eggshell for comparison
2431  if ( grid_[self_x_index][self_y_index][self_z_index] == EGGSHELL ) {
2432  match_score += match_weight;
2433  // KK output_res_num needed
2434  if ( write_comparison_pdb ) comparisonPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C MEG A "<<std::setw(8)<<output_res_num<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_x<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_y<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_z<<std::endl;
2435 
2436  } else {
2437  match_score += mismatch_weight;
2438  // KK output_res_num needed
2439  if ( write_comparison_pdb ) comparisonPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C NEG A "<<std::setw(8)<<output_res_num<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_x<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_y<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_z<<std::endl;
2440  }
2441  } else {
2442  //Score as mismatch if it's out of comparison's grid
2443  match_score += mismatch_weight;
2444  // KK output_res_num needed
2445  if ( write_comparison_pdb ) comparisonPDB_stream<<"HETATM "<<std::setw(2)<<1<<" C NEG A "<<std::setw(8)<<output_res_num<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_x<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_y<<std::setw(8)<<std::fixed<<std::setprecision(3)<<template_z<<std::endl;
2446  }
2447  output_res_num++;
2448  }
2449 
2450  if ( write_comparison_pdb ) {
2451  comparisonPDB_stream.close();
2452  comparisonPDB_stream.clear();
2453  }
2454 
2455  match_score = 2. * match_score / ( template_eggshell.eggshell_coord_list().size() + eggshell_coord_list().size() );
2456  TR << "Eggshell match score = " << match_score << std::endl;
2457 
2458  return 1. - match_score;
2459 }
2460 
2461 
2462  } // pockets
2463 } // protocols