Answers.Body: <p>You can use <code>str()</code> to convert bytes to strings and <code>print()</code> with the proper separator.</p>
  <pre><code>import requests
  from io import StringIO
  import pandas as pd
  
  cases_endpt = 'https://api.gdc.cancer.gov/cases'
  
  fields = [&quot;submitter_id&quot;,
            &quot;case_id&quot;,
            &quot;primary_site&quot;,
            &quot;disease_type&quot;,
            &quot;diagnoses.vital_status&quot;
            ]
  
  fields = ','.join(fields)
  
  params = {&quot;fields&quot;: fields,
            &quot;format&quot;: &quot;TSV&quot;,
            &quot;size&quot;: &quot;100&quot;
            }
  
  response = requests.get(cases_endpt, params=params)
  
  content = str(response.content, 'utf-8')
  
  print(content, sep='\t', end='\r')
  </code></pre>
  <p>If you want to convert to a data frame you can use <code>StringIO()</code> to load a data frame from an input string directly to a pandas data frame.</p>
  <pre><code>data = StringIO(content)
  df = pd.read_csv(data, sep='\t')
  </code></pre>
  <p>The output:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: right;"></th>
  <th style="text-align: left;">case_id</th>
  <th style="text-align: left;">disease_type</th>
  <th style="text-align: left;">id</th>
  <th style="text-align: left;">primary_site</th>
  <th style="text-align: left;">submitter_id</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: right;">0</td>
  <td style="text-align: left;">375436b3-66ac-4d5e-b495-18a96d812a69</td>
  <td style="text-align: left;">Adenomas and Adenocarcinomas</td>
  <td style="text-align: left;">375436b3-66ac-4d5e-b495-18a96d812a69</td>
  <td style="text-align: left;">Rectum</td>
  <td style="text-align: left;">TCGA-F5-6810</td>
  </tr>
  <tr>
  <td style="text-align: right;">1</td>
  <td style="text-align: left;">38f93ca8-4971-4932-b592-0a17eaece2ad</td>
  <td style="text-align: left;">Ductal and Lobular Neoplasms</td>
  <td style="text-align: left;">38f93ca8-4971-4932-b592-0a17eaece2ad</td>
  <td style="text-align: left;">Pancreas</td>
  <td style="text-align: left;">C3L-02115</td>
  </tr>
  <tr>
  <td style="text-align: right;">2</td>
  <td style="text-align: left;">5ea1361a-050d-4fa0-be59-b530c456b6e3</td>
  <td style="text-align: left;">Ductal and Lobular Neoplasms</td>
  <td style="text-align: left;">5ea1361a-050d-4fa0-be59-b530c456b6e3</td>
  <td style="text-align: left;">Pancreas</td>
  <td style="text-align: left;">C3L-00928</td>
  </tr>
  <tr>
  <td style="text-align: right;">3</td>
  <td style="text-align: left;">690378f3-e38a-4b25-8489-05a3b4bd40b6</td>
  <td style="text-align: left;">Adenomas and Adenocarcinomas</td>
  <td style="text-align: left;">690378f3-e38a-4b25-8489-05a3b4bd40b6</td>
  <td style="text-align: left;">Kidney</td>
  <td style="text-align: left;">C3N-01175</td>
  </tr>
  <tr>
  <td style="text-align: right;">4</td>
  <td style="text-align: left;">6b4f086e-4ef9-4f30-874e-8a155b2e3bab</td>
  <td style="text-align: left;">Squamous Cell Neoplasms</td>
  <td style="text-align: left;">6b4f086e-4ef9-4f30-874e-8a155b2e3bab</td>
  <td style="text-align: left;">Other and ill-defined sites</td>
  <td style="text-align: left;">C3N-04276</td>
  </tr>
  </tbody>
  </table>
  </div>
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-11T09:54:14.120Z
Answers.LastActivityDate: 2021-06-11T09:54:14.120Z
Answers.UserDisplayName: zorbax
Body: <p>I am trying to query metadata from the Genomic Data Commons (GDC) Data Portal and used the sample code from its website:</p>
  <pre><code>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 = [&quot;submitter_id&quot;,
            &quot;case_id&quot;,
            &quot;primary_site&quot;,
            &quot;disease_type&quot;,
            &quot;diagnoses.vital_status&quot;
           ]
  
  fields = ','.join(fields)
  
  params = {&quot;fields&quot;: fields,
            &quot;format&quot;: &quot;TSV&quot;,
            &quot;size&quot;: &quot;100&quot;
           }
  
  response = requests.get(cases_endpt, params = params)
  
  print(response.content)
  </code></pre>
  <p>However, I am not getting a tsv file in return. I am ultimately getting an FPKM instead. Any help is appreciated!</p>
  
Tags: <python>
Title: querying metadata (GDC Data Portal) in python3
ThreadText: # querying metadata (GDC Data Portal) in python3
  
  (Asked by: username_0 on 2021-06-10)
  
  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!
  
  **Tags:** python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-11. Score: 1
  
  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 |
  
  
  ---
  
  