source: trunk/test/alignment_test.cc @ 831

Last change on this file since 831 was 831, checked in by Peter, 16 years ago

Refs #185.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: alignment_test.cc 831 2007-03-27 13:22:09Z peter $
2
3/*
4  Copyright (C) 2005 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2006, 2007 Jari Häkkinen
6
7  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "yat/utility/Alignment.h"
26
27#include "yat/utility/matrix.h"
28
29#include <gsl/gsl_cdf.h>
30
31#include <cassert>
32#include <cmath>
33#include <fstream>
34#include <iostream>
35#include <sstream>
36#include <string>
37#include <vector>
38
39using namespace theplu::yat;
40
41void read_vector(std::vector<double>& v,std::string& str)
42{
43  std::istringstream s(str);
44  while (!s.eof()) {
45    double d;
46    s >> d;
47    if (!s.fail())
48      v.push_back(d);
49  }
50}
51
52std::istream& operator>>(std::istream& s,
53                         std::vector<std::vector<double> >& m)
54{
55  while (!s.eof()) {
56    std::string row;
57    char ch;
58    while (((ch=s.get())!='\n') && s.good())
59      row+=ch;
60    if (row.size() || s.good()) { // if stream good, read an empty peak set
61      std::vector<double> v;
62      read_vector(v,row);
63      m.push_back(v);
64    }
65  }
66  return s;
67}
68
69double match(const double x, const double y, const double s)
70{ 
71  return 2*gsl_cdf_gaussian_Q(fabs(x-y),sqrt(2)*s); 
72}
73 
74
75double score(const std::vector<double>& l1,
76             const std::vector<double>& l2,
77             const double sigma, 
78             theplu::yat::utility::matrix& dot_matrix,
79             std::vector<std::pair<size_t,size_t> >& path)
80{
81  dot_matrix.clone(theplu::yat::utility::matrix(l1.size(),l2.size()));
82  for (size_t i=0; i<l1.size(); i++) 
83    for (size_t j=0; j<l2.size(); j++) {
84      assert(i<dot_matrix.rows());
85      assert(j<dot_matrix.columns());
86      dot_matrix(i,j)=match(l1[i],l2[j],sigma);
87    }
88  return theplu::yat::utility::NeedlemanWunsch(dot_matrix,path,0);
89}
90
91
92int main()
93{
94  bool ok=true;
95
96  std::ifstream s(std::string("data/isoform.peaks").c_str());
97  std::vector<std::vector<double> > peaksets;
98  s >> peaksets;
99
100  for (size_t i=0; i<peaksets.size()-1; i++) 
101    for (size_t j=i+1; j<peaksets.size(); j++) {
102      utility::matrix dot_m;
103      std::vector<std::pair<size_t,size_t> > path;
104      score(peaksets[i], peaksets[j], 1.0, dot_m, path);
105    }
106
107
108  if (ok) {
109    return 0;
110  }
111  return -1;
112
113}
Note: See TracBrowser for help on using the repository browser.