pybcf 1.3.3__tar.gz → 1.3.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pybcf-1.3.3/src/pybcf.egg-info → pybcf-1.3.4}/PKG-INFO +1 -1
- {pybcf-1.3.3 → pybcf-1.3.4}/setup.py +3 -3
- {pybcf-1.3.3 → pybcf-1.3.4/src/pybcf.egg-info}/PKG-INFO +1 -1
- pybcf-1.3.4/src/sample_data.cpp +494 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/sample_data.h +3 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/test/test_pybcf.py +56 -4
- pybcf-1.3.3/src/sample_data.cpp +0 -247
- {pybcf-1.3.3 → pybcf-1.3.4}/LICENSE.txt +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/MANIFEST.in +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/README.md +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/pyproject.toml +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/setup.cfg +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/bcf.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/bcf.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/gzstream.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/gzstream.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/header.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/header.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/index.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/index.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/info.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/info.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf/__init__.py +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf/reader.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf/reader.pyx +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf.egg-info/SOURCES.txt +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf.egg-info/dependency_links.txt +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf.egg-info/requires.txt +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/pybcf.egg-info/top_level.txt +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/types.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/variant.cpp +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/variant.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/adler32.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/adler32_p.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/arch_functions.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/chunkset_tpl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/compare256_rle.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/compress.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/cpu_features.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/cpu_features.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/crc32.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/crc32.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/crc32_braid_comb.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/crc32_braid_comb_p.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/crc32_braid_p.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/crc32_braid_tbl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_fast.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_huff.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_medium.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_p.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_quick.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_rle.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_slow.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/deflate_stored.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/fallback_builtins.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/functable.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/functable.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/gzguts.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/gzlib.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/gzwrite.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/infback.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inffast_tpl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inffixed_tbl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inflate.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inflate.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inflate_p.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inftrees.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/inftrees.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/insert_string.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/insert_string_roll.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/insert_string_tpl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/match_tpl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/trees.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/trees.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/trees_emit.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/trees_tbl.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/uncompr.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/zbuild.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/zendian.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/zutil.c +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/zutil.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/src/zlib-ng/zutil_p.h +0 -0
- {pybcf-1.3.3 → pybcf-1.3.4}/test/test_indexed.py +0 -0
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
|
|
2
|
-
import glob
|
|
3
2
|
import io
|
|
4
3
|
from pathlib import Path
|
|
5
4
|
from setuptools import setup
|
|
@@ -9,7 +8,6 @@ import os
|
|
|
9
8
|
import platform
|
|
10
9
|
|
|
11
10
|
from distutils.core import Extension
|
|
12
|
-
from distutils.ccompiler import new_compiler
|
|
13
11
|
from Cython.Build import cythonize
|
|
14
12
|
|
|
15
13
|
EXTRA_COMPILE_ARGS = ['-std=c++11', '-I/usr/include']
|
|
@@ -19,6 +17,8 @@ if sys.platform == "darwin":
|
|
|
19
17
|
"-stdlib=libc++",
|
|
20
18
|
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1",
|
|
21
19
|
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
|
|
20
|
+
'-mavx',
|
|
21
|
+
'-mavx2',
|
|
22
22
|
]
|
|
23
23
|
EXTRA_LINK_ARGS += [
|
|
24
24
|
"-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib",
|
|
@@ -77,7 +77,7 @@ setup(name='pybcf',
|
|
|
77
77
|
description='Package for loading data from bcf files',
|
|
78
78
|
long_description=io.open('README.md', encoding='utf-8').read(),
|
|
79
79
|
long_description_content_type='text/markdown',
|
|
80
|
-
version='1.3.
|
|
80
|
+
version='1.3.4',
|
|
81
81
|
author='Jeremy McRae',
|
|
82
82
|
author_email='jmcrae@illumina.com',
|
|
83
83
|
license="MIT",
|
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
|
|
2
|
+
#include <cstring>
|
|
3
|
+
|
|
4
|
+
#include "sample_data.h"
|
|
5
|
+
#include "types.h"
|
|
6
|
+
|
|
7
|
+
#if defined(__x86_64__)
|
|
8
|
+
#include <immintrin.h>
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
#if defined(__aarch64__)
|
|
12
|
+
#include <arm_neon.h>
|
|
13
|
+
#endif
|
|
14
|
+
|
|
15
|
+
namespace bcf {
|
|
16
|
+
|
|
17
|
+
SampleData::SampleData(igzstream & infile, Header & _header, std::uint32_t len, std::uint32_t n_fmt, std::uint32_t _n_samples) {
|
|
18
|
+
n_samples = _n_samples;
|
|
19
|
+
header = &_header;
|
|
20
|
+
if (len == 0) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
phase.resize(n_samples);
|
|
24
|
+
missing.resize(n_samples);
|
|
25
|
+
|
|
26
|
+
// read the sample data into a buffer, but don't parse until required
|
|
27
|
+
buf.resize(len);
|
|
28
|
+
infile.read(reinterpret_cast<char *>(&buf[0]), len);
|
|
29
|
+
|
|
30
|
+
// read the available keys
|
|
31
|
+
std::uint32_t buf_idx=0;
|
|
32
|
+
std::uint32_t format_idx=0;
|
|
33
|
+
std::string key;
|
|
34
|
+
Typed type_val;
|
|
35
|
+
bool is_geno;
|
|
36
|
+
for (std::uint32_t i = 0; i < n_fmt; i++ ){
|
|
37
|
+
type_val = {&buf[0], buf_idx};
|
|
38
|
+
format_idx = parse_int(&buf[0], buf_idx, type_val.type_size);
|
|
39
|
+
key = header->format[format_idx].id;
|
|
40
|
+
is_geno = key == "GT";
|
|
41
|
+
|
|
42
|
+
type_val = {&buf[0], buf_idx};
|
|
43
|
+
keys[key] = {(std::uint8_t) type_val.type, type_val.type_size, buf_idx,
|
|
44
|
+
type_val.n_vals, is_geno};
|
|
45
|
+
buf_idx += (type_val.n_vals * type_val.type_size * n_samples);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
std::vector<std::string> SampleData::get_keys() {
|
|
50
|
+
std::vector<std::string> key_names;
|
|
51
|
+
for (auto & x : keys) {
|
|
52
|
+
key_names.push_back(x.first);
|
|
53
|
+
}
|
|
54
|
+
return key_names;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
FormatType SampleData::get_type(std::string &key) {
|
|
58
|
+
if (keys.count(key) == 0) {
|
|
59
|
+
throw std::invalid_argument("no entries for " + key + " in data");
|
|
60
|
+
}
|
|
61
|
+
return keys[key];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
std::vector<std::int32_t> SampleData::get_ints(FormatType & type) {
|
|
65
|
+
if (type.is_geno) {
|
|
66
|
+
return get_geno(type);
|
|
67
|
+
} else if (type.type_size == 1) {
|
|
68
|
+
return parse_8bit_ints(type);
|
|
69
|
+
} else if (type.type_size == 2) {
|
|
70
|
+
parse_16bit_ints(type);
|
|
71
|
+
} else if (type.type_size == 4) {
|
|
72
|
+
parse_32bit_ints(type);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
std::vector<std::int32_t> vals;
|
|
76
|
+
vals.resize(type.n_vals * n_samples);
|
|
77
|
+
std::uint32_t offset = type.offset;
|
|
78
|
+
std::uint32_t n=0;
|
|
79
|
+
for (; n < (n_samples * type.n_vals); n++) {
|
|
80
|
+
vals[n] = parse_int(&buf[0], offset, type.type_size);
|
|
81
|
+
}
|
|
82
|
+
return vals;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// convert 8-bit missing values to the 32-bit form
|
|
86
|
+
#if defined(__x86_64__)
|
|
87
|
+
__m128i missing_8bit_to_32bit(__m128i data) {
|
|
88
|
+
__m128 mask;
|
|
89
|
+
const __m128i missing_8bit = _mm_set_epi32(0xffffff80, 0xffffff80, 0xffffff80, 0xffffff80);
|
|
90
|
+
const __m128i missing_32bit = _mm_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000);
|
|
91
|
+
|
|
92
|
+
// find which entries have missing values
|
|
93
|
+
mask = (__m128) _mm_cmpeq_epi32(data, missing_8bit);
|
|
94
|
+
|
|
95
|
+
// erase original missing values
|
|
96
|
+
data = (__m128i) _mm_andnot_ps((__m128) mask, (__m128) data);
|
|
97
|
+
|
|
98
|
+
// replace 8-bit missing value with 32-bit missing value
|
|
99
|
+
return (__m128i) _mm_or_ps((__m128) data, _mm_and_ps((__m128) missing_32bit, (__m128) mask));
|
|
100
|
+
}
|
|
101
|
+
#elif defined(__aarch64__)
|
|
102
|
+
int32x4_t missing_8bit_to_32bit(int32x4_t data) {
|
|
103
|
+
int32x4_t mask;
|
|
104
|
+
const int32x4_t missing_8bit = vdupq_n_s32(0xffffff80);
|
|
105
|
+
const int32x4_t missing_32bit = vdupq_n_s32(0x80000000);
|
|
106
|
+
|
|
107
|
+
// find which entries have missing values
|
|
108
|
+
mask = vceqq_s32(data, missing_8bit);
|
|
109
|
+
|
|
110
|
+
// erase original missing values
|
|
111
|
+
data = vandq_s32(data, vmvnq_s32(mask));
|
|
112
|
+
|
|
113
|
+
// replace 8-bit missing value with 32-bit missing value
|
|
114
|
+
return vorrq_s32(data, vandq_s32(missing_32bit, mask));
|
|
115
|
+
}
|
|
116
|
+
#endif
|
|
117
|
+
|
|
118
|
+
/// convert 16-bit missing values to the 32-bit form
|
|
119
|
+
#if defined(__x86_64__)
|
|
120
|
+
__m128i missing_16bit_to_32bit(__m128i data) {
|
|
121
|
+
__m128 mask;
|
|
122
|
+
const __m128i missing_16bit = _mm_set_epi32(0xffff8000, 0xffff8000, 0xffff8000, 0xffff8000);
|
|
123
|
+
const __m128i missing_32bit = _mm_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000);
|
|
124
|
+
|
|
125
|
+
// find which entries have missing values
|
|
126
|
+
mask = (__m128) _mm_cmpeq_epi32(data, missing_16bit);
|
|
127
|
+
|
|
128
|
+
// erase original missing values
|
|
129
|
+
data = (__m128i) _mm_andnot_ps((__m128) mask, (__m128) data);
|
|
130
|
+
|
|
131
|
+
// replace 16-bit missing value with 32-bit missing value
|
|
132
|
+
return (__m128i) _mm_or_ps((__m128) data, _mm_and_ps((__m128) missing_32bit, (__m128) mask));
|
|
133
|
+
}
|
|
134
|
+
#elif defined(__aarch64__)
|
|
135
|
+
int32x4_t missing_16bit_to_32bit(int32x4_t data) {
|
|
136
|
+
int32x4_t mask;
|
|
137
|
+
const int32x4_t missing_16bit = vdupq_n_s32(0xffff8000);
|
|
138
|
+
const int32x4_t missing_32bit = vdupq_n_s32(0x80000000);
|
|
139
|
+
|
|
140
|
+
// find which entries have missing values
|
|
141
|
+
mask = vceqq_s32(data, missing_16bit);
|
|
142
|
+
|
|
143
|
+
// erase original missing values
|
|
144
|
+
data = vandq_s32(data, vmvnq_s32(mask));
|
|
145
|
+
|
|
146
|
+
// replace 16-bit missing value with 32-bit missing value
|
|
147
|
+
return vorrq_s32(data, vandq_s32(missing_32bit, mask));
|
|
148
|
+
}
|
|
149
|
+
#endif
|
|
150
|
+
|
|
151
|
+
std::vector<std::int32_t> SampleData::parse_8bit_ints(FormatType & type) {
|
|
152
|
+
std::vector<std::int32_t> vals;
|
|
153
|
+
std::uint64_t max_n = type.n_vals * n_samples;
|
|
154
|
+
vals.resize(max_n);
|
|
155
|
+
std::uint32_t offset = type.offset;
|
|
156
|
+
std::uint32_t n=0;
|
|
157
|
+
|
|
158
|
+
#if defined(__x86_64__)
|
|
159
|
+
if (__builtin_cpu_supports("avx2")) {
|
|
160
|
+
__m256i data, mask;
|
|
161
|
+
__m128i low, hi;
|
|
162
|
+
// std::int32_t missing - 8bit: 0x80, 16bit: 0x8000 etc
|
|
163
|
+
// std::int32_t not_recorded - missing | 0x1 8bit: 0x81, 16bit: 0x8001, 32bit: 0x80000001
|
|
164
|
+
__m256i missing = _mm256_set_epi32(0x80808080, 0x80808080, 0x80808080, 0x80808080,
|
|
165
|
+
0x80808080, 0x80808080, 0x80808080, 0x80808080);
|
|
166
|
+
__m256i not_recorded = _mm256_set_epi32(0x81818181, 0x81818181, 0x81818181, 0x81818181,
|
|
167
|
+
0x81818181, 0x81818181, 0x81818181, 0x81818181);
|
|
168
|
+
|
|
169
|
+
for (; n < (max_n - (max_n % 32)); n += 32) {
|
|
170
|
+
data = _mm256_loadu_si256((__m256i *) &buf[offset + n]);
|
|
171
|
+
|
|
172
|
+
// replace the missing indicator values with standard missing values
|
|
173
|
+
mask = _mm256_cmpeq_epi8(data, not_recorded);
|
|
174
|
+
data = (__m256i) _mm256_andnot_ps((__m256) mask, (__m256) data); // erase original not recorded values
|
|
175
|
+
data = (__m256i) _mm256_or_ps((__m256) data, _mm256_and_ps((__m256) missing, (__m256) mask)); // replace not recorded values with standard missing value
|
|
176
|
+
|
|
177
|
+
// widen 8-bit ints to 16 then 32bit, standardize missing value, and store
|
|
178
|
+
low = _mm256_extractf128_si256(data, 0);
|
|
179
|
+
hi = _mm256_extractf128_si256(data, 1);
|
|
180
|
+
_mm_storeu_ps((float *) &vals[n], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(low)));
|
|
181
|
+
_mm_storeu_ps((float *) &vals[n + 4], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(_mm_bsrli_si128(low, 4))));
|
|
182
|
+
_mm_storeu_ps((float *) &vals[n + 8], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(_mm_bsrli_si128(low, 8))));
|
|
183
|
+
_mm_storeu_ps((float *) &vals[n + 12], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(_mm_bsrli_si128(low, 12))));
|
|
184
|
+
_mm_storeu_ps((float *) &vals[n + 16], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(hi)));
|
|
185
|
+
_mm_storeu_ps((float *) &vals[n + 20], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 4))));
|
|
186
|
+
_mm_storeu_ps((float *) &vals[n + 24], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 8))));
|
|
187
|
+
_mm_storeu_ps((float *) &vals[n + 28], (__m128) missing_8bit_to_32bit(_mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 12))));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
#elif defined(__aarch64__)
|
|
191
|
+
int8x16_t data, mask;
|
|
192
|
+
int16x8_t wider;
|
|
193
|
+
int8x16_t missing = vdupq_n_s8(0x80);
|
|
194
|
+
int8x16_t not_recorded = vdupq_n_s8(0x81);
|
|
195
|
+
|
|
196
|
+
for (; n < (max_n - (max_n % 16)); n += 16) {
|
|
197
|
+
// load data from the array into SIMD registers.
|
|
198
|
+
data = vld1q_s8((std::int8_t *)&buf[offset + n]);
|
|
199
|
+
|
|
200
|
+
// replace the missing indicator values with standard missing values
|
|
201
|
+
mask = vceqq_s8(data, not_recorded); // find and set missing values
|
|
202
|
+
data = vandq_s8(data, vmvnq_s8(mask)); // erase original missing values
|
|
203
|
+
data = vorrq_s8(data, vandq_s8(missing, mask)); // swap in new missing values
|
|
204
|
+
|
|
205
|
+
// store genotypes as 32-bit ints, have to expand all values in turn
|
|
206
|
+
wider = vmovl_s8(vget_low_s8(data));
|
|
207
|
+
vst1q_s32(&vals[n], missing_8bit_to_32bit(vmovl_s16(vget_low_s16(wider))));
|
|
208
|
+
vst1q_s32(&vals[n + 4], missing_8bit_to_32bit(vmovl_s16(vget_high_s16(wider))));
|
|
209
|
+
|
|
210
|
+
wider = vmovl_s8(vget_high_s8(data));
|
|
211
|
+
vst1q_s32(&vals[n + 8], missing_8bit_to_32bit(vmovl_s16(vget_low_s16(wider))));
|
|
212
|
+
vst1q_s32(&vals[n + 12], missing_8bit_to_32bit(vmovl_s16(vget_high_s16(wider))));
|
|
213
|
+
}
|
|
214
|
+
#endif
|
|
215
|
+
offset += n;
|
|
216
|
+
for (; n < (n_samples * type.n_vals); n++) {
|
|
217
|
+
vals[n] = parse_int(&buf[0], offset, type.type_size);
|
|
218
|
+
}
|
|
219
|
+
return vals;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
std::vector<std::int32_t> SampleData::parse_16bit_ints(FormatType & type) {
|
|
223
|
+
std::vector<std::int32_t> vals;
|
|
224
|
+
std::uint64_t max_n = type.n_vals * n_samples;
|
|
225
|
+
vals.resize(max_n);
|
|
226
|
+
std::uint32_t offset = type.offset;
|
|
227
|
+
std::uint32_t n=0;
|
|
228
|
+
|
|
229
|
+
#if defined(__x86_64__)
|
|
230
|
+
if (__builtin_cpu_supports("avx2")) {
|
|
231
|
+
__m256i data, mask;
|
|
232
|
+
__m128i low, hi;
|
|
233
|
+
// std::int32_t missing - 8bit: 0x80, 16bit: 0x8000 etc
|
|
234
|
+
// std::int32_t not_recorded - missing | 0x1 8bit: 0x81, 16bit: 0x8001, 32bit: 0x80000001
|
|
235
|
+
__m256i missing = _mm256_set_epi32(0x80008000, 0x80808080, 0x80808080, 0x80808080,
|
|
236
|
+
0x80808080, 0x80808080, 0x80808080, 0x80808080);
|
|
237
|
+
__m256i not_recorded = _mm256_set_epi32(0x80018001, 0x80018001, 0x80018001, 0x80018001,
|
|
238
|
+
0x80018001, 0x80018001, 0x80018001, 0x80018001);
|
|
239
|
+
|
|
240
|
+
for (; n < (max_n - (max_n % 16)); n += 16) {
|
|
241
|
+
data = _mm256_loadu_si256((__m256i *) &buf[offset + n]);
|
|
242
|
+
|
|
243
|
+
// replace the missing indicator values with standard missing values
|
|
244
|
+
mask = _mm256_cmpeq_epi16(data, not_recorded);
|
|
245
|
+
data = (__m256i) _mm256_andnot_ps((__m256) mask, (__m256) data); // erase original not recorded values
|
|
246
|
+
data = (__m256i) _mm256_or_ps((__m256) data, _mm256_and_ps((__m256) missing, (__m256) mask)); // replace not recorded values with standard missing value
|
|
247
|
+
|
|
248
|
+
// widen 8-bit ints to 16 then 32bit, standardize missing value, and store
|
|
249
|
+
low = _mm256_extractf128_si256(data, 0);
|
|
250
|
+
hi = _mm256_extractf128_si256(data, 1);
|
|
251
|
+
_mm_storeu_ps((float *) &vals[n], (__m128) missing_16bit_to_32bit(_mm_cvtepi16_epi32(low)));
|
|
252
|
+
_mm_storeu_ps((float *) &vals[n + 4], (__m128) missing_16bit_to_32bit(_mm_cvtepi16_epi32(_mm_bsrli_si128(low, 8))));
|
|
253
|
+
_mm_storeu_ps((float *) &vals[n + 8], (__m128) missing_16bit_to_32bit(_mm_cvtepi16_epi32(hi)));
|
|
254
|
+
_mm_storeu_ps((float *) &vals[n + 12], (__m128) missing_16bit_to_32bit(_mm_cvtepi16_epi32(_mm_bsrli_si128(hi, 8))));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
#elif defined(__aarch64__)
|
|
258
|
+
int16x8_t data, mask;
|
|
259
|
+
int16x8_t missing = vdupq_n_s16(0x8000);
|
|
260
|
+
int16x8_t not_recorded = vdupq_n_s16(0x8001);
|
|
261
|
+
|
|
262
|
+
for (; n < (max_n - (max_n % 8)); n += 8) {
|
|
263
|
+
// load data from the array into SIMD registers.
|
|
264
|
+
data = vld1q_s16((std::int16_t *)&buf[offset + n]);
|
|
265
|
+
|
|
266
|
+
// replace the missing indicator values with standard missing values
|
|
267
|
+
mask = vceqq_s16(data, not_recorded); // find and set missing values
|
|
268
|
+
data = vandq_s16(data, vmvnq_s16(mask)); // erase original missing values
|
|
269
|
+
data = vorrq_s16(data, vandq_s16(missing, mask)); // swap in new missing values
|
|
270
|
+
|
|
271
|
+
// store genotypes as 32-bit ints, have to expand all values in turn
|
|
272
|
+
vst1q_s32(&vals[n], missing_16bit_to_32bit(vmovl_s16(vget_low_s16(data))));
|
|
273
|
+
vst1q_s32(&vals[n + 4], missing_16bit_to_32bit(vmovl_s16(vget_high_s16(data))));
|
|
274
|
+
}
|
|
275
|
+
#endif
|
|
276
|
+
offset += n;
|
|
277
|
+
for (; n < (n_samples * type.n_vals); n++) {
|
|
278
|
+
vals[n] = parse_int(&buf[0], offset, type.type_size);
|
|
279
|
+
}
|
|
280
|
+
return vals;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
std::vector<std::int32_t> SampleData::parse_32bit_ints(FormatType & type) {
|
|
284
|
+
std::vector<std::int32_t> vals;
|
|
285
|
+
std::uint64_t max_n = type.n_vals * n_samples;
|
|
286
|
+
vals.resize(max_n);
|
|
287
|
+
std::uint32_t offset = type.offset;
|
|
288
|
+
std::uint32_t n=0;
|
|
289
|
+
|
|
290
|
+
#if defined(__x86_64__)
|
|
291
|
+
if (__builtin_cpu_supports("avx2")) {
|
|
292
|
+
__m256i data, mask;
|
|
293
|
+
// std::int32_t missing - 8bit: 0x80, 16bit: 0x8000 etc
|
|
294
|
+
// std::int32_t not_recorded - missing | 0x1 8bit: 0x81, 16bit: 0x8001, 32bit: 0x80000001
|
|
295
|
+
__m256i missing = _mm256_set_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000,
|
|
296
|
+
0x80000000, 0x80000000, 0x80000000, 0x80000000);
|
|
297
|
+
__m256i not_recorded = _mm256_set_epi32(0x80000001, 0x80000001, 0x80000001, 0x80000001,
|
|
298
|
+
0x80000001, 0x80000001, 0x80000001, 0x80000001);
|
|
299
|
+
for (; n < (max_n - (max_n % 8)); n += 8) {
|
|
300
|
+
data = _mm256_loadu_si256((__m256i *) &buf[offset + n]);
|
|
301
|
+
|
|
302
|
+
// replace the missing indicator values with standard missing values
|
|
303
|
+
mask = _mm256_cmpeq_epi16(data, not_recorded);
|
|
304
|
+
data = (__m256i) _mm256_andnot_ps((__m256) mask, (__m256) data); // erase original not recorded values
|
|
305
|
+
data = (__m256i) _mm256_or_ps((__m256) data, _mm256_and_ps((__m256) missing, (__m256) mask)); // replace not recorded values with standard missing value
|
|
306
|
+
|
|
307
|
+
// store data
|
|
308
|
+
_mm256_storeu_ps((float *) &vals[n], (__m256) data);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
#elif defined(__aarch64__)
|
|
312
|
+
int32x4_t data, mask;
|
|
313
|
+
int32x4_t missing = vdupq_n_s32(0x8000000);
|
|
314
|
+
int32x4_t not_recorded = vdupq_n_s32(0x81);
|
|
315
|
+
|
|
316
|
+
for (; n < (max_n - (max_n % 4)); n += 4) {
|
|
317
|
+
// load data from the array into SIMD registers.
|
|
318
|
+
data = vld1q_s32((std::int32_t *)&buf[offset + n]);
|
|
319
|
+
|
|
320
|
+
// replace the missing indicator values with standard missing values
|
|
321
|
+
mask = vceqq_s32(data, not_recorded); // find and set missing values
|
|
322
|
+
data = vandq_s32(data, vmvnq_s32(mask)); // erase original missing values
|
|
323
|
+
data = vorrq_s32(data, vandq_s32(missing, mask)); // swap in new missing values
|
|
324
|
+
|
|
325
|
+
vst1q_s32(&vals[n], data);
|
|
326
|
+
}
|
|
327
|
+
#endif
|
|
328
|
+
offset += n;
|
|
329
|
+
for (; n < (n_samples * type.n_vals); n++) {
|
|
330
|
+
vals[n] = parse_int(&buf[0], offset, type.type_size);
|
|
331
|
+
}
|
|
332
|
+
return vals;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
std::vector<std::int32_t> SampleData::get_geno(FormatType & type) {
|
|
337
|
+
// confirm we checked sample phasing if we look at the genotype data
|
|
338
|
+
phase_checked = true;
|
|
339
|
+
|
|
340
|
+
std::vector<std::int32_t> vals;
|
|
341
|
+
std::uint64_t max_n = type.n_vals * n_samples;
|
|
342
|
+
vals.resize(max_n);
|
|
343
|
+
std::uint32_t offset = type.offset;
|
|
344
|
+
std::uint32_t n=0;
|
|
345
|
+
#if defined(__x86_64__)
|
|
346
|
+
if (__builtin_cpu_supports("avx2") && (type.n_vals == 2) && (type.type_size == 1)) {
|
|
347
|
+
__m256i initial, geno, phase_vec, missed;
|
|
348
|
+
__m128i low, hi, phase128;
|
|
349
|
+
__m256i mask_phase = _mm256_set_epi32(0x01000100, 0x01000100, 0x01000100, 0x01000100,
|
|
350
|
+
0x01000100, 0x01000100, 0x01000100, 0x01000100);
|
|
351
|
+
__m256i mask_geno = _mm256_set_epi32(0xfefefefe, 0xfefefefe, 0xfefefefe, 0xfefefefe,
|
|
352
|
+
0xfefefefe, 0xfefefefe, 0xfefefefe, 0xfefefefe);
|
|
353
|
+
__m256i sub = _mm256_set_epi64x(0x0101010101010101, 0x0101010101010101, 0x0101010101010101, 0x0101010101010101);
|
|
354
|
+
__m128i missing_mask = _mm_set_epi32(0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff);
|
|
355
|
+
__m128i missing_geno = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
|
|
356
|
+
__m256i missing_indicator = _mm256_set_epi32(0x81818181, 0x81818181, 0x81818181, 0x81818181,
|
|
357
|
+
0x81818181, 0x81818181, 0x81818181, 0x81818181);
|
|
358
|
+
__m128i shuffle = _mm_set_epi8(0, 2, 4, 6, 8, 10, 12, 14, 17, 3, 5, 7, 9, 11, 13, 15);
|
|
359
|
+
|
|
360
|
+
for (; n < (max_n - (max_n % 32)); n += 32) {
|
|
361
|
+
initial = _mm256_loadu_si256((__m256i *) &buf[offset + n]);
|
|
362
|
+
|
|
363
|
+
geno = _mm256_and_si256(initial, mask_geno);
|
|
364
|
+
geno = _mm256_sub_epi8(_mm256_srli_epi32(geno, 1), sub);
|
|
365
|
+
|
|
366
|
+
// account for missing values (due to different ploidy between samples)
|
|
367
|
+
missed = _mm256_cmpeq_epi8(initial, missing_indicator); // find missing values
|
|
368
|
+
geno = (__m256i) _mm256_andnot_ps((__m256)missed, (__m256)geno); // erase original missing values
|
|
369
|
+
geno = (__m256i) _mm256_or_ps((__m256)geno, (__m256)missed); // swap in new missing values
|
|
370
|
+
|
|
371
|
+
// expand the first 8 values to 32-bits, and store
|
|
372
|
+
low = _mm256_extractf128_si256(geno, 0);
|
|
373
|
+
hi = _mm256_extractf128_si256(geno, 1);
|
|
374
|
+
_mm_storeu_ps((float *) &vals[n], (__m128) _mm_cvtepi8_epi32(low));
|
|
375
|
+
_mm_storeu_ps((float *) &vals[n + 4], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(low, 4)));
|
|
376
|
+
_mm_storeu_ps((float *) &vals[n + 8], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(low, 8)));
|
|
377
|
+
_mm_storeu_ps((float *) &vals[n + 12], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(low, 12)));
|
|
378
|
+
_mm_storeu_ps((float *) &vals[n + 16], (__m128) _mm_cvtepi8_epi32(hi));
|
|
379
|
+
_mm_storeu_ps((float *) &vals[n + 20], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 4)));
|
|
380
|
+
_mm_storeu_ps((float *) &vals[n + 24], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 8)));
|
|
381
|
+
_mm_storeu_ps((float *) &vals[n + 28], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 12)));
|
|
382
|
+
|
|
383
|
+
// check for missing genotypes. We can check if every second genotype is
|
|
384
|
+
// -1, since that is the missing genotype indicator, then shuffle the data
|
|
385
|
+
// to remove the interspersed bytes.
|
|
386
|
+
low = _mm_or_si128(low, missing_mask);
|
|
387
|
+
hi = _mm_or_si128(hi, missing_mask);
|
|
388
|
+
low = _mm_or_si128(low, _mm_bsrli_si128(hi, 1));
|
|
389
|
+
low = _mm_abs_epi8(_mm_and_si128(low, missing_geno));
|
|
390
|
+
low = _mm_shuffle_epi8(low, shuffle);
|
|
391
|
+
_mm_storeu_ps((float *) &missing[n >> 1], (__m128) low);
|
|
392
|
+
|
|
393
|
+
// reorganize the phase data into correctly sorted form. Phase data is
|
|
394
|
+
// initially every second byte across the m256 register. First convert to
|
|
395
|
+
// two, m128 registers, interleave those, then shuffle to correct order.
|
|
396
|
+
phase_vec = _mm256_and_si256(initial, mask_phase);
|
|
397
|
+
low = _mm256_extractf128_si256(phase_vec, 0);
|
|
398
|
+
hi = _mm256_extractf128_si256(phase_vec, 1);
|
|
399
|
+
|
|
400
|
+
phase128 = _mm_or_si128(_mm_bsrli_si128(low, 1), hi);
|
|
401
|
+
phase128 = _mm_shuffle_epi8(phase128, shuffle);
|
|
402
|
+
_mm_storeu_ps((float *) &phase[n >> 1], (__m128)phase128);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
#elif defined(__aarch64__)
|
|
406
|
+
if ((type.type_size == 1) && (type.n_vals == 2)) {
|
|
407
|
+
|
|
408
|
+
int8x16_t initial, geno, missed;
|
|
409
|
+
uint16x8_t wider;
|
|
410
|
+
int8x8_t shrunk;
|
|
411
|
+
|
|
412
|
+
uint8x16_t missing_mask = vdupq_n_u64(0x00ff00ff00ff00ff);
|
|
413
|
+
uint8x16_t mask_phase = vdupq_n_u64(0x0001000100010001);
|
|
414
|
+
uint8x16_t mask_geno = vdupq_n_u8(0xfe);
|
|
415
|
+
uint8x16_t sub = vdupq_n_u8(0x01);
|
|
416
|
+
int8x8_t missing_geno = vdup_n_s8(-1);
|
|
417
|
+
int8x16_t missing_indicator = vdupq_n_s8(0x80);
|
|
418
|
+
|
|
419
|
+
for (; n < (max_n - (max_n % 16)); n += 16) {
|
|
420
|
+
// load data from the array into SIMD registers.
|
|
421
|
+
initial = vld1q_s8((std::int8_t *)&buf[offset + n]);
|
|
422
|
+
|
|
423
|
+
geno = vandq_s8(initial, mask_geno);
|
|
424
|
+
geno = vsubq_s8(vshrq_n_s8(geno, 1), sub); // shift right to remove phase bit,
|
|
425
|
+
// and subtract 1 to get allele
|
|
426
|
+
|
|
427
|
+
// account for missing values (due to different ploidy between samples)
|
|
428
|
+
missed = vceqq_s8(initial, missing_indicator); // find and set missing values
|
|
429
|
+
geno = vandq_s8(geno, vmvnq_s8(missed)); // erase original missing values
|
|
430
|
+
geno = vorrq_s8(geno, missed); // swap in new missing values
|
|
431
|
+
|
|
432
|
+
// store genotypes as 32-bit ints, have to expand all values in turn
|
|
433
|
+
wider = vmovl_s8(vget_low_s8(geno));
|
|
434
|
+
vst1q_s32(&vals[n], vmovl_s16(vget_low_s16(wider)));
|
|
435
|
+
vst1q_s32(&vals[n + 4], vmovl_s16(vget_high_s16(wider)));
|
|
436
|
+
|
|
437
|
+
wider = vmovl_s8(vget_high_s8(geno));
|
|
438
|
+
vst1q_s32(&vals[n + 8], vmovl_s16(vget_low_s16(wider)));
|
|
439
|
+
vst1q_s32(&vals[n + 12], vmovl_s16(vget_high_s16(wider)));
|
|
440
|
+
|
|
441
|
+
// check for missing genotypes
|
|
442
|
+
geno = vandq_s8(geno, missing_mask); // mask out every second value
|
|
443
|
+
shrunk = vmovn_s16(geno); // narrow to remove interspersed bytes
|
|
444
|
+
shrunk = vabs_s8(vand_s8(shrunk, missing_geno)); // check if value == -1
|
|
445
|
+
vst1_u8(&missing[n >> 1], shrunk);
|
|
446
|
+
|
|
447
|
+
// check if each sample has phased data
|
|
448
|
+
initial = vandq_s8(initial, mask_phase); // keep one mask bit per sample
|
|
449
|
+
shrunk = vmovn_s16(initial); // narrow to remove interspersed bytes
|
|
450
|
+
vst1_u8(&phase[n >> 1], shrunk);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
#endif
|
|
454
|
+
|
|
455
|
+
std::int32_t missing_indicator = 1 << ((8 * type.type_size) - 1);
|
|
456
|
+
offset += n;
|
|
457
|
+
std::uint32_t idx=n;
|
|
458
|
+
n = n / type.n_vals;
|
|
459
|
+
for (; n < n_samples; n++) {
|
|
460
|
+
for (std::uint32_t i = 0; i < type.n_vals; i++) {
|
|
461
|
+
vals[idx] = parse_int(&buf[0], offset, type.type_size);
|
|
462
|
+
if (vals[idx] == missing_indicator) {
|
|
463
|
+
vals[idx] = 0; // convert missing values to missing genotypes
|
|
464
|
+
}
|
|
465
|
+
phase[n] = vals[idx] & 0x00000001;
|
|
466
|
+
vals[idx] = (vals[idx] >> 1) - 1;
|
|
467
|
+
// this only checks on genotype status, but this should apply to other
|
|
468
|
+
// fields too (AD, DP etc), as if a sample lacks gt, other fields
|
|
469
|
+
// should also be absent
|
|
470
|
+
missing[n] = vals[idx] == -1;
|
|
471
|
+
idx++;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return vals;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
std::vector<float> SampleData::get_floats(FormatType & type) {
|
|
478
|
+
std::vector<float> vals;
|
|
479
|
+
vals.resize(type.n_vals * n_samples);
|
|
480
|
+
std::memcpy(&vals[0], &buf[type.offset], type.n_vals * n_samples * 4);
|
|
481
|
+
return vals;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
std::vector<std::string> SampleData::get_strings(FormatType & type) {
|
|
485
|
+
std::vector<std::string> vals;
|
|
486
|
+
vals.resize(n_samples);
|
|
487
|
+
std::uint32_t offset = type.offset;
|
|
488
|
+
for (std::uint32_t n=0; n < n_samples; n++) {
|
|
489
|
+
vals[n] = parse_string(&buf[0], offset, type.n_vals);
|
|
490
|
+
}
|
|
491
|
+
return vals;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
}
|
|
@@ -27,6 +27,9 @@ class SampleData {
|
|
|
27
27
|
std::vector<char> buf;
|
|
28
28
|
Header * header;
|
|
29
29
|
std::vector<std::int32_t> get_geno(FormatType &type);
|
|
30
|
+
std::vector<std::int32_t> parse_8bit_ints(FormatType & type);
|
|
31
|
+
std::vector<std::int32_t> parse_16bit_ints(FormatType & type);
|
|
32
|
+
std::vector<std::int32_t> parse_32bit_ints(FormatType & type);
|
|
30
33
|
public:
|
|
31
34
|
SampleData(igzstream &infile, Header &_header, std::uint32_t len, std::uint32_t n_fmt, std::uint32_t _n_samples);
|
|
32
35
|
SampleData(){};
|
|
@@ -51,8 +51,8 @@ class TestBcfReader(unittest.TestCase):
|
|
|
51
51
|
''' class to make sure BcfReader works correctly
|
|
52
52
|
'''
|
|
53
53
|
|
|
54
|
-
def
|
|
55
|
-
''' check this package matches pysam for
|
|
54
|
+
def test_without_sample_data(self):
|
|
55
|
+
''' check this package matches pysam for BCF without sample data
|
|
56
56
|
'''
|
|
57
57
|
path = Path(__file__).parent / 'data' / 'hapmap_3.3.hg38.shrunk.bcf'
|
|
58
58
|
vcf_pysam = VariantFile(path)
|
|
@@ -72,8 +72,8 @@ class TestBcfReader(unittest.TestCase):
|
|
|
72
72
|
for field in var_pysam.info:
|
|
73
73
|
self.assertEqual(var_pysam.info[field], var_pybcf.info[field],)
|
|
74
74
|
|
|
75
|
-
def
|
|
76
|
-
''' check this package matches pysam for
|
|
75
|
+
def test_with_sample_data(self):
|
|
76
|
+
''' check this package matches pysam for BCF with per sample data
|
|
77
77
|
'''
|
|
78
78
|
path = Path(__file__).parent / 'data' / '1000G.shrunk.bcf'
|
|
79
79
|
vcf_pysam = VariantFile(path)
|
|
@@ -101,6 +101,58 @@ class TestBcfReader(unittest.TestCase):
|
|
|
101
101
|
|
|
102
102
|
self.assertTrue((is_nan_pysam == is_nan_pybcf).all())
|
|
103
103
|
self.assertTrue((geno_pybcf[~is_nan_pysam] == geno_pysam[~is_nan_pysam]).all())
|
|
104
|
+
|
|
105
|
+
self.assertEqual(set(var_pysam.format), set(var_pybcf.samples))
|
|
106
|
+
for key in var_pybcf.samples:
|
|
107
|
+
if key == 'GT':
|
|
108
|
+
continue
|
|
109
|
+
data_pybcf = var_pybcf.samples[key]
|
|
110
|
+
data_pysam = [x[key] for x in var_pysam.samples.itervalues()]
|
|
111
|
+
|
|
112
|
+
if isinstance(data_pybcf, list):
|
|
113
|
+
for x, y in zip(data_pysam, data_pybcf):
|
|
114
|
+
if isinstance(x, str) and isinstance(y, tuple):
|
|
115
|
+
assert len(y) == 1
|
|
116
|
+
y = y[0]
|
|
117
|
+
self.assertEqual(x, y)
|
|
118
|
+
|
|
119
|
+
# pysam and pybcf represent the data differently. pysam returns
|
|
120
|
+
# a ragged list, whereas pybcf inserts nan values in the gaps
|
|
121
|
+
elif len(data_pybcf.shape) > 1:
|
|
122
|
+
sums_pybcf = (~np.isnan(data_pybcf)).sum(axis=1)
|
|
123
|
+
sums_pysam = np.array([len(x) for x in data_pysam])
|
|
124
|
+
|
|
125
|
+
# finding values to skip
|
|
126
|
+
skip = sums_pybcf == 0
|
|
127
|
+
if skip.sum() > 0:
|
|
128
|
+
# the places where sums_bcf is zero correspond to pysam
|
|
129
|
+
# gave a (None, ) tuple
|
|
130
|
+
self.assertTrue((sums_pysam[skip] == 1).all())
|
|
131
|
+
self.assertTrue([x for x, keep in zip(data_pysam, skip) if keep] == [(None, )] * skip.sum())
|
|
132
|
+
|
|
133
|
+
sums_pybcf = sums_pybcf[~skip]
|
|
134
|
+
sums_pysam = sums_pysam[~skip]
|
|
135
|
+
|
|
136
|
+
data_pybcf = data_pybcf[~skip]
|
|
137
|
+
data_pysam = [x for x, drop in zip(data_pysam, skip) if not drop]
|
|
138
|
+
|
|
139
|
+
self.assertTrue((sums_pybcf == sums_pysam).all())
|
|
140
|
+
data_pybcf = [tuple(x[~np.isnan(x)].tolist()) for x in data_pybcf]
|
|
141
|
+
self.assertEqual(data_pybcf, data_pysam)
|
|
142
|
+
else:
|
|
143
|
+
data_pysam = np.array(data_pysam)
|
|
144
|
+
if len(data_pysam.shape) > 1 and max(len(x) for x in data_pysam):
|
|
145
|
+
data_pysam = data_pysam.T
|
|
146
|
+
|
|
147
|
+
not_nan = ~np.isnan(data_pybcf)
|
|
148
|
+
data_pybcf = data_pybcf[not_nan]
|
|
149
|
+
data_pysam = data_pysam[not_nan]
|
|
150
|
+
|
|
151
|
+
for i, (x, y) in enumerate(zip(data_pybcf, data_pysam)):
|
|
152
|
+
if x != y:
|
|
153
|
+
print(i, x, y)
|
|
154
|
+
|
|
155
|
+
self.assertTrue((data_pybcf == data_pysam).all())
|
|
104
156
|
|
|
105
157
|
def test_header_access(self):
|
|
106
158
|
''' check this package matches pysam for the header fields
|
pybcf-1.3.3/src/sample_data.cpp
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
#include <cstring>
|
|
3
|
-
|
|
4
|
-
#include "sample_data.h"
|
|
5
|
-
#include "types.h"
|
|
6
|
-
|
|
7
|
-
#if defined(__x86_64__)
|
|
8
|
-
#include <immintrin.h>
|
|
9
|
-
#endif
|
|
10
|
-
|
|
11
|
-
#if defined(__aarch64__)
|
|
12
|
-
#include <arm_neon.h>
|
|
13
|
-
#endif
|
|
14
|
-
|
|
15
|
-
namespace bcf {
|
|
16
|
-
|
|
17
|
-
SampleData::SampleData(igzstream & infile, Header & _header, std::uint32_t len, std::uint32_t n_fmt, std::uint32_t _n_samples) {
|
|
18
|
-
n_samples = _n_samples;
|
|
19
|
-
header = &_header;
|
|
20
|
-
if (len == 0) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
phase.resize(n_samples);
|
|
24
|
-
missing.resize(n_samples);
|
|
25
|
-
|
|
26
|
-
// read the sample data into a buffer, but don't parse until required
|
|
27
|
-
buf.resize(len);
|
|
28
|
-
infile.read(reinterpret_cast<char *>(&buf[0]), len);
|
|
29
|
-
|
|
30
|
-
// read the available keys
|
|
31
|
-
std::uint32_t buf_idx=0;
|
|
32
|
-
std::uint32_t format_idx=0;
|
|
33
|
-
std::string key;
|
|
34
|
-
Typed type_val;
|
|
35
|
-
bool is_geno;
|
|
36
|
-
for (std::uint32_t i = 0; i < n_fmt; i++ ){
|
|
37
|
-
type_val = {&buf[0], buf_idx};
|
|
38
|
-
format_idx = parse_int(&buf[0], buf_idx, type_val.type_size);
|
|
39
|
-
key = header->format[format_idx].id;
|
|
40
|
-
is_geno = key == "GT";
|
|
41
|
-
|
|
42
|
-
type_val = {&buf[0], buf_idx};
|
|
43
|
-
keys[key] = {(std::uint8_t) type_val.type, type_val.type_size, buf_idx,
|
|
44
|
-
type_val.n_vals, is_geno};
|
|
45
|
-
buf_idx += (type_val.n_vals * type_val.type_size * n_samples);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
std::vector<std::string> SampleData::get_keys() {
|
|
50
|
-
std::vector<std::string> key_names;
|
|
51
|
-
for (auto & x : keys) {
|
|
52
|
-
key_names.push_back(x.first);
|
|
53
|
-
}
|
|
54
|
-
return key_names;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
FormatType SampleData::get_type(std::string &key) {
|
|
58
|
-
if (keys.count(key) == 0) {
|
|
59
|
-
throw std::invalid_argument("no entries for " + key + " in data");
|
|
60
|
-
}
|
|
61
|
-
return keys[key];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
std::vector<std::int32_t> SampleData::get_ints(FormatType & type) {
|
|
65
|
-
if (type.is_geno) {
|
|
66
|
-
return get_geno(type);
|
|
67
|
-
}
|
|
68
|
-
std::vector<std::int32_t> vals;
|
|
69
|
-
vals.resize(type.n_vals * n_samples);
|
|
70
|
-
std::uint32_t offset = type.offset;
|
|
71
|
-
std::uint32_t idx=0;
|
|
72
|
-
for (std::uint32_t n=0; n < n_samples; n++) {
|
|
73
|
-
for (std::uint32_t i = 0; i < type.n_vals; i++) {
|
|
74
|
-
vals[idx] = parse_int(&buf[0], offset, type.type_size);
|
|
75
|
-
idx++;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return vals;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
std::vector<std::int32_t> SampleData::get_geno(FormatType & type) {
|
|
82
|
-
// confirm we checked sample phasing if we look at the genotype data
|
|
83
|
-
phase_checked = true;
|
|
84
|
-
|
|
85
|
-
std::vector<std::int32_t> vals;
|
|
86
|
-
std::uint64_t max_n = type.n_vals * n_samples;
|
|
87
|
-
vals.resize(max_n);
|
|
88
|
-
std::uint32_t offset = type.offset;
|
|
89
|
-
std::uint32_t n=0;
|
|
90
|
-
#if defined(__x86_64__)
|
|
91
|
-
if (__builtin_cpu_supports("avx2") && (type.n_vals == 2) && (type.type_size == 1)) {
|
|
92
|
-
__m256i initial, geno, phase_vec, missed;
|
|
93
|
-
__m128i low, hi, phase128;
|
|
94
|
-
__m256i mask_phase = _mm256_set_epi32(0x01000100, 0x01000100, 0x01000100, 0x01000100,
|
|
95
|
-
0x01000100, 0x01000100, 0x01000100, 0x01000100);
|
|
96
|
-
__m256i mask_geno = _mm256_set_epi32(0xfefefefe, 0xfefefefe, 0xfefefefe, 0xfefefefe,
|
|
97
|
-
0xfefefefe, 0xfefefefe, 0xfefefefe, 0xfefefefe);
|
|
98
|
-
__m256i sub = _mm256_set_epi64x(0x0101010101010101, 0x0101010101010101, 0x0101010101010101, 0x0101010101010101);
|
|
99
|
-
__m128i missing_mask = _mm_set_epi32(0x00ff00ff, 0x00ff00ff, 0x00ff00ff, 0x00ff00ff);
|
|
100
|
-
__m128i missing_geno = _mm_set_epi8(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
|
|
101
|
-
__m256i missing_indicator = _mm256_set_epi32(0x81818181, 0x81818181, 0x81818181, 0x81818181,
|
|
102
|
-
0x81818181, 0x81818181, 0x81818181, 0x81818181);
|
|
103
|
-
__m128i shuffle = _mm_set_epi8(0, 2, 4, 6, 8, 10, 12, 14, 17, 3, 5, 7, 9, 11, 13, 15);
|
|
104
|
-
|
|
105
|
-
for (; n < (max_n - (max_n % 32)); n += 32) {
|
|
106
|
-
initial = _mm256_loadu_si256((__m256i *) &buf[offset + n]);
|
|
107
|
-
|
|
108
|
-
geno = _mm256_and_si256(initial, mask_geno);
|
|
109
|
-
geno = _mm256_sub_epi8(_mm256_srli_epi32(geno, 1), sub);
|
|
110
|
-
|
|
111
|
-
// account for missing values (due to different ploidy between samples)
|
|
112
|
-
missed = _mm256_cmpeq_epi8(initial, missing_indicator); // find missing values
|
|
113
|
-
geno = (__m256i) _mm256_andnot_ps((__m256)missed, (__m256)geno); // erase original missing values
|
|
114
|
-
geno = (__m256i) _mm256_or_ps((__m256)geno, (__m256)missed); // swap in new missing values
|
|
115
|
-
|
|
116
|
-
// expand the first 8 values to 32-bits, and store
|
|
117
|
-
low = _mm256_extractf128_si256(geno, 0);
|
|
118
|
-
hi = _mm256_extractf128_si256(geno, 1);
|
|
119
|
-
_mm_storeu_ps((float *) &vals[n], (__m128) _mm_cvtepi8_epi32(low));
|
|
120
|
-
_mm_storeu_ps((float *) &vals[n + 4], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(low, 4)));
|
|
121
|
-
_mm_storeu_ps((float *) &vals[n + 8], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(low, 8)));
|
|
122
|
-
_mm_storeu_ps((float *) &vals[n + 12], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(low, 12)));
|
|
123
|
-
_mm_storeu_ps((float *) &vals[n + 16], (__m128) _mm_cvtepi8_epi32(hi));
|
|
124
|
-
_mm_storeu_ps((float *) &vals[n + 20], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 4)));
|
|
125
|
-
_mm_storeu_ps((float *) &vals[n + 24], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 8)));
|
|
126
|
-
_mm_storeu_ps((float *) &vals[n + 28], (__m128) _mm_cvtepi8_epi32(_mm_bsrli_si128(hi, 12)));
|
|
127
|
-
|
|
128
|
-
// check for missing genotypes. We can check if every second genotype is
|
|
129
|
-
// -1, since that is the missing genotype indicator, then shuffle the data
|
|
130
|
-
// to remove the interspersed bytes.
|
|
131
|
-
low = _mm_or_si128(low, missing_mask);
|
|
132
|
-
hi = _mm_or_si128(hi, missing_mask);
|
|
133
|
-
low = _mm_or_si128(low, _mm_bsrli_si128(hi, 1));
|
|
134
|
-
low = _mm_abs_epi8(_mm_and_si128(low, missing_geno));
|
|
135
|
-
low = _mm_shuffle_epi8(low, shuffle);
|
|
136
|
-
_mm_storeu_ps((float *) &missing[n >> 1], (__m128) low);
|
|
137
|
-
|
|
138
|
-
// reorganize the phase data into correctly sorted form. Phase data is
|
|
139
|
-
// initially every second byte across the m256 register. First convert to
|
|
140
|
-
// two, m128 registers, interleave those, then shuffle to correct order.
|
|
141
|
-
phase_vec = _mm256_and_si256(initial, mask_phase);
|
|
142
|
-
low = _mm256_extractf128_si256(phase_vec, 0);
|
|
143
|
-
hi = _mm256_extractf128_si256(phase_vec, 1);
|
|
144
|
-
|
|
145
|
-
phase128 = _mm_or_si128(_mm_bsrli_si128(low, 1), hi);
|
|
146
|
-
phase128 = _mm_shuffle_epi8(phase128, shuffle);
|
|
147
|
-
_mm_storeu_ps((float *) &phase[n >> 1], (__m128)phase128);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
#elif defined(__aarch64__)
|
|
151
|
-
if ((type.type_size == 1) && (type.n_vals == 2)) {
|
|
152
|
-
|
|
153
|
-
int8x16_t initial, geno, missed;
|
|
154
|
-
uint16x8_t wider;
|
|
155
|
-
int8x8_t shrunk;
|
|
156
|
-
|
|
157
|
-
uint8x16_t missing_mask = vdupq_n_u64(0x00ff00ff00ff00ff);
|
|
158
|
-
uint8x16_t mask_phase = vdupq_n_u64(0x0001000100010001);
|
|
159
|
-
uint8x16_t mask_geno = vdupq_n_u8(0xfe);
|
|
160
|
-
uint8x16_t sub = vdupq_n_u8(0x01);
|
|
161
|
-
int8x8_t missing_geno = vdup_n_s8(-1);
|
|
162
|
-
int8x16_t missing_indicator = vdupq_n_s8(0x80);
|
|
163
|
-
|
|
164
|
-
for (; n < (max_n - (max_n % 16)); n += 16) {
|
|
165
|
-
// load data from the array into SIMD registers.
|
|
166
|
-
initial = vld1q_s8((std::int8_t *)&buf[offset + n]);
|
|
167
|
-
|
|
168
|
-
geno = vandq_s8(initial, mask_geno);
|
|
169
|
-
geno = vsubq_s8(vshrq_n_s8(geno, 1), sub); // shift right to remove phase bit,
|
|
170
|
-
// and subtract 1 to get allele
|
|
171
|
-
|
|
172
|
-
// account for missing values (due to different ploidy between samples)
|
|
173
|
-
missed = vceqq_s8(initial, missing_indicator); // find and set missing values
|
|
174
|
-
geno = vandq_s8(geno, vmvnq_s8(missed)); // erase original missing values
|
|
175
|
-
geno = vorrq_s8(geno, missed); // swap in new missing values
|
|
176
|
-
|
|
177
|
-
// store genotypes as 32-bit ints, have to expand all values in turn
|
|
178
|
-
wider = vmovl_s8(vget_low_s8(geno));
|
|
179
|
-
vst1q_s32(&vals[n], vmovl_s16(vget_low_s16(wider)));
|
|
180
|
-
vst1q_s32(&vals[n + 4], vmovl_s16(vget_high_s16(wider)));
|
|
181
|
-
|
|
182
|
-
wider = vmovl_s8(vget_high_s8(geno));
|
|
183
|
-
vst1q_s32(&vals[n + 8], vmovl_s16(vget_low_s16(wider)));
|
|
184
|
-
vst1q_s32(&vals[n + 12], vmovl_s16(vget_high_s16(wider)));
|
|
185
|
-
|
|
186
|
-
// check for missing genotypes
|
|
187
|
-
geno = vandq_s8(geno, missing_mask); // mask out every second value
|
|
188
|
-
shrunk = vmovn_s16(geno); // narrow to remove interspersed bytes
|
|
189
|
-
shrunk = vabs_s8(vand_s8(shrunk, missing_geno)); // check if value == -1
|
|
190
|
-
vst1_u8(&missing[n >> 1], shrunk);
|
|
191
|
-
|
|
192
|
-
// check if each sample has phased data
|
|
193
|
-
initial = vandq_s8(initial, mask_phase); // keep one mask bit per sample
|
|
194
|
-
shrunk = vmovn_s16(initial); // narrow to remove interspersed bytes
|
|
195
|
-
vst1_u8(&phase[n >> 1], shrunk);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
#endif
|
|
199
|
-
|
|
200
|
-
std::uint32_t missing_indicator = 1 << ((8 * type.type_size) - 1);
|
|
201
|
-
offset += n;
|
|
202
|
-
std::uint32_t idx=n;
|
|
203
|
-
n = n / type.n_vals;
|
|
204
|
-
for (; n < n_samples; n++) {
|
|
205
|
-
for (std::uint32_t i = 0; i < type.n_vals; i++) {
|
|
206
|
-
vals[idx] = parse_int(&buf[0], offset, type.type_size);
|
|
207
|
-
if (vals[idx] == missing_indicator) {
|
|
208
|
-
vals[idx] = 0; // convert missing values to missing genotypes
|
|
209
|
-
}
|
|
210
|
-
phase[n] = vals[idx] & 0x00000001;
|
|
211
|
-
vals[idx] = (vals[idx] >> 1) - 1;
|
|
212
|
-
// this only checks on genotype status, but this should apply to other
|
|
213
|
-
// fields too (AD, DP etc), as if a sample lacks gt, other fields
|
|
214
|
-
// should also be absent
|
|
215
|
-
missing[n] = vals[idx] == -1;
|
|
216
|
-
idx++;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return vals;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
std::vector<float> SampleData::get_floats(FormatType & type) {
|
|
223
|
-
std::vector<float> vals;
|
|
224
|
-
vals.resize(type.n_vals * n_samples);
|
|
225
|
-
std::uint32_t offset = type.offset;
|
|
226
|
-
std::uint32_t idx=0;
|
|
227
|
-
for (std::uint32_t n=0; n < n_samples; n++) {
|
|
228
|
-
for (std::uint32_t i = 0; i < type.n_vals; i++) {
|
|
229
|
-
vals[idx] = parse_float(&buf[0], offset);
|
|
230
|
-
idx++;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return vals;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
std::vector<std::string> SampleData::get_strings(FormatType & type) {
|
|
237
|
-
std::vector<std::string> vals;
|
|
238
|
-
vals.resize(n_samples);
|
|
239
|
-
std::uint32_t offset = type.offset;
|
|
240
|
-
std::uint32_t idx=0;
|
|
241
|
-
for (std::uint32_t n=0; n < n_samples; n++) {
|
|
242
|
-
vals[n] = parse_string(&buf[0], offset, type.n_vals);
|
|
243
|
-
}
|
|
244
|
-
return vals;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|