cdd9392a7d1c419b70709dbb1e9e70a069937dfc
[mussa.git] / alg / conserved_path.cxx
1 #include "alg/conserved_path.h"
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 }
77
78 void ConservedPath::push_back(const ConservedPath::path_element& element)
79 {
80   track_indexes.push_back(element);
81 }
82
83 void ConservedPath::pop_back()
84 {
85   track_indexes.pop_back();
86 }
87
88 ConservedPath::size_type ConservedPath::size() const
89 {
90   return track_indexes.size();
91 }
92
93 ConservedPath::iterator ConservedPath::begin()
94 {
95   return track_indexes.begin();
96 }
97
98 ConservedPath::const_iterator ConservedPath::begin() const
99 {
100   return track_indexes.begin();
101 }
102
103 ConservedPath::iterator ConservedPath::end()
104 {
105   return track_indexes.end();
106 }
107
108 ConservedPath::const_iterator ConservedPath::end() const
109 {
110   return track_indexes.end();
111 }
112
113 bool ConservedPath::nextTo(const ConservedPath& next) const
114 {
115   if (size() != next.size() ) {
116     throw runtime_error("paths must be the same length");
117   }    
118   ConservedPath::const_iterator this_itor = begin();
119   ConservedPath::const_iterator next_itor = next.begin();
120   for (; this_itor != end(); ++this_itor, ++next_itor)
121   {
122     if ( (*this_itor + 1) != *next_itor )
123       return false;
124   }
125   return true;
126 }
127
128 /////////////////////
129 ExtendedConservedPath::ExtendedConservedPath()
130   : ConservedPath(),
131     window_size(0)
132 {
133 }
134
135 ExtendedConservedPath::ExtendedConservedPath(const ExtendedConservedPath& other)
136   : ConservedPath(other), 
137     window_size(other.window_size)
138 {
139 }
140
141 ExtendedConservedPath::ExtendedConservedPath(int win_size, ConservedPath other)
142   : ConservedPath(other), 
143     window_size(win_size)
144 {
145 }
146
147 ExtendedConservedPath::ExtendedConservedPath(int win_size, 
148                                              double s, 
149                                              ConservedPath::path_type p)
150   : ConservedPath(s, p),
151     window_size(win_size)
152 {
153 }
154
155 ExtendedConservedPath& ExtendedConservedPath::extend(int growth)
156 {
157   window_size += growth;
158   
159   // What does this actually do? Tristan's code was doing this operation
160   // but I don't understand how it properly adjusts reverse compliment 
161   // windows?
162   for(ConservedPath::iterator path_i = begin();
163       path_i != end();
164       ++path_i)
165   {
166     if (*path_i < 0)
167       *path_i += growth;
168   }
169   return *this;
170 }
171
172