File: sam_parser.hpp

package info (click to toggle)
libbioparser-dev 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,932 kB
  • sloc: cpp: 1,275; makefile: 13
file content (193 lines) | stat: -rw-r--r-- 5,643 bytes parent folder | download | duplicates (3)
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
// Copyright (c) 2020 Robert Vaser

#ifndef BIOPARSER_SAM_PARSER_HPP_
#define BIOPARSER_SAM_PARSER_HPP_

#include <cstdint>
#include <cstdlib>
#include <memory>
#include <vector>
#include <stdexcept>

#include "bioparser/parser.hpp"

namespace bioparser {

template<class T>
class SamParser: public Parser<T> {
 public:
  SamParser(const SamParser&) = delete;
  SamParser& operator=(const SamParser&) = delete;

  SamParser(SamParser&&) = delete;
  SamParser& operator=(SamParser&&) = delete;

  ~SamParser() {}

  std::vector<std::unique_ptr<T>> Parse(
      std::uint64_t bytes, bool shorten_names = true) override {
    std::vector<std::unique_ptr<T>> dst;
    std::uint64_t parsed_bytes = 0;

    const char* q_name = nullptr;
    std::uint32_t q_name_len = 0;
    std::uint32_t flag = 0;
    const char* t_name = nullptr;
    std::uint32_t t_name_len = 0;
    std::uint32_t t_begin = 0;
    std::uint32_t map_quality = 0;
    const char* cigar = nullptr;
    std::uint32_t cigar_len = 0;
    const char* t_next_name = nullptr;
    std::uint32_t t_next_name_len = 0;
    std::uint32_t t_next_begin = 0;
    std::uint32_t template_len = 0;
    const char* data = nullptr;
    std::uint32_t data_len = 0;
    const char* quality = nullptr;
    std::uint32_t quality_len = 0;

    auto create_T = [&] () -> void {
      if (this->storage()[0] == '@') {  // file header
        this->Clear();
        return;
      }
      auto storage_ptr = this->RightStrip(
          this->storage().data(),
          this->storage_ptr());
      this->Terminate(storage_ptr);

      std::uint32_t num_values = 0;
      std::uint32_t begin_ptr = 0;
      while (true) {
        auto end_ptr = begin_ptr;
        while (end_ptr < storage_ptr && this->storage()[end_ptr] != '\t') {
          ++end_ptr;
        }
        this->Terminate(end_ptr);

        switch (num_values) {
          case 0:
            q_name = this->storage().data() + begin_ptr;
            q_name_len = end_ptr - begin_ptr;
            break;
          case 1: flag = std::atoi(this->storage().data() + begin_ptr); break;
          case 2:
            t_name = this->storage().data() + begin_ptr;
            t_name_len = end_ptr - begin_ptr;
            break;
          case 3: t_begin = std::atoi(this->storage().data() + begin_ptr); break;  // NOLINT
          case 4: map_quality = std::atoi(this->storage().data() + begin_ptr); break;  // NOLINT
          case 5:
            cigar = this->storage().data() + begin_ptr;
            cigar_len = end_ptr - begin_ptr;
            break;
          case 6:
            t_next_name = this->storage().data() + begin_ptr;
            t_next_name_len = end_ptr - begin_ptr;
            break;
          case 7: t_next_begin = std::atoi(this->storage().data() + begin_ptr); break;  // NOLINT
          case 8: template_len = std::atoi(this->storage().data() + begin_ptr); break;  // NOLINT
          case 9:
            data = this->storage().data() + begin_ptr;
            data_len = end_ptr - begin_ptr;
            break;
          case 10:
            quality = this->storage().data() + begin_ptr;
            quality_len = end_ptr - begin_ptr;
            break;
          default: break;
        }

        ++num_values;
        if (end_ptr == storage_ptr || num_values == 11) {
          break;
        }
        begin_ptr = end_ptr + 1;
      }

      if (num_values != 11) {
        throw std::invalid_argument(
            "[bioparser::SamParser] error: invalid file format");
      }

      q_name_len = shorten_names ?
          this->Shorten(q_name, q_name_len) :
          this->RightStrip(q_name, q_name_len);

      t_name_len = shorten_names ?
          this->Shorten(t_name, t_name_len) :
          this->RightStrip(t_name, t_name_len);

      cigar_len = this->RightStrip(cigar, cigar_len);

      t_next_name_len = shorten_names ?
          this->Shorten(t_next_name, t_next_name_len) :
          this->RightStrip(t_next_name, t_next_name_len);

      data_len = this->RightStrip(data, data_len);
      quality_len = this->RightStrip(quality, quality_len);

      if (q_name_len == 0 || t_name_len == 0 || cigar_len == 0 ||
          t_next_name_len == 0 || data_len == 0 || quality_len == 0 ||
          (data_len > 1 && quality_len > 1 && data_len != quality_len)) {
        throw std::invalid_argument(
            "[bioparser::SamParser] error: invalid file format");
      }

      dst.emplace_back(std::unique_ptr<T>(new T(
          q_name, q_name_len,
          flag,
          t_name, t_name_len, t_begin,
          map_quality,
          cigar, cigar_len,
          t_next_name, t_next_name_len, t_next_begin,
          template_len,
          data, data_len,
          quality, quality_len)));

      parsed_bytes += this->storage_ptr();
      this->Clear();
    };

    bool is_eof = false;

    while (true) {
      auto buffer_ptr = this->buffer_ptr();
      for (; buffer_ptr < this->buffer_bytes(); ++buffer_ptr) {
        auto c = this->buffer()[buffer_ptr];
        if (c == '\n') {
          this->Store(buffer_ptr - this->buffer_ptr());
          create_T();
          if (parsed_bytes >= bytes) {
            return dst;
          }
        }
      }
      if (this->buffer_ptr() < buffer_ptr) {
        this->Store(buffer_ptr - this->buffer_ptr());
      }

      if (is_eof) {
        break;
      }
      is_eof = this->Read();
    }

    if (this->storage_ptr() != 0) {
      create_T();
    }

    return dst;
  }

 private:
  explicit SamParser(gzFile file)
      : Parser<T>(file, 65536) {}  // 64 kB

  friend Parser<T>;
};

}  // namespace bioparser

#endif  // BIOPARSER_SAM_PARSER_HPP_