Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
denovo_density.cc
Go to the documentation of this file.
1 #include <devel/init.hh>
2 
3 #include <core/pose/Pose.hh>
4 #include <core/pose/PDBInfo.hh>
5 #include <core/pose/Pose.fwd.hh>
6 #include <core/pose/annotated_sequence.hh> //make_pose_from_sequence
7 #include <core/pose/util.hh>
8 
9 #include <core/import_pose/import_pose.hh>
10 
11 #include <core/conformation/Residue.hh>
12 #include <core/conformation/util.hh>
13 
14 #include <core/chemical/ChemicalManager.hh>
15 #include <core/chemical/ResidueTypeSet.hh>
16 
17 #include <core/util/SwitchResidueTypeSet.hh>
18 
19 #include <core/sequence/util.hh>
20 #include <core/sequence/Sequence.hh>
21 
22 #include <core/conformation/ResidueFactory.hh>
23 
24 // minimize pose into density
25 #include <protocols/electron_density/util.hh>
26 #include <protocols/electron_density/SetupForDensityScoringMover.hh>
27 #include <protocols/electron_density/DockIntoDensityMover.hh>
28 #include <protocols/simple_moves/MinMover.hh>
29 #include <protocols/rigid/RB_geometry.hh>
30 
31 #include <protocols/idealize/IdealizeMover.hh>
32 #include <protocols/idealize/IdealizeMover.fwd.hh>
33 
34 #include <protocols/simple_moves/PackRotamersMover.hh>
35 #include <core/pack/task/operation/TaskOperations.hh>
36 #include <core/pack/task/operation/OptH.hh>
37 #include <core/pack/task/PackerTask.hh>
38 #include <core/pack/task/TaskFactory.hh>
39 
40 #include <core/kinematics/MoveMap.hh>
41 #include <core/kinematics/FoldTree.hh>
42 #include <core/kinematics/Jump.hh>
43 #include <core/optimization/AtomTreeMinimizer.hh>
44 #include <core/optimization/CartesianMinimizer.hh>
45 #include <core/optimization/symmetry/SymAtomTreeMinimizer.hh>
46 #include <core/optimization/MinimizerOptions.hh>
47 
48 
49 #include <core/scoring/electron_density/ElectronDensity.hh>
50 #include <core/scoring/rms_util.hh>
51 #include <core/scoring/rms_util.tmpl.hh>
52 
53 #include <core/scoring/ScoreFunction.hh>
54 #include <core/scoring/ScoreFunctionFactory.hh>
55 
56 #include <core/fragment/util.hh>
57 #include <core/fragment/FragmentIO.hh>
58 #include <core/fragment/FragSet.hh>
59 #include <core/fragment/Frame.hh>
60 #include <core/fragment/FrameIterator.hh>
61 #include <core/fragment/FragData.hh>
62 #include <core/fragment/ConstantLengthFragSet.hh>
63 #include <core/fragment/BBTorsionSRFD.hh>
64 
65 #include <core/io/silent/BinarySilentStruct.hh>
66 #include <core/io/silent/SilentFileData.hh>
67 
68 #include <basic/options/option.hh>
71 #include <basic/options/keys/in.OptionKeys.gen.hh>
72 #include <basic/options/keys/out.OptionKeys.gen.hh>
73 #include <basic/options/keys/edensity.OptionKeys.gen.hh>
74 
75 #include <ObjexxFCL/format.hh>
76 #include <numeric/xyzMatrix.hh>
77 #include <numeric/xyzVector.hh>
78 #include <numeric/xyzVector.io.hh>
79 #include <numeric/random/random.hh>
80 #include <utility/io/izstream.hh>
81 #include <utility/io/ozstream.hh>
82 #include <utility/string_util.hh>
83 
84 #include <boost/tuple/tuple.hpp>
85 #include <boost/tuple/tuple_comparison.hpp>
86 
87 #include <basic/Tracer.hh>
88 
89 #include <iostream>
90 #include <string>
91 #include <list>
92 #include <algorithm>
93 
94 #include <utility/file/FileName.hh>
96 
98 OPT_KEY( String, ca_positions )
99 OPT_KEY( String, startmodel )
100 OPT_KEY( String, scorefile )
101 OPT_KEY( File, fragfile )
102 OPT_KEY( Integer, num_frags )
103 OPT_KEY( IntegerVector, pos )
104 OPT_KEY( IntegerVector, designated_rank )
105 OPT_KEY( Integer, movestep )
106 OPT_KEY( Integer, ncyc )
107 OPT_KEY( Boolean, min_pack_min )
108 OPT_KEY( Boolean, min_bb )
109 OPT_KEY( Boolean, norm_scores )
110 OPT_KEY( Integer, bw )
111 OPT_KEY( Integer, n_to_search )
112 OPT_KEY( Integer, n_filtered )
113 OPT_KEY( Integer, n_output )
114 OPT_KEY( Boolean, verbose )
115 OPT_KEY( Boolean, native_placements )
116 OPT_KEY( Real, delR )
117 OPT_KEY( Real, clust_radius )
118 OPT_KEY( Real, scale_cycles )
119 OPT_KEY( Integer, clust_oversample )
120 OPT_KEY( Integer, n_matches )
121 OPT_KEY( Real, frag_dens )
122 OPT_KEY( IntegerVector, frag_len )
123 OPT_KEY( RealVector, assembly_weights )
124 OPT_KEY( Real, null_weight )
125 OPT_KEY( Real, consensus_frac )
126 OPT_KEY( Real, consensus_stdev )
127 OPT_KEY( Real, energy_cut )
128 OPT_KEY( Boolean, cheat )
129 
130 using namespace ObjexxFCL::format;
131 using namespace basic::options;
132 using namespace basic::options::OptionKeys;
133 using namespace core::pose;
134 
135 static basic::Tracer TR("denovo_density");
136 
137 // helper function: read CAs from a PDB
138 void
139 ReadCAsFromPDB( std::string pdbfile, utility::vector1< numeric::xyzVector<core::Real> > &cas ) {
140  cas.clear();
141 
142  std::ifstream inpdb(pdbfile.c_str());
143  std::string buf;
144 
145  while ( std::getline(inpdb, buf ) ) {
146  if ( buf.substr(0,4)!="ATOM" && buf.substr(0,6)!="HETATM" ) continue;
147  if ( buf.substr(12,4) != " CA " ) continue;
148 
150  atof(buf.substr(30,8).c_str()),
151  atof(buf.substr(38,8).c_str()),
152  atof(buf.substr(46,8).c_str())
153  );
154 
155  cas.push_back( ca_i );
156  }
157 }
158 
159 // store a CA trace
160 struct CAtrace {
162  dens_score_ = 0;
163  rms_ = 0;
164  tag_ = "";
165  idx_ = 0;
166  }
167 
169  cas_.reserve( pose.total_residue() );
170  for ( int i=1; i<=(int)pose.total_residue(); ++i ) {
171  if ( pose.residue(i).is_protein() ) cas_.push_back( pose.residue(i).xyz(2) );
172  }
173  dens_score_ = score;
174  rms_ = rms;
175  tag_ = tag;
176  idx_ = 0;
177  }
178 
181  std::string tag_;
183 };
184 
185 // per fragment info
186 struct FragID {
187  FragID( int pos, std::string tag) {
188  pos_=pos;
189  tag_=tag;
190  }
191  int pos_;
192  std::string tag_;
193 };
194 
195 // ignores idx (pos/tag pair is unique)
196 bool operator<(const FragID& x, const FragID& y)
197 {
198  return boost::make_tuple(x.pos_,x.tag_) < boost::make_tuple(y.pos_,y.tag_);
199 }
200 
201 /// fpd
202 /// -- none of these are movers, since they do not follow the 1 struct in / 1 struct out model
203 /// -- they may be at some point ...
205 public:
206  //
207  void run( );
208 
209  // remove density around pose from map
210  void cut_from_map( core::pose::Pose const &pose );
211 
212  // read and preprocess fragment file
213  void process_fragfile();
214 
215  // cluster fragfile
216  core::fragment::FragSetOP
217  cluster_frags(core::fragment::FragSetOP fragments);
218 
219  virtual std::string get_name() const {
220  return "DockFragments";
221  }
222 
223 private:
224  std::map<core::Size, core::fragment::Frame> library_, libsmall_;
226 };
227 
228 ///
230 public:
231  //
233 
234  void run( );
235 
237 
238  core::Real clash_score( CAtrace &pose1, CAtrace &pose2, core::Size offset );
239 
240  core::Real closability_score( CAtrace &pose1, CAtrace &pose2, core::Size offset );
241 
242  virtual std::string get_name() const {
243  return "ScoreFragmentSet";
244  }
245 
246 private:
247  // parameters
250 };
251 
252 ///
254 public:
255  //
257 
258  void run( );
259 
260  // compute the score for the current assignment
261  core::Real score(bool);
262 
263  virtual std::string get_name() const {
264  return "FragmentAssembly";
265  }
266 
267 private:
268  // parameters
271 
272  // data structures
273  utility::vector1< FragID > allfrags; // all fragment indices
274  std::map< FragID, int > frag2idx; // all fragment indices
275  utility::vector1< utility::vector1<int> > pos2frags; // frag_candidates as fragidx
276  utility::vector1<int> assigned_frags; // current MC assignment
280 
281  // debug
283 };
284 
285 ///
287 public:
288  void run( );
289 
290  virtual std::string get_name() const {
291  return "ConsensusFragment";
292  }
293 };
294 
295 ///
297 public:
298  void run( );
299 
300  virtual std::string get_name() const {
301  return "SolutionRescore";
302  }
303 };
304 
305 
306 ///
307 core::fragment::FragSetOP
308 DockFragmentsMover::cluster_frags(core::fragment::FragSetOP fragments) {
309  core::Size nfrags = basic::options::option[ num_frags ];
310  core::Real fragfilter = 10;
311 
312  core::fragment::FragSetOP fragments_clust = fragments->empty_clone();
313 
314  for ( core::fragment::ConstFrameIterator it = fragments->begin(); it != fragments->end(); ++it ) {
315  core::fragment::Frame frame = **it;
316  core::fragment::FrameOP filteredframe = frame.clone();
317 
318  core::Size nfrags_i = std::min( nfrags, frame.nr_frags() );
319  for ( core::Size i_frag=1; i_frag<=nfrags_i; i_frag++ ) {
320  core::fragment::FragDataCOP oldfrag = frame.fragment_ptr(i_frag);
321 
322  bool addfrag=true;
323  core::Size storedfragcount = filteredframe->nr_frags();
324  for ( core::Size j_frag=1; j_frag<=storedfragcount && addfrag; j_frag++ ) {
325  core::fragment::FragDataCOP storedfrag = filteredframe->fragment_ptr(j_frag);
326 
327  core::Real error = 0;
328  core::Size ntors = 0;
329  for ( core::Size i_res=1; i_res<=oldfrag->size(); ++i_res ) {
330  core::fragment::BBTorsionSRFD const & frag_i =
331  dynamic_cast< core::fragment::BBTorsionSRFD const &> ( *(oldfrag->get_residue(i_res)) );
332  core::fragment::BBTorsionSRFD const & frag_j =
333  dynamic_cast< core::fragment::BBTorsionSRFD const &> ( *(storedfrag->get_residue(i_res)) );
334 
335  for ( core::Size i_tors=1; i_tors<=frag_i.nbb(); ++i_tors ) {
336  core::Real err_i = (frag_i.torsion(i_tors) - frag_j.torsion(i_tors));
337  error += err_i*err_i;
338  ntors++;
339  }
340  }
341  core::Real RMS = std::sqrt(error/ntors);
342  if ( RMS <= fragfilter ) addfrag=false;
343  }
344 
345  if ( addfrag ) filteredframe->add_fragment(oldfrag);
346  }
347 
348  TR.Debug << "pos " << (*it)->start() << " mer " << (*it)->length()
349  << " nfrags " << frame.nr_frags() << " -> " << filteredframe->nr_frags() << std::endl;
350 
351  fragments_clust->add(filteredframe);
352  }
353 
354  return fragments_clust;
355 }
356 
357 ///
359  /////
360  // read in fragments file
361  // process
362 
363  core::fragment::FragSetOP fragments, fragments_small;
364  fragments = core::fragment::FragmentIO().read_data( basic::options::option[ fragfile ]() );
365  nmer_ = fragments->max_frag_length(); // assumes constant length fragments!
366 
367  utility::vector1< int > nmer_target = basic::options::option[ frag_len ]();
368  if ( nmer_target.size() == 0 ) {
369  nmer_target.push_back(nmer_);
370  nmer_target.push_back(nmer_);
371  }
372  runtime_assert( nmer_target.size() == 2);
373 
374  core::Size nmer_target_big = (core::Size) std::max( nmer_target[1],nmer_target[2] );
375  core::Size nmer_target_small = (core::Size) std::min( nmer_target[1],nmer_target[2] );
376 
377  if ( nmer_target_big<nmer_ ) {
378  TR << "Chopping to " << nmer_target_big << " mers" << std::endl;
379  fragments_small = core::fragment::FragSetOP( new core::fragment::ConstantLengthFragSet( nmer_target_big ) );
380  core::fragment::chop_fragments( *fragments, *fragments_small );
381  fragments = fragments_small->clone();
382  }
383 
384  if ( nmer_target_small<nmer_target_big ) {
385  TR << "Chopping to " << nmer_target_small << " mers" << std::endl;
386  fragments_small = core::fragment::FragSetOP( new core::fragment::ConstantLengthFragSet( nmer_target_small ) );
387  core::fragment::chop_fragments( *fragments, *fragments_small );
388  } else {
389  fragments_small = fragments->clone();
390  }
391 
392  nmer_ = nmer_target_big;
393  nmer_small_ = nmer_target_small;
394 
395  core::fragment::FragSetOP fragclust = cluster_frags( fragments );
396  core::fragment::FragSetOP fragsmallclust = cluster_frags( fragments_small );
397 
398  // map resids to frames
399  for ( core::fragment::ConstFrameIterator i = fragclust->begin(); i != fragclust->end(); ++i ) {
400  core::Size position = (*i)->start();
401  library_[position] = **i;
402  }
403  for ( core::fragment::ConstFrameIterator i = fragsmallclust->begin(); i != fragsmallclust->end(); ++i ) {
404  core::Size position = (*i)->start();
405  libsmall_[position] = **i;
406  }
407 }
408 
409 
410 
411 ///
413  // get sequence from fasta or native pdb if provided
414  std::string sequence;
416  if ( option[ in::file::native ].user() ) {
417  core::import_pose::pose_from_pdb( native_pose, option[ in::file::native ]().name() );
418  sequence = native_pose.sequence();
419  } else {
420  sequence = core::sequence::read_fasta_file( option[ in::file::fasta ]()[1])[1]->sequence();
421  }
422 
423  // read fragments, preprocess
425 
426  // figure out: which fragments to use, what positions to steal
427  utility::vector1<bool> use_big( sequence.length() , true );
428  utility::vector1<bool> search_positions( sequence.length() , true );
429  utility::vector1<bool> steal_positions( sequence.length() , false );
430 
431  if ( basic::options::option[ pos ].user() ) {
433  for ( core::Size i=1; i<=search_positions.size(); ++i ) {
434  search_positions[i] = (std::find( inpos.begin(), inpos.end(), i ) != inpos.end());
435  }
436  }
437 
438  core::Real STRAND_LONGFRAG_CUT = 0.5; // if at least this percent of fragdata is extended, use shortfrags
439 
440  for ( std::map<core::Size, core::fragment::Frame>::iterator it=library_.begin(); it!=library_.end(); ++it ) {
441  core::Size idx = it->first;
442  core::fragment::Frame &f = it->second;
443 
444  // foreach fragment
445  core::Size nres=0,nstrand=0;
446  for ( core::Size i=1; i<=f.nr_frags(); ++i ) {
447  core::fragment::FragData const &f_i = f.fragment( i );
448  for ( core::Size j=1; j<=f_i.size(); ++j ) {
449  nres++;
450  if ( f_i.secstruct(j) == 'E' ) { nstrand++; }
451  }
452  }
453 
454  core::Real strand_frac = ((core::Real)nstrand)/((core::Real)nres);
455  if ( strand_frac > STRAND_LONGFRAG_CUT ) {
456  use_big[idx] = false;
457  }
458  }
459 
460  bool have_initial_pose = option[ startmodel ].user();
462  std::map<int,int> initial_pose_seqmap;
463  core::Size nsteal=0;
464  if ( have_initial_pose ) {
465  core::import_pose::pose_from_pdb( initial_pose, option[ startmodel ]() );
466 
467  // trim map
468  cut_from_map( initial_pose );
469 
470  // adjust search positions
471  utility::vector1<bool> init_pose_covers(search_positions.size());
472  for ( int i=1; i<=(int)initial_pose.total_residue(); ++i ) {
473  init_pose_covers[initial_pose.pdb_info()->number(i)] = true;
474  initial_pose_seqmap[initial_pose.pdb_info()->number(i)] = i;
475  }
476  for ( int i=1; i<=(int)search_positions.size(); ++i ) {
477  if ( !search_positions[i] ) continue;
478  core::Size nmer = use_big[i]? nmer_ : nmer_small_;
479 
480  bool frag_i_covered = init_pose_covers[i];
481  for ( int j=i+1; j<i+(int)nmer && frag_i_covered; ++j ) {
482  frag_i_covered = frag_i_covered && init_pose_covers[j];
483  }
484 
485  search_positions[i] = !frag_i_covered;
486  steal_positions[i] = frag_i_covered;
487  nsteal++;
488  }
489  }
490 
491  //
492  TR << "Searching positions:";
493  for ( int i=1; i<=(int)search_positions.size(); ++i ) {
494  if ( search_positions[i] ) TR << " " << i;
495  }
496  TR << std::endl;
497  if ( nsteal>0 ) {
498  TR << "Stealing positions:";
499  for ( int i=1; i<=(int)search_positions.size(); ++i ) {
500  if ( steal_positions[i] ) TR << " " << i;
501  }
502  TR << std::endl;
503  }
504 
505  // set up docking
506  protocols::electron_density::DockIntoDensityMover dock;
507  dock.setDelR(option[ delR ]); // option?
508 
509  // I think the reason there is a valgrind error stemming from this is that
510  // we aren't setting defaults for these options
511  // and the defaults from the DockIntoDensityMover ctor
512  // are being overwritten by the uninitialized values
513  // Therefore, I will be extra-careful here and set the default again
514  // if the option is unspecified.
515  dock.setB( option[ bw ]() );
516  dock.setTopN( option[ n_to_search ]() , option[ n_filtered ]() , option[ n_output ]() ); //y
517  dock.setGridStep(option[ movestep ]()); //y
518  dock.setMinBackbone(option[ min_bb ]()); //y
519  dock.setNCyc(option[ ncyc ]()); //y
520  dock.setClusterRadius(option[ clust_radius ]()); //y
521  dock.setFragDens(option[ frag_dens ]()); //y
522  if ( option[ norm_scores ].user() ) {
523  dock.setNormScores(option[ norm_scores ]());
524  } else {
525  dock.setNormScores( false ); // ctor default
526  }
527  dock.setClusterOversamp(option[ clust_oversample ]()); //y
528  dock.setMaxRotPerTrans( 1 );
529  if ( option[ out::file::silent ].user() ) {
530  std::string silent_fn = option[ out::file::silent ]();
531  dock.setOutputSilent( silent_fn );
532  }
533  // read CA positions (if specified)
534  if ( option[ ca_positions ].user() ) {
536  ReadCAsFromPDB( option[ ca_positions ](), cas );
537  dock.predefine_search(cas);
538  dock.setCenterOnMiddleCA(true);
539  }
540 
541  // for each position
542  for ( std::map<core::Size, core::fragment::Frame>::iterator it=library_.begin(); it!=library_.end(); ++it ) {
543  core::Size idx = it->first;
544  core::fragment::Frame frame;
545  if ( use_big[idx] ) {
546  frame = library_[idx];
547  } else {
548  frame = libsmall_[idx];
549  }
550 
551  core::Size seq_pos = frame.start();
552  core::Size nmer_len = frame.length();
553  if ( !search_positions[seq_pos] && !steal_positions[seq_pos] ) continue;
554 
555  // create native_frag_pose from native_pose if native are provided
556  core::pose::PoseOP native_frag_pose ( new core::pose::Pose() ) ;
557  if ( native_pose.total_residue() > 0 ) {
559  core::kinematics::FoldTree fold_tree( nmer_len );
560  for ( core::Size irsd=seq_pos; irsd<seq_pos+nmer_len; ++irsd ) { positions.push_back( irsd ); }
561  core::pose::create_subpose( native_pose, positions, fold_tree, *native_frag_pose ); // make native_frag_pose
562 
563  // pass native to docking
564  dock.setNative( native_frag_pose );
565  }
566 
567  std::string frag_seq = sequence.substr( seq_pos-1, nmer_len );
568  TR << "search for fragment: [" << seq_pos << "] " << frag_seq << std::endl;
569 
571 
572  if ( search_positions[seq_pos] ) {
573  dock.setPassThrough( false );
574  dock.setDoRefine(option[ min_pack_min ]());
575 
576  // each frag candidate at a designated position
577  core::pose::Pose frag_ref;
578  core::pose::make_pose_from_sequence( frag_ref, frag_seq,
579  *( core::chemical::ChemicalManager::get_instance()->residue_type_set( core::chemical::CENTROID )) );
580  for ( core::Size j=1; j<=frame.nr_frags(); ++j ) {
581  core::pose::PoseOP frag_i(new core::pose::Pose(frag_ref));
582  frame.fragment( j ).apply( *frag_i, 1, nmer_len );
583  //std::string const pdbid ( frame->fragment( j ).pdbid() );
584  frags.push_back( frag_i );
585  }
586  } else if ( steal_positions[seq_pos] ) {
587  dock.setPassThrough( true );
588  dock.setDoRefine( false );
589 
590  core::pose::PoseOP steal_pose ( new core::pose::Pose() );
591 
593  core::kinematics::FoldTree fold_tree( nmer_len );
594  for ( core::Size irsd=seq_pos; irsd<seq_pos+nmer_len; ++irsd ) {
595  positions.push_back( initial_pose_seqmap[irsd] );
596  }
597  core::pose::create_subpose( initial_pose, positions, fold_tree, *steal_pose ); // make native_frag_pose
598 
599  dock.setNative( steal_pose );
600  frags.push_back( steal_pose );
601  }
602 
603  dock.setTag( "S_"+utility::to_string(seq_pos) );
604  dock.apply_multi(frags);
605  }
606 }
607 
608 
609 void
611  using core::scoring::electron_density::poseCoords;
612  using core::scoring::electron_density::poseCoord;
613 
614  core::Size edge_trim = 5;
615  core::Real mask_radius = 2; //?
616  poseCoords litePose;
617 
618  for ( int i = 1; i <= (int)pose.total_residue(); ++i ) {
619  core::conformation::Residue const & rsd_i ( pose.residue(i) );
620  bool skipres = ( rsd_i.aa() == core::chemical::aa_vrt );
621  for ( int j = i-(int)edge_trim; j < (i+(int)edge_trim) && !skipres; ++j ) {
622  if ( j>=1 && j<=(int)pose.total_residue() && pose.fold_tree().is_cutpoint( j ) ) skipres=true;
623  }
624 
625  if ( skipres ) continue;
626 
627  core::Size natoms = rsd_i.nheavyatoms();
628  for ( core::Size j = 1; j <= natoms; ++j ) {
629  core::chemical::AtomTypeSet const & atom_type_set( rsd_i.atom_type_set() );
630  poseCoord coord_j;
631  coord_j.x_ = rsd_i.xyz( j );
632  coord_j.B_ = core::scoring::electron_density::getDensityMap().getEffectiveBfactor();
633  coord_j.elt_ = atom_type_set[ rsd_i.atom_type_index( j ) ].element();
634  litePose.push_back( coord_j );
635  }
636  }
637  ObjexxFCL::FArray3D< double > rhoC, rhoMask;
638  core::scoring::electron_density::getDensityMap().calcRhoC( litePose, 0, rhoC, rhoMask, -1, 600, mask_radius );
639 
640  // apply mask to map
641  ObjexxFCL::FArray3D< float > densnew = core::scoring::electron_density::getDensityMap().data();
642  for ( int z=1; z<=(int)densnew.u3(); z++ ) {
643  for ( int y=1; y<=(int)densnew.u2(); y++ ) {
644  for ( int x=1; x<=(int)densnew.u1(); x++ ) {
645  densnew(x,y,z) *= (1-rhoMask(x,y,z));
646  }
647  }
648  }
649  core::scoring::electron_density::getDensityMap().set( densnew );
650 
651  if ( basic::options::option[ basic::options::OptionKeys::edensity::debug ]() ) {
652  core::scoring::electron_density::getDensityMap().writeMRC( "trimmed.mrc" );
653  }
654 }
655 
656 
657 ///
659  steepness_ = 8.0;
660  overlap_width_ = 3.0;
661  clash_dist_ = 2.0;
662  unclosable_penalty_ = 10.0;
663 
664  gap_lengths_.push_back(6.0); // 1
665  gap_lengths_.push_back(9.6); // 2
666  gap_lengths_.push_back(13.0); // 3
667  gap_lengths_.push_back(16.0); // 4
668  gap_lengths_.push_back(19.5); // 5
669  gap_lengths_.push_back(22.0); // 6
670  gap_lengths_.push_back(26.0); // 7
671  gap_lengths_.push_back(29.0); // 8
672  gap_lengths_.push_back(33.0); // 9
673  gap_lengths_.push_back(35.0); // 10
674  gap_lengths_.push_back(38.0); // 11
675  gap_lengths_.push_back(40.0); // 12
676  gap_lengths_.push_back(43.0); // 13
677  gap_lengths_.push_back(45.0); // 14
678  gap_lengths_.push_back(47.0); // 15
679 
680  gap_weights_.push_back(1.0000); // 1
681  gap_weights_.push_back(0.5563); // 2
682  gap_weights_.push_back(0.3529); // 3
683  gap_weights_.push_back(0.2907); // 4
684  gap_weights_.push_back(0.1735); // 5
685  gap_weights_.push_back(0.1149); // 6
686  gap_weights_.push_back(0.1032); // 7
687  gap_weights_.push_back(0.0580); // 8
688  gap_weights_.push_back(0.0469); // 9
689  gap_weights_.push_back(0.0545); // 10
690  gap_weights_.push_back(0.0457); // 11
691  gap_weights_.push_back(0.0504); // 12
692  gap_weights_.push_back(0.0510); // 13
693  gap_weights_.push_back(0.0416); // 14
694  gap_weights_.push_back(0.0422); // 15
695 }
696 
697 ///
699  // read silent file
702 
703  core::io::silent::SilentFileData sfd;
704  for ( core::Size n=1; n<=insilent.size(); ++n ) {
705  sfd.read_file( insilent[n] );
706  }
707 
708  //core::Size mer_size=0;
709  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
710  std::string tag = iter->decoy_tag();
712  core::Real score_i = iter->get_energy( "dens_score" );
713  core::Real rms_i = iter->get_energy( "rms" );
714  core::Real rank_i = iter->get_energy( "dens_rank" );
715 
716  if ( option[ n_matches ].user() && (option[ n_matches ]+0.5) < rank_i ) continue;
717  if ( option[ cheat ].user() && rms_i > 2 ) continue;
718 
719  if ( score_i<-999 ) score_i=1000.0; // hack
720 
721  runtime_assert( score_i != 0 ); //??
722 
723  TR.Debug << "Processing " << tag << std::endl;
724 
725  // NOTE: order must match what is written!
726  std::string fragtag = fields[1];
727  core::Size resid = atoi( fields[2].c_str() );
728 
729  // get pose
730  core::pose::Pose frag;
731  iter->fill_pose( frag );
732 
733  CAtrace ca_i(frag,tag, -score_i, rms_i);
734 
735  //if (mer_size==0) mer_size=ca_i.cas_.size();
736  //runtime_assert( mer_size == ca_i.cas_.size() );
737 
738  if ( all_frags.size() < resid ) all_frags.resize( resid );
739  if ( all_frags[resid].size() < rank_i ) all_frags[resid].resize( rank_i );
740  all_frags[resid][rank_i] = ca_i;
741  }
742 
743  // score: 1b first (keep them first!)
744  // make idx to tag mapping that 2b energies will use
745  core::Size nres = all_frags.size();
746  core::Size idx=1, type=1;
747  std::ofstream outscore( option[ scorefile ]().c_str(), std::ios::out | std::ios::binary );
748  for ( core::Size i_res=1; i_res<=nres; ++i_res ) {
749  TR << "1b: [" << i_res << "/" << nres << "]" << "\r" << std::flush;
750  core::Size nfrag_i = all_frags[i_res].size();
751  for ( core::Size i_frag=1; i_frag<=nfrag_i; ++i_frag ) {
752  core::Real scalefactor = 9.0 / all_frags[i_res][i_frag].cas_.size();
753 
754  core::Real dens_sc = scalefactor * all_frags[i_res][i_frag].dens_score_;
755  core::Real rms = all_frags[i_res][i_frag].rms_;
756  std::string tag = all_frags[i_res][i_frag].tag_;
757 
758  char tagstr[32];
759  std::strncpy( tagstr, tag.c_str(), 31 );
760  tagstr[31]='\0';
761 
762  outscore.write( (char*)&type , sizeof(type) );
763  outscore.write( (char*)tagstr , 32*sizeof(char) ); // truncates tag at 32 chars (should be ok ....)
764  outscore.write( (char*)&idx , sizeof(idx) );
765  outscore.write( (char*)&i_res , sizeof(i_res) );
766  outscore.write( (char*)&dens_sc , sizeof(dens_sc) );
767  outscore.write( (char*)&rms , sizeof(rms) );
768 
769  all_frags[i_res][i_frag].idx_ = idx++; // make idx->tag mapping now
770  }
771  }
772  TR << "1b: [" << nres << "/" << nres << "]" << std::endl;
773 
774  // then 2b
775  type = 2;
776  for ( core::Size i_res=1; i_res<=nres; ++i_res ) {
777  TR << "2b: [" << i_res << "/" << nres << "]" << "\r" << std::flush;
778  core::Size nfrag_i = all_frags[i_res].size();
779 
780  for ( core::Size j_res=i_res+1; j_res<=nres; ++j_res ) {
781  core::Size nfrag_j = all_frags[j_res].size();
782  core::Size offset = j_res-i_res;
783  for ( core::Size i_frag=1; i_frag<=nfrag_i; ++i_frag ) {
784  for ( core::Size j_frag=1; j_frag<=nfrag_j; ++j_frag ) {
785  core::Real clash_sc = clash_score( all_frags[i_res][i_frag], all_frags[j_res][j_frag], offset );
786  core::Real overlap_sc = overlap_score( all_frags[i_res][i_frag], all_frags[j_res][j_frag], offset );
787  core::Real closability_sc = closability_score( all_frags[i_res][i_frag], all_frags[j_res][j_frag], offset );
788 
789  //core::Real scalefactor =
790  // (9.0 / all_frags[i_res][i_frag].cas_.size()) *
791  // (9.0 / all_frags[j_res][j_frag].cas_.size());
792  //TR << i_res << "." << i_frag << " to " << j_res << "." << j_frag << ": " << clash_sc << " , " << overlap_sc << " , " << closability_sc << std::endl;
793 
794  if ( std::abs(clash_sc)+std::abs(overlap_sc)+std::abs(closability_sc) > 1e-4 ) {
795  core::Size idxi = all_frags[i_res][i_frag].idx_;
796  core::Size idxj = all_frags[j_res][j_frag].idx_;
797 
798  outscore.write( (char*)&type , sizeof(type) );
799  outscore.write( (char*)&i_res , sizeof(i_res) );
800  outscore.write( (char*)&idxi , sizeof(idxi) );
801  outscore.write( (char*)&j_res , sizeof(j_res) );
802  outscore.write( (char*)&idxj , sizeof(idxj) );
803  outscore.write( (char*)&clash_sc , sizeof(clash_sc) );
804  outscore.write( (char*)&overlap_sc , sizeof(overlap_sc) );
805  outscore.write( (char*)&closability_sc , sizeof(closability_sc) );
806  }
807  }
808  }
809  }
810  }
811  TR << "2b: [" << nres << "/" << nres << "]" << std::endl;
812 }
813 
816  core::Size mersize = pose1.cas_.size();
817  core::Real overlap_ij = 0.0;
818 
819  if ( offset<mersize ) {
820  for ( int i=(int)(offset+1); i<=(int)mersize; ++i ) {
821  int j = i-offset;
822  core::Real dist = (pose1.cas_[i] - pose2.cas_[j]).length();
823 
824  // for bad violations, give full penalty
825  if ( dist > 5.0 ) return 8.0; // should be (mer size)-1?
826 
827  // check for clashes
828  // if there are any clashes give a large penalty
829  for ( int i=1; i<=(int)mersize; ++i ) {
830  for ( int j=1; j<=(int)mersize; ++j ) {
831  int seqsep = offset-i+j;
832  if ( seqsep >= 5 || seqsep <= -5 ) {
833  core::Real dist = (pose1.cas_[i] - pose2.cas_[j]).length_squared();
834  if ( dist < clash_dist_*clash_dist_ ) {
835  return 8.0; // should be nmer-1?
836  }
837  }
838  }
839  }
840 
841  // sigmoid
842  core::Real overlap_ij_xyz = 2.0 / ( 1 + std::exp( -steepness_*(dist-overlap_width_)) ) - 1;
843  //core::Real overlap_ij_xyz = std::exp( -1*dist*steepness_);
844  overlap_ij += overlap_ij_xyz;
845  }
846  }
847  return overlap_ij;
848 }
849 
852  core::Size mersize = pose1.cas_.size();
853  core::Real clash_ij = 0.0;
854  if ( offset >= mersize ) {
855  // fragments do not overlap -- use standard penalty
856  for ( int i=1; i<=(int)mersize; ++i ) {
857  for ( int j=1; j<=(int)mersize; ++j ) {
858  //if (i==(int)mersize && j==1 && offset==mersize) continue;
859 
860  core::Real dist = (pose1.cas_[i] - pose2.cas_[j]).length_squared();
861  if ( dist < clash_dist_*clash_dist_ ) clash_ij += 1.0;
862  }
863  }
864  }
865  return clash_ij;
866 }
867 
870  runtime_assert( gap_lengths_.size() == gap_weights_.size() );
871 
872  core::Size mersize = pose1.cas_.size();
873  core::Real close_ij = 0.0;
874 
875  if ( offset < mersize ) return 0.0;
876 
877  core::Size gap_size = offset - mersize + 1;
878  if ( gap_size < gap_lengths_.size() ) {
879  core::Real dist = (pose1.cas_[mersize] - pose2.cas_[1]).length();
880 
881  if ( dist < gap_lengths_[gap_size] ) {
882  close_ij -= gap_weights_[gap_size];
883  } else {
884  close_ij += unclosable_penalty_;
885  }
886  }
887  return close_ij;
888 }
889 
890 
891 
893  overlap_wt_ = 4.0;
894  clash_wt_ = 20.0;
895  close_wt_ = 6.0;
896  dens_wt_ = 0.85; //fd
897 
898  if ( option[ assembly_weights ].user() ) {
899  overlap_wt_ = option[ assembly_weights ]()[1];
900  clash_wt_ = option[ assembly_weights ]()[2];
901  close_wt_ = option[ assembly_weights ]()[3];
902  }
903 
904  null_frag_ = option[ null_weight ]();
905 }
906 
909  core::Size nres = assigned_frags.size();
910  core::Real score_total = 0.0;
911 
912  for ( core::Size i=1; i<=nres; ++i ) {
913  core::Real clash_i=0, overlap_i=0, close_i=0, dens_i=0, score_i=0, rms_i=0;
914 
915  if ( assigned_frags[i] == 0 ) {
916  score_total+=null_frag_; //null frag
917  dens_i = null_frag_;
918  } else {
919  score_total += scores_1b[ assigned_frags[i] ];
920  dens_i = scores_1b[ assigned_frags[i] ];
921  score_i = scores_1b[ assigned_frags[i] ];
922  rms_i = rmses[ assigned_frags[i] ];
923 
924  for ( core::Size j=1; j<=nres; ++j ) {
925  if ( assigned_frags[j] != 0 ) {
926  score_i += 0.5*scores_2b[ assigned_frags[i] ][ assigned_frags[j] ];
927  score_total += 0.5*scores_2b[ assigned_frags[i] ][ assigned_frags[j] ];
928 
929  //clash_i += 0.5*clash2b[ assigned_frags[i] ][ assigned_frags[j] ];
930  //overlap_i += 0.5*overlap2b[ assigned_frags[i] ][ assigned_frags[j] ];
931  //close_i += 0.5*close2b[ assigned_frags[i] ][ assigned_frags[j] ];
932  }
933  }
934  }
935  if ( verbose ) TR << "res " << i << ": " << rms_i << " " << score_i << " = " << clash_i << "/" << overlap_i << "/" << close_i << "/" << dens_i << std::endl;
936  }
937  return score_total;
938 }
939 
940 
941 void
943  if ( !option[ out::file::silent ].user() ) {
944  TR << "Must specify an output file with -out::file::silent!" << std::endl;
945  return;
946  }
947 
948  // read scorefile
949  std::ifstream inscore( option[ scorefile ]().c_str(), std::ios::in | std::ios::binary );
950  TR << "reading scorefile" << std::endl;
951  while ( !inscore.eof() ) {
952  std::string tag;
953  core::Real dens_sc,clash_sc,overlap_sc,closability_sc, rms_i;
954  core::Size ires,jres,idxi,idxj;
955  std::string itag;
956 
957  // ??? prevent too many reallocations
958  pos2frags.reserve(1000);
959  scores_1b.reserve(50*1000);
960  rmses.reserve(50*1000);
961 
963  inscore.read( (char*)&type , sizeof(type) );
964 
965  if ( inscore.eof() ) break;
966 
967  if ( type==1 ) {
968  char tagstr[32];
969  inscore.read( (char*)tagstr , 32*sizeof(char) ); // assumes max tag == 32 bytes
970  inscore.read( (char*)&idxi , sizeof(idxi) );
971  inscore.read( (char*)&ires , sizeof(ires) );
972  inscore.read( (char*)&dens_sc , sizeof(dens_sc) );
973  inscore.read( (char*)&rms_i , sizeof(rms_i) );
974 
975  itag = tagstr;
976 
977  FragID frag_i( ires, itag );
978  allfrags.push_back( frag_i );
979 
980  frag2idx[frag_i] = idxi;
981 
982  // have to assume fragments are not ordered
983  if ( ires > pos2frags.size() ) pos2frags.resize(ires);
984  pos2frags[ires].push_back( idxi );
985 
986  if ( idxi > scores_1b.size() ) scores_1b.resize( idxi );
987  scores_1b[idxi] = dens_wt_ * dens_sc;
988 
989  if ( idxi > rmses.size() ) rmses.resize( idxi );
990  rmses[idxi] = rms_i;
991 
992  } else if ( type==2 ) {
993  // assumption: all 1body energies have been seen
994  if ( scores_2b.size() == 0 ) {
995  TR << "read " << allfrags.size() << " fragments" << std::endl;
996  TR << "reading 2b scores" << std::endl;
997  scores_2b.resize( allfrags.size(), utility::vector1<core::Real>(allfrags.size(),0) );
998 
999  // debug
1000  //clash2b.resize( allfrags.size(), utility::vector1<core::Real>(allfrags.size(),0) );
1001  //overlap2b.resize( allfrags.size(), utility::vector1<core::Real>(allfrags.size(),0) );
1002  //close2b.resize( allfrags.size(), utility::vector1<core::Real>(allfrags.size(),0) );
1003  }
1004 
1005  inscore.read( (char*)&ires , sizeof(ires) );
1006  inscore.read( (char*)&idxi , sizeof(idxi) );
1007  inscore.read( (char*)&jres , sizeof(jres) );
1008  inscore.read( (char*)&idxj , sizeof(idxj) );
1009  inscore.read( (char*)&clash_sc , sizeof(clash_sc) );
1010  inscore.read( (char*)&overlap_sc , sizeof(overlap_sc) );
1011  inscore.read( (char*)&closability_sc , sizeof(closability_sc) );
1012 
1013  scores_2b[idxi][idxj] =
1014  overlap_wt_ * overlap_sc + clash_wt_ * clash_sc + close_wt_ * closability_sc;
1015 
1016  // debug
1017  //clash2b[idxi][idxj] = clash_wt_ * clash_sc;
1018  //overlap2b[idxi][idxj] = overlap_wt_ * overlap_sc;
1019  //close2b[idxi][idxj] = close_wt_ * closability_sc;
1020 
1021  // mirror
1022  scores_2b[idxj][idxi] = scores_2b[idxi][idxj];
1023  //clash2b[idxj][idxi] = clash2b[idxi][idxj];
1024  //overlap2b[idxj][idxi] = overlap2b[idxi][idxj];
1025  //close2b[idxj][idxi] = close2b[idxi][idxj];
1026  }
1027  }
1028 
1029  core::Size nres = pos2frags.size();
1031 
1032  // read silent files
1034  core::io::silent::SilentFileData sfd;
1036  for ( core::Size n=1; n<=insilent.size(); ++n ) {
1037  sfd.read_file( insilent[n] );
1038  }
1039 
1040 
1041  // parameters
1042  core::Real sa_start_temp = 500.0, sa_end_temp=1.0;
1043  core::Size sa_nsteps = 200, mc_nsteps = 25*nres*option[ scale_cycles ];
1044 
1045  for ( int rd=1; rd<=(int)nstruct; ++rd ) {
1046  // MC
1047  // initialize
1048  core::Size nres = pos2frags.size();
1049 
1050  // add null frags, initialize to it
1051  for ( int i=1; i<=(int)nres; ++i ) {
1052  pos2frags[i].push_back( 0 );
1053  }
1054  assigned_frags.resize( nres, 0 );
1055 
1056 
1057  // run
1058  core::Real temp = sa_start_temp;
1059  core::Real temp_scale = std::pow( sa_end_temp/sa_start_temp, 1.0/((core::Real)sa_nsteps) );
1060 
1061  for ( core::Size temp_ctr=1; temp_ctr<=sa_nsteps; ++temp_ctr ) {
1062  for ( core::Size step=1; step<=mc_nsteps; ++step ) {
1063  int select_pos = numeric::random::random_range( 1, nres );
1064 
1065  // calculate compatibility scores for all the cadidate placements at the given pos
1066  utility::vector1<int> const &frag_cands = pos2frags[select_pos];
1067  core::Size n_frag_cands = frag_cands.size();
1068 
1069  utility::vector1<core::Real> frag_scores( n_frag_cands );
1070  core::Real best_frag_score = 1e30, prob_sum = 0.0;
1071  for ( int i=1; i<=(int)n_frag_cands; ++i ) {
1072  frag_scores[i] = 0;
1073  if ( frag_cands[i] == 0 ) {
1074  frag_scores[i] = null_frag_;
1075  } else {
1076  frag_scores[i] = scores_1b[ frag_cands[i] ];
1077  for ( core::Size pos=1; pos<=nres; ++pos ) {
1078  int assigned_fragidx = assigned_frags[pos];
1079  if ( assigned_fragidx != 0 && (int)pos != select_pos ) {
1080  frag_scores[i] += scores_2b[ frag_cands[i] ][ assigned_fragidx ];
1081  }
1082  }
1083  }
1084 
1085  best_frag_score = std::min( frag_scores[i], best_frag_score );
1086  }
1087 
1088  for ( core::Size i=1; i<=n_frag_cands; ++i ) {
1089  frag_scores[i] -= best_frag_score;
1090  frag_scores[i] = std::exp( -std::min( frag_scores[i]/temp, 100.0 ) );
1091 
1092  //if (option[ cheat_assem ]() && frag_cands[i] != 0 && rmses[ frag_cands[i] ]<4) {
1093  // frag_scores[i] *= 1e6;
1094  //}
1095 
1096  prob_sum += frag_scores[i];
1097  }
1098 
1099  // select fragment, normalize prob
1100  core::Real fragpicker = prob_sum*numeric::random::uniform();
1101  int picked = 0;
1102  while ( fragpicker>=0 && picked<int(n_frag_cands) ) {
1103  picked++;
1104  fragpicker -= frag_scores[picked];
1105  }
1106  assert( fragpicker < 0 );
1107  assigned_frags[select_pos] = frag_cands[picked];
1108  }
1109  core::Real score_total = score( false );
1110  core::Real nassigned = 0;
1111  for ( core::Size i=1; i<=nres; ++i ) { if ( assigned_frags[i] != 0 ) nassigned++; }
1112 
1113  TR << "Finished temp = " << temp << " : score = " << score_total << " [" << nassigned << " assigned]" << std::endl;
1114 
1115  temp *= temp_scale;
1116  }
1117 
1118  // report
1119  core::Real score_total = score( true );
1120 
1121  utility::vector1< std::string > tags_to_fetch;
1122  utility::vector1< core::Size > tag_indices;
1123  TR << "Total score: " << score_total << std::endl;
1124  for ( core::Size i=1; i<=nres; ++i ) {
1125  //TR << " res " << i << " frag " << assigned_frags[i];
1126  if ( assigned_frags[i] != 0 ) {
1127  std::ostringstream oss;
1128  oss << allfrags[assigned_frags[i]].tag_; // << "_" << i;
1129  //TR << " (" << oss.str() << ")";
1130 
1131  tags_to_fetch.push_back( allfrags[assigned_frags[i]].tag_ );
1132  tag_indices.push_back( i );
1133  }
1134  //TR << std::endl;
1135  }
1136 
1137  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
1138  std::string tag = iter->decoy_tag();
1139  utility::vector1< std::string >::iterator tag_it = std::find( tags_to_fetch.begin(), tags_to_fetch.end(), tag );
1140  if ( tag_it == tags_to_fetch.end() ) continue;
1141  int index = std::distance (tags_to_fetch.begin(), tag_it)+1; // +1 for 1-indexing
1142 
1143  // do something ...
1144  core::pose::PoseOP frag_i ( new core::pose::Pose );
1145  iter->fill_pose( *frag_i );
1146  frags_sel[ tag_indices[index] ] = frag_i;
1147  }
1148 
1149 
1150  // write silent file of saved fragments + averaged model
1151  std::string outfile = option[ out::file::silent ]()+"_"+ObjexxFCL::right_string_of( rd, 4, '0' )+".silent";
1152  core::io::silent::SilentFileData sfd_out( outfile, false, false, "binary" ); //true to store argv in silent file
1153  for ( core::Size i=1; i<=tags_to_fetch.size(); ++i ) {
1154  core::Size idx = tag_indices[i];
1155 
1156  core::io::silent::BinarySilentStruct silent_stream( *(frags_sel[idx]), tags_to_fetch[i] );
1157  silent_stream.add_energy( "pos", idx );
1158  silent_stream.add_energy( "total_score", score_total );
1159  sfd.write_silent_struct( silent_stream, outfile );
1160  }
1161 
1162  // averaged pose ... to do
1163  }
1164 }
1165 
1166 
1167 void
1169  using namespace core::pack;
1170  using namespace core::pack::task;
1171  using namespace core::pack::task::operation;
1172 
1173  // read silent files
1175 
1176  // energy cut
1177  utility::vector1<core::Real> allscores;
1178  for ( core::Size n=1; n<=insilent.size(); ++n ) {
1179  core::io::silent::SilentFileData sfd;
1180  sfd.read_file( insilent[n] );
1181 
1182  // map resids to fragments
1183  std::map< core::Size, CAtrace > all_frags;
1184 
1185  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
1186  std::string tag = iter->decoy_tag();
1188 
1189  core::Real score_i = iter->get_energy( "dens_score" );
1190  core::Real rms_i = iter->get_energy( "rms" );
1191 
1192  if ( fields.size() < 2 || fields[1] == "empty" ) { // weird bug
1193  //TR << "Skipping tag >" << tag << "<" << std::endl;
1194  continue;
1195  }
1196 
1197  core::Size resid = atoi( fields[2].c_str() );
1198 
1199  TR.Debug << "Processing " << tag << std::endl;
1200  core::pose::PoseOP frag_i (new core::pose::Pose);
1201  iter->fill_pose( *frag_i );
1202 
1203  CAtrace ca_i(*frag_i, tag, -score_i, rms_i);
1204 
1205  //all_frags[resid] = ca_i;
1206  all_frags.insert ( std::make_pair(resid,ca_i) );
1207  }
1208 
1209  // we have our structure, now score it
1210  std::map< core::Size, CAtrace >::iterator iter1, iter2;
1211 
1212  // Directly call scorefunctions
1213  ScoreFragmentSetMover scoring;
1214 
1215  core::Real overlap_wt = 4.0, clash_wt = 20.0, close_wt = 6.0, dens_wt = 0.85;
1216  if ( option[ assembly_weights ].user() ) {
1217  overlap_wt = option[ assembly_weights ]()[1];
1218  clash_wt = option[ assembly_weights ]()[2];
1219  close_wt = option[ assembly_weights ]()[3];
1220  }
1221 
1222  core::Real score=0.0, clash_score=0.0, close_score=0.0, overlap_score=0.0, dens_score=0.0;
1223  core::Size ncorr = 0;
1224  for ( iter1 = all_frags.begin(); iter1 != all_frags.end(); iter1++ ) {
1225  core::Real overlap_i=0.0;
1226  core::Real clash_i=0.0;
1227  core::Real close_i=0.0;
1228  core::Real dens_i = dens_wt * iter1->second.dens_score_;
1229  core::Real rms = iter1->second.rms_;
1230 
1231  if ( rms < 2.0 ) ncorr++;
1232 
1233  dens_score += dens_i;
1234 
1235  //TR << iter1->second.tag_ << " " << dens_i << std::endl;
1236  for ( iter2 = all_frags.begin(); iter2 != all_frags.end(); iter2++ ) {
1237  if ( iter1->first == iter2->first ) continue;
1238 
1239  core::Real overlap_ij, clash_ij, close_ij;
1240  if ( iter2->first > iter1->first ) {
1241  core::Size offset = iter2->first-iter1->first;
1242  overlap_ij = scoring.overlap_score(iter1->second, iter2->second, offset);
1243  clash_ij = scoring.clash_score(iter1->second, iter2->second, offset);
1244  close_ij = scoring.closability_score(iter1->second, iter2->second, offset);
1245  } else {
1246  core::Size offset = iter1->first-iter2->first;
1247  overlap_ij = scoring.overlap_score(iter2->second, iter1->second, offset);
1248  clash_ij = scoring.clash_score(iter2->second, iter1->second, offset);
1249  close_ij = scoring.closability_score(iter2->second, iter1->second, offset);
1250  }
1251 
1252  overlap_i += 0.5*overlap_wt * overlap_ij;
1253  clash_i += 0.5*clash_wt * clash_ij;
1254  close_i += 0.5*close_wt * close_ij;
1255 
1256  //if (overlap_ij != 0.0 || clash_ij != 0.0 || close_ij != 0.0) {
1257  // TR << " " << iter1->second.tag_ << " : " << iter2->second.tag_ << " " << overlap_ij << " / " << clash_ij << " / " << close_ij << std::endl;
1258  //}
1259  }
1260  overlap_score += overlap_i;
1261  clash_score += clash_i;
1262  close_score += close_i;
1263  TR << iter1->second.tag_ << ": " << dens_i + overlap_i + clash_i + close_i << " " << dens_i << " / " << overlap_i << " / " << clash_i << " / " << close_i << std::endl;
1264  }
1265  score = clash_score + close_score + overlap_score + dens_score;
1266  TR << "TOTAL: " << score << " [" << dens_score << " / " << overlap_score << " / " << clash_score << " / " << close_score << "] "
1267  << ncorr << " " << all_frags.size() << std::endl;
1268 
1269  }
1270 }
1271 
1272 void
1274  using namespace core::pack;
1275  using namespace core::pack::task;
1276  using namespace core::pack::task::operation;
1277 
1278  // read silent files
1280 
1281  // map resids to fragments
1282  std::map< core::Size, utility::vector1< core::pose::PoseOP > > all_frags;
1283 
1284  // energy cut
1285  utility::vector1<core::Real> allscores;
1286  core::io::silent::SilentFileData sfd;
1287  for ( core::Size n=1; n<=insilent.size(); ++n ) {
1288  sfd.read_file( insilent[n] );
1289  }
1290 
1291  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
1292  core::Real score_i = iter->get_energy( "total_score" );
1293  allscores.push_back(score_i); // stored for each fragment, this is wasteful
1294  }
1295  std::sort( allscores.begin(), allscores.end() );
1296  core::Real score_cut = allscores[ (int)std::floor( option[energy_cut]*allscores.size() )+1 ];
1297  TR << "Score cut = " << score_cut << " (" << (int)std::floor( option[energy_cut]*allscores.size() ) << "/" << allscores.size() << ")" << std::endl;
1298 
1299  for ( core::io::silent::SilentFileData::iterator iter = sfd.begin(), end = sfd.end(); iter != end; ++iter ) {
1300  core::Real score_i = iter->get_energy( "total_score" );
1301  if ( score_i > score_cut ) continue;
1302 
1303  std::string tag = iter->decoy_tag();
1305 
1306  if ( fields.size() < 2 || fields[1] == "empty" ) { // weird bug
1307  TR << "Skipping tag >" << tag << "<" << std::endl;
1308  continue;
1309  }
1310 
1311  core::Size resid = atoi( fields[2].c_str() );
1312 
1313  TR.Debug << "Processing " << tag << std::endl;
1314  core::pose::PoseOP frag_i (new core::pose::Pose);
1315  iter->fill_pose( *frag_i );
1316 
1317  all_frags[resid].push_back( frag_i );
1318  }
1319 
1320  // find largest fragment occupancy
1321  core::Size maxcount = 0;
1322  std::map< core::Size, utility::vector1< core::pose::PoseOP > >::iterator iter;
1323  for ( iter = all_frags.begin(); iter != all_frags.end(); iter++ ) {
1324  maxcount = std::max( maxcount, iter->second.size() );
1325  }
1326  TR << "Highest frequency = " << maxcount << std::endl;
1327 
1328  // cutoff
1329  maxcount = (core::Size) std::floor( maxcount*option[consensus_frac] );
1330 
1331  // now average all positions with low stdev (<2A?)
1332  std::map< core::Size , numeric::xyzVector< core::Real > > ca_sum, c_sum, n_sum, o_sum;
1333  std::map< core::Size , numeric::xyzVector< core::Real > > ca_sum2;
1334  std::map< core::Size , core::Size > rescounts;
1335 
1336  for ( iter = all_frags.begin(); iter != all_frags.end(); iter++ ) {
1337  core::Size resid=iter->first;
1338  if ( iter->second.size() < maxcount ) continue;
1339 
1340  for ( int i=1; i<=(int)iter->second.size(); ++i ) {
1341  core::pose::PoseOP frag_i = iter->second[i];
1342  for ( int j=1; j<=(int)frag_i->total_residue(); ++j ) {
1343  if ( !frag_i->residue(j).is_protein() ) continue;
1344  core::Size resid_j = resid+j-1;
1345  if ( rescounts.find( resid_j ) == rescounts.end() ) {
1346  rescounts[resid_j] = 0;
1347  ca_sum[resid_j] = c_sum[resid_j] = n_sum[resid_j] = o_sum[resid_j]
1349  ca_sum2[resid_j] = numeric::xyzVector<core::Real>(0,0,0);
1350  }
1351  numeric::xyzVector< core::Real > ca_j, c_j, n_j, o_j;
1352  n_j = frag_i->residue(j).atom(1).xyz();
1353  ca_j = frag_i->residue(j).atom(2).xyz();
1354  c_j = frag_i->residue(j).atom(3).xyz();
1355  o_j = frag_i->residue(j).atom(4).xyz();
1356 
1357  rescounts[resid_j]++;
1358  n_sum[resid_j] += n_j;
1359  ca_sum[resid_j] += ca_j;
1360  c_sum[resid_j] += c_j;
1361  o_sum[resid_j] += o_j;
1362 
1363  ca_sum2[resid_j] += numeric::xyzVector<core::Real>(
1364  ca_j[0]*ca_j[0], ca_j[1]*ca_j[1], ca_j[2]*ca_j[2]);
1365  }
1366  }
1367  }
1368 
1369  core::pose::Pose averaged_pose;
1370  std::map< core::Size , core::Size >::iterator res_iter;
1371  core::Size lastres = 0;
1372 
1373  utility::vector1< int > pdb_numbering;
1374  utility::vector1< char > pdb_chains;
1375  for ( res_iter = rescounts.begin(); res_iter != rescounts.end(); res_iter++ ) {
1376  core::Size resid = res_iter->first;
1377 
1378  n_sum[resid] /= rescounts[resid];
1379  ca_sum[resid] /= rescounts[resid];
1380  c_sum[resid] /= rescounts[resid];
1381  o_sum[resid] /= rescounts[resid];
1382 
1383  for ( int j=0; j<3; ++j ) {
1384  ca_sum2[resid][j] = ( ca_sum2[resid][j] / rescounts[resid] - ca_sum[resid][j]*ca_sum[resid][j] );
1385  }
1386  core::Real ca_var = ca_sum2[resid].length();
1387  TR << "Residue " << resid << " stdev = " << ca_var << " pose = " << ca_sum[resid] << " count = " << rescounts[resid] << std::endl;
1388  if ( ca_var > option[ consensus_stdev ] ) continue; // TODO: make this a parameter
1389 
1390  // find a fragment containing this residue
1391  // this could be more efficient if we assume constant size
1392  core::conformation::ResidueOP res_to_add;
1393  bool done=false;
1394  for ( iter = all_frags.begin(); iter != all_frags.end() && !done; iter++ ) {
1395  core::Size resid_tgt=iter->first;
1396  for ( int i=1; i<=(int)iter->second.size() && !done; ++i ) {
1397  core::pose::PoseOP frag_i = iter->second[i];
1398  for ( int j=1; j<=(int)frag_i->total_residue() && !done; ++j ) {
1399  if ( !frag_i->residue(j).is_protein() ) continue;
1400  int resid_j = resid_tgt+j-1;
1401  if ( resid_j == (int)resid ) {
1402  done = true;
1403 
1404  core::conformation::Residue old_rsd = frag_i->residue(j);
1405  chemical::ResidueTypeSetCOP rsd_set( old_rsd.residue_type_set() );
1406  chemical::ResidueType const & new_rsd_type(
1407  rsd_set->get_residue_type_with_variant_removed( old_rsd.type(),
1408  chemical::LOWERTERM_TRUNC_VARIANT ) );
1409  chemical::ResidueType const & new2_rsd_type(
1410  rsd_set->get_residue_type_with_variant_removed( new_rsd_type,
1411  chemical::UPPERTERM_TRUNC_VARIANT ) );
1412  chemical::ResidueType const & new3_rsd_type(
1413  rsd_set->get_residue_type_with_variant_removed( new2_rsd_type,
1414  chemical::UPPER_TERMINUS_VARIANT ) );
1415  chemical::ResidueType const & new4_rsd_type(
1416  rsd_set->get_residue_type_with_variant_removed( new3_rsd_type,
1417  chemical::LOWER_TERMINUS_VARIANT ) );
1418  chemical::ResidueType const & new5_rsd_type(
1419  rsd_set->get_residue_type_with_variant_removed( new4_rsd_type,
1420  chemical::DISULFIDE ) );
1421 
1422  res_to_add = conformation::ResidueFactory::create_residue( new5_rsd_type );
1423  res_to_add->atom("N").xyz(n_sum[resid_j]);
1424  res_to_add->atom("CA").xyz(ca_sum[resid_j]);
1425  res_to_add->atom("C").xyz(c_sum[resid_j]);
1426  res_to_add->atom("O").xyz(o_sum[resid_j]);
1427  }
1428  }
1429  }
1430  }
1431 
1432  if ( averaged_pose.total_residue() == 0 || resid != lastres-1 ) {
1433  averaged_pose.append_residue_by_bond( *res_to_add );
1434  } else {
1435  core::conformation::add_variant_type_to_conformation_residue(
1436  averaged_pose.conformation(), chemical::UPPER_TERMINUS_VARIANT, averaged_pose.total_residue() );
1437  averaged_pose.append_residue_by_jump( *res_to_add, averaged_pose.total_residue() );
1438  core::conformation::add_variant_type_to_conformation_residue(
1439  averaged_pose.conformation(), chemical::LOWER_TERMINUS_VARIANT, averaged_pose.total_residue() );
1440  }
1441  lastres = resid;
1442  pdb_numbering.push_back(resid);
1443  pdb_chains.push_back('A');
1444  }
1445 
1446  if ( averaged_pose.total_residue() == 0 ) {
1447  TR << "NO RESIDUES IN CONSENSUS ASSIGNMENT." << std::endl;
1448  } else {
1449  // make pdbinfo, set residues
1450  core::pose::PDBInfoOP new_pdb_info( new core::pose::PDBInfo(averaged_pose,true) );
1451  new_pdb_info->set_numbering( pdb_numbering );
1452  new_pdb_info->set_chains( pdb_chains );
1453  averaged_pose.pdb_info( new_pdb_info );
1454  averaged_pose.pdb_info()->obsolete( false );
1455 
1456  // terminal variants
1457  if ( !averaged_pose.residue_type(1).has_variant_type( chemical::LOWER_TERMINUS_VARIANT ) ) {
1458  core::pose::add_variant_type_to_pose_residue( averaged_pose, chemical::LOWER_TERMINUS_VARIANT, 1 );
1459  }
1460  core::Size nres = averaged_pose.total_residue();
1461  while ( !averaged_pose.residue(nres).is_protein() ) nres--;
1462  if ( !averaged_pose.residue_type(nres).has_variant_type( chemical::UPPER_TERMINUS_VARIANT ) ) {
1463  core::pose::add_variant_type_to_pose_residue( averaged_pose, chemical::UPPER_TERMINUS_VARIANT, nres );
1464  }
1465 
1466  // sidechain + Hydrogens
1467  id::AtomID_Mask missing( false );
1468  core::pose::initialize_atomid_map( missing, averaged_pose ); // dimension the missing-atom mask
1469  for ( Size i=1; i<= averaged_pose.total_residue(); ++i ) {
1470  core::conformation::Residue const & rsd( averaged_pose.residue(i) );
1471  for ( Size j=5; j<= rsd.natoms(); ++j ) {
1472  core::id::AtomID atom_id( j, i );
1473  missing[ atom_id ] = true;
1474  }
1475  }
1476  averaged_pose.conformation().fill_missing_atoms( missing );
1477 
1478  // repack
1479  core::scoring::ScoreFunctionOP scorefxn = core::scoring::get_score_function();
1480  TaskFactoryOP tf( new TaskFactory() );
1481  tf->push_back(TaskOperationCOP( new RestrictToRepacking() ));
1482  protocols::simple_moves::PackRotamersMoverOP pack_full_repack( new protocols::simple_moves::PackRotamersMover( scorefxn ) );
1483  pack_full_repack->task_factory(tf);
1484  pack_full_repack->apply( averaged_pose );
1485 
1486  // cartmin
1487  kinematics::MoveMap mm;
1488  mm.set_bb ( true );
1489  mm.set_chi ( true );
1490  mm.set_jump( true );
1491  if ( scorefxn->get_weight( core::scoring::cart_bonded ) == 0 ) {
1492  scorefxn->set_weight( core::scoring::cart_bonded, 0.5 );
1493  }
1494  if ( scorefxn->get_weight( core::scoring::pro_close ) != 0 ) {
1495  scorefxn->set_weight( core::scoring::pro_close, 0.0 );
1496  }
1497  core::optimization::MinimizerOptions options( "lbfgs_armijo_nonmonotone", 0.00001, true, false, false );
1498  core::optimization::CartesianMinimizer minimizer;
1499  //minimizer.run( averaged_pose, mm, *scorefxn, options );
1500 
1501  averaged_pose.dump_pdb( "S_0001.pdb" );
1502  }
1503 }
1504 
1505 
1506 //////////////////////////
1507 //////////////////////////
1508 /// main
1509 int main(int argc, char* argv[]) {
1510  try {
1511 
1512  NEW_OPT( mode , "What mode to run (place, score, assemble, or consensus)", "place" );
1513  NEW_OPT( verbose , "Be verbose?", false );
1514 
1515  // search options
1516  NEW_OPT( ca_positions, "CA positions to limit search", "" );
1517  NEW_OPT( startmodel, "A starting model for matching", "" );
1518  NEW_OPT( fragfile, "Fragfile name", "" );
1519  NEW_OPT( num_frags, "Number of fragments to use for each residue position", 25 );
1520  NEW_OPT( pos, "Only calculate at these position", utility::vector1<int>() );
1521  NEW_OPT( designated_rank , "Only calculate at these ranks", utility::vector1<int>() );
1522  NEW_OPT( min_pack_min, "Min fragment hits into density?", true );
1523  NEW_OPT( movestep, "The grid stepsize for translational search", 2 );
1524  NEW_OPT( ncyc, "Min cycles", 1 );
1525  NEW_OPT( min_bb, "Allow backbone minimization?", false );
1526  NEW_OPT( norm_scores, "Normalize scores over the map", false );
1527  NEW_OPT( bw, "spharm bandwidth", 16 );
1528  NEW_OPT( n_to_search, "how many translations to search", 4000 );
1529  NEW_OPT( n_filtered, "how many solutions to take to refinement", 2000 );
1530  NEW_OPT( n_output, "how many solutions to output", 50 );
1531  NEW_OPT( clust_radius, "Cluster radius", 3.0 );
1532  NEW_OPT( clust_oversample, "Cluster oversampling", 2 );
1533  NEW_OPT( frag_dens, "Radius to use for docking fragments", 0.7 );
1534  NEW_OPT( frag_len, "Trim fragments to this length (0=use input length)", utility::vector1<int>() );
1535  NEW_OPT( native_placements , "Generate native placements only", false );
1536 
1537  // scoring and assembly options
1538  NEW_OPT( scorefile, "Scorefile name", "fragscores.sc" );
1539  NEW_OPT( scale_cycles , "scale the number of cycles", 1.0 );
1540  NEW_OPT( assembly_weights , "Weights in assembly (dens=1): <overlap> <clash> <close>", utility::vector1<core::Real>() );
1541  NEW_OPT( null_weight , "Weight on null fragment", -150 );
1542  NEW_OPT( n_matches , "number of fragment matches", 0 );
1543  NEW_OPT( cheat , "Cheat in scoring/assembly", false );
1544 
1545  // consensus options
1546  NEW_OPT( delR , "del R for rotational search", 2.0 );
1547  NEW_OPT( consensus_frac , "Fraction of assigned models needed for consensus", 1.0 );
1548  NEW_OPT( consensus_stdev , "Max stdev of CAs for consensus", 2.5 );
1549  NEW_OPT( energy_cut , "Energy cut before consensus assembly", 0.05 );
1550 
1551  devel::init(argc, argv);
1552 
1553  // force some options
1554  option[ in::missing_density_to_jump ].value(true);
1555  option[ out::nooutput ].value(true);
1556 
1557  if ( option[mode]() == "place" ) {
1558  DockFragmentsMover().run();
1559  } else if ( option[mode]() == "score" ) {
1561  } else if ( option[mode]() == "assemble" ) {
1563  } else if ( option[mode]() == "rescore" ) {
1565  } else if ( option[mode]() == "consensus" ) {
1567  } else {
1568  TR << "Unknown mode " << option[mode]() << std::endl;
1569  return -1;
1570  }
1571 
1572  } catch ( utility::excn::EXCN_Base const & e ) {
1573  std::cout << "caught exception " << e.msg() << std::endl;
1574  return -1;
1575  }
1576  return 0;
1577 }
1578 
static basic::Tracer TR("denovo_density")
static T min(T x, T y)
Definition: Svm.cc:16
virtual std::string get_name() const
virtual std::string const msg() const
Definition: EXCN_Base.hh:70
core::Real dens_score_
virtual std::string get_name() const
std::string String
Definition: remodel.cc:69
utility::vector1< FragID > allfrags
dictionary size
Definition: amino_acids.py:44
std::istream & getline(std::istream &stream, Fstring &s)
Get Line from Stream.
Definition: Fstring.cc:1610
utility::vector1< utility::vector1< float > > scores_2b
utility::vector1< core::Real > rmses
core::Real score(bool)
std::map< FragID, int > frag2idx
utility::vector1< utility::vector1< float > > clash2b
void init(int argc, char *argv[])
Command line init() version.
Definition: init.cc:23
BooleanOptionKey const user("options:user")
Definition: OptionKeys.hh:40
def x
core::pose::Pose Pose
Definition: supercharge.cc:101
utility::keys::lookup::end< KeyType > const end
CAtrace(core::pose::Pose pose, std::string tag, core::Real score, core::Real rms)
Platform independent operations on files (except I/O)
#define runtime_assert(_Expression)
Assert that the condition holds. Evaluated for both debug and release builds.
Definition: exit.hh:63
core::Real clash_score(CAtrace &pose1, CAtrace &pose2, core::Size offset)
std::string tag_
xyzVector input/output functions
void cut_from_map(core::pose::Pose const &pose)
Fast 3x3 matrix.
Random number generator system.
utility::vector1< std::string > string_split(std::string const &in, char splitchar)
Definition: string_util.cc:158
std::map< core::Size, core::fragment::Frame > libsmall_
int random_range(int low, int high)
Return a number uniformly drawn from the inclusive range between low and high. Threadsafe since each ...
Definition: random.cc:58
tuple scorefxn
Definition: PyMOL_demo.py:63
Fstring::size_type index(Fstring const &s, Fstring const &ss)
First Index Position of a Substring in an Fstring.
Definition: Fstring.hh:2180
core::Real rms_
std::string right_string_of(T const &t, int const w, char const f= ' ')
Right-Justified string of a Template Argument Type Supporting Stream Output.
FragID(int pos, std::string tag)
utility::vector1< utility::vector1< float > > close2b
def namespace
Definition: exclude.py:223
utility::vector1< utility::vector1< int > > pos2frags
def z
Tracer IO system.
basic::options::OptionKeys collection
std::map< core::Size, core::fragment::Frame > library_
bool operator<(const FragID &x, const FragID &y)
Input file stream wrapper for uncompressed and compressed files.
T abs(T const &x)
std::abs( x ) == | x |
Definition: Fmath.hh:295
std::vector with 1-based indexing
Definition: vector1.fwd.hh:44
super::iterator iterator
Definition: vector1.hh:61
File name class supporting Windows and UN*X/Linux format names.
int u3() const
Upper Index of Dimension 3.
Definition: FArray3D.hh:539
virtual std::string get_name() const
basic::options::IntegerOptionKey const nstruct("nstruct")
virtual std::string get_name() const
rule< Scanner, options_closure::context_t > options
Definition: Tag.cc:377
T distance(MathVector< T > const &VECTOR_A, MathVector< T > const &VECTOR_B)
utility::vector1< core::Real > scores_1b
#define OPT_KEY(type, key)
utility::options::OptionCollection option
OptionCollection global.
Definition: option.cc:45
Output file stream wrapper for uncompressed and compressed files.
tuple rms
Definition: loops_kic.py:130
list native
Definition: ContactMap.py:108
int u2() const
Upper Index of Dimension 2.
Definition: FArray3D.hh:512
double Real
Definition: types.hh:39
utility::vector1< core::Real > gap_weights_
DimensionExpressionPow pow(Dimension const &dim1, Dimension const &dim2)
pow( Dimension, Dimension )
#define NEW_OPT(akey, help, adef)
core::Real unclosable_penalty_
virtual std::string get_name() const
tuple pack
Definition: loops.py:50
ocstream cout(std::cout)
Wrapper around std::cout.
Definition: ocstream.hh:287
void ReadCAsFromPDB(std::string pdbfile, utility::vector1< numeric::xyzVector< core::Real > > &cas)
utility::vector1< core::Real > gap_lengths_
utility::vector1< utility::vector1< float > > overlap2b
def dist
Definition: coordlib.py:16
int u1() const
Upper Index of Dimension 1.
Definition: FArray3D.hh:485
utility::vector1< int > assigned_frags
int main(int argc, char *argv[])
core::fragment::FragSetOP cluster_frags(core::fragment::FragSetOP fragments)
rule< Scanner, string_closure::context_t > name
Definition: Tag.cc:376
double uniform()
Generate a random number between 0 and 1. Threadsafe since each thread uses its own random generator...
Definition: random.cc:56
std::string to_string(const T &t)
Definition: string_util.hh:205
string mode
Definition: contacts.py:32
Some std::string helper functions.
Program options global and initialization function.
rule< Scanner, tag_closure::context_t > tag
Definition: Tag.cc:373
core::Real closability_score(CAtrace &pose1, CAtrace &pose2, core::Size offset)
Fast (x,y,z)-coordinate numeric vector.
static T max(T x, T y)
Definition: Svm.cc:19
void frame(const utility::vector1< utility::vector1< Real > > &R, utility::vector1< utility::vector1< Real > > &U)
platform::Size Size
Definition: random.fwd.hh:30
core::Real overlap_score(CAtrace &pose1, CAtrace &pose2, core::Size offset)
TracerProxy Debug
Definition: Tracer.hh:262
core::Size idx_
int const silent
Named verbosity levels.
def y
utility::vector1< numeric::xyzVector< core::Real > > cas_
int const verbose
rule< Scanner, option_closure::context_t > option
Definition: Tag.cc:378
FArray3D: Fortran-Compatible 3D Array.
Definition: FArray3.hh:27
std::string tag_