1
/* $Revision: 7636 $ $Author: egonw $ $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
3
* Copyright (C) 2002-2007 Egon Willighagen <egonw@users.sf.net>
5
* Contact: cdk-devel@lists.sourceforge.net
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public License
9
* as published by the Free Software Foundation; either version 2.1
10
* of the License, or (at your option) any later version.
11
* All we ask is that proper credit is given for our work, which includes
12
* - but is not limited to - adding the above copyright notice to the beginning
13
* of your source code files, and to any copyright notice that you may distribute
14
* with programs based on this work.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26
package org.openscience.cdk.io.cml;
28
import java.util.StringTokenizer;
30
import org.openscience.cdk.interfaces.IChemFile;
31
import org.openscience.cdk.qsar.DescriptorSpecification;
32
import org.openscience.cdk.qsar.DescriptorValue;
33
import org.openscience.cdk.qsar.result.BooleanResult;
34
import org.openscience.cdk.qsar.result.DoubleArrayResult;
35
import org.openscience.cdk.qsar.result.DoubleResult;
36
import org.openscience.cdk.qsar.result.IDescriptorResult;
37
import org.openscience.cdk.qsar.result.IntegerArrayResult;
38
import org.openscience.cdk.qsar.result.IntegerResult;
39
import org.xml.sax.Attributes;
42
* This is an implementation for the CDK convention.
48
public class QSARConvention extends CMLCoreModule {
50
private String currentDescriptorAlgorithmSpecification;
51
private String currentDescriptorImplementationTitel;
52
private String currentDescriptorImplementationVendor;
53
private String currentDescriptorImplementationIdentifier;
54
private String currentDescriptorDataType;
55
private String currentDescriptorResult;
56
private boolean currentDescriptorDataIsArray;
58
public QSARConvention(IChemFile chemFile) {
62
public QSARConvention(ICMLModule conv) {
66
public void startElement(CMLStack xpath, String uri, String local, String raw, Attributes atts) {
67
// <property xmlns:qsar="http://www.blueobelisk.org/ontologies/chemoinformatics-algorithms/"
68
// convention="qsar:DescriptorValue">
70
// <metadata dictRef="qsar:specificationReference" content="qsar:weight"/>
71
// <metadata dictRef="qsar:implementationTitle" content="org.openscience.cdk.qsar.descriptors.atomic.WeightDescriptor"/>
72
// <metadata dictRef="qsar:implementationIdentifier" content="$Id: cml23TestFramework.xml 6845 2006-09-07 21:21:26Z egonw $"/>
73
// <metadata dictRef="qsar:implementationVendor" content="The Chemistry Development Kit"/>
74
// <metadataList title="qsar:descriptorParameters">
75
// <metadata title="elementSymbol" content="*"/>
78
// <scalar dataType="xsd:double" dictRef="qsar:weight">72.0</scalar>
81
if (xpath.endsWith("molecule", "propertyList", "property")) {
82
// cdo.startObject("MolecularDescriptor");
83
currentDescriptorDataIsArray = false;
84
currentDescriptorAlgorithmSpecification = "";
85
currentDescriptorImplementationTitel = "";
86
currentDescriptorImplementationVendor = "";
87
currentDescriptorImplementationIdentifier = "";
88
currentDescriptorDataType = "";
89
currentDescriptorResult = "";
90
} else if (xpath.endsWith("property", "metadataList", "metadata")) {
91
super.startElement(xpath, uri, local, raw, atts);
92
if (DICTREF.equals("qsar:specificationReference")) {
93
// cdo.setObjectProperty("MolecularDescriptor", "SpecificationReference", atts.getValue("content"));
94
currentDescriptorAlgorithmSpecification = atts.getValue("content");
95
} else if (DICTREF.equals("qsar:implementationTitle")) {
96
// cdo.setObjectProperty("MolecularDescriptor", "ImplementationTitle", atts.getValue("content"));
97
currentDescriptorImplementationTitel = atts.getValue("content");
98
} else if (DICTREF.equals("qsar:implementationIdentifier")) {
99
// cdo.setObjectProperty("MolecularDescriptor", "ImplementationIdentifier", atts.getValue("content"));
100
currentDescriptorImplementationIdentifier = atts.getValue("content");
101
} else if (DICTREF.equals("qsar:implementationVendor")) {
102
// cdo.setObjectProperty("MolecularDescriptor", "ImplementationVendor", atts.getValue("content"));
103
currentDescriptorImplementationVendor = atts.getValue("content");
105
} else if (xpath.endsWith("propertyList", "property", "scalar")) {
106
// cdo.setObjectProperty("MolecularDescriptor", "DataType", atts.getValue("dataType"));
107
currentDescriptorDataType = atts.getValue("dataType");
108
super.startElement(xpath, uri, local, raw, atts);
110
super.startElement(xpath, uri, local, raw, atts);
114
public void endElement (CMLStack xpath, String uri, String local, String raw) {
115
if (xpath.endsWith("molecule", "propertyList", "property")) {
116
// cdo.endObject("MolecularDescriptor");
117
DescriptorSpecification descriptorSpecification = new DescriptorSpecification(
118
currentDescriptorAlgorithmSpecification,
119
currentDescriptorImplementationTitel,
120
currentDescriptorImplementationIdentifier,
121
currentDescriptorImplementationVendor
123
currentMolecule.setProperty(descriptorSpecification,
125
descriptorSpecification,
126
new String[0], new Object[0],
127
currentDescriptorDataIsArray ?
128
newDescriptorResultArray(currentDescriptorResult) :
129
newDescriptorResult(currentDescriptorResult),
133
} else if (xpath.endsWith("property", "scalar")) {
134
System.out.println("touch1");
135
// cdo.setObjectProperty("MolecularDescriptor", "DescriptorValue", currentChars);
136
currentDescriptorResult = currentChars;
138
super.endElement(xpath, uri, local, raw);
142
private IDescriptorResult newDescriptorResult(String descriptorValue) {
143
IDescriptorResult result = null;
144
if ("xsd:double".equals(currentDescriptorDataType)) {
145
result = new DoubleResult(Double.parseDouble(descriptorValue));
146
} else if ("xsd:integer".equals(currentDescriptorDataType)) {
147
result = new IntegerResult(Integer.parseInt(descriptorValue));
148
} else if ("xsd:boolean".equals(currentDescriptorDataType)) {
149
result = new BooleanResult(new Boolean(descriptorValue).booleanValue());
154
private IDescriptorResult newDescriptorResultArray(String descriptorValue) {
155
IDescriptorResult result = null;
156
if ("xsd:double".equals(currentDescriptorDataType)) {
157
result = new DoubleArrayResult();
158
StringTokenizer tokenizer = new StringTokenizer(descriptorValue);
159
while (tokenizer.hasMoreElements()) {
160
((DoubleArrayResult)result).add(Double.parseDouble(tokenizer.nextToken()));
162
} else if ("xsd:integer".equals(currentDescriptorDataType)) {
163
result = new IntegerArrayResult();
164
StringTokenizer tokenizer = new StringTokenizer(descriptorValue);
165
while (tokenizer.hasMoreElements()) {
166
((IntegerArrayResult)result).add(Integer.parseInt(tokenizer.nextToken()));