File: KSW2Aligner.hpp

package info (click to toggle)
rapmap 0.15.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,228 kB
  • sloc: cpp: 48,810; ansic: 4,686; sh: 215; python: 82; makefile: 15
file content (134 lines) | stat: -rw-r--r-- 5,054 bytes parent folder | download | duplicates (5)
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
#ifndef __KSW2_ALIGNER_HPP__
#define __KSW2_ALIGNER_HPP__

#include <memory>
#include <vector>
#include <cstdlib>

extern "C" {
#include "ksw2pp/kalloc.h"
#include "ksw2pp/ksw2.h"
}

namespace ksw2pp {

/**
 * When we use a unique_ptr to hold a kalloc allocator, this is the
 * deleter we use to call the appropriate function to free / destroy the
 *allocator.
 **/
class KallocDeleter {
public:
  void operator()(void* p) { km_destroy(p); }
};

enum class KSW2AlignmentType : uint8_t { GLOBAL = 1, EXTENSION = 2 };

// Just like Int2Type from
// https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Int-To-Type
template <KSW2AlignmentType I> struct EnumToType {
  enum { value = static_cast<uint8_t>(I) };
};

// A structure to hold the relvant parameters for the aligner
struct KSW2Config {
  int8_t gapo = -1;
  int8_t gape = -1;
  int bandwidth = -1;
  int dropoff = -1;
  int flag = 0;
  int alphabetSize = 5;
  int end_bonus = 10;
  KSW2AlignmentType atype = KSW2AlignmentType::EXTENSION;
};

class KSW2Aligner {

public:
  KSW2Aligner(int8_t match = 2, int8_t mismatch = -4);
  KSW2Aligner(std::vector<int8_t> mat);

  int transformSequenceKSW2(const char* const queryOriginal, const int queryLength,
                            std::vector<unsigned char>& queryTransformed);

  int transformSequencesKSW2(const char* const queryOriginal, const int queryLength,
                             const char* const targetOriginal, const int targetLength,
                             std::vector<unsigned char>& queryTransformed,
                             std::vector<unsigned char>& targetTransformed);
  /**
   * Variants of the operator that require both an explicit type tag to
   * determine the type of alignment to perform, as well as a pointer to
   * an `ksw_extz_t` structure that will be used to hold the output.
   */
  int operator()(const char* const queryOriginal, const int queryLength,
                 const char* const targetOriginal, const int targetLength,
                 ksw_extz_t* ez, EnumToType<KSW2AlignmentType::GLOBAL>);

  int operator()(const char* const queryOriginal, const int queryLength,
                 const char* const targetOriginal, const int targetLength,
                 ksw_extz_t* ez, EnumToType<KSW2AlignmentType::EXTENSION>);

  int operator()(const uint8_t* const queryOriginal, const int queryLength,
                 const uint8_t* const targetOriginal, const int targetLength,
                 ksw_extz_t* ez, EnumToType<KSW2AlignmentType::GLOBAL>);

  int operator()(const uint8_t* const queryOriginal, const int queryLength,
                 const uint8_t* const targetOriginal, const int targetLength,
                 ksw_extz_t* ez, EnumToType<KSW2AlignmentType::EXTENSION>);

  /**
   * Variants of the operator that do not require an output
   * `ksw_extz_t*` variable.  They will store the result in this object's
   * `result_` variable, which can then be queried with the `result()` method.
   */
  int operator()(const char* const queryOriginal, const int queryLength,
                 const char* const targetOriginal, const int targetLength,
                 EnumToType<KSW2AlignmentType::GLOBAL>);

  int operator()(const char* const queryOriginal, const int queryLength,
                 const char* const targetOriginal, const int targetLength,
                 EnumToType<KSW2AlignmentType::EXTENSION>);

  int operator()(const uint8_t* const queryOriginal, const int queryLength,
                 const uint8_t* const targetOriginal, const int targetLength,
                 EnumToType<KSW2AlignmentType::GLOBAL>);

  int operator()(const uint8_t* const queryOriginal, const int queryLength,
                 const uint8_t* const targetOriginal, const int targetLength,
                 EnumToType<KSW2AlignmentType::EXTENSION>);

  /**
   * Variants of the operator that require neither an output
   * `ksw_extz_t*` variable or an explicit type tag to select the type of
   *alignment
   * to perform.  These variants will perform whichever type of alignment is
   * currently specified in the `atype` member of this class's `config_`
   * object.
   **/
  int operator()(const char* const queryOriginal, const int queryLength,
                 const char* const targetOriginal, const int targetLength);

  int operator()(const uint8_t* const queryOriginal, const int queryLength,
                 const uint8_t* const targetOriginal, const int targetLength);

  KSW2Config& config() { return config_; }
  const ksw_extz_t& result() { return result_; }
  void freeCIGAR(ksw_extz_t* ez) {
    if (ez->cigar and kalloc_allocator_) {
      kfree(kalloc_allocator_.get(), ez->cigar);
    }
  }

private:
  std::vector<uint8_t> query_;
  std::vector<uint8_t> target_;
  ksw_extz_t result_;
  std::unique_ptr<void, KallocDeleter> kalloc_allocator_{nullptr,
                                                         KallocDeleter()};
  std::vector<int8_t> mat_;
  KSW2Config config_;
  bool haveSSE41{false};
  bool haveSSE2{false};
};
} // namespace ksw2pp
#endif //__KSW2_ALIGNER_HPP__