Completed
Push — master ( f43547...71985b )
by Chris
12:00 queued 10s
created

abydos.distance._gotoh.Gotoh.dist_abs()   B

Complexity

Conditions 5

Size

Total Lines 72
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 30
nop 6
dl 0
loc 72
ccs 25
cts 25
cp 1
crap 5
rs 8.6933
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
3
# Copyright 2014-2018 by Christopher C. Little.
4
# This file is part of Abydos.
5
#
6
# Abydos is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Abydos is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Abydos. If not, see <http://www.gnu.org/licenses/>.
18
19 1
"""abydos.distance._gotoh.
20
21
Gotoh score
22
"""
23
24 1
from __future__ import (
25
    absolute_import,
26
    division,
27
    print_function,
28
    unicode_literals,
29
)
30
31 1
from numpy import float32 as np_float32
32 1
from numpy import zeros as np_zeros
33
34 1
from six.moves import range
35
36 1
from ._ident import sim_ident
37 1
from ._needleman_wunsch import NeedlemanWunsch
38
39 1
__all__ = ['Gotoh', 'gotoh']
40
41
42 1
class Gotoh(NeedlemanWunsch):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
43
    """Gotoh score.
44
45
    The Gotoh score :cite:`Gotoh:1982` is essentially Needleman-Wunsch with
46
    affine gap penalties.
47
    """
48
49 1
    def dist_abs(self, src, tar, gap_open=1, gap_ext=0.4, sim_func=sim_ident):
0 ignored issues
show
best-practice introduced by
Too many arguments (6/5)
Loading history...
Bug introduced by
Parameters differ from overridden 'dist_abs' method
Loading history...
50
        """Return the Gotoh score of two strings.
51
52
        Parameters
53
        ----------
54
        src : str
55
            Source string for comparison
56
        tar : str
57
            Target string for comparison
58
        gap_open : float
59
            The cost of an open alignment gap (1 by default)
60
        gap_ext : float
61
            The cost of an alignment gap extension (0.4 by default)
62
        sim_func : function
63
            A function that returns the similarity of two characters (identity
64
            similarity by default)
65
66
        Returns
67
        -------
68
        float
69
            Gotoh score
70
71
        Examples
72
        --------
73
        >>> cmp = Gotoh()
74
        >>> cmp.dist_abs('cat', 'hat')
75
        2.0
76
        >>> cmp.dist_abs('Niall', 'Neil')
77
        1.0
78
        >>> round(cmp.dist_abs('aluminum', 'Catalan'), 12)
79
        -0.4
80
        >>> cmp.dist_abs('cat', 'hat')
81
        2.0
82
83
        """
84 1
        d_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32)
85 1
        p_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32)
86 1
        q_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32)
87
88 1
        d_mat[0, 0] = 0
89 1
        p_mat[0, 0] = float('-inf')
90 1
        q_mat[0, 0] = float('-inf')
91 1
        for i in range(1, len(src) + 1):
92 1
            d_mat[i, 0] = float('-inf')
93 1
            p_mat[i, 0] = -gap_open - gap_ext * (i - 1)
94 1
            q_mat[i, 0] = float('-inf')
95 1
            q_mat[i, 1] = -gap_open
96 1
        for j in range(1, len(tar) + 1):
97 1
            d_mat[0, j] = float('-inf')
98 1
            p_mat[0, j] = float('-inf')
99 1
            p_mat[1, j] = -gap_open
100 1
            q_mat[0, j] = -gap_open - gap_ext * (j - 1)
101
102 1
        for i in range(1, len(src) + 1):
103 1
            for j in range(1, len(tar) + 1):
104 1
                sim_val = sim_func(src[i - 1], tar[j - 1])
105 1
                d_mat[i, j] = max(
106
                    d_mat[i - 1, j - 1] + sim_val,
107
                    p_mat[i - 1, j - 1] + sim_val,
108
                    q_mat[i - 1, j - 1] + sim_val,
109
                )
110
111 1
                p_mat[i, j] = max(
112
                    d_mat[i - 1, j] - gap_open, p_mat[i - 1, j] - gap_ext
113
                )
114
115 1
                q_mat[i, j] = max(
116
                    d_mat[i, j - 1] - gap_open, q_mat[i, j - 1] - gap_ext
117
                )
118
119 1
        i, j = (n - 1 for n in d_mat.shape)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable n does not seem to be defined.
Loading history...
120 1
        return max(d_mat[i, j], p_mat[i, j], q_mat[i, j])
121
122
123 1
def gotoh(src, tar, gap_open=1, gap_ext=0.4, sim_func=sim_ident):
124
    """Return the Gotoh score of two strings.
125
126
    This is a wrapper for :py:meth:`Gotoh.dist_abs`.
127
128
    Parameters
129
    ----------
130
    src : str
131
        Source string for comparison
132
    tar : str
133
        Target string for comparison
134
    gap_open : float
135
        The cost of an open alignment gap (1 by default)
136
    gap_ext : float
137
        The cost of an alignment gap extension (0.4 by default)
138
    sim_func : function
139
        A function that returns the similarity of two characters (identity
140
        similarity by default)
141
142
    Returns
143
    -------
144
    float
145
        Gotoh score
146
147
    Examples
148
    --------
149
    >>> gotoh('cat', 'hat')
150
    2.0
151
    >>> gotoh('Niall', 'Neil')
152
    1.0
153
    >>> round(gotoh('aluminum', 'Catalan'), 12)
154
    -0.4
155
    >>> gotoh('cat', 'hat')
156
    2.0
157
158
    """
159 1
    return Gotoh().dist_abs(src, tar, gap_open, gap_ext, sim_func)
160
161
162
if __name__ == '__main__':
163
    import doctest
164
165
    doctest.testmod()
166