def to_pdb(cg): used_chainids = set(cg.chains.keys()) def get_available_chainid(): for c in string.ascii_uppercase: if c not in used_chainids: used_chainids.add(c) return c raise ValueError("Too many chains. Cannot convert to old PDB format.") f = StringIO() for chain in cg.chains.values(): if len(chain.id) > 1: chain.id = get_available_chainid() ftup.output_multiple_chains(cg.chains.values(), f, "pdb") return f.getvalue()
def _get_fragment(self, stat, sm): key = stat.pdb_name + "__def_" + "-".join(map(str, stat.define)) new_fragment = False try: fragment, _, _ = ftup.get_all_chains(op.join( self.LIBRARY_DIRECTORY, key[2:4], key + ".cif"), no_annotation=True) except Exception: cg, chains = self._get_source_cg_and_chain(stat, sm) new_fragment = True else: fragment = {c.id: c for c in fragment} log.debug("Used stored fragment for %s", key) pdb_basename = stat.pdb_name.split(":")[0] cg_filename = op.expanduser( op.join(self.cg_library_path, pdb_basename + ".cg")) cg = self.get_cg(cg_filename) #The cg with the template try: elem = cg.get_node_from_residue_num(stat.define[0]) except Exception: log.error("stat %s with define %s", stat, stat.define) raise if stat.define != cg.defines[elem]: err = ValueError( "The CG files where the stats where extracted and " "the cg file used for reconstruction are not consistent!") with log_to_exception(log, err): log.error("%s != %s for element %s (%s)", stat.define, cg.defines[elem], elem, stat.pdb_name) raise err if new_fragment: fragment = ftup.extract_subchains_from_seq_ids( chains, cg.define_residue_num_iterator(elem, seq_ids=True, adjacent=(elem[0] != "s"))) if self.LIBRARY_DIRECTORY is not None: log.debug("Storing newly-created fragment for %s", key) import distutils.dir_util distutils.dir_util.mkpath( op.join(self.LIBRARY_DIRECTORY, key[2:4])) ftup.output_multiple_chains( fragment.values(), op.join(self.LIBRARY_DIRECTORY, key[2:4], key + ".cif"), "cif") return cg, elem, fragment
def reconstruct(cg, fn, args, rec): log.info("Processing %s", fn) stat_source = fbs.from_args(args, cg) sm = fbm.SpatialModel(cg) sm.load_sampled_elems(stat_source) if args.reassign_broken: sm.bg.traverse_graph() for ml in sm.bg.mloop_iterator(): if ml not in sm.bg.mst: try: del sm.elem_defs[ml] except KeyError: pass fbm._perml_energy_to_sm(sm, "MAX10000[1FJC1]", stat_source) for ml in sm.bg.mloop_iterator(): if ml not in sm.bg.mst: e = sm.junction_constraint_energy[ml].eval_energy(sm.bg) log.info("Deviation for %s is %s", ml, e) energies = list(sm.junction_constraint_energy[ml].iterate_energies()) if len(energies)==1: log.debug("Getting used_stat for single energy %s",energies) used_stat = energies[0].used_stat else: log.debug("Multiple energies. Iterating") for ef in energies: if ef.element==ml: used_stat = ef.used_stat log.info("Used stat for %s is %s", ml, used_stat) sm.elem_defs[ml] = used_stat sm.save_sampled_elems() log.info("Broken stats reassigned. Storing cg.") sm.bg.to_file(fn+".reassigned.cg") sm.new_traverse_and_build() chains = rec.reconstruct(sm) print("Writing", fn+".reconstr.pdb") chains = ftup.rename_chains_for_pdb(chains) ftup.output_multiple_chains(chains.values(), fn+".reconstr.pdb")
def main(args): rnas = fuc.cgs_from_args(args, '+', '3d') pp = pymol_printer_from_args(args) if args.align: align_rnas(rnas) if args.labels: label_list = args.labels.split(",") labels = {} for label in label_list: if not label: continue try: elem, lab = label.split(':') except ValueError: raise ValueError( "Please specify --labels with as list of colon-seperated tuples. Found invalid entry {}.".format(repr(label))) labels[elem] = lab if not pp.print_text: labels = defaultdict(lambda: "", labels) pp.print_text = True else: labels = {} color_modifier = 1.0 log.info("Visualizing {} rnas".format(len(rnas))) for rna in rnas: pp.add_cg(rna, labels, color_modifier) color_modifier *= 0.7 with make_temp_directory() as tmpdir: # The file describing the cg-structure as cylinders if args.pymol_file: stru_filename = args.pymol_file else: stru_filename = os.path.join(tmpdir, "structure") with open(stru_filename, "w") as f: f.write(pp.pymol_string()) pdb_fns = [] selections = "" for i, rna in enumerate(rnas): if rna.chains: obj_name = "pdb{}_{}".format(i, rna.name.replace("-", "_")) fn = os.path.join(tmpdir, obj_name + ".cif") pdb_fns.append(fn) ftup.output_multiple_chains(rna.chains.values(), fn, "cif") for d in rna.defines: resids = list( rna.define_residue_num_iterator(d, seq_ids=True)) if resids: chains = {r.chain for r in resids} sel = [] for c in chains: sel.append("( %{} and chain {} and resi {}) ".format( obj_name, c, "+".join(map(str, (r.resid[1] for r in resids))))) selections += "select {}, ".format( d + "_" + obj_name) + " or ".join(sel) + "\n" pymol_cmd = 'hide all\n' pymol_cmd += 'show cartoon, all\n' pymol_cmd += 'set cartoon_ring_mode\n' pymol_cmd += 'set cartoon_tube_radius, .3\n' if args.only_elements is not None: pymol_cmd += "hide all\n" for constraint in args.only_elements.split(','): color = pp.get_element_color(constraint) for r in cg.define_residue_num_iterator(constraint, seq_ids=True): pymol_cmd += "show sticks, resi %r\n" % (r[1]) pymol_cmd += "color %s, resi %r\n" % (color, r[1]) pymol_cmd += 'run %s\n' % (stru_filename) pymol_cmd += 'bg white\n' pymol_cmd += 'clip slab, 10000\n' #pymol_cmd += 'orient\n' pymol_cmd += selections if args.output is not None: pymol_cmd += 'ray\n' pymol_cmd += 'png %s\n' % (args.output) #pymol_cmd += 'quit\n' pml_filename = os.path.join(tmpdir, "command.pml") with open(pml_filename, "w") as f1: f1.write(pymol_cmd) if args.batch: p = sp.Popen(['pymol', '-cq'] + pdb_fns + [pml_filename], stdout=sp.PIPE, stderr=sp.PIPE) else: p = sp.Popen(['pymol'] + pdb_fns + [pml_filename], stdout=sp.PIPE, stderr=sp.PIPE) log.info("Now opening pymol") out, err = p.communicate() log.info("Out=\n%s", out) log.info("Errt=\n%s", err)
def update_statistics(self, sm, energy, member_energies=[], change="", clashes=[], bad_mls=[]): """ Add a new structure to the statistics. :param sm: The spatial model. :param energy: The energy used for sampling of the structure, or None. :param member_energies: A list of tuples `(energy_shortname, value)` """ self.step += 1 line = ["{:6d}\t{:10.3f}".format(self.step, energy)] if self.options["constituing_energies"] == "no_clash": ignore_names = [ fbe.RoughJunctionClosureEnergy().shortname, fbe.StemVirtualResClashEnergy().shortname ] else: ignore_names = [] line.append("( " + " ".join("{} {:10.3f}".format(*x) for x in member_energies if x[0] not in ignore_names) + " )") line.append(self.collector.update(sm, self.step)) line.append(change) if isinstance(clashes, str): line.append(clashes) else: line.append(",".join(["{}+{}".format(*c) for c in clashes]) or "noclash") if isinstance(bad_mls, str): line.append(bad_mls) else: line.append(",".join(bad_mls) or "noclash") self.printline("\t".join(line)) if self.best_cgs.can_insert_right((None, energy)): self.best_cgs.insert_right((sm.bg.to_cg_string(), energy)) if self.step % 10 == 0: for i, cg_stri in enumerate(self.best_cgs): with open( os.path.join(self.out_dir, 'best{:d}.coord'.format(i)), 'w') as f: f.write(cg_stri[0]) if self.options["step_save"] > 0 and self.step % self.options[ "step_save"] == 0: cg_stri = sm.bg.to_cg_string() with open( os.path.join(self.out_dir, 'step{:06d}.coord'.format(self.step)), "w") as f: # print("Opened file", os.path.join(self.out_dir, # 'step{:06d}.coord'.format(self.step))) print(cg_stri, file=f) if self.reconstructor is not None and self.step % self.options[ "reconstruct_n"] == 0: try: chains = self.reconstructor.reconstruct(sm) ftup.output_multiple_chains( chains.values(), os.path.join(self.out_dir, 'step{:06d}.reconstr.pdb'.format(self.step))) except: log.exception( "Could not reconstruct all-atom PDB, because of the following error:" )
def main(args): rnas = fuc.cgs_from_args(args, '+', '3d') pp = pymol_printer_from_args(args) if args.align: align_rnas(rnas) if args.labels: label_list = args.labels.split(",") labels = {} for label in label_list: if not label: continue try: elem, lab = label.split(':') except ValueError: raise ValueError( "Please specify --labels with as list of colon-seperated tuples. Found invalid entry {}." .format(repr(label))) labels[elem] = lab if not pp.print_text: labels = defaultdict(lambda: "", labels) pp.print_text = True else: labels = {} color_modifier = 1.0 log.info("Visualizing {} rnas".format(len(rnas))) for rna in rnas: pp.add_cg(rna, labels, color_modifier) color_modifier *= 0.7 with make_temp_directory() as tmpdir: # The file describing the cg-structure as cylinders if args.pymol_file: stru_filename = args.pymol_file else: stru_filename = os.path.join(tmpdir, "structure") with open(stru_filename, "w") as f: f.write(pp.pymol_string()) pdb_fns = [] selections = "" for i, rna in enumerate(rnas): if rna.chains: obj_name = "pdb{}_{}".format(i, rna.name.replace("-", "_")) fn = os.path.join(tmpdir, obj_name + ".cif") pdb_fns.append(fn) ftup.output_multiple_chains(rna.chains.values(), fn, "cif") for d in rna.defines: resids = list( rna.define_residue_num_iterator(d, seq_ids=True)) if resids: chains = {r.chain for r in resids} sel = [] for c in chains: sel.append( "( %{} and chain {} and resi {}) ".format( obj_name, c, "+".join( map(str, (r.resid[1] for r in resids))))) selections += "select {}, ".format( d + "_" + obj_name) + " or ".join(sel) + "\n" pymol_cmd = 'hide all\n' pymol_cmd += 'show cartoon, all\n' pymol_cmd += 'set cartoon_ring_mode\n' pymol_cmd += 'set cartoon_tube_radius, .3\n' if args.only_elements is not None: pymol_cmd += "hide all\n" for constraint in args.only_elements.split(','): color = pp.get_element_color(constraint) for r in cg.define_residue_num_iterator(constraint, seq_ids=True): pymol_cmd += "show sticks, resi %r\n" % (r[1]) pymol_cmd += "color %s, resi %r\n" % (color, r[1]) pymol_cmd += 'run %s\n' % (stru_filename) pymol_cmd += 'bg white\n' pymol_cmd += 'clip slab, 10000\n' #pymol_cmd += 'orient\n' pymol_cmd += selections if args.output is not None: pymol_cmd += 'ray\n' pymol_cmd += 'png %s\n' % (args.output) #pymol_cmd += 'quit\n' pml_filename = os.path.join(tmpdir, "command.pml") with open(pml_filename, "w") as f1: f1.write(pymol_cmd) if args.batch: p = sp.Popen(['pymol', '-cq'] + pdb_fns + [pml_filename], stdout=sp.PIPE, stderr=sp.PIPE) else: p = sp.Popen(['pymol'] + pdb_fns + [pml_filename], stdout=sp.PIPE, stderr=sp.PIPE) log.info("Now opening pymol") out, err = p.communicate() log.info("Out=\n%s", out) log.info("Errt=\n%s", err)
def to_pdb(cg): chains = ftup.rename_chains_for_pdb(cg.chains) f = StringIO() ftup.output_multiple_chains(chains.values(), f, "pdb") return f.getvalue()
def to_pdb(cg): chains = ftup.rename_chains_for_pdb(cg.chains) f = StringIO() ftup.output_multiple_chains(chains.values(), f, "pdb") return f.getvalue()
for elem in cg.defines: if elem in cg.incomplete_elements: continue base_name = "{}:{}_".format(cg.name, elem[0]) idnr = next_id[base_name] next_id[base_name] += 1 name = base_name + str(idnr) if args.fragment_dir: fragment_chains = ftup.extract_subchains_from_seq_ids( cg.chains, cg.define_residue_num_iterator(elem, seq_ids=True, adjacent=elem[0] != "s")) try: ftup.output_multiple_chains( list(fragment_chains.values()), op.join(args.fragment_dir, name + ".pdb")) except AttributeError: continue direction = "a" remark = "" for resid in cg.define_residue_num_iterator( elem, seq_ids=True, adjacent=elem[0] != "s"): if resid in cg.seq._modifications: remark += "# {}={}".format(fgr.resid_to_str(resid), cg.seq._modifications[resid]) for stat in cg.get_stats(elem): stat_name = name + direction stat.pdb_name = stat_name print(stat, remark) direction = "b"