[9b26b7]: / deepvariant / python / clif_converters.cc

Download this file

104 lines (92 with data), 3.9 kB

  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
/*
* Copyright 2017 Google LLC.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "deepvariant/python/clif_converters.h"
#include <memory>
#include <mutex>
// This has to go before numpy
#include <Python.h>
#include "deepvariant/pileup_image_native.h"
#include "numpy/arrayobject.h"
#include "numpy/ndarrayobject.h"
#include "absl/log/check.h"
#include "clif/python/postconv.h"
namespace learning {
namespace genomics {
namespace deepvariant {
using std::string;
// We need to initialize the numpy C ARRAY API via a call to import_array():
// https://docs.scipy.org/doc/numpy-1.13.0/reference/c-api.array.html#importing-the-api
// Usually this is put in the module initialization function, but we cannot
// easily a call there as that function is generated by CLIF. Instead we use
// std::call_once to initialize this C++ upon the first call to our converter.
std::once_flag import_array_flag;
// https://github.com/kornerc/brisk/issues/3#issuecomment-237107324
int call_import_array() {
import_array();
// https://github.com/ros-perception/vision_opencv/pull/292#issue-322425968
return NULL;
}
PyObject* Clif_PyObjFrom(std::unique_ptr<ImageRow> img_row,
const clif::py::PostConv& pc) {
// Initialize numpy C array API if needed.
std::call_once(import_array_flag, call_import_array);
if (!img_row) { Py_RETURN_NONE; }
npy_intp dims[] { 1, img_row->Width(), img_row->num_channels };
PyArrayObject* res = reinterpret_cast<PyArrayObject*>(
PyArray_SimpleNew(3, dims, PyArray_UBYTE));
CHECK(res != nullptr);
unsigned char* data = reinterpret_cast<unsigned char*> PyArray_DATA(res);
unsigned char* cur = data;
for (int i = 0; i < img_row->Width(); i++) {
*cur++ = img_row->base[i];
*cur++ = img_row->base_quality[i];
*cur++ = img_row->mapping_quality[i];
*cur++ = img_row->on_positive_strand[i];
*cur++ = img_row->supports_alt[i];
*cur++ = img_row->matches_ref[i];
if (img_row->use_allele_frequency) {
*cur++ = img_row->allele_frequency[i];
}
if (img_row->add_hp_channel) {
*cur++ = img_row->hp_value[i];
}
if (!img_row->channels.empty()) {
// Iterate over channels here and fill data...
for (int j = 0; j < img_row->channels.size(); j++) {
*cur++ = img_row->channel_data[j][i];
}
}
}
return PyArray_Return(res);
}
} // namespace deepvariant
} // namespace genomics
} // namespace learning