}
if (state.cmlMol == null && state.smiles != null) {
//build from smiles
CMLMolecule cmlMolecule = SMILESTool.createMolecule(state.smiles);
CMLCml cml = new CMLCml();
cml.appendChild(cmlMolecule);
if (state.inchi !=null){
StructureConverter.enhanceCMLMolecule(cml, name, state.inchi);
}
else{
StructureConverter.enhanceCMLMolecule(cml, name);
}
state.cmlMol = cml;
state.cmlPile.appendChild(state.cmlMol);
if (state.inchi != null) {
state.InChItoCML.put(state.inchi, state.cmlMol);
}
}
if (state.cmlMol == null && state.inchi != null) {
if(!state.InChItoCML.containsKey(state.inchi)) {
//build from inchi
try {
StereoInChIToMolecule.primeCacheForInChINoThrow(state.inchi);
IMolecule mol = ConverterToInChI.getMolFromInChI(state.inchi);
StructureConverter.configureMolecule(mol);
// IMolecule mol = parser.parseSmiles(smiles);
state.cmlMol = StructureConverter.molToCml(mol, name);
state.cmlPile.appendChild(state.cmlMol);
state.InChItoCML.put(state.inchi, state.cmlMol);
/* InChI checking, do something useful with this */
/*
System.out.println();
System.out.println(inchi);
System.out.println(cmlMol.query(".//cml:identifier/text()", new XPathContext("cml", "http://www.xml-cml.org/schema")).get(0).toXML());
*/
} catch (UnsupportedEncodingException e) {
throw new Error(e);
} catch (InvalidSmilesException e) {
//The Smiles parser should return null rather than throwing. Ho hum.
} catch (Exception e) {
e.printStackTrace();
}
/* If it does exist */
}
}
}
/* Associate molecule with ID, and CML with ID if possible */
if(state.cmlMol != null) {
String id = state.cmlMol.getAttributeValue("id");
if(id == null) {
cmlId = nextCmlId;
nextCmlId++;
id = "cml" + Integer.toString(cmlId);
state.cmlMol.addAttribute(new Attribute("id", id));
} else {
cmlId = Integer.parseInt(id.substring(3));
}
setNEAttribute(state.ne, "cmlRef", id);
} else {
cmlId = 0;
}
if((state.cmlMol != null) &&
((Element)state.ne.getParent()).getLocalName().equals(XMLStrings.getInstance().HEADER)) {
/* We might be above an experimental paragraph here */
Node headerNode = state.ne.getParent();
Node nextNode = XOMTools.getNextSibling(headerNode);
while(nextNode != null && !(nextNode instanceof Element)) nextNode = XOMTools.getNextSibling(nextNode);
if(nextNode != null && nextNode.query("datasection") != null) {
/* OK, so nextNode is an experimental paragraph */
Nodes n = state.ne.getParent().query("./" + XMLStrings.getInstance().COMPOUNDREF_XPATH, XMLStrings.getInstance().getXpc());
if(n.size() == 1) {
String xRefID = ((Element)n.get(0)).getAttributeValue(XMLStrings.getInstance().COMPOUNDREF_ID_ATTRIBUTE);
if(!xRefID.contains(" ")) {
/* Good, we have a single compound */
//System.out.printf("%s %d\n", xRefID, cmlId);
//System.out.println(headerNode.getValue());
if(cmlId != 0) state.xRefIDtoCmlID.put(xRefID, "cml" + Integer.toString(cmlId));
Nodes mpNodes = nextNode.query(".//property[@type=\"mp\"]");
if(mpNodes.size() == 1) {
Node mpNode = mpNodes.get(0);
Nodes tempValNodes = mpNode.query("./quantity[@type=\"temp\"]/value");
if(tempValNodes.size() == 1) {
/* Make this first, then namespace */
Element cmlMp = new Element("property");
cmlMp.addAttribute(new Attribute("dictRef", "cml:mpt"));
Element cmlScalar = new Element("scalar");
cmlMp.appendChild(cmlScalar);
/* FIXME - blatant hack */
cmlScalar.addAttribute(new Attribute("units", "units:celsius"));
Element tempValElem = (Element)tempValNodes.get(0);
Element min = tempValElem.getFirstChildElement("min");
Element point = tempValElem.getFirstChildElement("point");
if(min == null) {
cmlScalar.appendChild(point.getValue());
} else {
String minStr = min.getValue();
String maxStr = point.getValue();
while(!Character.isDigit(maxStr.charAt(0))) maxStr = maxStr.substring(1);
if(maxStr.length() == (minStr.length() - 1)) {
maxStr = minStr.substring(0,1) + maxStr;
}
cmlScalar.addAttribute(new Attribute("minValue", minStr));
cmlScalar.addAttribute(new Attribute("maxValue", maxStr));
}
XOMTools.setNamespaceURIRecursively(cmlMp, "http://www.xml-cml.org/schema");
state.cmlMol.getFirstChildElement("molecule", "http://www.xml-cml.org/schema").appendChild(cmlMp);
}
}
}
}
}
}
String ontIds = "";
if(state.inchi != null) {
Set<String> ontologyIds = ChemNameDictSingleton.getOntologyIdsFromInChI(state.inchi);
if(ontologyIds != null && ontologyIds.size() > 0) {
ontIds += StringTools.collectionToString(ontologyIds, " ") + " ";
}
}
String normName = StringTools.normaliseName(rawName);
if(OntologyTerms.hasTerm(normName)) {
ontIds += OntologyTerms.idsForTerm(normName) + " ";
}
if(ontIds.length() > 0) {
String oldOntIds = getNEAttribute(state.ne, "ontIDs");
if(oldOntIds == null || oldOntIds.length() == 0) {
setNEAttribute(state.ne, "ontIDs", ontIds.substring(0, ontIds.length()-1));
} else {
setNEAttribute(state.ne, "ontIDs", StringTools.mergeSpaceSeparatedSets(
oldOntIds, ontIds.substring(0, ontIds.length()-1)));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//System.out.println(xRefIDtoCmlID);
// TODO - make more general for SAF
if(makeCML) {
Nodes xRefNodes = doc.query(".//" + XMLStrings.getInstance().COMPOUNDREF_XPATH);
for(int i=0;i<xRefNodes.size();i++) {
Element xRefElem = (Element)xRefNodes.get(i);
String id = xRefElem.getAttributeValue(XMLStrings.getInstance().COMPOUNDREF_ID_ATTRIBUTE);
if(state.xRefIDtoCmlID.containsKey(id)) {
xRefElem.addAttribute(new Attribute("cmlRef", state.xRefIDtoCmlID.get(id)));
}
}
if (Oscar3Props.getInstance().urlEncodeCML) {
Elements cmlElems = state.cmlPile.getChildElements("cml", "http://www.xml-cml.org/schema");
for(int i=0;i<cmlElems.size();i++) {
Element cml = cmlElems.get(i);
String id = cml.getAttributeValue("id");
Element cmlUrlEnc = new Element("cmlUrlEnc");
cmlUrlEnc.addAttribute(new Attribute("idRef", id));
cmlUrlEnc.appendChild(StringTools.urlEncodeLongString(cml.toXML()));
state.cmlPile.appendChild(cmlUrlEnc);
}
}
}