def _isomorphism(gra1, gra2): """ """ nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.isomorphism(nxg1, nxg2) return iso_dct
def full_subgraph_isomorphism(gra1, gra2): """ gra2 is fully isomorphic to a subgraph of gra1 TODO: DEPRECATE """ assert gra1 == explicit(gra1) and gra2 == explicit(gra2) nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.subgraph_isomorphism(nxg1, nxg2) return iso_dct
def full_isomorphism(gra1, gra2): """ full graph isomorphism TODO: DEPRECATE """ assert gra1 == explicit(gra1) and gra2 == explicit(gra2) nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.isomorphism(nxg1, nxg2) return iso_dct
def _isomorphism(gra1, gra2, igraph=False): """ """ if igraph: igr1 = _igraph.from_graph(gra1) igr2 = _igraph.from_graph(gra2) iso_dcts = _igraph.isomorphisms(igr1, igr2) iso_dct = iso_dcts[0] if iso_dcts else None else: nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.isomorphism(nxg1, nxg2) return iso_dct
def backbone_isomorphism(gra1, gra2): """ graph backbone isomorphism TODO: DEPRECATE for implicit graphs, this is the relabeling of `gra1` to produce `gra2` for other graphs, it gives the correspondences between backbone atoms """ gra1 = implicit(gra1) gra2 = implicit(gra2) nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.isomorphism(nxg1, nxg2) return iso_dct
def full_isomorphism(gra1, gra2, igraph=False): """ full graph isomorphism TODO: DEPRECATE """ assert gra1 == explicit(gra1) and gra2 == explicit(gra2) if igraph: igr1 = _igraph.from_graph(gra1) igr2 = _igraph.from_graph(gra2) iso_dcts = _igraph.isomorphisms(igr1, igr2) iso_dct = iso_dcts[0] if iso_dcts else None else: nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.isomorphism(nxg1, nxg2) return iso_dct
def atom_shortest_paths(gra): """ shortest paths between any two atoms in the graph :returns: a 2d dictionary keyed by pairs of atoms """ nxg = _networkx.from_graph(gra) sp_dct = dict(_networkx.all_pairs_shortest_path(nxg)) return sp_dct
def align(geo1, geo2): """ For a given species, align geo1 to geo2 by reordering the the atom ordering of geo1 so that it matches that of geo2. Toy function calling igraph directly for now """ if not is_atom(geo1): # Convert the two geoms to graphs (need explicit graph) gra1, gra2 = graph_conv(geo1), graph_conv(geo2) # Generate an isomorphism so that we can map gra1 and gra2 # Will only be able to map most heavy atoms. # Can't figure out what to do when heavy atom has two similar R groups # Use a distance matrix evaluation to handle that igr1 = _networkx.from_graph(gra1) igr2 = _networkx.from_graph(gra2) iso_dcts = _networkx.all_isomorphisms(igr1, igr2) if any(iso_dcts): # Calculate distance matrix of geo2 geo2_distmat = distance_matrix(geo2) # Find isomorphism that minimizes diff in dist mat aligned_geo1s, diffs = [], [] for iso_dct in iso_dcts: # Reorder the geo1 using the isomoprhism _geo1 = reorder(geo1, iso_dct) aligned_geo1s.append(_geo1) # Calculate metric for difference between geo1 and geo2 # Distance Matrices by summing all elements of matrix: # |G2DistMat - G2DistMat| diffs.append( numpy.sum(numpy.abs(geo2_distmat - distance_matrix(_geo1)))) # Assigned aligned geo1 to the one with smalled dist matrix diff min_idx, _ = min(enumerate(diffs), key=operator.itemgetter(1)) align_geo1 = aligned_geo1s[min_idx] else: align_geo1 = None else: align_geo1 = geo1 return align_geo1
def backbone_isomorphism(gra1, gra2, igraph=False): """ graph backbone isomorphism TODO: DEPRECATE for implicit graphs, this is the relabeling of `gra1` to produce `gra2` for other graphs, it gives the correspondences between backbone atoms """ gra1 = implicit(gra1) gra2 = implicit(gra2) if igraph: igr1 = _igraph.from_graph(gra1) igr2 = _igraph.from_graph(gra2) iso_dcts = _igraph.isomorphisms(igr1, igr2) iso_dct = iso_dcts[0] if iso_dcts else None else: nxg1 = _networkx.from_graph(gra1) nxg2 = _networkx.from_graph(gra2) iso_dct = _networkx.isomorphism(nxg1, nxg2) return iso_dct
def rings_bond_keys(gra): """ bond keys for each ring in the graph (minimal basis) """ bnd_keys = bond_keys(gra) def _ring_bond_keys(rng_atm_keys): return frozenset(filter(lambda x: x <= rng_atm_keys, bnd_keys)) nxg = _networkx.from_graph(gra) rng_atm_keys_lst = _networkx.minimum_cycle_basis(nxg) rng_bnd_keys_lst = frozenset(map(_ring_bond_keys, rng_atm_keys_lst)) return rng_bnd_keys_lst
def connected_components_atom_keys(gra): """ atom keys for each connected component in the graph """ nxg = _networkx.from_graph(gra) cmp_gra_atm_keys_lst = _networkx.connected_component_atom_keys(nxg) return cmp_gra_atm_keys_lst