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

« back to all changes in this revision

Viewing changes to include/OpenMS/DATASTRUCTURES/DistanceMatrix.h

  • 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
#ifndef OPENMS_DATASTRUCTURES_DISTANCEMATRIX_H
 
29
#define OPENMS_DATASTRUCTURES_DISTANCEMATRIX_H
 
30
 
 
31
#include <OpenMS/CONCEPT/Macros.h>
 
32
#include <OpenMS/CONCEPT/Types.h>
 
33
 
 
34
#include <cmath>
 
35
#include <algorithm>
 
36
#include <iomanip>
 
37
#include <iostream>
 
38
 
 
39
namespace OpenMS
 
40
{
 
41
 
 
42
        /**
 
43
                @brief A two-dimensional distance matrix, similar to OpenMS::Matrix
 
44
 
 
45
                similar to OpenMS::Matrix, but contains only elements above the main diagonal, hence translating access with operator(,) for elements of above the main diagonal to corresponing elements below the main diagonal and returning 0 for requested elements in the main diagonal, since selfdistance is assumed to be 0. Keeps track of the minimal element in the Matrix with OpenMS::DistanceMatrix::min_element_ if only for setting a value OpenMS::DistanceMatrix::setValue is used. Other OpenMS::DistanceMatrix altering methods may require a maual update by call of OpenMS::DistanceMatrix::updateMinElement, see the respective methods documentation.
 
46
 
 
47
                @ingroup Datastructures
 
48
        */
 
49
        template <typename Value>
 
50
        class DistanceMatrix
 
51
 
 
52
        {
 
53
         public:
 
54
 
 
55
                ///@name STL compliance type definitions
 
56
                //@{
 
57
                typedef Value value_type;
 
58
                //@}
 
59
 
 
60
                ///@name OpenMS compliance type definitions
 
61
                //@{
 
62
                typedef Size SizeType;
 
63
                typedef value_type ValueType;
 
64
                //@}
 
65
 
 
66
                /** @brief default constructor
 
67
 
 
68
                */
 
69
                DistanceMatrix () : matrix_(0), init_size_(0), dimensionsize_(0), min_element_(0,0)
 
70
                {
 
71
                }
 
72
 
 
73
                /** @brief detailed constructor
 
74
 
 
75
                        @param dimensionsize the number of rows (and therewith cols)
 
76
                        @param value DistanceMatrix will be filled with this element (main diagonal will still "hold" only zeros)
 
77
                        @throw Exception::OutOfMemory if requested dimensionsize is to big to fit into memory
 
78
                */
 
79
                DistanceMatrix (SizeType dimensionsize, Value value = Value())
 
80
                        : matrix_(new ValueType*[dimensionsize]), init_size_(dimensionsize), dimensionsize_(dimensionsize), min_element_(0,0)
 
81
                {
 
82
                        matrix_[0] = NULL;
 
83
                        SizeType i = 1;
 
84
                        for (i = 1; i < dimensionsize; ++i)
 
85
                        {
 
86
                                matrix_[i] = new ValueType[i];
 
87
                                if (matrix_[i]==NULL)
 
88
                                {
 
89
                                        SizeType j = i;
 
90
                                        for (i = 1; i < j; i++)
 
91
                                        {
 
92
                                                delete[] matrix_[i];
 
93
                                        }
 
94
                                        delete[] matrix_;
 
95
                                        matrix_ = NULL;
 
96
                                        dimensionsize_ = 0;
 
97
                                        init_size_ = 0;
 
98
                                        throw Exception::OutOfMemory(__FILE__,__LINE__,__PRETTY_FUNCTION__,(UInt)((((dimensionsize-2)*(dimensionsize-1))/2)*sizeof(ValueType)));
 
99
                                }
 
100
                        }
 
101
                        if(matrix_!=NULL)
 
102
                        {
 
103
                                for(i = 1; i < dimensionsize; ++i)
 
104
                                {
 
105
                                        for(SizeType j = 0; j< i; ++j)
 
106
                                        {
 
107
                                                matrix_[i][j]=value;
 
108
                                        }
 
109
                                }
 
110
                                min_element_ = std::make_pair(1,0);
 
111
                        }
 
112
                }
 
113
 
 
114
                /** @brief copy constructor
 
115
 
 
116
                        @param source  this DistanceMatrix will be copied
 
117
                        @throw Exception::OutOfMemory if requested dimensionsize is to big to fit into memory
 
118
                */
 
119
                DistanceMatrix (const DistanceMatrix& source)
 
120
                        : matrix_(new ValueType*[source.dimensionsize_]),
 
121
                                init_size_(source.dimensionsize_),
 
122
                                dimensionsize_(source.dimensionsize_),
 
123
                                min_element_(source.min_element_)
 
124
                {
 
125
                        matrix_[0] = NULL;
 
126
                        SizeType i = 1;
 
127
                        for (i = 1; i < dimensionsize_; ++i)
 
128
                        {
 
129
                                matrix_[i] = new ValueType[i];
 
130
                                if (matrix_[i]==NULL)
 
131
                                {
 
132
                                        SizeType j = i;
 
133
                                        for (i = 1; i < j; i++)
 
134
                                        {
 
135
                                                delete[] matrix_[i];
 
136
                                        }
 
137
                                        delete[] matrix_;
 
138
                                        matrix_ = NULL;
 
139
                                        dimensionsize_ = 0;
 
140
                                        init_size_ = 0;
 
141
                                        min_element_ = std::make_pair(0,0);
 
142
                                        throw Exception::OutOfMemory(__FILE__,__LINE__,__PRETTY_FUNCTION__,(UInt)((((dimensionsize_-2)*(dimensionsize_-1))/2)*sizeof(ValueType)));
 
143
                                }
 
144
                        }
 
145
                        if(matrix_!=NULL)
 
146
                        {
 
147
                                for(i = 1; i < dimensionsize_; ++i)
 
148
                                {
 
149
                                        std::copy(source.matrix_[i],source.matrix_[i]+i,matrix_[i]);
 
150
                                }
 
151
                        }
 
152
                }
 
153
 
 
154
                /// destructor
 
155
                ~DistanceMatrix()
 
156
                {
 
157
                        for (SizeType i = 1; i < init_size_; i++)
 
158
                        {
 
159
                                delete[] matrix_[i];
 
160
                        }
 
161
                        delete[] matrix_;
 
162
                }
 
163
 
 
164
                /** @brief gets a value at a given position (read only):
 
165
 
 
166
                        @param i the i-th row
 
167
                        @param j the j-th col
 
168
                */
 
169
                const ValueType operator() (SizeType i, SizeType j) const
 
170
                {
 
171
                        return getValue(i,j);
 
172
                }
 
173
 
 
174
                /** @brief gets a value at a given position (read only):
 
175
 
 
176
                        @param i the i-th row
 
177
                        @param j the j-th col
 
178
                */
 
179
                ValueType operator() (SizeType i, SizeType j)
 
180
                {
 
181
                        return getValue(i,j);
 
182
                }
 
183
 
 
184
                /** @brief gets a value at a given position:
 
185
 
 
186
                        @param i the i-th row
 
187
                        @param j the j-th col
 
188
                        @throw Exception::OutOfRange if given coordinates are out of range
 
189
                */
 
190
                const ValueType getValue(SizeType i, SizeType j) const
 
191
                {
 
192
                        if(i>=dimensionsize_ || j >= dimensionsize_)
 
193
                        {
 
194
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
195
                        }
 
196
                        // elements on main diagonal are not stored and assumed to be 0
 
197
                        if(i==j)
 
198
                        {
 
199
                                return 0;
 
200
                        }
 
201
                        if(i<j)
 
202
                        {
 
203
                                std::swap(i,j);
 
204
                        }
 
205
                        return (const ValueType)(matrix_[i][j]);
 
206
                }
 
207
 
 
208
                /** @brief gets a value at a given position:
 
209
 
 
210
                        @param i the i-th row
 
211
                        @param j the j-th col
 
212
                        @throw Exception::OutOfRange if given coordinates are out of range
 
213
                */
 
214
                ValueType getValue(SizeType i, SizeType j)
 
215
                {
 
216
                        if(i>=dimensionsize_ || j >= dimensionsize_)
 
217
                        {
 
218
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
219
                        }
 
220
                        // elements on main diagonal are not stored and assumed to be 0
 
221
                        if(i==j)
 
222
                        {
 
223
                                return 0;
 
224
                        }
 
225
                        if(i<j)
 
226
                        {
 
227
                                std::swap(i,j);
 
228
                        }
 
229
                        return matrix_[i][j];
 
230
                }
 
231
 
 
232
                /** @brief sets a value at a given position:
 
233
 
 
234
                        @param i the i-th row
 
235
                        @param j the j-th col
 
236
                        @param value the set-value
 
237
                        @throw Exception::OutOfRange if given coordinates are out of range
 
238
                */
 
239
                void setValue(SizeType i, SizeType j, ValueType value)
 
240
                {
 
241
                        if(i>=dimensionsize_ || j >= dimensionsize_)
 
242
                        {
 
243
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
244
                        }
 
245
                        // elements on main diagonal are not stored and assumed to be 0
 
246
                        if(i!=j)
 
247
                        {
 
248
                                if(i<j)
 
249
                                {
 
250
                                        std::swap(i,j);
 
251
                                }
 
252
                                if(i!=min_element_.first && j!=min_element_.second)
 
253
                                {
 
254
                                        matrix_[i][j] = value;
 
255
                                        if(value < matrix_[min_element_.first][min_element_.second]) // keep min_element_ up-to-date
 
256
                                        {
 
257
                                                min_element_ = std::make_pair(i,j);
 
258
                                        }
 
259
                                }
 
260
                                else
 
261
                                {
 
262
                                        if(value <= matrix_[min_element_.first][min_element_.second])
 
263
                                        {
 
264
                                                matrix_[i][j] = value;
 
265
                                        }
 
266
                                        else
 
267
                                        {
 
268
                                                matrix_[i][j] = value;
 
269
                                                updateMinElement();
 
270
                                        }
 
271
                                }
 
272
                        }
 
273
                }
 
274
 
 
275
                /** @brief sets a value at a given position:
 
276
 
 
277
                        @param i the i-th row
 
278
                        @param j the j-th col
 
279
                        @param value the set-value
 
280
                        @throw Exception::OutOfRange if given coordinates are out of range
 
281
 
 
282
                        possible invalidation of min_element_ - make sure to update before further usage of matrix
 
283
                */
 
284
                void setValueQuick(SizeType i, SizeType j, ValueType value)
 
285
                {
 
286
                        if(i>=dimensionsize_ || j >= dimensionsize_)
 
287
                        {
 
288
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
289
                        }
 
290
                        // elements on main diagonal are not stored and assumed to be 0
 
291
                        if(i!=j)
 
292
                        {
 
293
                                if(i<j)
 
294
                                {
 
295
                                        std::swap(i,j);
 
296
                                }
 
297
                                matrix_[i][j] = value;
 
298
                        }
 
299
                }
 
300
 
 
301
                /// reset all
 
302
                void clear()
 
303
                {
 
304
                        for (SizeType i = 1; i < init_size_; i++)
 
305
                        {
 
306
                                delete[] matrix_[i];
 
307
                        }
 
308
                        delete[] matrix_;
 
309
                        matrix_ = NULL;
 
310
                        min_element_ = std::make_pair(0,0);
 
311
                        dimensionsize_ = 0;
 
312
                        init_size_ = 0;
 
313
                }
 
314
 
 
315
                /** @brief resizing the container
 
316
 
 
317
                        @param dimensionsize the desired number of rows (and therewith cols)
 
318
                        @param value which the matrix will be filled with
 
319
                        @throw Exception::OutOfMemory thrown if size of DistanceMatrix requested does not fit into memory
 
320
 
 
321
                        invalidates all content
 
322
                */
 
323
                void resize(SizeType dimensionsize, Value value = Value())
 
324
                {
 
325
                        for (SizeType j = 1; j < init_size_; j++)
 
326
                        {
 
327
                                delete matrix_[j];
 
328
                        }
 
329
                        delete[] matrix_;
 
330
                        dimensionsize_ = dimensionsize;
 
331
                        init_size_ = dimensionsize;
 
332
                        min_element_ = std::make_pair(0,0);
 
333
                        matrix_ = new ValueType*[dimensionsize_];
 
334
                        for (SizeType j = 1; j < dimensionsize_; ++j)
 
335
                        {
 
336
                                matrix_[j] = new ValueType[j];
 
337
                                if (matrix_[j]==NULL)
 
338
                                {
 
339
                                        for (SizeType k = 1; k < j; ++k)
 
340
                                        {
 
341
                                                delete[] matrix_[k];
 
342
                                        }
 
343
                                        delete[] matrix_;
 
344
                                        matrix_ = NULL;
 
345
                                        dimensionsize_ = 0;
 
346
                                        init_size_ = 0;
 
347
                                        throw Exception::OutOfMemory(__FILE__,__LINE__,__PRETTY_FUNCTION__,(UInt)((((dimensionsize_-2)*(dimensionsize_-1))/2)*sizeof(Value)));
 
348
                                }
 
349
                        }
 
350
                        if(matrix_!=NULL)
 
351
                        {
 
352
                                for(SizeType j = 0; j < dimensionsize; ++j)
 
353
                                {
 
354
                                        for(SizeType k = 0; k< j; ++k)
 
355
                                        {
 
356
                                                matrix_[j][k]=value;
 
357
                                        }
 
358
                                }
 
359
                                min_element_ = std::make_pair(1,0);
 
360
                        }
 
361
                }
 
362
 
 
363
                /** @brief reduces DistanceMatrix by one dimension. first the jth row, then jth column
 
364
 
 
365
                        @param j the jth row (and therewith also jth col) to be removed
 
366
                        @throw Exception::OutOfRange if @p j is grater than the greatest row number
 
367
 
 
368
                        may invalidates min_element_, make sure to update min_element_ if neccessary before used
 
369
                */
 
370
                void reduce(SizeType j)
 
371
                {
 
372
                        if(j >= dimensionsize_)
 
373
                        {
 
374
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
375
                        }
 
376
                        //delete row j and therefor overwrite with row j+1 and iterate like this to last row
 
377
                        SizeType i = j+1;
 
378
                        while(i < dimensionsize_ && matrix_[i] != NULL)
 
379
                        {
 
380
                                //left out in the copy is each rows jth element, pointer working here as iterators just fine
 
381
                                std::copy(matrix_[i]+j+1,matrix_[i]+i,std::copy(matrix_[i],matrix_[i]+j,matrix_[i-1]));
 
382
                                ++i;
 
383
                        }
 
384
                        //last row is freed and the pointer set to NULL (outer array's size is not changed)
 
385
                        delete[] matrix_[i-1];
 
386
                        matrix_[i-1] = NULL;
 
387
                        --dimensionsize_;
 
388
                }
 
389
 
 
390
                /// gives the number of rows (i.e. number of columns)
 
391
                SizeType dimensionsize() const
 
392
                {
 
393
                        return dimensionsize_;
 
394
                }
 
395
 
 
396
                /** @brief keep track of the actual minimum element after altering the matrix
 
397
 
 
398
                        @throw Exception::OutOfRange thrown if there is no element to access
 
399
                */
 
400
                void updateMinElement()
 
401
                {
 
402
                        min_element_ = std::make_pair(1,0);
 
403
                        //error if dimensionsize_<1, return if dimensionsize_ == 1, else
 
404
                        if(dimensionsize_ < 1)
 
405
                        {
 
406
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
407
                        }
 
408
                        if(dimensionsize_!=1) //else matrix has one element: (1,0)
 
409
                        {
 
410
                                ValueType* row_min_;
 
411
                                for(SizeType r = 2; r < dimensionsize_ && matrix_[r]!=NULL; ++r)
 
412
                                {
 
413
                                        row_min_ = std::min_element(matrix_[r],matrix_[r]+r);
 
414
                                        if(*row_min_ < matrix_[min_element_.first][min_element_.second])
 
415
                                        {
 
416
                                                min_element_ = std::make_pair(r,row_min_ - matrix_[r]);
 
417
                                        }
 
418
                                }
 
419
                        }
 
420
                }
 
421
 
 
422
 
 
423
 
 
424
                /**@brief Equality comparator.
 
425
 
 
426
                        @throw Exception::Precondition thrown if given DistanceMatrix is not compatible in size
 
427
                */
 
428
                bool operator== ( DistanceMatrix<ValueType> const & rhs ) const
 
429
                {
 
430
                        OPENMS_PRECONDITION(dimensionsize_ == rhs.dimensionsize_,"DistanceMatrices have different sizes.");
 
431
                        for (Size i = 1; i < rhs.dimensionsize(); ++i)
 
432
                        {
 
433
        for (Size j = 0; j < i; ++j)
 
434
                                {
 
435
                                        if(matrix_[i][j]!=rhs.matrix_[i][j])
 
436
                                        {
 
437
                                                return false;
 
438
                                        }
 
439
                                }
 
440
                        }
 
441
                        return true;
 
442
                }
 
443
 
 
444
 
 
445
                /** @brief Indexpair of minimal element
 
446
 
 
447
                        @throw Exception::OutOfRange thrown if there is no element to access
 
448
                */
 
449
                std::pair<SizeType,SizeType> getMinElementCoordinates() const
 
450
                {
 
451
                        if ( dimensionsize_ == 0 )
 
452
                        {
 
453
                                throw Exception::OutOfRange(__FILE__,__LINE__,__PRETTY_FUNCTION__);
 
454
                        }
 
455
                        return min_element_;
 
456
                }
 
457
 
 
458
         protected:
 
459
                /// sparse element not to be included in base container
 
460
                ValueType** matrix_;
 
461
                /// number of actually stored rows
 
462
                SizeType init_size_; // actual size of outer array
 
463
                /// number of accessably stored rows (i.e. number of columns)
 
464
                SizeType dimensionsize_; //number of virtual elements: ((dimensionsize-1)*(dimensionsize))/2
 
465
                /// index of minimal element(i.e. number in underlying SparseVector)
 
466
                std::pair<SizeType,SizeType> min_element_;
 
467
 
 
468
         private:
 
469
                /// assignment operator (unsafe)
 
470
                DistanceMatrix& operator= (const DistanceMatrix& rhs)
 
471
                {
 
472
                        matrix_= rhs.matrix_;
 
473
                        init_size_= rhs.init_size_;
 
474
                        dimensionsize_ = rhs.dimensionsize_;
 
475
                        min_element_ = rhs.min_element_;
 
476
 
 
477
                        return *this;
 
478
                }
 
479
 
 
480
 
 
481
        }; // class DistanceMatrix
 
482
 
 
483
        /**@brief Print the contents to a stream.
 
484
 
 
485
                @relatesalso DistanceMatrix
 
486
        */
 
487
        template <typename Value>
 
488
        std::ostream& operator<< (std::ostream& os, const DistanceMatrix<Value>& matrix)
 
489
        {
 
490
                typedef typename DistanceMatrix<Value>::SizeType SizeType;
 
491
 
 
492
                std::ios_base::fmtflags flag_backup = os.setf(std::ios::scientific);
 
493
                std::streamsize precision_backup = os.precision();
 
494
                //~ os.precision(15);
 
495
                os.precision(writtenDigits<DoubleReal>()); // #include <OpenMS/CONCEPT/Types.h>
 
496
 
 
497
                //evtl. color lower triangular matrix o.s.l.t.
 
498
                for ( SizeType i = 0; i < matrix.dimensionsize(); ++i ) {
 
499
                        for ( SizeType j = 0; j < matrix.dimensionsize(); ++j ) {
 
500
                                os << matrix(i,j) << '\t';
 
501
                        }
 
502
                        os << std::endl;
 
503
                }
 
504
                os.flags(flag_backup);
 
505
                os.precision(precision_backup);
 
506
                return os;
 
507
        }
 
508
 
 
509
 
 
510
} // namespace OpenMS
 
511
 
 
512
#endif // OPENMS_DATASTRUCTURES_DISTANCEMATRIX_H