// FIXME move to tools private string NormalizeConciseAndFormalCharge(string conciseS, int formalCharge) { if (conciseS != null) { CMLAtomArray atomArray = CreateAndAddAtomArrayAndFormalChargeFromConcise(conciseS); if (atomArray != null) { atomArray.Sort(SortType.CHFirst); conciseS = atomArray.GenerateConcise(formalCharge); } } return(conciseS); }
// element: formula // element: atomArray public void Normalize() { // create all possible renderings of formula // any or all may be present // concise var conciseAtt = this.Attribute(Attribute_concise); // formal charge int formalCharge = 0; if (this.Attribute(Attribute_formalCharge) != null) { formalCharge = int.Parse(this.Attribute(Attribute_formalCharge).Value, NumberFormatInfo.InvariantInfo); } string conciseS = conciseAtt?.Value; // convention string convention = this.Convention; // inline formula (might be SMILES) string inline = this.Inline; if (inline != null) { inline = inline.Trim(); } // atomArray CMLAtomArray atomArray = null; string atomArray2Concise = null; var atomArrays = this.Elements(XName_CML_atomArray).Cast <CMLAtomArray>().ToReadOnlyList(); if (atomArrays.Count > 1) { throw new ApplicationException($"Only one atomArray child allowed for formula; found: {atomArrays.Count}"); } else if (atomArrays.Count == 1) { atomArray = atomArrays.First(); atomArray.Sort(SortType.CHFirst); atomArray2Concise = atomArray.GenerateConcise(formalCharge); } // concise from inline if (inline != null) { if (SMILES.Equals(convention, StringComparison.Ordinal) || SMILES1.Equals(convention, StringComparison.Ordinal)) { } } if (conciseS == null) { if (atomArray2Concise != null) { conciseS = atomArray2Concise; } } if (conciseS != null) { conciseS = NormalizeConciseAndFormalCharge(conciseS, formalCharge); } // if no atomArray, create if (atomArray == null) { // causes problems with Jmol } else { CheckAtomArrayFormat(atomArray); } if (atomArray != null) { atomArray.Sort(SortType.CHFirst); } // check consistency if (atomArray2Concise != null && !atomArray2Concise.Equals(conciseS, StringComparison.Ordinal)) { throw new ApplicationException($"concise ({conciseS}) and atomArray ({atomArray2Concise}) differ"); } if (conciseS != null) { // by this time we may have generated a non-zero formal charge, so normalize it into concise conciseS = NormalizeConciseAndFormalCharge(conciseS, this.FormalCharge); ForceConcise(conciseS); } }