|
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._bwtf. |
|
20
|
|
|
|
|
21
|
|
|
Burrows-Wheeler transform 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
|
|
|
|
|
34
|
1 |
|
__all__ = ['BWTF'] |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
1 |
|
class BWTF(_Fingerprint): |
|
38
|
|
|
"""Burrows-Wheeler transform fingerprint. |
|
39
|
|
|
|
|
40
|
|
|
This is a wrapper of the BWT class in abydos.compression, which provides |
|
41
|
|
|
the same interface as other descendants of _Fingerprint. |
|
42
|
|
|
|
|
43
|
|
|
.. versionadded:: 0.4.1 |
|
44
|
|
|
""" |
|
45
|
|
|
|
|
46
|
1 |
|
def __init__(self, terminator='\0'): |
|
47
|
|
|
"""Initialize BWTF instance. |
|
48
|
|
|
|
|
49
|
|
|
Parameters |
|
50
|
|
|
---------- |
|
51
|
|
|
terminator : str |
|
52
|
|
|
A character added to signal the end of the string |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
.. versionadded:: 0.4.1 |
|
56
|
|
|
|
|
57
|
|
|
""" |
|
58
|
1 |
|
self._bwt = _BWT(terminator) |
|
59
|
|
|
|
|
60
|
1 |
|
def fingerprint(self, word): |
|
61
|
|
|
r"""Return the Burrows-Wheeler transform of a word. |
|
62
|
|
|
|
|
63
|
|
|
Parameters |
|
64
|
|
|
---------- |
|
65
|
|
|
word : str |
|
66
|
|
|
The word to fingerprint |
|
67
|
|
|
|
|
68
|
|
|
Returns |
|
69
|
|
|
------- |
|
70
|
|
|
str |
|
71
|
|
|
The Burrows-Wheeler transform of a word |
|
72
|
|
|
|
|
73
|
|
|
Examples |
|
74
|
|
|
-------- |
|
75
|
|
|
>>> fp = BWTF() |
|
76
|
|
|
>>> fp.fingerprint('hat') |
|
77
|
|
|
'th\x00a' |
|
78
|
|
|
>>> fp.fingerprint('niall') |
|
79
|
|
|
'linla\x00' |
|
80
|
|
|
>>> fp.fingerprint('colin') |
|
81
|
|
|
'n\x00loic' |
|
82
|
|
|
>>> fp.fingerprint('atcg') |
|
83
|
|
|
'g\x00tca' |
|
84
|
|
|
>>> fp.fingerprint('entreatment') |
|
85
|
|
|
'term\x00teetnan' |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
.. versionadded:: 0.4.1 |
|
89
|
|
|
|
|
90
|
|
|
""" |
|
91
|
1 |
|
return self._bwt.encode(word) |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
if __name__ == '__main__': |
|
95
|
|
|
import doctest |
|
96
|
|
|
|
|
97
|
|
|
doctest.testmod() |
|
98
|
|
|
|