# Work with the Steps Pooling Endpoint

Pooling steps require that each input analyte artifact (derived sample) in the step be inserted into a pool. You can automate this task by using the API steps pooling endpoint. Automation of pooling allows you to reduce error and user interaction with Clarity LIMS.

In this example, a script pools samples based on the value of the pool id user-defined field (UDF)/custom field of the artifact.

As of Clarity LIMS v5, the term **user-defined field** (UDF) has been replaced with custom field in the user interface. However, the API resource is still called **UDF**.

* **Master step fields**—Configured on master steps. Master step fields only apply to the following:
  * The master step on which the fields are configured.
  * The steps derived from those master steps.
* **Global fields**—Configured on entities (eg, submitted sample, derived sample, measurement, etc.). Global fields apply to the entire Clarity LIMS system.

To keep this example simple, the script does not handle samples with reagent labels.

{% hint style="info" %}
In the API, an artifact is an item generated by an earlier step. There are two types of artifacts: analyte (derived sample) and resultfile (measurement). In the Clarity LIMS web interface, the terms artifact, analyte, and resultfile have been replaced with derived sample or measurement.
{% endhint %}

### Prerequisites

Before you follow the example, make sure that you have the following items:

* A configured analyte UDF/derived-sample custom field named pool id in Clarity LIMS.
* Groovy that is installed on the server and accessible at **/opt/groovy/bin/groovy**
* The **GLSRestApiUtils.groovy** file is stored in **/opt/groovy/lib/**
* The **WorkingWithStepsPoolingEndpoint.groovy** script that is stored in **/opt/gls/clarity/customextensions/**
* A compatible version of API (v2 r21 or later).

### Example <a href="#example-pre5" id="example-pre5"></a>

#### Step 1. Add Master Step, Protocol, and Library Pooling Step <a href="#step2" id="step2"></a>

1. In Clarity LIMS, under Configuration, select the **Lab Work** tab.
2. Select an existing Pooling master step or add a new one.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-bbfda9cad2f135611a642280434558b263d8f1a5%2FPoolingEndpointCreateMasterStep_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
3. On the master step configuration form, select the **Pooling** milestone.
4. On the Pooling Settings form, set the Label Uniqueness toggle switch to **Off**.
5. Select **Save**.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-b7e519c3232bc48b700bb28e8cb25c5640687373%2FPoolingSettingsLabelUniquenessOff_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
6. Add a new protocol.
7. With the protocol selected, add a new Library Pooling step based on the master step you configured.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-19176a1b2093298e7aaff76d2ba72083540fc9be%2FPoolingEndpointAddProtocolAndStep_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>

#### Step 2. Configure Step Automation and Trigger <a href="#step2" id="step2"></a>

1. In Clarity LIMS, under Configuration, select the **Automation** tab.
2. Add a new step automation. Associate the automation with the WorkingWithStepsPoolingEndpoint.groovy script. The command line used in this example is as follows.

   `bash -c "/opt/groovy/bin/groovy -cp /opt/groovy/lib /opt/gls/clarity/customextensions/WorkingWithStepsPoolingEndpoint.groovy -u {username} -p {password} -s {stepURI:v2:http}"`

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-bb2562000323ca9074c6e4bc57563636f35f92b5%2FPoolSamplesAutomation_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
3. Enable the automation on the configured pooling master step. Select Save.

   You can now configure the automation trigger on the step or the master step. If you configure the trigger on the master step, the settings will be locked on all steps derived from the master step.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-1661dd5437d4f60ea0bccefd780edc41c6cdcf51%2FPoolSamplesEnableAutomation_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
4. On the Lab Work tab, select the library pooling step or master step.
5. On the Step Settings or Master Step Settings form, in the Automation section, configure the automation trigger so that the script is automatically initiated at the beginning of the step:
   1. **Trigger Location**—Step
   2. **Trigger Style**—Automatic upon entry

      <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-fa6c59787112dd86afdf7a74d8921726bb98c97f%2FPoolSamplesConfigureAutomationTrigger_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>

#### Step 3. Configure Protocol and Add Pool ID Step <a href="#step4" id="step4"></a>

1. In Clarity LIMS, under Configuration, select the **Lab Work** tab.
2. Select the pooling protocol containing the Library Pooling step.
3. Add the Add Pool ID step that sets the pool id custom field of the samples. Move this step to the top of the Steps list.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-21157cb6d2f2b82e7bc7f8183aff14b1d1a93975%2FPoolingEndpointAddStepAddPoolID_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
4. Select the **Add Pool ID** step.
5. On the **Record Details** milestone, add the pool id custom field to the Sample Details table.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-de62e2a6139f70f8a56363f8ea1c885335521849%2FPoolingEndpointConfigureSampleDetailsv5_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>

#### Step 4. Create Workflow and Add Samples <a href="#example-pre5" id="example-pre5"></a>

1. In Clarity LIMS, under Configuration, select the **Lab Work** tab.
2. Create a workflow containing the configured pooling protocol. Activate the workflow.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-bb5243ce6a7027bd36b8da552d8c58ec4ed5cd7f%2FPoolingEndpointAddWorkflow_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
3. On the Projects and Samples screen, create a project and add samples to it. Assign the samples to your pooling workflow.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-7be3fe136112fbb69bf5cfa66195ee21d47d5ecd%2FPoolingEndpointAddSamplesToWorkflowv5_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>

#### Step 5. Run Samples Through the Steps

1. Begin working on the samples. In the first step, enter values into the pool id custom field.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-1e013d323f115f9a4467eb8c4c118cdfb4b8862b%2FPoolingEndpointAssignPoolID_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>
2. Continue to the Library Pooling step and add samples to the Ice Bucket. Select **Begin Work** to execute the script.

   <figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-c69337f600147ecb9b783169c959afcf90afab50%2FPoolingEndpointPoolingExecutingCustomProgramv5_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>

### How the script works <a href="#script" id="script"></a>

The script is passed the URI of the pooling step. Then, using the URI, the pool node of the step is retrieved. This node contains an available-inputs node that lists the URIs of the available input artifacts.

```
// Get the pool for this step
poolNode = GLSRestApiUtils.httpGET(stepURI + '/pools', username, password)
poolNode.'available-inputs'.'input'.each{
    artifact_uris.add(it.@uri)
}
```

The script retrieves all available input artifacts, and then iterates through the list of retrieved artifacts. For each artifact, the script looks for the pool id custom field. If the field is not found, the script moves on to the next artifact. If the field is found, its value is stored in the poolID variable.

When the script encounters a new pool ID, it creates a new pool with a name equal to that ID. Input artifacts are sorted into pools based on the value of its pool id field, and as they are inserted into pools they are removed from the list of available inputs.

```
// Get all input artifacts
artifacts = GLSRestApiUtils.batchGET(artifact_uris, username, password)

// Iterate through retrieved artifacts
artifacts.each {
    // If the artifact has a UDF pool ID, then pool the artifact and remove it from the available-inputs node
    poolID = it.find { artifact_field -> artifact_field.@name == POOL_ID_UDF}?.value()[0]
    if (poolID != null) {
        // If a new pool ID is encountered, then use that new pool ID as the name for a new pool node
        if (!poolNode.'pooled-inputs'.'pool'.find { pool -> pool.@name == poolID }) {
            newPool = NodeBuilder.newInstance().pool(name:poolID)
            poolNode.'pooled-inputs'[0].append(newPool)
        }

        inputNode = NodeBuilder.newInstance().input(uri:it.@uri)

        // Add the input artifact to the pool with the name equal to 'poolID'
        poolNode.'pooled-inputs'.'pool'.find { poolID == it.@name }.append(inputNode)

        // Remove the input artifact from the 'available-inputs' node contained in the pool node
        poolNode.'available-inputs'[0].remove(inputNode)
    }
}

```

After all of the available inputs are iterated through, the updated pool node is sent back to Clarity LIMS:

```
//Update the pool for this step and examine the result data
resultNode = GLSRestApiUtils.httpPUT(poolNode, poolNode.@uri, username, password)
```

### Expected Output and Results <a href="#results" id="results"></a>

Artifacts with the same **Pool ID** UDF / custom field will be automatically added to the same pool.

<figure><img src="https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-e0b4e22f8735816d289c31a43f8b4c9cb20c5e3b%2FPoolingEndpointExpectedResults_swCL.jpeg?alt=media" alt=""><figcaption></figcaption></figure>

### Attachments

WorkingWithStepsPoolingEndpoint.groovy:

{% file src="<https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-d14e94ee4aedb6877da14762c914fd7e508fb12b%2FWorkingWithStepsPoolingEndpoint.groovy?alt=media&token=f6a5a197-9fc1-4b9f-b803-90032506408a>" %}

GLSRestApiUtils.groovy:

{% file src="<https://2084401275-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjuebS41N49G1Eh55hP7%2Fuploads%2Fgit-blob-fc353f16f9a53fccc26f8ff8e231abf315dc0ade%2FGLSRestApiUtils.groovy?alt=media&token=4c17af06-39cd-40b1-87a0-c25940201cf2>" %}
