Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Python client pyCGA

pyCGA is a client for OpenCGA Web Services, all the webservices are accesible through this client, and it offers a quick way to implement to query OpenCGA projects from custom scripts. In the same way than in  RESTful Web Services documentation, we will focus on those end points interesting for HGVA users. In order to make it easy to follow we will use the same examples used in RESTful Web Services.

Setting Up pyCGA for HGVS


Code Block
languagebash
themeMidnight
titleHow To Install pyCGA
cd 'path/to/pyCGA folder/'
[ sudo] pip install .  [ --upgrade ]

Configuration parameters can be passed as a JSON file, YAML file or a Python Dictionary:


Code Block
languagetext
themeConfluence
titleConfiguration File - JSON format
{
    "version": "v1",
    "rest": {
        "hosts": [
            "bioinfodev.hpc.cam.ac.uk/hgva-1.0"
        ]
    }
}


Code Block
languagetext
themeConfluence
titleConfiguration File - YAML format
---
version: v1
rest:
  hosts:
  - 10.5.5.4:8080/opencga-test


Code Block
languagepy
themeRDark
titleConfiguration Dictionary Python
configuration = {
    'version': 'v1',
    'rest': {
        'hosts': [
            '10.5.5.4:8080/opencga-test'
        ]
    }
}

Load the configuration will be the first step, to use the python client. We will use the ConfigClient class, passing the name of the path of the configuration file or the dictionary with the configuration. After that the instance created will be passed to the Client.

Code Block
languagepy
themeRDark
titleLoading the configuration
linenumberstrue
from pyCGA.opencgaconfig import ConfigClient
from pyCGA.opencgarestclients import Studies, AnalysisVariant, Projects, Samples, Cohorts



# configuration = '/path/to/configuration_file.json'
# configuration = '/path/to/configuration_file.yaml'
configuration = {
    'version': 'v1',
    'rest': {
        'hosts': [
            '10.5.5.4:8080/opencga-test'
        ]
    }
}

# Initialise configuration
conf = ConfigClient(configuration)


# conf instance should be passed to the clients
study_client = Studies(conf)

Getting information about genomic variants

Code Block
languagepy
themeRDark
firstline23
titleGetting information about genomic variants
linenumberstrue
analysis_variant_client = AnalysisVariant(conf)


# Get TTN variants from the Genome of the Netherlands study, which is framed within the reference_grch37 project ('limit=3' limit the number of results to 3) 
responses = analysis_variant_client.query(gene='TTN', studies='reference_grch37:GONL', limit=3)

# If the response status is 200 (OK), the response will be a dictionary with the responses, this dictionary is equivalent to the json response obtained through the Web Services.  
for response in responses:
	for result in response['result']
		print(result)

Getting information about projects

Code Block
languagepy
themeRDark
firstline33
titleGetting information about genomic variants
linenumberstrue
project_client = Projects(conf)


# Getting all metadata for the reference_grch37 project
responses = project_client.info('reference_grch37'


# Getting all studies and their metadata for the cancer_grch37 project
responses = project_client.studies('cancer_grch37')

Getting information about studies


Code Block
languagepy
themeRDark
firstline42
titleGetting information about genomic variants
linenumberstrue
study_client = Studies(conf)


# Getting all metadata for all available studies
responses = study_client.search()


#  Getting summary data for study 1kG_phase3 which is framed within project reference_grch37
responses = study_client.summary('reference_grch37:1kG_phase3')


# Getting all metadata for study GONL  which is framed within the project reference_grch37
responses = study_client.info('reference_grch37:GONL')


# Getting all samples metadata for study 1kG_phase3 which is framed within project reference_grch3
responses = study_client.samples('reference_grch37:1kG_phase3')

Getting information about samples

Code Block
languagepy
themeRDark
firstline59
titleGetting information about genomic variants
linenumberstrue
sample_client = Samples(conf)


# Get all metadata for sample HG00096 of the 1kG_phase3 study which is framed within the reference_grch37 project
responses = sample_client.info('HG00096', study='reference_grch37:GONL')

Getting information about cohorts

Code Block
languagepy
themeRDark
firstline64
titleGetting information about genomic variants
linenumberstrue
cohort_client = Cohorts(conf)


# Get all samples metadata for cohort GBR from study 1kG_phase3 which is framed within project reference_grch37
responses = cohort_client.samples('GBR', study='reference_grch37:1kG_phase3')


Table of Contents:

Table of Contents