class XBGFParser: def __init__(self, PERMISSIVE=1, structure_builder=None): if structure_builder != None: self.structure_builder = structure_builder else: self.structure_builder = StructureBuilder() self.PERMISSIVE = PERMISSIVE # public interface def parse(self, id, file): self.structure_builder.init_structure(id) if isinstance(file, basestring): file=open(file) self.charges = dict() self.chain_suffix = 0 self._parse(file.readlines()) self.structure = self.structure_builder.get_structure() return self._process_structure() # private methods def _parse(self, lines): self.structure_builder.init_model(0) self.structure_builder.init_seg("") self.current_chain_id = None self.current_residue_id = None self.current_resname = None for i in range(0, len(lines)): self.line_counter = i + 1 self.structure_builder.set_line_counter(self.line_counter) line = lines[i] if line[0:6] == 'ATOM ': self._update_atom(line) def _update_chain(self, line): chain_id = self._extract_chain(line) if self.current_chain_id != chain_id: try: self.structure_builder.init_chain(chain_id) self.current_chain_id = chain_id self.current_residue_id = None self.current_resname = None except PDBConstructionException, message: self._handle_PDB_exception(message)
class XBGFParser: def __init__(self, PERMISSIVE=1, structure_builder=None): if structure_builder != None: self.structure_builder = structure_builder else: self.structure_builder = StructureBuilder() self.PERMISSIVE = PERMISSIVE # public interface def parse(self, id, file): self.structure_builder.init_structure(id) if isinstance(file, basestring): file = open(file) self.charges = dict() self.chain_suffix = 0 self._parse(file.readlines()) self.structure = self.structure_builder.get_structure() return self._process_structure() # private methods def _parse(self, lines): self.structure_builder.init_model(0) self.structure_builder.init_seg("") self.current_chain_id = None self.current_residue_id = None self.current_resname = None for i in range(0, len(lines)): self.line_counter = i + 1 self.structure_builder.set_line_counter(self.line_counter) line = lines[i] if line[0:6] == 'ATOM ': self._update_atom(line) def _update_chain(self, line): chain_id = self._extract_chain(line) if self.current_chain_id != chain_id: try: self.structure_builder.init_chain(chain_id) self.current_chain_id = chain_id self.current_residue_id = None self.current_resname = None except PDBConstructionException, message: self._handle_PDB_exception(message)
def pdb_extract(structure, **kwargs): # model to extract from pdb extract_model = None if not 'model' in kwargs else kwargs['model'] new_model_id = -1 if not 'new_model' in kwargs else kwargs['new_model'] extract_chain = None if not 'chain' in kwargs else kwargs['chain'] first_res = None if not 'first_res' in kwargs else kwargs['first_res'] last_res = None if not 'last_res' in kwargs else kwargs['last_res'] new_first_res = None if not 'new_first_res' in kwargs else kwargs[ 'new_first_res'] gap_count = 0 if not 'gap_count' in kwargs else kwargs['gap_count'] water_id = None if not 'water' in kwargs else kwargs['water'] model_rebumber_flag = bool((extract_model is not None) or new_model_id >= 0) res_renumber_flag = bool(first_res or last_res or new_first_res or gap_count) structure_builder = StructureBuilder() structure_builder.init_structure('pdb_extract') structure_builder.set_line_counter(0) line_counter = 0 start_resseq_by_default = 1 if not new_first_res else new_first_res for model in structure: if model_rebumber_flag and \ ( extract_model is not None ) and model.get_id() != extract_model: continue if model_rebumber_flag and new_model_id >= 0: this_model_id = new_model_id new_model_id += 1 else: this_model_id = model.get_id() structure_builder.init_model(this_model_id, this_model_id) for chain in model: if extract_chain and chain.get_id() != extract_chain: continue structure_builder.init_seg(' ') structure_builder.init_chain(chain.get_id()) resdict = {} if res_renumber_flag: # first_res = res_range_tuple[0] # last_res = res_range_tuple[1] resdict['before'] = select_residues_from_chain( chain, first_res=first_res, gap_count=gap_count) resdict['hit'] = select_residues_from_chain( chain, first_res=first_res, last_res=last_res) resdict['after'] = select_residues_from_chain( chain, last_res=last_res, gap_count=gap_count) else: resdict['before'] = [] resdict['hit'] = chain.get_list() resdict['after'] = [] new_resseq = start_resseq_by_default - len(resdict['before']) resdict['water'] = chain_water_id(chain, water_id) for key in ['before', 'hit', 'after', 'water']: for residue in resdict[key]: if res_renumber_flag: new_resid = ' ', new_resseq, ' ' else: new_resid = residue.get_id() structure_builder.init_residue(residue.get_resname(), *new_resid) residue_atoms = None if key == 'before': residue_atoms = [atom for atom in residue if \ (atom.get_name() == 'C' or atom.get_name() == 'O')] elif key == 'hit' or key == 'water': residue_atoms = residue.get_list() elif key == 'after': residue_atoms = [atom for atom in residue if \ (atom.get_name() == 'N' or atom.get_name() == 'HN')] for atom in residue_atoms: structure_builder.init_atom(atom.get_name(), atom.get_coord(), atom.get_bfactor(), atom.get_occupancy(), atom.get_altloc(), atom.get_fullname()) structure_builder.set_line_counter(line_counter) line_counter += 1 new_resseq += 1 if key == 'water' and gap_count and len( resdict['after']) != gap_count: new_resseq += gap_count - len(resdict['after']) out_structure = structure_builder.get_structure() return out_structure