Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 17 Next »


verview

OpenCGA implements a Python client library called PyOpenCGA to perform any operation through the REST web services API. PyOpenCGA provides programmatic access to all the implemented REST webservices, providing and easy, lightweight, fast and intuitive solution to access OpenCGA data. The library offers the convenience of an object-oriented scripting language and provides the ability to integrate the obtained results into other Python applications.

Some of the main features include:

  • full RESTful web service API implemented, all endpoints are supported including new alignment or clinical functionality.
  • data is returned in a new RestResponse object which contains metadata and the results, some handy methods and iterators implemented.
  • it uses the OpenCGA client-configuration.yml file.
  • several Jupyter Notebooks implemented.

PyOpenCGA has been implemented and contributed by Pablo Marin and David Gomez and it is based on the previous pyCGA library implemented by Antonio Rueda and Daniel Perez from Genomics England. The code is open-source and can be found at https://github.com/opencb/opencga/tree/develop/opencga-client/src/main/python/pyOpenCGA. It can be easily installed using PyPI. Please, find more details on how to use the python library at Using the Python client.

Installation

Python client requires at least Python 3.x, although most of the code is fully compatible with Python 2.7. You can install PyOpenCGA either from PyPI repository or from the source code.

PyPI

PyOpenCGA client is deployed at PyPI and it is available at  https://pypi.org/project/pyopencga/. Installing it is as simple as running the following command line:

## Latest stable version
pip install pyopencga

Source Code

Python client source code can be found in OpenCGA GitHub repository at https://github.com/opencb/opencga/tree/develop/opencga-client/src/main/python/pyOpenCGA. To install any stable of development version of pyOpenCGA we will first need to clone the right branch of OpenCGA repository and install the library using the setup.py file.

## Latest stable version
git clone -b master https://github.com/opencb/opencga.git

## Move to the pyOpenCGA client folder
cd opencga/opencga-client/src/main/python/pyOpenCGA/

## Install the library
python setup.py install

Library Implementation

Developers only need to create an instance of the ClientConfiguration class passing it as an argument to the main OpenCGAClient class. They can optionally pass a valid token to start doing calls as an authenticated user. 

## Import OpenCGAClient and ClientConfiguration classes
from pyopencga.opencga_client import OpenCGAClient
from pyopencga.opencga_config import ClientConfiguration

## Creating a ClientConfiguration: 
# This can be done by passing the path to the main client-configuration.yml file
config = ClientConfiguration('/opt/opencga/conf/client-configuration.yml')
# Or by creating a dictionary using the below format passing the OpenCGA host to point to
config = ClientConfiguration({
    "rest": {
            "host": "http://bioinfo.hpc.cam.ac.uk/opencga-demo"
    }
})

## Create an instance of OpenCGAClient passing the configuration
oc = OpenCGAClient(config)

## Authenticate the user. Password is optional and if this is not passed to the login method, it will be prompted to the user
oc.login('user')
# or
oc.login('user', 'password')

Design

OpenCGAClient class works as a factory containing all the different clients, one per resource, that are necessary to call to any REST web service.

## Main clients
user_client = oc.users
project_client = oc.projects
study_client = oc.studies
file_client = oc.files
sample_client = oc.samples
cohort_client = oc.cohorts
individual_client = oc.individuals
family_client = oc.families
clinical_client = oc.clinical
job_client = oc.jobs
panel_client = oc.panels


## Analysis clients
variant_client = oc.variant
alignment_client = oc.alignment
ga4gh_client = oc.ga4gh


## Administrative clients
meta_client = oc.meta
admin_client = oc.admin

The list of available actions that can be performed with all those clients can be checked in Swagger as explained in RESTful Web Services#Swagger. Each particular client has a method defined for each available web service implemented for the resource. For instance, the whole list of actions available for the Sample resource are shown above.

For all those actions, there is a method available in the sample client. For instance, to search for samples using the /search web service, we will need to do something like:

## Look for the id of the first 5 samples of the study "study"
sample_result = oc.samples.search(study="study", limit=5, include="id")

As described in RESTful Web Services#RESTResponse, most of the web services return a RESTResponse object containing a list of OpenCGAResults. This structure has been maintained in the Python library and everytime a call to any WS is done, the response is automatically encapsulated into a custom RESTResponse class that automatically stores all the different values returned. For instance, the sample_result variable from the example above is a RESTResponse instance. This object defines a few public methods to help users navigating through the data.

The RESTResponse methods developed are:

## Return an iterator to help iterating over all the results.
sample_result.results()

## Return the total number of matches taking of all the QueryResponses.
sample_result.num_matches()

## Return the total number of results taking of all the QueryResponses.
sample_result.num_results()

Table of Contents:

Useful Links


  • No labels