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