Mussa aligned sequence view seems to work
[mussa.git] / alg / conserved_path.cpp
1 #include "alg/conserved_path.hpp"
2 #include <stdexcept>
3
4 using namespace std;
5
6 /////////////////////
7
8 ConservedPath::ConservedPath()
9   : score(0.0),
10     track_indexes()
11 {
12 }
13
14 ConservedPath::ConservedPath(const ConservedPath::ConservedPath& other)
15   : score(other.score),
16     track_indexes(other.track_indexes)
17 {
18 }
19
20 ConservedPath::ConservedPath(double s, ConservedPath::path_type path)
21   : score(s),
22     track_indexes(path)
23 {
24 }
25
26 void ConservedPath::clear()
27 {
28   score = 0.0;
29   track_indexes.clear();
30 }
31
32 ConservedPath::path_element&
33 ConservedPath::operator[](ConservedPath::size_type index)
34 {
35   return track_indexes[index];
36 }
37
38 bool operator==(const ConservedPath::ConservedPath& a,
39                 const ConservedPath::ConservedPath& b)
40 {
41   // this isn't good as score is now a double
42   return (     (a.score == b.score)
43            and (a.track_indexes.size() == b.track_indexes.size())
44            and (a.track_indexes == b.track_indexes));
45 }
46
47 bool operator!=(const ConservedPath::ConservedPath& a,
48                 const ConservedPath::ConservedPath& b)
49 {
50   return not (a == b);
51 }
52
53 std::ostream& operator<<(std::ostream& out, const ConservedPath& path)
54 {
55   out << "<path score=" << path.score 
56       << " len=" << path.track_indexes.size();
57   if (path.track_indexes.size() > 0)
58   {
59     // print the first element
60     ConservedPath::const_iterator itor = path.begin();
61     out << ">" << *itor++;
62     // print the remaining elements
63     for (;
64         itor != path.end();
65         ++itor)
66     {
67       out << ", " << *itor;
68     }
69     out << "</path>";
70   }
71   else
72   {
73     // if we had no elements just close the block
74     out << "/>";
75   }
76   return out;
77 }
78
79 void ConservedPath::push_back(const ConservedPath::path_element& element)
80 {
81   track_indexes.push_back(element);
82 }
83
84 void ConservedPath::pop_back()
85 {
86   track_indexes.pop_back();
87 }
88
89 ConservedPath::size_type ConservedPath::size() const
90 {
91   return track_indexes.size();
92 }
93
94 ConservedPath::iterator ConservedPath::begin()
95 {
96   return track_indexes.begin();
97 }
98
99 ConservedPath::const_iterator ConservedPath::begin() const
100 {
101   return track_indexes.begin();
102 }
103
104 ConservedPath::iterator ConservedPath::end()
105 {
106   return track_indexes.end();
107 }
108
109 ConservedPath::const_iterator ConservedPath::end() const
110 {
111   return track_indexes.end();
112 }
113
114 bool ConservedPath::nextTo(const ConservedPath& next) const
115 {
116   if (size() != next.size() ) {
117     throw runtime_error("paths must be the same length");
118   }    
119   ConservedPath::const_iterator this_itor = begin();
120   ConservedPath::const_iterator next_itor = next.begin();
121   for (; this_itor != end(); ++this_itor, ++next_itor)
122   {
123     if ( (*this_itor + 1) != *next_itor )
124       return false;
125   }
126   return true;
127 }
128
129 vector<bool> ConservedPath::reverseComplimented() const
130 {
131   vector<bool> reversed;
132   for (ConservedPath::const_iterator this_itor = begin(); 
133        this_itor != end(); 
134        ++this_itor)
135   {
136     if (*this_itor < 0) 
137       reversed.push_back(true);
138     else
139       reversed.push_back(false);
140   }
141   return reversed;
142
143 }
144
145 std::vector<ConservedPath::path_element> ConservedPath::normalizedIndexes() const
146 {
147   vector<path_element> paths;
148   for (ConservedPath::const_iterator this_itor = begin(); 
149        this_itor != end(); 
150        ++this_itor)
151   {
152     if (*this_itor < 0) {
153       paths.push_back(-*this_itor);
154     } else {
155       paths.push_back(*this_itor);
156     }
157   }
158   return paths;
159 }
160
161
162 /////////////////////
163 ExtendedConservedPath::ExtendedConservedPath()
164   : ConservedPath(),
165     window_size(0)
166 {
167 }
168
169 ExtendedConservedPath::ExtendedConservedPath(const ExtendedConservedPath& other)
170   : ConservedPath(other), 
171     window_size(other.window_size)
172 {
173 }
174
175 ExtendedConservedPath::ExtendedConservedPath(int win_size, ConservedPath other)
176   : ConservedPath(other), 
177     window_size(win_size)
178 {
179 }
180
181 ExtendedConservedPath::ExtendedConservedPath(int win_size, 
182                                              double s, 
183                                              ConservedPath::path_type p)
184   : ConservedPath(s, p),
185     window_size(win_size)
186 {
187 }
188
189 std::vector<ConservedPath::path_element> ExtendedConservedPath::normalizedIndexes() const
190 {
191   vector<path_element> paths;
192   for (ConservedPath::const_iterator this_itor = begin(); 
193        this_itor != end(); 
194        ++this_itor)
195   {
196     if (*this_itor < 0) {
197       paths.push_back(-(*this_itor) - window_size);
198     } else {
199       paths.push_back((*this_itor));
200     }
201   }
202   return paths;
203 }
204
205 ExtendedConservedPath& ExtendedConservedPath::extend(int growth)
206 {
207   window_size += growth;
208   
209   // What does this actually do? Tristan's code was doing this operation
210   // but I don't understand how it properly adjusts reverse compliment 
211   // windows?
212   for(ConservedPath::iterator path_i = begin();
213       path_i != end();
214       ++path_i)
215   {
216     if (*path_i < 0)
217       *path_i += growth;
218   }
219   return *this;
220 }
221
222 std::ostream& operator<<(std::ostream& out, const ExtendedConservedPath& path)
223 {
224   out << "<extended_path size=" << path.window_size;
225   out << (ConservedPath&)path;
226   return out;
227 }
228