/// <summary> /// Makes an array containing the morgan numbers of the atoms of /// atomContainer. These number are the extended connectivity values and not /// the lexicographic smallest labelling on the graph. /// </summary> /// <param name="molecule">the molecule to analyse.</param> /// <returns>The morgan numbers value.</returns> public static long[] GetMorganNumbers(IAtomContainer molecule) { int order = molecule.Atoms.Count; long[] currentInvariants = new long[order]; long[] previousInvariants = new long[order]; int[][] graph = Arrays.CreateJagged <int>(order, InitialDegree); int[] degree = new int[order]; // which atoms are the non-hydrogens. int[] nonHydrogens = new int[order]; for (int v = 0; v < order; v++) { nonHydrogens[v] = molecule.Atoms[v].AtomicNumber.Equals(AtomicNumbers.H) ? 0 : 1; } // build the graph and initialise the current connectivity // value to the number of connected non-hydrogens foreach (var bond in molecule.Bonds) { int u = molecule.Atoms.IndexOf(bond.Begin); int v = molecule.Atoms.IndexOf(bond.End); graph[u] = Ints.EnsureCapacity(graph[u], degree[u] + 1, InitialDegree); graph[v] = Ints.EnsureCapacity(graph[v], degree[v] + 1, InitialDegree); graph[u][degree[u]++] = v; graph[v][degree[v]++] = u; currentInvariants[u] += nonHydrogens[v]; currentInvariants[v] += nonHydrogens[u]; } // iteratively sum the connectivity values for each vertex for (int i = 0; i < order; i++) { Array.Copy(currentInvariants, 0, previousInvariants, 0, order); for (int u = 0; u < order; u++) { currentInvariants[u] = 0; // for each of the vertices adjacent to <paramref name="u"/> sum their // previous connectivity value int[] neighbors = graph[u]; for (int j = 0; j < degree[u]; j++) { int v = neighbors[j]; currentInvariants[u] += previousInvariants[v] * nonHydrogens[v]; } } } return(currentInvariants); }