Overview
[
Live Demo] [
Python API Module + Documentation]
Caution
This page contains preliminary content to guide you through setting up the Python API Tutorial on a server where you have project admin permissions.
Please note that this is an advanced tutorial. If you'd like a lighter intro to the Python API, just use the samples included in the labkeypython.py module file itself. You can use executeSql() and selectRows() on the PythonProject on LabKey Online without any further setup. The tutorial below involves more setup to allow alteration of data on a LabKey Server, not just retrieval of data, plus demo a real-world scenario.
Scenario
Your test labs have collected salmonella samples in the field. These labs sequence 7 indicator genes for each of the collected samples.
You want to use these 7 indicator genes to identify the strain type of each sample. The strain type is an identifier that lets you compare these samples with very similar samples.
Once you know a sample's strain type, you can use existing datasets to look at where this strain type has been collected before, in which species, etc.

Data used in this scenario come from the public MLST Databases at the ERI, University College Cork
How LabKey Server can be used in this scenario
LabKey Server helps you centralize data management through a web-based portal that can be used by end-labs, study managers, statisticians, PIs, etc. End labs can upload their data files, then central study managers or statisticians can import the data in these files into the LabKey database in a structured way. In this tutorial, sample information is analyzed using a python script that leverages existing sample/reference data that has already been uploaded to the system.
Data analysis steps in our python script
The python analysis script used in this tutorial does the following:
- Get the names of all your samples (runs)
- For each sample
- Get its 7 gene sequences
- For each of 7 genes
- Figure out which reference gene version # matches
- Keep a list of these versions #
- Match list of 7 gene version #s to matching strain type #
- Update sample results table:
- Sample name, strain type #, # related strains, reference strain
Tutorial
Set up portal
- Install LabKey Server or log on to your personal test project on LabKey Online. If you need a project, email eknelson@labkey.com
- Add a folder on your server (or to your project on LabKey Online)
- Select "Assay" as the folder type
- On the new portal page, add the following web parts:
Get files
- Download labkeyquery.py and labkeycredentials.txt from the Python API documentation page.
- Download tutorial files: LabKeyDemoFiles.zip
- Extract .zip
- Grab the files in the LabKeyDemoFiles///API///SalmonellaTutorial directory.
- Move all the labkeyquery.py, labkeycredentials.txt and straintypeX.py files into the same directory as your python installation.
- Start python and make sure you can see these files. Type:
import os
os.listdir(os.curdir)
- Doesn't matter where you keep the .xls and other files from the SalmonellaTutorial folder, but note the directory.
Edit the default tutorial files
- Update the straintypeX.py file with:
- Your server's URL
- The path of the assay folder you just set up. These appear only once at the beginning of the file.
- Update the labkeycredentials.txt file with:
- The machinename of your LabKey Server installation.
- The username/password you're using on this server as a project admin.
- Note that if you're running LabKey Server locally, the URL and machinename of your server may look something like this:
http://localhost:8080/labkey
Set up lists
Upload files to server
- Upload sample files to the files web part
Import 1 file to create an assay design
- In the files web part, select one file and click Import File
- Enter "Salmonella Sequence Assay" as the assay design name
- Yes to everything else
Import all your other data files to this assay design
- Select all of the files you haven't used yet in the files web part.
- Click Import Files
- Select the radio button for the "Salmonella Sequence Assay" design
- At the bottom of each of the following pages, Click "Save and Import Next File"
- On the last page, click "Save and Finish"
Run the script
- Go back to where you have Python running and type:
import straintypeX.py (Note: Replace X with the current version number of this file)
straintypeX.analyze()
You should see results scroll onto the screen.
Explore results
- Go to the portal page of the folder you've set up.
- In the list web part, click the "Sample Results" list.
- All the data you see in this list was put there by the script you just ran.
- Create custom views (draw in related tables), R views (e.g., a histogram), etc using your results.
Notes for Assay version of this tutorial
This section is still under construction. Information here is just in draft form - notes, really.
Do all of the steps listed in the previous section through "Upload files to server." Now, instead of following the next steps, use these:
Alternative 1: Set up assay design using shortcut
Import xar to define assay
Alternative 2: Set up assay design and import first set if data:
In the files web part, select a data file and choose to create a new GPAT-type assay design
Enter a name for the assay design
Open the assay designer
In the Assay Properties section, check the box next to “Editable Runs” and “Editable Results”
- Delete all batch properties
- ParticipantVisitResolver
- TargetStudy
- Add run properties:
- Name: StrainType
- Type: lists.Strain Type Genes
- Name: RelatedStrainTypes
- Type: Integer
- URL: https://hosted.labkey.com/list/PythonProject/SalmonellaV1/grid.view?listId=352&query.STRAINTYPE~eq=${STRAINTYPE}
- Name: Reference Strain
- Type: lists.Strain Metadata (String)
- For data fields leave most of the fields, but delete a few defaults:
- ParticipantID
- VisitID
- Data
Save assay design and finish uploading your assay run data