Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TorsionFragment.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 #if (defined _WIN32) && (!defined WIN_PYROSETTA)
11 #define ZLIB_WINAPI // REQUIRED FOR WINDOWS
12 #endif
13 
15 
16 // Rosetta Headers
17 #include <core/types.hh>
18 #include <core/pose/Pose.hh>
19 #include <core/id/types.hh>
20 #include <core/id/TorsionID.hh>
21 
22 // Utility Headers
23 #include <utility/io/izstream.hh>
24 
25 #include <basic/Tracer.hh>
26 using basic::T;
27 using basic::Error;
28 using basic::Warning;
29 
30 static basic::Tracer TR("protocols.frags.TorsionFragment");
31 
32 // ObjexxFCL Headers
33 #include <ObjexxFCL/string.functions.hh>
34 #include <map>
35 
36 #include <utility/exit.hh>
37 #include <utility/vector1.hh>
38 
39 
40 // C++ Headers
41 // #include <cmath>
42 // #include <cstdlib>
43 // #include <iostream>
44 // #include <fstream>
45 // #include <sstream>
46 
47 namespace protocols {
48 namespace frags {
49 
50 using namespace core;
51 
53 
54 ///\brief insert this piece of fragment to a pose at position "begin"
55 ///
56 ///call pose.set_torsion which maps TorsionID to DOF_ID, so it is safe
57 ///to use even if the folding direction is not standard as N to C.
58 void
59 TorsionFragment::insert( pose::Pose & pose, Size const begin ) const
60 {
61  std::cerr << "ERROR! USING **DEPRECATED** FRAGMENTS. PLEASE SWITCH TO src/core/fragments/* FRAGMENTS!" << std::endl;
62  for ( Size i=1; i<= size(); ++i ) {
63  utility::vector1< Real > const & bb( torsions_[i] );
64  Size const seqpos( begin + i - 1 );
65  for ( Size j=1; j<= bb.size(); ++j ) {
66  pose.set_torsion( id::TorsionID( seqpos, id::BB, j ), bb[j] );
67  }
68  pose.set_secstruct( seqpos, secstruct_[i] );
69  }
70 }
71 //////////////////////////////////////////////////////////////////////////////////
72 ///\brief initialize fragment data from a classic Rosetta fragment library
73 ///
74 ///frag_size is the size of fragment in the library and nbb is the number of backbone
75 ///torision angles stored in each fragment, for protein, this will be 3. Return false if
76 ///there is a reading error and all the data which have been stored will be erased.
77 ///
78 bool
80  std::string const filename,
81  Size const frag_size,
82  Size const nbb
83 )
84 {
85  utility::io::izstream data ( filename );
86  std::cerr << "ERROR! USING **DEPRECATED** FRAGMENTS. PLEASE SWITCH TO src/core/fragments/* FRAGMENTS!" << std::endl;
87  if ( !data ) {
88  std::cerr << "Cannot open " + ObjexxFCL::string_of(frag_size) + "-mer fragment library file: " + filename + "\n" ;
89  return false;
90  }
91  std::string line;
92  while( getline( data, line ) ) {
93  std::istringstream line_stream( line );
94  std::string tag1, tag2;
95  Size position, neighbors;
96  line_stream >> tag1 >> position >> tag2 >> neighbors;
97  if (line_stream.fail() || tag1 != "position:" || tag2 != "neighbors:" ) {
98  std::cerr << " format errors in fragment library file: " << line << std::endl;
99  resize(0);
100  return false;
101  }
102  resize( position );
103  getline(data, line); // skip blank line
104  SingleResidueTorsionFragmentLibrary & current_position_library( (*this)[position] );
105  for ( Size i=1; i<=neighbors; ++i ) {
106  TorsionFragmentOP fragment = new TorsionFragment( frag_size, nbb );
107  // read lines within each fragment
108  std::string last_pdb; char last_chain('0'); Size last_seqpos(0);
109  for ( Size j=1; j<=frag_size; ++j ) {
110  getline(data, line);
111  std::istringstream line_stream( line );
112  std::string pdb; char chain, aa, secstruct; Size seqpos;
113  line_stream >> pdb >> chain >> seqpos >> aa >> secstruct;
114  if ( j == 1 ) {
115  last_pdb = pdb;
116  last_chain = chain;
117  last_seqpos = seqpos;
118  } else {
119  if (last_pdb != pdb || last_chain != chain || last_seqpos != (seqpos-j+1) ) {
120  std::cerr << "fragment reading error -- pdb, chain and seqpos mismatch\n"
121  << "position: " << position << "; neighbor: " << i << "; line: " << j << std::endl;
122  resize(0);
123  return false;
124  }
125  }
126  fragment->set_secstruct(j,secstruct);
127  for ( Size k=1; k<=nbb; ++k ) {
128  Real torsion;
129  line_stream >> torsion;
130  fragment->set_torsion(j,k,torsion);
131  } // bb torsion
132  } // fragment
133  // append this fragment to SingleResidueFragmentLibrary
134  current_position_library.append_fragment( fragment );
135  getline(data,line); // skip blank line
136  } // SingleResidueFragmentLibrary
137  } // TorsionFragmentLibrary
138  TR.Info << "read succesfully " << frag_size << "-mer fragment library file: " << filename << std::endl;
139  return true;
140 }
141 
142 
144 
145 /////////////////////////////////////////////////////////////////////////////////////////////
146 ///\brief extract a fragment library with smaller fragment size from the one with larger lize
147 ///
148 /// for example, 1-mer library can ben extracted from 3-mer library. This function is set up
149 /// in a general way that both sizes of smaller fragment and larger fragment can be flexibly
150 /// specified by my_size and src_size. Also, the source library does not have to contain
151 /// fragment data for every residue position. For example, for loop modeling, we only need
152 /// fragment for loop segment and after this function is called, smaller library is created
153 /// only for regions in which there is data in the larger library. Lastly, smaller fragment
154 /// is aligned in the center of larger fragment to have data extracted.
155 bool
157  Size my_size,
158  Size src_size,
159  TorsionFragmentLibraryCOP src_lib_op
160 )
161 {
162  // can only go from larger size to smaller size
163  runtime_assert( my_size < src_size );
164  // get continuous segment in src_lib_op
165  std::map< Size, Size > seg_map;
166  Size prev_nbrs(0);
167  Size seg_start(0);
168  for ( Size i = 1; i <= src_lib_op->size(); ++i ) {
169  Size current_nbrs( (*src_lib_op)[i].size() );
170  if ( !prev_nbrs && current_nbrs ) {
171  runtime_assert( !seg_start );
172  seg_start = i;
173  } else if ( prev_nbrs && !current_nbrs ) {
174  runtime_assert( seg_start && seg_start <i );
175  seg_map.insert( std::make_pair( seg_start, i-1 ) );
176  seg_start = 0;
177  } else if ( i == src_lib_op->size() && prev_nbrs && current_nbrs ) {
178  runtime_assert( seg_start && seg_start < i );
179  seg_map.insert( std::make_pair( seg_start, i ) );
180  seg_start = 0;
181  }
182  prev_nbrs = current_nbrs;
183  }
184  runtime_assert( !seg_start );
185 
186  if ( !seg_map.size() ) {
187  std::cerr << "Warning: source fragment library does not have any data!" << std::endl;
188  return false;
189  }
190 
191  // resize myself lib to correct size
192  Size const size_diff(src_size - my_size);
193  resize( src_lib_op->size() + size_diff );
194  // find offset by which shorter frag is aligned to longer frag
195  Size const offset( size_diff%2 == 0 ? size_diff/2 : (size_diff-1)/2 );
196 
197  // loop through each segment and derive fragments
198  for ( std::map<Size, Size>::const_iterator it = seg_map.begin(),
199  it_end = seg_map.end(); it != it_end; it++ ) {
200  Size const seg_begin(it->first);
201  Size const seg_end(it->second);
202  Size lib_index, copy_start;
203  for ( Size my_index = seg_begin, my_end = seg_end+size_diff; my_index <= my_end; my_index++ ) {
204  // get hold of current residue frag lib
205  SingleResidueTorsionFragmentLibraryOP my_residue_lib( fragments_[my_index] );
206  // figure out which subset of fragment data we should copy
207  if ( my_index < seg_begin + offset ) {
208  lib_index = seg_begin;
209  copy_start = 1 + my_index - seg_begin;
210  } else if ( my_index > seg_end + offset ) {
211  lib_index = seg_end;
212  copy_start = 1 + my_index - seg_end;
213  } else {
214  lib_index = my_index - offset;
215  copy_start = 1 + offset;
216  }
217  // do actual copying from src residue_fragment_library
218  SingleResidueTorsionFragmentLibrary const & src_residue_lib( (*src_lib_op)[lib_index] );
219  for ( Size i = 1; i <= src_residue_lib.size(); ++i ) {
220  TorsionFragment const & src_fragment( src_residue_lib[i] );
221  Size const nbb( src_fragment.nbb() );
222  TorsionFragmentOP my_fragment( new TorsionFragment( my_size, nbb ) );
223  for ( Size j = 1, jj = copy_start; j <= my_size; ++j, ++jj ) {
224  for ( Size k = 1; k <= nbb; ++k ) {
225  my_fragment->set_torsion( j, k, src_fragment.get_torsion(jj, k) );
226  } // each torsion
227  my_fragment->set_secstruct( j, src_fragment.get_secstruct(jj) );
228  } // each frag_pos
229  my_residue_lib->append_fragment( my_fragment );
230  } // each neighbor
231  } // each residue position
232  } //seg_map
233 
234  // successful
235  return true;
236 }
237 
238 /// @details Show size info about library
239 void
240 TorsionFragmentLibrary::print( std::ostream & os ) const
241 {
242  os << "TorsionFragmentLibrary:: size= " << size() << std::endl;
243  for ( Size i=1; i<= size(); ++i ) {
244  os << "window: " << i << " depth: " << fragments_[i]->size() << std::endl;
245  }
246 }
247 
249 
250 } // ns frags
251 } // ns protocols
252