def __init__(self, atoms, resInfo=None, options=None): """ Constructer of chain object """ self.chainID = atoms[ atoms["keys"][0] ][0].chainID self.alignment = None self.residues = [] self.configurations = [] self.last_residue = None if options.verbose == True: print("constructing chain %c (atoms: configurations)" % (self.chainID)) # creating the 'residues' for key in atoms["keys"]: myResidue = Residue(atoms[key], resInfo=resInfo) if myResidue.checked(): self.last_residue = myResidue self.residues.append(myResidue) # setting up list of configuration keys in this chain for residue in self.residues: for key in residue.configurations: if key not in self.configurations: self.configurations.append(key) # checking and correcting C-terminus and N-terminus self.addNTerminus(resInfo=resInfo) self.addCTerminus(resInfo=resInfo)
def __init__(self, atoms, resInfo=None, options=None): """ Constructer of chain object """ self.chainID = atoms[atoms["keys"][0]][0].chainID self.alignment = None self.residues = [] self.configurations = [] self.last_residue = None if options.verbose == True: pka_print("constructing chain %c (atoms: configurations)" % (self.chainID)) # creating the 'residues' for key in atoms["keys"]: myResidue = Residue(atoms[key], resInfo=resInfo) if myResidue.checked(): self.last_residue = myResidue self.residues.append(myResidue) # setting up list of configuration keys in this chain for residue in self.residues: for key in residue.configurations: if key not in self.configurations: self.configurations.append(key) # checking and correcting C-terminus and N-terminus self.addNTerminus(resInfo=resInfo) self.addCTerminus(resInfo=resInfo)
def __init__(self, file_name): ''' Protein class constructor Receives the file from which it is to be read. It needs to be in pdb or gro formats. Proteins must be ungapped (all resiude numbers must be consecutive). Only standard residues are considered. ''' super(Protein, self).__init__() ext = file_name.split('.')[-1].lower() if ext == 'pdb': with open(fileName, 'r') as f: fileContent = f.read().split('\n') for line in fileContent: if line[0:6] in ['TER ', 'END ']: return None elif line[0:6] == 'ATOM ': index = int(line[6:11]) name = line[12:16].strip() resName = line[17:20].strip() if res_name in ['HIP', 'HIE', 'HID']: res_name = 'HIS' assert res_name in AMINO_ACID_NAMES, \ "Only standard residues are considered." resId = int(line[22:26]) x = float(line[30:38]) y = float(line[38:46]) z = float(line[46:54]) if resId > 0: try: self.addResidue(Residue(resId, resName)) except AssertionError: pass self.addAtom(Atom(index, name, x, y, z), resId) q = [x for x in self if x != None] assert None not in self[self.index(q[0]):self.index(q[-1])+1], \ "Protein contains gaps." elif ext == 'gro': with open(file_name, 'r') as read_file: file_content = read_file.read().split('\n') number_of_atoms = int(file_content[1]) for index in range(number_of_atoms): line = file_content[2 + index].split() index = int(line[2]) name = line[1] res_name = line[0][-3:] res_id = int(line[0][:-3]) pos_x = float(line[3]) * 10 pos_y = float(line[4]) * 10 pos_z = float(line[5]) * 10 try: self.add_residue(Residue(res_id, res_name)) except AssertionError: pass self.add_atom(Atom(index, name, (pos_x, pos_y, pos_z)), res_id) else: raise NameError('Unknown file extension') return None
def generate_residue_sounds(self): # settings for the residue d = 0.2 df = 50. g = 200. m = 0.9 ns = [9, 10, 11] dfs = np.arange(-6 + 1, 6) * df for n in ns: for df in dfs: sound = Residue(n, df, m, g) print str(sound) sound.save_to_wav(self.fs, d)
def find_common_residues(structures, save_file): if 1 == 2: with open(safe_file, "rb") as f: common_residues = pickle.load(f) return common_residues else: all_residues = [] for structure in structures: structure_residues = set() top = md.load(structure).topology for residue in top.residues: if residue.is_protein: res = Residue(resSeq=residue.resSeq, chain_id=residue.chain.id, res_name="%s%d" % (residue.name, residue.resSeq)) #res = Residue(resSeq = residue.resSeq) structure_residues.add(res) all_residues.append(structure_residues) common_residues = list(set.intersection(*all_residues)) print((sorted([r.resSeq for r in common_residues]))) print(("There are %d common residues between input structures" % len(common_residues))) import pickle with open(save_file, "wb") as f: pickle.dump(common_residues, f) return common_residues
def __init__(self): """ Takes a filename and initializes the self.residue_table list with the contents of a residue table file """ self.residue_table = {} residue_list = data.aminoacids.buildTable() for r in residue_list: res = Residue(r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7]) self.residue_table[res.letterCode3] = res
def addCTerminus(self, resInfo=None): """ Creating a C-terminus residue for this chain and adding it to 'residues' list """ last_residue = None for residue in self.residues: if residue.type == "amino-acid": last_residue = residue if last_residue != None: atoms = last_residue.checkOXT() Cterminus = Residue(atoms, resName="C- ", resInfo=resInfo) self.residues.append(Cterminus)
def addNTerminus(self, resInfo=None): """ Creating a N-terminus residue for this chain and adding it to 'residues' list """ atom = None for residue in self.residues: if residue.type == "amino-acid": atom = residue.getAtom(name="N") break if atom != None: Nterminus = Residue([atom], resName="N+ ", resInfo=resInfo) self.residues.append(Nterminus)
def LoadFromFile(fname): from residue import Residue from atom import Atom def parse_pdb_line(s): # atname, resname, resid, xyz return s[12:16].strip(), \ s[17:21].strip(), \ int(s[22:26]), \ np.array([s[i:i+8] for i in (30,38,46)], dtype=float) rindex = 0 pivot = None atoms = [None] is_head = True molecule = Molecule('mol') with open(fname, 'r') as fin: for line in fin: if line.startswith('TITLE'): molecule.name = line[5:].strip() elif line.startswith('ATOM'): atname,resname,resid,xyz = parse_pdb_line(line) if is_head or (resid != rindex): residue = Residue(resname) molecule.append_residue(residue, is_head) is_head = False rindex = resid atom = Atom(atname, xyz) atoms.append(atom) residue.append_atom(atom) elif line.startswith('TER'): is_head = True elif line.startswith('CONECT'): idxs = map(int, line[6:].split()) pivot = atoms[idxs[0]] for i in idxs[1:]: pivot.connect_to(atoms[i]) auto_setup = pivot is None molecule.compile(auto_setup=auto_setup) return molecule
def __init__(self, sequence): ## a. parse the sequence. match_pattern = re.compile(r'([A-Z])(?:\((\w+)\))?') # Use non-capturing group seq_list = match_pattern.findall(sequence) self.mass = 0.0 self.seq = [] for item in seq_list: res = Residue() res.bySymbol(item[0]) if item[1] != '': mod = Modification(item[1]) # Update!!! The mod mass should not be integrated into residue mass. #res.mass += mod.mass self.seq.append([res, [mod]]) # Update!!! The mod mass is appended to the precursor directly. self.mass += mod.mass else: self.seq.append([res, []]) self.mass += res.mass self.length = len(self.seq) self.mass += Composition("H2O").mass
def load_fragments(): from residue import Residue import os ref = Residue('REF') ref.append_atom(Atom( 'N', [0.000, 0.300, 0.000])) ref.append_atom(Atom('CA', [1.181, -0.540, 0.000])) ref.append_atom(Atom( 'C', [2.440, 0.300, 0.000])) # load all residues this_dir, this_filename = os.path.split(__file__) DATA_PATH = os.path.join(this_dir, "static", "residues.pdb") residue_container = Molecule.LoadFromFile(DATA_PATH) for fragment in residue_container.iter_residues(): fit(ref, ('N','CA','C'), fragment, ('N','CA','C')) FragmentProvider.FRAGMENTS[fragment.name] = fragment
def convert_list_to_resobject_list(contact_residues): resobject_list = [] for residue in contact_residues: new_residue = Residue(resSeq=residue[1], chain_id=residue[0]) resobject_list.append(new_residue) return (resobject_list)
def update(self, **kwargs): """Rebuild hierarchical view of atoms. This method is called at instantiation, but can be used to rebuild the hierarchical view when attributes of atoms change.""" array = np.array atoms = self._atoms if isinstance(atoms, AtomGroup): ag = atoms _indices = np.arange(ag._n_atoms) selstr = False else: ag = atoms.getAtomGroup() _indices = atoms._indices selstr = atoms.getSelstr() acsi = self._atoms.getACSIndex() n_atoms = len(ag) self._dict = _dict = dict() self._chains = _chains = list() self._residues = _residues = list() self._segments = _segments = list() segindex = -1 segindices = np.zeros(n_atoms, int) chindex = -1 chindices = np.zeros(n_atoms, int) resindex = -1 resindices = np.zeros(n_atoms, int) sgnms = ag._getSegnames() if sgnms is None: _segments = None else: if selstr: sgnms = sgnms[_indices] unique = np.unique(sgnms) s = sgnms[0] if len(unique) == 1: if s != '': segment = Segment(ag, _indices, acsi=acsi, unique=True, selstr=selstr) _dict[s] = segment _segments.append(segment) LOGGER.info('Hierarchical view contains segments.') else: _segments = None else: ps = None for i, s in enumerate(sgnms): if s == ps or s in _dict: continue ps = s segindex += 1 idx = _indices[i:][sgnms[i:] == s] segment = Segment(ag, idx, acsi=acsi, unique=True, selstr=selstr) segindices[idx] = segindex _dict[s] = segment _segments.append(segment) LOGGER.info('Hierarchical view contains segments.') chids = ag._getChids() if chids is None: _chains = None else: if selstr: chids = chids[_indices] if _segments is None: if len(np.unique(chids)) == 1: chain = Chain(ag, _indices, acsi=acsi, unique=True) _dict[(None, chids[0] or None)] = chain _chains.append(chain) else: pc = None for i, c in enumerate(chids): if c == pc or (None, c) in _dict: continue pc = c chindex += 1 idx = _indices[i:][chids[i:] == c] chain = Chain(ag, idx, acsi=acsi, unique=True) chindices[idx] = chindex _dict[(None, c)] = chain _chains.append(chain) else: pc = chids[0] ps = sgnms[0] _i = 0 for i, c in enumerate(chids): s = sgnms[i] if c == pc and s == ps: continue s_c = (ps, pc or None) chain = _dict.get(s_c) if chain is None: segment = _dict[ps] chindex += 1 idx = _indices[_i:i] chain = Chain(ag, idx, acsi=acsi, segment=segment, unique=True) chindices[idx] = chindex _dict[s_c] = chain segment._dict[pc] = len(segment._list) segment._list.append(chain) _chains.append(chain) else: idx = _indices[_i:i] chindices[idx] = chain._indices[0] chain._indices = np.concatenate((chain._indices, idx)) pc = c ps = s _i = i s_c = (ps, pc or None) chain = _dict.get(s_c) idx = _indices[_i:] if chain is None: segment = _dict[ps] chindex += 1 chindices[idx] = chindex chain = Chain(ag, idx, acsi=acsi, segment=segment, unique=True) _dict[s_c] = chain segment._dict[pc] = len(segment._list) segment._list.append(chain) _chains.append(chain) else: chindices[idx] = chain._indices[0] chain._indices = np.concatenate((chain._indices, idx)) if kwargs.get('chain') == True: return rnums = ag._getResnums() if rnums is None: raise ValueError('resnums are not set') if selstr: rnums = rnums[_indices] nones = None if _segments is None: if nones is None: nones = [None] * len(rnums) sgnms = nones if _chains is None: if nones is None: nones = [None] * len(rnums) chids = nones icods = ag._getIcodes() if icods is None: if nones is None: nones = [None] * len(rnums) icods = nones elif selstr: icods = icods[_indices] pr = rnums[0] pi = icods[0] or None pc = chids[0] ps = sgnms[0] or None _j = 0 for j, r in enumerate(rnums): i = icods[j] or None c = chids[j] or None s = sgnms[j] if r != pr or i != pi or c != pc or s != ps: s_c_r_i = (ps, pc, pr, pi) res = _dict.get(s_c_r_i) if res is None: chain = _dict.get((ps, pc)) resindex += 1 idx = _indices[_j:j] res = Residue(ag, idx, acsi=acsi, chain=chain, unique=True, selstr=selstr) resindices[idx] = resindex if chain is not None: chain._dict[(pr, pi)] = len(chain._list) chain._list.append(res) _residues.append(res) _dict[s_c_r_i] = res else: res._indices = np.concatenate((res._indices, _indices[_j:j])) ps = s pc = c pr = r pi = i _j = j s_c_r_i = (ps, pc, pr, pi) res = _dict.get(s_c_r_i) idx = _indices[_j:] if res is None: chain = _dict.get((ps, pc)) resindex += 1 res = Residue(ag, idx, acsi=acsi, chain=chain, unique=True, selstr=selstr) resindices[idx] = resindex if chain is not None: chain._dict[(pr, pi)] = len(chain._list) chain._list.append(res) _residues.append(res) _dict[s_c_r_i] = res else: resindices[idx] = res._indices[0] res._indices = np.concatenate((res._indices, idx)) ag._data['segindices'] = segindices ag._data['chindices'] = chindices ag._data['resindices'] = resindices
def update(self, **kwargs): """Update (or build) hierarchical view of atoms. This method is called at instantiation, but can be used to rebuild the hierarchical view when attributes of atoms change.""" atoms = self._atoms if isinstance(atoms, AtomGroup): ag = atoms selstr = False _indices = arange(atoms._n_atoms) else: ag = atoms.getAtomGroup() _indices = atoms._getIndices() selstr = atoms.getSelstr() set_indices = False if atoms == ag: set_indices = True _dict = dict() _residues = list() _segments = list() _chains = list() # also set the attributes, so that when no segments or chains # are available, num/iter methods won't raise exceptions self._dict = _dict self._residues = _residues self._segments = _segments self._chains = _chains acsi = atoms.getACSIndex() n_atoms = len(ag) # identify segments if set_indices: segindex = -1 segindices = zeros(n_atoms, int) sgnms = ag._getSegnames() if sgnms is None: _segments = None else: if selstr: sgnms = sgnms[_indices] s = sgnms[0] if len(unique(sgnms)) == 1: if s != '': segment = Segment(ag, _indices, acsi=acsi, unique=True, selstr=selstr) _dict[s] = segment _segments.append(segment) LOGGER.info('Hierarchical view contains segments.') else: _segments = None else: ps = None # previous segment name for i, s in enumerate(sgnms): if s == ps or s in _dict: continue ps = s idx = _indices[i:][sgnms[i:] == s] segment = Segment(ag, idx, acsi=acsi, unique=True, selstr=selstr) if set_indices: segindex += 1 segindices[idx] = segindex _dict[s] = segment _segments.append(segment) LOGGER.info('Hierarchical view contains segments.') if set_indices: ag._data['segindices'] = segindices chindex = -1 chindices = zeros(n_atoms, int) # identify chains chids = ag._getChids() if chids is None: _chains = None else: if selstr: chids = chids[_indices] if _segments is None: if len(unique(chids)) == 1: chain = Chain(ag, _indices, acsi=acsi, unique=True, selstr=selstr) _dict[(None, chids[0] or None)] = chain _chains.append(chain) else: pc = None for i, c in enumerate(chids): if c == pc or (None, c) in _dict: continue pc = c idx = _indices[i:][chids[i:] == c] chain = Chain(ag, idx, acsi=acsi, unique=True, selstr=selstr) if set_indices: chindex += 1 chindices[idx] = chindex _dict[(None, c)] = chain _chains.append(chain) else: pc = chids[0] ps = sgnms[0] _i = 0 for i, c in enumerate(chids): s = sgnms[i] if c == pc and s == ps: continue s_c = (ps, pc or None) chain = _dict.get(s_c) if chain is None: segment = _dict[ps] idx = _indices[_i:i] chain = Chain(ag, idx, acsi=acsi, segment=segment, unique=True, selstr=selstr) if set_indices: chindex += 1 chindices[idx] = chindex _dict[s_c] = chain _chains.append(chain) else: idx = _indices[_i:i] chindices[idx] = chain._indices[0] chain._indices = concatenate((chain._indices, idx)) pc = c ps = s _i = i s_c = (ps, pc or None) chain = _dict.get(s_c) idx = _indices[_i:] if chain is None: segment = _dict[ps] if set_indices: chindex += 1 chindices[idx] = chindex chain = Chain(ag, idx, acsi=acsi, segment=segment, unique=True, selstr=selstr) _dict[s_c] = chain _chains.append(chain) else: if set_indices: chindices[idx] = chain._indices[0] chain._indices = concatenate((chain._indices, idx)) if set_indices: ag._data['chindices'] = chindices if kwargs.get('chain') == True: return if set_indices: resindex = -1 resindices = zeros(n_atoms, int) rnums = ag._getResnums() if rnums is None: raise ValueError('resnums are not set') if selstr: rnums = rnums[_indices] nones = None if _segments is None: if nones is None: nones = [None] * len(rnums) sgnms = nones if _chains is None: if nones is None: nones = [None] * len(rnums) chids = nones icods = ag._getIcodes() if icods is None: if nones is None: nones = [None] * len(rnums) icods = nones elif selstr: icods = icods[_indices] pr = rnums[0] pi = icods[0] or None pc = chids[0] ps = sgnms[0] _j = 0 _append = _residues.append _get = _dict.get _set = _dict.__setitem__ for j, r in enumerate(rnums): i = icods[j] or None c = chids[j] s = sgnms[j] if r != pr or i != pi or c != pc or s != ps: s_c_r_i = (ps, pc, pr, pi) res = _get(s_c_r_i) if res is None: chain = _get((ps, pc)) idx = _indices[_j:j] res = Residue(ag, idx, acsi=acsi, chain=chain, unique=True, selstr=selstr) if set_indices: resindex += 1 resindices[idx] = resindex _append(res) _set(s_c_r_i, res) else: res._indices = concatenate((res._indices, _indices[_j:j])) ps = s pc = c pr = r pi = i _j = j s_c_r_i = (ps, pc, pr, pi) res = _get(s_c_r_i) idx = _indices[_j:] if res is None: chain = _get((ps, pc)) res = Residue(ag, idx, acsi=acsi, chain=chain, unique=True, selstr=selstr) if set_indices: resindex += 1 resindices[idx] = resindex _append(res) _set(s_c_r_i, res) else: if set_indices: resindices[idx] = res._indices[0] res._indices = concatenate((res._indices, idx)) if set_indices: ag._data['resindices'] = resindices
resobject_list = [] for residue in contact_residues: new_residue = Residue(resSeq = residue[1], chain_id = residue[0]) resobject_list.append(new_residue) return(resobject_list) #CHOOSE RESIDUES: bp_4dkl = md.load("/home/enf/md_simulations/MOR/4dkl_bp_6pt6.pdb") bp_5c1m = md.load("/home/enf/md_simulations/MOR/5c1m_bp_6pt6.pdb") bp_4dkl_residues = set([r.resSeq for r in bp_4dkl.topology.residues if r.is_protein]) bp_5c1m_residues = set([r.resSeq for r in bp_4dkl.topology.residues if r.is_protein]) bp_residues = sorted(list(bp_4dkl_residues | bp_5c1m_residues)) bp_residue_objects = convert_list_to_resobject_list([("R", i) for i in bp_residues]) cutoff = 1. feature_name = "bp_residues_4dkl_5c1m_under_cutoff%dA" %(int(10*cutoff)) common_residues_pkl = get_common_residues_pkl(base) contact_residues = find_common_residues([inactive_dir, active_dir, simulation_structure], common_residues_pkl) contact_resSeq = [r.resSeq for r in contact_residues] bfna_resobj = Residue(resSeq = 601, res_name = "BF0") _, bp_4dkl_residues = find_binding_pocket_residues(bfna_resobj, contact_residues, inactive_dir, cutoff=cutoff) bu72_resobj = Residue(resSeq = 401, res_name = "4VO") _, bp_5c1m_residues = find_binding_pocket_residues(bu72_resobj, contact_residues, active_dir, cutoff=cutoff) bp_residue_objects = sorted(list(set(bp_4dkl_residues) | set(bp_5c1m_residues))) #bp_residue_objects = [res for res in bp_residue_objects if res.resSeq in contact_resSeq]
def getStubs(self): """returns a list of dicts with peptide and stub glycopeptide ions """ for i in self.pep_seq: res = Residue() res.bySymbol(i) if i == 'C': self.mass += Carbamidomethyl self.mass += res.mass if self.mod_type == "Deamidated": self.mass += (Deamidation * self.n_mod) elif self.mod_type == '': pass stubs =[] stubs.append({"key":"peptide", "mass": (self.mass + Proton)}) sites = self.glyco_num if sites == 0 : pass elif sites>0: if self.dHex==0: for i in range(1,sites+1,1): for j in range(1,3,1): if j ==1: key = "pep+"+str(j)+"HexNAc+"+"0Hexose"+"-"+str(i)+"sites" mass = self.mass+Proton+(i*(j*HexNAc)) #print key, mass stubs.append({"key":key,"mass": mass}) elif (j == 2): for k in range(0,4,1): key = "pep+"+str(j)+"HexNAc+"+str(k)+"Hexose"+"-"+str(i)+"sites" mass = self.mass+Proton+(i*((j*HexNAc)+(k*Hex))) #print key, mass stubs.append({"key":key,"mass": mass}) else: pass elif self.dHex>0: for i in range(1,sites+1,1): for j in range(1,3,1): if j ==1: key = "pep+"+str(j)+"HexNAc+"+"0Hexose"+"-"+str(i)+"sites" mass = self.mass+Proton+(i*(j*HexNAc)) #print key, mass stubs.append({"key":key,"mass": mass}) if (j == 2): for l in range (0,2,1): for k in range(0,4,1): key = "pep+"+str(j)+"HexNAc+"+str(k)+"Hexose"+str(l)+"dHex"+"-"+str(i)+"sites" mass = self.mass+Proton+(i*((j*HexNAc)+(k*Hex)+(l*dHex))) #print key, mass stubs.append({"key":key,"mass": mass}) else: pass j +=1 return stubs