def abstract(self, graph): graph = edengraphtools._edge_to_vertex_transform(graph) tmpgraph = edengraphtools._revert_edge_to_vertex_transform(graph) abstract_graph = make_abstract(tmpgraph) _abstract_graph = edengraphtools._edge_to_vertex_transform(abstract_graph) for n, d in _abstract_graph.nodes(data=True): if 'contracted' not in d: d['contracted'] = set() getabstr = {contra: node for node, d in _abstract_graph.nodes(data=True) for contra in d.get('contracted', [])} for n, d in graph.nodes(data=True): if 'edge' in d: # if we have found an edge node... # lets see whos left and right of it: n1, n2 = graph.neighbors(n) # case1: ok those belong to the same gang so we most likely also belong there. if getabstr[n1] == getabstr[n2]: _abstract_graph.node[getabstr[n1]]['contracted'].add(n) # case2: neighbors belong to different gangs... else: blub = set(_abstract_graph.neighbors(getabstr[n1])) & set(_abstract_graph.neighbors(getabstr[n2])) for blob in blub: if 'contracted' in _abstract_graph.node[blob]: _abstract_graph.node[blob]['contracted'].add(n) else: _abstract_graph.node[blob]['contracted'] = set([n]) return _abstract_graph
def substitute_core(graph, cip, congruent_cip): # expand edges and remove old core graph = _edge_to_vertex(graph) graph.remove_nodes_from(cip.core_nodes) # relabel the nodes in the congruent cip such that the interface node-ids match with the graph and the # core ids dont overlap interface_map = next( find_all_isomorphisms(congruent_cip.interface, cip.interface)) if len(interface_map) != len(cip.interface): logger.log(10, "isomorphism failed, likely due to hash collision") return None maxid = max( graph.nodes()) # if we die here, likely the cip covers the whole graph core_rename = { c: i + maxid + 1 for i, c in enumerate(congruent_cip.core_nodes) } interface_map.update(core_rename) newcip = nx.relabel_nodes(congruent_cip.graph, interface_map, copy=True) # compose and undo edge expansion graph2 = nx.compose(graph, newcip) # if the reverserion fails, you use a wrong version of eden, where # expansion requires that edges are indexed by (0..n-1) return eg._revert_edge_to_vertex_transform(graph2) '''
def core_substitution(graph, orig_cip, new_cip): """ graph is the whole graph.. subgraph is the interface region in that we will transplant new_cip_graph which is the interface and the new core """ # preprocess graph = _edge_to_vertex(graph) assert ( set(orig_cip.graph.nodes()) - set(graph.nodes()) == set([])), 'lsgg_compose_util orig_cip_graph not in graph' # get isomorphism iso = find_all_isomorphisms(orig_cip.interface_graph, new_cip.interface_graph).next() if len(iso) != len(orig_cip.interface_graph): logger.log(5, "lsgg_compose_util grammar hash collision, discovered in 'core_substution' ") return None # make graph union (the old graph and the new cip are now floating side by side) graph = nx.union(graph, new_cip.graph, rename=('', '-')) graph.remove_nodes_from(map(str, orig_cip.core_nodes)) # merge interface nodes for k, v in iso.iteritems(): merge(graph, str(k), '-' + str(v)) graph = eg._revert_edge_to_vertex_transform(graph) re = nx.convert_node_labels_to_integers(graph) return re
def base_cip(core,cip): #draw.graphlearn(cip.graph) try: res= edengraphtools._revert_edge_to_vertex_transform(cip.graph) except: print 'abstractor.py failed to return base cip...' import structout as so so.gprint(cip.graph) return res
def make_abstract(graph): ''' graph should be the same expanded graph that we will feed to extract_cips later... Parameters ---------- graph Returns ------- ''' if isinstance(graph, nx.DiGraph): graph = graph.to_undirected() graph2 = edengraphtools._revert_edge_to_vertex_transform(graph) graph2 = edge_type_in_radius_abstraction(graph2) graph2 = edengraphtools._edge_to_vertex_transform(graph2) # find out to which abstract node the edges belong # finding out where the edge-nodes belong, because the contractor cant possibly do this getabstr = {contra: node for node, d in graph2.nodes(data=True) for contra in d.get('contracted', [])} for n, d in graph.nodes(data=True): if 'edge' in d: # if we have found an edge node... # lets see whos left and right of it: n1, n2 = graph.neighbors(n) # case1: ok those belong to the same gang so we most likely also belong there. if getabstr[n1] == getabstr[n2]: graph2.node[getabstr[n1]]['contracted'].add(n) # case2: neighbors belong to different gangs... else: blub = set(graph2.neighbors(getabstr[n1])) & set(graph2.neighbors(getabstr[n2])) for blob in blub: if 'contracted' in graph2.node[blob]: graph2.node[blob]['contracted'].add(n) else: graph2.node[blob]['contracted'] = set([n]) return graph2
def graph_to_molfile(graph): ''' helper of nx_to_chem Parameters ---------- graph: nx.graph Returns ------- sdf_string taken from eden. differences to eden: atom_line += d['label'].ljust(3) <- this is changed from eden. sdf_string += 'M END' <- needs 2 spaces! ''' symbols = {'1': 'H', '2': 'He', '3': 'Li', '4': 'Be', '5': 'B', '6': 'C', '7': 'N', '8': 'O', '9': 'F', '10': 'Ne', '11': 'Na', '12': 'Mg', '13': 'Al', '14': 'Si', '15': 'P', '16': 'S', '17': 'Cl', '18': 'Ar', '19': 'K', '20': 'Ca', '21': 'Sc', '22': 'Ti', '23': 'V', '24': 'Cr', '25': 'Mn', '26': 'Fe', '27': 'Co', '28': 'Ni', '29': 'Cu', '30': 'Zn', '31': 'Ga', '32': 'Ge', '33': 'As', '34': 'Se', '35': 'Br', '36': 'Kr', '37': 'Rb', '38': 'Sr', '39': 'Y', '40': 'Zr', '41': 'Nb', '42': 'Mo', '43': 'Tc', '44': 'Ru', '45': 'Rh', '46': 'Pd', '47': 'Ag', '48': 'Cd', '49': 'In', '50': 'Sn', '51': 'Sb', '52': 'Te', '53': 'I', '54': 'Xe', '55': 'Cs', '56': 'Ba', '57': 'La', '58': 'Ce', '59': 'Pr', '60': 'Nd', '61': 'Pm', '62': 'Sm', '63': 'Eu', '64': 'Gd', '65': 'Tb', '66': 'Dy', '67': 'Ho', '68': 'Er', '69': 'Tm', '70': 'Yb', '71': 'Lu', '72': 'Hf', '73': 'Ta', '74': 'W', '75': 'Re', '76': 'Os', '77': 'Ir', '78': 'Pt', '79': 'Au', '80': 'Hg', '81': 'Tl', '82': 'Pb', '83': 'Bi', '84': 'Po', '85': 'At', '86': 'Rn', '87': 'Fr', '88': 'Ra', '89': 'Ac', '90': 'Th', '91': 'Pa', '92': 'U', '93': 'Np', '94': 'Pu', '95': 'Am', '96': 'Cm', '97': 'Bk', '98': 'Cf', '99': 'Es', '100': 'Fm', '101': 'Md', '102': 'No', '103': 'Lr', '104': 'Rf', '105': 'Db', '106': 'Sg', '107': 'Bh', '108': 'Hs', '109': 'Mt', '110': 'Ds', '111': 'Rg', '112': 'Uub', '113': 'Uut', '114': 'Uuq', '115': 'Uup', '116': 'Uuh', '117': 'Uus', '118': 'Uuo'} graph = edengraphtools._revert_edge_to_vertex_transform(graph) graph = nx.convert_node_labels_to_integers(graph, first_label=0, ordering='default', label_attribute=None) # creating an SDF file from graph: # The header block, i.e. the first three lines, may be empty: sdf_string = "Networkx graph to molfile\n\n\n" # After the header block comes the connection table: # First the counts line - step by step counts_line = "" # Number of atoms counts_line += str(len(graph.nodes())).rjust(3) # Number of bonds counts_line += str(len(graph.edges())).rjust(3) # Number of atom lists counts_line += ' 0' # Three blank spaces, then the chirality flag counts_line += ' 1' # Five identical blocks counts_line += ' 0' * 5 # Finish with 0999 V2000 counts_line += '999 V2000\n' sdf_string += counts_line # Atom block - this contains one atom line per atom in the molecule for n, d in graph.nodes_iter(data=True): atom_line = '' # Set all coordinates to 0 atom_line += ' 0.0000 0.0000 0.0000 ' # Atom symbol: it should be the entry from the periodic table, using # atom type for now # atom_line += symbols.get(d['discrete_label']).ljust(3) atom_line += d['label'].ljust(3) # Lots of unnecessary atomic information: atom_line += ' 0 0 0 0 0 0 0 0 0 0 0 0\n' sdf_string += atom_line # Bond block for i, j, k in graph.edges_iter(data=True): edge_line = '' # Use the stored atom ids for the bonds, plus one edge_line += str(i + 1).rjust(3) + \ str(j + 1).rjust(3) + k['label'].rjust(3) # More information edge_line += ' 0 0 0 0\n' sdf_string += edge_line sdf_string += 'M END' # sdf_string += 'M END\n\n$$$$' return sdf_string
def contract_graph(graph): '''convenience for graphlearn/eden''' import eden.graph as eg graph = eg._revert_edge_to_vertex_transform(graph) return graph
def out(self): # copy and if digraph make graph graph = nx.Graph(self._base_graph) graph = _revert_edge_to_vertex_transform(graph) graph.graph['score'] = self.__dict__.get("_score", "?") return graph