Rename columns in Java Snippet

Dear all,
I want to rename columns if they have specific names. For that I am using the columnExists function in a Java snippet. It works fine, but now I would like to rename the existing columns by adding some prefix. Is that possible?
I know that its much easier with R or Python but I have sdf/smiles columns which are converted to string if I use such snippets. This will make my life harder as I would have to change the overall workflow (as the molecule column name changes), whereas with java this could be done within one node.
If there are other options to do this in a dynamic way I’ll also be glad to get to know these.

Thanks in advance!

Jennifer

Hi,
You can loop over the columns and use the Column Rename (Regex) node to add a prefix to each column name.
image
col rename.knwf (11.6 KB)

Hope this helps.
-Don

Hey,

thanks. Unfortunately this is not possible. I do not want to append the suffix to all columns, only the ones which have problematic names. And as the names are non-related I cannot generate a useful Regex to match all of the names.
What I want to do:
0. Load a file chosen by user

  1. Find out if I have columns with specific names, eg x = [ SMILES,Prop1,Name]
  2. If a column is named as in x : Append suffix given by user in Quickform
    –> If no column is named: proceed

In python this is straightforward but it does convert Molecule formats to strings, which would need me to know the molecules columns name and convert it back to the respective format. It is doable, but it would be easier if I could just change the names without converting the mol column to string (and therefore the need to know what the Molecule format and column name is).

Thanks again,
Jennifer

Hi,
You can still use my solution…
You can include literals in your Regex using \Q and \E.
\Q marks the beginning of the literal and \E the end.

So your regex might look something like this:
(\QmyFirstCol\E|\QmySecondCol\E|\QmyThirdCol\E)

You’ll want precede the Column Rename (Regex) node with a Java Edit Variable node that generates the regex using your actual column names.

image

That regex variable can then be used in the Column Rename (Regex) node.
(Go to the variables tab of the config dialog and select your variable:)

image

col rename.knwf (14.0 KB)

-Don

P.S. If you really want to use a Java Snippet node to do the renaming, you can try a similar approach, but it’s a bit more complex/inelegant, IMHO. Replace the col rename node with Java Snippet and use a flow variable to dynamically rename the column using the flow variable tab similar to the second pic above.

2 Likes

Hey,
thanks a ton! I did not know \Q\E and that just made my Life so much easier, and in that case I happily get rid of the Java snippet :wink:

With the Rename node you actually do not need the Loop as the node itself checks all present columns.

Thanks again, that helped a lot.

Best,
Jennifer

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