File: statistics.py

package info (click to toggle)
python-pbcore 2.1.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,476 kB
  • sloc: python: 13,393; xml: 2,504; makefile: 232; sh: 66
file content (40 lines) | stat: -rw-r--r-- 1,029 bytes parent folder | download | duplicates (3)
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
import math

import numpy as np


class Constants:
    MAX_QV = 60
    # (identical) cutoffs for RQ and QV for "High-Fidelity" reads
    HIFI_QV = 20
    HIFI_RQ = 0.99


def accuracy_as_phred_qv(accuracy, max_qv=Constants.MAX_QV):
    """
    Convert fractional accuracy to Phred QV: 0.999 --> 30

    returns: float or numpy array
    """
    if isinstance(accuracy, (float, int)):
        assert 0 <= accuracy <= 1.0
        if accuracy == 1:
            return max_qv
        return -10 * math.log10(1 - accuracy)
    else:
        if isinstance(accuracy, (tuple, list)):
            accuracy = np.array(accuracy)
        error_rate = 1.0 - accuracy
        min_error_rate = 10 ** (-max_qv / 10.0)
        zero_error = error_rate < min_error_rate
        error_rate[zero_error] = min_error_rate
        return -10 * np.log10(error_rate)


def phred_qv_as_accuracy(qv):
    if qv == 0:
        return 0
    elif qv < 0:
        raise ValueError("Negative Phred scores not allowed")
    else:
        return 1.0 - 10**(-qv/10)