-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISB_bigquery_1.html
430 lines (379 loc) · 17.5 KB
/
ISB_bigquery_1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Sean Davis" />
<title>Querying TCGA data using BigQuery</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet"
href="site_libs/highlight/textmate.css"
type="text/css" />
<script src="site_libs/highlight/highlight.js"></script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">TeamCGC</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="tutorials.html">Tutorials</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Querying TCGA data using BigQuery</h1>
<h4 class="author"><em>Sean Davis</em></h4>
<h4 class="date"><em>May 5, 2016</em></h4>
</div>
<div id="what-is-bigquery" class="section level2">
<h2>What is BigQuery?</h2>
<p>BigQuery is Google’s managed SQL-like database system that is designed to run at PB scales. It allows storing, querying, and manipulating absolutely <em>huge</em> datasets with little more effort (perhaps even less) than operating on a standard relational database. Cost is a pay-as-you-go model, based on the amount of data processed.</p>
</div>
<div id="what-is-in-bigquery" class="section level2">
<h2>What is in BigQuery?</h2>
<p>The <a href="http://cgc.systemsbiology.net/">Institute for Systems Biology (ISB) Cancer Genomics Cloud</a> have loaded all of the TCGA level 3 data into BigQuery, enabling very facile slicing of data to a very fine granularity. Bulk data in file form is still likely to be the most appropriate way to do large-scale data mining and inference, but for gene- or sample-based queries or to join datasets in novel ways, BigQuery offers a possible solution.</p>
</div>
<div id="setup" class="section level1">
<h1>Setup</h1>
<pre class="r"><code>library(bigrquery)</code></pre>
<p>In BigQuery, a <code>dataset</code> is something like a <code>database</code> or a <code>schema</code> from mysql or postgresql. Listing them is straightforward using bigrquery directly.</p>
<pre class="r"><code>bigrquery::list_datasets('isb-cgc')</code></pre>
<pre><code>## [1] "ccle_201602_alpha" "genome_reference" "platform_reference"
## [4] "tcga_201510_alpha" "tcga_201607_beta" "tcga_cohorts"
## [7] "tcga_seq_metadata"</code></pre>
<p>The first time you run this, you will be likely asked to login to a google billing account. This process will involve R kicking you over to a web browser (the first time) where you will need to login to your google cloud project. You will likely see this type of message….</p>
<pre><code>Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No
Selection: Yes
Adding ^\.httr-oauth$ to .Rbuildignore
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.</code></pre>
<p>Assuming that you have set up an account with the ISB Cancer Genomics Cloud Pilot, you can authorize R to access the account, and from then on will be able to operate without browser interaction.</p>
<pre class="r"><code>bigrquery::list_tables("isb-cgc", "tcga_201607_beta")</code></pre>
<pre><code>## [1] "Annotations" "Biospecimen_data"
## [3] "Clinical_data" "Copy_Number_segments"
## [5] "DNA_Methylation_betas" "DNA_Methylation_chr1"
## [7] "DNA_Methylation_chr10" "DNA_Methylation_chr11"
## [9] "DNA_Methylation_chr12" "DNA_Methylation_chr13"
## [11] "DNA_Methylation_chr14" "DNA_Methylation_chr15"
## [13] "DNA_Methylation_chr16" "DNA_Methylation_chr17"
## [15] "DNA_Methylation_chr18" "DNA_Methylation_chr19"
## [17] "DNA_Methylation_chr2" "DNA_Methylation_chr20"
## [19] "DNA_Methylation_chr21" "DNA_Methylation_chr22"
## [21] "DNA_Methylation_chr3" "DNA_Methylation_chr4"
## [23] "DNA_Methylation_chr5" "DNA_Methylation_chr6"
## [25] "DNA_Methylation_chr7" "DNA_Methylation_chr8"
## [27] "DNA_Methylation_chr9" "DNA_Methylation_chrX"
## [29] "DNA_Methylation_chrY" "Protein_RPPA_data"
## [31] "Somatic_Mutation_calls" "mRNA_BCGSC_GA_RPKM"
## [33] "mRNA_BCGSC_HiSeq_RPKM" "mRNA_BCGSC_RPKM"
## [35] "mRNA_UNC_GA_RSEM" "mRNA_UNC_HiSeq_RSEM"
## [37] "mRNA_UNC_RSEM" "miRNA_BCGSC_GA_isoform"
## [39] "miRNA_BCGSC_GA_mirna" "miRNA_BCGSC_HiSeq_isoform"
## [41] "miRNA_BCGSC_HiSeq_mirna" "miRNA_BCGSC_isoform"
## [43] "miRNA_BCGSC_mirna" "miRNA_Expression"</code></pre>
</div>
<div id="the-dplyr-package-for-bigquery" class="section level1">
<h1>The dplyr package for bigquery</h1>
<p>The <a href="https://cran.r-project.org/web/packages/dplyr/vignettes/databases.html">dplyr database tutorial</a> discusses how to use <code>dplyr</code> with Google BigQuery. We are going to use that approach here to simplify the interaction with the database.</p>
<pre class="r"><code>library(dplyr)</code></pre>
<p>Next, we set up a connection between <code>dplyr</code> and <code>bigquery</code>.</p>
<pre class="r"><code>my_billing = "isb-cgc-01-0006" # replace billing info here with your own
bq = src_bigquery("isb-cgc", "tcga_201607_beta", billing = my_billing)</code></pre>
<p>The available tables in the <code>tcga_201510_alpha</code> database are given by simply showing the <code>bq</code> object.</p>
<pre class="r"><code>bq</code></pre>
<pre><code>## src: bigquery [isb-cgc:tcga_201607_beta]
## tbls: Annotations, Biospecimen_data, Clinical_data, Copy_Number_segments,
## DNA_Methylation_betas, DNA_Methylation_chr1, DNA_Methylation_chr10,
## DNA_Methylation_chr11, DNA_Methylation_chr12, DNA_Methylation_chr13,
## DNA_Methylation_chr14, DNA_Methylation_chr15, DNA_Methylation_chr16,
## DNA_Methylation_chr17, DNA_Methylation_chr18, DNA_Methylation_chr19,
## DNA_Methylation_chr2, DNA_Methylation_chr20, DNA_Methylation_chr21,
## DNA_Methylation_chr22, DNA_Methylation_chr3, DNA_Methylation_chr4,
## DNA_Methylation_chr5, DNA_Methylation_chr6, DNA_Methylation_chr7,
## DNA_Methylation_chr8, DNA_Methylation_chr9, DNA_Methylation_chrX,
## DNA_Methylation_chrY, miRNA_BCGSC_GA_isoform, miRNA_BCGSC_GA_mirna,
## miRNA_BCGSC_HiSeq_isoform, miRNA_BCGSC_HiSeq_mirna, miRNA_BCGSC_isoform,
## miRNA_BCGSC_mirna, miRNA_Expression, mRNA_BCGSC_GA_RPKM,
## mRNA_BCGSC_HiSeq_RPKM, mRNA_BCGSC_RPKM, mRNA_UNC_GA_RSEM,
## mRNA_UNC_HiSeq_RSEM, mRNA_UNC_RSEM, Protein_RPPA_data,
## Somatic_Mutation_calls</code></pre>
<p>To work with a table from BigQuery using <code>dplyr</code>, we need to set up a <code>dplyr</code> table connection, one for each table that we wish to use. Here, I do that with the <code>Annotations</code> table.</p>
<pre class="r"><code>tbl_annot = tbl(bq,'Annotations')
# and show the columns
colnames(tbl_annot)</code></pre>
<pre><code>## [1] "annotationId" "annotationCategoryId"
## [3] "annotationCategoryName" "annotationClassification"
## [5] "annotationNoteText" "Study"
## [7] "itemTypeName" "itemBarcode"
## [9] "AliquotBarcode" "ParticipantBarcode"
## [11] "SampleBarcode" "dateAdded"
## [13] "dateCreated" "dateEdited"</code></pre>
<p>Our <code>dplyr</code> table object behaves essentially like a data.frame when using the <code>dplyr</code> style of coding.</p>
<pre class="r"><code>head(tbl_annot)</code></pre>
<pre><code>## Source: query [?? x 14]
## Database: bigquery [isb-cgc:tcga_201607_beta]
##
## annotationId annotationCategoryId annotationCategoryName
## <int> <int> <chr>
## 1 711 1 Tumor tissue origin incorrect
## 2 713 1 Tumor tissue origin incorrect
## 3 712 1 Tumor tissue origin incorrect
## 4 2908 1 Tumor tissue origin incorrect
## 5 19730 1 Tumor tissue origin incorrect
## 6 15108 2 Tumor type incorrect
## .. ... ... ...
## Variables not shown: annotationClassification <chr>, annotationNoteText
## <chr>, Study <chr>, itemTypeName <chr>, itemBarcode <chr>,
## AliquotBarcode <chr>, ParticipantBarcode <chr>, SampleBarcode <chr>,
## dateAdded <chr>, dateCreated <chr>, dateEdited <chr>.</code></pre>
</div>
<div id="example-use-cases" class="section level1">
<h1>Example use cases</h1>
<pre class="r"><code>tbl_mut = tbl(bq,'Somatic_Mutation_calls')</code></pre>
<div id="how-many-variants-of-each-type-are-present-for-each-disease-type" class="section level2">
<h2>How many variants of each type are present for each disease type?</h2>
<pre class="r"><code>mut_counts = tbl_mut %>%
select(Study, Variant_Classification) %>%
group_by(Study,Variant_Classification) %>%
summarize(n = n()) %>%
arrange(Study,Variant_Classification) %>%
collect()</code></pre>
<pre class="r"><code>mut_counts %>% tidyr::spread(Variant_Classification,n)</code></pre>
<pre><code>## Source: local data frame [33 x 24]
## Groups: Study [33]
##
## Study 3'UTR 5'Flank 5'UTR De_novo_Start_InFrame
## <chr> <int> <int> <int> <int>
## 1 ACC 186 2 171 18
## 2 BLCA 1243 3 1536 114
## 3 BRCA 2888 3 2011 82
## 4 CESC 2246 3 1916 57
## 5 CHOL 76 NA 78 4
## 6 COAD 2566 9 1988 165
## 7 DLBC 163 NA 148 6
## 8 ESCA 2211 2 1206 40
## 9 GBM 608 1 484 54
## 10 HNSC 1057 3 1203 63
## .. ... ... ... ... ...
## Variables not shown: De_novo_Start_OutOfFrame <int>, Frame_Shift_Del
## <int>, Frame_Shift_Ins <int>, IGR <int>, In_Frame_Del <int>,
## In_Frame_Ins <int>, Intron <int>, lincRNA <int>, Missense_Mutation
## <int>, Nonsense_Mutation <int>, Nonstop_Mutation <int>, RNA <int>,
## Silent <int>, Splice_Site <int>, Start_Codon_Del <int>, Start_Codon_Ins
## <int>, Start_Codon_SNP <int>, Stop_Codon_Del <int>, Stop_Codon_Ins <int>.</code></pre>
<pre class="r"><code>sessionInfo()</code></pre>
<pre><code>## R version 3.3.1 (2016-06-21)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.11.6 (El Capitan)
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats4 parallel stats graphics grDevices utils datasets
## [8] methods base
##
## other attached packages:
## [1] yaml_2.1.13 knitr_1.13
## [3] BiocStyle_2.0.2 dplyr_0.5.0
## [5] VariantAnnotation_1.18.1 Rsamtools_1.25.0
## [7] Biostrings_2.41.4 XVector_0.13.2
## [9] SummarizedExperiment_1.3.6 Biobase_2.33.0
## [11] GenomicRanges_1.25.9 GenomeInfoDb_1.8.1
## [13] IRanges_2.7.11 S4Vectors_0.11.7
## [15] BiocGenerics_0.19.1 bigrquery_0.3.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.5 highr_0.6
## [3] formatR_1.4 liftr_0.3
## [5] GenomicFeatures_1.24.3 objectProperties_0.6.5
## [7] bitops_1.0-6 tools_3.3.1
## [9] sevenbridges_1.2.4 zlibbioc_1.19.0
## [11] biomaRt_2.29.2 digest_0.6.9
## [13] docopt_0.4.5 uuid_0.1-2
## [15] BSgenome_1.41.2 RSQLite_1.0.0
## [17] jsonlite_1.0 evaluate_0.9
## [19] tibble_1.0 DBI_0.4-1
## [21] rstudioapi_0.6 curl_0.9.7
## [23] rtracklayer_1.33.8 httr_1.2.1
## [25] stringr_1.0.0 R6_2.1.2
## [27] AnnotationDbi_1.35.3 XML_3.98-1.4
## [29] BiocParallel_1.7.4 rmarkdown_0.9.6.9
## [31] tidyr_0.5.1 magrittr_1.5
## [33] GenomicAlignments_1.9.4 objectSignals_0.10.2
## [35] htmltools_0.3.5 rsconnect_0.4.3
## [37] assertthat_0.1 stringi_1.1.1
## [39] lazyeval_0.2.0 openssl_0.9.4
## [41] RCurl_1.95-4.8</code></pre>
</div>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-82652476-1', 'auto');
ga('send', 'pageview');
</script>
</div>
<script>
// add bootstrap table styles to pandoc tables
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>