File (Revision) upload example

To run this example, you'll need the Ovation Python API. Install with pip:

pip install ovation

In [1]:
import ovation.core as core

from ovation.session import connect
from ovation.upload import upload_revision, upload_file, upload_folder
from ovation.download import download_revision

from pprint import pprint
from getpass import getpass

from tqdm import tqdm_notebook as tqdm

Connection

You use a connection.Session to interact with the Ovaiton REST API. Use the connect method to create an authenticated Session.


In [2]:
session = connect(input('Ovation email: '), org=input('Organization (enter for default): ') or 0)


Ovation email: barry@ovation.io

Upload a file (revision)

The Python API wraps the Ovation REST API, using the awesome requests library. The Session provides some convenient additions to make working with Ovation's API a little easier. For example, it automatically sets the content type to JSON and handles URL creation from path and host.

The example below shows retrieving a project by ID, adding a new File and uploading a new Revision (a version) of that file using the ovation.revisions.upload_revision convenience method.


In [ ]:
project_id = input('Project UUID: ')

In [ ]:
# Get a project by ID
proj = session.get(session.path('project', project_id))

You can upload an entire folder or individual files to the Project. First, let's upload a folder:


In [ ]:
folder = upload_folder(session, proj, '/path/to/project_fastq_folder')

Alternatively, we can create a folder and upload individual files:


In [ ]:
import os

folder = core.create_folder(session, proj, 'FASTQ')
for f in os.glob('/path/to/project_fastq_folder/*.fastq')

For advanced users, we can create a File and then upload a Revision.


In [ ]:
# Create a new File
r = session.post(project_url, 
                 data={'entities': [{'type': 'File',
                                     'attributes': {'name': 'example.vcf'}}]})
file = r[0]
pprint(file)

In [ ]:
# Create a new Revision (version) of the new File by uploading a local file
revision = upload_revision(session, file, '/Users/barry/Desktop/example.vcf')
pprint(revision)

upload_revision is also how you can upload a new Revision to an existing file.

Download a revision

The Ovation API generates a temporary authenticated URL for downloading a Revision. This example uses the ovation.revisions.download_revision function to get this authenticated URL and then to download it to the local file system, returning the downloaded file's path:


In [ ]:
file_path = download_revision(session, revision._id)