Debian Codegraph @

src/utils/ranker.h

raw
1 /*
2 FastQTL: Fast and efficient QTL mapper for molecular phenotypes
3 Copyright (C) 2015 Olivier DELANEAU, Alfonso BUIL, Emmanouil DERMITZAKIS & Olivier DELANEAU
4
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 3 of the License, or
8 (at your option) any later version.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef RANKER_H
20 #define RANKER_H
21
22
23 #include <vector>
24 #include <string>
25 #include <algorithm>
26
27 using std::vector;
28 using std::string;
29
30 #ifndef uint
31 typedef unsigned int uint;
32 #endif
33
34 namespace myranker{
35 template <class T>
36 class lt { public: static int compare(T a, T b) { return(a < b); } };
37 template <class T>
38 class gt { public: static int compare(T a, T b) { return(a > b); } };
39
40 template <class T, class C>
41 class ranker
42 {
43 private:
44 const T* p;
45 uint sz;
46
47 public:
48 ranker(const vector<T>& v) : p(&v[0]), sz(v.size()) { }
49 ranker(const T* tp, uint s) : p(tp), sz(s) { }
50
51 int operator()(uint i1, uint i2) const { return(C::compare(p[i1],p[i2])); }
52
53 template <class S>
54 void get_orders(vector<S>& w) const {
55 w.resize(sz);
56 w.front() = 0;
57 for (typename vector<S>::iterator i = w.begin(); i != w.end() - 1; ++i)
58 *(i + 1) = *i + 1;
59 std::sort(w.begin(), w.end(), *this);
60 }
61
62 template <class S>
63 void get_partial_orders(vector<S>& w, uint num) const {
64 if (num > sz) num = sz;
65 w.resize(sz);
66 w.front() = 0;
67 for (typename vector<S>::iterator i = w.begin(); i != w.end() - 1; ++i)
68 *(i + 1) = *i + 1;
69 std::partial_sort(w.begin(), w.begin() + num, w.end(), *this);
70 w.resize(num);
71 }
72
73 template <class S>
74 void get_ranks(vector<S>& w, const string& method) const {
75 w.resize(sz);
76 vector<uint> tmp(w.size());
77 get_orders(tmp);
78 if (method == "average") {
79 for (uint c = 0, reps; c < w.size(); c += reps) {
80 reps = 1;
81 while (c + reps < w.size() && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
82 for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = S(2 * c + reps - 1) / 2 + 1;
83 }
84 } else if (method == "min") {
85 for (uint c = 0, reps; c < w.size(); c += reps) {
86 reps = 1;
87 while (c + reps < w.size() && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
88 for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + 1;
89 }
90 } else if (method == "max") {
91 for (uint c = 0, reps; c < w.size(); c += reps) {
92 reps = 1;
93 while (c + reps < w.size() && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
94 for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + reps;
95 }
96 } else // default
97 for (uint c = 0; c < w.size(); ++c) w[tmp[c]] = c + 1;
98 }
99
100 template <class S>
101 void get_partial_ranks(vector<S>& w, const string& method, size_t num) const {
102 if (num > sz) num = sz;
103 vector<uint> tmp(sz);
104 get_partial_orders(tmp, num);
105 w.resize(sz);
106 fill(w.begin(), w.end(), 0);
107 if (method == "average") {
108 for (uint c = 0, reps; c < num; c += reps) { reps = 1;
109 while (c + reps < num && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
110 for (uint k = 0; k < reps; ++k)
111 w[tmp[c + k]] = S(2 * c + reps - 1) / 2 + 1;
112 }
113 } else if (method == "min") {
114 for (uint c = 0, reps; c < num; c += reps) { reps = 1;
115 while (c + reps < num && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
116 for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + 1;
117 }
118 } else if (method == "max") {
119 for (uint c = 0, reps; c < num; c += reps) { reps = 1;
120 while (c + reps < num && p[tmp[c]] == p[tmp[c + reps]]) ++reps;
121 for (uint k = 0; k < reps; ++k) w[tmp[c + k]] = c + reps;
122 }
123 } else // default
124 for (uint c = 0; c < num; ++c) w[tmp[c]] = c + 1;
125 }
126
127 };
128
129 template <class T, class S>
130 inline void rank(const vector<T>& v, vector<S>& w,
131 const string& method = "average")
132 { ranker<T, lt<T> > r(v); r.get_ranks(w, method); }
133
134 template <class T, class S>
135 inline void rank(const T* d, uint size, vector<S>& w,
136 const string& method = "average")
137 { ranker<T, lt<T> > r(d, size); r.get_ranks(w, method); }
138
139 template <class T, class S>
140 inline void partial_rank(const vector<T>& v, vector<S>& w, uint num,
141 const string& method = "average")
142 { ranker<T, lt<T> > r(v); r.get_partial_ranks(w, method, num); }
143
144 template <class T, class S>
145 inline void partial_rank(const T* d, uint size, vector<S>& w, uint num,
146 const string& method = "average")
147 { ranker<T, lt<T> > r(d, size); r.get_partial_ranks(w, method, num); }
148
149 template <class T, class S>
150 inline void order(const vector<T>& v, vector<S>& w)
151 { ranker<T, lt<T> > r(v); r.get_orders(w); }
152
153 template <class T, class S>
154 inline void order(const T* d, uint size, vector<S>& w)
155 { ranker<T, lt<T> > r(d, size); r.get_orders(w); }
156
157 template <class T, class S>
158 inline void partial_order(const vector<T>& v, vector<S>& w, uint num)
159 { ranker<T, lt<T> > r(v); r.get_partial_orders(w, num); }
160
161 template <class T, class S>
162 inline void partial_order(const T* d, uint size, vector<S>& w, uint num)
163 { ranker<T, lt<T> > r(d, size); r.get_partial_orders(w, num); }
164
165 template <class T, class S>
166 inline void rankhigh(const vector<T>& v, vector<S>& w,
167 const string& method = "average")
168 { ranker<T, gt<T> > r(v); r.get_ranks(w, method); }
169
170 template <class T, class S>
171 inline void rankhigh(const T* d, uint size, vector<S>& w,
172 const string& method = "average")
173 { ranker<T, gt<T> > r(d, size); r.get_ranks(w, method); }
174
175 template <class T, class S>
176 inline void partial_rankhigh(const vector<T>& v, vector<S>& w, uint num,
177 const string& method = "average")
178 { ranker<T, gt<T> > r(v); r.get_partial_ranks(w, method, num); }
179
180 template <class T, class S>
181 inline void partial_rankhigh(const T* d, uint size, vector<S>& w, uint num,
182 const string& method = "average")
183 { ranker<T, gt<T> > r(d, size); r.get_partial_ranks(w, method, num); }
184
185 template <class T, class S>
186 inline void orderhigh(const vector<T>& v, vector<S>& w)
187 { ranker<T, gt<T> > r(v); r.get_orders(w); }
188
189 template <class T, class S>
190 inline void orderhigh(const T* d, uint size, vector<S>& w)
191 { ranker<T, gt<T> > r(d, size); r.get_orders(w); }
192
193 template <class T, class S>
194 inline void partial_orderhigh(const vector<T>& v, vector<S>& w, uint num)
195 { ranker<T, gt<T> > r(v); r.get_partial_orders(w, num); }
196
197 template <class T, class S>
198 inline void partial_orderhigh(const T* d, uint size, vector<S>& w, uint num)
199 { ranker<T, gt<T> > r(d, size); r.get_partial_orders(w, num); }
200
201 template <class T>
202 inline T quantile(const T* d, const uint size, const double q)
203 {
204 if (size == 0) return T(0);
205 if (size == 1) return d[0];
206 if (q <= 0) return *std::min_element(d, d + size);
207 if (q >= 1) return *std::max_element(d, d + size);
208
209 double pos = (size - 1) * q;
210 uint ind = uint(pos);
211 double delta = pos - ind;
212 vector<T> w(size); std::copy(d, d + size, w.begin());
213 std::nth_element(w.begin(), w.begin() + ind, w.end());
214 T i1 = *(w.begin() + ind);
215 T i2 = *std::min_element(w.begin() + ind + 1, w.end());
216 return i1 * (1.0 - delta) + i2 * delta;
217 }
218
219 template <class T>
220 inline T quantile(const vector<T>& v, const double q)
221 { return quantile(&v[0], v.size(), q); }
222 };
223
224 #endif