def log_new_clusters(output_file, n, leader_list): print "log %d ligands. For change this number set the -n parameter" % n str_info = "\t%s\t \t%20s\t %20s\t %20s\n" % ("File", "Model", "T.Energy", "I.Energy") for i in range(0, n): str_info += "%s\t%20s\t%20s\t%20s\n" % ( leader_list[i].getFileBelow().split('/').pop(), leader_list[i].getID(), leader_list[i].getTotalEnergy(), leader_list[i].getInteractionEnergy()) out_log = file(output_file + ".log", "w") out_log.write(str_info) out_log.close() conv = OBConversion() conv.SetOutFormat("mol2") conv.WriteFile(leader_list[0], output_file + ".mol2") for i in range(1, n): conv.Write(leader_list[i]) conv.CloseOutFile()
def convert_str(str_data, in_format, out_format): mol = OBMol() conv = OBConversion() conv.SetInFormat(in_format) conv.SetOutFormat(out_format) conv.ReadString(mol, str_data) return (conv.WriteString(mol), conv.GetOutFormat().GetMIMEType())
def cjson_to_ob_molecule(cjson): cjson_str = json.dumps(cjson) sdf_str = avo_convert_str(cjson_str, 'cjson', 'sdf') conv = OBConversion() conv.SetInFormat('sdf') conv.SetOutFormat('sdf') mol = OBMol() conv.ReadString(mol, sdf_str) return mol
def autodetect_bonds(cjson): mol = cjson_to_ob_molecule(cjson) mol.ConnectTheDots() mol.PerceiveBondOrders() conv = OBConversion() conv.SetInFormat('sdf') conv.SetOutFormat('sdf') sdf_str = conv.WriteString(mol) cjson_str = avo_convert_str(sdf_str, 'sdf', 'cjson') return json.loads(cjson_str)
def to_inchi(str_data, in_format): mol = OBMol() conv = OBConversion() conv.SetInFormat(in_format) # Hackish for now, convert to xyz first... conv.SetOutFormat('xyz') conv.ReadString(mol, str_data) xyz = conv.WriteString(mol) # Now convert to inchi and inchikey. mol = OBMol() conv.SetInFormat('xyz') conv.ReadString(mol, xyz) conv.SetOutFormat('inchi') inchi = conv.WriteString(mol).rstrip() conv.SetOptions("K", conv.OUTOPTIONS) inchikey = conv.WriteString(mol).rstrip() return (inchi, inchikey)
def to_inchi(str_data, in_format): mol = OBMol() conv = OBConversion() conv.SetInFormat(in_format) conv.ReadString(mol, str_data) conv.SetOutFormat('inchi') inchi = conv.WriteString(mol).rstrip() conv.SetOptions('K', conv.OUTOPTIONS) inchikey = conv.WriteString(mol).rstrip() return (inchi, inchikey)
class Converter: def __init__(self, can=False): self._OBconverter = OBConversion() self._OBconverter.SetOutFormat('smi') options = 'in' if can: options += 'c' self._OBconverter.SetOptions(options, OBConversion.OUTOPTIONS) def getSmiles(self, mol): smiles = self._OBconverter.WriteString(mol.OBMol)[:-1] if '@' in smiles: smiles = self._OBconverter.WriteString(mol.OBMol)[:-1] return smiles
def log_new_clusters(output_file, n, leader_list, ref=None): print "log %d ligands. For change this number set the -n parameter" % n str_info = "\t%s\t %20s\t %20s\t %20s \t%15s\n" % ( "File", "Model", "T.Energy", "I.Energy", "RMSD") if ref != None: mol_ref = loadReferenceMolecule(ref) if ref != None: for i in range(0, n): str_info += "%s\t%20s\t%20s\t%20s\t%15.3f\n" % ( leader_list[i].getFileBelow(), leader_list[i].getID(), leader_list[i].getTotalEnergy(), leader_list[i].getInteractionEnergy(), getRMSD(leader_list[i], mol_ref)) else: for i in range(0, n): str_info += "%s\t%20s\t%20s\t%20s\t%15.3f\n" % ( leader_list[i].getFileBelow(), leader_list[i].getID(), leader_list[i].getTotalEnergy(), leader_list[i].getInteractionEnergy(), getRMSD(leader_list[i], leader_list[0])) out_log = file(output_file + ".log", "w") out_log.write(str_info) out_log.close() conv = OBConversion() conv.SetOutFormat("mol2") conv.WriteFile(leader_list[0], output_file + ".mol2") for i in range(1, n): conv.Write(leader_list[i]) conv.CloseOutFile()
def write_ob_molecule(self, mol, format, f): """ Write an Open Babel molecule to file :param mol: The molecule :param format: The output format :param f: The file to write output to :param f_name: The file's name (for extension-finding purpose) """ conv = OBConversion() if not conv.SetOutFormat(format): raise ValueError("Error setting output format to " + format) # write to file try: s = conv.WriteString(mol) except (TypeError, ValueError, IOError): raise ValueError("Error writing data file using OpenBabel") if str.lower(format) == 'pdb': s = s.replace("END", "ENDMDL") f.write(s)
def convert_str(str_data, in_format, out_format, gen3d=False, add_hydrogens=False, perceive_bonds=False, out_options=None): # Make sure that the start of InChI is valid before passing it to # Open Babel, or Open Babel will crash the server. if in_format.lower() == 'inchi': validate_start_of_inchi(str_data) if out_options is None: out_options = {} obMol = OBMol() conv = OBConversion() conv.SetInFormat(in_format) conv.SetOutFormat(out_format) conv.ReadString(obMol, str_data) if add_hydrogens: obMol.AddHydrogens() if gen3d: # Generate 3D coordinates for the input mol = pybel.Molecule(obMol) mol.make3D() if perceive_bonds: obMol.ConnectTheDots() obMol.PerceiveBondOrders() for option, value in out_options.items(): conv.AddOption(option, conv.OUTOPTIONS, value) return (conv.WriteString(obMol), conv.GetOutFormat().GetMIMEType())