a b/third_party/nucleus/util/math.cc
1
/*
2
 * Copyright 2018 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
33
#include <algorithm>
34
#include <cmath>
35
36
#include "absl/log/check.h"
37
#include "third_party/nucleus/util/math.h"
38
39
namespace nucleus {
40
41
double PhredToPError(const int phred) {
42
  CHECK_GE(phred, 0);
43
  return std::pow(10.0, -static_cast<double>(phred) / 10.0);
44
}
45
46
double PhredToLog10PError(const int phred) {
47
  CHECK_GE(phred, 0);
48
  return -static_cast<double>(phred) / 10;
49
}
50
51
double PErrorToPhred(const double perror) {
52
  CHECK_GT(perror, 0);
53
  CHECK_LE(perror, 1);
54
  return Log10PErrorToPhred(PErrorToLog10PError(perror));
55
}
56
57
int PErrorToRoundedPhred(const double perror) {
58
  CHECK_GT(perror, 0);
59
  CHECK_LE(perror, 1);
60
  return Log10PErrorToRoundedPhred(PErrorToLog10PError(perror));
61
}
62
63
double PErrorToLog10PError(const double perror) {
64
  CHECK_GT(perror, 0);
65
  CHECK_LE(perror, 1);
66
  return std::log10(perror);
67
}
68
69
double Log10PErrorToPhred(const double log10_perror) {
70
  CHECK_LE(log10_perror, 0);
71
  return -10 * log10_perror;
72
}
73
74
int Log10PErrorToRoundedPhred(const double log10_perror) {
75
  return std::abs(std::round(Log10PErrorToPhred(log10_perror)));
76
}
77
78
double Log10PTrueToPhred(const double log10_ptrue,
79
                         const double value_if_not_finite) {
80
  const double ptrue = Log10ToReal(log10_ptrue);
81
  const double perror = std::log10(1 - ptrue);
82
  return std::isfinite(perror) ? -10 * perror : value_if_not_finite;
83
}
84
85
double Log10ToReal(const double log10_probability) {
86
  CHECK_LE(log10_probability, 0.0);
87
  return std::pow(10, log10_probability);
88
}
89
90
std::vector<double> ZeroShiftLikelihoods(
91
    const std::vector<double>& likelihoods) {
92
  std::vector<double> normalized(likelihoods.size());
93
  double max = *std::max_element(likelihoods.cbegin(), likelihoods.cend());
94
  std::transform(likelihoods.cbegin(), likelihoods.cend(),
95
                 normalized.begin(),
96
                 [max](double x) { return x - max; });
97
  return normalized;
98
}
99
100
}  // namespace nucleus