opencga web服务的rest客户机
pyCGA的Python项目详细描述
PyCGA
- 这个python包使用了为OpenCGA数据库实现的详尽的restful web服务api。
- 它提供了对opencga的简单访问,opencga是一个开源项目,旨在提供一个大数据存储引擎和分析框架,用于数百万亿甚至千兆字节的基因组级数据分析。
- 有关此项目的详细信息,请参见OpenCGA Wiki
Installation
克隆
通过在终端中执行,可以在本地计算机中克隆pycga:
$ git clone https://github.com/opencb/opencga.git
下载项目后,您可以安装库:
$ cd opencga/tree/develop/opencga-client/src/main/python
$ python setup.py install
Usage
Getting started
第一步是设置opencga服务器配置:
>>>configuration={"version":"v1","rest":{"hosts":["http://100.15.26.35:8080/opencga"]}}
配置也可以存储在json或yml文件中:
>>>configuration='/path/to/config/opencga_configuration.json'
第二步是导入模块并初始化opencgaclient。必须指定配置、用户和密码:
>>>frompyCGA.opencgarestclientsimportOpenCGAClient>>>oc=OpenCGAClient(configuration=configuration,user='user_example',pwd='pass_example')
如果不希望在脚本中写入用户和密码,则可以使用会话ID:
>>>frompyCGA.opencgarestclientsimportOpenCGAClient>>>oc=OpenCGAClient(configuration=configuration,user='user_example',pwd='pass_example')# Remove after getting session id>>>printoc.session_id# Remove after getting session id"I4MG3fXJIZARl1LhwZ">>>oc=OpenCGAClient(configuration=configuration,session_id='I4MG3fXJIZARl1LhwZ')
下一步是为我们要查询的数据创建特定的客户端:
>>>samples=oc.samples()# Query for samples>>>files=oc.files()# Query for files>>>cohorts=oc.cohorts()# Query for cohorts
现在您可以通过提供一个查询id来请求opencga restful服务:
>>>sample_search=samples.search(study='study1',name='sample1').get()>>>printsample_search"[{'acl': [{'member': '@gel', u'permissions': ['VIEW', 'VIEW_ANNOTATIONS']}..."
响应被检索为json格式的数据。因此,可以按以下键查询字段:
>>>creation_date=oc.samples.search(study='study1',name='sample1').get()[0]['creationDate']"20170204822738"
json输出中的第一级可以作为属性来访问:
>>>creation_date=samples.search(study='study1',name='sample1').get().creationDate"20170204122738">>>annotation=cohorts.search(study='study1',name='cohort1').get().annotationSets>>>printannotation[0]['annotations'][0]['value']['sex']"F"
某些字段允许使用regex。这在按名称搜索时特别有用:
>>>cohort_name=cohorts.search(study=study_id,name='~LP3000506-DNA_J01').get().name>>>printcohort_name"LP3000506-DNA_J01_LP3000924-DNA_Z02_0"
可以通过指定逗号分隔的ID或ID列表来访问数据:
>>>creation_date=oc.samples.search(study='study1',name='sample1').get()[0]['creationDate']"20170204822738">>>creation_date=oc.samples.search(study='study1',name='sample1').get()[1]['creationDate']"20170204822738">>>creation_date=samples.search(study='study1',name='sample1,sample2').get().creationDate["20170204122738","20170204123049"]
可选的过滤器和额外的选项可以添加为键值参数(值可以是逗号分隔的字符串或列表):
>>># e.g. "exclude" parameter>>>attributes=oc.files.search(study='study1',name='~sample',bioformat='VARIANT',status='READY',exclude='attributes').get().attributes>>>printattributes[{},{},{},{},{},{},{},{}]>>># e.g. "limit" parameter>>>files=oc.files.search(study='study1',name='~sample',bioformat='VARIANT',status='READY',limit=1).get()>>>printlen(files)1
特别提到“analysis_variant”端点,它返回一个迭代器:
>>>variant_iterator=oc.analysis_variant.query(pag_size=100,data={'studies':'study1','gene':'BRCA2'},limit=1)>>>forvariantinvar_iterator:>>>printv.get().type"SNV"
What can I ask for?
了解每个客户机可以检索哪些数据的最佳方法是查看opencga wiki的RESTful web services部分或OpenCGA web services