Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DistanceScoreMover.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 FragmentSampler.cc
11 /// @brief ab-initio fragment assembly protocol for proteins
12 /// @detailed
13 /// Contains currently: Classic Abinitio
14 ///
15 ///
16 /// @author Oliver Lange
17 
18 // Unit Headers
20 
21 // Package Headers
24 #include <core/id/Exceptions.hh>
25 // Project Headers
27 // AUTO-REMOVED #include <core/scoring/constraints/AmbiguousNMRConstraint.hh>
28 // Utility headers
29 #include <basic/Tracer.hh>
30 #include <basic/prof.hh>
31 
33 #include <utility/vector1.hh>
34 #include <cmath>
35 
36 
37 //// C++ headers
38 
39 
40 static basic::Tracer tr("protocols.noesy_assign.DistanceScoreMover");
41 
42 using core::Real;
43 using namespace core;
44 using namespace basic;
45 
46 namespace protocols {
47 namespace noesy_assign {
48 
49 DistanceScoreMover::DistanceScoreMover( CrossPeakList& cpl, pose::Pose const& pose, core::Real dcut ) :
50  cross_peaks_( cpl ),
51  count_decoys_( 0 ),
52  nr_assignments_( cpl.count_assignments() ),
53  peak_violation_counts_( cross_peaks_.size(), 0 ),
54  final_dist_power_( 6.0 ), //default
55  dcut_( dcut )
56 {
57  basic::ProfileThis doit( basic::NOESY_ASSIGN_DIST_INIT );
58  constraints_.reserve( nr_assignments_ );
59  // peak_constraints_.reserve( cross_peaks_.size() );
60 
61  for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it ) {
62  for ( CrossPeak::iterator ait = (*it)->begin(); ait != (*it)->end(); ++ait ) {
63  try {
64  constraints_.push_back( (*ait)->create_constraint( pose ) );
65  } catch ( core::id::EXCN_AtomNotFound& excn ) {
66  tr.Error << "while setting up constraints in DistanceScoreMover: " << excn << std::endl;
67  constraints_.push_back( NULL );
68  }
69  }
70  try {
71  // peak_constraints_.push_back( (*it)->create_constraint( cpl.resonances(), pose ) );
72  } catch ( core::id::EXCN_AtomNotFound& excn ) {
73  tr.Error << "while setting up constraints in DistanceScoreMover: " << excn << std::endl;
74  // peak_constraints_.push_back( NULL );
75  }
76  }
77 }
78 
79 
80 void DistanceScoreMover::prepare_scoring( bool use_for_calibration /*default false */ ) {
81  basic::ProfileThis doit( basic::NOESY_ASSIGN_DIST_PREP_SCORE );
82  //assignment_distances_ = VectorReal( nr_assignments_, 0.0 );
83  used_for_calibration_ = use_for_calibration;
84 
85  Size ct( 1 );
86  for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it, ++ct ) {
87  if ( !used_for_calibration_ ) {
88  for ( CrossPeak::iterator ait = (*it)->begin(); ait != (*it)->end(); ++ait ) {
89  (*ait)->set_decoy_compatibility( 0.0 );
90  }
91  }
92  peak_violation_counts_[ ct ] = 0;
94  }
95  count_decoys_ = 0;
97 }
98 
99 
100 // void DistanceScoreMover::find_violators_with_individual_dist_cutoff( PoseVector poses ) {
101 // basic::ProfileThis doit( basic::NOESY_ASSIGN_DIST_APPLY );
102 // PeakAssignmentParameters const& params( *PeakAssignmentParameters::get_instance() );
103 
104 // SingleConstraints::const_iterator constraint_it( constraints_.begin() );
105 // active_peaks_ = 0;
106 
107 // Size ct_peaks( 1 );
108 // typedef utility::vector1< Real > RealVector;
109 // RealVector distance_deltas( poses.size(), 0.0 );
110 
111 // for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it, ++ct_peaks ) {
112 // //for each CrossPeak do the following:
113 // // ct_peaks .. number of peak, ct ... number of assignment / peak
114 // // a) iterate over all initial assignments and get effective distance (i.e., QD1 is evaluated with d^-6 averaging )
115 // // b1) average over all assignments (d^-6) (--> sum_dist )
116 // // b2) average over all selected assignments ( calibration phase --> sum_dist_filt )
117 // // c) cache distance in dist_buf to compute Dk in the end..
118 // if ( (*it)->n_assigned() == 0 ) continue;
119 
120 // Size pose_ct( 1 );
121 // SingleConstraints::const_iterator pre_pose_cst_it = constraint_it;
122 // for ( PoseVector::const_iterator pose_it = poses.begin(); pose_it != poses.end(); ++pose_it, ++pose_ct ) {
123 // Real sum_dist( 0.0 ); //accumulate only distances where Vk > Vmin --> used for calibration ( eq. (12) ).
124 // constraint_it = pre_pose_cst_it;
125 // for ( CrossPeak::iterator ait = (*it)->begin(); ait != (*it)->end(); ++ait ) {
126 // runtime_assert( constraint_it != constraints_.end() );
127 // Real dist,invd6;
128 // if ( !( *constraint_it )) {
129 // dist=0;
130 // invd6=0;
131 // } else {
132 // {
133 // dist = (*constraint_it)->dist( **pose_it );
134 // }
135 // Real invd = 1.0/dist;
136 // Real invd3 = invd*invd*invd;
137 // invd6 = invd3*invd3;
138 // }
139 // sum_dist += ( (*ait)->normalized_peak_volume() > params.min_volume_ )*invd6;
140 // ++constraint_it; //used this constraint.... we have created these constraints in constructor in same sequence as used here
141 // }
142 // total_assigned_distances_ += sum_dist;
143 // if ( sum_dist > 0 ) {
144 // sum_dist=pow( sum_dist, -1.0/6.0 );
145 // ++active_peaks_;
146 // }
147 // distance_deltas[ pose_ct ] = sum_dist - (*it)->distance_bound();
148 // } // for all poses
149 
150 // //now figure out distribution of deltas and make call if this means we violated
151 
152 // //first sort to get a 90% distribution length --i.e., ignore 5% on each side and take distance between those
153 // std::sort( distance_deltas.begin(), distance_deltas.end() );
154 // Size const ind_low_5( 1+lrint( params.local_distviol_range_*distance_deltas.size() ) ); //lower 5% -
155 // Size const ind_high_5( lrint( 1.0*distance_deltas.size()*(1-params.local_distviol_range_) ) ); //upper 5%
156 // Real max_extension( distance_deltas[ ind_high_5 ] - distance_deltas[ ind_low_5 ] );
157 // tr.Debug << "ind_low_5 " << ind_low_5 << " ind_high_5 " << ind_high_5 << " min_delta: " << distance_deltas[ 1 ] << " max_delta "
158 // << distance_deltas.back() << std::endl;
159 // Size viol_count( 0 );
160 // for ( RealVector::const_iterator delta_it = distance_deltas.begin(); delta_it != distance_deltas.end(); ++delta_it ) {
161 // viol_count += ( *delta_it > ( max_extension + params.local_distviol_global_buffer_ ) ) ? 1 : 0;
162 // }
163 // tr.Debug << "peak: " << (*it)->peak_id() <<" " << (*it)->filename() << " max_extension " << max_extension << " viol_count " << viol_count << std::endl;
164 // (*it)->set_eliminated_due_to_dist_violations( viol_count > (params.nr_conformers_violatable_*distance_deltas.size() ) );
165 // }
166 // }
167 
169  basic::ProfileThis doit( basic::NOESY_ASSIGN_DIST_APPLY );
171 
172  count_decoys_++;
173  SingleConstraints::const_iterator constraint_it( constraints_.begin() );
174  // PeakConstraints::const_iterator peak_constraint_it( peak_constraints_.begin() );
175  active_peaks_ = 0;
176  Size ct_peaks( 1 );
177  if ( !used_for_calibration_ ) {
178  tr.Debug << "DistanceScoreMover is not used in calibration mode " << std::endl;
179  }
180  for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it, ++ct_peaks ) {
181  //for each CrossPeak do the following:
182  // ct_peaks .. number of peak, ct ... number of assignment / peak
183  // a) iterate over all initial assignments and get effective distance (i.e., QD1 is evaluated with d^-6 averaging )
184  // b1) average over all assignments (d^-6) (--> sum_dist )
185  // b2) average over all selected assignments ( calibration phase --> sum_dist_filt )
186  // c) cache distance in dist_buf to compute Dk in the end..
187 
188  Real dist_buf[ 2000 ];//some cheap memory buffer for distances
189  runtime_assert( (*it)->n_assigned() < 2000 );
190  if ( (*it)->n_assigned() == 0 ) continue;
191  Size ct_assignments( 1 );
192  Real sum_dist( 0.0 );
193  Real sum_dist_filt( 0.0 ); //accumulate only distances where Vk > Vmin --> used for calibration ( eq. (12) ).
194  for ( CrossPeak::iterator ait = (*it)->begin(); ait != (*it)->end(); ++ait, ++ct_assignments ) {
195  runtime_assert( constraint_it != constraints_.end() );
196  Real dist,invd6;
197  if ( !( *constraint_it )) {
198  dist=0;
199  invd6=0;
200  //tr.Trace << "PeakAssignment " << (*it)->peak_id() << " " << " assignment: " << ct_assignments << " "
201  // << (*ait)->resonance_id( 1 ) << " " << (*ait)->resonance_id( 2 )
202  // << " has invalid constraint assign 0" << std::endl;
203  } else {
204  // { basic::ProfileThis doit( basic::NOESY_ASSIGN_DIST_CST_EVAL );
205  dist = (*constraint_it)->dist( pose );
206  // }
207  // tr.Trace << " dist: " << dist << std::endl;
208  Real invd = 1.0/dist;
209  Real invd3 = invd*invd*invd;
210  invd6 = invd3*invd3;
211  // tr.Trace << "invd6: " << invd6 << std::endl;
212  }
213  sum_dist+=invd6;
214  tr.Trace << "sum_dist " << sum_dist << " dist " << dist << std::endl;
215  ///this resembles eq( 12 ) after filtering by peak volume --> used for calibration...
216  //careful: these values are messed up when !used_for_calibration since peak_volume() can return 0 or similar ....
217  sum_dist_filt += ( (*ait)->normalized_peak_volume() > params.min_volume_ )*invd6;
218  dist_buf[ ct_assignments ] = dist;
219  ++constraint_it; //used this constraint.... we have created these constraints in prepare_scoring() in same sequence as used here
220  }
221  /// is upper bound violated ?
222  if ( used_for_calibration_ ) {
223  sum_dist=sum_dist_filt;
224  }
225  total_assigned_distances_ += sum_dist;
226  // tr.Trace << "sum_dist " << sum_dist << std::endl;
227  if ( sum_dist > 0 ) {
228  sum_dist=pow( sum_dist, -1.0/6.0 );
229  ++active_peaks_;
230  }
231  // tr.Trace << "sum_dist " << sum_dist << std::endl;
232  bool violated( dcut_ > 0 && ( sum_dist - (*it)->distance_bound() ) > dcut_ );
233  peak_violation_counts_[ ct_peaks ] += violated ? 1 : 0;
234  total_violation_count_ += violated ? 1 : 0;
235 
236  if ( !used_for_calibration_ ) { ////NOTE THERE IS STILL A PROBLEM DUE TO SKIPPED CONSTRAINTS...
237  basic::ProfileThis doit( basic::NOESY_ASSIGN_DIST_SET_COMPABILITY_SCORE );
238  //now add to Dk in Assignments --- formulas (6)+(7) on p.214 eta --> final_dist_power
239  //d_ak,bk is distance of individual PeakAssignment (computed in for-loop above --> dist_buf[] ).
240  ct_assignments = 1;
241  for ( CrossPeak::iterator ait = (*it)->begin(); ait != (*it)->end(); ++ait, ++ct_assignments ) {
242  // if ( dist_buf[ ct_assignments ] == 0 ) {
243  // tr.Trace << "Crosspeak: " << (*it)->peak_id() << " assignment " << ct_assignments << " has zero dist_buf " << std::endl;
244  // }
245  if ( dist_buf[ ct_assignments ] ) {
246  // tr.Trace << " dist_buf " << dist_buf[ ct_assignments ] << " over " << sum_dist << " is " << dist_buf[ ct_assignments ]/sum_dist << std::endl;
247  // runtime_assert( (*ait)->decoy_compatibility() < 0.01 ); -- ah it is not 0 because we go through the different decoys and sum up!
248  (*ait)->set_decoy_compatibility( (*ait)->decoy_compatibility() + pow( dist_buf[ ct_assignments ]/sum_dist, -final_dist_power_) );
249  }
250  if ( (*ait)->decoy_compatibility() == 0 ) {
251  // tr.Trace <<" oh shit no compatiblity... " << (*it)->peak_id() << std::endl;
252  // tr.Trace << ct_assignments << " " << dist_buf[ct_assignments] << " " << sum_dist << " " << dist_buf[ ct_assignments ]/sum_dist << " " << pow( dist_buf[ ct_assignments ]/sum_dist, -final_dist_power_) << std::endl;
253  }
254  } ///... divide by M (aka count_decoys_ ) in finalize_scoring
255  }
256  } //loop over peaks
257 }
258 
260  Size ct_peaks( 1 );
261  for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it, ++ct_peaks ) {
262  for ( CrossPeak::iterator ait = (*it)->begin(); ait != (*it)->end(); ++ait ) {
263  (*ait)->set_decoy_compatibility( (*ait)->decoy_compatibility()/count_decoys_ );
264  }
265  }
266 }
267 
268 // void DistanceScoreMover::eliminate_violated_constraints() const {
269 // PeakAssignmentParameters const& params( *PeakAssignmentParameters::get_instance() );
270 // Size ct_peaks( 1 );
271 // for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it, ++ct_peaks ) {
272 // tr.Debug << "peak: " << (*it)->peak_id() <<" " << (*it)->filename() << " violations: " << peak_violation_counts_[ ct_peaks ] << std::endl;
273 // (*it)->set_eliminated_due_to_dist_violations( peak_violation_counts_[ ct_peaks ] > (params.nr_conformers_violatable_*count_decoys_) );
274 // }
275 // }
276 
277 // core::Real DistanceScoreMover::compute_violation_percentage() const {
278 // Real percent_violated( 0 );
279 // Size ct_peaks( 1 );
280 // for ( CrossPeakList::iterator it = cross_peaks_.begin(); it != cross_peaks_.end(); ++it, ++ct_peaks ) {
281 // percent_violated+=peak_violation_counts_[ ct_peaks ];
282 // }
283 // return percent_violated / active_peaks_ / count_decoys_;
284 // }
285 
286 
287 
288 
289 
290 }
291 }