Help for Compute tool syntax

Can one use “if…else” syntax in “compute on rows” tool? if yes, what’s the exact syntax? If not, what other tools can I use?

I have a tabular file. Column 1 is the list of gene loci. Column 2 and 3 are two numbers at each locus. I would like to calculate the ratio of c3/c2 if c2 does not have a value of 0. I’ve been trying to use “Compute on rows” and “Table compute” using various text, trying the expression
“if c2 == 0: 1 else c3 / c2”, but there is always an error. What is the correct way to achieve this?
Thank you!

Welcome @elodi55

Are you trying to skip the rows with the N/A or other values that are not a number? To avoid the error these tools can produce when some columns do not have standard numerical values?

If so, the usual way to do this is to either pre-filter the table with one of the Filter tools, or you can use the extra options near the end of the Compute on rows too form to customize which lines are processed versus ignored. More about Text Manipulation tools → Hands-on: Data Manipulation Olympics / Data Manipulation Olympics / Introduction to Galaxy Analyses

We have an example in this tutorial section. See → Hands-on: 3: RNA-seq genes to pathways / 3: RNA-seq genes to pathways / Transcriptomics. (with more examples linked at the bottom of the Compute on rows tool form, or the other Text Manipulation tool forms).

The last two options here fit together. 1) Instruct the tool not to fail and 2) instead do something else, such as skipping certain line. Toggle the form options to see what else is available.

Please give this and try and let us know if you have follow up questions! I wasn’t sure if I was understanding your question correctly. In short, you can combined some functions and not others, on a per tool basis, but you can always string together tools in a mini-workflow to create quick custom processing that you may want to repeat, sort of like creating your own custom tool if you hide the intermediate datasets. This is similar to how the analogous utilities are used on the command line and most are named in the same way to make them easier to find.

Hope this helps! :slight_smile:

XRef → add_a_column1 compute compute-on-rows

1 Like

Hi Jennifer,

I only just want to assign a fixed number (for example, 1) when the denominator is 0, as dividing by 0 will give an error. I know this can be easily processed if I write a script, but I really want to get it work in Galaxy, so I don’t have to go in and out of the galaxy environment.

I will read more about the Filter tool and check out the tutorials. Maybe I can first replace the zeros with a non-zero number. I will give it a try.

Thank you for your prompt response.

Bibo

Hi,
then like Jen said, the “Error handling” section of Compute on rows is what you want, specifically:

this should allow you to do what you want because, just like you’re saying dividng by zero will result in an error, which will be caught with this setting, and the tool will then use whatever replacement value you instruct it to use instead.

Hi wm75,

Thank you for the suggestion. Yes, I thought about this. However, I would like to replace the 0 value to a non-fixed number. The actual number will also depend on the value of the next column at the same row. I will need to think ways to check different conditions (requirements) at different steps, as Jennifer suggested.

Thanks,

Bibo

Well, that’s a very different usecase then.
You can still use the same tool, but (untested!) you’d use an expression like c3 / (c2 or cX) where cX is your column with the replacement values and where the or will make sure the value from cX is taken when c2 is zero (which is the same as a logical False in Python).

Ah, wait: you do not want to divide by the replacement value, but use it instead of the division’s result, correct?

Hi wm75,

I have a tabular file. In the first column is just the index (list of names). In the second and third columns are numbers. I would like to calculate the ratio of c3/c2 if c2 value is not 0, and if c2 value is 0, I would like to arbitrarily assign the result to be 1. I would like to save the result in column 4.

I have tried various conditional expressions using either “compute on row” or “Table compute”. The expressions are like "if c2 == 0: 1 else c3 / c2” or "c3 / c2 if c2 != 0 else 1”, or something similar. However, the app does not recognize the syntax I used. I think I must have got some details wrong. At one point, it seems that “compute on row” does not accept “if…else” argument. I’m not sure whether this is true.

I wonder whether the exact syntax depends on the version of Galaxy? We have Galaxy version 23.2 installed on our local server.

Alternatively, I wonder maybe I should use a completely different process than “compute on row” or “table compute”. If you have any other suggestions, I would like to give it a try.

Thank you for your help!

Bibo

So it is a constant value that you want to use in case c2=0.
Then my first suggestion, the one using error handling is just what you want, isn’t it?

Yes. Our server is tied up with another process, and I just uploaded my file to usegalaxy.org. I tried your way of error handling and it works now.

Thank you very much for your help!

Bibo

2 Likes