Skip to content

Instantly share code, notes, and snippets.

@hariaimar
Created January 14, 2011 09:14
Show Gist options
  • Save hariaimar/779394 to your computer and use it in GitHub Desktop.
Save hariaimar/779394 to your computer and use it in GitHub Desktop.
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.qsar.DescriptorEngine;
import org.openscience.cdk.qsar.DescriptorSpecification;
import org.openscience.cdk.qsar.DescriptorValue;
import org.openscience.cdk.qsar.descriptors.molecular.BCUTDescriptor;
import org.openscience.cdk.smiles.SmilesParser;
/**
*
* @author Hari
*acknowledgement:cdk team,Ramachandran.T.C.
*/
public class Descriptor extends javax.swing.JFrame {
/** Creates new form Descriptor */
public Descriptor(String smile) {
initComponents();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim = new Dimension(screenSize.width-8, screenSize.height-40);
this.setMinimumSize(dim);
this.validate();
tmodel = (DefaultTableModel) jTable1.getModel();
// jTextField1.setText(smile);
}
/** This method is called from within the constructor to
* initialize the form.
*
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Descriptor");
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Descriptor Specification", "Descriptor Value"
}
) {
boolean[] canEdit = new boolean [] {
false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.getTableHeader().setReorderingAllowed(false);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getColumn(0).setMinWidth(200);
jTable1.getColumnModel().getColumn(0).setMaxWidth(200);
jLabel1.setText("Smile");
jTextField1.setText("NC(CO)C(=O)O");
jButton1.setText("Generate");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(28, 28, 28)
.addComponent(jButton1)
.addContainerGap(93, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(1, 1, 1)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
setCursor(new Cursor(Cursor.WAIT_CURSOR));
BCUTDescriptor descriptor = new BCUTDescriptor();
// Molecule molecule = (Molecule) new SmilesParser(DefaultChemObjectBuilder.getInstance()).parseSmiles("NC(CO)C(=O)O");
Molecule molecule = (Molecule) new SmilesParser(DefaultChemObjectBuilder.getInstance()).parseSmiles(jTextField1.getText());
// Second program
DescriptorEngine engine = new DescriptorEngine(DescriptorEngine.MOLECULAR,new String[]{"lib/cdk-1.3.7.jar"});
engine.process(molecule);
/**
* The function getProperties returns a Map value. Map represents a Key Value set.
* It is retrieved using an iterator. Since the key is of DescriptorSpecification class and the
* value is of type DescriptorValue we have to typecast the key and value with the same while retrieving.
* since they have not override the same to String.
*/
Map i = molecule.getProperties();
Iterator iterator = i.keySet().iterator();
Object[] o = i.keySet().toArray();
System.out.print("" + o.length + "\n\n");
/**
* The following prints the key and Value set.
*/
while (iterator.hasNext()) {
Object l = iterator.next();
String key = ((DescriptorSpecification) l).getSpecificationReference().toString();
tmodel.addRow(new Object[]{key.substring(key.lastIndexOf("#") + 1), ((DescriptorValue) i.get((DescriptorSpecification)l)).getValue().toString()});
}
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
} catch (CDKException ex) {
Logger.getLogger(Descriptor.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Descriptor(null).setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
DefaultTableModel tmodel;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment