# Tanimoto similarity for pairs of compounds

**URL:** <https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322>\
**Category:** Cheminformatics\
**Created:** [November 8, 2019, 1:29pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322 "2019-11-08T13:29:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 1:29pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/1 "2019-11-08T13:29:00Z")

</div>

Hi,

I have been struggling to find a way in KNIME to compare molecules in 2 column pairwise (Note: not a pairwise matrix!!!).

Consider an input file (smiles of molecules in both columns):  
Mol1a,Mol1b  
Mol2a,Mol2b  
Mol3a,Mol3b  
…  
Mol1000a,Mol1000b

I’d like to get 1000 Tanimoto vaules for comparing 1000 pairs only (e.g. using ECFP4) and not 1M Tanimoto value pairwise matrix. Is there an efficient way to do so in KNIME? Any piece of advice would be highly appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![swebb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/swebb/32/369_2.png) [@swebb](https://forum.knime.com/u/swebb)\
**Post date:** [November 8, 2019, 1:48pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/2 "2019-11-08T13:48:06Z")

</div>

It’s not going to be a particularly quick solution but you could do this:

![image](https://forum-cdn.knime.com/uploads/default/original/2X/7/79266053ba06a996362e09b1086f38edeceb0444.png)

![image](https://forum-cdn.knime.com/uploads/default/original/2X/c/cfc2444079718a35f36cf493736205ea17b2e7b7.png)

![image](https://forum-cdn.knime.com/uploads/default/original/2X/1/1785e921830978af5f62f0f575dbd4f683a11991.png)

Cheers

Sam

---

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 1:53pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/3 "2019-11-08T13:53:53Z")

</div>

Hi Sam,

Thank you for the proposal. I am actually looking for a quicker solution as looping through the each of millions of pairs will be quite time demanding (I tested a similar solution myself). And the usual scale of my daily operations is \>10^6 molecule pairs… Any other ideas? Thanks!

Kind regards,  
/Alex

---

<div class="post-metadata">

**Author:** ![swebb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/swebb/32/369_2.png) [@swebb](https://forum.knime.com/u/swebb)\
**Post date:** [November 8, 2019, 2:37pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/4 "2019-11-08T14:37:15Z")

</div>

I don’t see an obvious solution with out of the box KNIME nodes and unfortunately the BitVector column isn’t yet compatible with the native type in the Java Snippet.

My next suggestion would be to use a Python snippet and do the calculation of similarity using RDKit in python and append a column with the similarity value.

Unless @s.roughley has done something in the Vernalis contribution that will make this easier?

Cheers

Sam

---

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 2:39pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/5 "2019-11-08T14:39:56Z")

</div>

Thank you, Sam! I will consider this idea. Sad that this easy comparison function is not available in KNIME yet. ☹

---

<div class="post-metadata">

**Author:** ![beginner](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/b/e495f1/32.png) [@beginner](https://forum.knime.com/u/beginner)\
**Post date:** [November 8, 2019, 2:58pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/6 "2019-11-08T14:58:53Z")

</div>

Instead of Python I would do it in the Java snippet as you can use rdkit there as well and you don’t have to pay the large serialization penalty between knime(java) and python. Given the large amount of molecules that will in my opinion be a lot faster.

---

<div class="post-metadata">

**Author:** ![Vernalis](https://forum-cdn.knime.com/user_avatar/forum.knime.com/vernalis/32/356_2.png) [@Vernalis](https://forum.knime.com/u/Vernalis)\
**Post date:** [November 8, 2019, 3:13pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/7 "2019-11-08T15:13:26Z")

</div>

WE have nodes which will do things like list the set bits from a fingerprint. I guess then you could use a java snippet to calculate your own Tanimoto, e.g.:

![image](https://forum-cdn.knime.com/uploads/default/original/2X/0/0d33a113ca9f116b775dceeb1f903c49a70f769e.png)

![image](https://forum-cdn.knime.com/uploads/default/original/2X/a/a6743a1694f2c3f82779fc816c6e696a34c05ed4.png)

![image](https://forum-cdn.knime.com/uploads/default/original/2X/c/ce71bee805f1c49b103360e2f045e513272664f1.png)

 ![image](https://forum-cdn.knime.com/uploads/default/original/2X/0/018b21c93ee4b9b77798292386a288b6cdac1d40.png)

Snippet:

```auto
// Your custom imports:
import java.util.Set;
import java.util.LinkedHashSet;
import java.util.Arrays;

// system variables
public class JSnippet extends AbstractJSnippet {
  // Fields for input columns
  /** Input column: "fp1 (Set bits)" */
  public Long[] c_fp1Setbits;
  /** Input column: "fp2 (Set bits)" */
  public Long[] c_fp2Setbits;

  // Fields for output columns
  /** Output column: "Tanimoto" */
  public Double out_Tanimoto;

// Your custom variables:

// expression start
    public void snippet() throws TypeException, ColumnException, Abort {
// Enter your code here:
Set<Long> fp1 = new LinkedHashSet<>(Arrays.asList(c_fp1Setbits));
Set<Long> fp2 = new LinkedHashSet<>(Arrays.asList(c_fp2Setbits));

int a=fp1.size();
int b=fp2.size();
fp1.retainAll(fp2);
int c=fp1.size();

		
out_Tanimoto = 1.0*c / (a + b -c);

```

![image](https://forum-cdn.knime.com/uploads/default/original/2X/6/618d747276190969f9ebb835457497f7a1186eb6.png)

Hope that helps?

Steve

---

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 3:16pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/8 "2019-11-08T15:16:35Z")

</div>

Thanks! Could you provide an example code for Java snippet, please?

---

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 3:19pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/9 "2019-11-08T15:19:00Z")

</div>

Have not seen Steve’s reply when I sent my previous message. I will try the code and let you know. Thank you to all for fast response! 😉

---

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 3:54pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/10 "2019-11-08T15:54:07Z")

</div>

Hi Steve,

I have tried your suggestion, however, Java snippet complains:

 ![image](https://forum-cdn.knime.com/uploads/default/original/2X/8/8971e85e8c2741461c750ae1d39d45db008ca7d3.png)

Could you, please check what is wrong? May be you can share a workflow, so i can load and test it? Thanks!

Kind regards,  
/Alex.  
P.S. I am using Knime 3.5.2. version (just in case it makes a difference).

---

<div class="post-metadata">

**Author:** ![Vernalis](https://forum-cdn.knime.com/user_avatar/forum.knime.com/vernalis/32/356_2.png) [@Vernalis](https://forum.knime.com/u/Vernalis)\
**Post date:** [November 8, 2019, 3:57pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/11 "2019-11-08T15:57:07Z")

</div>

I had the exact same problem - you need to change the Java Type of the input columns to ‘Array of Long’  
Example attached

[Tanimoto\_Example.knwf](https://forum-cdn.knime.com/uploads/default/original/2X/d/d72c0414d4eecc33055cff14e3b028db5609ebc1.knwf) (8.8 KB)

In case you cant open the example:

 ![image](https://forum-cdn.knime.com/uploads/default/original/2X/a/a02683f7f882ec1c7d36a5c60b19f735f95f865c.png)

Steve

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [November 8, 2019, 4:00pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/12 "2019-11-08T16:00:05Z")

</div>

> [@Aleksejs](#):
>
> I am using Knime 3.5.2. version

Nice! That is the version I started on 😃

Why not update?

Br,  
Ivan

---

<div class="post-metadata">

**Author:** ![s.roughley](https://forum-cdn.knime.com/user_avatar/forum.knime.com/s.roughley/32/366_2.png) [@s.roughley](https://forum.knime.com/u/s.roughley)\
**Post date:** [November 8, 2019, 5:23pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/13 "2019-11-08T17:23:39Z")

</div>

Oh dear, now I feel KNIME-old. I think I started on around KNIME 2.2!

[https://www.knime.com/changelog-v220](https://www.knime.com/changelog-v220)

Steve

---

<div class="post-metadata">

**Author:** ![Aleksejs](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Aleksejs](https://forum.knime.com/u/Aleksejs)\
**Post date:** [November 8, 2019, 5:50pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/14 "2019-11-08T17:50:40Z")

</div>

Dear all,

Thank you for your help. Everything works fine now!

Best,  
/Alex.

---

<div class="post-metadata">

**Author:** ![ipazin](https://forum-cdn.knime.com/user_avatar/forum.knime.com/ipazin/32/806_2.png) [@ipazin](https://forum.knime.com/u/ipazin)\
**Post date:** [November 12, 2019, 12:26pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/15 "2019-11-12T12:26:40Z")

</div>

> [@s.roughley](#):
>
> I think I started on around KNIME 2.2!

Wow! Now I feel like a fresh KNIME user!

Some nice features there 😃

---

<div class="post-metadata">

**Author:** ![s.roughley](https://forum-cdn.knime.com/user_avatar/forum.knime.com/s.roughley/32/366_2.png) [@s.roughley](https://forum.knime.com/u/s.roughley)\
**Post date:** [November 12, 2019, 6:08pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/16 "2019-11-12T18:08:07Z")

</div>

Yes, and the community has changed a lot too. No Vernalis, no RDKit back then, and possibly no Indigo, amongst many other things…

Steve

---

<div class="post-metadata">

**Author:** ![RamuE](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@RamuE](https://forum.knime.com/u/RamuE)\
**Post date:** [June 7, 2023, 5:37am UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/17 "2023-06-07T05:37:09Z")

</div>

Hi, Swebb,  
I am using KNIME 4.5.2, but in this version, Finger print similarity, Fingerprint to set Bits List nodes are not available. How can I install these nodes and deploy to the KNIME version right now I am using. Please help me in this regard.

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [June 23, 2023, 8:38pm UTC](https://forum.knime.com/t/tanimoto-similarity-for-pairs-of-compounds/19322/18 "2023-06-23T20:38:31Z")

</div>


