46 #include "../utils/Matrix.hpp"
47 #include "../utils/ThreadPool.hpp"
56 typedef std::pair<unsigned int, unsigned int>
Match;
76 Match_point(
unsigned int x_,
unsigned int y_,
float score_):x(x_),y(y_),score(score_)
111 template <
typename DataType>
117 const DataType &_data;
122 std::auto_ptr<std::vector<std::vector<Match_point> > > _pairs;
128 std::vector<std::pair<int,int> > used;
129 std::vector<int> n_hits;
131 RelaxHelper(
size_t max_length): final_mat(
Matrix<float>(max_length, max_length)), tmp_mat(
Matrix<float>(max_length, max_length*2)), used(max_length*max_length), n_hits(std::vector<int>(max_length*2, 0))
137 template <
typename RelaxFunc>
139 _relax_pair(
size_t i, std::vector<std::vector<Match_point> > *new_vals, RelaxFunc relax_function, RelaxHelper &helper);
175 void add(
unsigned int id1,
unsigned int id2,
unsigned int pos1,
unsigned int pos2,
float score=1.0);
183 void get(
size_t id1,
size_t id2, std::map<Match, int> &match_points)
const;
199 template<
typename GapFunc>
200 void get(
const std::vector<size_t> &ids1,
const std::vector<size_t> &ids2, std::map<Match, int> &match_points,
const GapFunc gap_func)
const;
224 template <
typename RelaxFunc>
226 relax(RelaxFunc relax_function);
228 template<
typename RelaxFunc>
230 relax(RelaxFunc relax_function,
int n_threads);
241 for (i=0; i<_n_elems; ++i)
243 for (j=i+1; j<_n_elems; ++j)
245 printf(
"%i %i\n", i, j);
246 pos= i*_n_elems-(i*i+i)/2 + j -i-1;
247 std::vector<Match_point> &matches=(*_pairs)[pos];
248 for (k=0; k<matches.size(); ++k)
249 printf(
"(%i %i %f) ", matches[k].x, matches[k].y, matches[k].score);
260 template<
typename DataType>
263 for (
size_t i = 0; i<_n_elems; ++i)
265 if (
set[i].length() > _max_length)
266 _max_length =
set[i].length();
271 template<
typename DataType>
277 template<
typename DataType>
288 id = id1*_n_elems - (id1*(id1+1)/2) +id2-id1-1;
289 (*_pairs)[id].push_back(
Match_point(pos1, pos2, score));
293 id = id2*_n_elems - (id2*(id2+1)/2) +id1-id2-1;
294 (*_pairs)[id].push_back(
Match_point(pos2, pos1, score));
298 template<
typename DataType>
304 id = id1*_n_elems - (id1*(id1+1)/2) +id2-id1-1;
306 id = id2*_n_elems - (id2*(id2+1)/2) +id1-id2-1;
307 (*_pairs)[id].reserve((*_pairs)[
id].size()+n_entries);
310 template<
typename DataType>
317 id = id1*_n_elems - (id1*(id1+1)/2) +id2-id1-1;
318 std::vector<Match_point> &matches=(*_pairs)[id];
319 size_t n_matches=matches.size();
320 for (
size_t i=0; i<n_matches; ++i)
321 match_points[
Match(matches[i].x, matches[i].y)] = matches[i].score;
325 id = id2*_n_elems - (id2*(id2+1)/2) +id1-id2-1;
326 std::vector<Match_point> &matches=(*_pairs)[id];
327 size_t n_matches=matches.size();
328 for (
size_t i=0; i<n_matches; ++i)
329 match_points[
Match(matches[i].y, matches[i].x)] = matches[i].score;
335 template<
typename DataType>
336 template<
typename GapFunc>
337 void Library<DataType>::get(
const std::vector<size_t> &ids1,
const std::vector<size_t> &ids2, std::map<Match, int> &match_points,
const GapFunc gap_func)
const
339 match_points.clear();
340 size_t n_ids1=ids1.size();
341 size_t n_ids2=ids2.size();
342 size_t n_elems1=_data[ids1[0]].length();
343 size_t n_elems2=_data[ids2[0]].length();
345 std::map<Match, int> tmp_match_points;
346 std::map<Match, int>::iterator it, it_end;
347 std::vector<unsigned int> convert1, convert2;
348 convert1.resize(n_elems1);
349 convert2.resize(n_elems2);
350 std::pair<std::map<Match, int>::iterator,
bool> ret;
351 for (i=0; i<n_ids1; ++i)
354 for (k=0; k<n_elems1; ++k)
356 if (!gap_func(_data[ids1[i]],k))
360 for (j=0; j<n_ids2; ++j)
363 for (k=0; k<n_elems2; ++k)
365 if (!gap_func(_data[ids2[j]],k))
369 get(_data[ids1[i]].id(), _data[ids2[j]].id(), tmp_match_points);
370 it_end=tmp_match_points.end();
371 for (it=tmp_match_points.begin(); it!=it_end; ++it)
373 Match tmp_m(convert1[it->first.first], convert2[it->first.second]);
374 ret=match_points.insert(std::pair<Match, int>(tmp_m, it->second));
376 ret.first->second+=it->second;
381 it_end = match_points.end();
382 size_t overall=n_ids1*n_ids2;
383 for (it = match_points.begin(); it != it_end; ++it )
390 template<
typename DataType>
391 template<
typename RelaxFunc>
397 relax(relax_function);
401 std::vector<std::vector<Match_point> > *new_vals =
new std::vector<std::vector<Match_point> >();
402 new_vals->resize(_n_pairs);
404 for (i=0; i<_n_elems; ++i)
405 pool.addTask(&Library<DataType>::_relax_pair<RelaxFunc>,
this, i, new_vals, relax_function);
407 _pairs.reset(new_vals);
412 template<
typename DataType>
413 template<
typename RelaxFunc>
417 std::vector<std::vector<Match_point> > *new_vals =
new std::vector<std::vector<Match_point> >();
418 new_vals->resize(_n_pairs);
421 std::vector<std::pair<int,int> > used;
422 used.resize(_max_length*_max_length);
423 std::vector<int> n_hits(_max_length*2, 0);
426 std::vector<Match_point>::const_iterator it,it_end;
428 size_t i,j,k,m,id, current_pair_id, pos;
430 for (i=0; i<_n_elems; ++i)
432 for (j=i+1; j<_n_elems; ++j)
434 current_pair_id=i*_n_elems - (i*(i+1)/2) +j-i-1;
435 it_end=(*_pairs)[current_pair_id].end();
437 for (it=(*_pairs)[current_pair_id].begin(); it!=it_end; ++it)
439 final_mat[it->x][it->y]+=2*it->score;
440 used[use].first=it->x;
441 used[use].second=it->y;
445 for (k=0; k<_n_elems; ++k)
447 if ((k==i) || (k==j))
453 id=k*_n_elems - (k*(k+1)/2) +i-k-1;
454 it=(*_pairs)[id].begin();
455 it_end=(*_pairs)[id].end();
456 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
460 tmp_mat[it->x][pos]=it->y;
461 tmp_mat[it->x][++pos]=it->score;
466 id=i*_n_elems - (i*(i+1)/2) +k-i-1;
467 it=(*_pairs)[id].begin();
468 it_end=(*_pairs)[id].end();
469 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
473 tmp_mat[it->y][pos]=it->x;
474 tmp_mat[it->y][++pos]=it->score;
479 id=k*_n_elems - (k*(k+1)/2) +j-k-1;
480 it=(*_pairs)[id].begin();
481 it_end=(*_pairs)[id].end();
482 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
483 for(l=0; l<n_hits[it->x]; ++l)
484 if (final_mat[tmp_mat[it->x][l*2]][it->y])
485 final_mat[tmp_mat[it->x][l*2]][it->y]+= relax_function(tmp_mat[it->x][l*2+1], it->score);
496 id=j*_n_elems - (j*(j+1)/2) +k-j-1;
497 it=(*_pairs)[id].begin();
498 it_end=(*_pairs)[id].end();
499 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
500 for(l=0; l<n_hits[it->y]; ++l)
501 if (final_mat[tmp_mat[it->y][l*2]][it->x])
502 final_mat[tmp_mat[it->y][l*2]][it->x]+= relax_function(tmp_mat[it->y][l*2+1], it->score);
515 id=k*_n_elems - (k*(k+1)/2) +i-k-1;
516 it=(*_pairs)[id].begin();
517 it_end=(*_pairs)[id].end();
518 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
522 tmp_mat[it->x][pos]=it->y;
523 tmp_mat[it->x][++pos]=0;
528 id=i*_n_elems - (i*(i+1)/2) +k-i-1;
529 it=(*_pairs)[id].begin();
530 it_end=(*_pairs)[id].end();
531 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
535 tmp_mat[it->y][pos]=it->x;
536 tmp_mat[it->y][++pos]=0;
539 for (m=0; m<2*_max_length; ++m)
543 for (k=0; k<use; ++k)
545 (*new_vals)[current_pair_id].push_back(
Match_point(used[k].first, used[k].second, (final_mat[used[k].first][used[k].second])));
546 final_mat[used[k].first][used[k].second] = 0;
551 _pairs.reset(new_vals);
555 template<
typename DataType>
556 template<
typename RelaxFunc>
562 std::vector<std::pair<int,int> > used = helper.used;
563 std::vector<int> &n_hits = helper.n_hits;
565 std::vector<Match_point>::const_iterator it,it_end;
566 size_t k,m,id, current_pair_id, pos;
568 for (
size_t j=i+1; j<_n_elems; ++j)
570 current_pair_id=i*_n_elems - (i*(i+1)/2) +j-i-1;
571 it_end=(*_pairs)[current_pair_id].end();
573 for (it=(*_pairs)[current_pair_id].begin(); it!=it_end; ++it)
575 final_mat[it->x][it->y]+=2*it->score;
576 used[use].first=it->x;
577 used[use].second=it->y;
581 for (k=0; k<_n_elems; ++k)
583 if ((k==i) || (k==j))
589 id=k*_n_elems - (k*(k+1)/2) +i-k-1;
590 it=(*_pairs)[id].begin();
591 it_end=(*_pairs)[id].end();
592 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
596 tmp_mat[it->x][pos]=it->y;
597 tmp_mat[it->x][++pos]=it->score;
602 id=i*_n_elems - (i*(i+1)/2) +k-i-1;
603 it=(*_pairs)[id].begin();
604 it_end=(*_pairs)[id].end();
605 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
609 tmp_mat[it->y][pos]=it->x;
610 tmp_mat[it->y][++pos]=it->score;
615 id=k*_n_elems - (k*(k+1)/2) +j-k-1;
616 it=(*_pairs)[id].begin();
617 it_end=(*_pairs)[id].end();
618 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
619 for(l=0; l<n_hits[it->x]; ++l)
620 if (final_mat[tmp_mat[it->x][l*2]][it->y])
621 final_mat[tmp_mat[it->x][l*2]][it->y]+= relax_function(tmp_mat[it->x][l*2+1], it->score);
632 id=j*_n_elems - (j*(j+1)/2) +k-j-1;
633 it=(*_pairs)[id].begin();
634 it_end=(*_pairs)[id].end();
635 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
636 for(l=0; l<n_hits[it->y]; ++l)
637 if (final_mat[tmp_mat[it->y][l*2]][it->x])
638 final_mat[tmp_mat[it->y][l*2]][it->x]+= relax_function(tmp_mat[it->y][l*2+1], it->score);
651 id=k*_n_elems - (k*(k+1)/2) +i-k-1;
652 it=(*_pairs)[id].begin();
653 it_end=(*_pairs)[id].end();
654 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
658 tmp_mat[it->x][pos]=it->y;
659 tmp_mat[it->x][++pos]=0;
664 id=i*_n_elems - (i*(i+1)/2) +k-i-1;
665 it=(*_pairs)[id].begin();
666 it_end=(*_pairs)[id].end();
667 for (it=(*_pairs)[
id].begin(); it!=it_end; ++it)
671 tmp_mat[it->y][pos]=it->x;
672 tmp_mat[it->y][++pos]=0;
675 for (m=0; m<2*_max_length; ++m)
679 for (k=0; k<use; ++k)
681 (*new_vals)[current_pair_id].push_back(Match_point(used[k].first, used[k].second, (final_mat[used[k].first][used[k].second])));
682 final_mat[used[k].first][used[k].second] = 0;