import requests import json # The 'fields' parameter is passed as a comma-separated string of single names. fields = [ "file_name", "cases.submitter_id", "cases.samples.sample_type", "cases.disease_type", "cases.project.project_id" ] fields = ",".join(fields) files_endpt = "https://api.gdc.cancer.gov/files" # This set of filters is nested under an 'and' operator. filters = { "op": "and", "content":[ { "op": "in", "content":{ "field": "cases.project.primary_site", "value": ["Lung"] } }, { "op": "in", "content":{ "field": "files.experimental_strategy", "value": ["RNA-Seq"] } }, { "op": "in", "content":{ "field": "files.data_format", "value": ["BAM"] } } ] } # A POST is used, so the filter parameters can be passed directly as a Dict object. params = { "filters": filters, "fields": fields, "format": "TSV", "size": "2000" } # The parameters are passed to 'json' rather than 'params' in this case response = requests.post(files_endpt, headers = {"Content-Type": "application/json"}, json = params) # OUTPUT METHOD 1: Write to a file. file = open("complex_filters.tsv", "w") file.write(response.text) file.close() # OUTPUT METHOD 2: View on screen. print(response.content.decode("utf-8"))