# Using groovy without bash in process

**URL:** <https://community.seqera.io/t/using-groovy-without-bash-in-process/2002>\
**Category:** Ask for help\
**Tags:** nextflow\
**Created:** [March 21, 2025, 5:14pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002 "2025-03-21T17:14:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![yinshiyi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/yinshiyi/32/1091_2.png) [@yinshiyi](https://community.seqera.io/u/yinshiyi)\
**Post date:** [March 21, 2025, 5:14pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002/1 "2025-03-21T17:14:00Z")

</div>

This works fine. But it has to go through bash for a simple math formula.

```auto
process CAL_SCALEFACTOR{
    input:
    tuple val(SAMPLE), val(total_reads), val(spike_in_reads)
    output:
    tuple val(SAMPLE), env(scaleFactor)
    script:
        def spike = spike_in_reads as double
        def total = total_reads as double
        def scaleFactor = (spike + total) / spike / 100.0
    """
    export scaleFactor=$scaleFactor
    """
}

```

i tried to use idea like these, it will still try to go through bash and error out.

```auto
process CAL_SCALEFACTOR{
    input:
    tuple val(SAMPLE), val(total_reads), val(spike_in_reads)
    output:
    tuple val(SAMPLE), val(scaleFactor)
    script:
        def spike = spike_in_reads as double
        def total = total_reads as double
        def scaleFactor = (spike + total) / spike / 100.0

}

```

any suggestions? Thank you.

---

<div class="post-metadata">

**Author:** ![bentsherman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bentsherman/32/52_2.png) [@bentsherman](https://community.seqera.io/u/bentsherman)\
**Post date:** [March 21, 2025, 6:31pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002/2 "2025-03-21T18:31:13Z")

</div>

Just replace the `script:` label with `exec:` and it will treat the process as a “native” process. No Bash script needed.

> **[Processes — Nextflow documentation](https://nextflow.io/docs/latest/process.html#native-execution)**

Also, remove the `def` from `scaleFactor` so that it can be referenced in the output section. It’s a quirk of processes, basically only variables declared without `def` can be used by the outputs.

---

<div class="post-metadata">

**Author:** ![yinshiyi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/yinshiyi/32/1091_2.png) [@yinshiyi](https://community.seqera.io/u/yinshiyi)\
**Post date:** [March 21, 2025, 6:39pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002/3 "2025-03-21T18:39:05Z")

</div>

I vaguely remembered there were some warnings about `def`.  
something about def will create a local variables, without def could be a global variable leak to other processes (race condition)?  
[Scripts — Nextflow documentation](https://www.nextflow.io/docs/latest/script.html#:~:text=Variables%20can%20also%20be%20declared%20without%20def%20in%20some%20cases.%20However%2C%20this%20practice%20is%20discouraged%20outside%20of%20simple%20code%20snippets%20because%20it%20can%20lead%20to%20a%20race%20condition).  
maybe i am overthinking it, since my script is very simple, probably not likely any race condition.  
[Syntax — Nextflow documentation](https://www.nextflow.io/docs/latest/reference/syntax.html#:~:text=Process%20input%20variables%20exist%20for%20the%20entire%20process%20body.%20Variables%20declared%20in%20the%20process%20script%2C%20exec%2C%20and%20stub%20sections%20exist%20only%20in%20their%20respective%20section%2C%20with%20one%20exception%20%E2%80%93%20variables%20declared%20without%20the%20def%20keyword%20also%20exist%20in%20the%20output%20section).  
Thanks!

---

<div class="post-metadata">

**Author:** ![robsyme](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/robsyme/32/342_2.png) [@robsyme](https://community.seqera.io/u/robsyme)\
**Post date:** [March 21, 2025, 9:00pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002/4 "2025-03-21T21:00:19Z")

</div>

This sort of very small operations might be better handled by a `map` operator.

It looks like you’re currently spinning up a new task, something like:

```groovy
workflow {
  // creating the channel not shown
  CAL_SCALEFACTOR(my_channel)

  factor_channel = CAL_SCALEFACTOR.out
}

```

Instead, you could avoid defining a process entirely and calculate your scale factor in a closure:

```groovy
factor_channel = my_channel.map { sample, total_rads, spike_in_reads ->
  spike = spike_in_reads as double
  total = total_reads as double
  scaleFactor = (spike + total) / spike / 100.0
  [sample, scaleFactor]
}

```

---

<div class="post-metadata">

**Author:** ![yinshiyi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/yinshiyi/32/1091_2.png) [@yinshiyi](https://community.seqera.io/u/yinshiyi)\
**Post date:** [March 21, 2025, 9:06pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002/5 "2025-03-21T21:06:10Z")

</div>

Thank you @robsyme ! great point

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [March 28, 2025, 9:06pm UTC](https://community.seqera.io/t/using-groovy-without-bash-in-process/2002/6 "2025-03-28T21:06:38Z")

</div>

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