Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for SmartsMatching on MolTable.
* Added checkbox for SmartsMatchingView
* Rewrote models to support SmartsFiles
* Added import SMARTS from file
* Running SmartsMatching with MolTable open highlights hits in chemical structures.

Smartsmatching in MolTable is very slow, mainly because results are not persisted among redraws (see bug 1676).

Solves bug 1675.
  • Loading branch information
olas committed Oct 23, 2009
1 parent b0a8e73 commit 3ce78ba
Show file tree
Hide file tree
Showing 14 changed files with 946 additions and 133 deletions.
8 changes: 5 additions & 3 deletions plugins/net.bioclipse.cdk.smartsmatching/META-INF/MANIFEST.MF
Expand Up @@ -19,7 +19,9 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.resources,
org.eclipse.ui.views,
net.bioclipse.chemoinformatics;bundle-version="2.0.0",
org.eclipse.help
Bundle-RequiredExecutionEnvironment: J2SE-1.5
org.eclipse.help,
org.openscience.cdk.renderbasic;bundle-version="1.3.0",
net.bioclipse.cdk.ui.sdfeditor;bundle-version="2.1.1"
Bundle-ActivationPolicy: lazy
Import-Package: org.apache.log4j;version="1.2.15"
Import-Package: javax.vecmath,
org.apache.log4j;version="1.2.15"
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions plugins/net.bioclipse.cdk.smartsmatching/plugin.xml
Expand Up @@ -37,4 +37,11 @@
</toc>
</extension>

<extension
point="net.bioclipse.cdk.jchempaint.generator">
<generator
class="net.bioclipse.cdk.smartsmatching.SmartsMatchGenerator">
</generator>
</extension>

</plugin>
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2009 Ola Spjuth.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ola Spjuth - initial API and implementation
******************************************************************************/
package net.bioclipse.cdk.smartsmatching;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.renderer.elements.ElementGroup;
import org.openscience.cdk.renderer.elements.IRenderingElement;
import org.openscience.cdk.renderer.elements.OvalElement;
import org.openscience.cdk.renderer.generators.IGenerator;
import org.openscience.cdk.renderer.generators.IGeneratorParameter;


public class SmartsMatchGenerator implements IGenerator {

public SmartsMatchGenerator() {

}

/**
* Set up the colored circles based on calculated properties = matches
*/
public IRenderingElement generate( IAtomContainer ac,
RendererModel model ) {

// System.out.println("Generator found AC: " + ac);

ElementGroup group = new ElementGroup();
Object o = ac.getProperty( SmartsMatchingConstants.SMARTS_MATCH_PROPERTY );
if (o==null) return group;

//Need to parse the property into something which we can highlight
List<Integer> highlightList=SmartsMatchingHelper.parseProperty(o);

for(int i = 0;i<ac.getAtomCount();i++) { //Loop over all atoms
for (Integer ii : highlightList){ //Loop over list of atom indices with M2D results
if (ii.intValue()==i){
IAtom atom = ac.getAtom( i );

Color drawColor=SmartsMatchingConstants.SMARTS_HIGHLIGHT_COLOR;
double radius=SmartsMatchingConstants.SMARTS_HIGHLIGHT_RADIUS;

group.add( new OvalElement( atom.getPoint2d().x,
atom.getPoint2d().y,
radius,true, drawColor ));
}
}
}

return group;
}

public List<IGeneratorParameter> getParameters() {
return Collections.emptyList();
}
}
@@ -0,0 +1,16 @@
package net.bioclipse.cdk.smartsmatching;

import java.awt.Color;

/**
*
* @author ola
*
*/
public class SmartsMatchingConstants {

public static final Object SMARTS_MATCH_PROPERTY = "SMARTS_PROPERTY";
public static final Color SMARTS_HIGHLIGHT_COLOR = Color.YELLOW;
public static final double SMARTS_HIGHLIGHT_RADIUS = 0.4;

}
@@ -0,0 +1,74 @@
package net.bioclipse.cdk.smartsmatching;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import net.bioclipse.cdk.business.CDKManager;
import net.bioclipse.cdk.domain.ICDKMolecule;

import org.apache.log4j.Logger;
import org.openscience.cdk.interfaces.IAtom;


public class SmartsMatchingHelper {

private static final Logger logger = Logger.getLogger(CDKManager.class);

/**
* Deliver a list of integers to highlight
* @param o
* @return
*/
public static List<Integer> parseProperty( Object o ) {

if (!( o instanceof String ))
return Collections.emptyList();

String prop=(String)o;

List<Integer> ret=new ArrayList<Integer>();

logger.debug( "Parsing property: " + prop );

//Parse 1,2,3,8 into list of Integers
//split up entire string by ,
StringTokenizer tk=new StringTokenizer(prop, ",");
while ( tk.hasMoreElements() ) {
String token = tk.nextToken();
Integer atomno=Integer.parseInt( token );
ret.add( atomno );
}

return ret;
}

/**
* Save the atoms to property
* @param cdkmol
* @param hitAtoms
*/
public static void serializeToProperty( ICDKMolecule cdkmol,
Set<IAtom> hitAtoms ) {

String prop="";
for (IAtom atom : hitAtoms){
int atomno=cdkmol.getAtomContainer().getAtomNumber( atom );
if (atomno>0)
prop=prop+atomno+",";
}

if (prop.length()<=0) return;

prop=prop.substring( 0,prop.length()-1 );

cdkmol.getAtomContainer().getProperties().put(
SmartsMatchingConstants.SMARTS_MATCH_PROPERTY, prop );

logger.debug("Serialized ac prop: " + prop);

}

}
@@ -0,0 +1,126 @@
/*******************************************************************************
* Copyright (c) 2009 Ola Spjuth.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ola Spjuth - initial API and implementation
******************************************************************************/
package net.bioclipse.cdk.smartsmatching;

import java.awt.Color;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;

import net.bioclipse.cdk.business.Activator;
import net.bioclipse.cdk.business.CDKManager;
import net.bioclipse.cdk.business.ICDKManager;
import net.bioclipse.cdk.domain.CDKMolecule;
import net.bioclipse.cdk.domain.ICDKMolecule;
import net.bioclipse.cdk.smartsmatching.views.SmartsMatchingView;
import net.bioclipse.cdk.ui.sdfeditor.editor.IRenderer2DConfigurator;
import net.bioclipse.core.business.BioclipseException;

public class SmartsMatchingRendererConfigurator
implements IRenderer2DConfigurator{

private static final Logger logger = Logger.getLogger(
SmartsMatchingRendererConfigurator.class);

//The bondcolor in M2d results
Color bondcolor=new Color(33,33,33);

/**
* Add tooltip read from M2D property
*/
public void configure(final RendererModel model, final IAtomContainer ac) {

// Job job=new Job("") {
//
// @Override
// protected IStatus run( IProgressMonitor monitor ) {

ICDKManager cdk = Activator.getDefault().getJavaCDKManager();

//Get the SMARTS from the SmartsView
List<String> allSmarts=SmartsMatchingView.getActiveSmarts();
if (allSmarts==null || allSmarts.size()<=0)
return;
// return Status.OK_STATUS;

//Read from prefs for AC to see if we need to calculate again
String smartsProp=(String) ac.getProperty(
SmartsMatchingConstants.SMARTS_MATCH_PROPERTY );

//The below does not work since properties are lost.
//Filed as Bug 1676
if (smartsProp!=null){
//Check if we have calculated, if so, do not repeat it
return;
}

//Calculate smarts matches and store as property
ICDKMolecule cdkmol=new CDKMolecule(ac);

//Store atoms for highlighting in a set, no duplicates
Set<IAtom> hitAtoms=new HashSet<IAtom>();

for (String activeSmarts : allSmarts ){
List<IAtomContainer> hitACs;
try {
hitACs = cdk.getSmartsMatches( cdkmol, activeSmarts );
if (hitACs!=null){

for (IAtomContainer hitAC : hitACs){
for (IAtom atom : hitAC.atoms()){
hitAtoms.add( atom );
}
}
}
} catch ( BioclipseException e ) {
logger.error( "SMARST matching failed for smarts: "
+ activeSmarts + " due to: " + e.getMessage());
}
}

if (hitAtoms.size()>0)
//Serialize to property
SmartsMatchingHelper.serializeToProperty(cdkmol, hitAtoms);

model.setIsCompact( true );
model.setShowAtomTypeNames( false );
model.setShowImplicitHydrogens( false );
model.setShowExplicitHydrogens( false );
model.setAtomRadius( 0 );


//Update drawing
//TODO: is this really needed here?
model.fireChange();

// TODO Auto-generated method stub
return;
// return Status.OK_STATUS;
// }
// };
//
// job.setUser( false );
// job.schedule();


}

}
@@ -0,0 +1,39 @@
package net.bioclipse.cdk.smartsmatching.model;

import java.util.ArrayList;
import java.util.List;

import net.bioclipse.core.domain.BioObject;


public class SmartsFile extends BioObject{

List<SmartsWrapper> smarts;
String name;

public List<SmartsWrapper> getSmarts() {

return smarts;
}

public void setSmarts( List<SmartsWrapper> smarts ) {

this.smarts = smarts;
}

public String getName() {

return name;
}

public void setName( String name ) {

this.name = name;
}

public void addSmartsWrapper( SmartsWrapper sw ) {
if (smarts==null) smarts= new ArrayList<SmartsWrapper>();
smarts.add( sw );
}

}

0 comments on commit 3ce78ba

Please sign in to comment.