1 /***************************************************************************
2 * Copyright (C) 2006 by Michael Andreen *
3 * andreen@student.chalmers.se *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
19 ***************************************************************************/
21 #include "genealgorithms.h"
22 #include "geneorder.h"
31 std::pair<int,int> longestSequences(const GeneOrder& go){
32 vector<vector<int> > v = robinsonSchensted(go);
33 return pair<int,int>(v[0].size(),v.size());
36 std::vector<std::vector<int> > robinsonSchensted(const GeneOrder& go){
37 vector<vector<int> > v;
38 for (GeneOrder::iterator i = go.begin(); i != go.end(); ++i){
41 for (vector<vector<int> >::iterator vs = v.begin();
43 vector<int>::iterator bigger = upper_bound(vs->begin(),vs->end(),n);
44 if ( bigger == vs->end()){
53 v.push_back(vector<int>());
54 v.back().push_back(n);
62 FindP(size_t p) : p(p) {}
63 bool operator()(Interval i){
64 return (i.first == p || i.second == p);
69 size_t countCycles(const GeneOrder& go){
72 vector<Interval> intervals = findIntervals(go);
73 vector<Interval> points = findIntervalsAtPoints(intervals);
74 for (size_t p = 1; p < go.size(); ++p){
75 if (marked.find(p) != marked.end())
77 Interval i = intervals[points[p].first];
78 while (marked.find(p) == marked.end()){
80 if (i == intervals[points[p].first])
81 i = intervals[points[p].second];
83 i = intervals[points[p].first];
95 size_t inversionDistance(const GeneOrder& go){
96 size_t cycles = countCycles(go);
98 return go.size() - 1 - cycles;
110 Gene operator()(Gene x) const{
114 std::vector<Component> findComponents(const GeneOrder& go){
115 vector<Component> components;
116 vector<int> os(go.size()-1);
117 for (size_t i = 0; i < os.size(); ++i)
118 os[i] = (go[i]*go[i+1] > 0 ? sign(go[i]) : 0);
120 Mdir.push(go.size()-1);
128 dir.push_back(go.size()-1);
132 vector<Gene> p(go.list());
133 transform(p.begin(),p.end(),p.begin(),Abs());
134 for (size_t i = 1; i < go.size(); ++i){
138 else while (Mdir.top() < p[i])
140 dir.push_back(Mdir.top());
143 while(p[Sdir.top()] > p[i] || dir[Sdir.top()] < p[i]){
145 os[Sdir.top()] = (os[Sdir.top()] == os[s] ? os[s] : 0);
148 if (go[i] > 0 && dir[i] == dir[s] && static_cast<Gene>(i - s) == p[i] - p[s])
149 components.push_back(Component(p[s],p[i],(s+1 == i ? 0 : os[s])));
154 else while (Mrev.top() > p[i])
156 rev.push_back(Mrev.top());
159 while((p[s] < p[i] || rev[s] > p[i]) && s > 0){
161 os[Srev.top()] *= (os[Srev.top()] == os[s] ? 1 : 0);
164 if (go[i] < 0 && rev[i] == rev[s] && static_cast<Gene>(i - s) == p[s] - p[i])
165 components.push_back(Component(-p[s],-p[i],(s+1 == i ? 0 : os[s])));
184 std::vector<Interval> findIntervals(const GeneOrder& go){
185 const size_t max = go.size();
186 vector<Interval> intervals(go.size()-1,Interval(max,max,false));
188 const GeneOrder::iterator end = go.end();
189 for (GeneOrder::iterator g = go.begin(); g != end; ++g, ++n){
192 Interval& curr = intervals[i];
193 curr.first = n + (*g >= 0 ? 1 : 0);
195 if (curr.second == max)
196 curr.oriented = *g < 0;
198 curr.oriented ^= *g < 0;
201 Interval& prev = intervals[i-1];
202 prev.second = n + (*g < 0 ? 1 : 0);
204 if (prev.first == max)
205 prev.oriented = *g < 0;
207 prev.oriented ^= *g < 0;
217 std::vector<Interval> findIntervalsAtPoints(const vector<Interval>& intervals){
218 size_t max = intervals.size()+1;
219 vector<Interval> points(max,Interval(max,max,false));
221 for (vector<Interval>::const_iterator i = intervals.begin(); i != intervals.end(); ++i, ++n){
222 if (points[i->first].first == max){
223 points[i->first].first = n;
225 points[i->first].second = n;
227 if (points[i->second].first == max){
228 points[i->second].first = n;
230 points[i->second].second = n;