File: repeat_base.h

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (598 lines) | stat: -rw-r--r-- 26,494 bytes parent folder | download
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, 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 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: David Weese <david.weese@fu-berlin.de>
// ==========================================================================

#ifndef SEQAN_HEADER_REPEAT_BASE_H
#define SEQAN_HEADER_REPEAT_BASE_H

#if SEQAN_ENABLE_PARALLELISM
#include <seqan/parallel.h>
#endif  // #if SEQAN_ENABLE_PARALLELISM

namespace seqan2 {

/*!
 * @class Repeat
 * @headerfile <seqan/index.h>
 * @brief Store information about a repeat.
 *
 * @signature template <typename TPos, typename TPeriod>
 *            struct Repeat;
 *
 * @tparam TPeriod Type to use for storing the repeat period. Default: 1
 * @tparam TPos Type to use for storing positions.
 *
 * @see findRepeats
 *
 * @var TPos Repeat::endPosition;
 * @brief The end position of the repeat of type <tt>TPos</tt>.
 *
 * @var TPos Repeat::beginPosition;
 * @brief The begin position of the repeat of type <tt>TPos</tt>.
 *
 * @var TPeriod Repeat::period;
 * @brief The period of the repeat of type <tt>TPeriod</tt>.
 */

    template <typename TPos, typename TPeriod>
    struct Repeat {
        TPos        beginPosition;
        TPos        endPosition;
        TPeriod        period;
    };

    template <typename TPos, typename TPeriod>
    struct Value< Repeat<TPos, TPeriod> > {
        typedef TPos Type;
    };

    template <typename TPos, typename TPeriod>
    struct Size< Repeat<TPos, TPeriod> > {
        typedef TPeriod Type;
    };


    template <typename TSize>
    struct RepeatFinderParams {
        TSize minRepeatLen;
        TSize maxPeriod;
    };

    // custom TSpec for our customized wotd-Index
    struct TRepeatFinder;

    template <typename TText>
    struct Cargo<Index<TText, IndexWotd<TRepeatFinder> > >
    {
        typedef Index<TText, IndexWotd<TRepeatFinder> >    TIndex;
        typedef typename Size<TIndex>::Type                    TSize;
        typedef RepeatFinderParams<TSize>                    Type;
    };


    // node predicate
    template <typename TText, typename TSpec>
    bool nodePredicate(Iter<Index<TText, IndexWotd<TRepeatFinder> >, TSpec> &it)
    {
//        return countOccurrences(it) * nodeDepth(it) >= cargo(container(it)).minRepeatLen;
        return countOccurrences(it) * repLength(it) >= cargo(container(it)).minRepeatLen;
    }

    // monotonic hull
    template <typename TText, typename TSpec>
    bool nodeHullPredicate(Iter<Index<TText, IndexWotd<TRepeatFinder> >, TSpec> &it)
    {
//        return nodeDepth(it) <= cargo(container(it)).maxPeriod;
        return repLength(it) <= cargo(container(it)).maxPeriod;
    }

    template <typename TPos>
    struct RepeatLess_ : public std::function<bool(TPos, TPos)>
    {
        // key less
        inline bool operator() (TPos const &a, TPos const &b) const {
            return posLess(a, b);
        }
    };

    template <typename TValue>
    inline bool _repeatMaskValue(TValue const &)
    {
        // TODO(holtgrew): Maybe use unknownValue<TValue>() instead of specializing for all alphabets, especially since we have Rna5 now and might want Rna5Q later.
        return false;
    }

    template <>
    inline bool _repeatMaskValue(Dna5 const &val)
    {
        return val == unknownValue<Dna5>(); // 'N'
    }

    template <>
    inline bool _repeatMaskValue(Dna5Q const &val)
    {
        return val == unknownValue<Dna5Q>(); // 'N'
    }

    template <>
    inline bool _repeatMaskValue(Iupac const &val)
    {
        return val == unknownValue<Iupac>(); // 'N'
    }
/*
    template <>
    inline bool _repeatMaskValue(AminoAcid val)
    {
        return val == 'X';
    }
*/
/*!
 * @fn findRepeats
 * @headerfile <seqan/index.h>
 * @brief Search for repeats in a text.
 *
 * @signature void findRepeats(repeatString, text, minRepeatLength[, maxPeriod]);
 *
 * @param[out] repeatString    A @link String @endlink of @link Repeat @endlink objects.
 * @param[in]  text            The text to search repeats in.  Types: @link ContainerConcept @endlink
 * @param[in]  minRepeatLength The minimum length each reported repeat must have.
 * @param[in]  maxPeriod       Optionally, the maximal period that reported repeats can have. Default: 1
 *
 * Subsequences of undefined values/<tt>N</tt>s will always be reported.
 *
 * @section Examples
 *
 * The following demonstrates finding repeats of period 3.
 *
 * @include demos/dox/index/find_repeats.cpp
 *
 * @code{.console}
 * # of repeats: 15
 * i == 0, beginPosition = 3, endPosition = 7, period = 1
 * i == 1, beginPosition = 46, endPosition = 53, period = 1
 * i == 2, beginPosition = 101, endPosition = 105, period = 1
 * i == 3, beginPosition = 105, endPosition = 109, period = 1
 * i == 4, beginPosition = 164, endPosition = 169, period = 1
 * i == 5, beginPosition = 291, endPosition = 297, period = 1
 * i == 6, beginPosition = 319, endPosition = 327, period = 1
 * i == 7, beginPosition = 400, endPosition = 404, period = 1
 * i == 8, beginPosition = 442, endPosition = 446, period = 1
 * i == 9, beginPosition = 468, endPosition = 473, period = 1
 * i == 10, beginPosition = 476, endPosition = 480, period = 1
 * i == 11, beginPosition = 507, endPosition = 513, period = 1
 * i == 12, beginPosition = 561, endPosition = 566, period = 1
 * i == 13, beginPosition = 623, endPosition = 627, period = 1
 * i == 14, beginPosition = 655, endPosition = 659, period = 1
 * @endcode
 *
 * @see AlphabetWithUnknownValueConcept#unknownValue
 * @see Repeat
 */
// TODO(holtgrew): minRepeatLength is 1-off.

    // period-1 optimization
    template <typename TRepeatStore, typename TString, typename TRepeatSize>
    inline void findRepeats(TRepeatStore &repString, TString const &text, TRepeatSize minRepeatLen)
    {
        typedef typename Value<TRepeatStore>::Type    TRepeat;
        typedef typename Iterator<TString const>::Type    TIterator;
        typedef typename Size<TString>::Type        TSize;

#if SEQAN_ENABLE_PARALLELISM
        typedef typename Value<TString>::Type        TValue;

        if (length(text) > (TSize)(omp_get_max_threads() * 2 * minRepeatLen)) {
            // std::cerr << ">>> PARALLEL WABOOGIE!" << std::endl;
            // std::cerr << "omp_get_max_threads() == " << omp_get_max_threads() << std::endl;
            // Parallel case.

            // NOTE(holtgrew): The minimum text length check above makes it impossible that more than two chunks are
            // required to form an otherwise too short repeat.

            // TODO(holtgrew): Load balancing? Probably not worth it.
            String<TSize> splitters;
            String<TRepeatStore> threadLocalStores;

            // Each threads finds repeats on its chunk in parallel.
            #pragma omp parallel
            {
                // We have to determine the number of available threads at this point.  We will use the number of thread
                // local stores to determin the number of available threads later on.
                #if _OPENMP >= 202011
                #pragma omp masked
                #else
                #pragma omp master
                #endif
                {
                    // std::cerr << "omp_get_num_threads() == " << omp_get_num_threads() << std::endl;
                    computeSplitters(splitters, length(text), omp_get_num_threads());
                    resize(threadLocalStores, omp_get_num_threads());
                }  // end of #pragma omp masked
                #pragma omp barrier

                int const t = omp_get_thread_num();
                TRepeatStore & store = threadLocalStores[t];

                TRepeat rep;
                rep.beginPosition = 0;
                rep.endPosition = 0;
                rep.period = 1;

                // Flags used for force-adding repeats for the chunks that have a left/right neighbour.
                bool forceFirst = t > 0;
                bool forceLast = (t + 1) < omp_get_num_threads();
                // #pragma omp critical
                // std::cerr << "omp_get_num_threads() == " << omp_get_num_threads() << std::endl;

                TIterator it = iter(text, splitters[t], Standard());
                TIterator itEnd = iter(text, splitters[t + 1], Standard());
                if (it != itEnd)
                {
                    TValue last = *it;
                    TSize repLeft = 0;
                    TSize repRight = 1;

                    for (++it; it != itEnd; ++it, ++repRight)
                    {
                        if (*it != last)
                        {
                            // #pragma omp critical
                            // std::cerr << "t == " << t << ", last == " << last << ", repRight = " << repRight << ", repLeft == " << repLeft << ", minRepeatLen = " << minRepeatLen << ", forceFirst = " << forceFirst << std::endl;
                            if (_repeatMaskValue(last) || (TRepeatSize)(repRight - repLeft) > minRepeatLen || forceFirst)
                            {
                                forceFirst = false;
                                // insert repeat
                                rep.beginPosition = splitters[t] + repLeft;
                                rep.endPosition = splitters[t] + repRight;
                                // #pragma omp critical
                                // std::cerr << "  t == " << t << ", append" << std::endl;
                                appendValue(store, rep);
                            }
                            repLeft = repRight;
                            last = *it;
                        }
                    }
                    // #pragma omp critical
                    // std::cerr << "t == " << t << ", last == " << last << ", repRight = " << repRight << ", repLeft == " << repLeft << ", minRepeatLen = " << minRepeatLen << ", forceLast = " << forceLast << std::endl;
                    if (_repeatMaskValue(last) || (TRepeatSize)(repRight - repLeft) > minRepeatLen || forceLast)
                    {
                        // Insert repeat but only if it is not already in there.
                        if (empty(store) || (back(store).beginPosition != repLeft && back(store).endPosition != repRight))
                        {
                            rep.beginPosition = splitters[t] + repLeft;
                            rep.endPosition = splitters[t] + repRight;
                            // #pragma omp critical
                            // std::cerr << "  t == " << t << ", append" << std::endl;
                            appendValue(store, rep);
                        }
                    }
                }
            }  // end of #pragma omp parallel

            // std::cerr << ",-- REPEATS BEFORE MENDING\n";
            // for (unsigned i = 0; i < length(threadLocalStores); ++i)
            // {
            //     std::cerr << "| i = " << i << std::endl;
            //     for (unsigned j = 0; j < length(threadLocalStores[i]); ++j)
            //         std::cerr << "| threadLocalStores[" << i << "][" << j << "] == {" << threadLocalStores[i][j].beginPosition << ", " << threadLocalStores[i][j].endPosition << "}" << std::endl;
            // }
            // std::cerr << "`--" << std::endl;

            // Mend the splice points.
            //
            // We will copy out infixes described by fromPositions.
            String<Pair<TSize> > fromPositions;
            resize(fromPositions, length(threadLocalStores));
            for (unsigned i = 0; i < length(fromPositions); ++i)
            {
                fromPositions[i].i1 = 0;
                fromPositions[i].i2 = length(threadLocalStores[i]);
            }
            // First, merge repeats spanning blocks.  Do this iteratively until all has been merged.
            bool anyChange;
            do
            {
                anyChange = false;
                int lastNonEmpty = -1;
                for (unsigned i = 0; i < length(threadLocalStores); ++i)
                {
                    if (fromPositions[i].i1 == fromPositions[i].i2)
                        continue;  // Skip empty buckets.

                    if (lastNonEmpty != -1)
                    {
                        bool const adjacent = back(threadLocalStores[lastNonEmpty]).endPosition == front(threadLocalStores[i]).beginPosition;
                        bool const charsEqual = text[back(threadLocalStores[lastNonEmpty]).beginPosition] == text[front(threadLocalStores[i]).beginPosition];
                        if (adjacent && charsEqual)
                        {
                            anyChange = true;
                            back(threadLocalStores[lastNonEmpty]).endPosition = front(threadLocalStores[i]).endPosition;
                            fromPositions[i].i1 += 1;
                        }
                    }

                    if (fromPositions[i].i1 != fromPositions[i].i2)
                        lastNonEmpty = i;
                }
            }
            while (anyChange);
            // Then, remove any repeats in the beginning and end of blocks that are too short.
            for (unsigned i = 0; i < length(threadLocalStores); ++i)
            {
                if (fromPositions[i].i1 == fromPositions[i].i2)
                    continue;
                unsigned j = fromPositions[i].i1;
                TRepeatSize len = threadLocalStores[i][j].endPosition - threadLocalStores[i][j].beginPosition;
                if (!_repeatMaskValue(text[threadLocalStores[i][j].beginPosition]) &&  // Never remove mask value.
                    len <= minRepeatLen)
                    fromPositions[i].i1 += 1;
                if (fromPositions[i].i1 == fromPositions[i].i2)
                    continue;
                j = fromPositions[i].i2 - 1;
                len = threadLocalStores[i][j].endPosition - threadLocalStores[i][j].beginPosition;
                if (!_repeatMaskValue(text[threadLocalStores[i][j].beginPosition]) &&  // Never remove mask value.
                    len <= minRepeatLen)
                    fromPositions[i].i2 -= 1;
            }
            // Last, build splitters for output in parallel.
            String<unsigned> outSplitters;
            appendValue(outSplitters, 0);
            for (unsigned i = 0; i < length(threadLocalStores); ++i)
                appendValue(outSplitters, back(outSplitters) + fromPositions[i].i2 - fromPositions[i].i1);

            // std::cerr << ",-- REPEATS AFTER MENDING\n";
            // for (unsigned i = 0; i < length(threadLocalStores); ++i)
            // {
            //     std::cerr << "| i = " << i << std::endl;
            //     std::cerr << "`--, fromPositions[" << i << "] = (" << fromPositions[i].i1 << ", " << fromPositions[i].i2 << std::endl;
            //     for (unsigned j = 0; j < length(threadLocalStores[i]); ++j)
            //         std::cerr << "   | threadLocalStores[" << i << "][" << j << "] == {" << threadLocalStores[i][j].beginPosition << ", " << threadLocalStores[i][j].endPosition << "}" << std::endl;
            // }
            // std::cerr << "    `--" << std::endl;

            // Allocate memory.
            clear(repString);
            resize(repString, back(outSplitters));

            // Copy back the repeats in parallel.
            unsigned nt = length(threadLocalStores);
            (void) nt;  // Otherwise, GCC 4.6 warns, does not see it used in pragma clause below.
            #pragma omp parallel num_threads(nt)
            {
                int const t = omp_get_thread_num();
                arrayCopy(iter(threadLocalStores[t], fromPositions[t].i1, Standard()),
                          iter(threadLocalStores[t], fromPositions[t].i2, Standard()),
                          iter(repString, outSplitters[t], Standard()));
            }  // end of #pragma omp parallel
        } else {
#endif  // #if SEQAN_ENABLE_PARALLELISM
            // Sequential case.
            TRepeat rep;
            rep.period = 1;
            clear(repString);

            TIterator it = begin(text, Standard());
            TIterator itEnd = end(text, Standard());
            if (it == itEnd) return;

            TSize repLen = 1;
            for (++it; it != itEnd; ++it)
            {
                if (*it != *(it-1))
                {
                    if (_repeatMaskValue(*(it-1)) || repLen > (TSize)minRepeatLen)
                    {
                        // insert repeat
                        rep.endPosition = it - begin(text, Standard());
                        rep.beginPosition = rep.endPosition - repLen;
                        //                    std::cerr<<"left:"<<rep.beginPosition<<"  right:"<<rep.endPosition<<"  length:"<<posSub(rep.endPosition,rep.beginPosition)<<"  period:"<<rep.period<<std::endl;
                        appendValue(repString, rep);
                    }
                    repLen = 1;
                } else
                    ++repLen;
            }
            if (_repeatMaskValue(*(it-1)) || repLen > (TSize)minRepeatLen)
            {
                // insert repeat
                rep.endPosition = length(text);
                rep.beginPosition = rep.endPosition - repLen;
                //            std::cerr<<"left:"<<rep.beginPosition<<"  right:"<<rep.endPosition<<"  length:"<<posSub(rep.endPosition,rep.beginPosition)<<"  period:"<<rep.period<<std::endl;
                appendValue(repString, rep);
            }
#if SEQAN_ENABLE_PARALLELISM
        }
#endif  // #if SEQAN_ENABLE_PARALLELISM
        // #pragma omp critical
        // {
        //     std::cerr << "thread #" << omp_get_thread_num() << " REPEATS:";
        //     for (unsigned i = 0; i < length(repString); ++i) {
        //         std::cerr << " (" << repString[i].beginPosition << ", " << repString[i].endPosition << ", " << repString[i].period << ")";
        //     }
        //     std::cerr << std::endl;
        // }
    }

    // TODO(holtgrew): Why for TString const and StringSet<> const?
    template <typename TRepeatStore, typename TString, typename TSpec, typename TRepeatSize>
    inline void findRepeats(TRepeatStore &repString, StringSet<TString, TSpec> const &text, TRepeatSize minRepeatLen)
    {
        typedef typename Value<TRepeatStore>::Type    TRepeat;
        typedef typename Iterator<TString>::Type    TIterator;
        typedef typename Value<TString>::Type        TValue;
        typedef typename Size<TString>::Type        TSize;

        TRepeat rep;
        rep.period = 1;
        clear(repString);

        for (unsigned i = 0; i < length(text); ++i)
        {
            TIterator it = begin(text[i], Standard());
            TIterator itEnd = end(text[i], Standard());
            if (it == itEnd) continue;

            TValue last = *it;
            TSize repLeft = 0;
            TSize repRight = 1;
            rep.beginPosition.i1 = i;
            rep.endPosition.i1 = i;

            for (++it; it != itEnd; ++it, ++repRight)
            {
                if (last != *it)
                {
                    if (_repeatMaskValue(last) || (TRepeatSize)(repRight - repLeft) > minRepeatLen)
                    {
                        // insert repeat
                        rep.beginPosition.i2 = repLeft;
                        rep.endPosition.i2 = repRight;
//                        std::cerr<<"left:"<<rep.beginPosition<<"  right:"<<rep.endPosition<<"  length:"<<posSub(rep.endPosition,rep.beginPosition)<<"  period:"<<rep.period<<std::endl;
                        appendValue(repString, rep);
                    }
                    repLeft = repRight;
                    last = *it;
                }
            }
            if (_repeatMaskValue(last) || (TRepeatSize)(repRight - repLeft) > minRepeatLen)
            {
                // insert repeat
                rep.beginPosition.i2 = repLeft;
                rep.endPosition.i2 = repRight;
//                std::cerr<<"left:"<<rep.beginPosition<<"  right:"<<rep.endPosition<<"  length:"<<posSub(rep.endPosition,rep.beginPosition)<<"  period:"<<rep.period<<std::endl;
                appendValue(repString, rep);
            }
        }
    }

    // main function
    template <typename TRepeatStore, typename TText, typename TRepeatSize, typename TPeriodSize>
    void findRepeats(TRepeatStore &repString, TText const &text, TRepeatSize minRepeatLen, TPeriodSize maxPeriod)
    {
        typedef Index<TText, IndexWotd<TRepeatFinder> >                    TIndex;
        typedef typename Size<TIndex>::Type                                    TSize;
        typedef typename Iterator<TIndex, TopDown<ParentLinks<> > >::Type    TNodeIterator;
        typedef typename Fibre<TIndex, FibreSA>::Type const                TSA;
        typedef typename Infix<TSA>::Type                                    TOccString;
        typedef typename Iterator<TOccString>::Type                            TOccIterator;

        typedef typename Value<TRepeatStore>::Type                            TRepeat;
        typedef typename Value<TOccString>::Type                            TOcc;

        typedef std::map<TOcc,TRepeat,RepeatLess_<TOcc> >                    TRepeatList;

        if (maxPeriod < 1) return;
        if (maxPeriod == 1)
        {
            findRepeats(repString, text, minRepeatLen);
            return;
        }

        TIndex        index(text);
        TRepeatList list;

        // set repeat finder parameters
        cargo(index).minRepeatLen = minRepeatLen;
        cargo(index).maxPeriod = maxPeriod;

        TNodeIterator nodeIt(index);
        TOccIterator itA, itB, itRepBegin, itEnd;
        TRepeat rep;
        for (; !atEnd(nodeIt); goNext(nodeIt))
        {
            if (isRoot(nodeIt)) continue;

            // get occurrences
            TOccString occ = getOccurrences(nodeIt);
            itA = begin(occ, Standard());
            itEnd = end(occ, Standard());
            itRepBegin = itB = itA;

            TSize repLen = repLength(nodeIt);        // representative length
            if ((TSize)minRepeatLen <= repLen) continue;

            TSize diff, period = 0;                    // period of current repeat
            TSize repeatLen = 0;                    // overall length of current repeat
            TSize minLen = minRepeatLen - repLen;    // minimum repeat length minus length of representative

            for (++itB; itB != itEnd; ++itB)
            {
                diff = posSub(*itB, *itA);
                if (diff != period || getSeqNo(*itA) != getSeqNo(*itB))
                {
                    // is the repeat long enough?
                    if (repeatLen >= minLen)
                        // is the repeat self overlapping or connected?
                        if (parentRepLength(nodeIt) < period && period <= repLen)
                        {
                            // insert repeat
                            rep.beginPosition = *itRepBegin;
                            rep.endPosition = posAdd(*itA, period);
                            rep.period = period;
//                            std::cerr<<"left:"<<rep.beginPosition<<"  right:"<<rep.endPosition<<"  length:"<<posSub(rep.endPosition,rep.beginPosition)<<"  period:"<<rep.period<<std::endl;
                            list.insert(std::pair<TOcc,TRepeat>(rep.beginPosition, rep));
                        }
                    itRepBegin = itA;
                    period = diff;
                    repeatLen = 0;
                }
                repeatLen += period;
                itA = itB;
            }

            // is the last repeat long enough?
            if (repeatLen >= minLen)
                // is the repeat self overlapping or connected?
                if (parentRepLength(nodeIt) < period && period <= repLen)
                {
                    // insert repeat
                    rep.beginPosition = *itRepBegin;
                    rep.endPosition = posAdd(*itA, period);
                    rep.period = period;
//                    std::cerr<<"left:"<<rep.beginPosition<<"  right:"<<rep.endPosition<<"  length:"<<posSub(rep.endPosition,rep.beginPosition)<<"  period:"<<rep.period<<std::endl;
                    list.insert(std::pair<TOcc,TRepeat>(rep.beginPosition, rep));
                }
        }

        // copy low-complex regions to result string
        clear(repString);
        reserve(repString, list.size(), Exact());
        typename TRepeatList::const_iterator lit = list.begin();
        typename TRepeatList::const_iterator litEnd = list.end();
        for (; lit != litEnd; ++lit)
            appendValue(repString, (*lit).second);
    }

}    // namespace seqan2

#endif