Package Bio :: Package FSSP
[hide private]
[frames] | no frames]

Source Code for Package Bio.FSSP

  1  # This code is part of the Biopython distribution and governed by its 
  2  # license.  Please see the LICENSE file that should have been included 
  3  # as part of this package. 
  4  # 
  5   
  6  """Parser for FSSP files, used in a database of protein fold classifications. 
  7   
  8  This is a module to handle FSSP files. For now it parses only the header, 
  9  summary and alignment sections. 
 10   
 11  See: Holm and Sander (1996) The FSSP database: fold classification based on 
 12  structure-structure alignment of proteins. 
 13   
 14  functions: read_fssp(file_handle): reads an fssp file into the records. Returns 
 15  a tuple of two instances. 
 16   
 17  mult_align: returns a Biopython alignment object 
 18  """ 
 19  from __future__ import print_function 
 20   
 21  import re 
 22   
 23  from . import fssp_rec 
 24   
 25   
 26  fff_rec = fssp_rec.fff_rec 
 27  header_records = { 
 28      'database': re.compile('^DATABASE'), 
 29      'pdbid': re.compile('^PDBID'), 
 30      'header': re.compile('^HEADER'), 
 31      'compnd': re.compile('^COMPND'), 
 32      'author': re.compile('^AUTHOR'), 
 33      'source': re.compile('^SOURCE'), 
 34      'seqlength': re.compile('^SEQLENGTH'), 
 35      'nalign': re.compile('^NALIGN') 
 36  } 
 37   
 38  summary_title = re.compile('## +SUMMARY') 
 39  summary_rec = re.compile(' *[0-9]+: +[1-9][0-9a-z]{3,3}') 
 40  alignments_title = re.compile('## +ALIGNMENTS') 
 41  alignments_rec = re.compile(' *[0-9]+ +-{0,1}[0-9]+') 
 42  equiv_title = re.compile('## +EQUIVALENCES') 
 43   
 44   
45 -class FSSPHeader(object):
46 - def __init__(self):
47 self.database = None 48 self.pdbid = '' 49 self.header = '' 50 self.compnd = '' 51 self.source = '' 52 self.author = [] 53 self.seqlength = 0 54 self.nalign = 0
55
56 - def fill_header(self, inline):
57 for i in header_records: 58 if header_records[i].match(inline): 59 if i == 'database' or i == 'seqlength' or i == 'nalign': 60 setattr(self, i, int(inline.split()[1])) 61 elif i == 'compnd' or i == 'author': 62 setattr(self, i, inline.split()[1:]) 63 elif i == 'source' or i == 'header': 64 attr = inline[inline.find(' ') + 1:].strip() 65 setattr(self, i, attr) 66 else: 67 setattr(self, i, inline.split()[1])
68 69
70 -class PosAlign(object):
71 - def __init__(self, inStr):
72 inStr = inStr.strip() 73 if len(inStr) != 1 and len(inStr) != 2: 74 raise ValueError('PosAlign: length not 2 chars' + inStr) 75 if inStr == '..': 76 self.aa = '-' 77 self.gap = 1 78 else: 79 self.gap = 0 80 self.aa = inStr[0] 81 if self.aa == self.aa.lower(): 82 self.aa = 'C' 83 if len(inStr) == 2: 84 self.ss = inStr[1].upper() 85 else: 86 self.ss = '0'
87
88 - def __repr__(self):
89 if self.gap: 90 outstring = '..' 91 else: 92 outstring = self.aa + self.ss.lower() 93 return outstring
94 95 __str__ = __repr__
96 97
98 -class FSSPSumRec(object):
99 """Contains info from an FSSP summary record""" 100
101 - def __init__(self, in_str):
102 self.raw = in_str 103 in_rec = in_str.strip().split() 104 # print(in_rec) 105 self.nr = int(in_rec[0][:-1]) 106 self.pdb1 = in_rec[1][:4] 107 if len(in_rec[1]) == 4: 108 self.chain1 = '0' 109 elif len(in_rec[1]) == 5: 110 self.chain1 = in_rec[1][4] 111 else: 112 raise ValueError('Bad PDB ID 1') 113 self.pdb2 = in_rec[2][:4] 114 if len(in_rec[2]) == 4: 115 self.chain2 = '0' 116 elif len(in_rec[2]) == 5: 117 self.chain2 = in_rec[2][4] 118 else: 119 raise ValueError('Bad PDB ID 2') 120 self.zscore = float(in_rec[3]) 121 self.rmsd = float(in_rec[4]) 122 self.lali = float(in_rec[5]) 123 self.lseq2 = float(in_rec[6]) 124 self.pID = float(in_rec[7]) 125 self.revers = int(in_rec[8]) 126 self.permut = int(in_rec[9]) 127 self.nfrag = int(in_rec[10]) 128 self.topo = in_rec[11] 129 self.doc = '' 130 for i in in_rec[12:]: 131 self.doc = self.doc + i + ' ' 132 self.doc = self.doc.rstrip() + '\n'
133
134 - def __repr__(self):
135 return self.raw
136 __str__ = __repr__
137 138
139 -class FSSPAlignRec(object):
140 - def __init__(self, in_fff_rec):
141 # print(in_fff_rec) 142 self.abs_res_num = int(in_fff_rec[fssp_rec.align.abs_res_num]) 143 self.pdb_res_num = in_fff_rec[fssp_rec.align.pdb_res_num].strip() 144 self.chain_id = in_fff_rec[fssp_rec.align.chain_id] 145 if self.chain_id == ' ': 146 self.chain_id = '0' 147 self.res_name = in_fff_rec[fssp_rec.align.res_name] 148 if self.res_name == self.res_name.lower(): 149 self.res_name = 'C' 150 self.ss1 = in_fff_rec[fssp_rec.align.ss1] 151 self.turn3 = in_fff_rec[fssp_rec.align.turn3] 152 self.turn4 = in_fff_rec[fssp_rec.align.turn4] 153 self.turn5 = in_fff_rec[fssp_rec.align.turn5] 154 self.pos_align_dict = {} 155 self.PosAlignList = []
156
157 - def add_align_list(self, align_list):
158 for i in align_list: 159 self.PosAlignList.append(PosAlign(i))
160
161 - def pos_align_list2dict(self):
162 j = 1 163 for i in self.PosAlignList: 164 self.pos_align_dict[j] = i 165 j = j + 1
166 167
168 -class FSSPAlignDict(dict):
169 - def __init__(self):
170 # The following two dictionaries are pointers to records in self 171 # The first dictionary is a "pdb_residue_number: self_key" 172 # The second dictionary is a "absolute_residue_number: self_key" 173 self.pdb_res_dict = {} 174 self.abs_res_dict = {} 175 self.data = {}
176
177 - def build_resnum_list(self):
178 for i in self: 179 self.abs_res_dict[self[i].abs_res_num] = i 180 self.pdb_res_dict[self[i].pdb_res_num] = i
181 182 # Given an absolute residue number & chain, returns the relevant fssp 183 # record
184 - def abs(self, num):
185 return self[self.abs_res_dict[num]]
186 187 # Given an PDB residue number & chain, returns the relevant fssp 188 # record
189 - def pdb(self, num):
190 return self[self.pdb_res_dict[num]]
191 192 # Returns a sequence string
193 - def sequence(self, num):
194 s = '' 195 for i in sorted(self.abs_res_dict): 196 s += self.abs(i).pos_align_dict[num].aa 197 return s
198
199 - def fasta_mult_align(self):
200 mult_align_dict = {} 201 for j in self.abs(1).pos_align_dict: 202 mult_align_dict[j] = '' 203 for fssp_record in self.values(): 204 for j in fssp_rec.pos_align_dict: 205 mult_align_dict[j] += fssp_record.pos_align_dict[j].aa 206 out_str = '' 207 for i in sorted(mult_align_dict): 208 out_str += '> %d\n' % i 209 k = 0 210 for j in mult_align_dict[i]: 211 k += 1 212 if k % 72 == 0: 213 out_str += '\n' 214 out_str += j 215 out_str += '\n' 216 return out_str
217 218
219 -class FSSPSumDict(dict):
220 pass
221 222 223 # 224 # Process a fssp file into its constituents. Return a 2-tuple containing 225 # a list of FSSPSumRecs and a dictionary of alignment records. 226 #
227 -def read_fssp(fssp_handle):
228 header = FSSPHeader() 229 sum_dict = FSSPSumDict() 230 align_dict = FSSPAlignDict() 231 curline = fssp_handle.readline() 232 while not summary_title.match(curline): 233 # Still in title 234 header.fill_header(curline) 235 curline = fssp_handle.readline() 236 237 if not summary_title.match(curline): 238 raise ValueError('Bad FSSP file: no summary record found') 239 curline = fssp_handle.readline() # Read the title line, discard 240 curline = fssp_handle.readline() # Read the next line 241 # Process the summary records into a list 242 while summary_rec.match(curline): 243 cur_sum_rec = FSSPSumRec(curline) 244 sum_dict[cur_sum_rec.nr] = cur_sum_rec 245 curline = fssp_handle.readline() 246 247 # Outer loop: process everything up to the EQUIVALENCES title record 248 while not equiv_title.match(curline): 249 while (not alignments_title.match(curline) and 250 not equiv_title.match(curline)): 251 curline = fssp_handle.readline() 252 if not alignments_title.match(curline): 253 if equiv_title.match(curline): 254 # print("Reached equiv_title") 255 break 256 else: 257 raise ValueError('Bad FSSP file: no alignments title record found') 258 259 if equiv_title.match(curline): 260 break 261 # If we got to this point, this means that we have matched an 262 # alignments title. Parse the alignment records in a loop. 263 curline = fssp_handle.readline() # Read the title line, discard 264 curline = fssp_handle.readline() # Read the next line 265 while alignments_rec.match(curline): 266 align_rec = FSSPAlignRec(fff_rec(curline)) 267 key = align_rec.chain_id + align_rec.res_name + str(align_rec.pdb_res_num) 268 align_list = curline[fssp_rec.align.start_aa_list:].strip().split() 269 if key not in align_dict: 270 align_dict[key] = align_rec 271 align_dict[key].add_align_list(align_list) 272 curline = fssp_handle.readline() 273 if not curline: 274 print('EOFEOFEOF') 275 raise EOFError 276 for i in align_dict.values(): 277 i.pos_align_list2dict() 278 del i.PosAlignList 279 align_dict.build_resnum_list() 280 return (header, sum_dict, align_dict)
281