def build_structure(atoms): sb = StructureBuilder() sb.init_structure('pdb') sb.init_seg(' ') sb.init_model(0) sb.init_chain('A') i = 1 for atom in atoms: sb.init_residue('DUM', ' ', i, ' ') sb.structure[0]['A'].child_list[i - 1].add(atom) i += 1 return sb.structure
def set_structure(self, pdb_object): """Check what the user is providing and build a structure.""" # The idea here is to build missing upstream components of # the SMCRA object representation. E.g., if the user provides # a Residue, build Structure/Model/Chain. if pdb_object.level == "S": structure = pdb_object else: # Not a Structure sb = StructureBuilder() sb.init_structure("pdb") sb.init_seg(" ") if pdb_object.level == "M": sb.structure.add(pdb_object.copy()) self.structure = sb.structure else: # Not a Model sb.init_model(0) if pdb_object.level == "C": sb.structure[0].add(pdb_object.copy()) else: # Not a Chain chain_id = "A" # default sb.init_chain(chain_id) if pdb_object.level == "R": # Residue # Residue extracted from a larger structure? if pdb_object.parent is not None: og_chain_id = pdb_object.parent.id sb.structure[0][chain_id].id = og_chain_id chain_id = og_chain_id sb.structure[0][chain_id].add(pdb_object.copy()) else: # Atom sb.init_residue("DUM", " ", 1, " ") # Dummy residue sb.structure[0][chain_id].child_list[0].add( pdb_object.copy()) # Fix chain identifier if Atom has grandparents. try: og_chain_id = pdb_object.parent.parent.id except AttributeError: # pdb_object.parent == None pass else: sb.structure[0][chain_id].id = og_chain_id # Return structure structure = sb.structure self.structure = structure
def pdb_writer(graph, results, dir_name="templates"): PDB_CODE = graph.protein_name structure = graph.structure path = "%s/%s/" % (dir_name, PDB_CODE) if len(results) > 0: if not os.path.exists(path): os.makedirs(path) else: print 'No results' return top = min(NUM_OF_RESULTS_TO_SAVE, len(results)) i = 0 for key, value in results[:top]: # TODO - save as different chains sb = StructureBuilder() sb.init_structure(PDB_CODE) sb.init_model(0) sb.init_chain('X') for node_id in key[0]: for res in graph.nodes[node_id].residues: sb.init_seg(res.get_segid()) sb.init_residue(res.get_resname(), res.get_id()[0], res.get_id()[1], res.get_id()[2]) for atom in res: sb.init_atom(atom.get_name(), atom.get_coord(), atom.get_bfactor(), atom.get_occupancy(), atom.get_altloc(), atom.get_fullname()) sb.init_chain('Y') for node_id in key[1]: for res in graph.nodes[node_id].residues: sb.init_residue(res.get_resname(), res.get_id()[0], res.get_id()[1], res.get_id()[2]) for atom in res: sb.init_atom(atom.get_name(), atom.get_coord(), atom.get_bfactor(), atom.get_occupancy(), atom.get_altloc(), atom.get_fullname()) filename = path + "interface%d.pdb" % (i) io = PDBIO() io.set_structure(sb.get_structure()) io.save(filename) i += 1 return 1
def residues_to_struct(name_residues_list, structID): """ Build a structure from a list of (chain name, residues) - each as a chain. """ builder = StructureBuilder() builder.init_structure(structID) builder.init_model(0) for (name, residues) in name_residues_list: builder.init_chain(name) for res in residues: builder.chain.add(res) return builder.get_structure()
def coarse_grain(self, cg_type="CA_TRACE"): """ Reduces the protein structure complexity to a few (pseudo-)atoms per residue. Parameters: - cg_type: CA_TRACE (Ca-only) [Default] ENCAD_3P (CA, O, SC Beads) MARTINI (CA, O, SC Beads) Returns a new structure object. """ # Import CG Types import CG_Models CG_Library = { "CA_TRACE": CG_Models.CA_TRACE, "ENCAD_3P": CG_Models.ENCAD_3P, "MARTINI": CG_Models.MARTINI } CG_Method = CG_Library[cg_type] # Creates a brand new structure object from Bio.PDB.StructureBuilder import StructureBuilder structure_builder = StructureBuilder() cg_id = "CG_" + self.id structure_builder.init_structure(cg_id) structure_builder.init_seg(' ') # Empty SEGID for model in self: structure_builder.init_model(model.id) for chain in model: structure_builder.init_chain(chain.id) cur_chain = structure_builder.chain for residue in chain: cg_residue = CG_Method(residue) cur_chain.add(cg_residue) cg_structure = structure_builder.get_structure() return cg_structure
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 write_helical_axes(self, filename): """Writes helical axes in PDB format.""" sb = StructureBuilder() sb.init_structure('') sb.init_model('') for cpos, chain in enumerate(self.chains): sb.init_chain(str(cpos)) sb.init_seg('') for pos, i in enumerate(chain.res[1:-1]): sb.init_residue('ALA', ' ', pos, ' ') sb.init_atom('CA', i.O._ar, 0, 0, " ", ' CA ', 1) io = PDBIO() io.set_structure(sb.structure) io.save(filename)
def set_structure(self, pdb_object): """Check what the user is providing and build a structure.""" if pdb_object.level == "S": structure = pdb_object else: sb = StructureBuilder() sb.init_structure("pdb") sb.init_seg(" ") # Build parts as necessary if pdb_object.level == "M": sb.structure.add(pdb_object.copy()) self.structure = sb.structure else: sb.init_model(0) if pdb_object.level == "C": sb.structure[0].add(pdb_object.copy()) else: sb.init_chain("A") if pdb_object.level == "R": try: parent_id = pdb_object.parent.id sb.structure[0]["A"].id = parent_id except Exception: pass sb.structure[0]["A"].add(pdb_object.copy()) else: # Atom sb.init_residue("DUM", " ", 1, " ") try: parent_id = pdb_object.parent.parent.id sb.structure[0]["A"].id = parent_id except Exception: pass sb.structure[0]["A"].child_list[0].add( pdb_object.copy()) # Return structure structure = sb.structure self.structure = structure
def set_structure(self, pdb_object): """Check what object the user is providing and build a structure.""" # This is duplicated from the PDBIO class if pdb_object.level == "S": structure = pdb_object else: sb = StructureBuilder() sb.init_structure('pdb') sb.init_seg(' ') # Build parts as necessary if pdb_object.level == "M": sb.structure.add(pdb_object) self.structure = sb.structure else: sb.init_model(0) if pdb_object.level == "C": sb.structure[0].add(pdb_object) else: sb.init_chain('A') if pdb_object.level == "R": try: parent_id = pdb_object.parent.id sb.structure[0]['A'].id = parent_id except ValueError: pass sb.structure[0]['A'].add(pdb_object) else: # Atom sb.init_residue('DUM', ' ', 1, ' ') try: parent_id = pdb_object.parent.parent.id sb.structure[0]['A'].id = parent_id except ValueError: pass sb.structure[0]['A'].child_list[0].add(pdb_object) # Return structure structure = sb.structure self.structure = structure
def set_structure(self, pdb_object): """Check what the user is providing and build a structure.""" if pdb_object.level == "S": structure = pdb_object else: sb = StructureBuilder() sb.init_structure('pdb') sb.init_seg(' ') # Build parts as necessary if pdb_object.level == "M": sb.structure.add(pdb_object.copy()) self.structure = sb.structure else: sb.init_model(0) if pdb_object.level == "C": sb.structure[0].add(pdb_object.copy()) else: sb.init_chain('A') if pdb_object.level == "R": try: parent_id = pdb_object.parent.id sb.structure[0]['A'].id = parent_id except Exception: pass sb.structure[0]['A'].add(pdb_object.copy()) else: # Atom sb.init_residue('DUM', ' ', 1, ' ') try: parent_id = pdb_object.parent.parent.id sb.structure[0]['A'].id = parent_id except Exception: pass sb.structure[0]['A'].child_list[0].add(pdb_object.copy()) # Return structure structure = sb.structure self.structure = structure
def set_structure(self, pdb_object): # Check what the user is providing and build a structure appropriately if pdb_object.level == "S": structure = pdb_object else: sb = StructureBuilder() sb.init_structure('pdb') sb.init_seg(' ') # Build parts as necessary if pdb_object.level == "M": sb.structure.add(pdb_object) self.structure = sb.structure else: sb.init_model(0) if pdb_object.level == "C": sb.structure[0].add(pdb_object) else: sb.init_chain('A') if pdb_object.level == "R": try: parent_id = pdb_object.parent.id sb.structure[0]['A'].id = parent_id except Exception: pass sb.structure[0]['A'].add(pdb_object) else: # Atom sb.init_residue('DUM', ' ', 1, ' ') try: parent_id = pdb_object.parent.parent.id sb.structure[0]['A'].id = parent_id except Exception: pass sb.structure[0]['A'].child_list[0].add(pdb_object) # Return structure structure = sb.structure self.structure = structure
def set_structure(self, pdb_object): # Check what the user is providing and build a structure appropriately if pdb_object.level == "S": structure = pdb_object else: sb = StructureBuilder() sb.init_structure("pdb") sb.init_seg(" ") # Build parts as necessary if pdb_object.level == "M": sb.structure.add(pdb_object) self.structure = sb.structure else: sb.init_model(0) if pdb_object.level == "C": sb.structure[0].add(pdb_object) else: sb.init_chain("A") if pdb_object.level == "R": try: parent_id = pdb_object.parent.id sb.structure[0]["A"].id = parent_id except Exception: pass sb.structure[0]["A"].add(pdb_object) else: # Atom sb.init_residue("DUM", " ", 1, " ") try: parent_id = pdb_object.parent.parent.id sb.structure[0]["A"].id = parent_id except Exception: pass sb.structure[0]["A"].child_list[0].add(pdb_object) # Return structure structure = sb.structure self.structure = structure
class StructureDecoder(object): """Class to pass the data from mmtf-python into a Biopython data structure.""" def __init__(self): """Initialize the class.""" self.this_type = "" def init_structure(self, total_num_bonds, total_num_atoms, total_num_groups, total_num_chains, total_num_models, structure_id): """Initialize the structure object. :param total_num_bonds: the number of bonds in the structure :param total_num_atoms: the number of atoms in the structure :param total_num_groups: the number of groups in the structure :param total_num_chains: the number of chains in the structure :param total_num_models: the number of models in the structure :param structure_id: the id of the structure (e.g. PDB id) """ self.structure_bulder = StructureBuilder() self.structure_bulder.init_structure(structure_id=structure_id) self.chain_index_to_type_map = {} self.chain_index_to_seq_map = {} self.chain_index_to_description_map = {} self.chain_counter = 0 def set_atom_info(self, atom_name, serial_number, alternative_location_id, x, y, z, occupancy, temperature_factor, element, charge): """Create an atom object an set the information. :param atom_name: the atom name, e.g. CA for this atom :param serial_number: the serial id of the atom (e.g. 1) :param alternative_location_id: the alternative location id for the atom, if present :param x: the x coordiante of the atom :param y: the y coordinate of the atom :param z: the z coordinate of the atom :param occupancy: the occupancy of the atom :param temperature_factor: the temperature factor of the atom :param element: the element of the atom, e.g. C for carbon. According to IUPAC. Calcium is Ca :param charge: the formal atomic charge of the atom """ # MMTF uses "\x00" (the NUL character) to indicate to altloc, so convert # that to the space required by StructureBuilder if alternative_location_id == "\x00": alternative_location_id = " " # Atom_name is in twice - the full_name is with spaces self.structure_bulder.init_atom(str(atom_name), [x, y, z], temperature_factor, occupancy, alternative_location_id, str(atom_name), serial_number=serial_number, element=str(element).upper()) def set_chain_info(self, chain_id, chain_name, num_groups): """Set the chain information. :param chain_id: the asym chain id from mmCIF :param chain_name: the auth chain id from mmCIF :param num_groups: the number of groups this chain has """ # A Bradley - chose to use chain_name (auth_id) as it complies # with current Biopython. Chain_id might be better. self.structure_bulder.init_chain(chain_id=chain_name) if self.chain_index_to_type_map[self.chain_counter] == "polymer": self.this_type = " " elif self.chain_index_to_type_map[self.chain_counter] == "non-polymer": self.this_type = "H" elif self.chain_index_to_type_map[self.chain_counter] == "water": self.this_type = "W" self.chain_counter += 1 def set_entity_info(self, chain_indices, sequence, description, entity_type): """Set the entity level information for the structure. :param chain_indices: the indices of the chains for this entity :param sequence: the one letter code sequence for this entity :param description: the description for this entity :param entity_type: the entity type (polymer,non-polymer,water) """ for chain_ind in chain_indices: self.chain_index_to_type_map[chain_ind] = entity_type self.chain_index_to_seq_map[chain_ind] = sequence self.chain_index_to_description_map[chain_ind] = description def set_group_info(self, group_name, group_number, insertion_code, group_type, atom_count, bond_count, single_letter_code, sequence_index, secondary_structure_type): """Set the information for a group :param group_name: the name of this group, e.g. LYS :param group_number: the residue number of this group :param insertion_code: the insertion code for this group :param group_type: a string indicating the type of group (as found in the chemcomp dictionary. Empty string if none available. :param atom_count: the number of atoms in the group :param bond_count: the number of unique bonds in the group :param single_letter_code: the single letter code of the group :param sequence_index: the index of this group in the sequence defined by the entity :param secondary_structure_type: the type of secondary structure used (types are according to DSSP and number to type mappings are defined in the specification) """ # MMTF uses a NUL character to indicate a blank insertion code, but # StructureBuilder expects a space instead. if insertion_code == "\x00": insertion_code = " " self.structure_bulder.init_seg(' ') self.structure_bulder.init_residue(group_name, self.this_type, group_number, insertion_code) def set_model_info(self, model_id, chain_count): """Set the information for a model. :param model_id: the index for the model :param chain_count: the number of chains in the model """ self.structure_bulder.init_model(model_id) def set_xtal_info(self, space_group, unit_cell): """Set the crystallographic information for the structure. :param space_group: the space group name, e.g. "P 21 21 21" :param unit_cell: an array of length 6 with the unit cell parameters in order: a, b, c, alpha, beta, gamma """ self.structure_bulder.set_symmetry(space_group, unit_cell) def set_header_info(self, r_free, r_work, resolution, title, deposition_date, release_date, experimnetal_methods): """Sets the header information. :param r_free: the measured R-Free for the structure :param r_work: the measure R-Work for the structure :param resolution: the resolution of the structure :param title: the title of the structure :param deposition_date: the deposition date of the structure :param release_date: the release date of the structure :param experimnetal_methods: the list of experimental methods in the structure """ pass def set_bio_assembly_trans(self, bio_assembly_index, input_chain_indices, input_transform): """Set the Bioassembly transformation information. A single bioassembly can have multiple transforms. :param bio_assembly_index: the integer index of the bioassembly :param input_chain_indices: the list of integer indices for the chains of this bioassembly :param input_transform: the list of doubles for the transform of this bioassmbly transform. """ pass def finalize_structure(self): """Any functions needed to cleanup the structure.""" pass def set_group_bond(self, atom_index_one, atom_index_two, bond_order): """Add bonds within a group. :param atom_index_one: the integer atom index (in the group) of the first partner in the bond :param atom_index_two: the integer atom index (in the group) of the second partner in the bond :param bond_order: the integer bond order """ pass def set_inter_group_bond(self, atom_index_one, atom_index_two, bond_order): """Add bonds between groups. :param atom_index_one: the integer atom index (in the structure) of the first partner in the bond :param atom_index_two: the integer atom index (in the structure) of the second partner in the bond :param bond_order: the bond order """ pass
class StructureDecoder(object): """Class to pass the data from mmtf-python into a Biopython data structure.""" def __init__(self): """Initialize the class.""" self.this_type = "" def init_structure(self, total_num_bonds, total_num_atoms, total_num_groups, total_num_chains, total_num_models, structure_id): """Initialize the structure object. :param total_num_bonds: the number of bonds in the structure :param total_num_atoms: the number of atoms in the structure :param total_num_groups: the number of groups in the structure :param total_num_chains: the number of chains in the structure :param total_num_models: the number of models in the structure :param structure_id: the id of the structure (e.g. PDB id) """ self.structure_bulder = StructureBuilder() self.structure_bulder.init_structure(structure_id=structure_id) self.chain_index_to_type_map = {} self.chain_index_to_seq_map = {} self.chain_index_to_description_map = {} self.chain_counter = 0 def set_atom_info(self, atom_name, serial_number, alternative_location_id, x, y, z, occupancy, temperature_factor, element, charge): """Create an atom object an set the information. :param atom_name: the atom name, e.g. CA for this atom :param serial_number: the serial id of the atom (e.g. 1) :param alternative_location_id: the alternative location id for the atom, if present :param x: the x coordiante of the atom :param y: the y coordinate of the atom :param z: the z coordinate of the atom :param occupancy: the occupancy of the atom :param temperature_factor: the temperature factor of the atom :param element: the element of the atom, e.g. C for carbon. According to IUPAC. Calcium is Ca :param charge: the formal atomic charge of the atom """ # MMTF uses "\x00" (the NUL character) to indicate to altloc, so convert # that to the space required by StructureBuilder if alternative_location_id == "\x00": alternative_location_id = " " # Atom_name is in twice - the full_name is with spaces self.structure_bulder.init_atom(str(atom_name), numpy.array((x, y, z), "f"), temperature_factor, occupancy, alternative_location_id, str(atom_name), serial_number=serial_number, element=str(element).upper()) def set_chain_info(self, chain_id, chain_name, num_groups): """Set the chain information. :param chain_id: the asym chain id from mmCIF :param chain_name: the auth chain id from mmCIF :param num_groups: the number of groups this chain has """ # A Bradley - chose to use chain_name (auth_id) as it complies # with current Biopython. Chain_id might be better. self.structure_bulder.init_chain(chain_id=chain_name) if self.chain_index_to_type_map[self.chain_counter] == "polymer": self.this_type = " " elif self.chain_index_to_type_map[self.chain_counter] == "non-polymer": self.this_type = "H" elif self.chain_index_to_type_map[self.chain_counter] == "water": self.this_type = "W" self.chain_counter += 1 def set_entity_info(self, chain_indices, sequence, description, entity_type): """Set the entity level information for the structure. :param chain_indices: the indices of the chains for this entity :param sequence: the one letter code sequence for this entity :param description: the description for this entity :param entity_type: the entity type (polymer,non-polymer,water) """ for chain_ind in chain_indices: self.chain_index_to_type_map[chain_ind] = entity_type self.chain_index_to_seq_map[chain_ind] = sequence self.chain_index_to_description_map[chain_ind] = description def set_group_info(self, group_name, group_number, insertion_code, group_type, atom_count, bond_count, single_letter_code, sequence_index, secondary_structure_type): """Set the information for a group. :param group_name: the name of this group, e.g. LYS :param group_number: the residue number of this group :param insertion_code: the insertion code for this group :param group_type: a string indicating the type of group (as found in the chemcomp dictionary. Empty string if none available. :param atom_count: the number of atoms in the group :param bond_count: the number of unique bonds in the group :param single_letter_code: the single letter code of the group :param sequence_index: the index of this group in the sequence defined by the entity :param secondary_structure_type: the type of secondary structure used (types are according to DSSP and number to type mappings are defined in the specification) """ # MMTF uses a NUL character to indicate a blank insertion code, but # StructureBuilder expects a space instead. if insertion_code == "\x00": insertion_code = " " self.structure_bulder.init_seg(' ') self.structure_bulder.init_residue(group_name, self.this_type, group_number, insertion_code) def set_model_info(self, model_id, chain_count): """Set the information for a model. :param model_id: the index for the model :param chain_count: the number of chains in the model """ self.structure_bulder.init_model(model_id) def set_xtal_info(self, space_group, unit_cell): """Set the crystallographic information for the structure. :param space_group: the space group name, e.g. "P 21 21 21" :param unit_cell: an array of length 6 with the unit cell parameters in order: a, b, c, alpha, beta, gamma """ self.structure_bulder.set_symmetry(space_group, unit_cell) def set_header_info(self, r_free, r_work, resolution, title, deposition_date, release_date, experimnetal_methods): """Set the header information. :param r_free: the measured R-Free for the structure :param r_work: the measure R-Work for the structure :param resolution: the resolution of the structure :param title: the title of the structure :param deposition_date: the deposition date of the structure :param release_date: the release date of the structure :param experimnetal_methods: the list of experimental methods in the structure """ pass def set_bio_assembly_trans(self, bio_assembly_index, input_chain_indices, input_transform): """Set the Bioassembly transformation information. A single bioassembly can have multiple transforms. :param bio_assembly_index: the integer index of the bioassembly :param input_chain_indices: the list of integer indices for the chains of this bioassembly :param input_transform: the list of doubles for the transform of this bioassmbly transform. """ pass def finalize_structure(self): """Any functions needed to cleanup the structure.""" pass def set_group_bond(self, atom_index_one, atom_index_two, bond_order): """Add bonds within a group. :param atom_index_one: the integer atom index (in the group) of the first partner in the bond :param atom_index_two: the integer atom index (in the group) of the second partner in the bond :param bond_order: the integer bond order """ pass def set_inter_group_bond(self, atom_index_one, atom_index_two, bond_order): """Add bonds between groups. :param atom_index_one: the integer atom index (in the structure) of the first partner in the bond :param atom_index_two: the integer atom index (in the structure) of the second partner in the bond :param bond_order: the bond order """ pass
io = PDBIO() # Parse PDB and run DSSP pdbf_path = os.path.abspath(sys.argv[1]) aa_model = P.get_structure('aa_model', pdbf_path) # Convert to MARTINI types # Assign by chain and build the cg structure already structure_builder = StructureBuilder() structure_builder.init_structure("cg_model") structure_builder.init_seg(' ') # Empty SEGID nbeads = 0 for model in aa_model: structure_builder.init_model(model.id) dssp = DSSP(model, pdbf_path) for chain in model: structure_builder.init_chain(chain.id) # Get SS information and translate it to MARTINI dssp_ss = [] for residue in chain: if residue.id[0] != ' ': continue if "SS_DSSP" in residue.xtra: dssp_ss.append(residue.xtra["SS_DSSP"]) else: dssp_ss.append('-')
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