def gorama(*args, **kwargs): if args[0] == "help": print("gorama selection1, selection2, ..., selectionN") print("all selections will be used in their _current_ state") print( "Each selection will be colored differently in the plot, from blue (first) to red (last)" ) print( "If one selection is given the colors will go from blue to red with increasing residue number." ) return seles = args proc = Popen("gorama", shell=True, stdin=PIPE) opt = gochem.Options() number = 0 name = "_".join(args) + "_Rama_%d.png" % number while os.path.isfile(name): number += 1 name = "_".join(args) + "_Rama_%d.png" % number opt.AddStringOptions([name.replace(".png", "")]) gochem.SendgoChem(proc, seles, options=opt) if proc.wait() != 0: print("There were some errors") print("The points are colored by residue") print("First residues in the (sub)-sequence are in blue") print("Last residues, in red") img = mpimg.imread("./" + name) fig = plt.imshow(img) fig.set_cmap('hot') fig.axes.get_xaxis().set_visible(False) fig.axes.get_yaxis().set_visible(False) plt.show()
def goreduce(sel): if sel == "help": print("goreduce selectio\n") print("The selection will be used in the _current_ state") print("The selection (must be a protein!) will be protonated with") print("Reduce, which must be installed and in the PATH)") print("The protonated structure will be shown in PyMOL") return proc = Popen("goreduce", shell=True, stdin=PIPE, stdout=PIPE) gochem.SendgoChem(proc, selnames=[sel]) info = gochem.get_info(proc) mod = gochem.get_model(proc, info, 0) gochem.LoadNew(mod, sel + "_H")
def gotraj(sel, filename="unlikelytobearealname.exe", skip=1, extension=None): if sel == "help": print("gotraj selection traj_filename skip\n") print( "The trajectory file will be loaded on the selection, in a new object" ) print( "A frame will be read every skip frames (default=1, all frames are read)" ) return ed = { "crd": "amber", "trj": "amber", "xyz": "xyz", "xtc": "xtc", "dcd": "dcd", "pdb": "pdb", "xtc": "xtc" } proc = Popen("gotraj", shell=True, stdin=PIPE, stdout=PIPE) if filename == "unlikelytobearealname.exe": raise "usage: gotraj selection traj_file_name\n (you must give the name of the trajectory to be read)" if not extension: ext = filename.split(".")[-1] extension = ed[ ext] #if this doesn't work you get a well deserved exception if extension == "xtc": print("this extension is probably not supported!" ) #you'll get a crash later opt = gochem.Options() opt.AddStringOptions([filename, extension]) opt.AddIntOptions([int(skip)]) gochem.SendgoChem(proc, selnames=[sel], options=opt, sort=True) info = gochem.get_info(proc) mod, states = gochem.get_model(proc, info, 0) gochem.LoadNew(mod, sel + "_Traj") while states.GetState(mod): cmd.load_model( mod, sel + "_Traj" ) #we don't want to re-calculate bonds, so we call this function instead.
def goshape(*args, **kwargs): if args[0] == "help": print("goshape selection1, selection2, ..., selectionN") print( "Obtains shape indicators (percentage of linearity/elongation and percentage of" ) print( "planarity, both based on the eigenvalues of the moment of inertia tensor, for each" ) print("selection given") print("all selections will be used in their _current_ state") print("The indicators are based on those proposed by") print("Taylor et al., .(1983), J Mol Graph, 1, 3") return seles = args proc = Popen("goshape", shell=True, stdin=PIPE, stdout=PIPE) gochem.SendgoChem(proc, seles) info = gochem.get_info(proc) lincirc = info["FloatInfo"] for i, v in enumerate(seles): print("%s: Elongation %4.1f %% Planarity %4.1f %%" % (v, lincirc[i][0], lincirc[i][1]))
def gomd(*args, **kwargs): if args[0] == "help": print("gomd: gomodel! MD analysis module") print("gomd taks selection1, selection2, ..., selectionN") print("available tasks are:") print( "gomd Distances, sel1a, sel1b, sel2a, sel2b ... \n Plots the distances between pairs of selections along the simulation\n" ) print( "gomd Elongation, sel1, sel2, sel3, ... \n Plots the % of elongation for the selections given\n" ) print( "gomd Ramachandran, sel1\n draws the Ramachandran plot for a selection along the trajectory (each frame is colores differently. Takes one selection\n" ) print( "gomd RMSD, ref1, sel1, ref2, sel2 ...\n plot the rmsd of a selection along a simulations, against a fixed reference\n" ) print( "gomd RMSF, sel1\n plots the per-atom RMSF for all atoms in the selection. Takes one selection only\n" ) print( "gomd Super reference_selection, test_selection, move_selection\n Obtains transformations to superimpose each frame of test_selection to reference_selection and applies the transformations to the different frames of move_selection" ) return task = args[0].upper() if task == "DISTANCE": task == "DISTANCES" seles = args[1:] print(task, "*", seles) proc = Popen("gomd", shell=True, stdin=PIPE, stdout=PIPE) opt = gochem.Options() number = 0 opt.AddStringOptions([task]) skip = 1 if "skip" in kwargs.keys(): skip = int(kwargs["skip"]) opt.AddIntOptions( [1] ) #the skip is handled in Python. Go just getst the already trimmed trajectory, so it never has to skip statesdic = [] for i in seles: statesdic.append({"first": 0, "last": -1, "skip": skip}) gochem.SendgoChem(proc, seles, states=statesdic, options=opt) info = gochem.get_info(proc) if task == "SUPER": mod, states = gochem.get_model(proc, info, 0) gochem.LoadNew(mod, seles[2] + "_sup") while states.GetState(mod): cmd.load_model( mod, seles[2] + "_sup" ) #we don't want to re-calculate bonds, so we call this function instead. return results = info["FloatInfo"] if task == "RAMACHANDRAN": ramas = [] for i in results: ramas.append({"phi": [], "psi": []}) for j, v in enumerate(i): if j % 2 == 0: ramas[-1]["phi"].append(v) else: ramas[-1]["psi"].append(v) fig, ax = plt.subplots() cmap = plt.get_cmap("jet") #"Spectral") plt.xlabel('Phi (deg)') plt.ylabel('Psi (deg)') axes = plt.gca() axes.set_xlim([-180, 180]) axes.set_ylim([-180, 180]) for i, v in enumerate(ramas): phi = np.array(v["phi"]) psi = np.array(v["psi"]) r, g, b = colors(i, len(ramas)) alpha = 1.0 plt.scatter(phi, psi, color=(r, g, b, alpha), marker="+") plt.show() return if task == "RMSF": fig, ax = plt.subplots() r = list(range(1, len(results[0]) + 1)) r = np.array(r) rmsf = np.array(results[0]) print(r, rmsf) ax.plot(r, rmsf, ".-") ax.set(xlabel='Residue/Atom ', ylabel='A', title=task) plt.show() return #For distances I'll have half as many results as I have selections, since a distance is obtained for 2 selections #so I re-structure the seles list to contain half as many elements, each with the list of the 2 selections measured. if task == "DISTANCES": s = [] for i, v in enumerate(seles): if i % 2 != 0: continue s.append(v + "-" + seles[i + 1]) seles = s #A similar thing happens with RMSD, the first selection is always the reference, so we take it away from the names. if task == "RMSD": s = [] for i, v in enumerate(seles): if i % 2 == 0: continue s.append(v) seles = s glyphs = [ "b-", "r-", "g-", "k-", "c-", "m-", "k--", "b^-", "ro-", "g.-", "c:" ] runav = int(len(results[0]) / 50) dorunav = True runavlegend = "(%d -Running average)" % (runav) unit = "A" if task in ["PLANARITY", "ELONGATION"]: unit = "%" if "runav" in kwargs.keys() and kwargs["runav"].upper() == "FALSE": dorunav = False runavlegend = "" fig, ax = plt.subplots() ax.set(xlabel="Frame %s" % (runavlegend), ylabel=task + ' ' + unit, title=task.title()) for i, y in enumerate(results): if i > len(glyphs) - 1: "No more glyphs to plot" continue if dorunav: y = np.convolve(y, np.ones((runav, )) / runav, mode='valid') xs = list(range(1, len(y) + 1)) ax.plot(xs, y, glyphs[i], label=seles[i]) ax.legend(loc='upper right') plt.show()
def goopt(sel, quality="high", fixed=None, charge=0, multi=1, solvent="water", dryrun=False): if sel == "help": print( "goopt selection quality=high, fixed=None, charge=0, multi=1,solvent=''\n" ) print( "Optimizes the selection with xtb (must be installed and in the PATH)" ) print("The optimized structure is loaded into PyMOL") print( "Selections can't have dangling bonds! if you want to optimize a sub-sequence in a protein" ) print("Use the gocap command to cap the dangling bonds first") print( "fixed is a selections with the atoms to be fixed/constrained during the optimization" ) print("gocap provides a 'tofix' selection you can use here") print( "Only one selection is allowed, so put together all your selections into one" ) print("Remember to get the charge right!") print( "3 qualities for the optimization are supported. 'high' (but slower) 'medium' and 'slow'" ) print( "3 possible solvents are supported. water, protein (actually modeled as dicloromethane, e=5, and no solvent (default)" ) print( "goopt always takes the first state for each selection. If you wish a different state, copy it as a new object (which gocap does)" ) return qual = {"vhigh": "gfn2", "high": "gfn0", "medium": "gfnff"} diel = {"water": 80, "protein": 5, "": -1} proc = Popen("goopt", shell=True, stdin=PIPE, stdout=PIPE) fix = [] #If we get a selection with atoms to be fixed #we can't just pick those indexes and give them to Go. #we need to find the indexes in the large selection that have the same #id as the atoms in the fixed selection, and give _those_ indexes to Go. if fixed: mod = cmd.get_model(sel) tofix = cmd.get_model(fixed) fixids = [] for i in tofix.atom: fixids.append(i.id) for i, v in enumerate(mod.atom): if v.id in fixids: fix.append(i) opt = gochem.Options() method = qual[quality] opt.AddStringOptions([method]) opt.AddBoolOptions([dryrun]) opt.AddIntOptions([int(charge), int(multi)]) opt.AddIntOptions(fix) opt.AddFloatOptions([float(diel[solvent])]) stat = {"first": -1, "last": 0, "skip": 1} #Ojects created from Chempy and objects read by PyMOL are somehow treated differently by the program. #For a PDB its OK to get the current "-1" state, but for the chempy-ones, it causes errors later. So, #I just put this completely ad-hoc fix. IF gocap created the object (thus, it's a Chempy object) we #ask for the first state, not the current. These objects have one state, so it's the same, but it prevents an #error later on. if sel.endswith("__cap"): stat = {"first": 0, "last": 0, "skip": 1} gochem.SendgoChem(proc, selnames=[sel], states=[stat], options=opt) info = gochem.get_info(proc) mod = gochem.get_model(proc, info, 0) gochem.LoadNew(mod, sel + "_opt") print( "Structure optimized with the xtb program from the Grimme group (https://github.com/grimme-lab/xtb), using the %s method" % method.upper()) print( "Remember to cite:\nGFN2-xTB: 10.1021/acs.jctc.8b01176\nGFN0-xTB: 10.26434/chemrxiv.8326202.v1\n GFNFF: 10.1002/anie.202004239" ) print("The xtb reference: 10.1002/wcms.1493")