File: test_index_fm_right_array_binary_tree.h

package info (click to toggle)
seqan2 2.4.0+dfsg-11~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 223,500 kB
  • sloc: cpp: 256,886; ansic: 91,672; python: 8,339; sh: 995; xml: 570; makefile: 251; awk: 51
file content (386 lines) | stat: -rw-r--r-- 14,641 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// Copyright (c) 2013 NVIDIA Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Knut Reinert or the FU Berlin nor the names of
//       its contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: Jochen Singer <jochen.singer@fu-berlin.de>
// ==========================================================================

#ifndef TESTS_WAVELT_TREE_STRUCTURE_BETA_H_
#define TESTS_WAVELT_TREE_STRUCTURE_BETA_H_

#include <seqan/basic.h>
#include <seqan/sequence.h>
#include <seqan/index.h>

using namespace seqan;

template <typename TIter, typename TBorderString, typename TChar>
void determineBorders(TIter & it, TBorderString & borderString, TChar left, TChar right)
{
    TIter it2 = it;
    borderString[getPosition(it)] = Pair<TChar>(left, right);

    TChar pivot = getCharacter(it);
    if (goLeftChild(it))
    {
        determineBorders(it, borderString, left, (TChar)(ordValue(pivot) - 1));
    }
    if (goRightChild(it2))
    {
        determineBorders(it2, borderString, pivot, right);
    }
}


template <typename TRightArrayBinaryTree>
void waveletTreeStructureConstructor(TRightArrayBinaryTree & /*tag*/)
{
    typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
    typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
    typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;

    {
        TRightArrayBinaryTree waveletTreeStructure;
        SEQAN_ASSERT_EQ(length(waveletTreeStructure.treeVertices), 0u);
    }
    {
        String<TChar> text;
        generateText(text);

        String<Pair<TChar> > borderString;
        resize(borderString, ValueSize<TChar>::VALUE);

        String<unsigned> freq;
        resize(freq, ValueSize<TChar>::VALUE, 0);
        _getFrequencies(freq, text);

        TRightArrayBinaryTree waveletTreeStructure(text);
        typename Iterator<TRightArrayBinaryTree, TopDown<> >::Type it(waveletTreeStructure, 0u);
        determineBorders(it, borderString, (TChar)0, (TChar)(ValueSize<TChar>::VALUE - 1));

        for (unsigned i = 0; i < length(getFibre(waveletTreeStructure, FibreTreeStructureEncoding())); ++i)
        {
            // determine the current difference
            TChar left = borderString[i].i1;
            int leftCounter = 0;
            for (; left < getFibre(waveletTreeStructure, FibreTreeStructureEncoding())[i].i1; ++left)
            {
                leftCounter += freq[ordValue(left)];
            }
            int rightCounter = 0;

            for (; left <= borderString[i].i2 && left > borderString[i].i1; ++left)
            {
                rightCounter+= freq[ordValue(left)];
            }
            int currentSum = leftCounter - rightCounter;
            if (currentSum < 0)
                currentSum *= -1;

            for (TChar newPivot = (unsigned)borderString[i].i1 + 1; newPivot <= borderString[i].i2 && newPivot > borderString[i].i1; ++newPivot)
            {
                TChar newLeft = borderString[i].i1;
                int newLeftCounter = 0;
                for (; newLeft < newPivot; ++newLeft)
                {
                    newLeftCounter += freq[ordValue(newLeft)];
                }
                int newRightCounter = 0;
                for (; newLeft <= borderString[i].i2 && newLeft > borderString[i].i1; ++newLeft)
                {
                    newRightCounter += freq[ordValue(newLeft)];
                }
                int newCurrentSum = newLeftCounter - newRightCounter;
                if (newCurrentSum < 0)
                    newCurrentSum *= -1;
                SEQAN_ASSERT_LEQ(currentSum, newCurrentSum);
            }
        }
    }
}

template <typename TRightArrayBinaryTree>
void waveletTreeStructureClear(TRightArrayBinaryTree & /*tag*/)
{
    typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
    typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
    typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;

    {
        TRightArrayBinaryTree waveletTreeStructure;
        SEQAN_ASSERT_EQ(length(waveletTreeStructure.treeVertices), 0u);
        clear(waveletTreeStructure);
        SEQAN_ASSERT_EQ(length(waveletTreeStructure.treeVertices), 0u);
    }
    {
        String<TChar> text;
        generateText(text);

        TRightArrayBinaryTree waveletTreeStructure(text);
        SEQAN_ASSERT_NEQ(length(waveletTreeStructure.treeVertices), 0u);
        clear(waveletTreeStructure);
        SEQAN_ASSERT_EQ(length(waveletTreeStructure.treeVertices), 0u);
    }
}

template <typename TRightArrayBinaryTree>
void waveletTreeStructureEmpty(TRightArrayBinaryTree & /*tag*/)
{
    typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
    typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
    typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;

    {
        TRightArrayBinaryTree waveletTreeStructure;
        SEQAN_ASSERT_EQ(empty(waveletTreeStructure), true);
        clear(waveletTreeStructure);
        SEQAN_ASSERT_EQ(empty(waveletTreeStructure), true);
    }
    {
        String<TChar> text;
        generateText(text);

        TRightArrayBinaryTree waveletTreeStructure(text);
        SEQAN_ASSERT_EQ(empty(waveletTreeStructure), false);
        clear(waveletTreeStructure);
        SEQAN_ASSERT_EQ(empty(waveletTreeStructure), true);
    }
}

template <typename TRightArrayBinaryTree>
void waveletTreeStructureGetFibre(TRightArrayBinaryTree & /*tag*/)
{
    typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
    //typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
    //typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;

    {
        TRightArrayBinaryTree waveletTreeStructure;

        TWaveletTreeVertices & temp = getFibre(waveletTreeStructure, FibreTreeStructureEncoding());
        SEQAN_ASSERT_EQ(length(waveletTreeStructure.treeVertices), 0u);

        resize(temp, 10);

        SEQAN_ASSERT_EQ(length(waveletTreeStructure.treeVertices), 10u);
    }
}

template <typename TRightArrayBinaryTree>
void waveletTreeStructureLength(TRightArrayBinaryTree & /*tag*/)
{
    typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
    //typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
    //typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;

    {
        TRightArrayBinaryTree waveletTreeStructure;

        TWaveletTreeVertices & temp = getFibre(waveletTreeStructure, FibreTreeStructureEncoding());
        SEQAN_ASSERT_EQ(length(waveletTreeStructure), 0u);

        resize(temp, 10);

        SEQAN_ASSERT_EQ(length(waveletTreeStructure), 10u);
    }
}

template <typename TRightArrayBinaryTree>
void waveletTreeStructureResize(TRightArrayBinaryTree & /*tag*/)
{
    typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
    typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
    //typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;

    {
        TRightArrayBinaryTree waveletTreeStructure;

        TWaveletTreeVertices & temp = getFibre(waveletTreeStructure, FibreTreeStructureEncoding());
        SEQAN_ASSERT_EQ(length(waveletTreeStructure), 0u);

        resize(temp, 10);

        SEQAN_ASSERT_EQ(length(waveletTreeStructure), 10u);
    }
    {
        TRightArrayBinaryTree waveletTreeStructure;

        TWaveletTreeVertices & temp = getFibre(waveletTreeStructure, FibreTreeStructureEncoding());
        SEQAN_ASSERT_EQ(length(waveletTreeStructure), 0u);

        resize(temp, 10, TWaveletTreeVertex(0, 10));

        SEQAN_ASSERT_EQ(length(waveletTreeStructure), 10u);
        SEQAN_ASSERT_EQ(getFibre(waveletTreeStructure, FibreTreeStructureEncoding())[9],  TWaveletTreeVertex(0, 10));
    }

}

// NOTE(esiragusa): OpenSave test should not use operator==()
//template <typename TRightArrayBinaryTree>
//void waveletTreeStructureOpenSave(TRightArrayBinaryTree & /*tag*/)
//{
//    //typedef typename Fibre<TRightArrayBinaryTree, FibreTreeStructureEncoding>::Type TWaveletTreeVertices;
//    //typedef typename Value<TWaveletTreeVertices>::Type TWaveletTreeVertex;
//    //typedef typename Value<TWaveletTreeVertex, 1>::Type TChar;
//    //typedef typename Value<TWaveletTreeVertex, 2>::Type TPos;
//
//
//    TRightArrayBinaryTree waveletTreeStructure;
//    _resize(waveletTreeStructure, 10, Exact());
//
//    CharString tempFilename = SEQAN_TEMP_FILENAME();
//
//    save(waveletTreeStructure, toCString(tempFilename));
//
//    TRightArrayBinaryTree openWaveletTreeStructure;
//    open(openWaveletTreeStructure, toCString(tempFilename));
//
//    SEQAN_ASSERT(waveletTreeStructure == openWaveletTreeStructure);
//
//    waveletTreeStructure = openWaveletTreeStructure;
//
//    SEQAN_ASSERT(waveletTreeStructure == openWaveletTreeStructure);
//}

SEQAN_DEFINE_TEST(wavelet_tree_structure_constructor)
{
    using namespace seqan;

    RightArrayBinaryTree<Dna, void> dnaTag;
    RightArrayBinaryTree<Dna5, void> dna5Tag;
    RightArrayBinaryTree<AminoAcid, void> asTag;
    RightArrayBinaryTree<signed char, void> charTag;
    RightArrayBinaryTree<unsigned char, void> uCharTag;
    waveletTreeStructureConstructor(dnaTag);
    waveletTreeStructureConstructor(dna5Tag);
    waveletTreeStructureConstructor(asTag);
    waveletTreeStructureConstructor(charTag);
    waveletTreeStructureConstructor(uCharTag);
}

SEQAN_DEFINE_TEST(wavelet_tree_structure_clear)
{
    using namespace seqan;

    RightArrayBinaryTree<Dna, void> dnaTag;
    RightArrayBinaryTree<Dna5, void> dna5Tag;
    RightArrayBinaryTree<AminoAcid, void> asTag;
    RightArrayBinaryTree<char, void> charTag;
    RightArrayBinaryTree<unsigned char, void> uCharTag;
    waveletTreeStructureClear(dnaTag);
    waveletTreeStructureClear(dna5Tag);
    waveletTreeStructureClear(asTag);
    waveletTreeStructureClear(charTag);
    waveletTreeStructureClear(uCharTag);
}

SEQAN_DEFINE_TEST(wavelet_tree_structure_empty)
{
    using namespace seqan;

    RightArrayBinaryTree<Dna, void> dnaTag;
    RightArrayBinaryTree<Dna5, void> dna5Tag;
    RightArrayBinaryTree<AminoAcid, void> asTag;
    RightArrayBinaryTree<char, void> charTag;
    RightArrayBinaryTree<unsigned char, void> uCharTag;
    waveletTreeStructureEmpty(dnaTag);
    waveletTreeStructureEmpty(dna5Tag);
    waveletTreeStructureEmpty(asTag);
    waveletTreeStructureEmpty(charTag);
    waveletTreeStructureEmpty(uCharTag);
}

SEQAN_DEFINE_TEST(wavelet_tree_structure_get_fibre)
{
    using namespace seqan;

    RightArrayBinaryTree<Dna, void> dnaTag;
    RightArrayBinaryTree<Dna5, void> dna5Tag;
    RightArrayBinaryTree<AminoAcid, void> asTag;
    RightArrayBinaryTree<char, void> charTag;
    RightArrayBinaryTree<unsigned char, void> uCharTag;
    waveletTreeStructureGetFibre(dnaTag);
    waveletTreeStructureGetFibre(dna5Tag);
    waveletTreeStructureGetFibre(asTag);
    waveletTreeStructureGetFibre(charTag);
    waveletTreeStructureGetFibre(uCharTag);
}

SEQAN_DEFINE_TEST(wavelet_tree_structure_length)
{
    using namespace seqan;

    RightArrayBinaryTree<Dna, void> dnaTag;
    RightArrayBinaryTree<Dna5, void> dna5Tag;
    RightArrayBinaryTree<AminoAcid, void> asTag;
    RightArrayBinaryTree<char, void> charTag;
    RightArrayBinaryTree<unsigned char, void> uCharTag;
    waveletTreeStructureLength(dnaTag);
    waveletTreeStructureLength(dna5Tag);
    waveletTreeStructureLength(asTag);
    waveletTreeStructureLength(charTag);
    waveletTreeStructureLength(uCharTag);
}

SEQAN_DEFINE_TEST(wavelet_tree_structure_resize)
{
    using namespace seqan;

    RightArrayBinaryTree<Dna, void> dnaTag;
    RightArrayBinaryTree<Dna5, void> dna5Tag;
    RightArrayBinaryTree<AminoAcid, void> asTag;
    RightArrayBinaryTree<char, void> charTag;
    RightArrayBinaryTree<unsigned char, void> uCharTag;
    waveletTreeStructureResize(dnaTag);
    waveletTreeStructureResize(dna5Tag);
    waveletTreeStructureResize(asTag);
    waveletTreeStructureResize(charTag);
    waveletTreeStructureResize(uCharTag);
}

// NOTE(esiragusa): OpenSave test should not use operator==()
//SEQAN_DEFINE_TEST(wavelet_tree_structure_open_save)
//{
//    using namespace seqan;
//
//    RightArrayBinaryTree<AminoAcid, void> asTag;
//    waveletTreeStructureOpenSave(asTag);
//}


#endif  // TESTS_WAVELT_TREE_STRUCTURE_BETA_H_