Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SequenceMapping.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
11 /// @brief
12 /// @author
13 
14 #include <core/types.hh>
18 #include <basic/Tracer.hh>
19 #include <utility/exit.hh>
20 
21 #include <algorithm>
22 
23 #include <ObjexxFCL/string.functions.hh>
24 
25 #include <utility/vector1.hh>
26 
27 
28 namespace core {
29 namespace id {
30 
31 using namespace ObjexxFCL;
32 
33 /// @brief ctor
35  size2_( mapping.size() ),
36  mapping_( mapping )
37 {}
38 
41 )
42  : size2_( 0 )
43 {
44 
45  //int direction(0);
46  bool longer( event.length_change > 0 );
47  Size upstream_res(0), old_num_res(0);
48 
49  mapping_.clear();
50 
52  //utility_exit_with_message("invalidated LengthEvent passed");
53  //i guess in this case it's better to return an empty sequence mapping and let
54  //the observers deal with it
55  return;
56  }
57 
59  //direction = 1;
60  upstream_res = event.position;
61  }
63  //direction = 1;
64  upstream_res = event.position - 1;
65  }
67  //direction = -1;
68  upstream_res = event.position - 1;
69  }
70  else {
71  utility_exit_with_message(
72  "unknown signal triggered by conformation length change. please update this file"
73  );
74  }
75 
76  old_num_res = event.conformation->size() - event.length_change;
77 
78  for ( Size i = 1; i <= upstream_res; ++i ) mapping_.push_back( i );
79 
80  //if ( direction == 1 ) mapping_.push_back( upstream_res + 1 + event.length_change );
81  if ( longer ) {
82  if (upstream_res < old_num_res)
83  mapping_.push_back( upstream_res + 1 + event.length_change );
84  } else {
85  for( int i = event.length_change; i < 0; ++i ) mapping_.push_back( 0 );
86  }
87  //for ( Size i = upstream_res + 2; i <= old_num_res; ++i ) mapping_.push_back( i + direction );
88  Size downstream_res( mapping_.size() + 1 );
89  runtime_assert( downstream_res <= old_num_res + 1);
90  for( Size i = downstream_res; i <= old_num_res; ++i ) mapping_.push_back( i + event.length_change );
91 }
92 
94 
95 
97  : ReferenceCount(src)
98 {
99  *this = src;
100 }
101 
104  size2_ = src.size2();
105  mapping_ = src.mapping();
106 
107  return *this;
108 }
109 
110 void
111 SequenceMapping::resize( Size const s1, Size const s2 )
112 {
113  size2_ = s2;
114  mapping_.clear();
115  mapping_.resize(s1,0);
116 }
117 
118 ///
119 void
121 {
122  // update size2!
123  size2_ = *max_element( mapping_.begin(), mapping_.end() );
124 
125  utility::vector1< Size > new_mapping( size2_, 0 );
126  for ( Size i = 1; i <= size1(); ++i ) {
127  if ( mapping_[i] != 0 ) {
128  new_mapping[ mapping_[i] ] = i;
129  }
130  }
131 
132  size2_ = mapping_.size();
133  mapping_.swap( new_mapping );
134 }
135 
136 void
138 {
139  for( core::Size i = 1; i <= mapping_.size(); ++i){
140  if( mapping_[i] != 0 ){
141  mapping_[i] = smap_to_add[ mapping_[i] ];
142  }
143  }
144  size2_ = smap_to_add.size2();
145 }
146 
147 void
149 {
150  utility::vector1< Size > new_mapping( smap_to_add.mapping() );
151  for( core::Size i = 1; i <= new_mapping.size(); ++i){
152  if( new_mapping[i] != 0 && new_mapping[i] <= mapping_.size() ){
153  new_mapping[i] = mapping_[ new_mapping[i] ];
154  } else {
155  new_mapping[i] = 0;
156  }
157  }
158  mapping_.swap( new_mapping );
159  // size2_ stays the same
160 }
161 
162 /// access
163 Size
165 {
166  return mapping_.size();
167 }
168 
169 Size
171 {
172  return size2_;
173 }
174 
175 //
176 bool
178 {
179  bool aligned( true );
180  for ( Size i=1; i<= size1(); ++i ) {
181  if ( mapping_[i] == 0 ) {
182  aligned = false;
183  break;
184  }
185  }
186  return aligned;
187 }
188 
189 //
190 bool
192  bool identity( true );
193  for ( Size i=1; i<= size1(); ++i ) {
194  if ( mapping_[i] != i ) {
195  identity = false;
196  break;
197  }
198  }
199  return identity;
200 }
201 
202 bool
204  bool identity( true );
205  for ( Size i = 1; i <= size1(); ++i ) {
206  if ( mapping_[i] != 0 && mapping_[i] != i ) {
207  identity = false;
208  break;
209  }
210  }
211  return identity;
212 
213 }
214 
215 void
217 {
218  size2_ = s2;
219 }
220 
221 void
223 {
224  mapping_.push_back( al );
225 }
226 
227 void
229 {
230  mapping_.erase( mapping_.begin() + pos1-1 );
231 }
232 
233 void
235 {
236  show( basic::T("id.SequenceMapping") );
237 }
238 
239 void
240 SequenceMapping::show( std::ostream & output ) const {
241  for ( Size i=1; i<= size1(); ++i ) {
242  output << ("id.SequenceMapping ") << i << " --> ";
243  if ( mapping_[i] ) output << mapping_[i] << std::endl;
244  else output << "----" << std::endl;
245  }
246 }
247 
248 
249 void
251  for ( Size i=1; i<= size1(); ++i ) {
252  if ( mapping_[ i ] ) {
253  int pos = static_cast< int >(mapping_[ i ]) - setting;
254  mapping_[ i ] = ( pos > 0 ) ? pos : 0;
255  }
256  }
257 }
258 
259 
260 void
262 {
263  mapping_.insert( mapping_.begin() + pos1-1, 0 );
264 }
265 
266 void
268 {
269  mapping_.insert( mapping_.begin() + pos1-1, pos2 );
270 }
271 
272 // same as insert_aligned_residue, but a couple of extra checks on size1 and size2.
273 void
275  Size const pos1,
276  Size const pos2
277 ) {
278  if ( pos1 == 0 ) return;
279  size2( std::max( pos2, size2() ) );
280  if ( pos1 >= size1() ) {
281  mapping_.resize( pos1, 0 );
282  }
283 
284  mapping_[ pos1 ] = pos2;
285 }
286 
287 void
289 {
290  ++size2_;
291  for ( Size i=1; i<= size1(); ++i ) {
292  if ( mapping_[i] >= pos ) ++mapping_[i];
293  }
294 }
295 
296 void
298 {
299  --size2_;
300  for ( Size i=1; i<= size1(); ++i ) {
301  if ( mapping_[i] == pos ) mapping_[i] = 0;
302  else if ( mapping_[i] > pos ) --mapping_[i];
303  }
304 }
305 
306 void
308 {
309  mapping_.clear();
310  size2_ = 0;
311 }
312 
313 Size
314 SequenceMapping::operator[]( Size const pos1 ) const {
315  if ( pos1 > mapping_.size() ) return 0;
316  return mapping_[ pos1 ];
317 }
318 
319 Size &
321  if ( pos1 > mapping_.size() ) mapping_.resize( pos1, 0 );
322  return mapping_[ pos1 ];
323 }
324 
325 bool SequenceMapping::operator==( SequenceMapping const & other ) const {
326  if ( other.size1() != size1() ) return false;
327 
328  for ( Size ii = 1; ii <= size1(); ++ii ) {
329  if ( mapping_[ii] != other[ii] ) return false;
330  }
331 
332  return true;
333 }
334 
336  std::string retval( "" );
337  for ( Size i = 1; i <= size1(); ++i ) {
338  retval += string_of(i) + " --> ";
339  if ( mapping_[i] ) {
340  retval += string_of(mapping_[i]) + "\n";
341  } else {
342  retval += static_cast< std::string > ("----") + "\n";
343  }
344  }
345  return retval;
346 }
347 
348 /// @details combine all input sequence mappings into one.
349 /// sequentially, that is
353 ){
354 
355  using namespace core::id;
356 
357  //gigo :)
358  if( smaps.size() == 0 ) return new SequenceMapping() ;
359 
360  SequenceMappingOP composite_smap = new SequenceMapping();
361  *composite_smap = smaps[1];
362 
363  for( core::Size i = 2; i <= smaps.size(); ++i ){
364  composite_smap->downstream_combine( smaps[i] );
365  }
366 
367  return composite_smap;
368 
369 } //combine_sequence_mappings
370 
371 
372 
373 /// @details combine smap_to_add into smap,
374 /// i.e. smap[j] becomes smap_to_add[ smap[j] ]
375 void
378  core::id::SequenceMapping const & smap_to_add )
379 {
380  smap.downstream_combine( smap_to_add );
381 }
382 
383 } // id
384 } // core
385