Skip to content

Commit

Permalink
Added to CMLCoreModule capability to read atomParity elements. Corrected
Browse files Browse the repository at this point in the history
spelling error. Formatting changes.

Signed-off-by: John May <john.wilkinsonmay@gmail.com>
  • Loading branch information
AndyHowlettGitHub authored and johnmay committed Aug 11, 2014
1 parent 80fd3af commit b0f1f7f
Showing 1 changed file with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import org.openscience.cdk.Atom;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.dict.DictRef;
import org.openscience.cdk.geometry.CrystalGeometryTools;
Expand All @@ -52,6 +53,8 @@
import org.openscience.cdk.interfaces.IReactionSet;
import org.openscience.cdk.interfaces.ISingleElectron;
import org.openscience.cdk.interfaces.IStrand;
import org.openscience.cdk.interfaces.ITetrahedralChirality.Stereo;
import org.openscience.cdk.stereo.TetrahedralChirality;
import org.openscience.cdk.tools.ILoggingTool;
import org.openscience.cdk.tools.LoggingToolFactory;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;
Expand Down Expand Up @@ -114,11 +117,17 @@ public class CMLCoreModule implements ICMLModule {
protected List<String> zfract;
protected List<String> hCounts;
protected List<String> atomParities;
protected List<String> parityARef1;
protected List<String> parityARef2;
protected List<String> parityARef3;
protected List<String> parityARef4;
protected List<String> atomDictRefs;
protected List<String> atomAromaticities;
protected List<String> spinMultiplicities;
protected List<String> occupancies;
protected Map<Integer,List<String>> atomCustomProperty;
protected boolean parityAtomsGiven;
protected boolean parityGiven;


protected int bondCounter;
Expand Down Expand Up @@ -199,6 +208,10 @@ public void inherit(ICMLModule convention) {
this.zfract = conv.zfract;
this.hCounts = conv.hCounts;
this.atomParities = conv.atomParities;
this.parityARef1 = conv.parityARef1;
this.parityARef2 = conv.parityARef2;
this.parityARef3 = conv.parityARef3;
this.parityARef4 = conv.parityARef4;
this.atomDictRefs = conv.atomDictRefs;
this.atomAromaticities = conv.atomAromaticities;
this.spinMultiplicities = conv.spinMultiplicities;
Expand Down Expand Up @@ -273,6 +286,10 @@ protected void newAtomData() {
zfract = new ArrayList<String>();
hCounts = new ArrayList<String>();
atomParities = new ArrayList<String>();
parityARef1 = new ArrayList<String>();
parityARef2 = new ArrayList<String>();
parityARef3 = new ArrayList<String>();
parityARef4 = new ArrayList<String>();
atomAromaticities = new ArrayList<String>();
atomDictRefs = new ArrayList<String>();
spinMultiplicities = new ArrayList<String>();
Expand Down Expand Up @@ -443,7 +460,7 @@ else if (att.equals("isotopeNumber")) {
isotope.add(value);
}
else if (att.equals("dictRef")) {
logger.debug("ocupaccy: "+value);
logger.debug("occupancy: "+value);
atomDictRefs.add(value);
}
else if (att.equals("spinMultiplicity")) {
Expand All @@ -456,6 +473,9 @@ else if (att.equals("occupancy")) {
else {
logger.warn("Unparsed attribute: " + att);
}

parityAtomsGiven = false;
parityGiven = false;
}
} else if ("atomArray".equals(name) &&
!xpath.endsWith("formula", "atomArray")) {
Expand Down Expand Up @@ -491,6 +511,26 @@ else if (att.equals("occupancy")) {
atomsCounted = true;
}
}
} else if ("atomParity".equals(name)) {
for (int i = 0; i < atts.getLength(); i++) {
String att = atts.getQName(i);
if (att.equals("atomRefs4") && !parityAtomsGiven) {
//Expect exactly four references
try {
StringTokenizer st = new StringTokenizer(
atts.getValue(i)
);
parityARef1.add((String) st.nextElement());
parityARef2.add((String) st.nextElement());
parityARef3.add((String) st.nextElement());
parityARef4.add((String) st.nextElement());
parityAtomsGiven = true;
} catch (Exception e) {
logger.error("Error in CML file: ", e.getMessage());
logger.debug(e);
}
}
}
} else if ("bond".equals(name)) {
bondCounter++;
for (int i = 0; i < atts.getLength(); i++) {
Expand Down Expand Up @@ -649,7 +689,7 @@ else if (att.equals("occupancy")) {
}
currentMolecule = currentChemFile.getBuilder().newInstance(IAtomContainer.class);
}
}else if ("formula".equals(name)){
} else if ("formula".equals(name)){
formulaCounter++;
for (int i = 0; i < atts.getLength(); i++) {
String att = atts.getQName(i);
Expand Down Expand Up @@ -705,6 +745,15 @@ public void endElement(CMLStack xpath, String uri, String name, String raw) {
formal charge when no number is given */
formalCharges.add("0");
}
if (!parityGiven) {
atomParities.add("");
}
if (!parityAtomsGiven) {
parityARef1.add("");
parityARef2.add("");
parityARef3.add("");
parityARef4.add("");
}
/* It may happen that not all atoms have
associated 2D or 3D coordinates. accept that */
if (atomCounter > x2.size() && x2.size() != 0) {
Expand Down Expand Up @@ -832,6 +881,11 @@ public void endElement(CMLStack xpath, String uri, String name, String raw) {
bondStereo.add(currentChars);
stereoGiven = Boolean.TRUE;
}
} else if ("atomParity".equals(name)){
if(!currentChars.isEmpty() && !parityGiven && parityAtomsGiven){
atomParities.add(currentChars);
parityGiven = Boolean.TRUE;
}
} else if ("float".equals(name)) {
if (BUILTIN.equals("x3")) {
x3.add(cData.trim());
Expand Down Expand Up @@ -1199,6 +1253,7 @@ protected void storeAtomData() {
boolean hasExactMasses = false;
boolean hasDictRefs = false;
boolean hasSpinMultiplicities = false;
boolean hasAtomParities = false;
boolean hasOccupancies = false;

if (elid.size() == atomCounter) {
Expand Down Expand Up @@ -1287,6 +1342,14 @@ protected void storeAtomData() {
" != " + atomCounter);
}

if (atomParities.size() == atomCounter) {
hasAtomParities = true;
} else {
logger.debug(
"No atomParity info: " + spinMultiplicities.size(),
" != " + atomCounter);
}

if (occupancies.size() == atomCounter) {
hasOccupancies = true;
} else {
Expand Down Expand Up @@ -1459,9 +1522,9 @@ protected void storeAtomData() {

if (hasSpinMultiplicities && spinMultiplicities.get(i) != null) {
// cdo.setObjectProperty("Atom", "spinMultiplicity", (String)spinMultiplicities.get(i));
int unpairedElectrons = Integer.parseInt((String)spinMultiplicities.get(i))-1;
for (int sm=0; sm<unpairedElectrons; sm++) {
currentMolecule.addSingleElectron(currentChemFile.getBuilder().newInstance(ISingleElectron.class,currentAtom));
int unpairedElectrons = Integer.parseInt((String)spinMultiplicities.get(i)) - 1;
for (int sm = 0; sm < unpairedElectrons; sm++) {
currentMolecule.addSingleElectron(currentChemFile.getBuilder().newInstance(ISingleElectron.class, currentAtom));
}
}

Expand Down Expand Up @@ -1497,6 +1560,21 @@ protected void storeAtomData() {

currentMolecule.addAtom(currentAtom);
}

for (int i = 0; i < atomCounter; i++) {
if (hasAtomParities && atomParities.get(i) != null) {
try {
int parity = (int) Math.round(Double.parseDouble(atomParities.get(i)));
//currentAtom.setStereoParity(parity);
currentMolecule.addStereoElement(new TetrahedralChirality(currentAtom, new IAtom[]{atomEnumeration.get(parityARef1.get(i)), atomEnumeration.get(parityARef2.get(i)), atomEnumeration.get(parityARef3.get(i)), atomEnumeration.get(parityARef4.get(i))}, (parity == 1 ? Stereo.CLOCKWISE : Stereo.ANTI_CLOCKWISE)));
} catch (NumberFormatException e) {
if (!e.getMessage().equals("empty String")) {
logger.warn("Cannot interpret stereo information: " + atomParities.get(i));
}
}
}
}

if (elid.size() > 0) {
// assume this is the current working list
bondElid = elid;
Expand Down

0 comments on commit b0f1f7f

Please sign in to comment.