Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SequenceProfileConstraint.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 core/scoring/constraints/SequenceProfileConstraint.cc
11 /// @brief This is a constraint that refers to a core::sequence::SequenceProfile? in order to influence the scoring of amino acid types based on multiple sequence alignments (i.e. for biasing amino acid choices during design). A note about the SequenceProfile::read_from_checkpoint function that is used to read in scores for amino acid types: the first line of the file will be ignored.
12 /// @author ashworth
13 
16 
19 #include <core/pose/Pose.hh>
20 
22 
23 #include <basic/Tracer.hh>
24 
25 #include <utility/file/file_sys_util.hh> // file_exists, create_directory
26 
27 #include <basic/options/option.hh>
28 // option key includes
29 #include <basic/options/keys/in.OptionKeys.gen.hh>
30 
31 //Auto Headers
35 #include <utility/vector1.hh>
36 
37 #include <boost/lexical_cast.hpp>
38 
39 
40 namespace core {
41 namespace scoring {
42 namespace constraints {
43 
44 using namespace core;
45 using namespace chemical;
46 using namespace conformation;
47 using namespace basic::options;
48 using namespace scoring;
49 using namespace constraints;
50 using namespace sequence;
51 
52 using basic::t_warning;
53 using basic::t_info;
54 using basic::t_debug;
55 using basic::t_trace;
56 static basic::Tracer TR("core.scoring.constraints.SequenceProfileConstraint");
57 
60  seqpos_(0),
61  sequence_profile_(NULL),
62  mapping_(NULL),
63  weight_( 1.0 )
64 {}
65 
67  Pose const &,
68  Size seqpos,
69  SequenceProfileCOP profile /* = NULL */,
70  core::id::SequenceMappingCOP mapping /* = NULL */ // current pose numbers onto profile numbers.
71 ):
73  seqpos_( seqpos ),
74  sequence_profile_( profile ),
75  mapping_( mapping ),
76  weight_( 1.0 )
77 {}
78 
80  Size seqpos,
81  SequenceProfileCOP sequence_profile /* = NULL */,
82  core::id::SequenceMappingCOP mapping /* = NULL */ // current pose numbers onto profile numbers.
83 ):
85  seqpos_( seqpos ),
86  sequence_profile_( sequence_profile ),
87  mapping_( mapping ),
88  weight_( 1.0 )
89 {}
90 
92 
96 }
97 
98 ///@details one line definition "SequenceProfile resindex profilefilename" (profilefilename can also be set to "none" in the constraints file, and specified by -in::file::pssm)
99 void
101  std::istream & is,
102  Pose const & pose,
103  FuncFactory const &
104 )
105 {
106 
107  int version = 0;
108 // note: is >> "SequenceProfile" has already occured
109  is >> version;
110  if( version == -1 ) {
111  std::string tmp;
112  std::getline( is, tmp );
113  std::stringstream tmpss(tmp);
114  Size residue_index, aa_count;
115  tmpss >> residue_index >> aa_count;
116  utility::vector1<Real> aa_scores;
117  TR(t_debug) << "Loading seqprof constraint for resi " << residue_index << " with aa_count " << aa_count << std::endl;
118  for( Size i = 1; i <= aa_count; i++ ) {
119  Real tmp;
120  tmpss >> tmp;
121  aa_scores.push_back( tmp );
122  }
123  if( ! sequence_profile_ ) {
124  SequenceProfileOP newseqprof( new SequenceProfile );
125  sequence_profile_ = newseqprof;
126  mapping_ = NULL;
127  }
128  seqpos_ = residue_index;
129  const_cast<SequenceProfile * >(sequence_profile_.get())->prof_row( aa_scores, residue_index );
130  } else {
131  Size residue_index(boost::lexical_cast<Size>(version));
132  std::string profile_filename;
133 
134  is >> profile_filename;
135 
136  TR(t_debug) << "reading: " << residue_index << " " << profile_filename << std::endl;
137  if ( residue_index < 1 || residue_index > pose.total_residue() ) {
138  std::cerr << "no such residue index " << residue_index << " in pose!)" << std::endl;
139  utility_exit();
140  }
141 
142  seqpos_ = residue_index;
143 
144  // figure out sequence profile filename
145  using namespace utility::file;
146  // if specified, verify file exists
147  if ( profile_filename != "none" ) {
148  if ( ! file_exists( profile_filename ) ) {
149  utility_exit_with_message( "no such file " + profile_filename );
150  }
151  // if filename not specified, load from commandline option -pssm only if sequence_profile_ is NULL
152  } else if ( !sequence_profile_ && option[ OptionKeys::in::file::pssm ].user() ) {
153  profile_filename = option[ OptionKeys::in::file::pssm ]().front();
154  // ps. don't name your file "none"
155  if ( profile_filename == "none" ) {
156  utility_exit_with_message("\"none\" is not a valid value for -pssm in this context!");
157  }
158  }
159 
160  // if filename is not "none" by this point, read it even if sequence_profile_ is not currently NULL
161  if ( profile_filename != "none" ) {
162  SequenceProfileOP newseqprof( new SequenceProfile );
163  newseqprof->read_from_checkpoint( FileName(profile_filename) );
164  sequence_profile_ = newseqprof;
165  mapping_ = NULL; // Reset mapping
166  }
167 
168  // if sequence_profile_ is still NULL by this point, it is assumed that the user intended so
169  }
170 
171 } // read_def
172 
173 void
174 SequenceProfileConstraint::show_def( std::ostream & os, Pose const & ) const
175 {
176  show( os );
177 }
178 
179 void
180 SequenceProfileConstraint::show( std::ostream & os ) const {
181  if ( sequence_profile_ ) {
182  core::Size profile_pos( seqpos_ );
183  if( mapping_ ) {
184  profile_pos = (*mapping_)[seqpos_];
185  if( profile_pos == 0 ) return; // safety/relevance check
186  }
187  typedef utility::vector1<Real> RealVec;
188  //if( profile_pos == 1 )
189 // os << "SequenceProfile start" << std::endl;
190 
191  if( profile_pos <= sequence_profile_->size() ) {
192  RealVec const & aa_scores( sequence_profile_->prof_row( profile_pos ) );
193  os << "SequenceProfile -1 " << profile_pos << " " << aa_scores.size() << " ";
194  for ( Size aa(1); aa <= aa_scores.size(); ++aa ) {
195  os << aa_scores[aa] << " ";
196  }
197  if( profile_pos != sequence_profile_->size() )
198  os << '\n';
199  }
200  }
201 }
202 
203 void
205 {
206  sequence_profile_ = profile;
207  mapping_ = mapping;
208 }
209 
212 
215 
218  utility::vector1< core::Size > pos_list(1, seqpos_); // length 1 containing "all" seqpos_ values
219  return pos_list;
220 }
221 
224  SequenceMapping const & seqmap
225 ) const {
226  Size newseqpos( seqmap[ seqpos_ ] );
227  if ( newseqpos == 0 ) { return NULL; }
228 
229  TR(t_debug) << "Remapping resid " << seqpos_ << " to " << newseqpos << std::endl;
230 
232  new_map->reverse(); // Go from old->new to new->old
233  if ( mapping_ ) {
234  new_map->downstream_combine( *mapping_ ); // Combine new->old and old->profile into new->profile
235  }
236 
238 }
239 
242  pose::Pose const&,
243  pose::Pose const&,
244  id::SequenceMappingCOP map /*=NULL*/ ) const
245 {
246  if ( ! map ) {
248  }
249 
250  // Hereafter map is valid
251  Size newseqpos( (*map)[ seqpos_ ] );
252 
253  if ( newseqpos == 0 ) { return NULL; }
254 
255  TR(t_debug) << "Remapping resid " << seqpos_ << " to " << newseqpos << std::endl;
256 
258  new_map->reverse(); // Go from old->new to new->old
259  if ( mapping_ ) {
260  new_map->downstream_combine( *mapping_ ); // Combine new->old and old->profile into new->profile
261  }
262 
264 }
265 
266 
267 // Calculates a score for this constraint using XYZ_Func, and puts the UNWEIGHTED score into
268 // emap. Although the current set of weights currently is provided, Constraint objects
269 // should put unweighted scores into emap.
270 void
272  XYZ_Func const & xyz_func,
273  EnergyMap const & weights,
274  EnergyMap & emap
275 ) const
276 {
277  if ( weights[ this->score_type() ] == 0 ) return; // what's the point?
278  runtime_assert( sequence_profile_ );
279 
280  core::Size profile_pos( seqpos_ );
281  if( mapping_ ) {
282  profile_pos = (*mapping_)[seqpos_];
283  if( profile_pos == 0 ) return; // safety/relevance check
284  }
285  chemical::AA aa( xyz_func.residue( seqpos_ ).type().aa() );
286  utility::vector1< utility::vector1< Real > > const & profile( sequence_profile_->profile() );
287  if ( profile_pos > profile.size() ) return; // safety/relevance check
288  utility::vector1< Real > const & position_profile( profile[ profile_pos ] );
289  if ( size_t(aa) > position_profile.size() ) return; // safety/relevance check
290  Real const score( position_profile[aa] );
291  TR(t_trace) << "seqpos " << seqpos_ << " aa " << aa << " " << weight() * score << std::endl;
292 
293  if( sequence_profile_->negative_better() ) {
294  emap[ this->score_type() ] += weight() * score;
295  } else {
296  emap[ this->score_type() ] -= weight() * score;
297  }
298 }
299 
300 void
302  AtomID const & ,//atom,
303  XYZ_Func const & ,//conformation,
304  Vector & ,//f1,
305  Vector & ,//f2,
306  EnergyMap const & //weights
307 ) const
308 {
309  // Do nothing, as the value of this function doesn't change with respect to
310  // the torsions.
311 }
312 
315 
318 {
319  return new SequenceProfileConstraint;
320 }
321 
324 {
325  return "SequenceProfile";
326 }
327 
330  return weight_;
331 }
332 
333 void
335  weight_ = w;
336 }
337 
338 } // namespace constraints
339 } // namespace scoring
340 } // namespace core