/
InsertTextPanel.java
257 lines (223 loc) · 9.41 KB
/
InsertTextPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* $RCSfile$
* $Author: egonw $
* $Date: 2007-01-04 17:26:00 +0000 (Thu, 04 Jan 2007) $
* $Revision: 7634 $
*
* Copyright (C) 1997-2008 Rajarshi Guha, Stefan Kuhn
*
* Contact: cdk-jchempaint@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.jchempaint;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.exception.InvalidSmilesException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.jchempaint.application.JChemPaint;
import org.openscience.jchempaint.inchi.InChITool;
/**
* A panel containing a text field and button to directly insert SMILES or InChI's
*
*/
public class InsertTextPanel extends JPanel implements ActionListener {
private static final long serialVersionUID = -5329451371043240706L;
private AbstractJChemPaintPanel jChemPaintPanel;
private JComboBox<String> textCombo;
private JTextComponent editor;
private JFrame closeafter = null;
private JButton button = new JButton(GT.get("Insert"));
public InsertTextPanel(AbstractJChemPaintPanel jChemPaintPanel, JFrame closeafter) {
super();
this.closeafter = closeafter;
setLayout(new GridBagLayout());
List<String> oldText = new ArrayList<String>();
oldText.add("");
textCombo = new JComboBox<String>(oldText.toArray(new String[]{}));
textCombo.setEditable(true);
textCombo.setToolTipText(GT.get("Enter a CAS, SMILES or InChI string"));
textCombo.addActionListener(this);
editor = (JTextComponent) textCombo.getEditor().getEditorComponent();
button.addActionListener(this);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
gridBagConstraints.weightx = 0.5;
gridBagConstraints.ipadx = 5;
gridBagConstraints.insets = new Insets(0, 0, 0, 5);
add(textCombo, gridBagConstraints);
gridBagConstraints.insets = new Insets(0, 0, 2, 0);
gridBagConstraints.ipadx = 5;
gridBagConstraints.ipady = 2;
gridBagConstraints.weightx = 0.0;
gridBagConstraints.gridx = 1;
add(button, gridBagConstraints);
this.jChemPaintPanel = jChemPaintPanel;
}
public void actionPerformed(ActionEvent actionEvent) {
String actionCommand = actionEvent.getActionCommand();
if (actionCommand.equals("comboBoxEdited")
|| actionCommand.equals(GT.get("Insert"))) {
IAtomContainer molecule = getMolecule();
if (molecule == null)
return;
try {
JChemPaint.generateModel(jChemPaintPanel, molecule, true, false);
} catch (CDKException e) {
e.printStackTrace();
return;
}
if (closeafter != null)
closeafter.setVisible(false);
}
}
private IAtomContainer getMolecule() {
IAtomContainer molecule;
String text = (String) textCombo.getSelectedItem();
text = text.trim(); // clean up extra white space
if (text.equals("")) return null;
if (text.startsWith("InChI")) { // handle it as an InChI
try {
IAtomContainer atomContainer = InChITool.parseInChI(text);
molecule = atomContainer.getBuilder().newInstance(IAtomContainer.class, atomContainer);
} catch (Exception e2) {
JOptionPane.showMessageDialog(jChemPaintPanel, GT.get("Could not load InChI subsystem"));
return null;
}
} else if (isCASNumber(text)) { // is it a CAS number?
try {
molecule = getMoleculeFromCAS(text);
} catch (IOException e) {
JOptionPane.showMessageDialog(jChemPaintPanel, GT.get("Error in reading data from PubChem"));
return null;
}
} else { // OK, it must be a SMILES
SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
try {
molecule = smilesParser.parseSmiles(text);
} catch (InvalidSmilesException e1) {
JOptionPane.showMessageDialog(jChemPaintPanel, GT.get("Invalid SMILES specified"));
return null;
}
}
// OK, we have a valid molecule, save it and show it
String tmp = (String) textCombo.getItemAt(0);
if (tmp.equals("")) textCombo.removeItemAt(0);
textCombo.addItem(text);
editor.setText("");
return molecule;
}
private boolean isCASNumber(String text) {
String[] chars = text.split("-");
if (chars.length != 3) return false;
for (int i = 0; i < 3; i++) {
if (i == 2 && chars[i].length() != 1) return false;
if (i == 1 && chars[i].length() != 2) return false;
if (i == 0 && chars[i].length() > 6) return false;
try {
Integer.parseInt(chars[i]);
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
private IAtomContainer getMoleculeFromCAS(String cas) throws IOException {
String firstURL = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pccompound&term=" + cas;
String data = getDataFromURL(firstURL);
Pattern pattern = Pattern.compile("http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi\\?cid=(\\d*)");
Matcher matcher = pattern.matcher(data);
String cid = null;
boolean found = false;
while (matcher.find()) {
cid = matcher.group(1);
try { // should be an integer
Integer.parseInt(cid);
found = true;
break;
} catch (NumberFormatException e) {
continue;
}
}
if (!found) return null;
String secondURL = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?tool=jcppubchem&db=pccompound&id=" + cid;
data = getDataFromURL(secondURL);
pattern = Pattern.compile("<Item Name=\"CanonicalSmile\" Type=\"String\">([^\\s]*?)</Item>");
matcher = pattern.matcher(data);
String smiles = "";
found = false;
while (matcher.find()) {
smiles = matcher.group(1);
if (!smiles.equals("")) {
found = true;
break;
}
}
if (!found) return null;
// got the canonical SMILES, lets get the molecule
SmilesParser smilesParser = new SmilesParser(DefaultChemObjectBuilder.getInstance());
try {
IAtomContainer mol=smilesParser.parseSmiles(smiles);
//for some reason, smilesparser sets valencies, which we don't want in jcp
for(int i=0;i<mol.getAtomCount();i++){
mol.getAtom(i).setValency(null);
}
return mol;
} catch (InvalidSmilesException e1) {
JOptionPane.showMessageDialog(jChemPaintPanel, "Couldn't process data from PubChem");
return null;
}
}
private String getDataFromURL(String url) throws IOException {
URL theURL = new URL(url);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(theURL.openStream()));
String data = "";
String line;
while ((line = bufferedReader.readLine()) != null) data += line;
bufferedReader.close();
return data;
}
public void updateLanguage() {
textCombo.setToolTipText(GT.get("Enter a CAS, SMILES or InChI string"));
button.setText(GT.get("Insert"));
}
}