vash
Fast genetic similarity estimation with hash tables
Loading...
Searching...
No Matches
similarityMatrix.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Anthony J. Greenberg
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 *
8 * 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.
9 *
10 * 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.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
14 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
15 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
16 * 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
17 * THE POSSIBILITY OF SUCH DAMAGE.
18 */
19
21
30#pragma once
31
32#include <vector>
33#include <array>
34#include <string>
35#include <cstdint>
36#include <cstddef>
37
38namespace BayesicSpace {
39 struct RowColIdx;
40 struct FullIdxValue;
41 struct JaccardPair;
42 struct DiffElementPair;
43 struct FullIdxTrio;
44 class SimilarityMatrix;
45
47 struct RowColIdx {
49 uint32_t iRow;
51 uint32_t jCol;
52 };
54 struct FullIdxValue {
56 uint64_t fullIdx;
59 };
61 struct JaccardPair {
63 uint64_t nIntersect;
65 uint64_t nUnion;
66 };
67
77 void chunkedAppend(std::vector<uint64_t> &source, std::vector<uint64_t> &target);
85 [[gnu::warn_unused_result]] RowColIdx recoverRCindexes(const uint64_t &vecIdx) noexcept;
86
95 public:
97 SimilarityMatrix() noexcept = default;
102 SimilarityMatrix(const SimilarityMatrix &toCopy) = default;
108 SimilarityMatrix& operator=(const SimilarityMatrix &toCopy) = default;
113 SimilarityMatrix( SimilarityMatrix &&toMove) noexcept = default;
119 SimilarityMatrix& operator=( SimilarityMatrix &&toMove) noexcept = default;
121 ~SimilarityMatrix() = default;
122
127 [[gnu::warn_unused_result]] static size_t elementSize() noexcept {return sizeof(uint64_t);};
132 [[gnu::warn_unused_result]] size_t objectSize() const noexcept {
133 return elementSize() * matrix_.size();
134 };
139 [[gnu::warn_unused_result]] size_t nElements() const noexcept {return matrix_.size();};
150 void insert(const RowColIdx &rowColPair, const JaccardPair &jaccardCounts);
158 void merge(SimilarityMatrix &toMerge);
168 void save(const std::string &outFileName, const size_t &nThreads, const std::string &locusNameFile = "") const;
169 private:
176 std::vector<uint64_t> matrix_;
177
178 // static members
184 static const std::array<float, 256> floatLookUp_;
190 static const std::array<const char*, 256> stringLookUp_;
192 static const uint64_t maxIdxBitfield_;
197 static const uint32_t maxRowColValue_;
199 static const uint64_t valueMask_;
201 static const uint64_t maxValueIdx_;
203 static const uint64_t valueSize_;
215 [[gnu::warn_unused_result]] static std::string stringify_(std::vector<uint64_t>::const_iterator start, std::vector<uint64_t>::const_iterator end,
216 const std::vector<std::string> &locusNames);
223 void insert_(const FullIdxValue &indexWithSimilarity);
224 };
225}
Similarity matrix.
Definition similarityMatrix.hpp:94
size_t objectSize() const noexcept
Object size in bytes.
Definition similarityMatrix.hpp:132
static size_t elementSize() noexcept
Matrix element size.
Definition similarityMatrix.hpp:127
void merge(SimilarityMatrix &toMerge)
Merge two matrices.
size_t nElements() const noexcept
Number of elements in the matrix.
Definition similarityMatrix.hpp:139
void insert(const RowColIdx &rowColPair, const JaccardPair &jaccardCounts)
Insert a value (updating the index)
SimilarityMatrix() noexcept=default
Default constructor.
void save(const std::string &outFileName, const size_t &nThreads, const std::string &locusNameFile="") const
Save to file.
void chunkedAppend(std::vector< uint64_t > &source, std::vector< uint64_t > &target)
Append one vector to another by chunks.
RowColIdx recoverRCindexes(const uint64_t &vecIdx) noexcept
Recover row and column indexes.
Full vectorized index and similarity value.
Definition similarityMatrix.hpp:54
uint8_t quantSimilarity
Quantized similarity value.
Definition similarityMatrix.hpp:58
uint64_t fullIdx
Full index of a vectorized triangular matrix.
Definition similarityMatrix.hpp:56
Pair of integers to calculate Jaccard similarity.
Definition similarityMatrix.hpp:61
uint64_t nUnion
Union size.
Definition similarityMatrix.hpp:65
uint64_t nIntersect
Intersection size.
Definition similarityMatrix.hpp:63
Row and column index pair.
Definition similarityMatrix.hpp:47
uint32_t jCol
Column index.
Definition similarityMatrix.hpp:51
uint32_t iRow
Row index.
Definition similarityMatrix.hpp:49