source: trunk/test/alignment_test.cc @ 792

Last change on this file since 792 was 792, checked in by Jari Häkkinen, 16 years ago

Addresses #193. matrix now works as outlined in the ticket
discussion. Added support for const views. Added a clone function that
facilitates resizing of matrices. clone is needed since assignement
operator functionality is changed.

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