File: getRatio.py

package info (click to toggle)
python-deeptools 3.5.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 34,456 kB
  • sloc: python: 14,503; xml: 4,212; sh: 33; makefile: 5
file content (82 lines) | stat: -rw-r--r-- 2,326 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
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
import numpy as np

old_settings = np.seterr(all='ignore')


def compute_ratio(value1, value2, args):
    value1 = value1 + args['pseudocount'][0]
    value2 = value2 + args['pseudocount'][1]

    ratio = float(value1) / value2
    if args['valueType'] == 'log2':
        ratio = np.log2(ratio)

    elif args['valueType'] == 'reciprocal_ratio':
        # the reciprocal ratio of a/b
        # is a/b if a/b > 1 else -1* b/a
        ratio = ratio if ratio >= 1 else -1.0 / ratio

    return ratio


def getRatio(tileCoverage, args):
    r"""
    The mapreduce method calls this function
    for each tile. The parameters (args) are fixed
    in the main method.

    >>> funcArgs= {'valueType': 'ratio', 'scaleFactors': (1,1), 'pseudocount': [1, 1]}
    >>> getRatio([9, 19], funcArgs)
    0.5
    >>> getRatio([0, 0], funcArgs)
    1.0
    >>> getRatio([np.nan, np.nan], funcArgs)
    nan
    >>> getRatio([np.nan, 1.0], funcArgs)
    nan
    >>> funcArgs['valueType'] ='subtract'
    >>> getRatio([20, 10], funcArgs)
    10
    >>> funcArgs['scaleFactors'] = (1, 0.5)
    >>> getRatio([10, 20], funcArgs)
    0.0

    The reciprocal ratio is of a and b is:
    is a/b if a/b > 1 else -1* b/a
    >>> funcArgs['valueType'] ='reciprocal_ratio'
    >>> funcArgs['scaleFactors'] = (1, 1)
    >>> funcArgs['pseudocount'] = [0, 0]
    >>> getRatio([2, 1], funcArgs)
    2.0
    >>> getRatio([1, 2], funcArgs)
    -2.0
    >>> getRatio([1, 1], funcArgs)
    1.0
    """

    value1 = args['scaleFactors'][0] * tileCoverage[0]
    value2 = args['scaleFactors'][1] * tileCoverage[1]

    # if any of the two values to compare
    # is nan, return nan
    if np.isnan(value1) or np.isnan(value2):
        return np.nan

    # ratio case
    if args['valueType'] in ['ratio', 'log2', 'reciprocal_ratio']:
        bin_value = compute_ratio(value1, value2, args)

    # non ratio case (diff, sum etc)
    else:
        if args['valueType'] == 'subtract':
            bin_value = value1 - value2
        elif args['valueType'] == 'add':
            bin_value = value1 + value2
        elif args['valueType'] == 'first':
            bin_value = value1
        elif args['valueType'] == 'second':
            bin_value = value2
        elif args['valueType'] == 'mean':
            bin_value = (value1 + value2) / 2.0

    return bin_value