Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FullAtomRNA_Fragments.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 // CVS information:
4 // $Revision: 1.1.2.1 $
5 // $Date: 2005/11/07 21:05:35 $
6 // $Author: pbradley $
7 // (c) Copyright Rosetta Commons Member Institutions.
8 // (c) This file is part of the Rosetta software suite and is made available under license.
9 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
10 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
11 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
12 /// @file FullAtomRNA_Fragments
13 /// @brief RNA fragments from database,
14 /// @detailed
15 /// @author Rhiju Das
16 
17 
18 // Rosetta Headers
22 #include <protocols/rna/RNA_ProtocolUtil.hh> // for compare_RNA_char, compare_RNA_secstruct
23 
25 // AUTO-REMOVED #include <core/conformation/Conformation.hh>
26 #include <core/pose/util.hh>
28 #include <core/id/TorsionID.hh>
29 #include <core/id/AtomID.hh>
30 #include <core/id/NamedAtomID.hh>
31 #include <core/pose/Pose.hh>
32 
33 #include <ObjexxFCL/FArray1D.hh>
34 #include <ObjexxFCL/FArray2D.hh>
35 #include <ObjexxFCL/StaticIndexRange.hh>
36 
37 #include <ObjexxFCL/format.hh>
38 
39 #include <utility/io/izstream.hh>
40 #include <utility/exit.hh>
41 
42 // AUTO-REMOVED #include <numeric/random/random.hh>
43 #include <numeric/xyzVector.hh>
44 
45 #include <core/types.hh>
46 
47 // C++ headers
48 #include <fstream>
49 #include <iostream>
50 
51 #include <utility/vector1.hh>
52 #include <utility/options/BooleanVectorOption.hh>
53 #include <numeric/random/random.fwd.hh>
54 
55 //Auto using namespaces
56 namespace ObjexxFCL { namespace fmt { } } using namespace ObjexxFCL::fmt; // AUTO USING NS
57 //Auto using namespaces end
58 
59 using namespace core;
60 
61 namespace protocols{
62 namespace rna{
63 
64 /// @details Auto-generated virtual destructor
65 FragmentLibrary::~FragmentLibrary() {}
66 
67  using core::Size;
68  using core::Real;
69 
70  /////////////////////////////////////////////////////////////////////////////////////////////////
71  /////////////////////////////////////////////////////////////////////////////////////////////////
72  /////////////////////////////////////////////////////////////////////////////////////////////////
73  TorsionSet::TorsionSet( Size const size ){
74  torsions.dimension( core::scoring::rna::NUM_RNA_TORSIONS, SRange(0, size) );
75  torsion_source_name.dimension( SRange(0, size), std::string( 4, ' ' ) );
76  secstruct.dimension( SRange(0, size), 'L' );
77  non_main_chain_sugar_coords_defined = false;
78  size_ = size;
79  }
80 
81 //////////////////////////////////////////////////////////////////////
82  TorsionSet &
83  TorsionSet::operator =(
84  TorsionSet const & src
85  ){
86  size_ = src.size_;
87 
88  for (Size offset = 0; offset < size_; offset++){
89  for (Size j = 1; j <= core::scoring::rna::NUM_RNA_TORSIONS; j++ ){
90  torsions( j, offset) = src.torsions( j, offset);
91  }
92  torsion_source_name( offset ) = src.torsion_source_name( offset );
93  secstruct( offset ) = src.secstruct( offset );
94  }
95 
96  non_main_chain_sugar_coords_defined = src.non_main_chain_sugar_coords_defined;
97 
98  if (non_main_chain_sugar_coords_defined) {
99  non_main_chain_sugar_coords.dimension( SRange(0,size_), 3, 3 );
100  for (Size offset = 0; offset < size_; offset++){
101  for (Size j = 1; j <= 3; j++ ) {
102  for (Size k = 1; k <= 3; k++ ) {
103  non_main_chain_sugar_coords( offset, j, k ) =
104  src.non_main_chain_sugar_coords( offset, j, k );
105  }
106  }
107  }
108  }
109 
110  return *this;
111  }
112 
113  /////////////////////////////////////////////////////////////////////////////////////////////////
114  /////////////////////////////////////////////////////////////////////////////////////////////////
115  /////////////////////////////////////////////////////////////////////////////////////////////////
116  Real FragmentLibrary::get_fragment_torsion( Size const num_torsion, Size const which_frag, Size const offset ){
117  return align_torsions_[ which_frag - 1 ].torsions( num_torsion, offset) ;
118  }
119 
120  /////////////////////////////////////////////////////////////////////////////////////////////////
121  TorsionSet const FragmentLibrary::get_fragment_torsion_set( Size const which_frag ){
122  return align_torsions_[ which_frag - 1 ];
123  }
124 
125  /////////////////////////////////////////////////////////////////////////////////////////////////
126  void FragmentLibrary::add_torsion( TorsionSet const torsion_set ){
127  align_torsions_.push_back( torsion_set );
128  }
129 
130  /////////////////////////////////////////////////////////////////////////////////////////////////
131  void FragmentLibrary::add_torsion(
132  FullAtomRNA_Fragments const & vall,
133  Size const position,
134  Size const size
135  )
136  {
137  TorsionSet torsion_set( size );
138 
139  for (Size offset = 0; offset < size; offset++){
140  for (Size j = 1; j <= core::scoring::rna::NUM_RNA_TORSIONS; j++ ){
141  torsion_set.torsions( j, offset) = vall.torsions( j, position+offset);
142  }
143  torsion_set.torsion_source_name( offset ) = vall.name( position+offset );
144  torsion_set.secstruct( offset ) = vall.secstruct( position+offset );
145 
146  //Defined non-ideal geometry of sugar ring -- to keep it closed.
148  torsion_set.non_main_chain_sugar_coords_defined = true;
149  torsion_set.non_main_chain_sugar_coords.dimension( SRange(0,size), 3, 3 );
150  for (Size j = 1; j <= 3; j++ ){
151  for (Size k = 1; k <= 3; k++ ){
152  torsion_set.non_main_chain_sugar_coords( offset, j, k ) =
153  vall.non_main_chain_sugar_coords( position+offset, j, k );
154  }
155  }
156  } else {
157  torsion_set.non_main_chain_sugar_coords_defined = false;
158  }
159 
160  }
161 
162  align_torsions_.push_back( torsion_set );
163  }
164 
165 
166 
167  /////////////////////////////////////////////////////////////////////////////////////////////////
168  Size FragmentLibrary::get_align_depth() {
169  return align_torsions_.size();
170  }
171 
172  ///////////////////////////////////////////////////////////////////////////////////////
173  ///////////////////////////////////////////////////////////////////////////////////////
174  ///////////////////////////////////////////////////////////////////////////////////////
175 
176  FullAtomRNA_Fragments::FullAtomRNA_Fragments( std::string const & filename):
177  RNA_Fragments()
178  {
179  read_vall_torsions( filename );
180  }
181 
182  ///////////////////////////////////////////////////////////////////////////////////////
183  void
185 
186  FragmentLibraryOP fragment_library_p;
187  fragment_library_p = new FragmentLibrary;
188 
189  std::string const RNA_string = key.first;
190  std::string const RNA_secstruct_string = key.second;
191 
192  Size const size = RNA_string.length();
193 
194  runtime_assert( RNA_string.length() == RNA_secstruct_string.length() );
195 
196  // dummy initialization.
197  std::string vall_current_sequence ( RNA_string );
198  std::string vall_current_secstruct( RNA_secstruct_string );
199 
200  for (Size i = 1; i <= vall_size_ - size + 1; i++ ){
201 
202  bool match( true );
203 
204  for (Size offset = 0; offset < size; offset++ ){
205  vall_current_sequence [offset] = vall_sequence_ ( i + offset );
206  vall_current_secstruct[offset] = vall_secstruct_( i + offset );
207 
208  if ( /*vall_is_chainbreak_( i + offset ) ||*/
209  !compare_RNA_char( vall_current_sequence[offset], RNA_string[ offset ] ) ||
210  !compare_RNA_secstruct( vall_current_secstruct[offset], RNA_secstruct_string[ offset ] ) ) {
211  match = false;
212  break;
213  }
214  }
215 
216  if (match) {
217  fragment_library_p->add_torsion( *this, i, size );
218  }
219 
220  }
221 
222 
223  if ( fragment_library_p->get_align_depth() == 0 ) {
224  // Problem -- need to repick with less stringent requirements?
225  for (Size i = 1; i <= vall_size_ - size + 1; i++ ){
226 
227  bool match( true );
228 
229  for (Size offset = 0; offset < size; offset++ ){
230  vall_current_sequence [offset] = vall_sequence_ ( i + offset );
231 
232  if ( !compare_RNA_char( vall_current_sequence[offset], RNA_string[ offset ] ) ) {
233  match = false;
234  break;
235  }
236  }
237 
238  if (match) {
239  fragment_library_p->add_torsion( *this, i, size );
240  }
241 
242  }
243  }
244 
245 
246  std::cout << "Picked Fragment Library for sequence " << RNA_string << " " <<
247  " and sec. struct " << RNA_secstruct_string << " ... found " <<
248  fragment_library_p->get_align_depth() << " potential fragments" << std::endl;
249 
250  fragment_library_pointer_map[ key ] = fragment_library_p;
251 
252  }
253 
254  ///////////////////////////////////////////////////////////////////////////////////////
255  ///////////////////////////////////////////////////////////////////////////////////////
256  void
258  TorsionSet & torsion_set,
259  std::string const RNA_string,
260  std::string const RNA_secstruct_string,
261  Size const type /* = MATCH_YR */){
262 
263  std::string const RNA_string_local = convert_based_on_match_type( RNA_string, type );
264 
265  SequenceSecStructPair const key( std::make_pair( RNA_string_local, RNA_secstruct_string ) );
266 
267  if (! fragment_library_pointer_map.count( key ) ){
268  pick_fragment_library( key );
269  }
270 
271  FragmentLibraryOP fragment_library_pointer = fragment_library_pointer_map[ key ];
272 
273  Size const num_frags = fragment_library_pointer->get_align_depth();
274 
275  if (num_frags == 0) { //trouble.
276  std::cout << "Fragment Library: zero fragments found for " << RNA_string_local << std::endl;
277  std::cerr << "Fragment Library: zero fragments found for " << RNA_string_local << std::endl;
278  utility::exit( EXIT_FAILURE, __FILE__, __LINE__);
279  }
280 
281  Size const which_frag = static_cast <Size> ( numeric::random::uniform() * num_frags) + 1;
282 
283  torsion_set = fragment_library_pointer->get_fragment_torsion_set( which_frag );
284 
285  }
286 
287  ///////////////////////////////////////////////////////////////////////////////////////
288  ///////////////////////////////////////////////////////////////////////////////////////
289  void
291  TorsionSet & torsion_set,
292  core::pose::Pose & pose,
293  Size const position,
294  Size const size,
295  Size const type /* = MATCH_YR */){
296 
297  std::string const & RNA_sequence( pose.sequence() );
298  std::string const & RNA_string = RNA_sequence.substr( position - 1, size );
299 
300  //Desired "secondary structure".
301  // TEMPORARY HACK!!!
302  // std::string const RNA_secstruct( pose.total_residue(), 'X' );
303  std::string const & RNA_secstruct( protocols::rna::get_rna_secstruct( pose ) );
304  std::string const & RNA_secstruct_string = RNA_secstruct.substr( position - 1, size );
305 
306  pick_random_fragment( torsion_set, RNA_string, RNA_secstruct_string, type );
307 
308  }
309 
310  ///////////////////////////////////////////////////////////////////////////////////////
311  ///////////////////////////////////////////////////////////////////////////////////////
312  void
314  core::pose::Pose & pose,
315  Size const position,
316  Size const size,
317  Size const type,
318  toolbox::AllowInsertOP allow_insert ){
319 
320  TorsionSet torsion_set( size );
321  pick_random_fragment( torsion_set, pose, position, size, type );
322  insert_fragment( pose, position, torsion_set, allow_insert );
323  }
324 
325  //////////////////////////////////////////////////////////////////////////////////////////////////////////////
326  void
328  core::pose::Pose & pose,
329  Size const position,
330  protocols::rna::TorsionSet const & torsion_set,
331  toolbox::AllowInsertOP allow_insert
332  )
333  {
334  using namespace core::scoring::rna;
335 
336  Size const size = torsion_set.get_size();
337 
338  for (Size offset = 0; offset < size; offset++){
339 
340  Size const position_offset = position + offset;
341 
342  pose.set_secstruct( position_offset, torsion_set.secstruct( offset ) );
343 
344  for (Size j = 1; j <= NUM_RNA_TORSIONS; j++) {
345  id::TorsionID rna_torsion_id( position_offset, id::BB, j );
346  if ( j > NUM_RNA_MAINCHAIN_TORSIONS) rna_torsion_id = id::TorsionID( position_offset, id::CHI, j - NUM_RNA_MAINCHAIN_TORSIONS );
347 
348  if ( !allow_insert->get( rna_torsion_id , pose.conformation() ) ) continue;
349 
350  // if ( position == 1 ) std::cout << "ABOUT TO INSERT: " << position_offset << " torsion number " << j << std::endl;
351 
352  pose.set_torsion( rna_torsion_id,
353  torsion_set.torsions( j, offset ) );
354  }
355 
356  }
357 
358  //////////////////////////////////////////////////////////////
359  if ( torsion_set.non_main_chain_sugar_coords_defined ) {
360 
362 
363  //Force one refold.
364  pose.residue(1).xyz( 1 );
365  pose::Pose const & reference_pose( pose ); //This will avoid lots of refolds. I think.
366 
367  for (Size offset = 0; offset < size; offset++){
368 
369  Size const position_offset = position + offset;
371  bool change_sugar( true );
372 
373  for (Size n = 1; n <= non_main_chain_sugar_atoms.size(); n++ ) {
374  Vector v( torsion_set.non_main_chain_sugar_coords( offset, n, 1) ,
375  torsion_set.non_main_chain_sugar_coords( offset, n, 2) ,
376  torsion_set.non_main_chain_sugar_coords( offset, n, 3) ) ;
377  vecs.push_back( v );
378 
379  id::AtomID sugar_atom_id( named_atom_id_to_atom_id( id::NamedAtomID(non_main_chain_sugar_atoms[n], position_offset ), pose ) );
380  if ( !allow_insert->get( sugar_atom_id ) ){
381  change_sugar = false; break;
382  }
383  }
384 
385  if (!change_sugar) continue;
386  apply_non_main_chain_sugar_coords( vecs, pose, reference_pose, position_offset );
387 
388  }
389  }
390 
391  }
392 
393 
394  ///////////////////////////////////////////////////////////////////////////////////////
395  ///////////////////////////////////////////////////////////////////////////////////////
396  void
398 
399  //Just read in this file once.
400  static bool init ( false );
401  if (init) return;
402  init = true;
403 
404  ///////////////////////////////////////////////////////////////
405  //A bunch of vectors for temporary readin.
406  //At the end, transfer all the data to FArrays for faster access.
409  utility::vector1< utility::vector1< Vector > > vall_non_main_chain_sugar_coords;
410  utility::vector1< char > vall_sequence;
411  utility::vector1< char > vall_secstruct;
412  utility::vector1< bool > vall_is_chainbreak;
413  utility::vector1< utility::vector1< bool > > vall_edge_is_base_pairing;
414  utility::vector1< bool > vall_makes_canonical_base_pair;
417 
418 
419  ///////////////////////////////////////////////////////////////
420  std::cout << "Reading in vall_torsions file: " << filename << std::endl;
421 
422  //This will check in rosetta_database first.
423  utility::io::izstream vall_in( filename.c_str() );
424  if ( vall_in.fail() ){
425  utility_exit_with_message( "Bad vall torsions file? " + filename );
426  }
427 
428  std::string line, tag;
429 
430  char dummy_char;
431  bool dummy_bool;
432  Real dummy_real;
433  std::string dummy_string;
434 
435  Size count( 0 );
436  while ( getline( vall_in, line) ){
437 
438  std::istringstream line_stream( line );
439 
440  count++;
441 
442  line_stream >> dummy_char;
443  vall_sequence.push_back( dummy_char );
444 
445  utility::vector1 < Real > dummy_vec;
446  for (Size i = 1; i <= core::scoring::rna::NUM_RNA_TORSIONS; i++ ) {
447  line_stream >> dummy_real;
448  dummy_vec.push_back( dummy_real );
449  }
450  vall_torsions.push_back( dummy_vec );
451 
452  line_stream >> dummy_char;
453 
454  //In the new style fragment set... keep track of C3*, C2*, O4* coordinates
455  // explicitly, allowing for non-ideal bond lengths and bond angles.
456  if ( dummy_char == 'S' ) {
459  Real x,y,z;
460  for (Size n = 1; n <= 3; n++ ) {
461  line_stream >> x >> y >> z;
462  vecs.push_back( Vector( x,y,z) );
463  }
464  vall_non_main_chain_sugar_coords.push_back( vecs );
465  line_stream >> dummy_char;
466  }
467 
468  vall_secstruct.push_back( dummy_char );
469 
470  utility::vector1 < bool > dummy_vec2;
471  for (Size i = 1; i <= core::scoring::rna::NUM_EDGES; i++ ) {
472  line_stream >> dummy_bool;
473  dummy_vec2.push_back( dummy_bool );
474  }
475  vall_edge_is_base_pairing.push_back( dummy_vec2 );
476 
477  //vall_is_chainbreak_( count ) = 0.0;
478  line_stream >> dummy_bool;
479  vall_is_chainbreak.push_back( dummy_bool );
480 
481  //In principle could look for one more string in the vall
482  // torsions file as a "name", but for now just keep track
483  // of line number.
484  vall_name.push_back( I( 4, count ) );
485 
486  } // line_stream
487 
488  vall_size_ = count;
489 
490  vall_in.close();
491 
492  std::cout << "Lines read from vall_torsions file: " << vall_size_ << std::endl;
493 
494  ///////////////////////////////////////////////////////////////
495  // Permanent storage.
497  vall_sequence_.dimension ( vall_size_ );
498  vall_secstruct_.dimension ( vall_size_ );
499  vall_is_chainbreak_.dimension ( vall_size_ );
501  vall_name_.dimension( vall_size_ );
502 
504 
505  for (Size n = 1; n <= vall_size_; n++ ) {
506 
507  for (Size i = 1; i <= core::scoring::rna::NUM_RNA_TORSIONS; i++ ) {
508  vall_torsions_( i, n ) = vall_torsions[ n ][ i ];
509  }
510 
512  for ( Size i = 1; i <= 3; i++ ) {
513  vall_non_main_chain_sugar_coords_( n, i, 1 ) = vall_non_main_chain_sugar_coords[ n ][ i ].x();
514  vall_non_main_chain_sugar_coords_( n, i, 2 ) = vall_non_main_chain_sugar_coords[ n ][ i ].y();
515  vall_non_main_chain_sugar_coords_( n, i, 3 ) = vall_non_main_chain_sugar_coords[ n ][ i ].z();
516  }
517  }
518 
519  vall_sequence_( n ) = vall_sequence[ n ];
520  vall_secstruct_( n ) = vall_secstruct[ n ];
521  vall_is_chainbreak_( n ) = vall_is_chainbreak[ n ];
522  for (Size i = 1; i <= core::scoring::rna::NUM_EDGES; i++ ) {
523  vall_edge_is_base_pairing_( n , i) = vall_edge_is_base_pairing[ n ][ i ];
524  }
525  vall_name_( n ) = vall_name[ n ];
526  }
527 
528 
529  }
530 
531  ////////////////////////////////////////////////////////////////////////////////////////
532  bool
534 
535 }
536 }
537