Skip to main content

Python client library for wrapping and handling BioStudies REST API calls

Project description

Build Status

BioStudies-client - Python library

This library aims to provide a Python client to access/wrap BioStudies REST API.

The client is under development, so any contribution encouraged and welcome.
Please, create a branch from the latest main branch,
do your modification(s) and create a Pull Request against the latest main branch.
We are going to review it and after careful consideration
we might merge it into the main branch.

Overview

This API client can be used to do the followings:

  • authenticate to BioStudies REST API with the provided credentials,
  • create folders and folder structures under the user's root folder,
  • upload a file to the user's root folder
    or a specific folder if the folder parameter has been given by the user,
  • get the list of user's files/folders from the given folder,
  • delete a user's file/folder,
  • send a submission to BioStudies archive with or without data files belong to it,
  • query an existing submission in the BioStudies archive,
  • delete an existing submission from the BioStudies archive.

Authentication/login is always a mandatory first step for the user to get the session Id
for the follow up requests.
It only need to do once, then the user can reuse that session ID.

If the user would like to upload files that belongs to a submission,
then the upload should happen first, after that the submission can be submitted.
The submission would fail if the user do these steps in the opposite order.

Prerequisites

  • Python3 should be installed in your environment.

Installation

    pip install biostudies-client  

Configuration

Before using the library some configuration values need to be set. This library won't work properly without configuring it in one way or another.

Currently there are 2 ways to configure the library.

Configure with parameters

This is the suggested way to configure the library.

  1. Instantiate the Auth class with base URL to BioStudies REST API
auth = Auth('http://example.url.to.biostudies/rest/api')  
  1. Pass the user's credentials to the login method.
auth.login('username', 'password')  

Configure with environmental parameters

You can also configure the library by using environmental variables. You need to set the:

Environmental variable Code example
URL for BioStudies REST API BIOSTUDIES_API_URL=http://biostudies.url
username for BioStudies REST API BIOSTUDIES_USERNAME=biostudies_username
username for BioStudies REST API BIOSTUDIES_PASSWORD=biostudies_password

In this case you don't have to pass any parameter to Auth class and its login method. You can login like this:

auth = Auth()
auth.login()

Running the integration tests

  1. Require user credentials (user name and password) and URL for BioStudies REST API from the BioStudies team biostudies@ebi.ac.uk.

  2. Use environmental variables for the configuration. You can find how to do it here: Configure with environmental parameters

  3. Login to EBI VPN using your EBI credentials to be able to access BioStudies DEV or TEST REST API.

  4. Execute the following statement

python3 -m unittest discover -s tests/integration

Example usage

NOTE:

  1. If you execute the above examples against BioStudies TEST environment, then you need to login into EBI VPN.
  2. All the above code examples using environmental variables for authentication to BioStudies REST API. If you would like to pass the credentials for authentication then please check how to do it in this section: Configure with parameters

Login into BioStudies REST API and get the session id using in further requests

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)  

Upload a file into user's root folder in BioStudies archive after authentication

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)  

Create a folder in user's root folder in BioStudies server after authentication

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)  

Submit a submission with metadata and file into BioStudies archive after authentication

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'])  

Developer Notes

Publish to PyPI

  1. Create PyPI Account through the registration page.

    Take note that PyPI requires email addresses to be verified before publishing.

  2. Add a setup.py configuration file containing the name and version of the project.

  3. Package the project for distribution.

     python setup.py sdist
    

    Take note that setup.py is configured to build a distribution with name biostudies-client. Currently this PyPI project is owned privately and may require access rights to change. Alternatively, the project name in setup.py can be changed so that it can be built and uploaded to a different PyPI entry.

  4. Install Twine

     pip install twine        
    
  5. Upload the distribution package to PyPI.

     twine upload dist/*
    

    Running python setup.py sdist will create a package in the dist directory of the project base directory. Specific packages can be chosen if preferred instead of the wildcard *:

     twine upload dist/biostudies-client-0.1.0.tar
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

biostudies-client-0.1.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

biostudies_client-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file biostudies-client-0.1.1.tar.gz.

File metadata

  • Download URL: biostudies-client-0.1.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.3

File hashes

Hashes for biostudies-client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 91e5e4a986d10cbfab84edd72ee3aae9f4435b0e21db9a6b02cc4e5e69c119f0
MD5 4c522460e4a3491a2bee9574e10f1257
BLAKE2b-256 4b688d0d137fc5a54c391f3a907ed3721091b7769e53805dd8758014fa07157a

See more details on using hashes here.

File details

Details for the file biostudies_client-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: biostudies_client-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.3

File hashes

Hashes for biostudies_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e1c164c8b09fc1041a9d734ebc3b2fccaa8acb15a004e263e9299e6d944ea0c
MD5 37c4021efdd930dc938b32b9209df3cb
BLAKE2b-256 20a7698ee5ff8cecd9ad1924d1edc4306462ef14a08f27102b8ef84002ac1da2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page