Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ActiveSiteGrid.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file protocols/match/downstream/ActiveSiteGrid.cc
12 /// @brief
13 /// @author Alex Zanghellini (zanghell@u.washington.edu)
14 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com), porting to mini
15 
16 // Unit headers
18 
19 // Package headers
21 
22 // Project headers
24 
25 // Utility headers
26 #include <utility/io/izstream.hh>
27 #include <utility/string_util.hh>
28 
29 // ObjexxFCL headers
30 #include <ObjexxFCL/string.functions.hh>
31 
32 #include <utility/vector1.hh>
33 
34 
35 namespace protocols {
36 namespace match {
37 namespace downstream {
38 
39 using namespace ObjexxFCL;
40 
42 
44  bin_width_( 0.0 ),
45  bb_( Vector( 0.0 ), Vector( 0.0 ) ),
46  grid_( new Bool3DGrid ),
47  reset_grid_bb_( false )
48 {
49 }
50 
52  utility::pointer::ReferenceCount(),
53  bin_width_( other.bin_width_ ),
54  bb_( other.bb_ ),
55  grid_( new Bool3DGrid( *other.grid_ )),
56  reset_grid_bb_( other.reset_grid_bb_ )
57 {
58 }
59 
60 ActiveSiteGrid const &
62 {
63  if ( this != & rhs ) {
64  bin_width_ = rhs.bin_width_;
65  bb_ = rhs.bb_;
66  grid_ = new Bool3DGrid( *rhs.grid_ );
68  }
69  return *this;
70 }
71 
72 void
74 {
75  std::string filename = fname;
76  utility::io::izstream istr( filename.c_str() );
77 
78  if ( ! istr ) {
79  utility_exit_with_message( "Gridlig file " + fname + " not found" );
80  }
81 
82  std::string name, liggrid;
83  istr >> name >> liggrid;
84  runtime_assert( name == "NAME:" );
85  runtime_assert( liggrid == "gridlig" );
86  std::string base;
87  Real xbase( 0.0 ), ybase( 0.0 ), zbase( 0.0 );
88  istr >> base;
89  runtime_assert( base == "BASE:" );
90  istr >> xbase; runtime_assert( ! istr.bad() );
91  istr >> ybase; runtime_assert( ! istr.bad() );
92  istr >> zbase; runtime_assert( ! istr.bad() );
93 
95  istr >> size;
96  runtime_assert( size == "SIZE:" );
97  Size xsize( 0 ), ysize( 0 ), zsize( 0 );
98  istr >> xsize; runtime_assert( ! istr.bad() );
99  istr >> ysize; runtime_assert( ! istr.bad() );
100  istr >> zsize; runtime_assert( ! istr.bad() );
101 
102  runtime_assert( xsize != 0 );
103  runtime_assert( ysize != 0 );
104  runtime_assert( zsize != 0 );
105 
106  std::string length;
107  istr >> length;
108  runtime_assert( length == "LENGTH:");
109  Real xwidth( 0.0 ), ywidth( 0.0 ), zwidth( 0.0 );
110 
111  istr >> xwidth; runtime_assert( ! istr.bad() );
112  istr >> ywidth; runtime_assert( ! istr.bad() );
113  istr >> zwidth; runtime_assert( ! istr.bad() );
114 
115  runtime_assert( xwidth != 0 );
116  runtime_assert( ywidth != 0 );
117  runtime_assert( zwidth != 0 );
118 
119  /// the widths must be the same for all three dimensions.
120  runtime_assert( xwidth == ywidth );
121  runtime_assert( xwidth == zwidth );
122 
123  Vector lower_corner( xbase, ybase, zbase );
124  Vector upper_corner( lower_corner );
125  upper_corner.x() += xwidth * xsize;
126  upper_corner.y() += ywidth * ysize;
127  upper_corner.z() += zwidth * zsize;
128 
129  bin_width_ = xwidth;
130  bb_ = BoundingBox( lower_corner, upper_corner );
131  grid_ = new Bool3DGrid;
132  grid_->set_bin_width( bin_width_ );
133  grid_->set_bounding_box( bb_ );
134  reset_grid_bb_ = false;
135 
136 
137  numeric::geometry::hashing::Bin3D bin; bin[ 1 ] = 0; bin[ 2 ] = 0; bin[ 3 ] = 0;
138  std::string line;
139  std::istringstream line_stream;
140  Size occupied;
141 
142  Size linenumber = 4; // already read first three lines.
143 
144  getline(istr, line); /// clear the EOL from the last >>
145 
146  while ( istr ) {
147  getline(istr, line);
148  if ( ! istr ) break;
149  ++linenumber;
150 
151  if (is_blank(line)) {
152  ++bin[ 1 ];
153  bin[ 2 ] = 0;
154  bin[ 3 ] = 0;
155  continue;
156  }
157 
158  line_stream.clear();
159  line_stream.str(line);
160  line_stream.seekg( std::ios::beg );
161 
162  if ( bin[ 1 ] >= xsize ) {
163  utility_exit_with_message( "Expected active site definition to end at line " + utility::to_string( linenumber ) + ". X dim is out of range" );
164  }
165 
166  if ( bin[ 2 ] >= ysize ) {
167  utility_exit_with_message( "Expected blank line to separate y/z slices at line " + utility::to_string( linenumber ) + ". Y dim is out of range" );
168  }
169 
170  for ( bin[ 3 ] = 0; bin[ 3 ] < zsize; ++bin[ 3 ] ) {
171  line_stream >> occupied;
172  if ( line_stream.bad() ) {
173  std::cerr << "ERROR: liggrid file " << filename << " near line " << linenumber << std::endl;
174  std::cerr << "ERROR: Expected to read " << zsize << " entries, but found only " << bin[ 3 ] << " entries" << std::endl;
175  utility_exit_with_message( "Could not read active-site gridlig definition file" );
176  }
177  if ( occupied ) {
178  grid_->set_value_for_bin( bin, true );
179  }
180  }
181  ++bin[ 2 ];
182  }
183 }
184 
185 
186 /// @brief Set the bounding box for this grid
187 void
189 {
190  bb_ = bb;
191  grid_->set_bounding_box( bb_ );
192 }
193 
194 void
196 {
197  bin_width_ = width;
198  grid_->set_bin_width( bin_width_ );
199 }
200 
201 Bool3DGrid const &
203 {
204  assert( ! reset_grid_bb_ );
205  return *grid_;
206 }
207 
208 
209 /// @brief Is a point in this grid active? False for a point outside the bounding box.
210 bool
212 {
213  return grid_->occupied( p );
214 }
215 
216 /// @brief Reset all the voxels to false
217 void
219 {
220  grid_->clear();
221 }
222 
223 void
225  core::conformation::Residue const & res,
226  Real radius
227 )
228 {
229  if ( res.nheavyatoms() < 1 ) return;
230 
231  Vector lower( res.xyz( 1 )), upper( lower );
232  for ( Size ii = 2; ii <= res.nheavyatoms(); ++ii ) {
233  lower.min( res.xyz( ii ) );
234  upper.max( res.xyz( ii ) );
235  }
236  swell_bounding_box( lower, upper, radius );
237 }
238 
239 void
241  core::conformation::Residue const & res,
242  Real radius
243 )
244 {
245  if ( res.first_sidechain_atom() > res.nheavyatoms() ) return;
246 
247  Vector lower( res.xyz( res.first_sidechain_atom() )), upper( lower );
248  for ( Size ii = res.first_sidechain_atom() + 1; ii <= res.nheavyatoms(); ++ii ) {
249  lower.min( res.xyz( ii ) );
250  upper.max( res.xyz( ii ) );
251  }
252  swell_bounding_box( lower, upper, radius );
253 }
254 
255 void
257  core::conformation::Residue const & res,
258  Real radius
259 )
260 {
261  if ( res.nheavyatoms() < 1 || res.first_sidechain_atom() == 1 ) return;
262 
263  Vector lower( res.xyz( 1 )), upper( lower );
264  for ( Size ii = 2; ii < res.first_sidechain_atom(); ++ii ) {
265  lower.min( res.xyz( ii ) );
266  upper.max( res.xyz( ii ) );
267  }
268  swell_bounding_box( lower, upper, radius );
269 }
270 
271 void
273  core::conformation::Residue const & res,
274  Real radius
275 )
276 {
277  prep_grid();
278  for ( Size ii = 1; ii <= res.nheavyatoms(); ++ii ) {
279  grid_->or_by_sphere_liberal( res.xyz( ii ), radius );
280  }
281 }
282 
283 /// @brief Set all the voxels within a certain radius of the sidechain atoms to true.
284 void
286  core::conformation::Residue const & res,
287  Real radius
288 )
289 {
290  for ( Size ii = res.first_sidechain_atom(); ii <= res.nheavyatoms(); ++ii ) {
291  grid_->or_by_sphere_liberal( res.xyz( ii ), radius );
292  }
293  prep_grid();
294 }
295 
296 /// @brief Set all the voxels within a certain radius of the backbone atoms to true.
297 void
299  core::conformation::Residue const & res,
300  Real radius
301 )
302 {
303  prep_grid();
304  for ( Size ii = 1; ii < res.first_sidechain_atom(); ++ii ) {
305  grid_->or_by_sphere_liberal( res.xyz( ii ), radius );
306  }
307 }
308 
310 {
311  prep_grid();
312 }
313 
314 
315 void
317  Vector lower,
318  Vector upper,
319  Real radius
320 )
321 {
322  lower -= radius;
323  upper += radius;
324 
325  for ( Size ii = 1; ii <= 3; ++ii ) {
326  if ( lower( ii ) < bb_.lower()( ii ) ) {
327  lower.min( bb_.lower() );
328  bb_.set_lower( lower );
329  reset_grid_bb_ = true;
330  }
331  }
332 
333  for ( Size ii = 1; ii <= 3; ++ii ) {
334  if ( upper( ii ) > bb_.upper()( ii ) ) {
335  upper.max( bb_.upper() );
336  bb_.set_upper( upper );
337  reset_grid_bb_ = true;
338  }
339  }
340 }
341 
342 void
344 {
345  if ( ! reset_grid_bb_ ) return;
346 
347  grid_ = new Bool3DGrid;
348  grid_->set_bin_width( bin_width_ );
349 
350  Vector snapped_lower( bb_.lower() );
351  for ( Size ii = 1; ii <= 3; ++ii ) {
352  /// align the lower boundary to an integer
353  snapped_lower( ii ) = static_cast< Real > ( static_cast< int > ( snapped_lower( ii ) ));
354  }
355  bb_.set_lower( snapped_lower );
356 
357  grid_->set_bounding_box( bb_ );
358  reset_grid_bb_ = false;
359 
360 }
361 
362 }
363 }
364 }