Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AllowInsert.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/toolbox/AllowInsert.cc
11 /// @brief
12 /// @detailed
13 /// @author Rhiju Das
14 
15 // Unit Headers
17 
18 // Package Headers
19 #include <core/pose/Pose.hh>
20 #include <core/pose/util.hh>
24 // AUTO-REMOVED #include <core/chemical/VariantType.hh>
26 #include <core/types.hh>
27 #include <core/id/AtomID.hh>
28 #include <core/id/NamedAtomID.hh>
29 
30 #include <utility/exit.hh>
31 
32 #include <ObjexxFCL/format.hh>
33 #include <ObjexxFCL/string.functions.hh>
34 using ObjexxFCL::fmt::I;
35 
36 // C++ headers
37 #include <map>
38 
39 #include <utility/vector1.hh>
40 
41 
42 using namespace core;
43 using core::id::AtomID;
45 
46 namespace protocols{
47 namespace toolbox{
48 
49  Size const FIXED_DOMAIN( 999 );
50 
51  AllowInsert::AllowInsert( core::pose::Pose const & pose ):
52  nres_( pose.total_residue() ),
53  force_ideal_chainbreak_( false )
54  {
55  initialize( pose );
56  }
57 
58  //////////////////////////////////////////////////////////////////
60 
61  //////////////////////////////////////////////////////////////////
64  {
65  AllowInsertOP new_allow_insert( new AllowInsert( *this ) );
66  return new_allow_insert;
67  }
68 
69 ///////////////////////////////////////////////////////////////////////////////
70  AllowInsert &
72  {
73  if ( this == &src ) return *this;
74 
78 
80 
82 
83  nres_ = src.nres_;
84 
86 
87  return *this;
88  }
89 
90 ///////////////////////////////////////////////////////////////////////////////
92  ReferenceCount()
93  {
94  *this = src;
95  }
96 
97 
98 
99  //////////////////////////////////////////////////////////////////
100  void
102  {
103  allow_insert_.clear();
104  for (Size i = 1; i <= pose.total_residue(); i++ ) {
105 
107 
108  for ( Size j = 1; j <= pose.residue_type( i ).natoms(); j++ ) {
109 
110  AtomID const atom_id( j, i );
111 
112  allow_insert_[ atom_id ] = 0;
113 
114  // Needed in case of changes in atom names/indices
115  // The main allow_insert map is keyed on number but not names for speed.
116  NamedAtomID const named_atom_id( pose.residue_type(i).atom_name( j ), i );
117  named_atom_id_map_[ named_atom_id ] = atom_id;
118 
119  map_to_original_[ atom_id ] = atom_id;
120 
121  atom_ids.push_back( atom_id );
122 
123  }
124 
125  atom_ids_in_res_.push_back( atom_ids );
126 
127  }
128 
130  }
131 
132  //////////////////////////////////////////////////////////////////
133  bool
134  AllowInsert::get( Size const & i ) const{
135  return ( get_domain( i ) == 0 );
136  }
137 
138  //////////////////////////////////////////////////////////////////
139  bool
140  AllowInsert::get( core::id::AtomID const & atom_id ) const{
141  return ( get_domain( atom_id ) == 0 );
142  }
143 
144  //////////////////////////////////////////////////////////////////
145  bool
146  AllowInsert::get( core::id::TorsionID const & torsion_id, core::conformation::Conformation const & conformation ) const{
147 
148  // Check allow insert -- get atoms associated with these torsions.
149  // are any allowed to move?
150  id::AtomID id1,id2,id3,id4;
151 
152  bool const fail = conformation.get_torsion_angle_atom_ids( torsion_id, id1, id2, id3, id4 );
153  if (fail) return false;
154 
155  if ( !get( id1 ) && !get( id4 ) && ( get_domain( id1 ) == get_domain( id4 ) ) ) return false;
156 
157  return true;
158 
159  }
160 
161  //////////////////////////////////////////////////////////////////
162  Size
163  AllowInsert::get_domain( Size const & i ) const{
164  if ( i > nres_ ) {
165  utility_exit_with_message( "Out of bounds for allow_insert" );
166  }
167  Size domain( 0 );
168  for ( Size j = 1; j <= atom_ids_in_res_[ i ].size(); j++ ) {
169  domain = get_domain( atom_ids_in_res_[ i ][ j ] );
170  if ( domain == 0 ) return 0;
171  }
172  return domain;
173  }
174 
175  //////////////////////////////////////////////////////////////////
176  bool
177  AllowInsert::has_domain( core::id::AtomID const & atom_id ) const{
178  std::map< AtomID, AtomID >::const_iterator it_original = map_to_original_.find( atom_id );
179  return !( it_original == map_to_original_.end() );
180  }
181 
182  //////////////////////////////////////////////////////////////////
183  Size
184  AllowInsert::get_domain( core::id::AtomID const & atom_id ) const{
185 
186  std::map< AtomID, AtomID >::const_iterator it_original = map_to_original_.find( atom_id );
187  if ( it_original == map_to_original_.end() ) return FIXED_DOMAIN;
188  AtomID original_atom_id = it_original->second;
189 
190  std::map< core::id::AtomID, Size >::const_iterator it = allow_insert_.find( original_atom_id );
191  if ( it == allow_insert_.end() ){
192  utility_exit_with_message( "Asked allow_insert for an atom_id it does not know about!" );
193  }
194  return it->second;
195  }
196 
197  //////////////////////////////////////////////////////////////////
198  void
199  AllowInsert::set_domain( Size const & i, Size const & setting ){
200  for ( Size j = 1; j <= atom_ids_in_res_[ i ].size(); j++ ) {
201  set_domain( atom_ids_in_res_[ i ][ j ], setting );
202  }
203  }
204 
205  //////////////////////////////////////////////////////////////////
206  void
207  AllowInsert::set_domain( core::id::AtomID const & atom_id, Size const & setting ){
208  std::map< AtomID, AtomID >::const_iterator it_original = map_to_original_.find( atom_id );
209  if ( it_original == map_to_original_.end() ) {
210  std::cerr << "Problem ID: " << atom_id << std::endl;
211  utility_exit_with_message( "Asked allow_insert to set atom_id that cannot be mapped to original pose!" );
212  }
213 
214  AtomID original_atom_id = it_original->second;
215 
216  if ( allow_insert_.find( original_atom_id ) == allow_insert_.end() ){
217  utility_exit_with_message( "Asked allow_insert to set atom_id it does not know about!" );
218  }
219  allow_insert_[ original_atom_id ] = setting;
220  }
221 
222 
223  //////////////////////////////////////////////////////////////////
224  void
225  AllowInsert::set_domain( Size const & setting ){
226  for( std::map< AtomID, Size >::iterator it = allow_insert_.begin();
227  it != allow_insert_.end(); it++ ){
228  it->second = setting;
229  }
230  }
231 
232  //////////////////////////////////////////////////////////////////
233  void
235  pose::Pose const & pose,
236  Size const & setting ){
237 
238  if ( pose.residue(i).is_coarse() ){
239  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( " P ", i ), pose ) ), setting );
240  } else {
241  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( " P ", i ), pose ) ), setting );
242  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( " O1P", i ), pose ) ), setting );
243  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( " O2P", i ), pose ) ), setting );
244  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( " O5*", i ), pose ) ), setting );
245  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( "1H5*", i ), pose ) ), setting );
246  set_domain( AtomID( named_atom_id_to_atom_id( NamedAtomID( "2H5*", i ), pose ) ), setting );
247  }
248  }
249 
250 
251  //////////////////////////////////////////////////////////////////
252  void
253  AllowInsert::set( bool const & setting ){
254  set_domain( ( setting ) ? 0 : FIXED_DOMAIN );
255  }
256 
257  //////////////////////////////////////////////////////////////////
258  void
259  AllowInsert::set( Size const & i, bool const & setting ){
260  set_domain( i, ( setting ) ? 0 : FIXED_DOMAIN );
261  }
262 
263  //////////////////////////////////////////////////////////////////
264  void
265  AllowInsert::set( AtomID const & atom_id, bool const & setting ){
266  set_domain( atom_id, ( setting ) ? 0 : FIXED_DOMAIN );
267  }
268 
269 
270  //////////////////////////////////////////////////////////////////
271  void
273  pose::Pose const & pose,
274  bool const & setting ){
275 
276  set_phosphate_domain( i, pose, ( ( setting ) ? 0 : FIXED_DOMAIN ) );
277  }
278 
279  //////////////////////////////////////////////////////////////////
280  void
282  for( Size i = 1; i <= nres_; i++ ) {
283  std::cout << "RES" << i;
284  for ( Size j = 1; j <= atom_ids_in_res_[ i ].size(); j++ ) {
285  std::cout << ' ' << I( 3, allow_insert_[ atom_ids_in_res_[ i ][ j ] ] );
286  }
287  std::cout << std::endl;
288  }
289  }
290 
291  //////////////////////////////////////////////////////////////////
292  void
294 
295  for( std::map< AtomID, Size >::iterator it = allow_insert_.begin();
296  it != allow_insert_.end(); it++ ){
297 
298  Size const & current_setting = it->second;
299  Size const & other_setting = allow_insert_in->get_domain( it->first );
300 
301  if ( other_setting > current_setting ) it->second = other_setting;
302 
303  }
304 
305  }
306 
307  //////////////////////////////////////////////////////////////////
308  std::map< AtomID, Size > const &
311  }
312 
313  //////////////////////////////////////////////////////////////////
314  void
316  std::map< core::Size, core::Size > const & res_map,
317  core::kinematics::FoldTree const & scratch_fold_tree,
318  std::map< AtomID, AtomID > & atom_id_map ){
319 
320  atom_id_map.clear();
321 
322  std::map< core::Size, core::Size > in_source_res; //basically reverse of res_map.
323  for( std::map< Size, Size >::const_iterator it = res_map.begin(); it != res_map.end(); it++ ){
324  Size const & insert_pos = it->first;
325  Size const & source_pos = it->second;
326  in_source_res[ source_pos ] = insert_pos;
327  }
328 
329  for( std::map< Size, Size >::const_iterator it = res_map.begin(); it != res_map.end(); it++ ){
330 
331  Size const & insert_pos = it->first;
332  Size const & source_pos = it->second;
333 
334  for ( Size j = 1; j <= pose.residue_type( insert_pos ).natoms(); j++ ){
335 
336  AtomID const atom_id( j, insert_pos );
337 
338  std::map< AtomID, AtomID >::const_iterator it_original = map_to_original_.find( atom_id );
339  if ( it_original == map_to_original_.end() ) continue;
340  // {
341  // utility_exit_with_message( "Asked allow_insert to set atom_id that cannot be mapped to original pose!" );
342  // }
343 
344  AtomID original_atom_id = it_original->second;
345 
346  int const rsd_offset = int( original_atom_id.rsd() ) - int( atom_id.rsd() ); //this is nonzero for OVL1, OVU1, etc. (chainbreak atoms)
347  Size const source_atomno = original_atom_id.atomno();
348 
349  Size const source_pos_offset = source_pos + rsd_offset;
350 
351  if ( in_source_res.find( source_pos_offset ) == in_source_res.end() ) continue;
352 
353  if ( rsd_offset == +1 && scratch_fold_tree.is_cutpoint( source_pos ) ) continue;
354  if ( rsd_offset == -1 && scratch_fold_tree.is_cutpoint( source_pos-1 ) ) continue;
355 
356  // PUTTING IN MATT'S CRAZY OPTION FOR HOMOLOGY MODELING -- make this an option though!
357  //if ( true ){
358  // if ( rsd_offset != 0) continue;
359  // }
360 
361  // if ( source_pos + rsd_offset == 0 ) {
362  // std::cout << pose.annotated_sequence( true ) << std::endl;
363  // std::cout << "FAIL on Atom " << atom_id << "; ''original'' atom: " << original_atom_id << std::endl;
364  // utility_exit_with_message( "mapping atom_id to rsd 0?" );
365  // }
366 
367  AtomID source_atom_id( source_atomno, source_pos + rsd_offset );
368 
369  atom_id_map[ atom_id ] = source_atom_id;
370 
371  // change this to an assert later?
372  // if ( AtomID( named_atom_id_map_[ atom_id ], pose ) != atom_id ) utility_exit_with_message( "allow insert atom_id mismatch" );
373 
374  }
375 
376  }
377 
378 
379  }
380 
381 
382  //////////////////////////////////////////////////////////////////
383  void
385 
386 
387  if ( pose.total_residue() != nres_ ) {
388  utility_exit_with_message( "AllowInsert cannot currenty handle changes in no. residues!" );
389  }
390 
391  map_to_original_.clear();
392 
393  for( Size i = 1; i <= nres_; i++ ) {
394 
395  for ( Size j = 1; j <= pose.residue_type( i ).natoms(); j++ ) {
396 
397  AtomID const new_atom_id( j, i );
398 
399  std::string const & atom_name = pose.residue_type(i).atom_name( j );
400  NamedAtomID const new_named_atom_id( atom_name, i );
401 
402  std::map< NamedAtomID, AtomID >::const_iterator it = named_atom_id_map_.find( new_named_atom_id );
403 
404  if ( it != named_atom_id_map_.end() ) { //awesome, this atom is recognizable by its name.
405 
406  AtomID const & original_atom_id = it->second;
407  map_to_original_[ new_atom_id ] = original_atom_id;
408 
409  } else { // there are some special cases....
410 
411  NamedAtomID alternative_named_atom_id;
412 
413  // if we prevent mapping of chainbreak virtual atoms OVL1, OVL2, OVU1 to their
414  // corresponding real atoms, they won't move during fragment insertions.
415  if ( force_ideal_chainbreak_ ) continue;
416 
417  //note that this is hardcoded, but it is also hardcoded in Conformation.cc so I don't feel so bad.
418  // later generalize based on mainchain[ ... ], so can handle any polymer with cutpoint variants.
419  // later can add H1, H for protein terminus variants.
420  core::chemical::ResidueType const & rsd_type = pose.residue_type( i );
421  if ( rsd_type.is_RNA() ) {
422  if ( rsd_type.is_coarse() ){
423  if ( atom_name == "OVL1" ){
424  alternative_named_atom_id = NamedAtomID( " P ", i+1 );
425  } else if ( atom_name == "OVL2" ) {
426  alternative_named_atom_id = NamedAtomID( " S ", i+1 );
427  } else if ( atom_name == "OVU1" ) {
428  alternative_named_atom_id = NamedAtomID( " S ", i-1 );
429  } else {
430  continue;
431  }
432  } else {
433  if ( atom_name == "OVL1" ){
434  alternative_named_atom_id = NamedAtomID( " P ", i+1 );
435  } else if ( atom_name == "OVL2" ) {
436  alternative_named_atom_id = NamedAtomID( " O5*", i+1 );
437  } else if ( atom_name == "OVU1" ) {
438  alternative_named_atom_id = NamedAtomID( " O3*", i-1 );
439  } else {
440  continue;
441  }
442  }
443  }
444 
445  std::map< NamedAtomID, AtomID >::const_iterator it2 = named_atom_id_map_.find( alternative_named_atom_id );
446 
447  if ( it2 != named_atom_id_map_.end() ) { //awesome, this atom is recognizable by its name.
448  AtomID const & original_atom_id = it2->second;
449  map_to_original_[ new_atom_id ] = original_atom_id;
450  }
451 
452  }
453  }
454  }
455 
456  // std::cout << "NEW ALLOW INSERT FOR CHANGED POSE " << std::endl;
457  // for( Size i = 1; i <= nres_; i++ ) {
458  // std::cout << "RES " << i << " ";
459  // for ( Size j = 1; j <= pose.residue_type( i ).natoms(); j++ ) {
460  // if ( get_domain( AtomID(j,i) ) == 0 ) {
461  // std::cout << ' ' << pose.residue_type( i ).atom_name( j ); //I(3,get_domain( AtomID( j,i ) ) );
462  // }
463  // }
464  // std::cout << std::endl;
465  // }
466 
468 
469 
470  }
471 
472  //////////////////////////////////////////////////////////////////////////
473 
474  std::map< AtomID, Size > const &
476 
478 
479  for( Size i = 1; i <= pose.total_residue(); i++ ) {
480 
481  for ( Size j = 1; j <= pose.residue_type( i ).natoms(); j++ ) {
482 
483  AtomID const atom_id( j, i );
484 
485  calculated_atom_id_domain_map_[ atom_id ] = get_domain( atom_id );
486 
487  }
488 
489  }
490 
492  }
493 
494  //////////////////////////////////////////////////////////////////
495  // Used in appending virtual residue to pose.
496  void
498  Size const & i,
499  bool const & setting ){
500 
501  Size const domain = setting ? 0 : FIXED_DOMAIN;
502 
504  for ( Size j = 1; j <= pose.residue( i ).natoms(); j++ ) {
505  AtomID const atom_id( j, i );
506  allow_insert_[ atom_id ] = domain;
507  named_atom_id_map_[ atom_id_to_named_atom_id( atom_id, pose ) ] = atom_id;
508  atom_ids.push_back( atom_id );
509  map_to_original_[ atom_id ] = atom_id;
510  }
511  atom_ids_in_res_.push_back( atom_ids );
512  nres_++;
513 
514  }
515 
516 
517 }
518 }
519 
520 
521