Skip to content

Instantly share code, notes, and snippets.

@eweitz
Last active February 1, 2019 15:03
Show Gist options
  • Save eweitz/8b7b240b57e282e7c354eb37abecb938 to your computer and use it in GitHub Desktop.
Save eweitz/8b7b240b57e282e7c354eb37abecb938 to your computer and use it in GitHub Desktop.
Extract study metadata using Python bindings for Single Cell Portal REST API
"""Extract public study metadata using Python bindings for SCP REST API
See https://github.com/broadinstitute/single_cell_portal/issues/52 for context and usage notes.
"""
import argparse
import json
import pprint
import scp_api
def print_summaries(studies, only_public=True):
for study in studies:
if only_public and study['public'] is False:
continue
print('Name: ' + study['name'])
print('Date created: ' + study['created_at'])
print('Date updated: ' + study['updated_at'])
print('Cell count: ' + str(study['cell_count']))
print('Gene count: ' + str(study['gene_count']))
print('')
args = argparse.ArgumentParser(
prog='extract_public_study_metadata.py',
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
args.add_argument(
'--token', dest='token', default=None,
help='Personal token after logging into Google (Oauth2). This token is not persisted after the finish of the script.'
)
parsed_args = args.parse_args()
manager = scp_api.SCPAPIManager()
manager.login(token=parsed_args.token)
api_base_endpoint = manager.api
studies = manager.do_get(api_base_endpoint + 'studies')['response'].json()
# Debug
# pprint.pprint(studies)
print_summaries(studies)
print('Number of accessible studies: ' + str(len(studies)))
public_studies = [study for study in studies if study['public'] is True]
print('Number of public studies: ' + str(len(public_studies)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment