Skip to content

Instantly share code, notes, and snippets.

@sminot
Created May 9, 2016 23:07
Show Gist options
  • Save sminot/6632e8a2f30fa59b94d2b7c0a2f217e4 to your computer and use it in GitHub Desktop.
Save sminot/6632e8a2f30fa59b94d2b7c0a2f217e4 to your computer and use it in GitHub Desktop.
Download results from One Codex and combine into a single table (python) (note: requires pandas)
#!/usr/bin/python
"""Download results from One Codex and combine into a single table (note: requires pandas)."""
import requests
import os
import pandas as pd
# Expects the API key for One Codex to be stored in the ONE_CODEX_API_KEY environment variable
api_key = os.environ['ONE_CODEX_API_KEY']
# Set authentication for requests
auth = (api_key, '')
# Get the complete set of analyses
analyses = requests.get('https://app.onecodex.com/api/v0/analyses', auth=auth)
analyses = analyses.json()
dat = {}
for a in analyses:
# Only keep completed analyses with the One Codex Database
if a['reference_name'] == 'One Codex Database' and a['analysis_status'] == 'Success':
url = 'https://app.onecodex.com/api/v0/analyses/%s/extended_table' % (a['id'])
results = requests.get(url, auth=auth)
results = results.json()
# Convert to a Pandas DataFrame
results = pd.DataFrame(results)
# Index data by name, rank, and tax_id
results.set_index(['name', 'rank', 'tax_id'], inplace=True)
# Store the `readcount_w_children` variable, keyed by sample_filename and sample_id
dat[(a['sample_filename'], a['sample_id'])] = results['readcount_w_children']
# Format the complete result table (replacing NaN's with 0 values)
dat = pd.DataFrame(dat).fillna(0)
# Write out the result table to a file
dat.to_csv('results_table.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment