1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,561 @@ |
1 |
+// ========================================================================== |
|
2 |
+// SeqAn - The Library for Sequence Analysis |
|
3 |
+// ========================================================================== |
|
4 |
+// Copyright (c) 2006-2018, Knut Reinert, FU Berlin |
|
5 |
+// All rights reserved. |
|
6 |
+// |
|
7 |
+// Redistribution and use in source and binary forms, with or without |
|
8 |
+// modification, are permitted provided that the following conditions are met: |
|
9 |
+// |
|
10 |
+// * Redistributions of source code must retain the above copyright |
|
11 |
+// notice, this list of conditions and the following disclaimer. |
|
12 |
+// * Redistributions in binary form must reproduce the above copyright |
|
13 |
+// notice, this list of conditions and the following disclaimer in the |
|
14 |
+// documentation and/or other materials provided with the distribution. |
|
15 |
+// * Neither the name of Knut Reinert or the FU Berlin nor the names of |
|
16 |
+// its contributors may be used to endorse or promote products derived |
|
17 |
+// from this software without specific prior written permission. |
|
18 |
+// |
|
19 |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
20 |
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
21 |
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
22 |
+// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE |
|
23 |
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
24 |
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
25 |
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
26 |
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
27 |
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
28 |
+// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
29 |
+// DAMAGE. |
|
30 |
+// |
|
31 |
+// ========================================================================== |
|
32 |
+// Author: Rene Rahn <rene.rahn@fu-berlin.de> |
|
33 |
+// ========================================================================== |
|
34 |
+// Jst traversal buffer used to construct sequence contents. |
|
35 |
+// ========================================================================== |
|
36 |
+ |
|
37 |
+#ifndef EXTRAS_INCLUDE_SEQAN_JOURNALED_STRING_TREE_JOURNALED_SEQUENCE_BUFFER_H_ |
|
38 |
+#define EXTRAS_INCLUDE_SEQAN_JOURNALED_STRING_TREE_JOURNALED_SEQUENCE_BUFFER_H_ |
|
39 |
+ |
|
40 |
+namespace seqan { |
|
41 |
+ |
|
42 |
+// ============================================================================ |
|
43 |
+// Forwards |
|
44 |
+// ============================================================================ |
|
45 |
+ |
|
46 |
+// ============================================================================ |
|
47 |
+// Tags, Classes, Enums |
|
48 |
+// ============================================================================ |
|
49 |
+ |
|
50 |
+// ---------------------------------------------------------------------------- |
|
51 |
+// Tag Member Tags |
|
52 |
+// ---------------------------------------------------------------------------- |
|
53 |
+ |
|
54 |
+struct JstBufferSetMember_; |
|
55 |
+typedef Tag<JstBufferSetMember_> JstBufferSetMember; |
|
56 |
+ |
|
57 |
+struct JstBufferDeltaMapMember_; |
|
58 |
+typedef Tag<JstBufferDeltaMapMember_> JstBufferDeltaMapMember; |
|
59 |
+ |
|
60 |
+// ---------------------------------------------------------------------------- |
|
61 |
+// Class JstBuffer_ |
|
62 |
+// ---------------------------------------------------------------------------- |
|
63 |
+ |
|
64 |
+template <typename TJournaledStringTree, typename TDirection = ForwardTraversal> |
|
65 |
+class JstBuffer_ |
|
66 |
+{ |
|
67 |
+public: |
|
68 |
+ |
|
69 |
+ // __ Types ___________________________________________________________________ |
|
70 |
+ |
|
71 |
+ typedef typename Member<JstBuffer_, JstBufferSetMember>::Type TJournaledSet; |
|
72 |
+ typedef typename Member<TJournaledStringTree, JstSourceMember>::Type TSource; |
|
73 |
+ typedef typename Iterator<TSource, Rooted>::Type TSourceIterator; |
|
74 |
+ |
|
75 |
+ typedef typename Member<JstBuffer_, JstBufferDeltaMapMember>::Type TDeltaMap; |
|
76 |
+ typedef typename Iterator<TDeltaMap, Standard>::Type TDeltaIterator; |
|
77 |
+ |
|
78 |
+ typedef typename Size<TSource>::Type TSourceSize; |
|
79 |
+ typedef String<TSourceSize> TStringPositions; |
|
80 |
+ |
|
81 |
+ // __ Members _________________________________________________________________ |
|
82 |
+ |
|
83 |
+ bool _isSynchronized; |
|
84 |
+ |
|
85 |
+ TDeltaMap* _deltaMapPtr; |
|
86 |
+ |
|
87 |
+ TSourceIterator _sourceBegin; // Iterator to the clipped begin position of the source. |
|
88 |
+ TSourceIterator _sourceEnd; // Iterator to the clipped end position of the source. |
|
89 |
+ |
|
90 |
+ TDeltaIterator _deltaRangeBegin; |
|
91 |
+ TDeltaIterator _deltaRangeEnd; |
|
92 |
+ |
|
93 |
+ TStringPositions _startPositions; // The begin positions of the strings within the chunk. |
|
94 |
+ TJournaledSet _journaledSet; // The actual sequences over the current chunk. |
|
95 |
+ |
|
96 |
+ // __ Constructor _____________________________________________________________ |
|
97 |
+ |
|
98 |
+ JstBuffer_() |
|
99 |
+ {} |
|
100 |
+ |
|
101 |
+ JstBuffer_(TDeltaMap & map) : _deltaMapPtr(&map) |
|
102 |
+ {} |
|
103 |
+ |
|
104 |
+ JstBuffer_(TDeltaMap & map, TSourceIterator const & srcBeg, TSourceIterator const & srcEnd) : |
|
105 |
+ _deltaMapPtr(&map), |
|
106 |
+ _sourceBegin(srcBeg), |
|
107 |
+ _sourceEnd(srcEnd) |
|
108 |
+ { |
|
109 |
+ sync(*this); |
|
110 |
+ } |
|
111 |
+ |
|
112 |
+}; |
|
113 |
+ |
|
114 |
+// ============================================================================ |
|
115 |
+// Metafunctions |
|
116 |
+// ============================================================================ |
|
117 |
+ |
|
118 |
+// ---------------------------------------------------------------------------- |
|
119 |
+// Metafunction Size |
|
120 |
+// ---------------------------------------------------------------------------- |
|
121 |
+ |
|
122 |
+template <typename TJournaledStringTree, typename TSpec> |
|
123 |
+struct Size<JstBuffer_<TJournaledStringTree, TSpec> > |
|
124 |
+{ |
|
125 |
+ typedef typename Source<TJournaledStringTree>::Type TSource_; |
|
126 |
+ typedef typename Size<TSource_>::Type Type; |
|
127 |
+}; |
|
128 |
+ |
|
129 |
+// ---------------------------------------------------------------------------- |
|
130 |
+// Metafunction Member [JstSeqBufferJournaledSet] |
|
131 |
+// ---------------------------------------------------------------------------- |
|
132 |
+ |
|
133 |
+template <typename TJournaledStringTree, typename TSpec> |
|
134 |
+struct Member<JstBuffer_<TJournaledStringTree, TSpec>, JstBufferSetMember> |
|
135 |
+{ |
|
136 |
+ typedef typename Member<TJournaledStringTree, JstSourceMember>::Type TSource_; |
|
137 |
+ typedef StringSet<TSource_, Owner<JournaledSet> > Type; |
|
138 |
+}; |
|
139 |
+ |
|
140 |
+// ---------------------------------------------------------------------------- |
|
141 |
+// Metafunction Member [JstBufferDeltaMapMember] |
|
142 |
+// ---------------------------------------------------------------------------- |
|
143 |
+ |
|
144 |
+template <typename TJst, typename TSpec> |
|
145 |
+struct Member<JstBuffer_<TJst, TSpec>, JstBufferDeltaMapMember> |
|
146 |
+{ |
|
147 |
+ typedef typename Member<TJst, JstDeltaMapMember>::Type Type; |
|
148 |
+}; |
|
149 |
+ |
|
150 |
+template <typename TJst, typename TSpec> |
|
151 |
+struct Member<JstBuffer_<TJst, TSpec> const, JstBufferDeltaMapMember> |
|
152 |
+{ |
|
153 |
+ typedef typename Member<TJst, JstDeltaMapMember>::Type const Type; |
|
154 |
+}; |
|
155 |
+ |
|
156 |
+// ============================================================================ |
|
157 |
+// Private Functions |
|
158 |
+// ============================================================================ |
|
159 |
+ |
|
160 |
+namespace impl |
|
161 |
+{ |
|
162 |
+ |
|
163 |
+// ---------------------------------------------------------------------------- |
|
164 |
+// Function impl::journalDelta() [DeltaTypeSnp] |
|
165 |
+// ---------------------------------------------------------------------------- |
|
166 |
+ |
|
167 |
+template <typename TTarget, typename TPos, typename TSnp> |
|
168 |
+inline void |
|
169 |
+journalDelta(TTarget & target, |
|
170 |
+ TPos refPos, |
|
171 |
+ TSnp const & snp, |
|
172 |
+ DeltaTypeSnp const & /*tag*/) |
|
173 |
+{ |
|
174 |
+ typedef typename JournalType<TTarget>::Type TJournalEntries; |
|
175 |
+ typedef typename Iterator<TJournalEntries, Standard>::Type TEntriesIterator; |
|
176 |
+ typedef typename Position<TJournalEntries>::Type TEntryPos; |
|
177 |
+ |
|
178 |
+ TEntriesIterator entryIt = end(_journalEntries(target), Standard()) -1; |
|
179 |
+ SEQAN_ASSERT_EQ(entryIt->segmentSource, SOURCE_ORIGINAL); |
|
180 |
+ SEQAN_ASSERT_GEQ(refPos, entryIt->physicalOriginPosition); |
|
181 |
+ SEQAN_ASSERT_GT(entryIt->physicalOriginPosition + entryIt->length, refPos); |
|
182 |
+ |
|
183 |
+ TEntryPos virtPos = entryIt->virtualPosition + (refPos - entryIt->physicalOriginPosition); |
|
184 |
+ appendValue(target._insertionBuffer, snp); |
|
185 |
+ _doRecordInsertion(target._journalEntries, entryIt, virtPos, length(target._insertionBuffer) - 1, 1u); |
|
186 |
+ entryIt = end(_journalEntries(target), Standard()) -1; |
|
187 |
+ ++virtPos; |
|
188 |
+ _doRecordErase(target._journalEntries, entryIt, virtPos, virtPos + 1); |
|
189 |
+} |
|
190 |
+ |
|
191 |
+// ---------------------------------------------------------------------------- |
|
192 |
+// Function impl::journalDelta() [DeltaTypeDel] |
|
193 |
+// ---------------------------------------------------------------------------- |
|
194 |
+ |
|
195 |
+template <typename TTarget, typename TPos, typename TSize> |
|
196 |
+inline void |
|
197 |
+journalDelta(TTarget & target, |
|
198 |
+ TPos refPos, |
|
199 |
+ TSize const & delLength, |
|
200 |
+ DeltaTypeDel const & /*tag*/) |
|
201 |
+{ |
|
202 |
+ typedef typename JournalType<TTarget>::Type TJournalEntries; |
|
203 |
+ typedef typename Iterator<TJournalEntries, Standard>::Type TEntryIterator; |
|
204 |
+ typedef typename Position<TJournalEntries>::Type TEntryPos; |
|
205 |
+ |
|
206 |
+ TEntryIterator entryIt = end(target._journalEntries, Standard()) - 1; |
|
207 |
+ SEQAN_ASSERT_EQ(entryIt->segmentSource, SOURCE_ORIGINAL); |
|
208 |
+ SEQAN_ASSERT_GEQ(refPos, entryIt->physicalOriginPosition); |
|
209 |
+ SEQAN_ASSERT_GT(entryIt->physicalOriginPosition + entryIt->length, refPos); |
|
210 |
+ |
|
211 |
+ target._length -= delLength; |
|
212 |
+ TEntryPos virtPos = entryIt->virtualPosition + (refPos - entryIt->physicalOriginPosition); |
|
213 |
+ _doRecordErase(target._journalEntries, entryIt, virtPos, virtPos + delLength); |
|
214 |
+ if (length(target._journalEntries) == 0) |
|
215 |
+ clear(target._insertionBuffer); |
|
216 |
+} |
|
217 |
+ |
|
218 |
+// ---------------------------------------------------------------------------- |
|
219 |
+// Function impl::journalDelta() [DeltaTypeIns] |
|
220 |
+// ---------------------------------------------------------------------------- |
|
221 |
+ |
|
222 |
+template <typename TTarget, typename TPos, typename TInsertion> |
|
223 |
+inline void |
|
224 |
+journalDelta(TTarget & target, |
|
225 |
+ TPos refPos, |
|
226 |
+ TInsertion const & insSeq, |
|
227 |
+ DeltaTypeIns const & /*tag*/) |
|
228 |
+{ |
|
229 |
+ typedef typename JournalType<TTarget>::Type TJournalEntries; |
|
230 |
+ typedef typename Iterator<TJournalEntries, Standard>::Type TEntryIterator; |
|
231 |
+ typedef typename Position<TJournalEntries>::Type TEntryPos; |
|
232 |
+ |
|
233 |
+ TEntryIterator entryIt = end(target._journalEntries, Standard()) - 1; |
|
234 |
+ SEQAN_ASSERT_EQ(entryIt->segmentSource, SOURCE_ORIGINAL); |
|
235 |
+ SEQAN_ASSERT_GEQ(refPos, entryIt->physicalOriginPosition); |
|
236 |
+ SEQAN_ASSERT_GEQ(entryIt->physicalOriginPosition + entryIt->length, refPos); // Special case for the insertion where it is valid to insert behind the sequence. |
|
237 |
+ |
|
238 |
+ target._length += length(insSeq); |
|
239 |
+ TEntryPos virtPos = entryIt->virtualPosition + (refPos - entryIt->physicalOriginPosition); |
|
240 |
+ TEntryPos physPos = length(target._insertionBuffer); |
|
241 |
+ append(target._insertionBuffer, insSeq); |
|
242 |
+ _doRecordInsertion(target._journalEntries, entryIt, virtPos, physPos, length(insSeq)); |
|
243 |
+} |
|
244 |
+ |
|
245 |
+// ---------------------------------------------------------------------------- |
|
246 |
+// Function impl::journalDelta() [DeltaTypeSV] |
|
247 |
+// ---------------------------------------------------------------------------- |
|
248 |
+ |
|
249 |
+template <typename TTarget, typename TPos, typename TSV> |
|
250 |
+inline void |
|
251 |
+journalDelta(TTarget & target, |
|
252 |
+ TPos refPos, |
|
253 |
+ TSV const & sv, |
|
254 |
+ DeltaTypeSV const & /*tag*/) |
|
255 |
+{ |
|
256 |
+ typedef typename JournalType<TTarget>::Type TJournalEntries; |
|
257 |
+ typedef typename Iterator<TJournalEntries, Standard>::Type TEntryIterator; |
|
258 |
+ typedef typename Position<TJournalEntries>::Type TEntryPos; |
|
259 |
+ |
|
260 |
+ TEntryIterator entryIt = end(target._journalEntries, Standard()) - 1; |
|
261 |
+ SEQAN_ASSERT_EQ(entryIt->segmentSource, SOURCE_ORIGINAL); |
|
262 |
+ SEQAN_ASSERT_GEQ(refPos, entryIt->physicalOriginPosition); |
|
263 |
+ SEQAN_ASSERT_GT(entryIt->physicalOriginPosition + entryIt->length, refPos); |
|
264 |
+ |
|
265 |
+ target._length -= sv.i1; |
|
266 |
+ TEntryPos virtPos = entryIt->virtualPosition + (refPos - entryIt->physicalOriginPosition); |
|
267 |
+ _doRecordErase(target._journalEntries, entryIt, virtPos, virtPos + sv.i1); |
|
268 |
+ |
|
269 |
+ entryIt = end(target._journalEntries, Standard()) - 1; |
|
270 |
+ target._length += length(sv.i2); |
|
271 |
+ TEntryPos physPos = length(target._insertionBuffer); |
|
272 |
+ append(target._insertionBuffer, sv.i2); |
|
273 |
+ _doRecordInsertion(target._journalEntries, entryIt, virtPos, physPos, length(sv.i2)); |
|
274 |
+} |
|
275 |
+ |
|
276 |
+// ---------------------------------------------------------------------------- |
|
277 |
+// Function impl::create() |
|
278 |
+// ---------------------------------------------------------------------------- |
|
279 |
+ |
|
280 |
+template <typename TMapIter, typename TJSIter> |
|
281 |
+struct JournaledStringCreateFunctor |
|
282 |
+{ |
|
283 |
+ TMapIter mapIt; |
|
284 |
+ TJSIter setIt; |
|
285 |
+ |
|
286 |
+ template <typename TTag> |
|
287 |
+ inline void |
|
288 |
+ operator()(TTag const & /*deltaType*/) |
|
289 |
+ { |
|
290 |
+ if ((*mapIt).deltaTypeEnd != DeltaEndType::IS_RIGHT) |
|
291 |
+ impl::journalDelta(*setIt, getDeltaPosition(*mapIt), deltaValue(mapIt, TTag()), TTag()); |
|
292 |
+ } |
|
293 |
+}; |
|
294 |
+ |
|
295 |
+template <typename TJst, typename TSpec> |
|
296 |
+inline void |
|
297 |
+create(JstBuffer_<TJst, TSpec> & buffer) |
|
298 |
+{ |
|
299 |
+ typedef typename Member<JstBuffer_<TJst, TSpec>, JstBufferSetMember>::Type TJSet; |
|
300 |
+ typedef typename Value<TJSet>::Type TJString; |
|
301 |
+ typedef typename Iterator<TJSet, Standard>::Type TJSetIter; |
|
302 |
+ typedef typename Member<JstBuffer_<TJst, TSpec>, JstBufferDeltaMapMember>::Type TDeltaMap; |
|
303 |
+ typedef typename Iterator<TDeltaMap, Standard>::Type TDeltaMapIter; |
|
304 |
+ |
|
305 |
+ SEQAN_ASSERT(!empty(buffer._startPositions)); |
|
306 |
+ |
|
307 |
+ clear(buffer._journaledSet); // Guarentee empty set. |
|
308 |
+ |
|
309 |
+ // Initialize the journaled set. |
|
310 |
+ resize(buffer._journaledSet, length(buffer._startPositions), Exact()); |
|
311 |
+ |
|
312 |
+ forEach(buffer._journaledSet, [&buffer](TJString &jStr) |
|
313 |
+ { |
|
314 |
+ setHost(jStr, host(container(buffer._sourceBegin))); |
|
315 |
+ }, Parallel()); |
|
316 |
+ |
|
317 |
+ // Construct the journaled string context |
|
318 |
+ |
|
319 |
+ Splitter<TJSetIter> jSetSplitter(begin(buffer._journaledSet, Standard()), end(buffer._journaledSet, Standard()), |
|
320 |
+ Parallel()); |
|
321 |
+ SEQAN_OMP_PRAGMA(parallel for) |
|
322 |
+ for (auto jobId = 0; jobId < static_cast<int>(length(jSetSplitter)); ++jobId) |
|
323 |
+ { |
|
324 |
+ impl::JournaledStringCreateFunctor<TDeltaMapIter, TJSetIter> f; |
|
325 |
+ f.mapIt = buffer._deltaRangeBegin; |
|
326 |
+ for (; f.mapIt != buffer._deltaRangeEnd; ++f.mapIt) |
|
327 |
+ { |
|
328 |
+ f.setIt = jSetSplitter[jobId]; |
|
329 |
+ if (SEQAN_UNLIKELY((*f.mapIt).deltaTypeEnd == DeltaEndType::IS_RIGHT)) |
|
330 |
+ continue; |
|
331 |
+ |
|
332 |
+ auto covIt = begin(getDeltaCoverage(*f.mapIt), Standard()) + |
|
333 |
+ (f.setIt - begin(buffer._journaledSet, Standard())); |
|
334 |
+ for (; f.setIt != jSetSplitter[jobId + 1]; ++f.setIt, ++covIt) |
|
335 |
+ { |
|
336 |
+ DeltaTypeSelector deltaSelector; |
|
337 |
+ if (*covIt) // If the current sequence covers the current delta. |
|
338 |
+ applyOnDelta(f, getDeltaType(*f.mapIt), deltaSelector); |
|
339 |
+ } |
|
340 |
+ } |
|
341 |
+ } |
|
342 |
+} |
|
343 |
+ |
|
344 |
+// ---------------------------------------------------------------------------- |
|
345 |
+// Function impl::synchronize() |
|
346 |
+// ---------------------------------------------------------------------------- |
|
347 |
+ |
|
348 |
+template <typename TJst, typename TSpec> |
|
349 |
+inline bool |
|
350 |
+synchronize(JstBuffer_<TJst, TSpec> & buffer) |
|
351 |
+{ |
|
352 |
+ typedef typename Size<JstBuffer_<TJst, TSpec> >::Type TSize; |
|
353 |
+ |
|
354 |
+ SEQAN_ASSERT(buffer._sourceBegin < buffer._sourceEnd); |
|
355 |
+ |
|
356 |
+ // TODO(rrahn): Implement block wise computation. |
|
357 |
+ // Discover overlapping deletions at begin and end. |
|
358 |
+ // 0123456789012345678901234... |
|
359 |
+ // xxxxx------------xxxxxxxx... |
|
360 |
+ // [------xxxxxxxx...[ // Beginning of the buffer lies within a deletion of a sequence. |
|
361 |
+ // |
|
362 |
+ // When detecting the deletion check if end point lies behind source begin position. |
|
363 |
+ // If false: ignore deletion. |
|
364 |
+ // If true: Add deletion point to the mapExtension -> Maybe we can even add it as a new branching point. |
|
365 |
+ // In this case we add a deletion right from the beginning. |
|
366 |
+ // We just communicate through this object anyway. -> Then the algorithm does not have to change. |
|
367 |
+ // If this is at the end: we cut the deletion -> But we have one at the same position which is just longer. |
|
368 |
+ // But you would cut after the deletion anyway. |
|
369 |
+ // We could only represent infixes over the journaled strings after they have been constructed? |
|
370 |
+ // So we have a fixed end position and begin position. |
|
371 |
+ |
|
372 |
+ buffer._deltaRangeBegin = begin(*buffer._deltaMapPtr, Standard()); |
|
373 |
+ buffer._deltaRangeEnd = end(*buffer._deltaMapPtr, Standard()); |
|
374 |
+ |
|
375 |
+// TODO(rrahn): Enable this when allowing chunking. |
|
376 |
+// buffer._deltaRangeBegin = std::lower_bound(begin(buffer._deltaMap, Standard()), |
|
377 |
+// end(buffer._deltaMap, Standard()), |
|
378 |
+// position(buffer._sourceBegin), DeltaExtensionCompareLessPos_()); |
|
379 |
+// buffer._deltaRangeEnd = std::lower_bound(begin(buffer._deltaMap, Standard()), end(buffer._deltaMap, Standard()), |
|
380 |
+// position(buffer._sourceEnd), DeltaExtensionCompareLessPos_()); |
|
381 |
+ |
|
382 |
+ // Stream from the beginning to the expected range to get the begin positions of the current segment. |
|
383 |
+ auto mapIt = begin(*buffer._deltaMapPtr, Standard()); |
|
384 |
+ resize(buffer._startPositions, length(getDeltaCoverage(*mapIt)), position(buffer._sourceBegin), Exact()); |
|
385 |
+ |
|
386 |
+ if (position(buffer._sourceBegin) == 0) // Special case: We also set the begin position to 0 even if there is an insertion at the 0th position. |
|
387 |
+ return true; |
|
388 |
+ |
|
389 |
+ for (; mapIt != buffer._deltaRangeBegin; ++mapIt) |
|
390 |
+ { |
|
391 |
+ auto net = netSize(mapIt); |
|
392 |
+ auto covBegin = begin(getDeltaCoverage(*mapIt), Standard()); |
|
393 |
+ for (auto covIt = covBegin; covIt != end(getDeltaCoverage(*mapIt), Standard()); ++covIt) |
|
394 |
+ { |
|
395 |
+ if (*covIt) |
|
396 |
+ { |
|
397 |
+ if (SEQAN_UNLIKELY(static_cast<TSize>(std::abs(net)) > buffer._startPositions[covIt - covBegin] && |
|
398 |
+ (net < 0))) |
|
399 |
+ buffer._startPositions[covIt - covBegin] = 0; // In case the entire prefix of this sequence is deleted. |
|
400 |
+ else |
|
401 |
+ buffer._startPositions[covIt - covBegin] += net; |
|
402 |
+ } |
|
403 |
+ } |
|
404 |
+ } |
|
405 |
+ buffer._isSynchronized = true; |
|
406 |
+ return true; |
|
407 |
+} |
|
408 |
+ |
|
409 |
+} // namespace impl |
|
410 |
+ |
|
411 |
+// ============================================================================ |
|
412 |
+// Public Functions |
|
413 |
+// ============================================================================ |
|
414 |
+ |
|
415 |
+// ---------------------------------------------------------------------------- |
|
416 |
+// Function sourceBegin() |
|
417 |
+// ---------------------------------------------------------------------------- |
|
418 |
+ |
|
419 |
+template <typename TJst, typename TSpec> |
|
420 |
+inline typename JstBuffer_<TJst, TSpec>::TSourceIterator |
|
421 |
+sourceBegin(JstBuffer_<TJst, TSpec> & buffer) |
|
422 |
+{ |
|
423 |
+ return buffer._sourceBegin; |
|
424 |
+} |
|
425 |
+ |
|
426 |
+// ---------------------------------------------------------------------------- |
|
427 |
+// Function setSourceBegin() |
|
428 |
+// ---------------------------------------------------------------------------- |
|
429 |
+ |
|
430 |
+template <typename TJst, typename TSpec, typename TSourceIter> |
|
431 |
+inline void |
|
432 |
+setSourceBegin(JstBuffer_<TJst, TSpec> & buffer, |
|
433 |
+ TSourceIter const & begin) |
|
434 |
+{ |
|
435 |
+ buffer._isSynchronized = false; |
|
436 |
+ buffer._sourceBegin = begin; |
|
437 |
+} |
|
438 |
+ |
|
439 |
+// ---------------------------------------------------------------------------- |
|
440 |
+// Function sourceEnd() |
|
441 |
+// ---------------------------------------------------------------------------- |
|
442 |
+ |
|
443 |
+template <typename TJst, typename TSpec> |
|
444 |
+inline typename JstBuffer_<TJst, TSpec>::TSourceIterator |
|
445 |
+sourceEnd(JstBuffer_<TJst, TSpec> & buffer) |
|
446 |
+{ |
|
447 |
+ return buffer._sourceEnd; |
|
448 |
+} |
|
449 |
+ |
|
450 |
+// ---------------------------------------------------------------------------- |
|
451 |
+// Function setSourceEnd() |
|
452 |
+// ---------------------------------------------------------------------------- |
|
453 |
+ |
|
454 |
+template <typename TJst, typename TSpec, typename TSourceIter> |
|
455 |
+inline void |
|
456 |
+setSourceEnd(JstBuffer_<TJst, TSpec> & buffer, |
|
457 |
+ TSourceIter const & end) |
|
458 |
+{ |
|
459 |
+ buffer._isSynchronized = false; |
|
460 |
+ buffer._sourceEnd = end; |
|
461 |
+} |
|
462 |
+ |
|
463 |
+// ---------------------------------------------------------------------------- |
|
464 |
+// Function setDeltaMap() |
|
465 |
+// ---------------------------------------------------------------------------- |
|
466 |
+ |
|
467 |
+template <typename TJst, typename TSpec> |
|
468 |
+inline void |
|
469 |
+setDeltaMap(JstBuffer_<TJst, TSpec> & buffer, |
|
470 |
+ typename Member<JstBuffer_<TJst, TSpec>, JstBufferDeltaMapMember>::Type & map) |
|
471 |
+{ |
|
472 |
+ if (buffer._deltaMapPtr != &map) |
|
473 |
+ { |
|
474 |
+ markModified(buffer); |
|
475 |
+ buffer._deltaMapPtr = ↦ |
|
476 |
+ } |
|
477 |
+} |
|
478 |
+ |
|
479 |
+// ---------------------------------------------------------------------------- |
|
480 |
+// Function sync() |
|
481 |
+// ---------------------------------------------------------------------------- |
|
482 |
+ |
|
483 |
+template <typename TJst, typename TSpec> |
|
484 |
+inline bool |
|
485 |
+sync(JstBuffer_<TJst, TSpec> & buffer) |
|
486 |
+{ |
|
487 |
+ if (isSynchronized(buffer)) |
|
488 |
+ return true; |
|
489 |
+ return impl::synchronize(buffer); |
|
490 |
+} |
|
491 |
+ |
|
492 |
+// ---------------------------------------------------------------------------- |
|
493 |
+// Function create() |
|
494 |
+// ---------------------------------------------------------------------------- |
|
495 |
+ |
|
496 |
+template <typename TJst, typename TSpec> |
|
497 |
+inline bool |
|
498 |
+create(JstBuffer_<TJst, TSpec> & buffer) |
|
499 |
+{ |
|
500 |
+ if (!isSynchronized(buffer)) |
|
501 |
+ if (!sync(buffer)) |
|
502 |
+ return false; |
|
503 |
+ impl::create(buffer); |
|
504 |
+ return true; |
|
505 |
+} |
|
506 |
+ |
|
507 |
+// ---------------------------------------------------------------------------- |
|
508 |
+// Function isSynchronized() |
|
509 |
+// ---------------------------------------------------------------------------- |
|
510 |
+ |
|
511 |
+template <typename TJst, typename TSpec> |
|
512 |
+inline bool |
|
513 |
+isSynchronized(JstBuffer_<TJst, TSpec> const & buffer) |
|
514 |
+{ |
|
515 |
+ return buffer._isSynchronized; |
|
516 |
+} |
|
517 |
+ |
|
518 |
+// ---------------------------------------------------------------------------- |
|
519 |
+// Function clear() |
|
520 |
+// ---------------------------------------------------------------------------- |
|
521 |
+ |
|
522 |
+template <typename TJst, typename TSpec> |
|
523 |
+inline void |
|
524 |
+clear(JstBuffer_<TJst, TSpec> & buffer) |
|
525 |
+{ |
|
526 |
+ buffer._isSynchronized = false; |
|
527 |
+ buffer._deltaMapPtr = nullptr; |
|
528 |
+ clear(buffer._startPositions); |
|
529 |
+} |
|
530 |
+ |
|
531 |
+// ---------------------------------------------------------------------------- |
|
532 |
+// Function markModified() |
|
533 |
+// ---------------------------------------------------------------------------- |
|
534 |
+ |
|
535 |
+template <typename TJst, typename TSpec> |
|
536 |
+inline void |
|
537 |
+markModified(JstBuffer_<TJst, TSpec> & buffer) |
|
538 |
+{ |
|
539 |
+ buffer._isSynchronized = false; |
|
540 |
+} |
|
541 |
+ |
|
542 |
+// ---------------------------------------------------------------------------- |
|
543 |
+// Function init(); |
|
544 |
+// ---------------------------------------------------------------------------- |
|
545 |
+ |
|
546 |
+// TODO(rrahn): Fix constness issue. |
|
547 |
+template <typename TJst, typename TSpec> |
|
548 |
+inline void |
|
549 |
+init(JstBuffer_<TJst, TSpec> & me, |
|
550 |
+ TJst & jst) |
|
551 |
+{ |
|
552 |
+ setDeltaMap(me, impl::member(jst, JstDeltaMapMember())); |
|
553 |
+ setSourceBegin(me, begin(impl::member(jst, JstSourceMember()), Standard())); |
|
554 |
+ setSourceEnd(me, end(impl::member(jst, JstSourceMember()), Standard())); |
|
555 |
+ sync(me); |
|
556 |
+ create(me); |
|
557 |
+} |
|
558 |
+ |
|
559 |
+} // namespace seqan |
|
560 |
+ |
|
561 |
+#endif // EXTRAS_INCLUDE_SEQAN_JOURNALED_STRING_TREE_JOURNALED_SEQUENCE_BUFFER_H_ |