1
$\begingroup$

I am trying to query metadata from the Genomic Data Commons (GDC) Data Portal and used the sample code from its website:

import requests
import json

cases_endpt = 'https://api.gdc.cancer.gov/cases'

# The 'fields' parameter is passed as a comma-separated string of single names.
fields = ["submitter_id",
          "case_id",
          "primary_site",
          "disease_type",
          "diagnoses.vital_status"
         ]

fields = ','.join(fields)

params = {"fields": fields,
          "format": "TSV",
          "size": "100"
         }

response = requests.get(cases_endpt, params = params)

print(response.content)

However, I am not getting a tsv file in return. I am ultimately getting an FPKM instead. Any help is appreciated!

$\endgroup$

1 Answer 1

1
$\begingroup$

You can use str() to convert bytes to strings and print() with the proper separator.

import requests
from io import StringIO
import pandas as pd

cases_endpt = 'https://api.gdc.cancer.gov/cases'

fields = ["submitter_id",
          "case_id",
          "primary_site",
          "disease_type",
          "diagnoses.vital_status"
          ]

fields = ','.join(fields)

params = {"fields": fields,
          "format": "TSV",
          "size": "100"
          }

response = requests.get(cases_endpt, params=params)

content = str(response.content, 'utf-8')

print(content, sep='\t', end='\r')

If you want to convert to a data frame you can use StringIO() to load a data frame from an input string directly to a pandas data frame.

data = StringIO(content)
df = pd.read_csv(data, sep='\t')

The output:

case_id disease_type id primary_site submitter_id
0 375436b3-66ac-4d5e-b495-18a96d812a69 Adenomas and Adenocarcinomas 375436b3-66ac-4d5e-b495-18a96d812a69 Rectum TCGA-F5-6810
1 38f93ca8-4971-4932-b592-0a17eaece2ad Ductal and Lobular Neoplasms 38f93ca8-4971-4932-b592-0a17eaece2ad Pancreas C3L-02115
2 5ea1361a-050d-4fa0-be59-b530c456b6e3 Ductal and Lobular Neoplasms 5ea1361a-050d-4fa0-be59-b530c456b6e3 Pancreas C3L-00928
3 690378f3-e38a-4b25-8489-05a3b4bd40b6 Adenomas and Adenocarcinomas 690378f3-e38a-4b25-8489-05a3b4bd40b6 Kidney C3N-01175
4 6b4f086e-4ef9-4f30-874e-8a155b2e3bab Squamous Cell Neoplasms 6b4f086e-4ef9-4f30-874e-8a155b2e3bab Other and ill-defined sites C3N-04276
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.