Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NeighborsByDistanceCalculator.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file /src/protocols/toolbox/PoseMetricCalculators/NeighborsByDistanceCalculator.cc
11 /// @brief NeighborsByDistanceCalculator can determine all the neighbors of a residue within a certain distance. The pose does not have to have been scored (have a full Energies object). It uses the PointGraph tools to find neighbors. There is probably a much more sophisticated way to do this with existing Graph tools but I don't know what it is.
12 /// @author Steven Lewis
13 
14 
15 
16 //Unit headers
18 
19 //
20 #include <core/pose/Pose.hh>
21 
22 #include <basic/MetricValue.hh>
23 
27 
29 
30 //Utility headers
31 //#include <basic/options/option.hh>
32 //#include <core/types.hh>
33 #include <basic/Tracer.hh>
34 #include <utility/exit.hh>
35 #include <utility/string_util.hh>
36 
37 // option key includes
38 #include <basic/options/keys/packing.OptionKeys.gen.hh>
39 
40 //C++ headers
41 //#include <set>
42 
43 static basic::Tracer TR("protocols.toolbox.PoseMetricCalculators.NeighborsByDistanceCalculator");
44 
45 namespace protocols{
46 namespace toolbox {
47 namespace pose_metric_calculators {
48 
50  : parent(), central_residue_(central_residue), dist_cutoff_(dist_cutoff), num_neighbors_(0)
51  //not doing anything to std::set<core::Size> - should initialize empty
52 {}
53 
55  : parent(), central_residue_(calculator.central_residue()), dist_cutoff_(calculator.dist_cutoff())
56 {}
57 
59 { return new NeighborsByDistanceCalculator(*this); }
60 
61 void
63  std::string const & key,
64  basic::MetricValueBase * valptr
65 ) const
66 {
67  if ( key == "central_residue" ) {
68  basic::check_cast( valptr, &central_residue_, "central_residue expects to return a core::Size" );
69  (static_cast<basic::MetricValue<core::Size> *>(valptr))->set( central_residue_ );
70 
71  } else if ( key == "dist_cutoff" ) {
72  basic::check_cast( valptr, &dist_cutoff_, "dist_cutoff expects to return a core::Real" );
73  (static_cast<basic::MetricValue<core::Real> *>(valptr))->set( dist_cutoff_ );
74 
75  } else if ( key == "num_neighbors" ) {
76  basic::check_cast( valptr, &num_neighbors_, "num_neighbors expects to return a core::Size" );
77  (static_cast<basic::MetricValue<core::Size> *>(valptr))->set( num_neighbors_ );
78 
79  } else if ( key == "neighbors" ) {
80  basic::check_cast( valptr, &neighbors_, "neighbors expects to return a std::set< core::Size >" );
81  (static_cast<basic::MetricValue< std::set< core::Size > > *>(valptr))->set( neighbors_ );
82 
83  } else {
84  basic::Error() << "NeighborsByDistanceCalculator cannot compute metric " << key << std::endl;
85  utility_exit();
86  }
87 
88 } //lookup
89 
92 {
93  if ( key == "central_residue" ) {
94  return utility::to_string( central_residue_ );
95 
96  } else if ( key == "dist_cutoff" ) {
97  return utility::to_string( dist_cutoff_ );
98 
99  } else if ( key == "num_neighbors" ) {
100  return utility::to_string( num_neighbors_ );
101 
102  } else if ( key == "neighbors" ) {
103  using namespace basic::options; //this lets you get + or (space) as spacer
104  std::string const spacer( option[ OptionKeys::packing::print_pymol_selection].value() ? "+" : " ");
105  std::string nbrs_string("");
106  for( std::set< core::Size >::const_iterator it(neighbors_.begin()), end(neighbors_.end()); it != end; ++it)
107  nbrs_string += utility::to_string(*it) + spacer;
108  return nbrs_string;
109 
110  }//else
111  basic::Error() << "NeighborsByDistanceCalculator cannot compute metric " << key << std::endl;
112  utility_exit();
113  return "";
114 } //print
115 
116 void
118 {
119  //clear old data
120  neighbors_.clear();
121  num_neighbors_ = 0;
122 
123  //if the central residue was never set, or was set outside the pose, this will cause problems!
124  if( (central_residue_ < 1) || (central_residue_ > pose.total_residue())) {
125  TR.Error << "central residue " << central_residue_ << " outside of pose; NBDC reporting empty set!" << std::endl;
126  return;
127  }
128 
129 ///This is not necessarily the best implementation of this - this code's real utility is that it patches cleanly into the Calculator and TaskOperation hierarchies. If you have a better/faster implementation, replace this and feel no guilt.
130 
133  core::conformation::find_neighbors<core::conformation::PointGraphVertexData,core::conformation::PointGraphEdgeData>( pg, dist_cutoff_ ); //create edges
134 
135  for ( core::Size r(1); r <= central_residue_; ++r){
136  for ( core::conformation::PointGraph::UpperEdgeListConstIter edge_iter = pg->get_vertex(r).upper_edge_list_begin(),
137  edge_end_iter = pg->get_vertex(r).upper_edge_list_end(); edge_iter != edge_end_iter; ++edge_iter ) {
138  core::Size const other = edge_iter->upper_vertex();
139 
140  //we know the two nodes connected to this edge. We want to remember the one that is NOT central_residue_, if one of them IS central_residue_. if neither is central_residue_, do nothing.
141  if (other == central_residue_) {
142  neighbors_.insert(r);
143  }
144  else if ( r == central_residue_ ){
145  neighbors_.insert(other);
146  }
147  }
148  }
149 
150  //now account for the residue itself
152  num_neighbors_ = neighbors_.size();
153 
154  return;
155 } //recompute
156 
157 } //namespace pose_metric_calculators
158 } //namespace toolbox
159 } //namespace protocols