Skip to content

Instantly share code, notes, and snippets.



Created Mar 27, 2017
What would you like to do?
Retrieve TCGA gene expression data using GDC api
# -*- coding: utf-8 -*-
import logging as log
import pandas as pd
import requests as rq
class TCGA:
def __init__(self, gdc_url='', per_page=100,
''' Initialisation '''
self.gdc_url = gdc_url
self.per_page = per_page
log.basicConfig(filename=logfile, level=log.INFO,
format='%(asctime)s : %(levelname)8s : %(message)s (%(module)s.%(funcName)s)',
datefmt='%Y-%m-%d %H:%M:%S')
def get_geneexp(self, projects, exp_strategy='RNA-Seq',
workflow='HTSeq - FPKM'):
filters = _FilterBuilder.logical(
'and', [
'Gene Expression Quantification'),
_FilterBuilder.equal('experimental_strategy', exp_strategy),
_FilterBuilder.equal('files.analysis.workflow_type', workflow),
_FilterBuilder.inclusion('cases.project.project_id', projects)])
file_ids = self._get_file_ids(filters)'{len(file_ids)} files found')
df = None
for i, fid in enumerate(file_ids):
f = pd.read_table(f'{self.gdc_url}/data/{fid}', compression='gzip',
index_col=0, header=None)
df = pd.concat([df, f], axis=1)'{i+1:4}. File {fid} integrated')
df.columns = file_ids
return df
def _get_file_ids(self, filters):
file_ids = []
resp ='{self.gdc_url}/files?size={self.per_page}',
json={'filters': filters})
if resp.status_code == 200:
resp = resp.json()
meta = resp['data']['pagination']
file_ids += [h['file_id'] for h in resp['data']['hits']]
if meta['pages'] > 1:
for _from in range(self.per_page + 1, meta['total'], self.per_page):
resp =
json={'filters': filters})
if resp.status_code == 200:
resp = resp.json()
file_ids += [h['file_id'] for h in resp['data']['hits']]
return file_ids
class _FilterBuilder:
def logical(op, args):
''' Logical operator '''
_filter = { 'op': op, 'content': [o for o in args] }
return _filter
def inclusion(field, values):
''' Inclusion operator '''
if len(values) < 1:
raise RuntimeError(f'Invalid number of values: {len(values)}')
_filter = { 'op': 'in', 'content': { 'field': field, 'value': values }}
return _filter
def equal(field, value):
''' Equal operator '''
_filter = { 'op': '=', 'content': { 'field': field, 'value': value }}
return _filter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment