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 // Adapts traceback to a journaled string using SortedArray specialization.
35 // ==========================================================================
36 
37 #ifndef INCLUDE_SEQAN_JOURNALED_SET_JOURNAL_ALIGNMENT_TRACEBACK_ADAPTOR_H_
38 #define INCLUDE_SEQAN_JOURNALED_SET_JOURNAL_ALIGNMENT_TRACEBACK_ADAPTOR_H_
39 
40 namespace seqan {
41 
42 // ============================================================================
43 // Forwards
44 // ============================================================================
45 
46 // ============================================================================
47 // Tags, Classes, Enums
48 // ============================================================================
49 
50 // ============================================================================
51 // Metafunctions
52 // ============================================================================
53 
54 // ============================================================================
55 // Functions
56 // ============================================================================
57 
58 // ----------------------------------------------------------------------------
59 // Function _adaptTraceSegmentsTo()           [String<Journaled<SortedArray> >]
60 // ----------------------------------------------------------------------------
61 
62 
63 template <typename TValue, typename THostSpec, typename TBuffSpec, typename TReference,
64           typename TSource, typename TPosition, typename TSize, typename TStringSpec>
65 inline void
_adaptTraceSegmentsTo(String<TValue,Journaled<THostSpec,SortedArray,TBuffSpec>> & targetJournal,TReference const &,TSource const & source,String<TraceSegment_<TPosition,TSize>,TStringSpec> const & traceSegments)66 _adaptTraceSegmentsTo(String<TValue, Journaled<THostSpec, SortedArray, TBuffSpec> > & targetJournal,
67                       TReference const & /*reference*/, //TODO(rmaerker): What if the TSequenceH is a journal string.
68                       TSource const & source,
69                       String<TraceSegment_<TPosition, TSize>, TStringSpec> const & traceSegments)
70 {
71 
72     typedef String<TValue, Journaled<THostSpec, SortedArray, TBuffSpec> > TJournalString;
73     typedef typename JournalType<TJournalString>::Type TJournalEntries;
74     typedef typename Value<TJournalEntries>::Type TJournalEntry;
75 
76 //    typedef typename Position<TReference>::Type TPhysicalPosition;
77     typedef typename Position<TSource>::Type TVirtualPosition;
78 
79     typedef TraceSegment_<TPosition, TSize> TTraceSegment;
80     typedef typename Iterator<String<TTraceSegment, TStringSpec> const>::Type TTraceIterator;
81 
82     SEQAN_ASSERT_NOT_MSG(empty(host(targetJournal)), "No reference sequence set!");
83 
84     // TODO(rmaerker): Hot-fix for new alignment module.
85     clear(targetJournal._journalEntries._journalNodes);
86     clear(targetJournal._insertionBuffer);
87     targetJournal._journalEntries._originalStringLength = length(host(targetJournal));
88 
89     TVirtualPosition virtualPos = 0;
90 
91     TTraceIterator traceSegIter = end(traceSegments, Standard()) - 1;
92     TTraceIterator traceSegIterEnd = begin(traceSegments, Standard()) - 1;
93 
94     for (; traceSegIter != traceSegIterEnd; --traceSegIter)
95     {
96         switch (value(traceSegIter)._traceValue)
97         {
98         case TraceBitMap_<>::DIAGONAL: //matching area
99         {
100             appendValue(targetJournal._journalEntries._journalNodes, TJournalEntry(SOURCE_ORIGINAL, value(traceSegIter)._horizontalBeginPos,
101                                                                                    virtualPos, value(traceSegIter)._horizontalBeginPos, value(traceSegIter)._length));
102             virtualPos += value(traceSegIter)._length;
103             break;
104         }
105 
106         case TraceBitMap_<>::VERTICAL: //insertion
107         {
108             appendValue(targetJournal._journalEntries._journalNodes, TJournalEntry(SOURCE_PATCH, length(targetJournal._insertionBuffer),
109                                                                                    virtualPos, 0, value(traceSegIter)._length));
110             append(targetJournal._insertionBuffer,
111                    infix(source, value(traceSegIter)._verticalBeginPos, value(traceSegIter)._verticalBeginPos +
112                          value(traceSegIter)._length));
113             virtualPos += value(traceSegIter)._length;
114             break;
115         }
116 
117         default:
118             break;  // Otherwise we are in a deletion and do nothing.
119         }
120     }
121     _setLength(targetJournal, virtualPos);
122 }
123 
124 }  // namespace seqan
125 
126 #endif  // #ifndef INCLUDE_SEQAN_JOURNALED_SET_JOURNAL_ALIGNMENT_TRACEBACK_ADAPTOR_H_
127