Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SWAligner.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 SWAligner.hh
11 /// @brief class definition for a class that aligns two Sequence objects using
12 /// a ScoringScheme object and the Smith-Waterman alignment algorithm.
13 /// @author James Thompson
14 
15 #include <core/types.hh>
16 
20 
24 
25 #include <string>
26 
27 #include <utility/vector1.hh>
28 
29 
30 namespace core {
31 namespace sequence {
32 
33 SequenceAlignment SWAligner::align(
34  SequenceOP seq_y,
35  SequenceOP seq_x,
37 ) {
38  validate_input( seq_y, seq_x, ss );
39 
40  SequenceOP new_seq_x = seq_x->clone();
41  SequenceOP new_seq_y = seq_y->clone();
42 
43  new_seq_x->insert_gap(0);
44  new_seq_y->insert_gap(0);
45 
46  DP_Matrix scores( new_seq_x->length(), new_seq_y->length() );
47  //scores.xlab( new_seq_x->sequence_vec() );
48  //scores.ylab( new_seq_y->sequence_vec() );
49 
50  // bail out of alignment if our score doesn't beat threshold
51  Real const threshold( 0.0 );
52 
53  CellOP best_cell( new Cell( 0.0 ) );
54  for ( Size y = 2; y <= new_seq_y->length(); ++y ) {
55  for ( Size x = 2; x <= new_seq_x->length(); ++x ) {
56 
57  // score this position
58  Real l_gap_penalty( ss->gap_open() ), u_gap_penalty( ss->gap_open() );
59  if ( scores(x,y-1)->came_from() == above ) u_gap_penalty = ss->gap_extend();
60  if ( scores(x-1,y)->came_from() == left ) l_gap_penalty = ss->gap_extend();
61 
62  Real u_gap = scores( x, y-1 )->score() + u_gap_penalty;
63  Real l_gap = scores( x-1, y )->score() + l_gap_penalty;
64  Real mm = scores( x-1, y-1 )->score() + ss->score( new_seq_x, new_seq_y, x, y );
65  //Real mm = scores( x-1, y-1 )->score() + ss->score( new_seq_x, new_seq_y, x, y ) - 0.45; // hacky
66 
67  //std::cout << mm << " = " << scores( x-1, y-1 )->score() << " + " << ss->score( new_seq_x, new_seq_y, x, y ) << std::endl;
68 
69  //std::cout << scores << std::endl;
70  //std::cout << "(" << x << "," << y << ")"
71  // << " can choose from " << mm << "," << l_gap << "," << u_gap
72  // << std::endl;
73  //std::cout << "mm = " << mm << "("
74  // << scores( x-1, y-1 )->score() << "+"
75  // << ss->score( new_seq_x, new_seq_y, x, y ) << ")"
76  // << std::endl;
77  CellOP current_cell = scores(x,y);
78  if ( mm > l_gap && mm > u_gap && mm >= threshold ) {
79  //if ( mm >= l_gap && mm >= u_gap && mm >= threshold ) {
80  //std::cout << "came from diagonal with a score of " << mm << std::endl;
81  current_cell->score( mm );
82  current_cell->next( scores(x-1,y-1) );
83  current_cell->came_from( diagonal );
84  } else if ( l_gap >= mm && l_gap >= u_gap && l_gap >= threshold ) {
85  //std::cout << "came from left with a score of " << l_gap << std::endl;
86  current_cell->score( l_gap );
87  current_cell->next( scores(x-1,y) );
88  current_cell->came_from( left );
89  } else if ( u_gap >= mm && u_gap >= l_gap && u_gap >= threshold ) {
90  //std::cout << "came from above with a score of " << u_gap << std::endl;
91  current_cell->score( u_gap );
92  current_cell->next( scores(x,y-1) );
93  current_cell->came_from( above );
94  } else {
95  current_cell->score( threshold );
96  current_cell->came_from( end );
97  }
98 
99  if ( current_cell->score() > best_cell->score() )
100  best_cell = current_cell;
101  } // x
102  } // y
103  //std::cout << scores << std::endl;
104 
105  // traceback
106  CellOP current_cell = best_cell;
107  //std::cout << "best_cell = " << best_cell->x() << "," << best_cell->y() << std::endl;
108  SequenceAlignment alignment = traceback(
109  new_seq_x,
110  new_seq_y,
111  scores,
112  best_cell
113  );
114 
115  scores.clear();
116  return alignment;
117 } // align
118 
119 } // sequence
120 } // core