Calculate Tanimoto similarity for last used query and selected row

Calculate Similarity score
/*
* Find Similarity Value
*
*
* Usage:
* 1. Run your Similarity Query
* 2. Run button script
*
* The button script expects that SIMILARITY search
* and Tanimoto metric is used. If you want to use different
* metric, please see all available
* https://docs.chemaxon.com/display/docs/Functions+by+Categories#dissimilarity_functions
*
* @author David Pech <[email protected]>
*/
import com.im.df.api.*
import com.im.df.api.chem.DissimilarityCalculator
import chemaxon.struc.Molecule
import chemaxon.jep.*
import chemaxon.jep.context.MolContext
import chemaxon.formats.MolImporter;
 
import chemaxon.sss.search.JChemSearchOptions;
import chemaxon.standardizer.Standardizer;
import com.im.df.api.capabilities.JChemEntityCapability;
init = { widget ->
}
destroy = { widget ->
}
evaluate = { widget ->
def ety = dataTree.rootVertex.entity // assumes you have reference to the data tree
def edp = ety.schema.dataProvider.getEntityDataProvider(ety)
def molFld = ety.fields.items.find { it.name == 'Structure' } // find the structure field
def rs = ety.schema.dataProvider.getDefaultResultSet(dataTree, false, DFEnvironmentRO.DEV_NULL) // find the ResultSet
def rootVS = rs.getVertexState(dataTree.rootVertex) // obtain the VertexState
 
// define the entity Capability so that we can sniff out the query paramenters
JChemEntityCapability entityCap = DIFUtilities.findCapability(ety, JChemEntityCapability.class);
 
// obtain the query paramenters from last used query
boolean isReaction = false
int bitCount = entityCap.getNumberOfOnes();
int bondCount = entityCap.getNumberOfEdges();
int fpLengthInBits = entityCap.getNumberOfInts() * 32;
 
MarvinStructure queryStructure; // define empty query structure (will be defined in the cycle below)
 
// obtain the query structure from last used query parameters
List<DFTermExpression> expressions = DIFUtilities.findSimpleFieldUsagesInQuery(rs.getLastExecutedQuery(), molFld);
for (DFTermExpression dFTermExpression : expressions) {
DFOperator operator = dFTermExpression.getOperator();
if (operator instanceof Operators.StructureOperator) {
boolean caseInsensitive = (Boolean) dFTermExpression.getOptions()
.get(LegacyConstants.CASE_INSENSITIVE_SEARCH);
List<DFTerm> operands = dFTermExpression.getOperands();
for (DFTerm dFTerm : operands) {
if (dFTerm instanceof DFTermValue && ((DFTermValue) dFTerm).getValue() instanceof MarvinStructure) {
queryStructure = ((DFTermValue) dFTerm).getValue(); // assign the query structure value to a variable
}
}
}
}
 
List ids = rootVS.getSelectedRowsIds() // get the selected IDs
if (ids.size == 1) {
Map rows = rootVS.getData(ids, DFEnvironmentRO.DEV_NULL) // get the data
Map row = rows[ids[0]] // get the first and only row
MarvinStructure mol = row[molFld.id] // Get the Structure. Its a com.im.df.api.chem.MarvinStructure instance
Molecule cxnMol = mol.getNative() // obtain the chemaxon.struc.Molecule instance
// set other needed parameters for the calculation
JChemSearchOptions jcso = new JChemSearchOptions(JChemSearchOptions.SIMILARITY); // set SIMILARITY search type
jcso.setDissimilarityMetric("TANIMOTO"); // use TANIMOTO metric
Molecule query = queryStructure.getNative() // obtain the chemaxon.struct.Molecule instance and set it as query
Standardizer noSt = new Standardizer("<StandardizerConfiguration><Actions></Actions></StandardizerConfiguration>")
 
// create Dissimilarity calculator with all needed paramenters
DissimilarityCalculator dissimilarity = new DissimilarityCalculator(isReaction, query, jcso, bitCount, bondCount, fpLengthInBits, noSt);
def dissimilarityValue = dissimilarity.computeDissimilarity(cxnMol) // caluclate Disimilarity score for selected molecule
def similarityValue = 1 - dissimilarityValue // calculate the Similarity value
 
println "Similarity Value is ${similarityValue}"
 
} else {
println "bad selection"
}
}
on_change = { widget, button ->
}