File: __init__.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 46,860 kB
  • ctags: 13,237
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (441 lines) | stat: -rw-r--r-- 14,959 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright 2000-2002 by Andrew Dalke.
# Revisions copyright 2007-2010 by Peter Cock.
# All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Alphabets used in Seq objects etc to declare sequence type and letters.

This is used by sequences which contain a finite number of similar words.
"""


class Alphabet(object):
    """Generic alphabet base class.

    This class is used as a base class for other types of alphabets.

    Attributes:
        - letters - list-like object containing the letters of the alphabet.
               Usually it is a string when letters are single characters.
        - size    - size of the alphabet's letters (e.g. 1 when letters are
               single characters).
    """

    size = None     # default to no fixed size for words
    letters = None  # default to no fixed alphabet
    # In general, a list-like object. However,
    # assuming letters are single characters, use a
    # string. This is expected for use with Seq like
    # objects.

    def __repr__(self):
        return self.__class__.__name__ + "()"

    def contains(self, other):
        """Does this alphabet 'contain' the other (OBSOLETE?).

        Returns a boolean.  This relies on the Alphabet subclassing
        hierarchy only, and does not check the letters property.
        This isn't ideal, and doesn't seem to work as intended
        with the AlphabetEncoder classes."""
        return isinstance(other, self.__class__)

    def _case_less(self):
        """Return a case-less variant of the current alphabet (PRIVATE)."""
        # TODO - remove this method by dealing with things in subclasses?
        if isinstance(self, ProteinAlphabet):
            return generic_protein
        elif isinstance(self, DNAAlphabet):
            return generic_dna
        elif isinstance(self, RNAAlphabet):
            return generic_rna
        elif isinstance(self, NucleotideAlphabet):
            return generic_nucleotide
        elif isinstance(self, SingleLetterAlphabet):
            return single_letter_alphabet
        else:
            return generic_alphabet

    def _upper(self):
        """Return an upper case variant of the current alphabet (PRIVATE)."""
        if not self.letters or self.letters == self.letters.upper():
            # Easy case, no letters or already upper case!
            return self
        else:
            # TODO - Raise NotImplementedError and handle via subclass?
            return self._case_less()

    def _lower(self):
        """Return a lower case variant of the current alphabet (PRIVATE)."""
        if not self.letters or self.letters == self.letters.lower():
            # Easy case, no letters or already lower case!
            return self
        else:
            # TODO - Raise NotImplementedError and handle via subclass?
            return self._case_less()

generic_alphabet = Alphabet()


class SingleLetterAlphabet(Alphabet):
    """Generic alphabet with letters of size one."""
    size = 1
    letters = None   # string of all letters in the alphabet

single_letter_alphabet = SingleLetterAlphabet()

# ########## Protein


class ProteinAlphabet(SingleLetterAlphabet):
    """Generic single letter protein alphabet."""
    pass

generic_protein = ProteinAlphabet()

# ########## DNA


class NucleotideAlphabet(SingleLetterAlphabet):
    """Generic single letter nucleotide alphabet."""
    pass

generic_nucleotide = NucleotideAlphabet()


class DNAAlphabet(NucleotideAlphabet):
    """Generic single letter DNA alphabet."""
    pass

generic_dna = DNAAlphabet()


# ########## RNA


class RNAAlphabet(NucleotideAlphabet):
    """Generic single letter RNA alphabet."""
    pass

generic_rna = RNAAlphabet()

# ########## Other per-sequence encodings


class SecondaryStructure(SingleLetterAlphabet):
    """Alphabet used to describe secondary structure.

    Letters are 'H' (helix), 'S' (strand), 'T' (turn) and 'C' (coil).
    """
    letters = "HSTC"


class ThreeLetterProtein(Alphabet):
    """Three letter protein alphabet."""
    size = 3
    letters = [
        "Ala", "Asx", "Cys", "Asp", "Glu", "Phe", "Gly", "His", "Ile",
        "Lys", "Leu", "Met", "Asn", "Pro", "Gln", "Arg", "Ser", "Thr",
        "Sec", "Val", "Trp", "Xaa", "Tyr", "Glx",
        ]

    def _upper(self):
        raise NotImplementedError("We don't have an uppercase three letter protein alphabet.")

    def _lower(self):
        raise NotImplementedError("We don't have a lowercase three letter protein alphabet.")

# ##### Non per-sequence modifications

# (These are Decorator classes)


class AlphabetEncoder(object):
    def __init__(self, alphabet, new_letters):
        self.alphabet = alphabet
        self.new_letters = new_letters
        if alphabet.letters is not None:
            self.letters = alphabet.letters + new_letters
        else:
            self.letters = None

    def __getattr__(self, key):
        if key[:2] == "__" and key[-2:] == "__":
            raise AttributeError(key)
        return getattr(self.alphabet, key)

    def __repr__(self):
        return "%s(%r, %r)" % (self.__class__.__name__, self.alphabet,
                               self.new_letters)

    def contains(self, other):
        """Does this alphabet 'contain' the other (OBSOLETE?).

        This is isn't implemented for the base AlphabetEncoder,
        which will always return 0 (False)."""
        return 0

    def _upper(self):
        """Return an upper case variant of the current alphabet (PRIVATE)."""
        return AlphabetEncoder(self.alphabet._upper(), self.new_letters.upper())

    def _lower(self):
        """Return a lower case variant of the current alphabet (PRIVATE)."""
        return AlphabetEncoder(self.alphabet._lower(), self.new_letters.lower())


class Gapped(AlphabetEncoder):
    def __init__(self, alphabet, gap_char="-"):
        AlphabetEncoder.__init__(self, alphabet, gap_char)
        self.gap_char = gap_char

    def contains(self, other):
        """Does this alphabet 'contain' the other (OBSOLETE?).

        Returns a boolean.  This relies on the Alphabet subclassing
        hierarchy, and attempts to check the gap character.  This fails
        if the other alphabet does not have a gap character!
        """
        return other.gap_char == self.gap_char and \
               self.alphabet.contains(other.alphabet)

    def _upper(self):
        """Return an upper case variant of the current alphabet (PRIVATE)."""
        return Gapped(self.alphabet._upper(), self.gap_char.upper())

    def _lower(self):
        """Return a lower case variant of the current alphabet (PRIVATE)."""
        return Gapped(self.alphabet._lower(), self.gap_char.lower())


class HasStopCodon(AlphabetEncoder):
    def __init__(self, alphabet, stop_symbol="*"):
        AlphabetEncoder.__init__(self, alphabet, stop_symbol)
        self.stop_symbol = stop_symbol

    def contains(self, other):
        """Does this alphabet 'contain' the other (OBSOLETE?).

        Returns a boolean.  This relies on the Alphabet subclassing
        hierarchy, and attempts to check the stop symbol.  This fails
        if the other alphabet does not have a stop symbol!
        """
        return other.stop_symbol == self.stop_symbol and \
               self.alphabet.contains(other.alphabet)

    def _upper(self):
        """Return an upper case variant of the current alphabet (PRIVATE)."""
        return HasStopCodon(self.alphabet._upper(), self.stop_symbol.upper())

    def _lower(self):
        """Return a lower case variant of the current alphabet (PRIVATE)."""
        return HasStopCodon(self.alphabet._lower(), self.stop_symbol.lower())


def _get_base_alphabet(alphabet):
    """Returns the non-gapped non-stop-codon Alphabet object (PRIVATE)."""
    a = alphabet
    while isinstance(a, AlphabetEncoder):
        a = a.alphabet
    assert isinstance(a, Alphabet), \
           "Invalid alphabet found, %s" % repr(a)
    return a


def _ungap(alphabet):
    """Returns the alphabet without any gap encoder (PRIVATE)."""
    # TODO - Handle via method of the objects?
    if not hasattr(alphabet, "gap_char"):
        return alphabet
    elif isinstance(alphabet, Gapped):
        return alphabet.alphabet
    elif isinstance(alphabet, HasStopCodon):
        return HasStopCodon(_ungap(alphabet.alphabet), stop_symbol=alphabet.stop_symbol)
    elif isinstance(alphabet, AlphabetEncoder):
        return AlphabetEncoder(_ungap(alphabet.alphabet), letters=alphabet.letters)
    else:
        raise NotImplementedError


def _consensus_base_alphabet(alphabets):
    """Returns a common but often generic base alphabet object (PRIVATE).

    This throws away any AlphabetEncoder information, e.g. Gapped alphabets.

    Note that DNA+RNA -> Nucleotide, and Nucleotide+Protein-> generic single
    letter.  These DO NOT raise an exception!"""
    common = None
    for alpha in alphabets:
        a = _get_base_alphabet(alpha)
        if common is None:
            common = a
        elif common == a:
            pass
        elif isinstance(a, common.__class__):
            pass
        elif isinstance(common, a.__class__):
            common = a
        elif isinstance(a, NucleotideAlphabet) \
        and isinstance(common, NucleotideAlphabet):
            # e.g. Give a mix of RNA and DNA alphabets
            common = generic_nucleotide
        elif isinstance(a, SingleLetterAlphabet) \
        and isinstance(common, SingleLetterAlphabet):
            # This is a pretty big mis-match!
            common = single_letter_alphabet
        else:
            # We have a major mis-match... take the easy way out!
            return generic_alphabet
    if common is None:
        # Given NO alphabets!
        return generic_alphabet
    return common


def _consensus_alphabet(alphabets):
    """Returns a common but often generic alphabet object (PRIVATE).

    >>> from Bio.Alphabet import IUPAC
    >>> _consensus_alphabet([IUPAC.extended_protein, IUPAC.protein])
    ExtendedIUPACProtein()
    >>> _consensus_alphabet([generic_protein, IUPAC.protein])
    ProteinAlphabet()

    Note that DNA+RNA -> Nucleotide, and Nucleotide+Protein-> generic single
    letter.  These DO NOT raise an exception!

    >>> _consensus_alphabet([generic_dna, generic_nucleotide])
    NucleotideAlphabet()
    >>> _consensus_alphabet([generic_dna, generic_rna])
    NucleotideAlphabet()
    >>> _consensus_alphabet([generic_dna, generic_protein])
    SingleLetterAlphabet()
    >>> _consensus_alphabet([single_letter_alphabet, generic_protein])
    SingleLetterAlphabet()

    This is aware of Gapped and HasStopCodon and new letters added by
    other AlphabetEncoders.  This WILL raise an exception if more than
    one gap character or stop symbol is present.

    >>> from Bio.Alphabet import IUPAC
    >>> _consensus_alphabet([Gapped(IUPAC.extended_protein), HasStopCodon(IUPAC.protein)])
    HasStopCodon(Gapped(ExtendedIUPACProtein(), '-'), '*')
    >>> _consensus_alphabet([Gapped(IUPAC.protein, "-"), Gapped(IUPAC.protein, "=")])
    Traceback (most recent call last):
        ...
    ValueError: More than one gap character present
    >>> _consensus_alphabet([HasStopCodon(IUPAC.protein, "*"), HasStopCodon(IUPAC.protein, "+")])
    Traceback (most recent call last):
        ...
    ValueError: More than one stop symbol present
    """
    base = _consensus_base_alphabet(alphabets)
    gap = None
    stop = None
    new_letters = ""
    for alpha in alphabets:
        # Gaps...
        if not hasattr(alpha, "gap_char"):
            pass
        elif gap is None:
            gap = alpha.gap_char
        elif gap == alpha.gap_char:
            pass
        else:
            raise ValueError("More than one gap character present")
        # Stops...
        if not hasattr(alpha, "stop_symbol"):
            pass
        elif stop is None:
            stop = alpha.stop_symbol
        elif stop == alpha.stop_symbol:
            pass
        else:
            raise ValueError("More than one stop symbol present")
        # New letters...
        if hasattr(alpha, "new_letters"):
            for letter in alpha.new_letters:
                if letter not in new_letters \
                and letter != gap and letter != stop:
                    new_letters += letter

    alpha = base
    if new_letters:
        alpha = AlphabetEncoder(alpha, new_letters)
    if gap:
        alpha = Gapped(alpha, gap_char=gap)
    if stop:
        alpha = HasStopCodon(alpha, stop_symbol=stop)
    return alpha


def _check_type_compatible(alphabets):
    """Returns True except for DNA+RNA or Nucleotide+Protein (PRIVATE).

    >>> _check_type_compatible([generic_dna, generic_nucleotide])
    True
    >>> _check_type_compatible([generic_dna, generic_rna])
    False
    >>> _check_type_compatible([generic_dna, generic_protein])
    False
    >>> _check_type_compatible([single_letter_alphabet, generic_protein])
    True

    This relies on the Alphabet subclassing hierarchy.  It does not
    check things like gap characters or stop symbols."""
    dna, rna, nucl, protein = False, False, False, False
    for alpha in alphabets:
        a = _get_base_alphabet(alpha)
        if isinstance(a, DNAAlphabet):
            dna = True
            nucl = True
            if rna or protein:
                return False
        elif isinstance(a, RNAAlphabet):
            rna = True
            nucl = True
            if dna or protein:
                return False
        elif isinstance(a, NucleotideAlphabet):
            nucl = True
            if protein:
                return False
        elif isinstance(a, ProteinAlphabet):
            protein = True
            if nucl:
                return False
    return True


def _verify_alphabet(sequence):
    """Check all letters in sequence are in the alphabet (PRIVATE).

    >>> from Bio.Seq import Seq
    >>> from Bio.Alphabet import IUPAC
    >>> my_seq = Seq("MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVF",
    ...              IUPAC.protein)
    >>> _verify_alphabet(my_seq)
    True

    This example has an X, which is not in the IUPAC protein alphabet
    (you should be using the IUPAC extended protein alphabet):

    >>> bad_seq = Seq("MKQHKAMIVALIVICITAVVAALVTRKDLCEVHIRTGQTEVAVFX",
    ...                IUPAC.protein)
    >>> _verify_alphabet(bad_seq)
    False

    This replaces Bio.utils.verify_alphabet() since we are deprecating
    that. Potentially this could be added to the Alphabet object, and
    I would like it to be an option when creating a Seq object... but
    that might slow things down.
    """
    letters = sequence.alphabet.letters
    if not letters:
        raise ValueError("Alphabet does not define letters.")
    for letter in sequence:
        if letter not in letters:
            return False
    return True