Skip to content

TanimotoMultiple

Calculate Similarity score

1
{primary}

Since version 21.8.0 there was API upgrade. Use follwing script, if you are using this version or newer, otherwise use the older below.

/*
* Find Similarity Value of multiple selection
*
* Usage:
* 1. Run your Similarity Query - needed, otherwise script will not run
* 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
*
* Since 20.18.0 API has been changed on the DissimilarityCalculator interface
* https://dl.chemaxon.com/instantjchem/ijc_latest/docs/developer/api/com-chemaxon-ijc-wrappers-dif-api/com/im/df/api/chem/DissimilarityCalculator.html
*
* @author David Pech <dpech@chemaxon.com>
*
*/

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 com.chemaxon.dif.jchem.calc.*; // since 20.18.0

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()
                        // deprecated .get(LegacyConstants.CASE_INSENSITIVE_SEARCH);
                        .get(Operators.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 IDs of the selection
    // obtain the structure field from Entity
    // expects Structure Entity type
    DFField structureField = ety.getStructureField()
    Set<DFField> set = new HashSet<DFField>();
    set.add(structureField);
    // obtain the data
    // method expects list of IDs and a HashSet of the fields to obtain
    // as we defined above, the HashSet contains only Structure field
    Map rows = rootVS.getData(ids, set, DFEnvironmentRO.DEV_NULL)

    // Create the dissimilarity calculator and
    // set other needed parameters for the calculation
    JChemEntityCapability jchem = DIFUtilities.findCapability(structureField.getEntity(), JChemEntityCapability.class); //Since 20.18.0 with new implementation
    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
    //Replaced by JChemDissimilarityCalculator since 20.18.0 *DissimilarityCalculator dissimilarity = new DissimilarityCalculator(isReaction, query, jcso, bitCount, bondCount, fpLengthInBits, noSt);
    JChemDissimilarityCalculator dissimilarity = new JChemDissimilarityCalculator(query, jcso, noSt, jchem);
    // cycle through the data, for each row obtain the structure
    // convert it to chemaxon.struct.Molecule object
    // and parse it to dissimilarity value calculator
    for (row in rows.values()) {
        MarvinStructure mol = row[molFld.id]
        Molecule cxnMol = mol.getNative()
        //Replaced by JChemDissimilarityCalculator since 20.18.0 *def dissimilarityValue = dissimilarity.computeDissimilarity(cxnMol)
        def dissimilarityValue = dissimilarity.calculateDissimilarity(cxnMol) // caluclate Disimilarity score for selected molecule
        def similarityValue = 1 - dissimilarityValue // calculate the Similarity value

        println "Similarity Value is ${similarityValue}"
    }

}
on_change = { widget, button ->

}

Until version 20.18.0 use this version of script below

/*
* Find Similarity Value of multiple selection
*
*
* Usage:
* 1. Run your Similarity Query - needed, otherwise script will not run
* 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 <dpech@chemaxon.com>
*/
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 IDs of the selection
    // obtain the structure field from Entity
    // expects Structure Entity type
    DFField structureField = ety.getStructureField()
    Set<DFField> set = new HashSet<DFField>();
    set.add(structureField);
    // obtain the data
    // method expects list of IDs and a HashSet of the fields to obtain
    // as we defined above, the HashSet contains only Structure field
    Map rows = rootVS.getData(ids, set, DFEnvironmentRO.DEV_NULL)

    // Create the dissimilarity calculator and
    // 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);


    // cycle through the data, for each row obtain the structure
    // convert it to chemaxon.struct.Molecule object
    // and parse it to dissimilarity value calculator
    for (row in rows.values()) {
        MarvinStructure mol = row[molFld.id]
        Molecule cxnMol = mol.getNative()

        def dissimilarityValue = dissimilarity.computeDissimilarity(cxnMol) // caluclate Disimilarity score for selected molecule
        def similarityValue = 1 - dissimilarityValue // calculate the Similarity value

        println "Similarity Value is ${similarityValue}"
    }

}
on_change = { widget, button ->

}