File: test_find2_index_approx.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 (489 lines) | stat: -rw-r--r-- 20,069 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, Knut Reinert, FU Berlin
// 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 NVIDIA Corporation 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 NVIDIA CORPORATION 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: Christopher Pockrandt <github@cpockrandt.de>
// ==========================================================================

#ifndef TESTS_FIND2_INDEX_APPROX_H_
#define TESTS_FIND2_INDEX_APPROX_H_

#include <mutex>
#include <ctime>

#include <seqan/index.h>

#include "test_index_helpers.h"

using namespace seqan;

// ============================================================================
// Functions
// ============================================================================

template <typename TChar, typename TConfig>
void generateText(String<TChar, TConfig> & text, unsigned const length)
{
    unsigned alphabetSize = ValueSize<TChar>::VALUE;
    unsigned minChar = MinValue<TChar>::VALUE;

    resize(text, length);

    for (unsigned i = 0; i < length; ++i)
    {
        text[i] = std::rand() % alphabetSize - minChar;
    }
}

template< typename TContainer>
inline void printVector(const TContainer & c, std::ostream & stream)
{
    using T = std::decay_t<decltype(c[0])>;
    stream << "[";
    for (uint8_t i = 0; i < c.size(); ++i)
    {
        stream << (std::is_same<T, uint8_t>::value ? (unsigned) c[i] : c[i])
               << ((i < c.size() - 1) ? ", " : "");
    }
    stream << "]" << std::endl;
}

template <size_t nbrBlocks>
inline void printSearch(OptimalSearch<nbrBlocks> const & s, std::ostream & stream)
{
    stream << "SearchScheme (Pi): ";
    printVector(s.pi, stream);
    stream << "SearchScheme (L): ";
    printVector(s.l, stream);
    stream << "SearchScheme (U): ";
    printVector(s.u, stream);
    stream << "SearchScheme (BL): ";
    printVector(s.blocklength, stream);
}

inline void _getErrorDistributions(std::vector<std::vector<uint8_t> > & res,
                                   std::vector<uint8_t> l,
                                   std::vector<uint8_t> u,
                                   uint8_t const e)
{
    if (l.size() == 0)
    {
        std::vector<uint8_t> emptyVector;
        res.push_back(emptyVector);
        return;
    }

    uint8_t l1 = l[0];
    uint8_t u1 = u[0];
    l.erase(l.begin());
    u.erase(u.begin());

    for (uint8_t i = std::max(e, l1); i <= u1; ++i)
    {
        std::vector<std::vector<uint8_t> > _res;
        _getErrorDistributions(_res, l, u, i);
        for (auto & _resElem : _res)
        {
            _resElem.insert(_resElem.begin(), i - e);
            res.push_back(_resElem);
        }
    }
}

// Compute all possible error distributions given a search.
// The result is ordered as the search (s.pi)
template <size_t nbrBlocks>
inline void getErrorDistributions(std::vector<std::vector<uint8_t> > & res,
                                  OptimalSearch<nbrBlocks> const & s)
{
    std::vector<uint8_t> l(s.l.begin(), s.l.end());
    std::vector<uint8_t> u(s.u.begin(), s.u.end());
    _getErrorDistributions(res, l, u, 0u);
}

// Reorder blocks s.t. they are in a sequential order (from left to right)
template <size_t nbrBlocks, typename T>
inline void orderVector(OptimalSearch<nbrBlocks> const & s, std::vector<T> & v)
{
    std::vector<T> v_tmp = v;
    for (uint8_t i = 0; i < s.pi.size(); ++i)
    {
        uint8_t index = s.pi[i] - 1;
        v[index] = v_tmp[i];
    }
}

// Reorder blocks s.t. they are in a sequential order (from left to right)
// Blocklength is stored as absolute values instead of cumulative values.
template <size_t nbrBlocks>
inline void getOrderedSearch(OptimalSearch<nbrBlocks> const & s, OptimalSearch<nbrBlocks> & os)
{
    for (uint8_t i = 0; i < s.pi.size(); ++i)
    {
        uint8_t index = s.pi[i] - 1;
        os.pi[index] = s.pi[i];
        os.l[index] = s.l[i];
        os.u[index] = s.u[i];
        os.blocklength[index] = s.blocklength[i] - ((i > 0) ? s.blocklength[i - 1] : 0);
    }
}

// Trivial backtracking that finds *all* matches with given distance.
template <typename TDelegate, typename TText, typename TIndex,
          typename TIndexSpec, typename TText2, typename TNeedleIter>
inline void trivialSearch(TDelegate && delegate,
                          Iter<Index<TText, TIndex>, VSTree<TopDown<TIndexSpec> > > it,
                          TText2 const & needle,
                          TNeedleIter const needleIt,
                          uint8_t const errorsLeft,
                          bool const indels)
{
    if (errorsLeft == 0)
    {
        if (goDown(it, suffix(needle, position(needleIt, needle)), Rev()))
            delegate(it);
        return;
    }

    if (atEnd(needleIt, needle))
    {
        delegate(it);
        if (!(indels && errorsLeft > 0))
            return;
    }

    // Insertion
    if (indels && !atEnd(needleIt, needle))
        trivialSearch(delegate, it, needle, needleIt + 1, errorsLeft - 1, indels);

    if (goDown(it, Rev()))
    {
        do
        {
            // Match / Mismatch
            if (!atEnd(needleIt, needle))
            {
                auto delta = !ordEqual(parentEdgeLabel(it, Rev()), value(needleIt));
                trivialSearch(delegate, it, needle, needleIt + 1, errorsLeft - delta, indels);
            }

            // Deletion
            if (indels)
                trivialSearch(delegate, it, needle, needleIt, errorsLeft - 1, indels);

        } while (goRight(it, Rev()));
    }
}

// Compute random blocklengths (order: left to right)
template <size_t nbrBlocks, size_t N>
inline void setRandomBlockLength(std::array<OptimalSearch<nbrBlocks>, N> & ss, uint32_t needleLength)
{
    std::vector<uint32_t> blocklength(ss[0].pi.size());

    // Set minimum length for each block considerung all searches.
    uint8_t maxErrors = 0;
    for (auto const & s : ss)
        maxErrors = std::max(maxErrors, s.u.back());
    // NOTE(cpockrandt): this enforces to be the needles much longer than necessary!
    for (uint8_t i = 0; i < blocklength.size(); ++i)
        blocklength[i] = maxErrors;
    needleLength -= maxErrors * blocklength.size();

    // Randomly distribute the rest on all blocks
    while (needleLength > 0)
    {
        ++blocklength[std::rand() % blocklength.size()];
        --needleLength;
    }

    _optimalSearchSchemeSetBlockLength(ss, blocklength);
    _optimalSearchSchemeInit(ss);
}

template <typename TText, typename TIndex, typename TIndexSpec, size_t nbrBlocks, typename TDistanceTag>
inline void testOptimalSearch(Iter<Index<TText, BidirectionalIndex<TIndex> >, VSTree<TopDown<TIndexSpec> > > it,
                              OptimalSearch<nbrBlocks> const & s,
                              OptimalSearch<nbrBlocks> const & os,
                              unsigned const needleLength,
                              std::vector<uint8_t> const & errorDistribution,
                              time_t const seed,
                              TDistanceTag const & /**/)
{
    typedef typename Value<TText>::Type TChar;
    typedef Iter<Index<TText, BidirectionalIndex<TIndex> >, VSTree<TopDown<TIndexSpec> > >  TIt;

    TText & text = indexText(container(it.fwdIter));

    unsigned pos = std::rand() % (length(text) - needleLength + 1);
    TText origNeedle(infix(text, pos, pos + needleLength));

    // Modify needle s.t. it has errors matching errorDistribution.
    TText needle(origNeedle);
    uint32_t cumulativeBlocklength = 0;
    for (uint8_t block = 0; block < s.pi.size(); ++block)
    {
        uint32_t blocklength = os.blocklength[block];
        if (errorDistribution[block] > blocklength)
        {
            std::stringstream stream;
            stream << "Error in block " << (unsigned) block << "(+ 1): "
                   << (unsigned) errorDistribution[block]
                   << " errors cannot fit into a block of length "
                   << (unsigned) blocklength << "." << std::endl;
            stream << "Error Distribution: ";
            printVector(errorDistribution, stream);
            printSearch(s, stream);
            printSearch(os, stream);
            SEQAN_ASSERT_FAIL(toCString(stream.str()));
        }

        // choose random positions in needle that will be a mismatch/indel
        // repeat until all error positions are unique
        std::vector<uint8_t> errorPositions(errorDistribution[block]);
        do
        {
            clear(errorPositions);
            for (uint8_t error = 0; error < errorDistribution[block]; ++error)
                errorPositions.push_back(std::rand() % blocklength);
            sort(errorPositions.begin(), errorPositions.end());
        } while (adjacent_find(errorPositions.begin(), errorPositions.end()) != errorPositions.end());

        // construct needle with chosen error positions
        for (unsigned error = 0; error < errorPositions.size(); ++error)
        {
            unsigned pos = errorPositions[error] + cumulativeBlocklength;
            TChar newChar;
            do
            {
                newChar = TChar(std::rand() % ValueSize<TChar>::VALUE);
            } while(needle[pos] == newChar);
            needle[pos] = newChar;
        }
        cumulativeBlocklength += blocklength;
    }

    std::vector<unsigned> hits, expectedHitsSS, expectedHitsTrivial;
    auto delegate = [&hits](TIt const & it, TText const & /*needle*/, uint8_t /*errors*/)
    {
        for (unsigned i = 0; i < length(getOccurrences(it)); ++i)
            hits.push_back(getOccurrences(it)[i]);
    };
    auto delegateTrivial = [&hits](TIt const & it)
    {
        for (unsigned i = 0; i < length(getOccurrences(it)); ++i)
            hits.push_back(getOccurrences(it)[i]);
    };

    // Find all hits using search schemes.
    _optimalSearchScheme(delegate, it, needle, s, TDistanceTag());
    for (unsigned hit : hits)
        if (infix(text, hit, hit + needleLength) == origNeedle)
            expectedHitsSS.push_back(hit);

    // Find all hits using trivial backtracking.
    clear(hits);
    uint8_t maxErrors = s.u.back();
    trivialSearch(delegateTrivial, it, needle, begin(needle, Standard()), maxErrors, std::is_same<TDistanceTag, EditDistance>::value);
    for (unsigned hit : hits)
    {
        // filter only correct error distributions
        if (origNeedle == infix(text, hit, hit + needleLength))
        {
            bool distributionOkay = true;
            unsigned leftRange = 0, rightRange = 0;
            for (unsigned block = 0; block < s.pi.size(); ++block)
            {
                rightRange += os.blocklength[block];

                uint8_t errors = 0;
                for (unsigned i = leftRange; i < rightRange; ++i)
                    if (hit + i >= length(text))
                        ++errors;
                    else
                        errors += !ordEqual(needle[i], text[hit + i]);
                if (errors != errorDistribution[block])
                    distributionOkay = false;
                leftRange += os.blocklength[block];
            }
            if (distributionOkay || std::is_same<TDistanceTag, EditDistance>::value)
                expectedHitsTrivial.push_back(hit);
        }
    }

    // eliminate duplicates
    sort(expectedHitsSS.begin(), expectedHitsSS.end());
    sort(expectedHitsTrivial.begin(), expectedHitsTrivial.end());
    expectedHitsSS.erase(unique(expectedHitsSS.begin(), expectedHitsSS.end()), expectedHitsSS.end());
    expectedHitsTrivial.erase(unique(expectedHitsTrivial.begin(), expectedHitsTrivial.end()), expectedHitsTrivial.end());

    if (expectedHitsSS != expectedHitsTrivial)
    {
        std::stringstream stream;
        stream << "Seed: " << seed << std::endl
               << "Text: " << text << std::endl
               << "ErrorDistribution: ";
        printVector(errorDistribution, stream);
        stream << "Original: " << origNeedle << std::endl
               << "Modified: " << needle << std::endl
               << "ExpectedHitsSS: ";
        printVector(expectedHitsSS, stream);
        stream << "ExpectedHitsTrivial: ";
        printVector(expectedHitsTrivial, stream);
        printSearch(s, stream);
        SEQAN_ASSERT_FAIL(toCString(stream.str()));
    }
}

template <size_t nbrBlocks, size_t N, typename TDistanceTag>
inline void testOptimalSearchScheme(std::array<OptimalSearch<nbrBlocks>, N> ss, TDistanceTag const & /**/)
{
    typedef DnaString TText;
    typedef FastFMIndexConfig<void, uint32_t, 2, 1> TMyFastConfig;
    typedef Index<TText, BidirectionalIndex<FMIndex<void, TMyFastConfig> > > TIndex;
    typedef Iter<TIndex, VSTree<TopDown<> > > TIter;

    time_t seed = std::time(nullptr);
    std::srand(seed);

    TText text;
    std::array<OptimalSearch<nbrBlocks>, N> os;
    std::vector<std::vector<std::vector<uint8_t> > > errorDistributions(ss.size());

    // Calculate all error distributions and sort each of them (from left to right).
    uint8_t errors = 0;
    for (uint8_t schemeId = 0; schemeId < ss.size(); ++schemeId)
    {
        os[schemeId] = ss[schemeId];
        getErrorDistributions(errorDistributions[schemeId], ss[schemeId]);
        for (std::vector<uint8_t> & resElem : errorDistributions[schemeId])
            orderVector(ss[schemeId], resElem);
        errors = std::max(errors, ss[schemeId].u.back());
    }

    for (uint32_t textLength = 10; textLength < 10000; textLength *= 10)
    {
        generateText(text, textLength);
        TIndex index(text);
        TIter it(index);
        for (uint16_t needleLength = std::max(static_cast<size_t>(5), ss[0].pi.size() * errors); needleLength < std::min(16u, textLength); ++needleLength)
        {
            setRandomBlockLength(ss, needleLength);

            for (uint8_t schemeId = 0; schemeId < ss.size(); ++schemeId)
                getOrderedSearch(ss[schemeId], os[schemeId]);

            for (uint8_t schemeId = 0; schemeId < ss.size(); ++schemeId)
                for (auto & errorDistribution : errorDistributions[schemeId])
                    testOptimalSearch(it, ss[schemeId], os[schemeId], needleLength, errorDistribution, seed, TDistanceTag());
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

// ----------------------------------------------------------------------------
// Test test_find2_index_approx_hamming
// ----------------------------------------------------------------------------

SEQAN_DEFINE_TEST(test_find2_index_approx_hamming)
{
    testOptimalSearchScheme(OptimalSearchSchemes<0, 0>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 1>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 2>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 3>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 4>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 1>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 2>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 3>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 4>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<2, 2>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<2, 3>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<2, 4>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<3, 3>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<3, 4>::VALUE, HammingDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<4, 4>::VALUE, HammingDistance());
}

// ----------------------------------------------------------------------------
// Test test_find2_index_approx_edit
// ----------------------------------------------------------------------------

SEQAN_DEFINE_TEST(test_find2_index_approx_edit)
{
    testOptimalSearchScheme(OptimalSearchSchemes<0, 0>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 1>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 2>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 3>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<0, 4>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 1>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 2>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 3>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<1, 4>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<2, 2>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<2, 3>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<2, 4>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<3, 3>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<3, 4>::VALUE, EditDistance());
    testOptimalSearchScheme(OptimalSearchSchemes<4, 4>::VALUE, EditDistance());
}

SEQAN_DEFINE_TEST(test_find2_index_approx_small_test)
{
    DnaString genome(
        "GAGAGGCCACTCGCAGGATTAAGTCAATAAGTTAATGGCGTCGGCTTCCTGGTATGTAGTACGACGCCCACAGTGACCTCATCGGTGCATTTCCTCATCGTAG"
        "GCGGAACGGTAGACACAAGGCATGATGTCAAATCGCGACTCCAATCCCAAGGTCGCAAGCCTATATAGGAACCCGCTTATGCCCTCTAATCCCGGACAGACCC"
        "CAAATATGGCATAGCTGGTTGGGGGTACCTACTAGGCACAGCCGGAAGCA");
    StringSet<DnaString> needles;
    appendValue(needles, "GGGGTTAT");
    appendValue(needles, "CTAGCTAA");

    std::set<unsigned> hits, expectedHits {186, 226, 227, 234};

    std::mutex mtx;
    auto delegate = [&hits, &mtx](auto & iter, DnaString const & /*pattern*/, uint8_t /*errors*/)
    {
        std::lock_guard<std::mutex> lck(mtx);
        for (auto occ : getOccurrences(iter))
            hits.insert(occ);
    };
    Index<DnaString, BidirectionalIndex<FMIndex<> > > index(genome);

    find<1, 2>(delegate, index, needles, EditDistance(), Serial());
    SEQAN_ASSERT(hits == expectedHits);

    hits.clear();
    find<1, 2>(delegate, index, needles, EditDistance(), Parallel());
    SEQAN_ASSERT(hits == expectedHits);
}

#endif  // TESTS_FIND2_INDEX_APPROX_H_