Extending LabKey Server Part 1:
Retrieving and Presenting Data
Adam Rauch
Partner, LabKey Software [email protected]
Extending LabKey Server
LabKey Server is a large system that combines an extensive set
of tools and applications for managing biomedical research data
But the single most powerful feature is the ability to extend and
customize LabKey Server to meet your specific needs
LabKey Server is not a product with a fixed set of functionality….
it’s a platform that you (and we) can extend in many different ways via code and customization
A Few Examples of Extending LabKey Server
Customize the look and feel, navigation model, and data
presentation of your projects, studies, and assays
Assemble LabKey and external components into mash-ups,
dashboards, and other custom views
Encapsulate your “process logic” in custom data entry
applications and workflows
Augment existing features with processing steps,
transformations & analyses matching your science
In two presentations I’ll cover six key extension mechanisms: Custom queries
External schemas / data sources Script reports
Server API / client libraries File-based modules
Java modules
Retrieve & present data
Create applications Extension Mechanisms Easy Hard Limited Powerful Primary Use
Security and Permissions
All extension mechanisms respect access privileges
Custom code typically “impersonates” the requesting user
Your code can’t subvert permissions (e.g., retrieve forbidden data) It’s critical that you test your code/queries as a non-admin user
Caveats & warnings
Exception: Java code in a module can subvert security; be careful! Custom R code running on your web server can do nasty things
Custom JavaScript running in your users’ browsers can do nasty things You damn well better trust people who add code to your server!
Special Developers group determines who can write code
Site administrators are added automatically Other users must be added explicitly
Key extension mechanisms: Custom queries
External schemas / data sources Script reports
Server API / client libraries File-based modules
Java modules
Retrieve & present data
Create applications
LabKey Query Terminology
Query: a named, saved SQL SELECT statement
Like a VIEW in a SQL database
LabKey translates SQL to underlying implementation & enforces security
Schema: a named collection of related tables and queries Table/query metadata:
Column names, types, formats, etc.
Custom view
Subset of query functionality (column list, sort, filter) Intended for UI definition (not defined in SQL)
Custom Queries
Use the full power of SQL to do custom analysis of your data Allow joins, grouping, aggregates, complex filters, pivots,
functions, expression columns, etc.
Access tables & queries exposed by modules and external
schemas
Respect the executing user’s permissions
Use meta data to configure display properties such as
lookups, labels, URLs, standard and conditional formats, etc.
Custom Queries: Tips & Tricks
SQL is a good tool for narrowing down the data you really
want to see or preparing data for other analysis
The query schema browser is your friend
Use it to explore the data sources, schemas and queries (built-in and user-defined) available in a folder
Documentation on labkey.org
LabKey SQL reference Meta data XML reference
External Schemas & Data Sources
Provide access to data that is not managed by LabKey Scenarios
Use LabKey to publish, report & visualize any data in your institution Build LabKey applications without relocating the data
Join LabKey data with user-defined tables (same database) Publish SAS data sets securely and dynamically via LabKey
External Schemas & Data Sources
Currently support:
Microsoft SQL Server (2005 & 2008) PostgreSQL (8.3 – 9.1)
MySQL (5.1 & 5.5) SAS (9.1 & 9.2) Oracle (11g)
Steps
1. Add DataSource <Resource> elements to labkey.xml
2. Expose external schemas from these data sources (per-folder) Tables and views are then visible like any other query
DataSource <Resource> Example
Example <Resource> element that enables a MySQL data source. LabKey documentation includes examples for all supported databases.
<Resource name="jdbc/mySqlDataSource" auth="Container" type="javax.sql.DataSource" username="root" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/?autoReconnect=true &useUnicode=true&characterEncoding=utf8" maxActive="15" maxIdle="7" useInformationSchema="true" accessToUnderlyingConnectionAllowed="true"
Script Reports
Reports (“views”) provide alternate ways of displaying data,
beyond a simple grid
Time chart and crosstab are two built-in examples
Use script reports to create custom transformations &
visualizations of datasets and queries Write script reports in R or JavaScript
Once created, users can invoke the custom report
LabKey executes the code, providing query data & meta data Artifacts created by the code are then sent to the browser
Script Reports: Tips & Tricks
Configure external script engines from the admin console Script reports are created with functional placeholder code The “help” tab in the script report designer is… helpful