def load_mcard(filename, gzipped=False): if (gzipped): file = gzip.GzipFile(filename, 'r') else: file = open(filename, 'r') print >> sys.stderr, "Parsing database file: %s" % filename # Parse the Mcard SDF database file: while True: header = file.readline() if (header == ""): break body = util.readlines_until_mark(file, "$$$$") compound = header.split('\t')[-1].rstrip().upper() G = ChemGraph() G.filename = filename try: G.read_mol(body) map_compound2graph[compound] = G map_hash2compound[G.hash()] = compound map_hash2compound_unchiral[G.hash_unchiral()] = compound except ChemException, errstring: print >> sys.stderr, " - Skipping this compound [%s] because of: %s" % ( compound, errstring) pass
def compound2graph(compound): compound = compound.upper() if (compound in map_compound2graph): return map_compound2graph[compound] elif (compound.find(' + ') != -1): G = ChemGraph() G.ignore_attributes = False G.add([compound2graph(c) for c in compound.split(' + ')]) return G else: raise ChemException("Unknown compound: %s" % compound)
def hash2graph(hash, update_attributes=False): """Converts a hash string into a ChemGraph. Works only one single molecule hashes """ G_total = ChemGraph() N = 0 for (atoms, bonds) in parse_hash(hash): G = ChemGraph() G.resize(len(atoms)) for n in range(len(atoms)): for m in range(n): G.set_bond(n, m, bonds.pop(0)) for n in range(len(atoms)): (G.nodes[n], G.valences[n], G.hydrogens[n], G.charges[n], G.chirality[n]) = parse_atom(atoms[n]) if (update_attributes): G.update_attributes() G.initialize_pos() G_total.add(G) return G_total
def bonds2graph(bonds): graph = ChemGraph() n = 0 m = 0 graph.add_node(atom_wildcard) for b in bonds: if (m == n): graph.add_node(atom_wildcard) m = 0 n += 1 graph.set_bond(n, m, b) m += 1 if (m != n): raise ChemException("length of bonds list is not N(N-1)/2: " + str(bonds)) return graph
def mol2svg(mol, width=200, height=200, font_size=7): G = ChemGraph() G.read_mol(mol.split("\n")) scene = svg.Scene(width, height, font_size) G.svg(scene) return scene
def molfile2graph(filename): G = ChemGraph() G.read_file(filename) return G
def smiles2graph(smiles): mol = smiles2mol(smiles) G = ChemGraph() G.read_mol(mol.split("\n")) return G