/// <summary> /// Constructs a query bond with a given order and stereo orientation from an array of atoms. /// </summary> /// <param name="atom1">the first Atom in the query bond</param> /// <param name="atom2">the second Atom in the query bond</param> /// <param name="order">the query bond order</param> /// <param name="stereo">a descriptor the stereochemical orientation of this query bond</param> public QueryBond(IAtom atom1, IAtom atom2, BondOrder order, BondStereo stereo, IChemObjectBuilder builder) { this.builder = builder; SetAtoms(new[] { atom1, atom2 }); this.order = order; this.stereo = stereo; }
/// <summary> /// Determines if the atom can be of atom type. That is, it sees if this /// atom type only differs in bond orders, or implicit hydrogen count. /// </summary> private static bool CouldMatchAtomType(IAtom atom, double bondOrderSum, BondOrder maxBondOrder, IAtomType type) { Debug.WriteLine($"{nameof(CouldMatchAtomType)}: ... matching atom {atom} vs {type}"); var hcount = atom.ImplicitHydrogenCount.Value; var charge = atom.FormalCharge.Value; if (charge == type.FormalCharge) { Debug.WriteLine($"{nameof(CouldMatchAtomType)}e: formal charge matches..."); if (bondOrderSum + hcount <= type.BondOrderSum) { Debug.WriteLine($"{nameof(CouldMatchAtomType)}: bond order sum is OK..."); if (!BondManipulator.IsHigherOrder(maxBondOrder, type.MaxBondOrder)) { Debug.WriteLine($"{nameof(CouldMatchAtomType)}: max bond order is OK... We have a match!"); return(true); } } else { Debug.WriteLine($"{nameof(CouldMatchAtomType)}: no match {(bondOrderSum + hcount)} > {type.BondOrderSum}"); } } else { Debug.WriteLine($"{nameof(CouldMatchAtomType)}: formal charge does NOT match..."); } Debug.WriteLine($"{nameof(CouldMatchAtomType)}e: No Match"); return(false); }
/// <summary> /// Returns the maximum bond order for the two bond orders. /// </summary> /// <param name="firstOrder">first bond order to compare</param> /// <param name="secondOrder">second bond order to compare</param> /// <returns>The maximum bond order found</returns> public static BondOrder GetMaximumBondOrder(BondOrder firstOrder, BondOrder secondOrder) { if (firstOrder == BondOrder.Unset) { if (secondOrder == BondOrder.Unset) { throw new ArgumentException("Both bond orders are unset"); } return(secondOrder); } if (secondOrder == BondOrder.Unset) { if (firstOrder == BondOrder.Unset) { throw new ArgumentException("Both bond orders are unset"); } return(firstOrder); } if (IsHigherOrder(firstOrder, secondOrder)) { return(firstOrder); } else { return(secondOrder); } }
/// <summary> /// Creates a new bond energy for the given elements and /// bond order. /// /// <param name="symbol1">element symbol for the first atom</param> /// <param name="symbol2">element symbol for the second atom</param> /// <param name="order">bond order</param> /// <param name="energy">energy for this bond type</param> /// </summary> public BondEnergy(string symbol1, string symbol2, BondOrder order, int energy) { this.symbol1 = symbol1; this.symbol2 = symbol2; this.bondOrder = order; this.energy = energy; }
private static int CountAttachedBonds(IAtomContainer container, IAtom atom, BondOrder order, string symbol) { var neighbors = container.GetConnectedBonds(atom).ToReadOnlyList(); int neighborcount = neighbors.Count; int doubleBondedAtoms = 0; for (int i = neighborcount - 1; i >= 0; i--) { var bond = neighbors[i]; if (bond.Order == order) { if (bond.Atoms.Count == 2 && bond.Contains(atom)) { if (symbol != null) { var neighbor = bond.GetOther(atom); if (string.Equals(neighbor.Symbol, symbol, StringComparison.Ordinal)) { doubleBondedAtoms++; } } else { doubleBondedAtoms++; } } } } return(doubleBondedAtoms); }
internal static bool TestIsAlk_x_ne(IAtomContainer mol, BondOrder order) { if (mol == null) { return(false); } if (!mol.IsHydrocarbon()) { return(false); } foreach (var bond in mol.Bonds) { if (bond.Begin.Symbol != "C" || bond.End.Symbol != "C") { continue; } if (bond.Order != order) { continue; } return(true); } return(false); }
/// <summary> /// Constructs a new <see cref="IDifference"/> object. /// </summary> /// <param name="name">a name reflecting the nature of the created <see cref="IDifference"/></param> /// <param name="first">the first object to compare</param> /// <param name="second">the second object to compare</param> /// <returns>an <see cref="IDifference"/> reflecting the differences between the first and second object</returns> public static IDifference Construct(string name, BondOrder first, BondOrder second) { if (first == second) { return(null); } return(new BondOrderDifference(name, first, second)); }
public void TestGetBondOrder() { BondEnergy instance = new BondEnergy("H", "I", BondOrder.Single, 295); BondOrder expResult = BondOrder.Single; BondOrder result = instance.BondOrder; Assert.AreEqual(expResult, result); }
static IBond Bond(IAtomContainer m, int v, int w, BondOrder ord, bool arom) { IBond b = builder.NewBond(m.Atoms[v], m.Atoms[w]); b.Order = ord; b.IsAromatic = arom; return(b); }
private void UpdateElectronCount(BondOrder order) { if (order == BondOrder.Unset) { return; } this.ElectronCount = order.Numeric() * 2; }
public Bond(IEnumerable <IAtom> atoms, BondOrder order, BondStereo stereo) : base() { InitAtoms(atoms); this.order = order; UpdateElectronCount(order); this.stereo = stereo; }
/// <summary> /// Returns true if the first bond has a higher bond order than the second bond. /// It returns false if the bond order is equal, and if the order of the first /// bond is lower than that of the second. Also returns false if either bond /// order is unset. /// </summary> /// <param name="first">The first bond order object</param> /// <param name="second">The second bond order object</param> /// <returns>true if the first bond order is higher than the second one, false otherwise</returns> /// <seealso cref="IsLowerOrder(BondOrder, BondOrder)"/> public static bool IsHigherOrder(BondOrder first, BondOrder second) { if (first == BondOrder.Unset || second == BondOrder.Unset) { return(false); } return(first.CompareTo(second) > 0); }
void AssertBondOrders(IAtomContainer ac, params BondOrder[] expected) { Kekulization.Kekulize(ac); IBond[] bonds = ac.Bonds.ToArray();; BondOrder[] actual = new BondOrder[bonds.Length]; for (int i = 0; i < bonds.Length; i++) { actual[i] = bonds[i].Order; } Assert.IsTrue(Compares.AreEqual(expected, actual)); }
public void TestGetEnergies() { IAtom sourceAtom = new Atom("C"); IAtom targetAtom = new Atom("C"); BondOrder bondOrder = BondOrder.Single; BondEnergies instance = new BondEnergies(); int expResult = 346; int result = instance.GetEnergies(sourceAtom, targetAtom, bondOrder); Assert.AreEqual(expResult, result); }
/// <summary> /// Returns the maximum bond order for a List of bonds, given an iterator to the list. /// <param name="bonds">An iterator for the list of bonds</param> /// <returns>The maximum bond order found</returns> /// </summary> /// <seealso cref="GetMaximumBondOrder(IEnumerable{IBond})"/> public static BondOrder GetMaximumBondOrder(IEnumerable <IBond> bonds) { BondOrder maxOrder = BondOrder.Single; foreach (var bond in bonds) { if (IsHigherOrder(bond.Order, maxOrder)) { maxOrder = bond.Order; } } return(maxOrder); }
/// <summary> /// Returns the minimum bond order for a List of bonds, given an iterator /// to the list. /// </summary> /// <param name="bonds">An iterator for the list of bonds</param> /// <returns>The minimum bond order found</returns> /// <seealso cref="GetMinimumBondOrder(IEnumerable{IBond})"/> public static BondOrder GetMinimumBondOrder(IEnumerable <IBond> bonds) { BondOrder minOrder = BondOrder.Sextuple; foreach (var bond in bonds) { if (IsLowerOrder(bond.Order, minOrder)) { minOrder = bond.Order; } } return(minOrder); }
public void ccBondTest(BondOrder order) { IAtomContainer cc = builder.NewAtomContainer(); cc.Atoms.Add(builder.NewAtom("C")); cc.Atoms.Add(builder.NewAtom("C")); cc.AddBond(cc.Atoms[0], cc.Atoms[1], order); string signature = SignatureForAtom(cc, 0); IAtomContainer reconstructed = Reconstruct(signature); Assert.AreEqual(2, reconstructed.Atoms.Count); Assert.AreEqual(1, reconstructed.Bonds.Count); Assert.AreEqual(order, reconstructed.Bonds[0].Order); }
/// <summary> /// Get the single bond equivalent (SBE) of a list of bonds, given an iterator to the list. /// </summary> /// <param name="bonds">An iterator to the list of bonds</param> /// <returns>The SBE sum</returns> public static int GetSingleBondEquivalentSum(IEnumerable <IBond> bonds) { int sum = 0; foreach (var bond in bonds) { BondOrder order = bond.Order; if (!order.IsUnset()) { sum += order.Numeric(); } } return(sum); }
/// <summary> /// Calculate the number of bonds of a given type in an atomContainer /// </summary> /// <param name="order">The bond order. Default is <see cref="BondOrder.Unset"/>, which means count all bonds.</param> /// <returns>The number of bonds of a certain type.</returns> public Result Calculate(IAtomContainer container, BondOrder order = BondOrder.Unset) { if (order.IsUnset()) { var count = container.Bonds .Select(bond => bond.Atoms .Count(atom => atom.AtomicNumber.Equals(AtomicNumbers.H))) .Sum(); return(new Result(count, order)); } else { var count = container.Bonds.Count(bond => bond.Order.Equals(order)); return(new Result(count, order)); } }
public static int ConvertBondOrderToInt(BondOrder bondOrder) { switch (bondOrder) { case BondOrder.None: return(0); case BondOrder.Single: return(1); case BondOrder.Double: return(2); case BondOrder.Triple: return(3); case BondOrder.Aromatic: return(4); // hmmm... default: return(1); } }
/// <summary> /// Method that tests if the matched <see cref="IAtomType"/> and the <see cref="IAtom"/> are /// consistent. For example, it tests if hybridization states and formal charges are equal. /// // @cdk.bug 1897589 /// </summary> private void AssertConsistentProperties(IAtomContainer mol, IAtom atom, IAtomType matched) { // X has no properties; nothing to match if (string.Equals("X", matched.AtomTypeName, StringComparison.Ordinal)) { return; } if (!atom.Hybridization.IsUnset() && !matched.Hybridization.IsUnset()) { Assert.AreEqual(atom.Hybridization, matched.Hybridization, "Hybridization does not match"); } if (atom.FormalCharge != null && matched.FormalCharge != null) { Assert.AreEqual(atom.FormalCharge, matched.FormalCharge, "Formal charge does not match"); } var connections = mol.GetConnectedBonds(atom); int connectionCount = connections.Count(); if (matched.FormalNeighbourCount != null) { Assert.IsFalse(connectionCount > matched.FormalNeighbourCount, "Number of neighbors is too high"); } if (!matched.MaxBondOrder.IsUnset()) { BondOrder expectedMax = matched.MaxBondOrder; foreach (var bond in connections) { BondOrder order = bond.Order; if (!order.IsUnset()) { if (BondManipulator.IsHigherOrder(order, expectedMax)) { Assert.Fail("At least one bond order exceeds the maximum for the atom type"); } } else if (bond.IsSingleOrDouble) { if (expectedMax != BondOrder.Single && expectedMax != BondOrder.Double) { Assert.Fail("A single or double flagged bond does not match the bond order of the atom type"); } } } } }
/// <summary> /// Generate a LineElement or an ElementGroup of LineElements for this bond. /// This version should be used if you want to override the type - for /// example, for ring double bonds. /// </summary> /// <param name="bond">the bond to generate for</param> /// <param name="type">the type of the bond - single, double, etc</param> /// <param name="model">the renderer model</param> /// <returns>one or more rendering elements</returns> public virtual IRenderingElement GenerateBondElement(IBond bond, BondOrder type, RendererModel model) { // More than 2 atoms per bond not supported by this module if (bond.Atoms.Count > 2) { return(null); } // is object right? if not replace with a good one Vector2 point1 = bond.Begin.Point2D.Value; Vector2 point2 = bond.End.Point2D.Value; Color color = this.GetColorForBond(bond, model); double bondWidth = this.GetWidthForBond(bond, model); double bondDistance = model.GetBondDistance() / model.GetScale(); if (type == BondOrder.Single) { return(new LineElement(ToPoint(point1), ToPoint(point2), bondWidth, color)); } else { ElementGroup group = new ElementGroup(); switch (type) { case BondOrder.Double: CreateLines(point1, point2, bondWidth, bondDistance, color, group); break; case BondOrder.Triple: CreateLines(point1, point2, bondWidth, bondDistance * 2, color, group); group.Add(new LineElement(ToPoint(point1), ToPoint(point2), bondWidth, color)); break; case BondOrder.Quadruple: CreateLines(point1, point2, bondWidth, bondDistance, color, group); CreateLines(point1, point2, bondWidth, bondDistance * 4, color, group); break; default: break; } return(group); } }
/// <summary> /// Constructs an isotope by copying the symbol, atomic number, /// flags, identifier, exact mass, natural abundance and mass /// number from the given IIsotope. It does not copy the /// listeners and properties. If the element is an instanceof /// IAtomType, then the maximum bond order, bond order sum, /// van der Waals and covalent radii, formal charge, hybridization, /// electron valency, formal neighbour count and atom type name /// are copied too. /// </summary> /// <param name="element">IIsotope to copy information from</param> public AtomType(IElement element) : base(element) { if (element is IAtomType aa) { maxBondOrder = aa.MaxBondOrder; bondOrderSum = aa.BondOrderSum; covalentRadius = aa.CovalentRadius; formalCharge = aa.FormalCharge; hybridization = aa.Hybridization; valency = aa.Valency; formalNeighbourCount = aa.FormalNeighbourCount; atomTypeName = aa.AtomTypeName; SetIsHydrogenBondAcceptorWithoutNotify(aa.IsHydrogenBondAcceptor); SetIsHydrogenBondDonorWithoutNotify(aa.IsHydrogenBondDonor); SetIsAromaticWithoutNotify(aa.IsAromatic); SetIsInRingWithoutNotify(aa.IsInRing); } }
/// <summary> /// This method tries to set the bond order on the current bond. /// </summary> /// <param name="atomContainer">The molecule</param> /// <param name="index">The index of the current bond</param> /// <exception cref="CDKException">when no suitable solution can be found</exception> private void CheckBond(IAtomContainer atomContainer, int index) { var bond = atomContainer.Bonds[index]; if (bond != null && bond.IsSingleOrDouble) { try { oldBondOrder = bond.Order; bond.Order = BondOrder.Single; SetMaxBondOrder(bond, atomContainer); } catch (CDKException e) { bond.Order = oldBondOrder; Debug.WriteLine(e); } } }
/// <summary> /// Create the <see cref="INode"/> matching the given <see cref="BondOrder"/>. /// </summary> /// <param name="order">bond order to return the matching <see cref="INode"/> for.</param> /// <returns>the matching <see cref="INode"/>.</returns> public INode Order2Resource(BondOrder order) { switch (order) { case BondOrder.Single: return(R_SINGLEBOND); case BondOrder.Double: return(R_DOUBLEBOND); case BondOrder.Triple: return(R_TRIPLEBOND); case BondOrder.Quadruple: return(R_QUADRUPLEBOND); default: return(null); } }
private void SetTargetSingleAtomMap(bool removeHydrogen) { int counter = 0; BondEnergies be = BondEnergies.Instance; foreach (var targetAtom in target.Atoms) { if ((removeHydrogen && !targetAtom.AtomicNumber.Equals(AtomicNumbers.H)) || (!removeHydrogen)) { foreach (var sourceAtoms in source.Atoms) { var mapAtoms = new Dictionary <IAtom, IAtom>(); if (string.Equals(targetAtom.Symbol, sourceAtoms.Symbol, StringComparison.OrdinalIgnoreCase)) { mapAtoms[sourceAtoms] = targetAtom; var bonds = source.GetConnectedBonds(sourceAtoms); double totalOrder = 0; foreach (var bond in bonds) { BondOrder order = bond.Order; totalOrder += order.Numeric() + BondEnergies.GetEnergies(bond); } if (sourceAtoms.FormalCharge != targetAtom.FormalCharge) { totalOrder += 0.5; } connectedBondOrder[counter] = totalOrder; mappings.Insert(counter++, mapAtoms); } } } else { Console.Error.WriteLine("Skipping Hydrogen mapping or This is not a single mapping case!"); } } }
private void SetSourceSingleAtomMap(IQueryAtomContainer source, bool removeHydrogen) { int counter = 0; BondEnergies be = BondEnergies.Instance; foreach (var sourceAtom in source.Atoms) { var smartAtom = (IQueryAtom)sourceAtom; if ((removeHydrogen && !smartAtom.AtomicNumber.Equals(AtomicNumbers.H)) || (!removeHydrogen)) { foreach (var targetAtom in target.Atoms) { var mapAtoms = new Dictionary <IAtom, IAtom>(); if (smartAtom.Matches(targetAtom)) { mapAtoms[sourceAtom] = targetAtom; var bonds = target.GetConnectedBonds(targetAtom); double totalOrder = 0; foreach (var bond in bonds) { BondOrder order = bond.Order; totalOrder += order.Numeric() + BondEnergies.GetEnergies(bond); } if (targetAtom.FormalCharge != sourceAtom.FormalCharge) { totalOrder += 0.5; } connectedBondOrder[counter] = totalOrder; mappings.Insert(counter++, mapAtoms); } } } else { Console.Error.WriteLine("Skipping Hydrogen mapping or This is not a single mapping case!"); } } }
internal ImmutableAtomType(IAtomType type) { this.element = type.Element; this.symbol = type.Symbol; this.atomicNumber = type.AtomicNumber; this.abundance = type.Abundance; this.exactMass = type.ExactMass; this.massNumber = type.MassNumber; this.formalCharge = type.FormalCharge; this.hybridization = type.Hybridization; this.formalNeighbourCount = type.FormalNeighbourCount; this.atomTypeName = type.AtomTypeName; this.maxBondOrder = type.MaxBondOrder; this.bondOrderSum = type.BondOrderSum; this.covalentRadius = type.CovalentRadius; this.isHydrogenBondAcceptor = type.IsHydrogenBondAcceptor; this.isHydrogenBondDonor = type.IsHydrogenBondDonor; this.isAromatic = type.IsAromatic; this.isAliphatic = type.IsAliphatic; this.isInRing = this.IsInRing; this.baseAtomType = type; if (type.Valency != null) { this.valency = type.Valency; } else { var piBondCount = type.GetProperty <int?>(CDKPropertyName.PiBondCount); if (piBondCount != null && formalNeighbourCount != null) { this.valency = (int)piBondCount + (int)formalNeighbourCount; } else { this.valency = null; } } }
/// <summary> /// Gets the hybridisation state of an atom. /// </summary> /// <param name="atom">atom</param> /// <returns>The hybridisationState value (sp=1;sp2=2;sp3=3)</returns> private static int GetHybridisationState(IAtom atom) { BondOrder maxBondOrder = atom.MaxBondOrder; // if (atom1.FormalNeighbourCount == 1 || maxBondOrder > 4) { if (atom.FormalNeighbourCount == 1) { // WTF?? } else if (atom.FormalNeighbourCount == 2 || maxBondOrder == BondOrder.Triple) { return(1); //sp } else if (atom.FormalNeighbourCount == 3 || (maxBondOrder == BondOrder.Double)) { return(2); //sp2 } else { return(3); //sp3 } return(-1); }
private static string BondOrderToKey(BondOrder order) { switch (order) { case BondOrder.Unset: return("nB"); case BondOrder.Single: return("nBs"); case BondOrder.Double: return("nBd"); case BondOrder.Triple: return("nBt"); case BondOrder.Quadruple: return("nBq"); default: throw new ArgumentException(nameof(order), "The only allowed bond types are single, double, truple, and quadruple bonds."); } }