Assay transformation scripts offer an automatic way to clean and validate data during assay import. This topic shows how create a transformation script in Python which adds a new column containing the average value of three columns in the same data. The transform_helper utility described here is a helpful way to simplify writing transformation scripts.

Learn more about the basics of writing transformation scripts in this topic:

Set Up

Install Python3

Check the API documentation to find out the current supported version of python. Download and install a supported version for your operating system from the Python site. If you include Python on your path (one of the options during installation) you will be able to run it from anywhere.

If you're installing Python3 on a mac, you may want to use Homebrew.

To test that the correct version of Python is active on your path, run this from the location where you will run your Python scripts:

python --version

Set Up Python Scripting

Follow the instructions in this topic to configure your server for Python scripting, if you have not already done so:

Configure LabKey Folder

Create a working folder:

  • Log in to your server and navigate to your "Tutorials" project. Create it if necessary.
  • Create a new subfolder named "PythonDemo" (with no spaces). Choose the folder type Assay and accept all other defaults.

Upload files:

Next, import our preconfigured assay design:

  • In the Files web part, check the box for assayForPythonTransformationScript.xar.
  • Click Import Data.
  • In the popup, confirm that Import Experiment is selected, then click Import.
  • When the upload is complete, refresh your browser.
  • You'll see the assay design listed.

Edit the assay to add the pyScript.py transformation script.

  • Click assayForPythonTransformationScript on the Assay List.
  • Select Manage Assay Design > Edit assay design.
  • Click Add Script and select or drag and drop the "pyScript.py" file you downloaded earlier.
  • Click Save.

Use Script

  • Return to the PythonDemo folder.
  • The sample data file, uploadedResults.csv, is in the Files browser.
  • If you open the downloaded version, you will see it includes three columns of "result" values.
  • In the Files web part, check the box for uploadResults.csv to select it.
  • Click Import Data.
  • In the popup, select Use assayForPythonTransformationScript and click Import.
  • Click Save and Finish.
  • After the import completes, in the Assay ID column, click uploadedResults.csv.
  • You will see that there is new data populating the Average Result column.

Read the Transformation Script

The pyScript.py file reads each row of the imported data file, calculates the average, and then appends the average before writing to the output file.

Transform Helper

The Transform Helper function provides the utilities of:

  • Read in the tabular input and run properties
  • Run the user-provided transformation on each row of the input
  • Write the transformed results data to the target assay.
To use the helper, your script must begin with these two lines:
from labkey.utils import transform_helper

filepath = '${runInfo}'

The example below shows how to use it.

Learn more about the transform_helper function in the API documentation here:

pyScript.py Script

The script attached to this page passes a function that will calculate the average and append it to the row within the transform helper, then the results will be written out.

from labkey.utils import transform_helper

filepath = '${runInfo}'

def transform(grid):
isHeaderChecked = False
# iterate through the rows of your results data, checking for the header
for row in grid:
if isHeaderChecked == False:
row.append('averageResult')
isHeaderChecked = True
else:
# In this example, we are taking the average of the 3rd-5th column values by row
# and appending that average value in a 6th column called "averageResult"
newValTemp = sum([float(val) for val in row[2:]])/len(row[2:])
row.append(round(newValTemp, 2))
return grid

transform_helper(transform, filepath)

You can use this script as a starting place for developing more complex transformations for your assay data.

Troubleshooting Tips

When developing a transformation script in python, starting from a small working example, and using the Save Script Data for Debugging options are the best ways to start. Other tips include:

  • Use print() within the script to see incremental progress and identify where you may not have the content you expect. For example, if you expect a loop to read all rows, but it is only reading the first row, using a print() statement can help identify that issue.
  • You can shorten the test loop by starting to import assay data, then if the failure occurs before the import completes, refreshing the import page will retrigger the script.
  • Consider whether you will be more successful working on the originally imported file or the TSV file that is automatically inferred. Specifically handling of commas and spaces may be different in parsing CSV, TSV, and Excel source files.
  • Note that any column aliases you have defined will be applied before the script is run. If you are expecting actions on column aliases, make sure the script expects the right name.

Related Resources

These alternative scripts show how to parse Excel files directly.

  • pyScriptExcelDebug.py - A script that works on the original Excel file. It writes to a 'scrap' file as a data staging and debugging mechanism.
  • pyScriptExcel.py - Another script that works on the original Excel file.

Related Topics

Was this content helpful?

Log in or register an account to provide feedback


previousnext
 
expand allcollapse all