1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2019 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.fingerprint._bwtrlef. |
20
|
|
|
|
21
|
|
|
Burrows-Wheeler transform plus run-length encoding fingerprint |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from ._fingerprint import _Fingerprint |
32
|
1 |
|
from ..compression import BWT as _BWT |
33
|
1 |
|
from ..compression import RLE as _RLE |
34
|
|
|
|
35
|
1 |
|
__all__ = ['BWTRLEF'] |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
class BWTRLEF(_Fingerprint): |
39
|
|
|
"""Burrows-Wheeler transform plus run-length encoding fingerprint. |
40
|
|
|
|
41
|
|
|
This is a wrapper of the BWT and RLE classes in abydos.compression, which |
42
|
|
|
provides the same interface as other descendants of _Fingerprint. |
43
|
|
|
|
44
|
|
|
.. versionadded:: 0.4.1 |
45
|
|
|
""" |
46
|
|
|
|
47
|
1 |
|
def __init__(self, terminator='\0'): |
48
|
|
|
"""Initialize BWTRLEF instance. |
49
|
|
|
|
50
|
|
|
Parameters |
51
|
|
|
---------- |
52
|
|
|
terminator : str |
53
|
|
|
A character added to signal the end of the string |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
.. versionadded:: 0.4.1 |
57
|
|
|
|
58
|
|
|
""" |
59
|
1 |
|
self._bwt = _BWT(terminator) |
60
|
1 |
|
self._rle = _RLE() |
61
|
|
|
|
62
|
1 |
|
def fingerprint(self, word): |
63
|
|
|
r"""Return the run-length encoded Burrows-Wheeler transform of a word. |
64
|
|
|
|
65
|
|
|
Parameters |
66
|
|
|
---------- |
67
|
|
|
word : str |
68
|
|
|
The word to fingerprint |
69
|
|
|
|
70
|
|
|
Returns |
71
|
|
|
------- |
72
|
|
|
str |
73
|
|
|
The run-length encoded Burrows-Wheeler transform of a word |
74
|
|
|
|
75
|
|
|
Examples |
76
|
|
|
-------- |
77
|
|
|
>>> fp = BWTRLEF() |
78
|
|
|
>>> fp.fingerprint('hat') |
79
|
|
|
'th\x00a' |
80
|
|
|
>>> fp.fingerprint('niall') |
81
|
|
|
'linla\x00' |
82
|
|
|
>>> fp.fingerprint('colin') |
83
|
|
|
'n\x00loic' |
84
|
|
|
>>> fp.fingerprint('atcg') |
85
|
|
|
'g\x00tca' |
86
|
|
|
>>> fp.fingerprint('entreatment') |
87
|
|
|
'term\x00teetnan' |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
.. versionadded:: 0.4.1 |
91
|
|
|
|
92
|
|
|
""" |
93
|
1 |
|
return self._rle.encode(self._bwt.encode(word)) |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
if __name__ == '__main__': |
97
|
|
|
import doctest |
98
|
|
|
|
99
|
|
|
doctest.testmod() |
100
|
|
|
|