File: match.hpp

package info (click to toggle)
lambda-align 1.0.3-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 788 kB
  • sloc: cpp: 4,653; sh: 70; makefile: 27
file content (290 lines) | stat: -rw-r--r-- 9,759 bytes parent folder | download | duplicates (4)
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
// ==========================================================================
//                                  lambda
// ==========================================================================
// Copyright (c) 2013-2017, Hannes Hauswedell <h2 @ fsfe.org>
// Copyright (c) 2016-2017, Knut Reinert and Freie Universität Berlin
// All rights reserved.
//
// This file is part of Lambda.
//
// Lambda is Free Software: you can redistribute it and/or modify it
// under the terms found in the LICENSE[.md|.rst] file distributed
// together with this file.
//
// Lambda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// ==========================================================================
// match.h: Main File for the match class
// ==========================================================================

#ifndef SEQAN_LAMBDA_FINDER_H_
#define SEQAN_LAMBDA_FINDER_H_

// #include <forward_list>
// #include <unordered_map>
#include <vector>

#include "options.hpp"

using namespace seqan;

//-----------------------------------------------------------------------------
//                  Finder
//-----------------------------------------------------------------------------

template<typename TAlph>
struct Match
{
    typedef SizeTypeNum_<TAlph>    TQId;
    typedef SizeTypeNum_<TAlph>    TSId;
    typedef SizeTypePos_<TAlph>    TPos;

    TQId qryId;
    TSId subjId;
    TPos qryStart;
//     TPos qryEnd;

    TPos subjStart;
//     TPos subjEnd;

//     Match()
//     :
//         qryId(0), qryStart(0), /*qryEnd(0),*/ subjId(0), subjStart(0)/*, subjEnd(0)*/
//     {
//     }
// 
//     Match(Match const & m2)
//     {
//         qryId       = m2.qryId;
//         qryStart    = m2.qryStart;
//         /*qryEnd      = m2.qryEnd;*/
//         subjId      = m2.subjId;
//         subjStart   = m2.subjStart;
//         /*subjEnd     = m2.subjEnd;*/
//     }

    inline bool operator== (Match const & m2) const
    {
         return std::tie(qryId, subjId, qryStart, subjStart/*, qryEnd, subjEnd*/)
             == std::tie(m2.qryId, m2.subjId, m2.qryStart, m2.subjStart/*, m2.qryEnd, m2.subjEnd*/);
    }
    inline bool operator< (Match const & m2) const
    {
         return std::tie(qryId, subjId, qryStart, subjStart/*, qryEnd, subjEnd*/)
              < std::tie(m2.qryId, m2.subjId, m2.qryStart, m2.subjStart/*, m2.qryEnd, m2.subjEnd*/);
    }
};

template <typename TAlph>
inline void
setToSkip(Match<TAlph> & m)
{
    using TPos          = typename Match<TAlph>::TPos;
    constexpr TPos posMax = std::numeric_limits<TPos>::max();
    m.qryStart = posMax;
    m.subjStart = posMax;
}

template <typename TAlph>
inline bool
isSetToSkip(Match<TAlph> const & m)
{
    using TPos          = typename Match<TAlph>::TPos;
    constexpr TPos posMax = std::numeric_limits<TPos>::max();
    return (m.qryStart == posMax) && (m.subjStart == posMax);
}

// struct IdPairHash
// {
//     std::size_t
//     operator()(std::pair<Match::TQId, Match::TSId> const & pair) const
//     {
//         return std::hash<Match::TQId>()(std::get<0>(pair)) ^
//                std::hash<Match::TSId>()(std::get<1>(pair));
//     }
// };
// 
// 
// // this is a comparison structure that takes the relative abundance of hits
// // on a specific subject into account thereby moving those subjects to
// // front of a queries hits that are more likely to be top-scoring
// struct MatchSortComp :
//     public ::std::binary_function <Match, Match, bool>
// {
//     std::unordered_map<std::pair<Match::TQId, Match::TSId>,
//                        uint64_t,
//                        IdPairHash> map;
// 
//     MatchSortComp(std::vector<Match> const & matches)
//     {
//         for (Match const & m : matches)
//             ++map[std::make_pair(m.qryId, m.subjId)];
//     }
// 
//     inline bool operator() (Match const & i, Match const & j) const
//     {
//         int64_t iAb = -map.at(std::make_pair(i.qryId, i.subjId));
//         int64_t jAb = -map.at(std::make_pair(j.qryId, j.subjId));
//         return std::tie(i.qryId,
//                         iAb,
//                         i.qryStart,
//                         i.subjStart)
//            <   std::tie(j.qryId,
//                         jAb,
//                         j.qryStart,
//                         j.subjStart);
//     }
// };

template <typename TGH, typename TAlph>
inline void
myHyperSortSingleIndex(std::vector<Match<TAlph>> & matches,
                       bool const doubleIndexing,
                       TGH const &)
{
    using TId = typename Match<TAlph>::TQId;

    // regular sort
    std::sort(matches.begin(), matches.end());

    //                    trueQryId, begin,    end
    std::vector<std::tuple<TId, TId, TId>> intervals;
    for (TId i = 1; i <= length(matches); ++i)
    {
        if ((i == length(matches))                      ||
            (matches[i-1].qryId != matches[i].qryId)    ||
            (matches[i-1].subjId / sNumFrames(TGH::blastProgram)) !=
             (matches[i].subjId / sNumFrames(TGH::blastProgram)))
        {
            if (length(intervals) == 0) // first interval
                intervals.emplace_back(std::make_tuple(matches[i-1].qryId
                                                       / qNumFrames(TGH::blastProgram),
                                                       0,
                                                       i));
            else
                intervals.emplace_back(std::make_tuple(matches[i-1].qryId
                                                       / qNumFrames(TGH::blastProgram),
                                                       std::get<2>(intervals.back()),
                                                       i));
        }
    }

    if (doubleIndexing)
    {
        // sort by trueQryId, then lengths of interval
        std::sort(intervals.begin(), intervals.end(),
                [] (std::tuple<TId, TId, TId> const & i1,
                    std::tuple<TId, TId, TId> const & i2)
        {
            return (std::get<0>(i1) != std::get<0>(i2))
                    ? (std::get<0>(i1) < std::get<0>(i2))
                    : ((std::get<2>(i1) - std::get<1>(i1))
                     > (std::get<2>(i2) - std::get<1>(i2)));
        });
    } else
    {
        // sort by lengths of interval, since trueQryId is the same anyway
        std::sort(intervals.begin(), intervals.end(),
                [] (std::tuple<TId, TId, TId> const & i1,
                    std::tuple<TId, TId, TId> const & i2)
        {
            return (std::get<2>(i1) - std::get<1>(i1))
                >  (std::get<2>(i2) - std::get<1>(i2));
        });
    }

    std::vector<Match<TAlph>> tmpVector;
    tmpVector.resize(matches.size());

    TId newIndex = 0;
    for (auto const & i : intervals)
    {
        TId limit = std::get<2>(i);
        for (TId j = std::get<1>(i); j < limit; ++j)
        {
            tmpVector[newIndex] = matches[j];
            newIndex++;
        }
    }
    std::swap(tmpVector, matches);
}


// inline bool
// overlap(Match const & m1, Match const & m2, unsigned char d = 0)
// {
//     SEQAN_ASSERT_EQ(m1.qryId,  m2.qryId);
//     SEQAN_ASSERT_EQ(m1.subjId, m2.subjId);
// 
// //     if (     //match beginnings overlap
// //         ((m1.qryStart  >= _protectUnderflow(m2.qryStart, d)) &&
// //          (m1.qryStart  <= _protectOverflow( m2.qryEnd,   d)) &&
// //          (m1.subjStart >= _protectUnderflow(m2.subjStart,d)) &&
// //          (m1.subjStart <= _protectOverflow( m2.subjEnd,  d))
// //         ) || // OR match endings overlap
// //         ((m1.qryEnd  >= _protectUnderflow(m2.qryStart, d)) &&
// //          (m1.qryEnd  <= _protectOverflow( m2.qryEnd,   d)) &&
// //          (m1.subjEnd >= _protectUnderflow(m2.subjStart,d)) &&
// //          (m1.subjEnd <= _protectOverflow( m2.subjEnd,  d))
// //         )
// //        )
// //         return true;
// 
//     //DEBUG DEBUG DEBUG TODO TODO TODO WHEN ALIGNMENT IS FIXED
//     // also add check if diffference of overlaps is  <= maximum number of gaps
//     if ((intervalOverlap(m1.qryStart, m1.qryEnd, m2.qryStart, m2.qryEnd) > -d) &&
//         (intervalOverlap(m1.subjStart, m1.subjEnd, m2.subjStart, m2.subjEnd) > -d) &&
//         ((m1.qryStart < m2.qryStart ) == (m1.subjStart < m2.subjStart))) // same order
//         return true;
// 
//     return false;
// }

// inline bool contains(Match const & m1, Match const & m2)
// {
// //     if (m1.qryId != m2.qryId)
// //         return false;
//     SEQAN_ASSERT_EQ(m1.qryId,  m2.qryId);
//     if (m1.qryStart != m2.qryStart)
//         return false;
// 
//     if (m1.qryStart > m2.qryStart)
//         return false;
//     if (m1.subjStart > m2.subjStart)
//         return false;
//     if (m1.qryEnd < m2.qryEnd)
//         return false;
//     if (m1.subjEnd < m2.subjEnd)
//         return false;
//     return true;
// }
// 
// inline void
// mergeUnto(Match & m1, Match const & m2)
// {
//     SEQAN_ASSERT_EQ(m1.qryId,  m2.qryId);
//     SEQAN_ASSERT_EQ(m1.subjId, m2.subjId);
//     m1.qryStart  = std::min(m1.qryStart, m2.qryStart);
//     m1.qryEnd    = std::max(m1.qryEnd,   m2.qryEnd);
//     m1.subjStart = std::min(m1.subjStart,m2.subjStart);
//     m1.subjEnd   = std::max(m1.subjEnd,  m2.subjEnd);
// }


// inline void
// _printMatch(Match const & m)
// {
//     std::cout << "MATCH  Query " << m.qryId
//               << "(" << m.qryStart << ", " << m.qryEnd
//               << ")   on Subject "<< m.subjId
//               << "(" << m.subjStart << ", " << m.subjEnd
//               << ")" <<  std::endl << std::flush;
// }





#endif // header guard