Switch to unified view

a b/deepvariant/realigner/python/ssw_pybind.cc
1
/*
2
 * Copyright 2024 Google LLC.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions
6
 * are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * 3. Neither the name of the copyright holder nor the names of its
16
 *    contributors may be used to endorse or promote products derived from this
17
 *    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 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
31
32
#include "absl/strings/string_view.h"
33
#if true  // Trick to stop tooling from moving the #include around.
34
// MUST appear before any standard headers are included.
35
#include <pybind11/pybind11.h>
36
#endif
37
38
#include <pybind11/stl.h>
39
40
#include <memory>
41
#include <utility>
42
43
#include "deepvariant/realigner/ssw.h"
44
#include "third_party/nucleus/core/python/type_caster_nucleus_status.h"
45
#include "third_party/nucleus/core/python/type_caster_nucleus_statusor.h"
46
#include "third_party/nucleus/util/python/type_caster_nucleus_proto_ptr.h"
47
#include "pybind11_protobuf/native_proto_caster.h"
48
49
namespace py = pybind11;
50
51
PYBIND11_MODULE(ssw, m) {
52
  pybind11_protobuf::ImportNativeProtoCasters();
53
  using namespace ::learning::genomics::deepvariant;  // NOLINT
54
55
  py::classh<Aligner>(m, "Aligner")
56
      .def(py::init<>())
57
      .def(py::init<uint8_t, uint8_t, uint8_t, uint8_t>())
58
      .def_static(
59
          "construct",
60
          [](uint8_t match_score, uint8_t mismatch_penalty,
61
             uint8_t gap_opening_penalty, uint8_t gap_extending_penalty) {
62
            return std::make_unique<Aligner>(std::move(match_score),
63
                                             std::move(mismatch_penalty),
64
                                             std::move(gap_opening_penalty),
65
                                             std::move(gap_extending_penalty));
66
          },
67
          py::arg("match_score"), py::arg("mismatch_penalty"),
68
          py::arg("gap_opening_penalty"), py::arg("gap_extending_penalty"))
69
      .def("set_reference_sequence", &Aligner::SetReferenceSequence,
70
           py::arg("seq"))
71
      .def("align", [](Aligner* self, const string& query, const Filter& filter,
72
                       int maskLen) {
73
        Alignment alignment;
74
        int cpp_result = self->Align(query, filter, maskLen, &alignment);
75
        py::object ret1 = py::cast(std::move(alignment));
76
        auto postproc =
77
            py::module_::import("third_party.nucleus.io.clif_postproc");
78
        py::object ret0 =
79
            postproc.attr("ValueErrorOnInaccurate")(cpp_result, ret1);
80
        return ret0;
81
      });
82
83
  py::classh<Filter>(m, "Filter")
84
      .def(py::init())
85
      .def(py::init<const bool&, const bool&, uint16_t, uint16_t>())
86
      .def_static(
87
          "construct",
88
          [](bool pos, bool cigar, uint16_t score, uint16_t dis) {
89
            return std::make_unique<Filter>(std::move(pos), std::move(cigar),
90
                                            std::move(score), std::move(dis));
91
          },
92
          py::arg("pos"), py::arg("cigar"), py::arg("int"), py::arg("dis"))
93
      .def_readwrite("report_begin_position", &Filter::report_begin_position)
94
      .def_readwrite("report_cigar", &Filter::report_cigar)
95
      .def_readwrite("score_filter", &Filter::score_filter)
96
      .def_readwrite("distance_filter", &Filter::distance_filter);
97
98
  py::classh<Alignment>(m, "Alignment")
99
      .def_readwrite("sw_score", &Alignment::sw_score)
100
      .def_readwrite("sw_score_next_best", &Alignment::sw_score_next_best)
101
      .def_readwrite("ref_begin", &Alignment::ref_begin)
102
      .def_readwrite("ref_end", &Alignment::ref_end)
103
      .def_readwrite("query_begin", &Alignment::query_begin)
104
      .def_readwrite("query_end", &Alignment::query_end)
105
      .def_readwrite("ref_end_next_best", &Alignment::ref_end_next_best)
106
      .def_readwrite("mismatches", &Alignment::mismatches)
107
      .def_property(
108
          "cigar_string",
109
          [](const Alignment* self) { return py::bytes(self->cigar_string); },
110
          [](Alignment* self, absl::string_view value) {
111
            self->cigar_string = value;
112
          });
113
}