def count_interactions(grof, xtcf, btime, cutoff, debug): u = Universe(grof, xtcf) un_query = ('(resname PRO and (name CB or name CG or name CD)) or' '(resname VAL and (name CG1 or name CG2)) or' '(resname GLY and name CA) or' '(resname ALA and name CB)') vp_query = ('name OW') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm un_atoms = u.selectAtoms(un_query) for ts in u.trajectory: if ts.time >= btime: numcount = 0 tropo_vp_atoms = u.selectAtoms('({0}) and around 8 ({1})'.format( vp_query, un_query)) # different from when calculating unun, there is no overlap atom # between un_atoms & tropo_vp_atoms for ai in un_atoms: for aj in tropo_vp_atoms: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: numcount += 1 yield '{0:10.0f}{1:8d}\n'.format(ts.time, numcount) # per 100 frames, num of frames changes with the size of xtc file, for debugging if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format( ts.time, ts.step, ts.frame)
def count_interactions(A): logger.debug('loading {0}'.format(A.grof)) univ = Universe(A.grof) logger.debug('loaded {0}'.format(A.grof)) pro_atoms = univ.selectAtoms( 'protein and not resname ACE and not resname NH2') pl = pro_atoms.residues.numberOfResidues() # +1: for missing resname ACE, such that it's easier to proceed in the next # step logger.debug('loading {0}, {1}'.format(A.grof, A.xtcf)) u = Universe(A.grof, A.xtcf) logger.debug('loaded {0}, {1}'.format(A.grof, A.xtcf)) # Just for reference to the content of query when then code was first # written and used # query = ('(resname PRO and (name CB or name CG or name CD)) or' # '(resname VAL and (name CG1 or name CG2)) or' # '(resname GLY and name CA) or' # '(resname ALA and name CB)') query = A.query atoms = u.selectAtoms(query) logger.info('Number of atoms selected: {0}'.format(atoms.numberOfAtoms())) # MDAnalysis will convert the unit of length to angstrom, though in Gromacs # the unit is nm cutoff = A.cutoff * 10 nres_away = A.nres_away btime = A.btime etime = A.etime nframe = 0 unun_map = None for ts in u.trajectory: if btime > ts.time: continue if etime > 0 and etime < ts.time: break nframe += 1 map_ = np.zeros((pl + 1, pl + 1)) # map for a single frame for i, ai in enumerate(atoms): ai_resid = ai.resid for j, aj in enumerate(atoms): aj_resid = aj.resid # to avoid counting the same pair twices, # the 2 resid cannot be neigbors if i < j and aj_resid - ai_resid >= nres_away: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: # -1: resid in MDAnalysis starts from 1 map_[ai_resid - 1][aj_resid - 1] += 1 if unun_map is None: unun_map = map_ else: unun_map = unun_map + map_ utils.print_progress(ts) sys.stdout.write("\n") return unun_map / float(nframe)
def count_interactions(grof, xtcf, btime, cutoff, debug): u = Universe(grof, xtcf) un_query = ('(resname PRO and (name CB or name CG or name CD)) or' '(resname VAL and (name CG1 or name CG2)) or' '(resname GLY and name CA) or' '(resname ALA and name CB)') vp_query = ('name OW') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm un_atoms = u.selectAtoms(un_query) for ts in u.trajectory: if ts.time >= btime: numcount = 0 tropo_vp_atoms = u.selectAtoms( '({0}) and around 8 ({1})'.format(vp_query, un_query)) # different from when calculating unun, there is no overlap atom # between un_atoms & tropo_vp_atoms for ai in un_atoms: for aj in tropo_vp_atoms: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: numcount += 1 yield '{0:10.0f}{1:8d}\n'.format(ts.time, numcount) # per 100 frames, num of frames changes with the size of xtc file, for debugging if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format(ts.time, ts.step, ts.frame)
def test_write_selection(self): ref = Universe(mol2_molecule) gr0 = ref.selectAtoms("name C*") gr0.write(self.outfile) u = Universe(self.outfile) gr1 = u.selectAtoms("name C*") assert_equal(len(gr0), len(gr1))
def calc_rama(grof, xtcf, btime, etime): u = Universe(grof, xtcf) resname_query = 'resname GLY or resname VAL or resname PRO' atoms = u.selectAtoms(resname_query) resname = atoms.resnames()[0] # [0] because .resnames() returns a list of one element resid = atoms.resids()[0] # [0] because .resnames() returns a list of one element phi_query = ('(resname ACE and name C) or ' '(resname GLY or resname VAL or resname PRO and ' '(name N or name CA or name C))') psi_query = ('(resname GLY or resname VAL or resname PRO and (name N or name CA or name C or name NT)) or ' '(resname NH2 and name N)') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm phi = u.selectAtoms(phi_query) psi = u.selectAtoms(psi_query) for _ in phi.atoms: print _ for _ in psi.atoms: print _ for ts in u.trajectory: if btime > ts.time: continue if etime > 0 and etime < ts.time: break yield '{0:.3f} {1:.3f} {2}-{3}\n'.format( phi.dihedral(), psi.dihedral(), resname, resid) U.print_progress(ts)
def count_interactions(A): logger.debug('loading {0}'.format(A.grof)) univ = Universe(A.grof) logger.debug('loaded {0}'.format(A.grof)) pro_atoms = univ.selectAtoms('protein and not resname ACE and not resname NH2') pl = pro_atoms.residues.numberOfResidues() # +1: for missing resname ACE, such that it's easier to proceed in the next # step logger.debug('loading {0}, {1}'.format(A.grof, A.xtcf)) u = Universe(A.grof, A.xtcf) logger.debug('loaded {0}, {1}'.format(A.grof, A.xtcf)) # Just for reference to the content of query when then code was first # written and used # query = ('(resname PRO and (name CB or name CG or name CD)) or' # '(resname VAL and (name CG1 or name CG2)) or' # '(resname GLY and name CA) or' # '(resname ALA and name CB)') query = A.query atoms = u.selectAtoms(query) logger.info('Number of atoms selected: {0}'.format(atoms.numberOfAtoms())) # MDAnalysis will convert the unit of length to angstrom, though in Gromacs # the unit is nm cutoff = A.cutoff * 10 nres_away = A.nres_away btime = A.btime etime = A.etime nframe = 0 unun_map = None for ts in u.trajectory: if btime > ts.time: continue if etime > 0 and etime < ts.time: break nframe += 1 map_ = np.zeros((pl+1, pl+1)) # map for a single frame for i, ai in enumerate(atoms): ai_resid = ai.resid for j, aj in enumerate(atoms): aj_resid = aj.resid # to avoid counting the same pair twices, # the 2 resid cannot be neigbors if i < j and aj_resid - ai_resid >= nres_away: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: # -1: resid in MDAnalysis starts from 1 map_[ai_resid-1][aj_resid-1] += 1 if unun_map is None: unun_map = map_ else: unun_map = unun_map + map_ utils.print_progress(ts) sys.stdout.write("\n") return unun_map / float(nframe)
def test_atomgroups(self): u = Universe(altloc) segidB0 = len(u.selectAtoms("segid B and (not altloc B)")) segidB1 = len(u.selectAtoms("segid B and (not altloc A)")) assert_equal(segidB0, segidB1) altlocB0 = len(u.selectAtoms("segid B and (altloc A)")) altlocB1 = len(u.selectAtoms("segid B and (altloc B)")) assert_equal(altlocB0, altlocB1) sum = len(u.selectAtoms("segid B")) assert_equal(sum, segidB0 + altlocB0)
def gen_hbond_map(xpm, ndx, grof): xpm = objs.XPM(xpm) hbndx = objs.HBNdx(ndx) univ = Universe(grof) pro_atoms = univ.selectAtoms( 'protein and not resname ACE and not resname NH2') hbonds_by_resid = hbndx.map_id2resid(pro_atoms) # pl: peptide length pl = pro_atoms.residues.numberOfResidues() hblist = [] for i, j in zip(hbonds_by_resid, xpm.color_count): # j[1] is the probability of hbonds, while j[0] = 1 - j[1] # format: [resid of donor, resid of acceptor] # -1 is because resid in MDAnalysis starts from 1, minus so as to fit # -into hb_map initialized by hb_map hblist.append([i[0] - 1, i[1] - 1, j[1]]) # +1: for missing resname ACE, such that it's easier to proceed in the next # step pl1 = pl + 1 hb_map = np.zeros((pl1, pl1)) for _ in hblist: hb_map[_[0]][_[1]] = _[2] return hb_map
def count_interactions(grof, xtcf, btime, etime, cutoff): cutoff = cutoff * 10 # * 10: convert from nm to angstrom to work with MDAnalysis u = Universe(grof, xtcf) query = ('(resname PRO and (name CB or name CG or name CD)) or' '(resname VAL and (name CG1 or name CG2)) or' '(resname GLY and name CA) or' '(resname ALA and name CB)') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm atoms = u.selectAtoms(query) for ts in u.trajectory: if btime > ts.time: continue if etime > 0 and etime < ts.time: break numcount = 0 for i, ai in enumerate(atoms): for j, aj in enumerate(atoms): # to avoid counting the same pair twices, # the 2 resid cannot be neigbors if i < j and abs(ai.resid - aj.resid) >= 2: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: numcount += 1 yield '{0:10.0f}{1:8d}\n'.format(ts.time, numcount) utils.print_progress(ts)
def count_interactions(grof, xtcf, btime, etime, cutoff): cutoff = cutoff * 10 # * 10: convert from nm to angstrom to work with MDAnalysis u = Universe(grof, xtcf) query = ('(resname PRO and (name CB or name CG or name CD)) or' '(resname VAL and (name CG1 or name CG2)) or' '(resname GLY and name CA) or' '(resname ALA and name CB)') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm atoms = u.selectAtoms(query) for ts in u.trajectory: if btime > ts.time: continue if etime > 0 and etime < ts.time: break numcount = 0 for i, ai in enumerate(atoms): for j, aj in enumerate(atoms): # to avoid counting the same pair twices, # the 2 resid cannot be neigbors if i < j and abs(ai.resid - aj.resid) >= 2: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: numcount += 1 yield '{0:10.0f}{1:8d}\n'.format(ts.time, numcount) utils.print_progress(ts)
def sequence_spacing(pf, grof, xtcf, peptide_length, atom_sel, output=None): u = Universe(grof, xtcf) # this selection part should be better customized # here, only have been backbone atoms are used, u.selectAtoms doesn't # include Hydrogen atoms # REMMEMBER: OPTIONS verification should be done in main ONLY! residues = [ u.selectAtoms(atom_sel.format(i)) for i in range(2, peptide_length) ] ijdist_dict = {} for ts in u.trajectory: for i, resi in enumerate(residues): for j, resj in enumerate(residues): if i < j: resi_pos = resi.centerOfGeometry() # residue i position resj_pos = resj.centerOfGeometry() # residue j position ijdist = np.linalg.norm(resi_pos - resj_pos) dij = j - i # distance between i and j if dij not in ijdist_dict.keys(): ijdist_dict[dij] = [dij] else: ijdist_dict[dij].append(ijdist) if ts.step % 2000000 == 0: # 2000ps print "time step: {0:d}".format(ts.step) return ijdist_dict
def main(): arg_parser = argparse.ArgumentParser(description='通过给定残基名称,残基内原子数目,两个原子在残基内的索引(从0开始),计算所有残基内这两个原子之间的直线距离。') arg_parser.add_argument('resname', action='store', help='残基名称') arg_parser.add_argument('atoms_num', type=int, action='store', help='残基内原子数目') arg_parser.add_argument('index1', type=int, action='store', help='第一个原子的索引,索引从0开始') arg_parser.add_argument('index2', type=int, action='store', help='第二个原子的索引,索引从0开始') arg_parser.add_argument('topology_file', action='store', help='拓扑文件,例如gro, pdb') args = arg_parser.parse_args() resname, atoms_num, index1, index2 = args.resname, args.atoms_num, args.index1, args.index2 universe = Universe(args.topology_file) atom_groups = universe.selectAtoms("resname " + resname) if len(atom_groups) % atoms_num != 0: print("拓扑文件内对应残基原子总数不是所给原子数目的整数倍,请给予正确的原子数目。") exit(1) atoms1 = [] atoms2 = [] for i in range(0, len(atom_groups), atoms_num): atoms1.append(atom_groups[i:i + atoms_num][index1]) atoms2.append(atom_groups[i:i + atoms_num][index2]) dists = dist(AtomGroup(atoms1), AtomGroup(atoms2)) print("The distance between atoms %s and %s is:" % (index1, index2)) for i in dists[2]: print(i) print("The average distance between atoms %s and %s is:" % (index1, index2)) print(np.average(dists[2]))
def gen_hbond_map(xpm, ndx, grof): xpm = objs.XPM(xpm) hbndx = objs.HBNdx(ndx) univ = Universe(grof) pro_atoms = univ.selectAtoms('protein and not resname ACE and not resname NH2') hbonds_by_resid = hbndx.map_id2resid(pro_atoms) # pl: peptide length pl = pro_atoms.residues.numberOfResidues() hblist = [] for i, j in zip(hbonds_by_resid, xpm.color_count): # j[1] is the probability of hbonds, while j[0] = 1 - j[1] # format: [resid of donor, resid of acceptor] # -1 is because resid in MDAnalysis starts from 1, minus so as to fit # -into hb_map initialized by hb_map hblist.append([i[0]-1, i[1]-1, j[1]]) # +1: for missing resname ACE, such that it's easier to proceed in the next # step pl1 = pl + 1 hb_map = np.zeros((pl1, pl1)) for _ in hblist: hb_map[_[0]][_[1]] = _[2] return hb_map
def test_bonds(self): u = Universe(altloc, bonds=True) # need to force topology to load before querying individual atom bonds u.build_topology() bonds0 = u.selectAtoms("segid B and (altloc A)")[0].bonds bonds1 = u.selectAtoms("segid B and (altloc B)")[0].bonds assert_equal(len(bonds0), len(bonds1))
def main(): arg_parser = argparse.ArgumentParser( description='通过给定残基名称,残基内原子数目,原子在残基内的索引(从0开始),计算原子的坐标。') arg_parser.add_argument('resname', action='store', help='残基名称') arg_parser.add_argument('atoms_num', type=int, action='store', help='残基内原子数目') arg_parser.add_argument('index', type=int, action='store', help='原子的索引,索引从0开始') arg_parser.add_argument('topology_file', action='store', help='拓扑文件,例如gro, pdb') args = arg_parser.parse_args() resname, atoms_num, index = args.resname, args.atoms_num, args.index universe = Universe(args.topology_file) atom_groups = universe.selectAtoms("resname " + resname) if len(atom_groups) % atoms_num != 0: print("拓扑文件内对应残基原子总数不是所给原子数目的整数倍,请给予正确的原子数目。") exit(1) positions = [] for i in range(0, len(atom_groups), atoms_num): positions.append(atom_groups[i:i + atoms_num][index].position) print("The positions of atoms %s is:" % (index)) for i in positions: print(i)
def main(): args = parse_args() u = Universe(args.input) gr = u.selectAtoms(args.selection) print(gr) if args.center: center(gr) gr.write(args.output)
def main(struct): u = Universe(struct) phi = u.selectAtoms(PHI_SEL) psi = u.selectAtoms(PSI_SEL) print u.filename print 'phi: {0:8.2f}'.format(phi.dihedral()) print 'psi: {0:8.2f}'.format(psi.dihedral()) print
def calc_rama(grof, xtcf, btime, etime): u = Universe(grof, xtcf) resname_query = 'resname GLY or resname VAL or resname PRO' atoms = u.selectAtoms(resname_query) resname = atoms.resnames()[ 0] # [0] because .resnames() returns a list of one element resid = atoms.resids()[ 0] # [0] because .resnames() returns a list of one element phi_query = ('(resname ACE and name C) or ' '(resname GLY or resname VAL or resname PRO and ' '(name N or name CA or name C))') psi_query = ( '(resname GLY or resname VAL or resname PRO and (name N or name CA or name C or name NT)) or ' '(resname NH2 and name N)') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm phi = u.selectAtoms(phi_query) psi = u.selectAtoms(psi_query) for _ in phi.atoms: print _ for _ in psi.atoms: print _ for ts in u.trajectory: if btime > ts.time: continue if etime > 0 and etime < ts.time: break yield '{0:.3f} {1:.3f} {2}-{3}\n'.format(phi.dihedral(), psi.dihedral(), resname, resid) U.print_progress(ts)
def calc_rg(grof, xtcf, btime, debug): u = Universe(grof, xtcf) query = 'name CA' # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm atoms = u.selectAtoms(query) natoms = atoms.numberOfAtoms() for ts in u.trajectory: if ts.time >= btime: com = atoms.centerOfMass() # center of mass _sum = sum((sum(i**2 for i in (a.pos - com)) for a in atoms)) rg = np.sqrt(_sum / natoms) yield '{0:10.0f}{1:15.6f}\n'.format(ts.time, rg) # per 100 frames, num of frames changes with the size of xtc file, for debugging if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format(ts.time, ts.step, ts.frame)
def calc_rg(grof, xtcf, btime, debug): u = Universe(grof, xtcf) query = 'name CA' # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm atoms = u.selectAtoms(query) natoms = atoms.numberOfAtoms() for ts in u.trajectory: if ts.time >= btime: com = atoms.centerOfMass() # center of mass _sum = sum((sum(i**2 for i in (a.pos - com)) for a in atoms)) rg = np.sqrt(_sum / natoms) yield '{0:10.0f}{1:15.6f}\n'.format(ts.time, rg) # per 100 frames, num of frames changes with the size of xtc file, for debugging if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format( ts.time, ts.step, ts.frame)
def sequence_spacing(grof, xtcf, btime, etime, peptide_length, atom_sel): u = Universe(grof, xtcf) # this selection part should be better customized # here, only have been backbone atoms are used, u.selectAtoms doesn't # include Hydrogen atoms # REMMEMBER: ARGS verification should be done in main ONLY! # range works like this: # in MDAnalysis, resid starts from 1, in sequence_spacing.py, we don't count # the C- and N- termini, so it's from 2 to peptide_len+2 residues = [ u.selectAtoms(atom_sel.format(i)) for i in range(2, peptide_length + 2) ] ijdist_dict = {} for ts in u.trajectory: # btime, etime defaults to 0, if etime is 0, loop till the end of the # trajectory if btime > ts.time: continue if etime > 0 and etime < ts.time: break # the good stuff for i, resi in enumerate(residues): for j, resj in enumerate(residues): # to remove duplicate since resi & resj are within the same peptide if i < j: dij = abs(i - j) d_atomi_atomj = [] # loop through every atom in both residues for atomi in resi: for atomj in resj: d_atomi_atomj.append( np.linalg.norm(atomi.pos - atomj.pos)) # add the result to the dictionary ij_dist = np.average( d_atomi_atomj) # distance between i and j if dij not in ijdist_dict.keys(): ijdist_dict[dij] = [ij_dist] else: ijdist_dict[dij].append(ij_dist) utils.print_progress(ts) return ijdist_dict
def yN_chi(prmfile, trjfile, pro_name): ''' return the chi1, chi2 of the conserved yN residue ''' univ = Universe(prmfile, trjfile) frames = np.empty((univ.trajectory.numframes, )) Dih_yN_chi1 = np.empty((univ.trajectory.numframes, )) Dih_yN_chi2 = np.empty((univ.trajectory.numframes, )) if univ.residues[ int(pro_conser_marks(pro_name)['Yn'])-univ.residues[0].resnum+1 ].name not \ in [ 'ASN', 'TYR', 'THR']: return (None, None, None) i_res = int(pro_conser_marks(pro_name)['Yn']) + 1 if univ.residues[int(pro_conser_marks(pro_name)['Yn']) - univ.residues[0].resnum + 1].name == 'ASN': yN_chi1 = univ.selectAtoms('resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name CG') yN_chi2 = univ.selectAtoms('resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name CG', 'resnum ' + str(i_res) + ' and name OD1') elif univ.residues[int(pro_conser_marks(pro_name)['Yn']) - univ.residues[0].resnum + 1].name == 'TYR': yN_chi1 = univ.selectAtoms('resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name CG') yN_chi2 = univ.selectAtoms('resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name OH', 'resnum ' + str(i_res) + ' and name HH') elif univ.residues[int(pro_conser_marks(pro_name)['Yn']) - univ.residues[0].resnum + 1].name == 'THR': yN_chi1 = univ.selectAtoms('resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name OG1') yN_chi2 = univ.selectAtoms('resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name OG1', 'resnum ' + str(i_res) + ' and name HG1') index = 0 for ts in univ.trajectory: frames[index] = ts.frame Dih_yN_chi1[index] = yN_chi1.dihedral() Dih_yN_chi2[index] = yN_chi2.dihedral() index += 1 return (frames, Dih_yN_chi1, Dih_yN_chi2)
def sequence_spacing(grof, xtcf, btime, etime, peptide_length, atom_sel): u = Universe(grof, xtcf) # this selection part should be better customized # here, only have been backbone atoms are used, u.selectAtoms doesn't # include Hydrogen atoms # REMMEMBER: ARGS verification should be done in main ONLY! # range works like this: # in MDAnalysis, resid starts from 1, in sequence_spacing.py, we don't count # the C- and N- termini, so it's from 2 to peptide_len+2 residues = [u.selectAtoms(atom_sel.format(i)) for i in range(2, peptide_length + 2)] ijdist_dict = {} for ts in u.trajectory: # btime, etime defaults to 0, if etime is 0, loop till the end of the # trajectory if btime > ts.time: continue if etime > 0 and etime < ts.time: break # the good stuff for i, resi in enumerate(residues): for j, resj in enumerate(residues): # to remove duplicate since resi & resj are within the same peptide if i < j: dij = abs(i - j) d_atomi_atomj = [] # loop through every atom in both residues for atomi in resi: for atomj in resj: d_atomi_atomj.append( np.linalg.norm(atomi.pos - atomj.pos)) # add the result to the dictionary ij_dist = np.average(d_atomi_atomj) # distance between i and j if dij not in ijdist_dict.keys(): ijdist_dict[dij] = [ij_dist] else: ijdist_dict[dij].append(ij_dist) utils.print_progress(ts) return ijdist_dict
def getWaterCoorWithH(self,centre,psf,dcd,outputFile): rho=Universe(psf,dcd) H2OCoordinate=[] no=0 title='resname'+' '+'atomid'+' '+'resnumber'+' X Y Z '+' '+'segname'+' '+'frameNo'+' '+'centreNo'+'\n' outputFile.write(title) for oxygenInforSet in self: H2OCoordinateSet=[] print 'There were',len(oxygenInforSet),'waters in the' for oxygenInfor in oxygenInforSet: ## no1+=1 ## print no1 frameNo=oxygenInfor[-2] frameNo=int(frameNo)-1 segName=oxygenInfor[-3] resNumber=oxygenInfor[2] frame=rho.trajectory[frameNo] infor='segid '+segName+' and resid '+resNumber selected=rho.selectAtoms(infor) atomID=[] for atoms in selected.atoms: ID=str(atoms).split()[2][:-1] atomID.append(ID) selectedResId=selected.resids() selectedResNa=selected.resnames() coordsOH1H2=selected.coordinates() for i in range(3): atomInfor=str(selectedResNa[0])+' '+str(atomID[i])+' '+str(resNumber)+' '+str(coordsOH1H2[i])[1:-1]+' '+segName+' '+str(frameNo)+' '+str(no)+'\n' outputFile.write(atomInfor) H2OCoordinateSet.append(coordsOH1H2) no+=1 H2OCoordinate.append(H2OCoordinateSet) print no,'is finished' outputFile.close() return H2OCoordinate
def main(): arg_parser = argparse.ArgumentParser(description='通过给定残基名称,残基内原子数目,原子在残基内的索引(从0开始),计算原子的坐标。') arg_parser.add_argument('resname', action='store', help='残基名称') arg_parser.add_argument('atoms_num', type=int, action='store', help='残基内原子数目') arg_parser.add_argument('index', type=int, action='store', help='原子的索引,索引从0开始') arg_parser.add_argument('topology_file', action='store', help='拓扑文件,例如gro, pdb') args = arg_parser.parse_args() resname, atoms_num, index = args.resname, args.atoms_num, args.index universe = Universe(args.topology_file) atom_groups = universe.selectAtoms("resname " + resname) if len(atom_groups) % atoms_num != 0: print("拓扑文件内对应残基原子总数不是所给原子数目的整数倍,请给予正确的原子数目。") exit(1) positions = [] for i in range(0, len(atom_groups), atoms_num): positions.append(atom_groups[i:i + atoms_num][index].position) print("The positions of atoms %s is:" % (index)) for i in positions: print(i)
def Tyr_CaCbOhHh(prmfile, trjfile, pro_name): ''' return the dihedral angle of Yn residue: Ca-Cb__Oh-Hh ''' univ = Universe(prmfile, trjfile) frames = np.empty((univ.trajectory.numframes, )) Dih_Tyr_CaCbOhHh = np.empty((univ.trajectory.numframes, )) if univ.residues[int(pro_conser_marks(pro_name)['pDy']) + 1 - univ.residues[0].resnum].name != 'TYR': return (None, None) i_res = int(pro_conser_marks(pro_name)['pDy']) + 1 CaCbOhHh = univ.selectAtoms('resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name OH', 'resnum ' + str(i_res) + ' and name HH') index = 0 for ts in univ.trajectory: frames[index] = ts.frame Dih_Tyr_CaCbOhHh[index] = CaCbOhHh.dihedral() index += 1 return (frames, Dih_Tyr_CaCbOhHh)
def count_interactions(grof, xtcf, btime, cutoff, debug): u = Universe(grof, xtcf) query = ('(resname PRO and (name CB or name CG or name CD)) or' '(resname VAL and (name CG1 or name CG2)) or' '(resname GLY and name CA) or' '(resname ALA and name CB)') # MDAnalysis will convert the unit of length to angstrom, though in Gromacs the unit is nm atoms = u.selectAtoms(query) for ts in u.trajectory: if ts.time >= btime: numcount = 0 for i, ai in enumerate(atoms): for j, aj in enumerate(atoms): # to avoid counting the same pair twices, # the 2 resid cannot be neigbors if i < j and abs(ai.resid - aj.resid) >= 2: d = np.linalg.norm(ai.pos - aj.pos) if d <= cutoff: numcount += 1 yield '{0:10.0f}{1:8d}\n'.format(ts.time, numcount) # per 100 frames, num of frames changes with the size of xtc file, for debugging if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format(ts.time, ts.step, ts.frame)
def sequence_spacing(pf, grof, xtcf, peptide_length, atom_sel, output=None): u = Universe(grof, xtcf) # this selection part should be better customized # here, only have been backbone atoms are used, u.selectAtoms doesn't # include Hydrogen atoms # REMMEMBER: OPTIONS verification should be done in main ONLY! residues = [u.selectAtoms(atom_sel.format(i)) for i in range(2, peptide_length)] ijdist_dict = {} for ts in u.trajectory: for i, resi in enumerate(residues): for j, resj in enumerate(residues): if i < j: resi_pos = resi.centerOfGeometry() # residue i position resj_pos = resj.centerOfGeometry() # residue j position ijdist = np.linalg.norm(resi_pos - resj_pos) dij = j - i # distance between i and j if dij not in ijdist_dict.keys(): ijdist_dict[dij] = [dij] else: ijdist_dict[dij].append(ijdist) if ts.step % 2000000 == 0: # 2000ps print "time step: {0:d}".format(ts.step) return ijdist_dict
# NMP: centers of geometry of the backbone and C-beta atoms in residues 115-125 # (CORE-LID), 90-100 (CORE), and 35-55 (NMP) # LID: 179-185 (CORE), 115-125 (CORE-hinge-LID), and 125-153 (LID) strct_dir = adk + "/structures/" trj_dir = adk + "/trj/" trjraw_dir = trj_dir + "raw/001/" trjfit_dir = trj_dir + "fit/" ref_clsd = Universe(strct_dir+"adk1AKE_nw.pdb") ref_open = Universe(strct_dir+"adk4AKE_nw.pdb") u = Universe(strct_dir+"adk4AKE_nw.pdb", trjraw_dir+adk+"_nw_1-20_all.dcd") bb = u.selectAtoms("backbone") # Theta NMP: 115-125 (CORE-hinge-LID), 90-100 (CORE), and 35-55 (NMP) core_lid = bb.selectAtoms("resid 115-125") core1 = bb.selectAtoms("resid 90-100") nmp = bb.selectAtoms("resid 35-55") # Theta LID: 179-185 (CORE), 115-125 (CORE-hinge-LID), and 125-153 (LID) core2 = bb.selectAtoms("resid 179-185") core_lid = bb.selectAtoms("resid 115-125") lid = bb.selectAtoms("resid 125-153") def vsqnorm(v, axis=0): return np.sum(v*v, axis=axis) def computeangle(v1, v2):
def __init__( self, psf, pdb, delta=1.0, atomselection="name OH2", metadata=None, padding=4.0, sigma=None, verbosity=3 ): """Construct the density from psf and pdb and the atomselection. DC = BfactorDensityCreator(psf, pdb, delta=<delta>, atomselection=<MDAnalysis selection>, metadata=<dict>, padding=2, sigma=None) density = DC.PDBDensity() psf Charmm psf topology file pdb PDB file atomselection selection string (MDAnalysis syntax) for the species to be analyzed delta approximate bin size for the density grid (same in x,y,z) (It is slightly adjusted when the box length is not an integer multiple of delta.) metadata dictionary of additional data to be saved with the object padding increase histogram dimensions by padding (on top of initial box size) sigma width (in Angstrom) of the gaussians that are used to build up the density; if None then uses B-factors from pdb verbosity=int level of chattiness; 0 is silent, 3 is verbose For assigning X-ray waters to MD densities one might have to use a sigma of about 0.5 A to obtain a well-defined and resolved x-ray water density that can be easily matched to a broader density distribution. """ from MDAnalysis import Universe set_verbosity(verbosity) # set to 0 for no messages u = Universe(psf, pdbfilename=pdb) group = u.selectAtoms(atomselection) coord = group.coordinates() logger.info( "BfactorDensityCreator: Selected %d atoms (%s) out of %d total.", coord.shape[0], atomselection, len(u.atoms), ) smin = numpy.min(coord, axis=0) - padding smax = numpy.max(coord, axis=0) + padding BINS = fixedwidth_bins(delta, smin, smax) arange = zip(BINS["min"], BINS["max"]) bins = BINS["Nbins"] # get edges by doing a fake run grid, self.edges = numpy.histogramdd(numpy.zeros((1, 3)), bins=bins, range=arange, normed=False) self.delta = numpy.diag(map(lambda e: (e[-1] - e[0]) / (len(e) - 1), self.edges)) self.midpoints = map(lambda e: 0.5 * (e[:-1] + e[1:]), self.edges) self.origin = map(lambda m: m[0], self.midpoints) numframes = 1 if sigma is None: # histogram individually, and smear out at the same time # with the appropriate B-factor if numpy.any(group.bfactors == 0.0): wmsg = "BfactorDensityCreator: Some B-factors are Zero." warnings.warn(wmsg, category=hop.MissingDataWarning) logger.warn(wmsg) rmsf = Bfactor2RMSF(group.bfactors) grid *= 0.0 # reset grid self.g = self._smear_rmsf(coord, grid, self.edges, rmsf) else: # histogram 'delta functions' grid, self.edges = numpy.histogramdd(coord, bins=bins, range=arange, normed=False) logger.info("Histogrammed %6d atoms from pdb.", len(group.atoms)) # just a convolution of the density with a Gaussian self.g = self._smear_sigma(grid, sigma) try: metadata["psf"] = psf except TypeError: metadata = dict(psf=psf) metadata["pdb"] = pdb metadata["atomselection"] = atomselection metadata["numframes"] = numframes metadata["sigma"] = sigma self.metadata = metadata # Density automatically converts histogram to density for isDensity=False logger.info("BfactorDensityCreator: Histogram completed (initial density in Angstrom**-3)\n")
ax.set_title(title) ppl.bar(bin_edges[:-1], hist, width = 1, xticklabels = histogram_bin, annotate = True) fig.savefig(title_of_file) ########################################## # Load a universe, i.e. an object that contains the topology and all the # available coordinates: u = Universe(topology,intraj) # Where are the residues we are interested in in the Universe ? center_in_file = u.selectAtoms(center) residue_in_file = u.selectAtoms(residue_to_find) # Now, we build an histogram # - hydration_number_histogram_bin contains the possible values for the # hydration number. # - hydration_number stores all the values along the trajectory histogram_bin = [] hydration_number = np.array([]) num_frame = 0 # File where we write the hydration number hydration_file = open(file_title,"w") # On each frame, we find the residues around the center. # Then, append the array containing the number of residues aroud the center.
prefix = '/nfs/homes/sseyler/Dropbox/Beckstein/data' prot = '/AdK' ofp = basedir + '/analysis' ofn1 = ofp + '/' + 'q1-1ake_' ofn2 = ofp + '/' + 'q2-4ake_' ext1 = '.dat' cutoff = 8.0 # in Angstroms #------------------------------------------------ # Setup reference structures #------------------------------------------------ reffpath = prefix + prot + '/boundary_conformations' ref_c = Universe(reffpath + '/1AKE_A.pdb') ref_o = Universe(reffpath + '/4AKE_A.pdb') atomsel = 'name CA' # change to 'name CA and segid A' for top files with chains ca_c = ref_c.selectAtoms(atomsel) ca_o = ref_o.selectAtoms(atomsel) # Reference coordinates ref_o.atoms.translate(-ref_o.atoms.centerOfMass()) ref_o_coor = ref_o.atoms.CA.coordinates() # Process a single trajectory for q1q2 analysis if False: u = Universe(top, basedir + 'adk_eq_langevin_' + struct + '.dcd') # u = Universe(filename, multiframe=True) # Excruciatingly slow LoL CA1 = ContactAnalysis1(u, selection=atomsel,refgroup=ca_c, \ radius=cutoff,outfile=ofn1+ext1) CA2 = ContactAnalysis1(u, selection=atomsel,refgroup=ca_o, \ radius=cutoff,outfile=ofn2+ext1) CA1.run(force=True) CA2.run(force=True)
#print pro_marks; pro_names = pro_marks.keys() #pro_ = os.getcwd().split('/')[-1] #pro = pro_.split('_run')[0]; pro_ = sys.argv[1] pro = pro_.split('.')[0] print pro.ljust(7), prmfile = sys.argv[1] trjfile = sys.argv[2] univ = Universe(prmfile, trjfile) mark1 = univ.selectAtoms('resid ' + pro_marks[pro][0] + ' and name O')[0] # resi wPf and atom name O mark2 = univ.selectAtoms('resid ' + str(int(pro_marks[pro][1]) - 2) + ' and name O')[0] # resi preP and atom name O mark3 = univ.selectAtoms('resid ' + str(int(pro_marks[pro][4]) + 1) + ' and name OH')[0] # resi pdY and name OH mark4 = univ.selectAtoms('resid ' + pro_marks[pro][5] + ' and name O')[0] mark5 = univ.selectAtoms('resid ' + pro_marks[pro][5] + ' and name N')[0] # resi pMd and name O, N mark6 = univ.selectAtoms('resid ' + pro_marks[pro][8] + ' and name O')[0] # resi V1 Valine O mark7 = univ.selectAtoms('resid ' + pro_marks[pro][9] + ' and name ND2')[0] # resi N1 and name ND2
if len(iwords) == 13: pro_marks[iwords[0]] = iwords[1:] #print pro_marks; pro_names = pro_marks.keys() pro_ = os.getcwd().split('/')[-1] pro = pro_.split('_run')[0] print pro.ljust(7), prmfile = sys.argv[1] trjfile = sys.argv[2] univ = Universe(prmfile, trjfile) mark1 = univ.selectAtoms('resid ' + str(int(pro_marks[pro][4]) + 1) + ' and name OH')[0] # resi pDy and atom name O mark2 = univ.selectAtoms('resid ' + pro_marks[pro][3] + ' and name CA')[0] # resi I94 and atom name O mark3 = univ.selectAtoms('resid ' + pro_marks[pro][2] + ' and name CA')[0] # resi L92 and name OH mark4 = univ.selectAtoms('resid ' + pro_marks[pro][1] + ' and name CA')[0] # resi pVd and name O mark5 = univ.selectAtoms('resid ' + pro_marks[pro][10] + ' and name CA')[0] # resi Yn Valine O mark6 = univ.selectAtoms('resid ' + pro_marks[pro][11] + ' and name CA')[0] # resi HC1 and name ND2 mark7 = univ.selectAtoms('resid ' + pro_marks[pro][0] + ' and name CA')[0] # resi wPf and name O #print pro_marks[pro][0], str( int( pro_marks[pro][1] )-2 ), str( int( pro_marks[pro][2] )+1 ), pro_marks[pro][3] , \
def calc_bond_length(grof, xtcf, btime, etime, debug): # thebonds contains all the bonds that I am interested thebonds = { #atom names should be UNIQUE within each residue for this script 'BACKBONE_INTRA': [('N', 'CA'), ('CA', 'C'), ('C', 'O'), ], # backbone, intramolecular interactions # PB: peptide bond, which is the only intermolecular bonds that I am interested 'PB': [('C', 'N'),], 'GLY': [('CA', 'HA1'),], 'PRO': [('CA', 'CB'), ('CB', 'CG'), ('CG', 'CD'), ('CD', 'N' )], 'VAL': [('CA', 'CB'), ('CB', 'CG1'), ('CB', 'CG2')], 'MeO': [('C', 'OA'), ('C', 'H'), ('OA', 'HO')], 'SOL': [('OW', 'HW1')], } aas = ['GLY', 'PRO', 'VAL'] # rl: residue list solvents = ['MeO', 'SOL'] # initialize ibonds ibonds = {} # interested bonds, not very legible to human for k in thebonds: ibonds[k] = {} if k in aas: for kk in thebonds[k] + thebonds['BACKBONE_INTRA']: ibonds[k][tuple(sorted(kk))] = [] elif k in solvents: for kk in thebonds[k]: ibonds[k][tuple(sorted(kk))] = [] ibonds['PB'] = {} ibonds['PB'][('C', 'N')] = [] # data structure would be (to do) # ibonds = { # 'PRO': { # (a1, b1):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # (a2, b2):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # ... # }, # 'VAL': { # (a1, b1):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # (a2, b2):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # ... # }, # 'GLY': { # (a1, b1):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # (a2, b2):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # ... # }, # } univer = Universe(grof, xtcf) atom_selection = "not resname ACE and not resname NH2" # get rid of the ends # atom_selection = "resname MeO and resid 3000" atoms = univer.selectAtoms(atom_selection) # initialize ibonds data structure # a bondname is composed of readable plain text # a bond is composed of Atom object for ki, ai in enumerate(atoms): for kj, aj in enumerate(atoms): if ki < kj: if ai.resid == aj.resid: # collecting intramolecular bonds associated with real atom objects resname = ai.resname # will also equal aj.resname bondname = tuple(sorted([ai.name, aj.name])) if bondname in ibonds[resname]: bond = [ai, aj] ibonds[resname][bondname].append(bond) elif ai.resid - aj.resid == -1: # collecting itermolecular bonds: i.e. peptide bond bondname = tuple([ai.name, aj.name]) if bondname == ('C', 'N'): bond = [ai, aj] ibonds['PB'][bondname].append(bond) ################################################################################ # VERIFICATION STATUS: ibonds initiation verified for # sq1w00_md.gro & sq1m00_md.gro # 2012-04-25 # for i in ibonds: # for j in ibonds[i]: # print i, j, len(ibonds[i][j]) # from pprint import pprint as pp # pp(ibonds) # VAL ('CB', 'CG2') 14 # VAL ('C', 'CA') 14 # VAL ('CA', 'N') 14 # VAL ('CB', 'CG1') 14 # VAL ('C', 'O') 14 # VAL ('CA', 'CB') 14 # PRO ('CD', 'CG') 7 # PRO ('C', 'CA') 7 # PRO ('CA', 'N') 7 # PRO ('CA', 'CB') 7 # PRO ('C', 'O') 7 # PRO ('CD', 'N') 7 # PRO ('CB', 'CG') 7 # SOL ('HW1', 'OW') 0 # PB ('C', 'N') 34 # GLY ('CA', 'N') 14 # GLY ('C', 'O') 14 # GLY ('CA', 'HA1') 14 # GLY ('C', 'CA') 14 # MeO ('HO', 'OA') 0 # MeO ('C', 'OA') 0 # MeO ('C', 'H') 0 # import sys # sys.exit() ################################################################################ # Just for Printing the Header sorted_resname = sorted( ibonds.keys()) # sort to keep the value in the right order partial_header = [] for resname in sorted_resname: resname_header = [] # the header specific to residue for bondname in sorted(ibonds[resname].keys()): # bn: since bondname has been used in previous codes bn = '{0}|{1}'.format(resname[0], '-'.join(bondname)) resname_header.append('{0:9s}'.format(bn)) partial_header.extend(resname_header) yield '#{0:8s}{1}\n'.format('t(ps)', ''.join(partial_header)) # import sys # sys.exit() # Production Calculation # use < when for formatting values to align with headers, and the width will # be 1 col narrower than that in the corresponding header for ts in univer.trajectory: # for debugging only if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format( ts.time, ts.step, ts.frame) if etime > ts.time >= btime: partial_yield = [] for resname in sorted_resname: resname_yield = [] for bondname in sorted(ibonds[resname].keys()): bonds = ibonds[resname][bondname] ds = [] for bond in bonds: r = bond[0].pos - bond[ 1].pos # end-to-end vector from atom positions d = np.linalg.norm(r) # distance ds.append(d) resname_yield.append('{0:<8.3f}'.format( np.average(ds))) #, np.std(ds)) partial_yield.extend(resname_yield) # a space in order to align with # in the header yield ' {0:<8.0f}{1}\n'.format(ts.time, ' '.join(partial_yield))
trj_psf='GSBPsetup/ifabp_apo_gsbp_15_0.psf', trj_pdb='GSBPsetup/ifabp_apo_gsbp_15_0.pdb', ), outputfiles=dict(fit_pdb='GSBPsetup/rmsfit_ifabp_apo_gsbp_15_0.pdb', )) job.stage() from MDAnalysis import Universe import hop.trajectory print "Setting up the Universes..." ref = Universe(job.filenames['ref_psf'],pdbfilename=job.filenames['ref_pdb']) trj = Universe(job.filenames['trj_psf'],job.filenames['trj_pdb']) ref_resids = [a.resid for a in ref.selectAtoms('name CA')] target_resids = [a.resid for a in trj.selectAtoms('name CA')] print "Alignment and selection string..." selection = hop.trajectory.fasta2select(job.filenames['sequence'], ref_resids=ref_resids,target_resids=target_resids, is_aligned=True) print "Fitting trajectory to reference..." hop.trajectory.RMS_fit_trj(trj,ref, select=selection, filename=job.filenames['fit_pdb']) print "Done: result is '%(fit_pdb)s'" % job.filenames job.unstage() job.cleanup()
def test_write_read(self): u = Universe(altloc) u.selectAtoms("all").write(self.outfile) u2 = Universe(self.outfile) assert_equal(len(u.atoms), len(u2.atoms))
inp = { 'rmsfit': os.path.join(os.environ['CHARMM'], 'analysis', 'trajectory', 'rmsfit.inp'), } inp.update(executables) inp.update(job.filenames) # 1. build reference frame from alignment #------------------------------------------------------------ from MDAnalysis import Universe import hop.trajectory print "Setting up the Universes..." ref = Universe(job.filenames['ref_psf'], pdbfilename=job.filenames['ref_pdb']) trj = Universe(job.filenames['trj_psf'], job.filenames['trj_pdb']) ref_resids = [a.resid for a in ref.selectAtoms('name CA')] target_resids = [a.resid for a in trj.selectAtoms('name CA')] print "Alignment and selection string..." selection = hop.trajectory.fasta2select(job.filenames['sequence'], ref_resids=ref_resids, target_resids=target_resids, is_aligned=True) print "Fitting trajectory to reference..." hop.trajectory.RMS_fit_trj(trj, ref, select=selection, filename=job.filenames['REF_PDB']) print "Done: result is '%(REF_PDB)s'" % job.filenames #------------------------------------------------------------ # 2. orient (RMS-fit)
import sys sys.path.append('/home/x/xiansu/pfs/program/numpy/lib/python2.6/site-packages') from MDAnalysis import Universe, Writer from MDAnalysis.analysis.distances import distance_array import MDAnalysis import numpy from Numeric import * top='npt.gro' traj='md_extract1.trr' water=Universe(top,traj) o=water.selectAtoms('name O*') resid=o.resids() print resid #resnu=o.resnums() #resna=o.resnames() atomInf=[] for i in o.atoms: atomid= str(i).split()[2] atomseg=str(i).split()[-1] atomidandseg=[] atomidandseg.append(atomid) atomidandseg.append(atomseg) atomInf.append(atomidandseg)
[ molecules ] {topology} """ include, topology = [], [] if True: topology.append(("Protein_A", 1)) include.append("Protein_A.itp") if True: topology.append(("Protein_B", 1)) include.append("Protein_B.itp") gr = u.selectAtoms("resname POPC and name PO4") if len(gr): topology.append(("POPC", len(gr))) gr = u.selectAtoms("resname W") if len(gr): topology.append(("W", len(gr))) include_str = ['#include "./{0}"'.format(i) for i in include] topology_str = ["{0}\t{1}".format(name, count) for name, count in topology] print template.format(**{"topology": "\n".join(topology_str), "include": "\n".join(include_str)})
pro_marks[ iwords[0] ] = iwords[1:] pro_names = pro_marks.keys() #pro_ = os.getcwd().split('/')[-1] pro_name_ = sys.argv[1]; pro_name = pro_name_.split('.')[0]; prmfile = sys.argv[1]; trjfile = sys.argv[2]; univ = Universe( prmfile, trjfile ); # define the dihedral i_res = int(pro_marks[pro_name][4]) + 1 pdY_phi = univ.selectAtoms( 'resnum ' + str(i_res-1) + ' and name C', 'resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name C'); #C-1-N-CA-C pdY_psi = univ.selectAtoms( 'resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name C', 'resnum ' + str(i_res+1) + ' and name N'); #N-CA-C-N+1 pdY_O = univ.selectAtoms( 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name OH', 'resnum ' + str(i_res) + ' and name HH' ); #CA-CB-OH-HH #pdY_chi2 = univ.selectAtoms( 'resnum ' + str(i_res) + ' and name CA', # 'resnum ' + str(i_res) + ' and name CB', # 'resnum ' + str(i_res) + ' and name CG', # 'resnum ' + str(i_res) + ' and name CD1'); #CA-CB-CG-CD1 print i_res, pdY_psi[0].resname;
if len(iwords) == 13: pro_marks[ iwords[0] ] = iwords[1:] #print pro_marks; pro_names = pro_marks.keys() pro_ = os.getcwd().split('/')[-1] pro = pro_.split('_run')[0]; print pro.ljust(7), prmfile = sys.argv[1]; trjfile = sys.argv[2]; univ = Universe( prmfile, trjfile ); mark1 = univ.selectAtoms( 'resid ' + pro_marks[pro][4] + ' and name CA' )[0]; # resi wPf and atom name O mark2 = univ.selectAtoms( 'resid ' + pro_marks[pro][3] + ' and name CA' )[0]; # resi pVd-2 and atom name O mark3 = univ.selectAtoms( 'resid ' + pro_marks[pro][2] + ' and name CA')[0]; # resi pdY and name OH mark4 = univ.selectAtoms( 'resid ' + pro_marks[pro][1] + ' and name CA' )[0]; # resi pMd and name O mark5 = univ.selectAtoms( 'resid ' + pro_marks[pro][10] + ' and name CA' )[0]; # resi N1-3 Valine O mark6 = univ.selectAtoms( 'resid ' + pro_marks[pro][11] + ' and name CA' )[0]; # resi N1 and name ND2 mark7 = univ.selectAtoms( 'resid ' + pro_marks[pro][0] + ' and name CA' )[0]; # resi N1 and name O #print pro_marks[pro][0], str( int( pro_marks[pro][1] )-2 ), str( int( pro_marks[pro][2] )+1 ), pro_marks[pro][3] , \
import os sys.path.append('/share/udata1/xiaoxiao/BRD_Ana_15.4.22/scripts/') from pro_conser_marks import pro_conser_marks pro_name_ = os.getcwd().split('/')[-1] pro_name = pro_name_.split('_run')[0] print 'pro_name', pro_name prmfile = sys.argv[1] trjfile = sys.argv[2] univ = Universe(prmfile, trjfile) # define the dihedral i_res = int(pro_conser_marks(pro_name)['pDy']) + 1 pdY_phi = univ.selectAtoms('resnum ' + str(i_res - 1) + ' and name C', 'resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name C') #C-1-N-CA-C pdY_psi = univ.selectAtoms('resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name C', 'resnum ' + str(i_res + 1) + ' and name N') #N-CA-C-N+1 pdY_O = univ.selectAtoms('resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB', 'resnum ' + str(i_res) + ' and name OH', 'resnum ' + str(i_res) + ' and name HH') #CA-CB-OH-HH pdY_chi = univ.selectAtoms('resnum ' + str(i_res) + ' and name N', 'resnum ' + str(i_res) + ' and name CA', 'resnum ' + str(i_res) + ' and name CB',
import numpy from MDAnalysis import Universe, collection, Timeseries from MDAnalysis.tests.datafiles import PSF, DCD try: import matplotlib matplotlib.use('agg') # no interactive plotting, only save figures from pylab import errorbar, legend, xlabel, ylabel, savefig, clf, gca, draw have_matplotlib = True except ImportError: have_matplotlib = False universe = Universe(PSF, DCD) protein = universe.selectAtoms("protein") numresidues = protein.numberOfResidues() collection.clear() for res in range(2, numresidues-1): print "Processing residue %d" % res # selection of the atoms involved for the phi for resid '%d' %res ## selectAtoms("atom 4AKE %d C"%(res-1), "atom 4AKE %d N"%res, "atom %d 4AKE CA"%res, "atom 4AKE %d C" % res) phi_sel = universe.residues[res].phi_selection() print phi_sel; # selection of the atoms involved for the psi for resid '%d' %res psi_sel = universe.residues[res].psi_selection() print psi_sel; # collect the timeseries of a dihedral collection.addTimeseries(Timeseries.Dihedral(phi_sel))
DCD = sys.argv[2] FILENAME=sys.argv[3] #TOP = '/Users/ronaldholt/1JJS_autopsf.psf' #DCD = '/Users/ronaldholt/Google_Drive/ORNL_Research/1JJS/1JJS_1us.dcd' #FILENAME="1JJS" u =Universe(TOP,DCD) # Extract position of N and C terminal can calculate distace, write it to a file along with the radius of gyration (RG) f=open(str(FILENAME) +'Rg_data.txt','w') nterm = u.P1.N[0] # can access structure via segid (s4AKE) and atom name cterm = u.P1.C[-1] # ... takes the last atom named 'C' bb = u.selectAtoms('protein and backbone') # a selection (a AtomGroup) for ts in u.trajectory: # iterate through all frames r = cterm.pos - nterm.pos # end-to-end vector from atom positions d = numpy.linalg.norm(r) # end-to-end distance rgyr = bb.radiusOfGyration() # method of a AtomGroup; updates with each frame print >>f, " %d %f %f " % (ts.frame, d, rgyr) f.close() #Extract distance of N to C terminal and RG overtrajectory x, y,z = np.loadtxt(str(FILENAME)+'Rg_data.txt', usecols=(0,1,2),unpack=True) Xnew = np.arange(1,50000,500) xnew = np.arange(1,50000,100) f = interpolate.interp1d(x,y) #smooth the line F = interpolate.interp1d(x,z) #smooth the line fig=plt.figure(figsize=(8,8),dpi=80,facecolor='w',edgecolor='k') plt.subplot(211)
linkage = 'ward' # 'single' 'complete' 'weighted' 'average' plotname = 'df_ward_psa-full.pdf' import numpy from MDAnalysis import Universe from MDAnalysis.analysis.align import rotation_matrix from MDAnalysis.analysis.psa import PSA if __name__ == '__main__': print("Generating AdK CORE C-alpha reference coordinates and structure...") # Read in closed/open AdK structures; work with C-alphas only u_closed = Universe('../structs/adk1AKE.pdb') u_open = Universe('../structs/adk4AKE.pdb') ca_closed = u_closed.selectAtoms('name CA') ca_open = u_open.selectAtoms('name CA') # Move centers-of-mass of C-alphas of each structure's CORE domain to origin adkCORE_resids = "(resid 1:29 or resid 60:121 or resid 160:214)" u_closed.atoms.translate(-ca_closed.selectAtoms(adkCORE_resids).centerOfMass()) u_open.atoms.translate(-ca_open.selectAtoms(adkCORE_resids).centerOfMass()) # Get C-alpha CORE coordinates for each structure closed_ca_core_coords = ca_closed.selectAtoms(adkCORE_resids).positions open_ca_core_coords = ca_open.selectAtoms(adkCORE_resids).positions # Compute rotation matrix, R, that minimizes rmsd between the C-alpha COREs R, rmsd_value = rotation_matrix(open_ca_core_coords, closed_ca_core_coords) # Rotate open structure to align its C-alpha CORE to closed structure's
def calc_bond_length(grof, xtcf, btime, etime, debug): # thebonds contains all the bonds that I am interested thebonds = { #atom names should be UNIQUE within each residue for this script 'BACKBONE_INTRA': [('N', 'CA'), ('CA', 'C'), ('C', 'O'), ], # backbone, intramolecular interactions # PB: peptide bond, which is the only intermolecular bonds that I am interested 'PB': [('C', 'N'),], 'GLY': [('CA', 'HA1'),], 'PRO': [('CA', 'CB'), ('CB', 'CG'), ('CG', 'CD'), ('CD', 'N' )], 'VAL': [('CA', 'CB'), ('CB', 'CG1'), ('CB', 'CG2')], 'MeO': [('C', 'OA'), ('C', 'H'), ('OA', 'HO')], 'SOL': [('OW', 'HW1')], } aas = ['GLY', 'PRO', 'VAL'] # rl: residue list solvents = ['MeO', 'SOL'] # initialize ibonds ibonds = {} # interested bonds, not very legible to human for k in thebonds: ibonds[k] = {} if k in aas: for kk in thebonds[k] + thebonds['BACKBONE_INTRA']: ibonds[k][tuple(sorted(kk))] = [] elif k in solvents: for kk in thebonds[k]: ibonds[k][tuple(sorted(kk))] = [] ibonds['PB'] = {} ibonds['PB'][('C', 'N')] = [] # data structure would be (to do) # ibonds = { # 'PRO': { # (a1, b1):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # (a2, b2):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # ... # }, # 'VAL': { # (a1, b1):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # (a2, b2):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # ... # }, # 'GLY': { # (a1, b1):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # (a2, b2):[(atom_object1, atom_object2), (atom_object3, atom_object4), ... , ], # ... # }, # } univer = Universe(grof, xtcf) atom_selection = "not resname ACE and not resname NH2" # get rid of the ends # atom_selection = "resname MeO and resid 3000" atoms = univer.selectAtoms(atom_selection) # initialize ibonds data structure # a bondname is composed of readable plain text # a bond is composed of Atom object for ki, ai in enumerate(atoms): for kj, aj in enumerate(atoms): if ki < kj: if ai.resid == aj.resid: # collecting intramolecular bonds associated with real atom objects resname= ai.resname # will also equal aj.resname bondname = tuple(sorted([ai.name, aj.name])) if bondname in ibonds[resname]: bond = [ai, aj] ibonds[resname][bondname].append(bond) elif ai.resid - aj.resid == -1: # collecting itermolecular bonds: i.e. peptide bond bondname = tuple([ai.name, aj.name]) if bondname == ('C', 'N'): bond = [ai, aj] ibonds['PB'][bondname].append(bond) ################################################################################ # VERIFICATION STATUS: ibonds initiation verified for # sq1w00_md.gro & sq1m00_md.gro # 2012-04-25 # for i in ibonds: # for j in ibonds[i]: # print i, j, len(ibonds[i][j]) # from pprint import pprint as pp # pp(ibonds) # VAL ('CB', 'CG2') 14 # VAL ('C', 'CA') 14 # VAL ('CA', 'N') 14 # VAL ('CB', 'CG1') 14 # VAL ('C', 'O') 14 # VAL ('CA', 'CB') 14 # PRO ('CD', 'CG') 7 # PRO ('C', 'CA') 7 # PRO ('CA', 'N') 7 # PRO ('CA', 'CB') 7 # PRO ('C', 'O') 7 # PRO ('CD', 'N') 7 # PRO ('CB', 'CG') 7 # SOL ('HW1', 'OW') 0 # PB ('C', 'N') 34 # GLY ('CA', 'N') 14 # GLY ('C', 'O') 14 # GLY ('CA', 'HA1') 14 # GLY ('C', 'CA') 14 # MeO ('HO', 'OA') 0 # MeO ('C', 'OA') 0 # MeO ('C', 'H') 0 # import sys # sys.exit() ################################################################################ # Just for Printing the Header sorted_resname = sorted(ibonds.keys()) # sort to keep the value in the right order partial_header = [] for resname in sorted_resname: resname_header = [] # the header specific to residue for bondname in sorted(ibonds[resname].keys()): # bn: since bondname has been used in previous codes bn = '{0}|{1}'.format(resname[0], '-'.join(bondname)) resname_header.append('{0:9s}'.format(bn)) partial_header.extend(resname_header) yield '#{0:8s}{1}\n'.format('t(ps)', ''.join(partial_header)) # import sys # sys.exit() # Production Calculation # use < when for formatting values to align with headers, and the width will # be 1 col narrower than that in the corresponding header for ts in univer.trajectory: # for debugging only if debug and ts.frame % 2 == 0: print "time: {0:10.0f}; step: {1:10d}; frame: {2:10d}".format(ts.time, ts.step, ts.frame) if etime > ts.time >= btime: partial_yield = [] for resname in sorted_resname: resname_yield = [] for bondname in sorted(ibonds[resname].keys()): bonds = ibonds[resname][bondname] ds = [] for bond in bonds: r = bond[0].pos - bond[1].pos # end-to-end vector from atom positions d = np.linalg.norm(r) # distance ds.append(d) resname_yield.append('{0:<8.3f}'.format(np.average(ds))) #, np.std(ds)) partial_yield.extend(resname_yield) # a space in order to align with # in the header yield ' {0:<8.0f}{1}\n'.format(ts.time, ' '.join(partial_yield))
import sys sys.path.append('/home/x/xiansu/pfs/program/numpy/lib/python2.6/site-packages') from MDAnalysis import Universe, Writer from MDAnalysis.analysis.distances import distance_array import MDAnalysis import numpy from Numeric import * top = 'npt.gro' traj = 'md.xtc' water = Universe(top, traj) o = water.selectAtoms('name O*') resid = o.resids() print resid #resnu=o.resnums() #resna=o.resnames() atomInf = [] for i in o.atoms: atomid = str(i).split()[2] atomseg = str(i).split()[-1] atomidandseg = [] atomidandseg.append(atomid) atomidandseg.append(atomseg) atomInf.append(atomidandseg) print atomInf ##print len(waterResnu)
import numpy import sys sys.path.append('/home/x/xiansu/pfs/program/numpy/lib/python2.6/site-packages') from MDAnalysis import Universe, Writer from MDAnalysis.analysis.distances import distance_array import MDAnalysis DCD='water_analysis.dcd' PSF='ionized.psf' distanceMat=open('distance.txt','w') rho=Universe(PSF,DCD) ##print rho ##print list(rho.residues) p=rho.selectAtoms('protein and not backbone and not(name H*)') w=rho.selectAtoms('resname TIP3 and not(name H*)') ##print list(p) pc=p.coordinates() print len(pc) proteResid=p.resids() waterResid=w.resids() proteResnu=p.resnames() waterResna=w.resnames() waterResnu=w.resnums() atomInf=[] for i in w.atoms: atomid= str(i).split()[2] atomseg=str(i).split()[-1] atomidandseg=[] atomidandseg.append(atomid) atomidandseg.append(atomseg)