Skip to content

Instantly share code, notes, and snippets.

@boydgreenfield
Last active February 23, 2017 22:21
Show Gist options
  • Save boydgreenfield/471bb4fab23f2f71fe6ad8b05acad416 to your computer and use it in GitHub Desktop.
Save boydgreenfield/471bb4fab23f2f71fe6ad8b05acad416 to your computer and use it in GitHub Desktop.
Download samples based on their ID from One Codex
from __future__ import print_function
import argparse
import onecodex
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=("Download a set of FASTA/Q files stored in One Codex with their sample IDs."))
parser.add_argument('samples', metavar='sample ID', nargs='+',
help='One Codex sample IDs to download')
parser.add_argument('--api-key', default=None,
help='One Codex API key (optional if a ~/.onecodex file exists)')
args = parser.parse_args()
# Automatically fetch API key from ~/.onecodex file
ocx = onecodex.Api(api_key=args.api_key)
# Check that the verison is OK (> 0.2.0)
assert tuple(int(x) for x in onecodex.version.__version__.split('.')) > (0, 2, 0)
for ix, uuid in enumerate(args.samples):
sample = ocx.Samples.get(uuid)
if sample is None:
raise Exception('Could not fetch sample {}. Double check that the Sample ID '
'is correct and that you are logged in with '
'`onecodex login`.'.format(uuid))
print('Downloading {} (sample {}/{})...'.format(sample.filename, ix + 1, len(args.samples)))
sample.download()
print('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment