Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.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 src/core/sequence/util.cc
11 /// @brief small bundle of utilities for dealing with sequences
12 /// @author James Thompson
13 
14 // Unit header
15 #include <core/sequence/util.hh>
16 
17 // C/C++ headers
18 #include <algorithm>
19 #include <string>
20 
21 // Utility headers
22 #include <basic/Tracer.hh>
23 #include <basic/options/option.hh>
24 #include <basic/options/keys/in.OptionKeys.gen.hh>
25 #include <ObjexxFCL/string.functions.hh>
26 #include <utility/io/izstream.hh>
27 #include <utility/vector1.hh>
28 #include <utility/exit.hh>
29 
30 // Project headers
31 #include <core/types.hh>
32 #include <core/id/AtomID.hh>
33 #include <core/id/AtomID_Map.hh>
35 #include <core/pose/Pose.hh>
36 #include <core/pose/util.hh>
37 #include <core/scoring/rms_util.hh>
38 
39 // Package headers
48 
49 //Auto Headers
50 #include <core/pose/util.tmpl.hh>
51 namespace core {
52 namespace sequence {
53 
54 using utility::vector1;
55 using std::string;
56 
57 static basic::Tracer tr("core.sequence");
58 
59 void read_all_alignments(const std::string& format,
60  const utility::vector1<std::string>& files,
62  using std::string;
63  using utility::vector1;
64 
65  assert(alignments);
66  for (vector1<string>::const_iterator i = files.begin(); i != files.end(); ++i) {
67  vector1<SequenceAlignment> current = read_aln(format, *i);
68  std::copy(current.begin(), current.end(), std::back_inserter(*alignments));
69  }
70 }
71 
72 ///////////////////////////////////////////////////////////////////////////////
73 ///
74 /// @details if position i in seq1 is aligned with position j in seq2, mapping[ i ] == j
75 /// if position i in seq1 is unaligned, mapping[ i ] == 0
76 //
77 void
79  std::string const & filename,
80  std::string & seq1,
81  std::string & seq2,
82  sequence::DerivedSequenceMapping & mapping // from numbering in sequence 1 to numbering in sequence 2
83 )
84 {
85  std::string align1, align2;
86  { // parse the file
87  std::ifstream data( filename.c_str() );
88  std::string line;
89  // 1st sequence
90  getline( data,line );
91  runtime_assert( line[0] == '>' );
92  getline( data, align1 );
93  // 2nd sequence
94  getline( data, line );
95  runtime_assert( line[0] == '>' );
96  getline( data, align2 );
97  data.close();
98  }
99 
100  runtime_assert( align1.size() == align2.size() );
101 
102  seq1.clear();
103  seq2.clear();
104  mapping.clear();
105  int pos1(0), pos2(0);
106  for ( Size i=0; i< align1.size(); ++i ) {
107  char const al1( align1[i] ), al2( align2[i] );
108  bool const gap1( al1 == '.' || al1 == '-' );
109  bool const gap2( al2 == '.' || al2 == '-' );
110  if ( !gap2 ) {
111  ++pos2;
112  seq2 += al2;
113  }
114  if ( !gap1 ) {
115  ++pos1;
116  seq1 += al1;
117  if ( !gap2 ) {
118  mapping.push_back( pos2 );
119  } else {
120  mapping.push_back( 0 ); // unaligned
121  }
122  }
123  }
124 
125  basic::T("core.sequence.DerivedSequenceMapping") << "align1: " << align1 << "\nalign2: " << align2 <<
126  "\nseq1: " << seq1 << "\nseq2: " << seq2 << '\n';
127 
128  runtime_assert( mapping.size1() == seq1.size() );
129  mapping.size2( seq2.size() ); // set sequence 2 size
130 } // read_alignment_file
131 
133  vector1< SequenceOP > seqs = read_fasta_file( filename );
134  vector1< string > seq_strings;
135  for ( vector1< SequenceOP >::const_iterator it = seqs.begin(), end = seqs.end(); it != end; ++it ) {
136  seq_strings.push_back( (*it)->sequence() );
137  }
138  return seq_strings;
139 }
140 
142  vector1< SequenceOP > sequences;
143 
144  utility::io::izstream input( filename.c_str() );
145  std::string line, current_sequence = "", current_id = "empty";
146 
147  if ( !input ) {
148  utility_exit_with_message( "Warning: can't open file " + filename + "!" );
149  return sequences;
150  }
151 
152  while( getline( input, line ) ) {
153  if ( line.substr(0,1) == ">" ) {
154  if ( current_sequence != "" ) {
155  ObjexxFCL::strip_whitespace( current_id );
156  ObjexxFCL::strip_whitespace( current_sequence );
157  sequences.push_back( new Sequence( current_sequence, current_id ) );
158  current_sequence = "";
159  }
160  current_id = line.substr(1,line.size());
161  continue;
162  }
163  current_sequence = current_sequence + ObjexxFCL::rstrip(line);
164  }
165  if ( current_sequence != "" ) {
166  ObjexxFCL::strip_whitespace( current_id );
167  ObjexxFCL::strip_whitespace( current_sequence );
168  sequences.push_back( new Sequence( current_sequence, current_id ) );
169  }
170 
171  return sequences;
172 } // read_fasta_file
173 
174 
176  utility_exit_with_message(
177  "This function is redundant with the functions above it. Ask for help with C++ if you need it, but this is really embarassing duplication."
178  );
179  std::string sequences;
180 
181  utility::io::izstream input( filename.c_str() );
182 
183  if ( !input ) {
184  utility_exit_with_message( "Warning: can't open file " + filename + "!" );
185  return sequences;
186  }
187 
188  std::string line;
189  char aa;
190 
191  // the following line is dangerous, because FASTA files don't necessarily
192  // have a > line.
193  getline( input, line ); // skip the > line
194  while( getline( input,line) ) {
195  std::istringstream line_stream( line );
196  while( line_stream >> aa ) {
197  sequences += aa;
198  }
199  }
200 
201  return sequences;
202 } // read_fasta_file
203 
204 
206  // file I/O stuff
207  utility::io::izstream input( filename.c_str() );
208  std::string line, current_sequence = "";
209 
210  if ( !input ) {
211  utility_exit_with_message( "Warning: can't open file " + filename + "!" );
212  }
213 
215  Size max_resi = 0, max_resj = 0;
216 
217  std::string seq1, seq2;
218  Size start_seq2( 0 );
219  while( getline( input, line ) ) {
220  //can I read ungapped ?
221  tr.Trace << "read line: " << line << std::endl;
222  {
223  std::istringstream line_stream( line );
224  std::string tag;
225  line_stream >> tag;
226  if ( line_stream && tag.substr(0, std::string("ungapped").size() ) == "ungapped" ) {
227  std::string type = tag.substr( std::string("ungapped_").size() );
228  if ( type == "template:" ) {
229  line_stream >> seq2;
230  tr.Info << "read template sequence " << seq2 << std::endl;
231  } else if ( type == "query:" ) {
232  line_stream >> seq1;
233  tr.Info << "read query sequence " << seq1 << std::endl;
234  } else {
235  utility_exit_with_message( "expected either ungapped_template or ungapped_query in file " + filename );
236  }
237  continue; // next line
238  } // read sequence
239  } //scope
240  std::istringstream line_stream( line );
241  Size resi, resj;
242  line_stream >> resi >> resj;
243 
244  aligned.push_back( std::make_pair( resi, resj ) );
245  max_resi = std::max( resi, max_resi );
246  max_resj = std::max( resj, max_resj );
247  if ( start_seq2 == 0 ) start_seq2 = resj;
248  } // while( getline( input, line ) )
249 
250  // create SequenceMapping object
251  DerivedSequenceMapping mapping( max_resi, max_resj );
252  for ( vector1< std::pair< Size,Size > >::const_iterator it = aligned.begin(), end = aligned.end();
253  it != end; ++it ) {
254  mapping.insert_aligned_residue_safe( it->first, it->second );
255  } // for ( aligned )
256 
257  mapping.seq1( seq1 );
258  mapping.seq2( seq2 );
259  mapping.start_seq2( start_seq2 );
260 
261  // if ( tr.Trace.visible() ) mapping.show( tr.Trace );
262 
263  return mapping;
264 } // mapping_from_file
265 
267  using utility::vector1;
269  using namespace basic::options;
270  using namespace basic::options::OptionKeys;
271 
273 
274  if ( option[ in::file::fasta ].user() ) {
275  vector1< FileName > fns( option[ in::file::fasta ]() );
277  for ( iter it = fns.begin(), end = fns.end(); it != end; ++it ) {
278  vector1< SequenceOP > temp_seqs( read_fasta_file( *it ) );
279  for ( vector1< SequenceOP >::const_iterator s_it = temp_seqs.begin(),
280  s_end = temp_seqs.end(); s_it != s_end; ++s_it
281  ) {
282  seqs.push_back( *s_it );
283  }
284  }
285  }
286 
287  if ( option[ in::file::pssm ].user() ) {
288  vector1< FileName > fns( option[ in::file::pssm ]() );
289  for ( vector1< FileName >::const_iterator it = fns.begin(), end = fns.end();
290  it != end; ++it
291  ) {
293  prof->read_from_file( *it );
294  prof->convert_profile_to_probs(); // was previously implicit in read_from_file()
295  seqs.push_back( prof );
296  }
297  }
298 
299  return seqs;
300 }
301 
303  std::string const & format,
304  std::string const & filename
305 ) {
306 
308  if ( format == "general" ) {
309  retval = read_general_aln_file( filename );
310  } else if ( format == "grishin" ) {
311  retval = read_grishin_aln_file( filename );
312  } else {
313  utility_exit_with_message(
314  std::string( "No match for format " + format + "!" )
315  );
316  }
317  tr.Debug << "read " << retval.size() << " alignments from file " << filename
318  << " with format " << format << "." << std::endl;
319  return retval;
320 }
321 
322 
324  std::istream & input
325 ) {
326  std::string line;
328  SequenceAlignmentOP current( new SequenceAlignment );
329  while( getline( input, line ) ) {
330  if ( line.substr(0,5) == "score" ) {
331  std::istringstream line_input( line );
332  std::string dummy;
333  Real score( 0.0 );
334 
335  line_input >> dummy >> score;
336  current->score( score );
337  //while ( line_input.is_good() ) {
338  //
339  //}
340  } else if ( line.substr(0,2) == "--" ) {
341  if ( current->size() > 0 ) alignments.push_back( *current );
342  current = new SequenceAlignment;
343  } else if ( line.substr(0,1) == "#" ) {
344  // do nothing. eventually add a comment here.
345  } else {
346  std::istringstream line_input( line );
347  SequenceOP new_seq( new Sequence );
348  new_seq->read_data( line_input );
349  current->add_sequence( new_seq );
350  }
351  } // while
352  if ( current->size() > 0 ) {
353  if ( tr.Trace.visible() ) {
354  tr.Trace << "have read alignment\n" << *current << std::endl;
355  }
356  alignments.push_back( *current );
357  }
358  return alignments;
359 } // read_general_aln
360 
362  std::string const & filename
363 ) {
364  utility::io::izstream input( filename.c_str() );
365  if ( !input ) {
366  utility_exit_with_message( "Warning: can't open file " + filename + "!" );
368  return aligns;
369  } else {
370  return read_general_aln( input );
371  }
372 }
373 
375  std::string const & filename
376 ) {
377  utility::io::izstream input( filename.c_str() );
379  if ( !input ) {
380  utility_exit_with_message( "Warning: can't open file " + filename + "!" );
381  }
382 
383  std::string line, id1, id2, method, dummy;
384  SequenceAlignmentOP current( new SequenceAlignment );
385  while( getline( input, line ) ) {
386  if ( line.substr(0,2) == "--" ) {
387  if ( current->size() > 0 ) {
388  alignments.push_back( *current );
389  }
390  current = new SequenceAlignment;
391  } else if ( line.substr(0,2) == "##" ) {
392  std::istringstream line_input( line );
393  line_input >> dummy >> id1 >> id2;
394  } else if ( line.substr(0,1) == "#" ) {
395  // do nothing
396  } else if ( line.substr(0,5) == "score" ) {
397  std::istringstream line_input( line );
398  Real score( 0.0 );
399  line_input >> dummy >> score;
400  current->score( score );
401 
402  using ObjexxFCL::string_of;
403  core::Size count(1);
404  line_input >> score;
405  while ( !line_input.fail() ) {
406  count++;
407  current->score( (std::string) ("score" + string_of(count)), score ) ;
408  line_input >> score;
409  }
410  } else {
412  std::string myseq, id;
413  std::istringstream line_input( line );
414  line_input >> start >> myseq;
415  if ( ! line_input.fail() ) {
416  start = start + 1; // convert from zero-based index
417 
418  if ( current->size() >= 1 ) {
419  id = id2;
420  } else {
421  id = id1;
422  }
423 
424  SequenceOP new_seq( new Sequence( myseq, id, start ) );
425  current->add_sequence( new_seq );
426  }
427  }
428  } // while( getline( input, line ) )
429 
430  if ( current->size() > 0 ) alignments.push_back( *current );
431 
432  return alignments;
433 }
434 
435 Size
437  SequenceAlignment & candidate_aln,
438  SequenceAlignment & true_aln
439 ) {
440  runtime_assert( candidate_aln.size() == true_aln.size() );
441 
443  SequenceMapping true_map = true_aln. sequence_mapping( 1, 2 );
444  SequenceMapping candidate_map = candidate_aln.sequence_mapping( 1, 2 );
445 
446  Size n_correct(0);
447  for ( Size i = 1; i <= true_aln.length(); ++i ) {
448  Size resi_idx( true_aln.sequence(1)->start() + i );
449  if ( true_map[ resi_idx ] == 0 ) {
450  // true_map has a gap here
451  if ( candidate_map[ resi_idx ] == 0 ) {
452  // candidate_map also has a gap here, it's a true negative
453  ++n_correct;
454  }
455  } else {
456  // true_map does not have a gap here
457  if ( candidate_map[ resi_idx ] == true_map[ resi_idx ] ) {
458  // candidate_map also does not have a gap, this is a true positive
459  ++n_correct;
460  }
461  }
462  } // true_aln.length()
463 
464  return n_correct;
465 } // alignment_quality
466 
468  SequenceAlignment aln_to_steal,
470 ) {
471 
472  using std::string;
473  using utility::vector1;
475 
476  runtime_assert( aln_to_steal.size() == seqs.size() );
477 
478  // insertion positions
479  vector1< Size > insertion_positions;
480  for ( Size ii = 1; ii <= aln_to_steal.size(); ++ii ) {
481  Size const start( aln_to_steal.sequence(ii)->start() );
482  insertion_positions.push_back( start );
483  }
484 
485  // internal gaps
486  for ( Size ii = 1; ii <= aln_to_steal.length(); ++ii ) {
487  for ( Size jj = 1; jj <= aln_to_steal.size(); ++jj ) {
488  if ( aln_to_steal.sequence(jj)->is_gap(ii) ) {
489  seqs[jj]->insert_gap( insertion_positions[jj] );
490  std::string debug_s( seqs[jj]->sequence() );
491  }
492  ++insertion_positions[jj];
493  }
494  }
495 
496  // leading gaps
497  for ( Size ii = 1; ii <= aln_to_steal.size(); ++ii ) {
498  Size const start( aln_to_steal.sequence(ii)->start() );
499  for ( Size jj = 1; jj < start; ++jj ) {
500  seqs[ii]->delete_position( 1 );
501  }
502  seqs[ii]->start( start );
503  }
504 
505  // trailing gaps
506  for ( Size jj = 1; jj <= seqs.size(); ++jj ) {
507  Size const seq_length( seqs[jj]->length() );
508  Size const desired_length( aln_to_steal.length() );
509  Size const n_to_delete( seq_length - desired_length + 1 );
510 
511  if ( seq_length < desired_length ) {
512  std::cout << "--------------------------------------------------" << std::endl;
513  std::cout << aln_to_steal << std::endl;
514  std::cout << "seq: " << seqs[jj]->to_string() << std::endl;
515  std::cout << "seqs[jj]->sequence(): " << seqs[jj]->sequence() << std::endl;
516  std::cout << "length = " << seqs[jj]->length()
517  << " desired_length = " << desired_length << std::endl;
518  std::cout << "n_to_delete = " << n_to_delete << std::endl;
519  utility_exit_with_message( "error!" );
520  }
521  for ( Size ii = 1; ii < n_to_delete; ++ii ) {
522  seqs[jj]->delete_position( seqs[jj]->length() );
523  }
524  }
525 
526  // do a quick consistency check here to make sure that sequences are
527  // identical. If they're not, something has gone wrong!
528  runtime_assert( aln_to_steal.size() == seqs.size() );
529  for ( Size ii = 1; ii <= aln_to_steal.size(); ++ii ) {
530  using std::string;
531  string const aln_seq( aln_to_steal.sequence(ii)->sequence() );
532  string const stolen_seq( seqs[ii]->sequence() );
533  bool error( aln_seq.length() != stolen_seq.length() );
534 
535  // quick hack to deal with some sequences - X compared to
536  // anything should be ok.
537  if ( aln_seq != stolen_seq ) {
538  for ( Size idx = 1; idx <= stolen_seq.length(); ++idx ) {
539  error = ( error && aln_seq[idx] != 'X' && stolen_seq[idx] != 'X' &&
540  aln_seq[idx] != stolen_seq[idx]
541  );
542  }
543  }
544 
545  if ( error ) {
546  std::string msg( "sequences are not the same!\n" );
547  msg += "to_steal:\n";
548  msg += aln_to_steal.sequence(ii)->to_string() + "\n";
549  msg += "stolen:\n";
550  msg += seqs[ii]->to_string() + "\n";
551  utility_exit_with_message( msg );
552  }
553  }
554 
555  SequenceAlignment new_aln;
556  for ( Size j = 1; j <= seqs.size(); ++j ) {
557  new_aln.add_sequence( seqs[j] );
558  }
559 
560  return new_aln;
561 } // steal_alignment
562 
564  core::id::SequenceMapping const & mapping,
565  SequenceOP seq1_orig,
566  SequenceOP seq2_orig
567 ) {
568  SequenceOP seq1( new Sequence( "", seq1_orig->id(), seq1_orig->start() ) );
569  SequenceOP seq2( new Sequence( "", seq2_orig->id(), seq2_orig->start() ) );
570 
571  runtime_assert( seq1->length() == seq1->ungapped_length() );
572  runtime_assert( seq2->length() == seq2->ungapped_length() );
573 
574  core::id::SequenceMapping rev_mapping( mapping );
575  rev_mapping.reverse();
576 
577  //std::cout << "forward: " << std::endl << mapping << std::endl;
578  //std::cout << "reverse: " << std::endl << rev_mapping << std::endl;
579 
580  //Size idx1 = i_stop, idx2 = i_stop;
581  Size ngaps1 = seq1_orig->start()-1, ngaps2 = seq2_orig->start()-1;
582  Size idx1 = seq1_orig->start(), idx2 = seq2_orig->start();
583  while ( idx1 <= mapping.size1() && idx2 <= rev_mapping.size1() ) {
584  if ( mapping[ idx1 ] == 0 ) {
585  // gap in sequence 2
586  seq2->append_gap();
587  seq1->append_char( (*seq1_orig)[idx1-ngaps1] );
588  ++idx1;
589  } else if ( rev_mapping[ idx2 ] == 0 ) {
590  // gap in sequence 1
591  seq1->append_gap();
592  seq2->append_char( (*seq2_orig)[idx2-ngaps2] );
593  ++idx2;
594  } else {
595  seq1->append_char( (*seq1_orig)[idx1-ngaps1] );
596  seq2->append_char( (*seq2_orig)[idx2-ngaps2] );
597  ++idx1;
598  ++idx2;
599  }
600  }
601 
602  // trailing gaps
603  while ( idx1-ngaps1 <= seq1_orig->length() ) {
604  seq2->append_gap();
605  seq1->append_char( (*seq1_orig)[idx1-ngaps1] );
606  idx1++;
607  }
608  while ( idx2-ngaps2 <= seq2_orig->length() ) {
609  seq1->append_gap();
610  seq2->append_char( (*seq2_orig)[idx2-ngaps2] );
611  idx2++;
612  }
613 
614  runtime_assert( seq1->length() == seq2->length() );
615 
616  SequenceAlignment align;
617  align.add_sequence( seq1 );
618  align.add_sequence( seq2 );
619  return align;
620 }
621 
623  core::id::SequenceMapping const & map1,
624  core::id::SequenceMapping const & map2
625 ) {
626  //runtime_assert( map1.size2() == map2.size1() );
627  //runtime_assert( map1.seq2 () == map2.seq1 () );
628 
629  // maps from sequence 1 in map1 to sequence 2 in map2
630  core::id::SequenceMapping new_mapping( map1.size1(), map2.size2() );
631  for ( Size ii = 1; ii <= map1.size1(); ++ii ) {
632  if ( map1[ ii ] != 0 ) new_mapping[ii] = map2[ map1[ ii ] ];
633  }
634  return new_mapping;
635 }
636 
640 ) {
641  using namespace core::sequence;
642  core::sequence::SequenceOP copy1( seq1->clone() );
643  core::sequence::SequenceOP copy2( seq2->clone() );
644 
645  bool success( false ); // pessimism by default
647  SWAligner sw_align;
648  ScoringSchemeOP ss( new SimpleScoringScheme( 6, 1, -4, -1 ) );
649 
650  // test aligning with gaps
651  if ( !success ) {
652  SequenceAlignment intermediate = sw_align.align( copy1, copy2, ss );
653  if ( intermediate.identities() != intermediate.length() ) {
654  success = false;
655  } else {
656  success = true;
657  }
658  retval = intermediate.sequence_mapping( 1, 2 );
659  }
660 
661  // test aligning with no gaps
662  if ( !success ) {
663  copy1->sequence( seq1->ungapped_sequence() );
664  copy2->sequence( seq2->ungapped_sequence() );
665 
666  SequenceAlignment intermediate = sw_align.align( copy1, copy2, ss );
667  if ( intermediate.identities() != intermediate.length() ) {
668  tr.Warning << "Error: potential mismatch between sequence from alignment ";
669  tr.Warning << "and sequence from PDB!" << std::endl;
670  tr.Warning << "alignment: " << std::endl << intermediate
671  << std::endl;
672  }
673  retval = intermediate.sequence_mapping( 1, 2 );
674  }
675 
676  return retval;
677 }
678 
682 ) {
683  using namespace core::sequence;
684  core::sequence::SequenceOP copy1( seq1->clone() );
685  core::sequence::SequenceOP copy2( seq2->clone() );
686 
687  SWAligner sw_align;
688  ScoringSchemeOP ss( new SimpleScoringScheme( 6, 1, -2, -1 ) );
689 
690  SequenceAlignment aln = sw_align.align( copy1, copy2, ss );
691  return aln;
692 }
693 
695  core::pose::Pose & pose1,
696  core::pose::Pose & pose2
697 ) {
698  using namespace core::sequence;
699  SequenceOP seq1( new Sequence( pose1.sequence(), "pose1", 1 ) );
700  SequenceOP seq2( new Sequence( pose2.sequence(), "pose2", 1 ) );
701  return align_naive(seq1,seq2);
702 }
703 
704 
709 ) {
710  using core::Real;
711  using utility::vector1;
712 
713  vector1< Real > scores( seq->length(), 0.0 );
714  for ( Size ii = 1; ii <= seq->length(); ++ii ) {
715  scores[ ii ] = ss->score( seq, seq, ii, ii );
716  }
717 
718  return scores;
719 }
720 
723  core::pose::Pose & pose
724 ) {
726  using std::string;
730  string q_seq, t_seq;
731 
732  bool success(
733  get_comment( pose, "query_alignment ", q_seq ) &&
734  get_comment( pose, "template_alignment", t_seq )
735  );
736 
737  SequenceAlignment aln;
738  if ( !success ) {
739  tr.Error << "Can't extract alignment from pose!" << std::endl;
740  tr.Error << "query_aln: " << q_seq << std::endl;
741  tr.Error << "template_aln: " << t_seq << std::endl;
742  tr.flush_all_channels();
743  return aln;
744  }
745 
746  SequenceOP query( new Sequence ), templ( new Sequence );
747  std::istringstream q_in( q_seq ), t_in( t_seq );
748  query->read_data( q_in );
749  templ->read_data( t_in );
750 
751  aln.add_sequence( query );
752  aln.add_sequence( templ );
753  tr.Debug << "extracted sequence alignment from Pose: " << std::endl
754  << aln << std::endl;
755  return aln;
756 }
757 
760  core::pose::Pose & pose
761 ) {
762  add_comment( pose, "query_alignment ", aln.sequence(1)->to_string() );
763  add_comment( pose, "template_alignment", aln.sequence(2)->to_string() );
764 }
765 
768  core::pose::Pose & mod_pose,
769  core::pose::Pose const & ref_pose,
770  core::id::SequenceMapping const & mapping // mod_pose -> ref_pose
771 ) {
774  Size const mod_resn( mod_pose.total_residue() );
775  Size const ref_resn( ref_pose.total_residue() );
776  static std::string const atom_name("CA");
777  for ( Size mod_resi = 1; mod_resi <= mod_resn; ++mod_resi ) {
778  Size const ref_resi( mapping[mod_resi] );
779  if ( ref_resi && mod_resi <= mod_resn && ref_resi <= ref_resn ) {
780  if ( ! mod_pose.residue(mod_resi).has(atom_name) ) continue;
781  if ( ! ref_pose.residue(ref_resi).has(atom_name) ) continue;
782 
783  id::AtomID const id1( mod_pose.residue(mod_resi).atom_index(atom_name), mod_resi );
784  id::AtomID const id2( ref_pose.residue(ref_resi).atom_index(atom_name), ref_resi );
785  atom_map.set( id1, id2 );
786  }
787  }
788  return core::scoring::superimpose_pose( mod_pose, ref_pose, atom_map );
789 }
790 
791 } // sequence
792 } // core