1 | // $Id: alignment_test.cc 869 2007-09-14 18:59:46Z 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 | |
---|
39 | using namespace theplu::yat; |
---|
40 | |
---|
41 | void 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 | |
---|
52 | std::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 | |
---|
69 | double 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 | |
---|
75 | double 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 | |
---|
92 | int 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 | // testing ssearch |
---|
117 | /* some strange lookup problem with ssearch |
---|
118 | |
---|
119 | if (utility::ssearch("Hello", "Hll", 0.0, 1.0)!=2){ |
---|
120 | *error << "aligning 'Hello' and 'Hll' gives score " |
---|
121 | << utility::ssearch("Hello", "Hll", 0.0, 1.0) |
---|
122 | << " expected " << 2 << std::endl; |
---|
123 | ok=false; |
---|
124 | } |
---|
125 | if (utility::ssearch("Hello", "Peter said you can't say 'allo", 1, 1)!=3) |
---|
126 | ok=false; |
---|
127 | */ |
---|
128 | if (ok) { |
---|
129 | return 0; |
---|
130 | } |
---|
131 | return -1; |
---|
132 | |
---|
133 | } |
---|