Home | Trees | Indices | Help |
|
---|
|
1 """ 2 NMR related objects. 3 """ 4 5 import os 6 import csb.io.tsv 7 import csb.core as pu 8 9 from csb.bio.sequence import SequenceAlphabets 14 1720 """ 21 Utility class containing all necessary data and methods for computing 22 secondary chemical shifts. 23 24 @note: You are supposed to obtain an instance of this object only via 25 the dedicated factory (see L{RandomCoil.get}). The factory 26 ensures a "singleton with lazy instantiation" behavior. This is 27 needed since this object loads static data from the file system. 28 """ 29 30 RESOURCES = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources') 31 32 _instance = None 33 34 35 @staticmethod16837 """ 38 Get the current L{RandomCoil} instance (and create it, if this 39 method is called for the first time). 40 """ 41 if RandomCoil._instance is None: 42 RandomCoil._instance = RandomCoil() 43 44 return RandomCoil._instance4547 48 if RandomCoil._instance is not None: 49 raise NotImplementedError("Can't instantiate a singleton") 50 51 RandomCoil._instance = self 52 53 self._offsets = (-2, -1, 1, 2) 54 self._reference = {} 55 self._corrections = {} 56 57 self._initialize()5860 61 ref = os.path.join(RandomCoil.RESOURCES, 'RandomCoil.Reference.tsv') 62 cor = os.path.join(RandomCoil.RESOURCES, 'RandomCoil.Corrections.tsv') 63 64 self._load(ref, cor)6567 68 self._reference = {} 69 self._corrections = {} 70 71 header = 'Residue:str Nucleus:str Value:float' 72 73 for row in csb.io.tsv.Table.from_tsv(ref, header): 74 residue = pu.Enum.parsename(SequenceAlphabets.Protein, row[0]) 75 nucleus, value = row[1:] 76 77 if residue not in self._reference: 78 self._reference[residue] = {} 79 80 self._reference[residue][nucleus] = value 81 82 header = 'Residue:str Nucleus:str CS1:float CS2:float CS3:float CS4:float' 83 84 for row in csb.io.tsv.Table.from_tsv(cor, header): 85 residue = pu.Enum.parsename(SequenceAlphabets.Protein, row[0]) 86 nucleus = row[1] 87 values = row[2:] 88 89 if residue not in self._corrections: 90 self._corrections[residue] = {} 91 92 self._corrections[residue][nucleus] = dict(zip(self._offsets, values))9395 """ 96 Compute a secondary shift given a raw shift C{value}. 97 Residue neighborhood is not taken into account. 98 99 @param residue: residue type (amino acid code) 100 @type residue: str or L{EnumItem} 101 @param nucleus: atom name (PDB format) 102 @type nucleus: str 103 @param value: raw chemical shift value 104 @type value: float 105 106 @return: float 107 108 @raise EntityNotSupportedError: on unsupported residue or nucleus 109 """ 110 111 try: 112 if isinstance(residue, pu.string): 113 if len(residue) == 1: 114 residue = pu.Enum.parse(SequenceAlphabets.Protein, residue) 115 else: 116 residue = pu.Enum.parsename(SequenceAlphabets.Protein, residue) 117 else: 118 if residue.enum is not SequenceAlphabets.Protein: 119 raise TypeError(residue) 120 121 return value - self._reference[residue][nucleus] 122 123 except (pu.EnumValueError, pu.EnumMemberError): 124 raise InvalidResidueError('{0} is not a protein residue'.format(residue)) 125 126 except KeyError as ke: 127 raise EntityNotSupportedError('{0!s}, context: {1!r} {2}'.format(ke, residue, nucleus))128130 """ 131 Compute a secondary shift given a raw shift C{value} for a specific 132 residue and its neighboring residues. 133 134 @param chain: the protein chain containing the C{nucleus} 135 @type chain: L{Chain} 136 @param residue: the residue containing the C{nucleus}. This can be 137 a residue object, id (sequence number + insertion 138 code, string) or rank (integer, 1-based) 139 @type residue: L{Residue}, str or int 140 @param nucleus: atom name (PDB format) 141 @type nucleus: str 142 @param value: raw chemical shift value 143 @type value: float 144 """ 145 try: 146 if isinstance(residue, int): 147 residue = chain.residues[residue] 148 elif isinstance(residue, pu.string): 149 residue = chain.find(residue) 150 else: 151 residue = chain.residues[residue.rank] 152 except (pu.ItemNotFoundError, pu.CollectionIndexError): 153 raise InvalidResidueError("Can't find residue {0} in {1}".format(residue, chain)) 154 155 shift = self.simple_secondary_shift(residue.type, nucleus, value) 156 157 for offset in self._offsets: 158 159 if 1 <= (residue.rank + offset) <= chain.length: 160 try: 161 neighbor = chain.residues[residue.rank + offset] 162 shift -= self._corrections[neighbor.type][nucleus][offset * -1] 163 164 except KeyError: 165 continue 166 167 return shift
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Fri Jul 5 14:24:52 2013 | http://epydoc.sourceforge.net |