~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/TEST/ClusterHierarchical_test.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
//
 
14
//  This 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
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Mathias Walzer$
 
25
// $Authors: $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#include <OpenMS/CONCEPT/ClassTest.h>
 
29
 
 
30
///////////////////////////
 
31
#include <OpenMS/COMPARISON/CLUSTERING/ClusterHierarchical.h>
 
32
#include <OpenMS/COMPARISON/CLUSTERING/SingleLinkage.h>
 
33
#include <OpenMS/COMPARISON/SPECTRA/BinnedSpectrum.h>
 
34
#include <OpenMS/COMPARISON/SPECTRA/BinnedSharedPeakCount.h>
 
35
#include <OpenMS/SYSTEM/File.h>
 
36
#include <OpenMS/FORMAT/DTAFile.h>
 
37
 
 
38
#include <vector>
 
39
#include <algorithm>
 
40
///////////////////////////
 
41
 
 
42
using namespace OpenMS;
 
43
using namespace std;
 
44
 
 
45
 
 
46
class LowlevelComparator
 
47
{
 
48
        public:
 
49
        double operator()(const Size first, const Size second) const
 
50
        {
 
51
                Size x,y;
 
52
                x = min(second,first);
 
53
                y = max(first,second);
 
54
 
 
55
                switch (x)
 
56
                {
 
57
                        case 0:
 
58
                                switch(y)
 
59
                                {
 
60
                                        default:
 
61
                                                return 0;
 
62
                                                break;
 
63
                                        case 1:
 
64
                                                return 1-0.5;
 
65
                                                break;
 
66
                                        case 2:
 
67
                                                return 1-0.8;
 
68
                                                break;
 
69
                                        case 3:
 
70
                                                return 1-0.6;
 
71
                                                break;
 
72
                                        case 4:
 
73
                                                return 1-0.8;
 
74
                                                break;
 
75
                                        case 5:
 
76
                                                return 1-0.7;
 
77
                                                break;
 
78
                                }
 
79
                        break;
 
80
                        case 1:
 
81
                                switch(y)
 
82
                                {
 
83
                                        default:
 
84
                                                return 0;
 
85
                                                break;
 
86
                                        case 2:
 
87
                                                return 1-0.3;
 
88
                                                break;
 
89
                                        case 3:
 
90
                                                return 1-0.8;
 
91
                                                break;
 
92
                                        case 4:
 
93
                                                return 1-0.8;
 
94
                                                break;
 
95
                                        case 5:
 
96
                                                return 1-0.8;
 
97
                                                break;
 
98
                                }
 
99
 
 
100
                        break;
 
101
                        case 2:
 
102
                                switch(y)
 
103
                                {
 
104
                                        default:
 
105
                                                return 0;
 
106
                                                break;
 
107
                                        case 3:
 
108
                                                return 1-0.8;
 
109
                                                break;
 
110
                                        case 4:
 
111
                                                return 1-0.8;
 
112
                                                break;
 
113
                                        case 5:
 
114
                                                return 1-0.8;
 
115
                                                break;
 
116
                                }
 
117
 
 
118
                        break;
 
119
                        case 3:
 
120
                                switch(y)
 
121
                                {
 
122
                                        default:
 
123
                                                return 0;
 
124
                                                break;
 
125
                                        case 4:
 
126
                                                return 1-0.4;
 
127
                                                break;
 
128
                                        case 5:
 
129
                                                return 1-0.8;
 
130
                                                break;
 
131
                                }
 
132
 
 
133
                        break;
 
134
                        case 4:
 
135
                                switch(y)
 
136
                                {
 
137
                                        default:
 
138
                                                return 0;
 
139
                                                break;
 
140
                                        case 5:
 
141
                                                return 1-0.8;
 
142
                                                break;
 
143
                                }
 
144
 
 
145
                        break;
 
146
                        default:
 
147
                                return 666;
 
148
                                break;
 
149
 
 
150
                }
 
151
        }
 
152
};
 
153
 
 
154
 
 
155
START_TEST(ClusterHierarchical, "$Id: ClusterHierarchical_test.C 8210 2011-03-28 13:19:52Z aiche $")
 
156
 
 
157
/////////////////////////////////////////////////////////////
 
158
/////////////////////////////////////////////////////////////
 
159
 
 
160
ClusterHierarchical* ptr = 0;
 
161
ClusterHierarchical* nullPointer = 0;
 
162
START_SECTION(ClusterHierarchical())
 
163
{
 
164
        ptr = new ClusterHierarchical();
 
165
        TEST_NOT_EQUAL(ptr, nullPointer)
 
166
}
 
167
END_SECTION
 
168
 
 
169
START_SECTION(~ClusterHierarchical())
 
170
{
 
171
        delete ptr;
 
172
}
 
173
END_SECTION
 
174
 
 
175
START_SECTION((ClusterHierarchical(const ClusterHierarchical &source)))
 
176
{
 
177
        ClusterHierarchical ch;
 
178
        ch.setThreshold(66.6);
 
179
        ClusterHierarchical copy(ch);
 
180
        TEST_EQUAL(copy.getThreshold(), 66.6);
 
181
}
 
182
END_SECTION
 
183
 
 
184
START_SECTION((double getThreshold()))
 
185
{
 
186
        ClusterHierarchical ch;
 
187
        ch.setThreshold(0.666);
 
188
        TEST_EQUAL(ch.getThreshold(),0.666);
 
189
}
 
190
END_SECTION
 
191
 
 
192
START_SECTION((void setThreshold(double x)))
 
193
{
 
194
        ClusterHierarchical ch;
 
195
        ch.setThreshold(0.666);
 
196
        TEST_EQUAL(ch.getThreshold(),0.666);
 
197
}
 
198
END_SECTION
 
199
 
 
200
START_SECTION((template <typename Data, typename SimilarityComparator> void cluster(std::vector< Data > &data, const SimilarityComparator &comparator, const ClusterFunctor &clusterer, std::vector<BinaryTreeNode>& cluster_tree, DistanceMatrix<Real>& original_distance)))
 
201
{
 
202
        vector<Size> d(6,0);
 
203
        for (Size i = 0; i<d.size(); ++i)
 
204
        {
 
205
                d[i]=i;
 
206
        }
 
207
        ClusterHierarchical ch;
 
208
        LowlevelComparator lc;
 
209
        SingleLinkage sl;
 
210
        vector< BinaryTreeNode > result;
 
211
        vector< BinaryTreeNode > tree;
 
212
        tree.push_back(BinaryTreeNode(1,2,0.3f));
 
213
        tree.push_back(BinaryTreeNode(3,4,0.4f));
 
214
        tree.push_back(BinaryTreeNode(0,1,0.5f));
 
215
        tree.push_back(BinaryTreeNode(0,3,0.6f));
 
216
        tree.push_back(BinaryTreeNode(0,5,0.7f));
 
217
        DistanceMatrix<Real> matrix;
 
218
 
 
219
        ch.cluster<Size,LowlevelComparator>(d,lc,sl,result, matrix);
 
220
 
 
221
        TEST_EQUAL(tree.size(), result.size());
 
222
        for (Size i = 0; i < tree.size(); ++i)
 
223
        {
 
224
                        TOLERANCE_ABSOLUTE(0.0001);
 
225
                        TEST_EQUAL(tree[i].left_child, result[i].left_child);
 
226
                        TEST_EQUAL(tree[i].right_child, result[i].right_child);
 
227
                        TEST_REAL_SIMILAR(tree[i].distance, result[i].distance);
 
228
        }
 
229
}
 
230
END_SECTION
 
231
 
 
232
START_SECTION((void cluster(std::vector<PeakSpectrum>& data, const BinnedSpectrumCompareFunctor& comparator, double sz, UInt sp, const ClusterFunctor& clusterer, std::vector<BinaryTreeNode>& cluster_tree, DistanceMatrix<Real>& original_distance)))
 
233
{
 
234
 
 
235
        PeakSpectrum s1, s2, s3;
 
236
        Peak1D peak;
 
237
 
 
238
        DTAFile().load(OPENMS_GET_TEST_DATA_PATH("PILISSequenceDB_DFPIANGER_1.dta"), s1);
 
239
        s2 = s1;
 
240
        s3 = s1;
 
241
        s2.pop_back();
 
242
        s3.pop_back();
 
243
        peak.setMZ(666.66);
 
244
        peak.setIntensity(999.99f);
 
245
        s2.push_back(peak);
 
246
        s2.sortByPosition();
 
247
        s3.push_back(peak);
 
248
        s3.sortByPosition();
 
249
 
 
250
        vector<PeakSpectrum> d(3);
 
251
        d[0] = s1; d[1] = s2; d[2] = s3;
 
252
        ClusterHierarchical ch;
 
253
        BinnedSharedPeakCount bspc;
 
254
        SingleLinkage sl;
 
255
        vector< BinaryTreeNode > result;
 
256
        vector< BinaryTreeNode > tree;
 
257
        tree.push_back(BinaryTreeNode(1,2,0.0));
 
258
        tree.push_back(BinaryTreeNode(0,1,0.0086f));
 
259
        DistanceMatrix<Real> matrix;
 
260
 
 
261
        ch.cluster(d,bspc,1.5,2,sl,result, matrix);
 
262
 
 
263
        TEST_EQUAL(tree.size(), result.size());
 
264
        for (Size i = 0; i < tree.size(); ++i)
 
265
        {
 
266
                        TOLERANCE_ABSOLUTE(0.0001);
 
267
                        TEST_EQUAL(tree[i].left_child, result[i].left_child);
 
268
                        TEST_EQUAL(tree[i].right_child, result[i].right_child);
 
269
                        TEST_REAL_SIMILAR(tree[i].distance, result[i].distance);
 
270
        }
 
271
}
 
272
END_SECTION
 
273
 
 
274
/////////////////////////////////////////////////////////////
 
275
/////////////////////////////////////////////////////////////
 
276
END_TEST
 
277
 
 
278
 
 
279