File: AbstractPropertyMap.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 46,856 kB
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (136 lines) | stat: -rw-r--r-- 4,105 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
# Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk)
# 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.

"""Class that maps (chain_id, residue_id) to a residue property."""

from __future__ import print_function


class AbstractPropertyMap(object):
    def __init__(self, property_dict, property_keys, property_list):
        self.property_dict = property_dict
        self.property_keys = property_keys
        self.property_list = property_list

    def _translate_id(self, entity_id):
        return entity_id

    def __contains__(self, id):
        """True if the mapping has a property for this residue.

        Example:
            >>> if (chain_id, res_id) in apmap:
            ...     res, prop = apmap[(chain_id, res_id)]

        @param chain_id: chain id
        @type chain_id: char

        @param res_id: residue id
        @type res_id: char
        """
        translated_id = self._translate_id(id)
        return (translated_id in self.property_dict)

    def __getitem__(self, key):
        """
        Return property for a residue.

        @param chain_id: chain id
        @type chain_id: char

        @param res_id: residue id
        @type res_id: int or (char, int, char)

        @return: some residue property
        @rtype: anything (can be a tuple)
        """
        translated_id = self._translate_id(key)
        return self.property_dict[translated_id]

    def __len__(self):
        """
        Return number of residues for which the property is available.

        @return: number of residues
        @rtype: int
        """
        return len(self.property_dict)

    def has_key(self, id):
        """True if the mapping has a property for this residue.

        (Obsolete; use "id in mapping" instead.)

        Example:

            >>> if apmap.has_key((chain_id, res_id)):
            ...     res, prop = apmap[(chain_id, res_id)]

        Is equivalent to:

            >>> if (chain_id, res_id) in apmap:
            ...     res, prop = apmap[(chain_id, res_id)]

        @param chain_id: chain id
        @type chain_id: char

        @param res_id: residue id
        @type res_id: char
        """
        import warnings
        from Bio import BiopythonDeprecationWarning
        warnings.warn("This function is deprecated; use 'id in mapping' instead", BiopythonDeprecationWarning)
        return (id in self)

    def keys(self):
        """
        Return the list of residues.

        @return: list of residues for which the property was calculated
        @rtype: [(chain_id, res_id), (chain_id, res_id),...]
        """
        return self.property_keys

    def __iter__(self):
        """
        Iterate over the (entity, property) list. Handy alternative to
        the dictionary-like access.

        Example:
            >>> for (res, property) in iter(map):
            ...     print(res, property)

        @return: iterator
        """
        for i in range(0, len(self.property_list)):
            yield self.property_list[i]


class AbstractResiduePropertyMap(AbstractPropertyMap):
    def __init__(self, property_dict, property_keys, property_list):
        AbstractPropertyMap.__init__(self, property_dict, property_keys,
                property_list)

    def _translate_id(self, ent_id):
        chain_id, res_id = ent_id
        if isinstance(res_id, int):
            ent_id = (chain_id, (' ', res_id, ' '))
        return ent_id


class AbstractAtomPropertyMap(AbstractPropertyMap):
    def __init__(self, property_dict, property_keys, property_list):
        AbstractPropertyMap.__init__(self, property_dict, property_keys,
                property_list)

    def _translate_id(self, ent_id):
        if len(ent_id) == 4:
            chain_id, res_id, atom_name, icode = ent_id
        else:
            chain_id, res_id, atom_name = ent_id
            icode = None
        if isinstance(res_id, int):
            ent_id = (chain_id, (' ', res_id, ' '), atom_name, icode)
        return ent_id