Skip to content

Commit

Permalink
Add IAtomContainer.RemoveBond(bond).
Browse files Browse the repository at this point in the history
Fix AtomContainer.RemoveBond to remove stereoElement info.
  • Loading branch information
k-ujihara committed Apr 14, 2019
1 parent 922cd22 commit 90d75ec
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 69 deletions.
3 changes: 3 additions & 0 deletions NCDK/Graphs/AllPairsShortestPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public void RemoveProperty(object description) { }
public void Remove(IElectronContainer electronContainer)
{ }

public void RemoveBond(IBond bond)
{ }

public IBond RemoveBond(IAtom atom0, IAtom atom1)
{
return null;
Expand Down
7 changes: 7 additions & 0 deletions NCDK/IAtomContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ public interface IAtomContainer
/// <returns>The bond that connects the two atoms</returns>
IBond RemoveBond(IAtom atom1, IAtom atom2);

/// <summary>
/// Removes the bond.
/// </summary>
/// <param name="bond">The bond to remove.</param>
/// <returns></returns>
void RemoveBond(IBond bond);

/// <summary>
/// <see langword="true"/>, if the <see cref="IAtomContainer"/> contains the given atom object.
/// </summary>
Expand Down
11 changes: 6 additions & 5 deletions NCDK/Isomorphisms/Matchers/QueryBond.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public class QueryBond
/// <example>
/// <code>
/// // pi-bond in a ring
/// Expr e = new Expr(IS_IN_RING);
/// e.And(new Expr(ALIPHATIC_ORDER, 2));
/// Expr e = new Expr(ExprType.IsInRing);
/// e.And(new Expr(ExprType.AliphaticOrder, 2));
/// new QueryBond(beg, end, e);
/// </code>
/// </example>
Expand All @@ -60,7 +60,7 @@ public QueryBond(IAtom beg, IAtom end, Expr expr)
/// </summary>
/// <example>
/// <code>
/// new QueryBond(beg, end, IS_IN_RING);
/// new QueryBond(beg, end, ExprType.IsInRing);
/// </code>
/// </example>
/// <param name="type">the expression type</param>
Expand All @@ -71,18 +71,19 @@ public QueryBond(IAtom beg, IAtom end, ExprType type)
while (Atoms.Count < 2)
Atoms.Add(null);
}

/// <summary>
/// Constructs an query bond from an expression type and value.
/// </summary>
/// <example>
/// <code>
/// new QueryBond(beg, end, ALIPHATIC_ORDER, 8);
/// new QueryBond(beg, end, ExprType.AliphaticOrder, 8);
/// </code></example>
/// <param name="type">the expression type</param>
/// <param name="val">the expression value</param>
public QueryBond(IAtom beg, IAtom end, ExprType type, int val)
: this(beg, end, BondOrder.Unset, BondStereo.None)
{
{
this.Expression.SetPrimitive(type, val);
while (Atoms.Count < 2)
Atoms.Add(null);
Expand Down
7 changes: 7 additions & 0 deletions NCDK/NCDKUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,12 @@ public static IAtom Add(this IAtomContainer mol, ChemicalElement element)
mol.Add(atom);
return atom;
}

public static IAtom AddAtom(this IAtomContainer mol, string elementSymbol)
{
var atom = mol.Builder.NewAtom(elementSymbol);
mol.Add(atom);
return atom;
}
}
}
50 changes: 35 additions & 15 deletions NCDK/TT/AtomContainer.tt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace <#= ns.NS #>
/// <summary>
/// Stereo elements contained by this object.
/// </summary>
internal List<IStereoElement<IChemObject, IChemObject>> stereoElements;
internal List<IStereoElement<IChemObject, IChemObject>> stereo;

/// <inheritdoc/>
public override IChemObjectBuilder Builder => builder;
Expand Down Expand Up @@ -141,7 +141,7 @@ namespace <#= ns.NS #>
List<IStereoElement<IChemObject, IChemObject>> oldStereo = null;
List<IStereoElement<IChemObject, IChemObject>> newStereo = null;

foreach (var se in parent.stereoElements)
foreach (var se in parent.stereo)
{
if (se.Contains(oldAtom))
{
Expand All @@ -161,9 +161,9 @@ namespace <#= ns.NS #>
if (oldStereo != null)
{
foreach (var stereo in oldStereo)
parent.stereoElements.Remove(stereo);
parent.stereo.Remove(stereo);
foreach (var stereo in newStereo)
parent.stereoElements.Add(stereo);
parent.stereo.Add(stereo);
}

<# if (!ns.IsSilent) { #>
Expand All @@ -178,28 +178,28 @@ namespace <#= ns.NS #>
ObservableChemObjectCollection<IBond> bonds,
ObservableChemObjectCollection<ILonePair> lonePairs,
ObservableChemObjectCollection<ISingleElectron> singleElectrons,
List<IStereoElement<IChemObject, IChemObject>> stereoElements)
List<IStereoElement<IChemObject, IChemObject>> stereo)
{
this.atoms = atoms;
this.bonds = bonds;
this.lonePairs = lonePairs;
this.singleElectrons = singleElectrons;
this.stereoElements = stereoElements;
this.stereo = stereo;
}

public AtomContainer(
IEnumerable<IAtom> atoms,
IEnumerable<IBond> bonds,
IEnumerable<ILonePair> lonePairs,
IEnumerable<ISingleElectron> singleElectrons,
IEnumerable<IStereoElement<IChemObject, IChemObject>> stereoElements)
IEnumerable<IStereoElement<IChemObject, IChemObject>> stereo)
{
Init(
new ObservableChemObjectCollection_IAtom(this, atoms ?? Array.Empty<IAtom>()),
CreateObservableChemObjectCollection(bonds ?? Array.Empty<IBond>(), true),
CreateObservableChemObjectCollection(lonePairs ?? Array.Empty<ILonePair>(), true),
CreateObservableChemObjectCollection(singleElectrons ?? Array.Empty<ISingleElectron>(), true),
new List<IStereoElement<IChemObject, IChemObject>>(stereoElements ?? Array.Empty<IStereoElement<IChemObject, IChemObject>>())
new List<IStereoElement<IChemObject, IChemObject>>(stereo ?? Array.Empty<IStereoElement<IChemObject, IChemObject>>())
);
}

Expand Down Expand Up @@ -271,7 +271,7 @@ var thisListener = ns.IsSilent ? "null" : "this";
public virtual IList<ISingleElectron> SingleElectrons => singleElectrons;

/// <inheritdoc/>
public virtual ICollection<IStereoElement<IChemObject, IChemObject>> StereoElements => stereoElements;
public virtual ICollection<IStereoElement<IChemObject, IChemObject>> StereoElements => stereo;

/// <summary>
/// Returns the bond that connects the two given atoms.
Expand Down Expand Up @@ -414,7 +414,7 @@ var thisListener = ns.IsSilent ? "null" : "this";
foreach (var singleElectron in that.SingleElectrons.Where(singleElectron => !Contains(singleElectron)))
SingleElectrons.Add(singleElectron);
foreach (var se in that.StereoElements)
stereoElements.Add(se);
stereo.Add(se);

<# if (!ns.IsSilent) { #> NotifyChanged(); <# } #>
}
Expand Down Expand Up @@ -498,9 +498,9 @@ var thisListener = ns.IsSilent ? "null" : "this";
singleElectrons.Remove(singleElectron);
}
{
var toRemove = stereoElements.Where(stereoElement => stereoElement.Contains(atom)).ToList();
var toRemove = stereo.Where(stereoElement => stereoElement.Contains(atom)).ToList();
foreach (var stereoElement in toRemove)
stereoElements.Remove(stereoElement);
stereo.Remove(stereoElement);
}

Atoms.Remove(atom);
Expand All @@ -521,7 +521,7 @@ var thisListener = ns.IsSilent ? "null" : "this";
foreach (var atom in atoms)
atom.Listeners?.Remove(this);
atoms.Clear();
stereoElements.Clear();
stereo.Clear();

<# if (!ns.IsSilent) { #> NotifyChanged(); <# } #>
}
Expand Down Expand Up @@ -549,9 +549,29 @@ var thisListener = ns.IsSilent ? "null" : "this";
public virtual IBond RemoveBond(IAtom atom1, IAtom atom2)
{
var bond = GetBond(atom1, atom2);
RemoveBond(bond);
return bond;
}

public virtual void RemoveBond(IBond bond)
{
if (bond != null)
{
var stereoToRemove = new List<IStereoElement<IChemObject, IChemObject>>();
foreach (var stereoBond in stereo)
{
if (stereoBond.Focus == bond
|| stereoBond.Carriers.Contains(bond))
{
stereoToRemove.Add(stereoBond);
continue;
}
}
foreach (var remove in stereoToRemove)
stereo.Remove(remove);

Bonds.Remove(bond);
return bond;
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -586,7 +606,7 @@ var thisListener = ns.IsSilent ? "null" : "this";
clone.bonds = CreateObservableChemObjectCollection(bonds.Where(n => n != null).Select(n => (IBond)n.Clone(map)), true);
clone.lonePairs = CreateObservableChemObjectCollection(lonePairs.Where(n => n != null).Select(n => (ILonePair)n.Clone(map)), true);
clone.singleElectrons = CreateObservableChemObjectCollection(singleElectrons.Where(n => n != null).Select(n => (ISingleElectron)n.Clone(map)), true);
clone.stereoElements = new List<IStereoElement<IChemObject, IChemObject>>(stereoElements.Select(n => (IStereoElement<IChemObject, IChemObject>)n.Clone(map)));
clone.stereo = new List<IStereoElement<IChemObject, IChemObject>>(stereo.Select(n => (IStereoElement<IChemObject, IChemObject>)n.Clone(map)));

// update sgroups
var sgroups = this.GetCtabSgroups();
Expand Down
Loading

0 comments on commit 90d75ec

Please sign in to comment.