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