Package Bio :: Package PDB :: Module MMCIFParser
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.MMCIFParser

  1  # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk) 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  """mmCIF parser (partly implemented in C).""" 
  7   
  8  from string import letters 
  9   
 10  import numpy 
 11   
 12  from Bio.PDB.MMCIF2Dict import MMCIF2Dict 
 13  from Bio.PDB.StructureBuilder import StructureBuilder 
 14   
 15   
16 -class MMCIFParser:
17 - def get_structure(self, structure_id, filename):
18 self._mmcif_dict=MMCIF2Dict(filename) 19 self._structure_builder=StructureBuilder() 20 self._build_structure(structure_id) 21 return self._structure_builder.get_structure()
22
23 - def _build_structure(self, structure_id):
24 mmcif_dict=self._mmcif_dict 25 atom_id_list=mmcif_dict["_atom_site.label_atom_id"] 26 residue_id_list=mmcif_dict["_atom_site.label_comp_id"] 27 seq_id_list=mmcif_dict["_atom_site.label_seq_id"] 28 chain_id_list=mmcif_dict["_atom_site.label_asym_id"] 29 x_list=map(float, mmcif_dict["_atom_site.Cartn_x"]) 30 y_list=map(float, mmcif_dict["_atom_site.Cartn_y"]) 31 z_list=map(float, mmcif_dict["_atom_site.Cartn_z"]) 32 alt_list=mmcif_dict["_atom_site.label_alt_id"] 33 b_factor_list=mmcif_dict["_atom_site.B_iso_or_equiv"] 34 occupancy_list=mmcif_dict["_atom_site.occupancy"] 35 fieldname_list=mmcif_dict["_atom_site.group_PDB"] 36 try: 37 aniso_u11=mmcif_dict["_atom_site.aniso_U[1][1]"] 38 aniso_u12=mmcif_dict["_atom_site.aniso_U[1][2]"] 39 aniso_u13=mmcif_dict["_atom_site.aniso_U[1][3]"] 40 aniso_u22=mmcif_dict["_atom_site.aniso_U[2][2]"] 41 aniso_u23=mmcif_dict["_atom_site.aniso_U[2][3]"] 42 aniso_u33=mmcif_dict["_atom_site.aniso_U[3][3]"] 43 aniso_flag=1 44 except KeyError: 45 # no anisotropic B factors 46 aniso_flag=0 47 # if auth_seq_id is present, we use this. 48 # Otherwise label_seq_id is used. 49 if "_atom_site.auth_seq_id" in mmcif_dict: 50 seq_id_list=mmcif_dict["_atom_site.auth_seq_id"] 51 else: 52 seq_id_list=mmcif_dict["_atom_site.label_seq_id"] 53 # Now loop over atoms and build the structure 54 current_chain_id=None 55 current_residue_id=None 56 current_model_id=0 57 structure_builder=self._structure_builder 58 structure_builder.init_structure(structure_id) 59 structure_builder.init_model(current_model_id) 60 structure_builder.init_seg(" ") 61 for i in xrange(0, len(atom_id_list)): 62 x=x_list[i] 63 y=y_list[i] 64 z=z_list[i] 65 resname=residue_id_list[i] 66 chainid=chain_id_list[i] 67 altloc=alt_list[i] 68 if altloc==".": 69 altloc=" " 70 resseq=seq_id_list[i] 71 name=atom_id_list[i] 72 tempfactor=b_factor_list[i] 73 occupancy=occupancy_list[i] 74 fieldname=fieldname_list[i] 75 if fieldname=="HETATM": 76 hetatm_flag="H" 77 else: 78 hetatm_flag=" " 79 if current_chain_id!=chainid: 80 current_chain_id=chainid 81 structure_builder.init_chain(current_chain_id) 82 current_residue_id=resseq 83 icode, int_resseq=self._get_icode(resseq) 84 structure_builder.init_residue(resname, hetatm_flag, int_resseq, 85 icode) 86 elif current_residue_id!=resseq: 87 current_residue_id=resseq 88 icode, int_resseq=self._get_icode(resseq) 89 structure_builder.init_residue(resname, hetatm_flag, int_resseq, 90 icode) 91 coord=numpy.array((x, y, z), 'f') 92 structure_builder.init_atom(name, coord, tempfactor, occupancy, altloc, 93 name) 94 if aniso_flag==1: 95 u=(aniso_u11[i], aniso_u12[i], aniso_u13[i], 96 aniso_u22[i], aniso_u23[i], aniso_u33[i]) 97 mapped_anisou=map(float, u) 98 anisou_array=numpy.array(mapped_anisou, 'f') 99 structure_builder.set_anisou(anisou_array) 100 # Now try to set the cell 101 try: 102 a=float(mmcif_dict["_cell.length_a"]) 103 b=float(mmcif_dict["_cell.length_b"]) 104 c=float(mmcif_dict["_cell.length_c"]) 105 alpha=float(mmcif_dict["_cell.angle_alpha"]) 106 beta=float(mmcif_dict["_cell.angle_beta"]) 107 gamma=float(mmcif_dict["_cell.angle_gamma"]) 108 cell=numpy.array((a, b, c, alpha, beta, gamma), 'f') 109 spacegroup=mmcif_dict["_symmetry.space_group_name_H-M"] 110 spacegroup=spacegroup[1:-1] # get rid of quotes!! 111 if spacegroup==None: 112 raise Exception 113 structure_builder.set_symmetry(spacegroup, cell) 114 except: 115 pass # no cell found, so just ignore
116
117 - def _get_icode(self, resseq):
118 """Tries to return the icode. In MMCIF files this is just part of 119 resseq! In PDB files, it's a separate field.""" 120 last_resseq_char=resseq[-1] 121 if last_resseq_char in letters: 122 icode=last_resseq_char 123 int_resseq=int(resseq[0:-1]) 124 else: 125 icode=" " 126 int_resseq=int(resseq) 127 return icode, int_resseq
128 129 130 if __name__=="__main__": 131 import sys 132 133 filename=sys.argv[1] 134 135 p=MMCIFParser() 136 137 structure=p.get_structure("test", filename) 138 139 for model in structure.get_list(): 140 print model 141 for chain in model.get_list(): 142 print chain 143 print "Found %d residues." % len(chain.get_list()) 144