File: find_multiple_bfam.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 (366 lines) | stat: -rw-r--r-- 12,553 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
// ==========================================================================
//                 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: Andreas Gogol-Doering <andreas.doering@mdc-berlin.de>
// ==========================================================================

#ifndef SEQAN_HEADER_FIND_MULTIPLE_BFAM_H
#define SEQAN_HEADER_FIND_MULTIPLE_BFAM_H

namespace seqan2
{

//////////////////////////////////////////////////////////////////////////////
// MultiBfam
//////////////////////////////////////////////////////////////////////////////

/*!
 * @class MultiBfamPattern
 * @extends Pattern
 * @headerfile <seqan/find.h>
 * @brief Multi-Pattern Backward Factor Automaton Matching.
 *
 * @signature template <typename TNeedle, typename TAutomaton>
 *            class Pattern<TNeedle, MultiBfam<TAutomaton> >;
 *
 * @tparam TAutomaton A tag that specifies the used automaton. Default: @link OracleMultiBfamPattern @endlink.
 * @tparam TNeedle    The needle type. Types: String
 *
 * @see BfamPattern
 */

/*!
 * @class OracleMultiBfamPattern
 * @extends MultiBfamPattern
 * @headerfile <seqan/find.h>
 * @brief Multi-Pattern Backward Factor Automaton Matching using an oracle automaton.
 *
 * @signature template <typename TNeedle>
 *            class Pattern<TNeedle, MultiBfam<Oracle> >;
 *
 * @tparam TNeedle The needle type. Types: String
 *
 * @see OracleBfamPattern
 */

//struct Oracle //defined in find_bom.h

template <typename TSpec = Oracle>
struct MultiBfam; //multiple backward factor automaton searching

[[deprecated]]
typedef MultiBfam<Oracle> SBomAlgo; //deprecated

//////////////////////////////////////////////////////////////////////////////

template <typename TNeedle, typename TSpec>
class Pattern<TNeedle, MultiBfam<TSpec> >
{
//____________________________________________________________________________
public:
    typedef typename Value<TNeedle>::Type TKeyword;
    typedef typename Position<TNeedle>::Type TNeedlePosition;
    typedef typename Size<TKeyword>::Type TSize;
    typedef typename Value<TKeyword>::Type TValue;
    typedef Graph<Automaton<TValue, void, WithoutEdgeId> > TGraph;

    //searching data: these members are initialized in _patternInit or during search
    TNeedlePosition * position;        //pointer to last found position
    TNeedlePosition * position_end; //end of list in verify
            //note: if to_verify_begin == to_verify_end then searching in Haystack must go on

    //preprocessed data: these members are initialized in setHost
    Holder<TNeedle> data_host;
    TGraph automaton; //automaton of the reverse lmin-prefixes of the keywords
    String<String<TNeedlePosition> > terminals; //map of terminal states in automaton

    TSize lmin;    //min length of keyword

//____________________________________________________________________________

    Pattern():
        lmin(0)
    {
    }

    template <typename TNeedle2>
    Pattern(TNeedle2 && ndl,
            SEQAN_CTOR_DISABLE_IF(IsSameType<typename std::remove_reference<TNeedle2>::type const &, Pattern const &>))
    {
        setHost(*this, std::forward<TNeedle2>(ndl));
        ignoreUnusedVariableWarning(dummy);
    }

    ~Pattern()
    {}
//____________________________________________________________________________

private:
    Pattern(Pattern const& other);
    Pattern const & operator=(Pattern const & other);
    Pattern(Pattern && other);
    Pattern & operator=(Pattern && other);
//____________________________________________________________________________
};

//////////////////////////////////////////////////////////////////////////////


template <typename TNeedle, typename TStrs>
inline void
_buildAutomatonMultiBfam(Pattern<TNeedle, MultiBfam<Oracle> > & me,
                          TStrs const & strs)
{
    createSetOracle(me.automaton, me.terminals, strs);
}

template <typename TNeedle, typename TStrs>
inline void
_buildAutomatonMultiBfam(Pattern<TNeedle, MultiBfam<Trie> > & me,
                          TStrs const & strs)
{
    createSetSuffixTrie(me.automaton, me.terminals, strs);
}

//____________________________________________________________________________

template <typename TNeedle, typename TAutomaton>
void _reinitPattern(Pattern<TNeedle, MultiBfam<TAutomaton> > & me)
{
    typedef typename Value<TNeedle>::Type TKeyword;
    typedef typename Value<TKeyword>::Type TValue;
    typedef typename Size<TKeyword>::Type TSize;

    //me.needle
    TNeedle & ndl = needle(me);

    //determine lmin
    TSize len = length(ndl);
    if (len == 0)
    {
        me.lmin = 0;
        return;
    }

    me.lmin = length(ndl[0]);
    for (TSize i = 1; i < len; ++i)
    {
        TSize len = length(ndl[i]);
        if (len < me.lmin)
        {
            me.lmin = len;
        }
    }

    if (me.lmin == 0) return;

    //collect reverse prefixes for automaton
    String<String<TValue> > strs;
    resize(strs, len);
    for (unsigned int i = 0; i < len; ++i)
    {
        strs[i] = prefix(ndl[i], me.lmin);
        reverse(strs[i]);
    }

    //build automaton
    _buildAutomatonMultiBfam(me, strs);
}

//////////////////////////////////////////////////////////////////////////////

template <typename TNeedle, typename TAutomaton>
inline typename Size<TNeedle>::Type
position(Pattern<TNeedle, MultiBfam<TAutomaton> > & me)
{
    return *(me.position);
}

//////////////////////////////////////////////////////////////////////////////

//called when search begins
template <typename TNeedle, typename TAutomaton>
inline void _patternInit (Pattern<TNeedle, MultiBfam<TAutomaton> > & me)
{
    me.position = 0;
    me.position_end = 0;
}

//////////////////////////////////////////////////////////////////////////////

//test whether the me.lmin prefix of the *(me.position)-th pattern matches haystack beginning at *it
//the Haystack is long enough that *(it+me.lmin) is a valid value.
//default implementation: it is assumed that if me.automaton parses a me.lmin-length string S, then S is a pattern
template <typename TNeedle, typename TAutomaton, typename THaystackIterator>
inline bool
_startVerifyMultiBfam(Pattern<TNeedle, MultiBfam<TAutomaton> > &,
                       THaystackIterator)
{
    return true;
}

//specialization for oracles: must test explicitely, since set-oracles may also parse me.lmin-length strings that are no patterns
template <typename TNeedle, typename THaystackIterator>
inline bool
_startVerifyMultiBfam(Pattern<TNeedle, MultiBfam<Oracle> > & me,
                       THaystackIterator tit)
{
    typedef typename Value<TNeedle>::Type TKeyword;
    typedef typename Iterator<TKeyword, Standard>::Type TKeywordIterator;
    TKeyword & keyword = needle(me)[*(me.position)];
    TKeywordIterator kit = begin(keyword);
    TKeywordIterator kit_end = kit + me.lmin;
    while (kit != kit_end)
    {
        if (*kit != *tit)
        {
            return false;
        }
        ++kit;
        ++tit;
    }

    return true;
}

//____________________________________________________________________________

template <typename TFinder, typename TAutomaton, typename TNeedle>
inline bool find(TFinder & finder,
                 Pattern<TNeedle, MultiBfam<TAutomaton> > & me)
{
    typedef typename Haystack<TFinder>::Type THaystack;
    typedef typename Iterator<THaystack, Standard>::Type THaystackIterator;
    typedef typename Value<TNeedle>::Type TKeyword;
    typedef typename Value<TKeyword>::Type TValue;
    typedef typename Size<TKeyword>::Type TSize;
    typedef typename Iterator<TKeyword, Standard>::Type TKeywordIterator;
    typedef Graph<Automaton<TValue, void, WithoutEdgeId> > TGraph;
    typedef typename VertexDescriptor<TGraph>::Type TVertexDescriptor;

    if (me.lmin == 0) return false;

    //some variables
    THaystackIterator haystack_end = end(haystack(finder));
    THaystackIterator it1;
    THaystackIterator it1_end = haystack_end - me.lmin + 1;
    THaystackIterator it2;
    TGraph & automaton = me.automaton;
    TVertexDescriptor root = getRoot(automaton);
    TVertexDescriptor nil_ = getNil<TVertexDescriptor>();
    TVertexDescriptor current;
    TKeywordIterator kit;
    TKeywordIterator kit_end;
    THaystackIterator tit;
    TKeyword * p_keyword;
    TSize len;

    if (empty(finder))
    {
//START
        _patternInit(me);
        _finderSetNonEmpty(finder);
        it1 = hostIterator(finder);
    }
    else
    {
//RESUME
        it1 = hostIterator(finder);
        goto VERIFY_NEXT;
    }

//SEARCH
    while (it1 < it1_end)
    {
        it2 = it1 + me.lmin; //me.lmin > 0 => it1 != it2
        current = root;
        while (true)
        {
            --it2;
            current = getSuccessor(automaton, current, *it2);
            if (current == nil_)
            {
//SKIP
                it1 = it2 + 1;
                break;
            }
            if (it1 == it2)
            {
//VERIFY
                me.position = begin(property(me.terminals, current), Standard());
                me.position_end = end(property(me.terminals, current), Standard());
                SEQAN_ASSERT_NEQ(me.position, me.position_end);

                if (_startVerifyMultiBfam(me, it1)) //this returns true if the lmin-length prefixe matches
                {
                    while (me.position != me.position_end)
                    {
                        p_keyword = & needle(me)[*me.position];
                        len = length(*p_keyword);
                        if ((it1 + len) <= haystack_end)
                        {
                            //compare rest
                            kit = begin(*p_keyword) + me.lmin;
                            kit_end = end(*p_keyword);
                            tit = it1 + me.lmin;
                            while (true)
                            {
                                if (kit == kit_end)
                                {
//MATCH FOUND
                                    setPosition(finder, it1 - begin(haystack(finder), Standard()));
                                    _setFinderLength(finder, length(needle(*p_keyword)));
                                    _setFinderEnd(finder, position(finder) + length(finder));
                                    return true;
                                }
                                if (*kit != *tit) break;
                                ++kit;
                                ++tit;
                            }
                        }
VERIFY_NEXT:
                        ++me.position;
                    }
                }
                ++it1;
                break;
            }

        }
    }
    return false;
}

//////////////////////////////////////////////////////////////////////////////

}// namespace seqan2

#endif //#ifndef SEQAN_HEADER_...