def contact_pairs(self): """obtain all residue pairs with atoms in close proximity""" radius = 4.5 ns = NeighborSearch(self.atom_list) self.contact_pairs = ns.search_all(radius, level='R') #residues returned
def calculate_ic(structure, d_cutoff=5.0, selection=None): """ Calculates intermolecular contacts in a parsed structure object. """ atom_list = list(structure.get_atoms()) ns = NeighborSearch(atom_list) all_list = ns.search_all(radius=d_cutoff, level="R") if selection: _sd = selection def _chain(x): return x.parent.id ic_list = [ c for c in all_list if (_chain(c[0]) in _sd and _chain(c[1]) in _sd) and ( _sd[_chain(c[0])] != _sd[_chain(c[1])]) ] else: ic_list = [c for c in all_list if c[0].parent.id != c[1].parent.id] if not ic_list: raise ValueError("No contacts found for selection") return ic_list
def secondary_struc_cmap(chain, sequence, structure, cutoff_distance=4.5, cutoff_numcontacts=10, exclude_neighbour=3, ss_elements=['H', 'E', 'B', 'b', 'G']): atom_list = Selection.unfold_entities(chain, 'A') res_list = Selection.unfold_entities(chain, 'R') res_names, numbering = [], [] for res in res_list: res_names.append(res.get_resname()) numbering.append(res.get_id()[1]) numbering = np.array(numbering) res_range = np.array(range(len(numbering))) assert len(structure) == len( numbering ), f'PDB file and Secondary structure map do not match!\n {chain.get_parent().get_parent().id} - PDB: {len(res_list)} Residues VS. STRIDE: {len(sequence)} Residues. ' ns = NeighborSearch(atom_list) all_neighbours = ns.search_all(cutoff_distance, 'A') struc_length = len(structure) segment = np.zeros([struc_length], dtype='int') nseg = 1 for i in range(struc_length): if structure[i] in ss_elements: segment[i] = nseg if i == struc_length: nseg += 1 elif structure[i + 1] != structure[i]: nseg += 1 nseg -= 1 index_list = [] for atompair in all_neighbours: res1 = res_range[numbering == atompair[0].get_parent().id[1]][0] res2 = res_range[numbering == atompair[1].get_parent().id[1]][0] if abs(res1 - res2) > exclude_neighbour: if segment[res1] != 0 and segment[res2] != 0 and segment[ res1] != segment[res2]: index_list.append((segment[res1] - 1, segment[res2] - 1)) index_list.sort() count = Counter(index_list) index = [values for values in count if count[values] >= cutoff_numcontacts] return np.array(index), segment
def get_contacts(model, cutOff=5., minSeqDist=5): contacts = list() ns = NeighborSearch(list(model.get_atoms())) foundPairs = ns.search_all(cutOff) for pair in foundPairs: fullAtomIDs = [pair[0].get_full_id(), pair[1].get_full_id()] distance = pair[1] - pair[0] weight = 1. contacts.append((fullAtomIDs, distance, weight)) pass return contacts
def calculate_ic(structure, d_cutoff=5.5, selection=None): """ Calculates intermolecular contacts in a parsed structure object. """ atom_list = list(structure.get_atoms()) ns = NeighborSearch(atom_list) all_list = ns.search_all(radius=d_cutoff, level='R') if selection: _sd = selection_dict ic_list = [c for c in all_list if (c[0].parent.id in _sd and c[1].parent.id in _sd) and (_sd[c[0].parent.id] != _sd[c[1].parent.id]) ] else: ic_list = [c for c in all_list if c[0].parent.id != c[1].parent.id] if not ic_list: raise ValueError('No contacts found for selection') return ic_list
def find_pairs(atoms, distance=Constants.ATOM_ATOM_DISTANCE, level='A', do_print=False): """ Find pairs of atoms/residues/chains that are closer than specified distance. Pairs could represent edges in a graph of atoms. :param atoms: list of atoms of a structure :param distance: min distance to pair two atoms :param level: which entity pairs to return, atoms/residues or chains (A, R, C) :param do_print: boolean :return: pairs """ ns = NeighborSearch(atoms) pairs = ns.search_all(distance, level=level) if do_print: print('Number of pairs:', len(pairs)) return pairs, ns
SCALAR_EXPRESSION %s_exp = exp(%s_s*(%s_currentenergy-%s_o)) SCALAR_EXPRESSION %s_k = %s SCALAR_EXPRESSION %s_sig = (1-%s_k)+(%s_k/(1+%s_exp)) """) %(structure_id, structurefilename, correspondencefilename, resfilename, structure_id, startenergy, structure_id, structure_id, structure_id, s_value, structure_id, structure_id, structure_id, structure_id, structure_id, k_value, structure_id, structure_id, structure_id, structure_id) fitnessfile.write(outstring) fitnessstring = fitnessstring + str("*%s_sig") % (structure_id) nucleosome = structure[0] atom_list = Selection.unfold_entities(nucleosome, 'A') # A for atoms neighbor_search = NeighborSearch(atom_list) contacts_list = neighbor_search.search_all(radius, level = 'R') repack_residues = [] for contact in contacts_list: res1 = contact[0] res2 = contact[1] res1id = int(res1.get_id()[1]) chain1 = res1.get_parent() chain1id = chain1.get_id() res2id = int(res2.get_id()[1]) chain2 = res2.get_parent() chain2id = chain2.get_id() res1_in_patch = False res2_in_patch = False
def get_neighbours_residues(self, neighbor_radius=NEIGHBOUR_DEFAULT_RADIUS): # type: () -> List[Tuple[int, int]] """ :return: list of tuples (receptor_residue_index, ligand_residue_index) in which the euclidean distance between them is at most $(NEIGHBOURS_RADIUS) """ def is_protein_residue(residue): # type: (Residue) -> bool """ Checks if the residue comes from a protein :param residue: the given residue :return: is from protein """ return residue.id[0] == ' ' def add_true_residue_indexes(struct): """ adds for each residue in struct add a 'true' index, i.e it's index when counting all residues starting from one in same order they appear in the pdb file. the indices are global among chains of same protein """ true_index = 0 for residue in struct.get_residues(): if not is_protein_residue(residue): continue residue.true_index = true_index true_index += 1 if neighbor_radius not in NEIGHBOUR_RADII: raise ValueError("radius value is invalid") neighbour_attr = NEIGHBOUR_ATTR_TEMPLATE % neighbor_radius if hasattr(self, neighbour_attr) and getattr( self, neighbour_attr) is not None: return getattr(self, neighbour_attr) add_true_residue_indexes(self.receptor) add_true_residue_indexes(self.ligand) # TODO: change this hardcoded shit if neighbor_radius == 8: ligand_atoms = [ res['CA'] for res in self.ligand.get_residues() if res.has_id('CA') ] else: ligand_atoms = list(self.ligand.get_atoms()) if neighbor_radius == 8: receptor_atoms = [ res['CA'] for res in self.receptor.get_residues() if res.has_id('CA') ] else: receptor_atoms = list(self.receptor.get_atoms()) nb = NeighborSearch(ligand_atoms + receptor_atoms) all_neighbours = nb.search_all(neighbor_radius, level='R') neighbor_indexes = [] for residue_neighbor_A, residue_neighbor_B in all_neighbours: neighbor_A_struct_id, neighbor_B_struct_id = residue_neighbor_A.get_full_id()[0], \ residue_neighbor_B.get_full_id()[0] if neighbor_A_struct_id == neighbor_B_struct_id: # means both from receptor or both from ligand continue # one of the residues is not animo-acid if not is_protein_residue( residue_neighbor_A) or not is_protein_residue( residue_neighbor_B): continue if neighbor_A_struct_id == RECEPTOR_STRUCT_ID: receptor_residue, ligand_residue = residue_neighbor_A, residue_neighbor_B else: receptor_residue, ligand_residue = residue_neighbor_B, residue_neighbor_A # import pdb; pdb.set_trace() neighbor_indexes.append( (receptor_residue.true_index, ligand_residue.true_index)) setattr(self, neighbour_attr, neighbor_indexes) return getattr(self, neighbour_attr)
def contact_pairs(self, radius = ''): #define radius here in case want to change but keep if not radius: d_radii = {"Bloom": 4.5, "Shakh": 7.5} #in Angstroms radius = d_radii[self.contact_defn] ns = NeighborSearch(self.atoms) self.contact_pairs_list = ns.search_all(radius, level = 'R')
def _build_interface(self, model, id, threshold, rsa_calculation, rsa_threshold, include_waters=False, *chains): """ Return the interface of a model """ self.threshold=threshold # Recover chain list from initial unpacking chain_list = self.chain_list # Unfold atom list atom_list = [] for c in model: if c.id in chain_list: atom_list.extend(Selection.unfold_entities(c,'A')) # Using of NeighborSearch class in order to get the list of all residues at least than # the threshold distance of each others ns=NeighborSearch(atom_list) pairs=ns.search_all(threshold, 'R') if not pairs: raise ValueError("No atoms found in the interface") # Selection of residues pairs # 1. Exclude water contacts # 2. Filter same-chain contacts # 3. Filter user-defined chain pairs uniq_pairs=[] for pair in pairs: pair_resnames = (pair[0].resname, pair[1].resname) pair_chains = (pair[0].parent.id, pair[1].parent.id) if (not include_waters and 'HOH' in pair_resnames) or (pair_chains[0] == pair_chains[1]): continue if not (chains and not (pair_chains in chains)): uniq_pairs.append(pair) # Build the Interface # 1. Iterate over the pair list # 2. Add residues. for resA, resB in uniq_pairs: if resA not in self.interface: self._add_residue(resA) if resB not in self.interface: self._add_residue(resB) # Accessible surface area calculated for each residue # if naccess setup on user computer and rsa_calculation # argument is TRUE if rsa_calculation and os.system('which naccess') == 0: rsa_pairs=self._rsa_calculation(model, chain_list, rsa_threshold) for res in rsa_pairs: if res not in self.interface: self._add_residue(res) self._secondary_structure(model) #interface=uniq_pairs self.interface.uniq_pairs=uniq_pairs
# CONSTRUCT KDTREE neighborsearch = NeighborSearch(structure_atoms) logging.info('Constructured NeighborSearch.') # GET INTERACTIONS logging.info('Calculating interactions...') for interaction_level in 'ARC': if interaction_level in OUTPUTS: logging.info('Calculating interactions for {}s...'.format( LEVEL_MAP[interaction_level])) pairs = neighborsearch.search_all(INTERACTION_THRESHOLD, level=interaction_level) logging.info('Search complete for {}s.'.format( LEVEL_MAP[interaction_level])) logging.info('Organising interactions for {}s...'.format( LEVEL_MAP[interaction_level])) interactions = {} for entities in pairs: entity1, entity2 = entities # NO SELFIES if (entity1 is entity2) or (entity1 == entity2):
def get_cmap(chain, level='chain', cutoff_distance=4.5, cutoff_numcontacts=5, exclude_neighbour=3): if level == 'chain': atom_list = Selection.unfold_entities(chain, 'A') res_list = Selection.unfold_entities(chain, 'R') res_names, numbering = [], [] for res in res_list: res_names.append(res.get_resname()) numbering.append(res.get_id()[1]) ns = NeighborSearch(atom_list) all_neighbours = ns.search_all(cutoff_distance, 'A') numbering = np.array(numbering) segment = np.array(range(len(numbering))) index_list = [] for atompair in all_neighbours: res1 = segment[numbering == atompair[0].get_parent().id[1]][0] res2 = segment[numbering == atompair[1].get_parent().id[1]][0] if abs(res1 - res2) > exclude_neighbour: index_list.append((res1, res2)) index_list.sort() count = Counter(index_list) index = [ values for values in count if count[values] >= cutoff_numcontacts ] protid = chain.get_parent().get_parent().id + '_' + chain.id return np.array(index), numbering, protid, res_names if level == 'model': atom_list_model = Selection.unfold_entities(chain.get_parent(), 'A') res_list_model = Selection.unfold_entities(chain.get_parent(), 'R') ns = NeighborSearch(atom_list_model) all_neighbours = ns.search_all(cutoff_distance, 'A') res_names, numbering = [], [] for res in res_list_model: res_names.append(res.get_resname()) numbering.append([res.get_full_id()[2], res.get_full_id()[3][1]]) numbering = np.array(numbering) segment = np.array(range(len(numbering))) index_list = [] for atompair in all_neighbours: res1 = segment[(numbering == [ atompair[0].get_parent().get_full_id()[2], str(atompair[0].get_parent().get_full_id()[3][1]) ]).all(axis=1)][0] res2 = segment[(numbering == [ atompair[1].get_parent().get_full_id()[2], str(atompair[1].get_parent().get_full_id()[3][1]) ]).all(axis=1)][0] chain1 = atompair[0].get_parent().get_full_id()[2] chain2 = atompair[1].get_parent().get_full_id()[2] if abs(res1 - res2) > exclude_neighbour: index_list.append((res1, res2, chain1, chain2)) index_list.sort() index_list.sort(key=lambda x: x[2:4]) count = Counter(index_list) index = [ values for values in count if count[values] >= cutoff_numcontacts ] protid = chain.get_parent().get_parent().id return np.array(index), numbering, protid, res_names