setCell(index) in Java Snippet

Hello KNIME community,
Sorry for the re-post, I think I hijacked a thread in my previous post...

I am using a Java Snippet node and perform some iterative calculation based on each row of a column. I would like to update the last row of this column with the result of the calculation.

The issue is that the column name is generated at run time. So, I use the getCell() function fine to retrieve the content of a cell, based on its column index. However, I cannot find an elegant way to set the content of a cell.

Here is the pseudo code for what I'm using (first two lines) and what I would like to use (last line):

value = getCell(v_columnIndex, tDouble);
// Perform calculation
setCell(v_columnIndex) = value;


It is the last function's functionality that I am looking for (setCell() does not exist, AFAIK).

One alternative would be to use Don's suggestion. However, I would like to check with you, user and/or KNIME developers, if it is possible to have the equivalent to setCell().

Cheers,
Fred

 

1 Like

Hi Fred,

Correct, you can't change the content of a KNIME DataTable which is read-only. But the good news are that the Java Snippet (I used the simple one) can help:

Global Declaration:

Double sum = null;

Method Body:

Double d = $Universe_1_1$;
if (sum == null) {
  sum = d;
} else {
  sum = sum + d;
}
if ($$ROWINDEX$$ + 1 < $$ROWCOUNT$$) {
  return d;
} else {
  return sum;
}

Replace Column:

Universe_1_1

In this example, I iterate all rows and keep the sum of all values in a variable d; if the currently ROWINDEX+1 is the ROWCOUNT, max number of rows, I return this value, otherwise I return d which is the current cell value. Hope this helps?

Cheers, Thomas

 

Hello Thomas,

 

Thanks for your reply.

 

You mention that the DataTable is read-only but, as you just examplified, we are able to write to it.

 

In your example, Universe_1_1 is a known column name. I am looking for a solution that will be able to cope with a column name that is only known at run time.

 

In order to get this column's value at the row of interest, I iterate through the rows and use getCell(column_index, type). Unfortunately, it does not seem to be possible to natively return a value to a column or a variable if you do not know its name before run time, unless you do as suggested by Don (dnaki) here.

 

Cheers,

Fred

Hi Fred,

Got it. I guess you want to iterate those columns a compute a final value for each of those indiviual columns. There is a Column Loop Start and Column Loop End node which can be used to process one column at a time. It's a little bit tricky to get an unique column name, but it can be achieved by using the Column Rename (Regex). Please check out the attached workflow.

Cheers, Thomas

Hello Thomas,

 

Thanks for the workflow you provided. It is a good workaround. However, after spending a lot of time trying to adapt it to my problem, I cannot find a way to do so.

 

  • A, B and C are values found in 3 different columns.
  • These columns are child results of a biological assay. There are multiple assays (number not known).
  • So, we have columns assay1_A, assay1_B, assay1_C, assay2_A, assay2_B, assay2_C... assayn_C.
  • I calculate 3 values, Ai, Bi and Ci, where i is the row index.
  • Ai+1 is based on values of Ai and Ci, Bi+1 on values of Ai, Bi and Ci, and Ci+1 on values of Ci.
  • At the end of each assay group (one or many results), I want to set the result of A in column A, and so on.

 

All this information to see if there can be a solution to this problem, when I know that modifying the underlying code to expose a setCell() method would do the trick...

Can this be done? If so, how long would it take?

 

Cheers,

Fred