用于包装和处理BioStudies REST API调用的Python客户端库
项目描述
BioStudies-client - Python库
此库旨在提供一个Python客户端来访问/包装BioStudies REST API。
客户端正在开发中,因此任何贡献都受到鼓励和欢迎。
请,从最新主分支创建一个分支,
进行修改并针对最新主分支创建一个Pull Request。
我们将对其进行审查,并在仔细考虑后
我们可能会将其合并到主分支。
概述
此API客户端可用于以下操作
- 使用提供的凭据对BioStudies REST API进行身份验证,
- 在用户的根文件夹下创建文件夹和文件夹结构,
- 将文件上传到用户的根文件夹
或用户指定的文件夹(如果用户已提供文件夹参数), - 从给定文件夹获取用户的文件/文件夹列表,
- 删除用户的文件/文件夹,
- 将提交发送到BioStudies存档,包含或不包含属于它的数据文件,
- 查询BioStudies存档中的现有提交,
- 从BioStudies存档中删除现有提交。
认证/登录是用户获取会话ID的强制第一步
以进行后续请求。
只需要做一次,然后用户就可以重复使用该会话ID。
如果用户想上传属于提交的文件,
那么上传应该先进行,然后才能提交提交。
如果用户以相反的顺序执行这些步骤,则提交将失败。
先决条件
- Python3应安装在你的环境中。
安装
pip install biostudies-client
配置
在使用库之前,需要设置一些配置值。如果不以某种方式配置该库,则库将无法正常运行。
目前有2种方法可以配置库。
使用参数配置
这是配置库的推荐方法。
- 使用BioStudies REST API的基础URL实例化Auth类
auth = Auth('http://example.url.to.biostudies/rest/api')
- 将用户的凭据传递给登录方法。
auth.login('username', 'password')
使用环境参数配置
您还可以通过使用环境变量来配置库。您需要设置以下环境变量
环境变量 | 代码示例 |
---|---|
BioStudies REST API的URL | BIOSTUDIES_API_URL=http://biostudies.url |
BioStudies REST API的用户名 | BIOSTUDIES_USERNAME=biostudies_username |
BioStudies REST API的用户名 | BIOSTUDIES_PASSWORD=biostudies_password |
在这种情况下,您不需要向Auth类及其login方法传递任何参数。您可以像这样登录
auth = Auth()
auth.login()
运行集成测试
-
需要从BioStudies团队获取用户凭据(用户名和密码)以及BioStudies REST API的URL biostudies@ebi.ac.uk。
-
使用环境变量进行配置。您可以在以下位置找到如何操作:使用环境参数配置
-
使用您的EBI凭据登录EBI VPN,以便能够访问BioStudies DEV或TEST REST API。
-
执行以下语句
python3 -m unittest discover -s tests/integration
示例用法
注意:
- 如果您针对BioStudies TEST环境执行上述示例,那么您需要登录到EBI VPN。
- 所有上述代码示例都使用环境变量对BioStudies REST API进行认证。如果您想传递认证凭据,请检查本节中如何操作:使用参数配置
在认证后登录到BioStudies REST API并获取后续请求中使用的会话ID
from biostudiesclient.auth import Auth
auth = Auth()
response = auth.login()
# Get the session ID from the response object
session_id = response.session_id
assert session_id
print(session_id)
在认证后,将文件上传到BioStudies存档中的用户根文件夹
from biostudiesclient.api import Api
auth = Auth()
auth.login()
api = Api(auth)
file_path = "path/to/test_file.txt"
# Upload the given file
api.upload_file(file_path)
在认证后,在BioStudies服务器中的用户根文件夹中创建一个文件夹
from biostudiesclient.api import Api
auth = Auth()
auth.login()
api = Api(auth)
folder_name = "test_folder"
# Create the given folder for the user
api.create_user_sub_folder(folder_name)
在认证后,将带有元数据和文件的提交提交到BioStudies存档
from biostudiesclient.api import Api
auth = Auth()
auth.login()
api = Api(auth)
# Upload the given file
file_path = "path/to/test_file.txt"
api.upload_file(file_path)
metadata = {
"attachTo": "Phoenix Project",
"attributes": [
{
"name": "Title",
"value": "phoenix submission example"
},
{
"name": "Description",
"value": "This is the description of a test phoenix submission."
}
],
"section": {
"accno": "Project",
"type": "Study",
"attributes": [
{
"name": "Title",
"value": "Cells of the adult human heart"
},
{
"name": "Description",
"value": "Cardiovascular disease is the leading cause of death worldwide."
},
{
"name": "Organism",
"value": "Homo sapiens (human)"
},
{
"name": "alias",
"value": "Phoenix-test-1"
}
],
"files": [
{
"path": "test_file.txt",
"attributes": [
{
"name": "Description",
"value": "Raw Data File"
}
],
"type": "file"
}
],
"links": [
{
"url": "ABC123",
"attributes":[
{
"name": "type",
"value": "gen"
}
]
},
{
"url": "SAMEA7249626",
"attributes": [
{
"name": "Type",
"value": "BioSample"
}
]
}
],
"subsections": [
{
"type": "Author",
"attributes": [
{
"name": "Name",
"value": "John Doe"
}
]
}
]
}
}
# submit the submission
response = api.create_submission(metadata)
assert response.json
assert response.json['accno']
# print the accession ID of the submission
print(response.json['accno'])
开发者说明
发布到PyPI
-
通过注册页面创建PyPI账户。
请注意,PyPI在发布之前需要验证电子邮件地址。
-
添加包含项目名称和版本的
setup.py
配置文件。 -
打包项目以进行分发。
python setup.py sdist
请注意,
setup.py
已配置为构建名称为biostudies-client
的发行版。目前,此PyPI项目为私有所有,可能需要更改访问权限。或者,可以更改setup.py
中的项目名称,以便可以构建并上传到不同的PyPI条目。 -
安装Twine
pip install twine
-
将发行包上传到PyPI。
twine upload dist/*
运行
python setup.py sdist
将在项目基本目录的dist
目录中创建一个包。如果需要,可以选择特定的包而不是通配符*
。twine upload dist/biostudies-client-0.1.0.tar
项目详情
下载文件
下载适用于您平台的文件。如果您不确定该选择哪个,请了解有关 安装包 的更多信息。