# Generating Bit Strings using SMARTs query node

**URL:** <https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865>\
**Category:** Community Extensions\
**Tags:** cdk\
**Created:** [December 30, 2015, 5:11am UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865 "2015-12-30T05:11:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dischiessel](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@dischiessel](https://forum.knime.com/u/dischiessel)\
**Post date:** [December 30, 2015, 5:11am UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865/1 "2015-12-30T05:11:27Z")

</div>

I have a database of chemicals and want to evaluate each chemical against a list of SMARTs patterns also from a database.&nbsp; The goal is to generate my own BitStrings for each chemical that relate to the SMARTs patterns.&nbsp; I have successfully done this BUT I can only do it one SMARTs pattern at a time (attached workflow).

Ideally, the workflow would be to run each chemical from one database table through all the SMARTs patterns (ie: 1-10) from another database table and generate a concatenated bit-string like "1010101111".&nbsp; Then do a database update for each chemical bit-string.&nbsp; Each position in the bit-string means match(1) or non-match(0) relating to the SMARTs ID (1-10) in the database.

I've tried to use Chunk Loop Start on the SMARTs patterns but this doesn't allow the Bitstring to be looped back so that it can be updated (at a particlur bit position) using the String manipulation node.

I downloaded the example workflows for the Recursive loop nodes but have had limited success.&nbsp; I must be missing something but this seems like a doable task for KNIME.&nbsp; In essence, I need to do a nested FOR loop like this:

for x=1 to numChemicals  
&nbsp;&nbsp;&nbsp;&nbsp; for y=1 to numSMARTs  
&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; run SMARTs query and generate BitString using String manipulation node  
&nbsp;&nbsp;&nbsp;&nbsp; end  
end

- [BitString Gen - All chemicals one SMARTs at a time](https://www.knime.com/sites/default/files/smartsbitstringgenerator.jpg)

---

<div class="post-metadata">

**Author:** ![richards99](https://forum-cdn.knime.com/user_avatar/forum.knime.com/richards99/32/48547_2.png) [@richards99](https://forum.knime.com/u/richards99)\
**Post date:** [December 31, 2015, 12:35am UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865/2 "2015-12-31T00:35:42Z")

</div>

My recommendation is to use the RDKit nodes instead.

The node which should deliver what you need is;

RDKit Substructure Counter (with Add Column with tags for matching queries selected).

&nbsp;

You can then use the Column Aggregator node to Aggregate all these 0 and 1 columns together using Concatenate option in the Options tab, and make sure to clear out the Value Delimiter box.

Simon.

---

<div class="post-metadata">

**Author:** ![dischiessel](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@dischiessel](https://forum.knime.com/u/dischiessel)\
**Post date:** [December 31, 2015, 8:45am UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865/3 "2015-12-31T08:45:10Z")

</div>

Thanks for the input Simon.&nbsp; Your instructions were clear and concise.&nbsp;  
I modified the SQL query that retrieves the SMARTs to have a prefix:  
SELECT concat("SubStruc-",FGroupName) AS FGroupName, SMARTS FROM tbl\_ChemFuncGroups

This allowed me to select columns in the aggregator by wildcard(SubStruc\*) avoiding the need to manually add each column as my list of SMARTs grows.

I didn't foresee the value of having "counts" makeup the bitstring as opposed to true/false.&nbsp; The only problem arises when a count is greater than 9; the bit will be 2 chars (unless I convert to something like a 36-base&nbsp; 0-9-A-Z).

Do you have any suggestions on how to handle the possibility of a substructure count \>9? For most of my substructures, they will not be present more than 9, but it's possible.&nbsp;

I could also delimit the bitstring with a comma as this would be adaptable in my application.

-David

---

<div class="post-metadata">

**Author:** ![richards99](https://forum-cdn.knime.com/user_avatar/forum.knime.com/richards99/32/48547_2.png) [@richards99](https://forum.knime.com/u/richards99)\
**Post date:** [December 31, 2015, 11:00am UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865/4 "2015-12-31T11:00:38Z")

</div>

Hi, if you use a comma delimiter, in the column aggregator node, you could follow up your resultant combined column with a string replacer using RegEx to replace all values greater than 0 with a 1, setting the option as a pattern rather than the whole string.&nbsp;For example;

[2-9]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;and then in replace box put a 1

then in a second string replacer node use; (note commas are deliberate here)

[10|11], &nbsp; &nbsp; &nbsp; and then in replace put a 1,

You should now be left with just&nbsp;0's and 1's,

&nbsp;

if if you finally want the commas out you could use a third string replace node with , in the find box and replace box as empty.&nbsp;

Simon.&nbsp;

---

<div class="post-metadata">

**Author:** ![dischiessel](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@dischiessel](https://forum.knime.com/u/dischiessel)\
**Post date:** [January 1, 2016, 8:18pm UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865/5 "2016-01-01T20:18:17Z")

</div>

Thanks Simon.  
Using string replacer node was a good idea.

I was able to use 1 string replacer node to capture counts 1-99 using the regex expression:

**([1-9]{2}|[1-9][0]|[1-9])**

1-9 in the 2nd digit OR  
1-9 followed by a 0 OR  
1-9

The order of this regex pattern matters; putting [1-9] at the beginning will negate the [1-9][0] condition.

Thanks!

David

---

<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:** [April 21, 2023, 9:42pm UTC](https://forum.knime.com/t/generating-bit-strings-using-smarts-query-node/2865/6 "2023-04-21T21:42:07Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
