Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NWAligner.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 NWAligner.cc
11 /// @brief class definition for a class that aligns two Sequence objects using
12 /// the Needleman-Wunsch alignment algorithm.
13 /// @author James Thompson
14 
15 #include <core/types.hh>
16 
17 #include <core/sequence/Aligner.hh>
21 
25 
26 // AUTO-REMOVED #include <iostream>
27 #include <string>
28 
29 #include <utility/vector1.hh>
30 
31 
32 namespace core {
33 namespace sequence {
34 
36  Size y_len,
37  Size x_len,
38  ScoringSchemeOP ss,
39  DP_Matrix & scores
40 ) {
41  // matrix initialization
42  for ( Size y = 1; y <= y_len; ++y ) {
43  if ( y == 1 ) {
44  scores(1,y)->score( 0.0 );
45  scores(1,y)->came_from( end );
46  } else {
47  scores(1,y)->score( ss->gap_open() + (y-2) * ss->gap_extend() );
48  scores(1,y)->came_from( end );
49  scores(1,y)->next( scores(1,y-1) );
50  }
51  } // for y
52 
53  for ( Size x = 1; x <= x_len; ++x ) {
54  if ( x == 1 ) {
55  scores(x,1)->score( 0.0 );
56  scores(x,1)->came_from( end );
57  } else {
58  scores(x,1)->score( ss->gap_open() + (x-2) * ss->gap_extend() );
59  scores(x,1)->came_from( end );
60  scores(x,1)->next( scores(x-1,1) );
61  }
62  } // for x
63 } // init_matrix
64 
66  SequenceOP seq_y,
67  SequenceOP seq_x,
69 ) {
70  validate_input( seq_y, seq_x, ss );
71 
72  // replace these with clones, fix up match/mismatch calculation
73  SequenceOP new_seq_x = seq_x->clone();
74  SequenceOP new_seq_y = seq_y->clone();
75 
76  new_seq_x->insert_gap(0);
77  new_seq_y->insert_gap(0);
78 
79  //std::cout << "originals are: " << (*seq_x) << std::endl << (*seq_y) << std::endl;
80  //std::cout << "copies are: " << (*new_seq_x) << std::endl << (*new_seq_y) << std::endl;
81 
82  DP_Matrix scores( new_seq_x->length(), new_seq_y->length() );
83  //scores.xlab( new_seq_x->sequence_vec() );
84  //scores.ylab( new_seq_y->sequence_vec() );
85 
86  // std::cout << scores << std::endl;
87  init_matrix( new_seq_y->length(), new_seq_x->length(), ss, scores );
88 
89  // std::cout << seq_x << std::endl << seq_y << std::endl;
90  // std::cout << scores << std::endl;
91 
92  CellOP best_cell( new Cell( 0.0 ) );
93  for ( Size y = 2; y <= new_seq_y->length(); ++y ) {
94  for ( Size x = 2; x <= new_seq_x->length(); ++x ) {
95 
96  // score this position
97  Real l_gap_penalty( ss->gap_open() ), u_gap_penalty( ss->gap_open() );
98  if ( scores(x,y-1)->came_from() == above ) u_gap_penalty = ss->gap_extend();
99  if ( scores(x-1,y)->came_from() == left ) l_gap_penalty = ss->gap_extend();
100 
101  Real u_gap = scores( x, y-1 )->score() + u_gap_penalty;
102  Real l_gap = scores( x-1, y )->score() + l_gap_penalty;
103  Real mm = scores( x-1, y-1 )->score() + ss->score( new_seq_x, new_seq_y, x, y );
104 
105  // set up pointers properly
106  CellOP current_cell = scores(x,y);
107  if ( mm >= l_gap && mm >= u_gap ) {
108  // std::cout << "came from diagonal with a score of " << mm << std::endl;
109  current_cell->score( mm );
110  current_cell->next( scores(x-1,y-1) );
111  current_cell->came_from( diagonal );
112  } else if ( l_gap >= mm && l_gap >= u_gap ) {
113  // std::cout << "came from left with a score of " << l_gap << std::endl;
114  current_cell->score( l_gap );
115  current_cell->next( scores(x-1,y) );
116  current_cell->came_from( left );
117  } else { // if ( u_gap >= mm && u_gap >= l_gap ) {
118  // std::cout << "came from above with a score of " << u_gap << std::endl;
119  current_cell->score( u_gap );
120  current_cell->next( scores(x,y-1) );
121  current_cell->came_from( above );
122  }
123  // if ( current_cell->score() > best_cell->score() ) best_cell = current_cell;
124  } // x
125  } // y
126 
127  best_cell = scores( scores.rows(),scores.cols() ); // start in lower right-hand corner for NW
128 
130  new_seq_x,
131  new_seq_y,
132  scores,
133  best_cell
134  );
135 
136  scores.clear();
137 
138  return aln;
139 } // align
140 
141 } // sequence
142 } // core