Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MultiStatePacker.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 MultiStatePacker.hh
11 /// @brief
12 /// @author ashworth
13 
17 
20 #include <core/pose/Pose.hh>
23 
24 #include <utility/string_util.hh>
25 #include <utility/exit.hh>
26 using utility::string_split;
27 
28 #include <utility/vector0.hh>
29 #include <utility/vector1.hh>
30 using utility::vector1;
31 
32 #include <basic/Tracer.hh>
33 using basic::t_info;
34 using basic::t_debug;
35 using basic::t_trace;
36 static basic::Tracer TR("protocols.multistate_design.MultiStatePacker",t_info);
37 
38 #include <ObjexxFCL/format.hh>
39 using namespace ObjexxFCL::fmt;
40 
41 #include <boost/functional/hash.hpp>
42 
43 #include <cmath> // std::exp
44 #include <vector>
45 #include <iostream>
46 
47 //Auto Headers
50 #include <utility/options/IntegerVectorOption.hh>
51 
52 
53 namespace protocols {
54 namespace multistate_design {
55 
57 
58 using core::Size;
59 using core::Real;
60 
61 PosType::PosType() : parent(), type_(core::chemical::aa_unk) {}
63 PosType::PosType( Size index, core::chemical::AA type ) : parent( index ), type_(type) {}
64 PosType::PosType( std::string word ) : parent( word ), type_( core::chemical::aa_unk )
65 {
66  if ( word.size() != 1 ) {
67  utility_exit_with_message( "PosType std::string constructor failed to read a one-character word: " + word );
68  }
70 }
71 
74  return new PosType( *this );
75 }
76 
78  return new PosType;
79 }
80 
82  return boost::hash_value( type_ );
83 }
84 
85 bool PosType::operator < ( EntityElement const & rhs ) const
86 {
87  if ( parent::operator < ( rhs ) ) {
88  return true;
89  } else if ( parent::operator == ( rhs ) ) {
90  if ( ! dynamic_cast< PosType const * > ( &rhs ) ) {
91  utility_exit_with_message( "operator < unable to compare a " + name() + " object to a " + rhs.name() + " object!" );
92  }
93  PosType const & pt_rhs( static_cast< PosType const & > ( rhs ) );
94  return type_ < pt_rhs.type_;
95  }
96  return false;
97 }
98 
99 bool PosType::operator == ( EntityElement const & rhs ) const
100 {
101  if ( parent::operator == ( rhs ) ) {
102  if ( ! dynamic_cast< PosType const * > ( &rhs ) ) {
103  utility_exit_with_message( "operator < unable to compare a " + name() + " object to a " + rhs.name() + " object!" );
104  }
105  PosType const & pt_rhs( static_cast< PosType const & > ( rhs ) );
106  if ( type_ == pt_rhs.type_ ) {
107  return true;
108  }
109  }
110  return false;
111 }
112 
114 PosType::operator = ( EntityElement const & rhs )
115 {
116  if ( this != &rhs ) {
117  parent::operator = ( rhs );
118 
119  if ( ! dynamic_cast< PosType const * > ( &rhs ) ) {
120  utility_exit_with_message( "operator < unable to compare a " + name() + " object to a " + rhs.name() + " object!" );
121  }
122  PosType const & pt_rhs( static_cast< PosType const & > ( rhs ) );
123 
124  type_ = pt_rhs.type_;
125  }
126  return *this;
127 }
128 
131 {
133 }
134 
137 {
138  PosTypeCreator ptc;
139  return ptc.widget_name();
140 }
141 
143 
144 /// PosTypeCreator
145 
147 
148 std::string PosTypeCreator::widget_name() const { return "AA"; }
149 
152 {
153  return new PosType( word );
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////////////////////////
157 void
158 MultiStatePacker::single_state_design( bool restrict_to_canonical /* = true */ )
159 {
160  for ( SingleStateOPs::iterator ss( states().begin() ), end( states().end() );
161  ss != end; ++ss ) {
162  PackingStateOP state = dynamic_cast< PackingState* >( (*ss)() );
163  runtime_assert( state );
164  utility::vector0< int > rot_to_pack;
165  // this is important if alternate states are represented in the rotamer set (e.g. for DNA)
166  if ( restrict_to_canonical ) restrict_to_canonical_aas( *state, rot_to_pack );
167  state->run_packer( rot_to_pack );
168  ( *scorefxn() )( state->nonconst_pose() );
169  Real const score( state->fitness_function()->evaluate(state->nonconst_pose()) );
170  state->set_best_score( score );
171  TR(t_info) << "Best single-state design score: " << F(8,2,score) << std::endl;
172  }
173 }
174 
175 Real
178  core::Size single_state_num
179 )
180 {
181  PackingStateOP state = dynamic_cast< PackingState* >( states()[single_state_num]() );
182  runtime_assert( state );
183 
184  // Filter down to the rotamers needed for this single sequence
185  utility::vector0<int> rot_to_pack;
186  limit_rotamer_set( rot_to_pack, *state, entity.traits() );
187 
188  // optionally pack multiple times to find best energy
189  Real E(0.), bestE(0.);
190  for ( Size i(0); i < num_packs_; ++i ) {
191 
192  state->run_packer( rot_to_pack );
193 
194  ( *scorefxn() )( state->nonconst_pose() );
195  E = state->fitness_function()->evaluate(state->nonconst_pose());
196  if ( E < bestE || i == 0 ) {
197  bestE = E;
198  if ( dynamic_cast< protocols::multistate_design::MultiStateEntity * >( &entity ) ) {
200  static_cast< protocols::multistate_design::MultiStateEntity & >( entity );
201  multi_state_entity.single_state_entity_data()[single_state_num].fitness(E);
202  for (MetricValueGetterMap::const_iterator iter = metric_value_getters().begin();
203  iter != metric_value_getters().end(); ++iter) {
204  multi_state_entity.single_state_entity_data()[single_state_num].metric_value(
205  iter->first,
206  iter->second.get(state->pose())
207  );
208  }
209  }
210  }
211  }
212  return bestE;
213 }
214 
215 void
217  utility::vector0< int > & rot_to_pack,
218  PackingState const & state,
220 )
221 {
222  rot_to_pack.clear();
223 
224  // Allocate enough to accomodate full rotamer set
225  core::pack::rotamer_set::RotamerSets const & rotsets( *state.rotamersets() );
226  Size const nrotamers( rotsets.nrotamers() );
227 
228  for ( Size rot_i(1); rot_i <= nrotamers; ++rot_i ) {
229 
230  Size const rot_pos( rotsets.res_for_rotamer( rot_i ) );
231  core::chemical::ResidueTypeCOP rot_type( rotsets.rotamer( rot_i )->type() );
232 
235  it( seq.begin() ), end( seq.end() );
236  it != end; ++it ) {
237  if ( ! it->get() ) {
238  utility_exit_with_message( "Null pointer in EntityElement array" );
239  }
240  PosTypeCOP postype( dynamic_cast< PosType const * > ( it->get() ) );
241  if ( ! postype ) {
242  utility_exit_with_message( "Dynamic cast to PosType failed for object of type " + (*it)->name() );
243  }
244  if ( postype->index() == rot_pos ) { seq_type = postype->type(); break; }
245  }
246 
247  if ( seq_type == core::chemical::aa_unk ) {
248  // this is not a position whose mutation is controlled by genetic algorithm,
249  // and should just repack for this state--allow the rotamer if it is not a mutation
250  if ( rot_type->aa() != state.pose().residue_type( rot_pos ).aa() ) continue;
251  rot_to_pack.push_back( rot_i );
252  } else {
253  // this is not a position whose mutation is controlled by genetic algorithm:
254  // accept this rotamer only if its identity matches that which is specified in seq
255  if ( rot_type->aa() == seq_type ) rot_to_pack.push_back( rot_i );
256  }
257  }
258 }
259 
260 void
262  PackingState const & state,
263  utility::vector0< int > & rot_to_pack
264 )
265 {
266  rot_to_pack.clear();
267  core::pack::rotamer_set::RotamerSets const & rotsets( *state.rotamersets() );
268  Size const nrot( rotsets.nrotamers() );
269 
270  for ( Size i(1); i <= nrot; ++i ) {
271  core::conformation::Residue const & rot( *rotsets.rotamer(i) );
272  if ( rot.aa() > core::chemical::num_canonical_aas ) {
273  // if non-canonical rotamer, restrict to existing residue type at this position
274  if ( rot.type().name3() ==
275  state.pose().residue( rotsets.res_for_rotamer(i) ).type().name3() ) {
276  rot_to_pack.push_back(i);
277  }
278  }
279  else rot_to_pack.push_back(i);
280  }
281 }
282 
283 } // namespace multistate_design
284 } // namespace protocols