source: trunk/test/alignment_test.cc @ 875

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

rewriting SW algo, making code clearer, and perhaps removing a few bugs...?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1// $Id: alignment_test.cc 875 2007-09-19 00:17:05Z 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://trac.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(const int argc,const char* argv[])
93{
94  bool ok=true;
95  std::ostream* error;
96  if (argc>1 && argv[1]==std::string("-v"))
97    error = &std::cerr;
98  else {
99    error = new std::ofstream("/dev/null");
100    if (argc>1)
101      std::cout << "alignment_test -v : for printing extra " 
102                << "information\n";
103  }
104
105  std::ifstream s(std::string("data/isoform.peaks").c_str());
106  std::vector<std::vector<double> > peaksets;
107  s >> peaksets;
108
109  for (size_t i=0; i<peaksets.size()-1; i++) 
110    for (size_t j=i+1; j<peaksets.size(); j++) {
111      utility::matrix dot_m;
112      std::vector<std::pair<size_t,size_t> > path;
113      score(peaksets[i], peaksets[j], 1.0, dot_m, path);
114    }
115
116  std::string a("AGGUUGUCCGUGGUGAGUUCGCA");
117  std::string b("GAGGUUGUCCGUGGUGAGUUCG");
118  utility::matrix m(a.size(), b.size());
119  for (size_t j=0; j<a.size(); ++j)
120    for (size_t k=0; k<b.size(); ++k)
121      m(j,k) = (a[j]==b[k] ? 1 : -1000);
122  double score=utility::SmithWaterman(m, 100, 100);
123  if (score!=21)
124    ok=false;
125
126  // testing ssearch
127  if (utility::ssearch("Hello", "Hll", 0.0, 1.0)!=2){
128    *error << "aligning 'Hello' and 'Hll' gives score " 
129           << utility::ssearch("Hello", "Hll", 0.0, 1.0)
130           << " expected " << 2 << std::endl;
131    ok=false;
132  }
133  if (utility::ssearch("Hello", "Peter said you can't say 'allo", 1, 1)!=3)
134    ok=false;
135
136  if (ok)
137    *error << "Test is ok." << std::endl;
138  else
139    *error << "Test fails." << std::endl;
140   
141  if (error!=&std::cerr)
142    delete error;
143
144  if (ok) {
145    return 0;
146  }
147  return -1;
148
149}
Note: See TracBrowser for help on using the repository browser.