Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0d7b58aeb1
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 233 lines (194 sloc) 8.623 kb
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
package net.guha.apps.gui;

import chemlib.ebi.core.tools.EBIAtomContainerManipulator;
import chemlib.ebi.core.tools.EBIMCSCalculator;
import net.guha.util.cdk.Misc;
import net.guha.util.cdk.Renderer2DPanel;
import net.guha.util.cdk.StatusBar;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector;
import org.openscience.cdk.exception.InvalidSmilesException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.smiles.SmilesParser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;


public class SMSDTest extends JFrame {
    JTextField molfield1;
    JTextField molfield2;
    JPanel structurePane;
    JRadioButton rbCommon;
    JRadioButton rbDifference;
    StatusBar statusBar;

    boolean doCommon = true;

    public SMSDTest() {
        setTitle("SMSDTest");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JPanel content = new JPanel(new BorderLayout());
        setSize(600, 300);

        molfield1 = new JTextField(50);
        molfield2 = new JTextField(50);
        JPanel textPanel = new JPanel(new BorderLayout());
        textPanel.add(molfield1, BorderLayout.NORTH);
        textPanel.add(molfield2, BorderLayout.SOUTH);

        setKeyControls(molfield1);
        setKeyControls(molfield2);

        JPanel optionPanel = new JPanel();
        rbCommon = new JRadioButton("Common SS");
        rbDifference = new JRadioButton("Difference SS");
        rbCommon.setSelected(true);
        ButtonGroup group = new ButtonGroup();
        group.add(rbCommon);
        group.add(rbDifference);
        optionPanel.add(rbCommon);
        optionPanel.add(rbDifference);

        rbCommon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doCommon = true;
            }
        });

        rbDifference.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doCommon = false;
            }
        });

        JPanel inputPanel = new JPanel(new BorderLayout());
        inputPanel.add(optionPanel, BorderLayout.NORTH);
        inputPanel.add(textPanel, BorderLayout.SOUTH);

        content.add(inputPanel, BorderLayout.NORTH);

        structurePane = new JPanel(new GridBagLayout(), true);
        JScrollPane scrollpane = new JScrollPane(structurePane,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        content.add(scrollpane, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel(new BorderLayout());

        JButton go = new JButton("Go");
        go.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    doMCSS();
                } catch (InvalidSmilesException e1) {
                    JOptionPane.showMessageDialog(molfield1, "Error parsing SMILES");
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(molfield1, "Some error\n" + e1.toString());
                }
            }
        });
        bottomPanel.add(go, BorderLayout.NORTH);
        statusBar = new StatusBar();
        bottomPanel.add(statusBar, BorderLayout.SOUTH);

        content.add(bottomPanel, BorderLayout.SOUTH);

        setContentPane(content);
    }

    private void setKeyControls(final JTextField textField) {
        InputMap inputMap = textField.getInputMap();
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
        ActionMap actionMap = textField.getActionMap();
        actionMap.put("enter", new AbstractAction() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    doMCSS();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(molfield1, "Some error\n" + e.toString());
                }
            }
        });
        actionMap.put("escape", new AbstractAction() {
            public void actionPerformed(ActionEvent ae) {
                textField.setText("");
            }
        });
    }

    private void doMCSS() throws Exception {
        SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
        IMolecule A1 = sp.parseSmiles(molfield1.getText().trim());
        IMolecule A2 = sp.parseSmiles(molfield2.getText().trim());

        EBIAtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(A1);
        EBIAtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(A2);

        A1 = (IMolecule) EBIAtomContainerManipulator.removeHydrogensAndPreserveAtomID(A1);
        A2 = (IMolecule) EBIAtomContainerManipulator.removeHydrogensAndPreserveAtomID(A2);

        // bond sensitive
        long start = System.currentTimeMillis();
        EBIMCSCalculator comparison = new EBIMCSCalculator(true, false);
        comparison.init(A1, A2);

        List<Integer> solution = comparison.getFirstMapping();
        long end = System.currentTimeMillis();

        statusBar.setMessage((solution.size()/2)+" atoms matched in "+((end-start)/1000.0)+"s");
        // render the match
        IAtomContainer mol1 = Misc.get2DCoords(A1);
        IAtomContainer mol2 = Misc.get2DCoords(A2);

        CDKHueckelAromaticityDetector.detectAromaticity(mol1);
        CDKHueckelAromaticityDetector.detectAromaticity(mol2);

        // create the needle for the first mol. First add the
        // mapped atoms. The add the bonds

        IAtomContainer needle1 = A1.getBuilder().newAtomContainer();
        IAtomContainer needle2 = A1.getBuilder().newAtomContainer();

        if (solution == null) {
            needle1 = null;
            needle2 = null;
        } else if (solution != null && doCommon) {
            for (int i = 0; i < solution.size(); i += 2) {
                needle1.addAtom(mol1.getAtom(solution.get(i)));
            }
            for (IBond bond : mol1.bonds()) {
                if (needle1.contains(bond.getAtom(0)) &&
                        needle1.contains(bond.getAtom(1))) needle1.addBond(bond);
            }
            for (int i = 1; i < solution.size(); i += 2) {
                needle2.addAtom(mol2.getAtom(solution.get(i)));
            }
            for (IBond bond : mol2.bonds()) {
                if (needle2.contains(bond.getAtom(0)) &&
                        needle2.contains(bond.getAtom(1))) needle2.addBond(bond);
            }
        } else if (solution != null && !doCommon) {
            List<Integer> tmp = new ArrayList<Integer>();
            for (int i = 0; i < solution.size(); i += 2) tmp.add(solution.get(i));
            for (int i = 0; i < mol1.getAtomCount(); i++) {
                if (!tmp.contains(i))
                    needle1.addAtom(mol1.getAtom(i));
            }
            for (IBond bond : mol1.bonds()) {
                if (needle1.contains(bond.getAtom(0)) &&
                        needle1.contains(bond.getAtom(1))) needle1.addBond(bond);
            }

            tmp.clear();
            for (int i = 1; i < solution.size(); i += 2) tmp.add(solution.get(i));
            for (int i = 0; i < mol2.getAtomCount(); i++) {
                if (!tmp.contains(i))
                    needle2.addAtom(mol2.getAtom(i));
            }
            for (IBond bond : mol2.bonds()) {
                if (needle2.contains(bond.getAtom(0)) &&
                        needle2.contains(bond.getAtom(1))) needle2.addBond(bond);
            }
        }

        structurePane.removeAll();
        Renderer2DPanel panel1 = new Renderer2DPanel(mol1, needle1, 300, 300, true, Color.white);
        Renderer2DPanel panel2 = new Renderer2DPanel(mol2, needle2, 300, 300, true, Color.white);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;

        gbc.ipadx = 10;
        gbc.ipady = 10;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;

        gbc.gridx = 0;
        gbc.gridy = 0;
        structurePane.add(panel1, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        structurePane.add(panel2, gbc);


        validate();
        pack();
    }

    public static void main(String[] args) {
        SMSDTest s = new SMSDTest();
        s.setVisible(true);
    }
}
Something went wrong with that request. Please try again.