bool IsInChain(BondingAtom b, int parentIndex) { if (b.Element != Element.C) return false; if (parentIndex != 0 && b == chain[parentIndex - 1]) return true; if (parentIndex != chain.Length - 1 && b == chain[parentIndex + 1]) return true; return false; }
static string AlkylSMILES(BondingAtom b, BondingAtom parent) { string smiles = b.Element.ToString(); foreach (BondingAtom child in b) { if (child != parent) smiles += AlkylSMILES(child, b); } return smiles; }
private void AddChildren(BondingAtom b, BondingAtom parent) { foreach (BondingAtom child in b) { if (child != parent) { atoms.Add(child); AddChildren(child, b); } } }
int AlkylLength(BondingAtom b, BondingAtom parent) { foreach (BondingAtom child in b) { if (child != parent) return AlkylLength(child, b) + 1; } return 1; }
public OrganicMolecule(BondingAtom a) : this(new Molecule(a)) { }
public OrganicMoleculeNamer(BondingAtom[] chain, Group highest) { this.chain = chain; this.highest = highest; prefixes = new Dictionary<string, List<int>>(); suffixes = new Dictionary<string, List<int>>(); EnumerateChain(); }
int LongestChildChain(BondingAtom b, BondingAtom parent) { int max = 0; foreach (BondingAtom child in b) { if (child != parent && child.Element == Element.C) { max = Math.Max(max, LongestChildChain(child, b) + 1); } } return max; //return b.Where(child => child != parent && child.Element == Element.C).Max<BondingAtom, int>(child => LongestChildChain(child, b) + 1); }
bool IsEnd(BondingAtom b) { int bondedCInChain = 0; foreach (BondingAtom atom in b) { if (chain.Contains(atom)) bondedCInChain++; } return bondedCInChain < 2; //return b.Count(atom => chain.Contains(atom)) < 2; }
bool HasNonAlkylChild(BondingAtom b, BondingAtom parent) { foreach (Bond bond in b.Bonds) { if (bond.Target != parent) { if (bond.Target.Element != Element.C || bond.Order != 1) return true; if (HasNonAlkylChild(bond.Target, b)) return true; } } return false; //return b.Bonds.Where(bond => bond.Target != parent).Any( // bond => bond.Target.Element != Element.C || bond.Order != 1 || HasNonAlkylChild(bond.Target, b)); }
BondingAtom ChildWithLongestChain(BondingAtom b, BondingAtom parent) { int max = 0; BondingAtom maxChild = null; foreach (BondingAtom child in b) { if (child != parent && child.Element == Element.C) { int childChainLength = LongestChildChain(child, b) + 1; if (childChainLength > max) maxChild = child; max = Math.Max(max, childChainLength); } } return maxChild; }
void AssignChildren(BondingAtom b, BondingAtom parent) { foreach (BondingAtom child in b) { if (child != parent && child.Element == Element.C) { if (HasNonAlkylChild(child, b)) { chain.Add(child); AssignChildren(child, b); return; } } } //what if all children were alkyl? BondingAtom child2 = ChildWithLongestChain(b, parent); if (child2 != null) { chain.Add(child2); AssignChildren(child2, b); } }
public BondingAtom[] GetChain() { BondingAtom[] bAs = new BondingAtom[chain.Count]; bAs[0] = chain.Find((BondingAtom b) => IsEnd(b)); BondingAtom parent = null; for (int i = 1; i < bAs.Length; i++) { foreach (BondingAtom b in bAs[i - 1]) { if (b.Element == Element.C && b != parent && chain.Contains(b)) { parent = bAs[i - 1]; bAs[i] = b; break; } } } return bAs; }
public Molecule(BondingAtom b) { atoms = new List<BondingAtom>(); atoms.Add(b); AddChildren(b, null); }