Author: Vincent J. Carey, stvjc at channing.harvard.edu

# 03 Working with UK Biobank summary statistics

# Overview

In this document we illustrate some approaches to working with UK Biobank summary statistics. Be sure that that the python module `ukbb_pan_ancestry` has been installed where reticulate can find it. (We don’t use basilisk as of 12/24/2022 because of issues in the terra spark cluster.)

    library(BiocHail)

If the above command indicates that BiocHail is not available, see the Installation section near the end of this document.

## Initialization and description

### Standalone

We have produced a representation of summary statistics for a sample of 9888 loci. This 5GB resource can be retrieved and cached with the following code:

    hl = hail_init()
    ss = get_ukbb_sumstat_10kloci_mt(hl) # can take about a minute to unzip 5GB
    ss$count()   # but if a persistent MatrixTable is at the location given
                 # by env var HAIL_UKBB_SUMSTAT_10K_PATH it goes quickly

To get a description of available content, we need a python chunk:

    r.ss.describe()

### Terra

Here’s a basic description of the summary stats table, with code that works in terra.bio:

    hl = bare_hail()
    hl$init(idempotent=TRUE, spark_conf=list(
      'spark.hadoop.fs.gs.requester.pays.mode'= 'CUSTOM',
      'spark.hadoop.fs.gs.requester.pays.buckets'= 'ukb-diverse-pops-public',
      'spark.hadoop.fs.gs.requester.pays.project.id'= Sys.getenv("GOOGLE_PROJECT")))

We need to use a python chunk to get the output, using gs:// storage references.

    r.hl.read_matrix_table('gs://ukb-diverse-pops-public/sumstats_release/results_full.mt').describe()

## Exploring the subset

### Metadata on study phenotypes

We’ll collect the column metadata to start to understand details of annotation of phenotypes.

    sscol = ss$cols()$collect() # OK because we are just working over column metadata
    ss1 = sscol[[1]]
    names(ss1)
    ss1$get("phenocode")
    ss1$get("description")

We’ve introduced a function that extracts selected fields for a given phenotype, that accommodates availability of results for specific populations.

    multipop_df(ss1)

This can be iterated over all the elements of `sscol` to produce a comprehensive searchable table. Here’s a small illustration:

    library(DT)
    ndf = do.call(rbind, lapply(sscol[1:10], multipop_df))
    datatable(ndf)

### Metadata on loci

We’ll trim the material to be worked with by sampling both rows and columns. (2023.01.08: In future revisions we will be able to control the seed for random sampling.)

    sss = ss$sample_rows(.01)$sample_cols(.01)
    sss$count()

Row metadata are simple to collect:

    rss = sss$rows()$collect()
    rss1 = rss[[1]]
    names(rss1)
    names(rss1$locus)
    rss1$locus$contig
    sapply(c("contig", "position"), function(x) rss1$locus[[x]])

### Summary statistics

The summary statistics themselves reside in entries of the MatrixTable. This can be expensive to collect and so filtering methods beyond random sampling must be mastered. But here is a basic view.

    sse = sss$entries()$collect()
    length(sse)
    names(sse[[1]])
    sse1 = sse[[1]]

The `summary_stats` component has the association p-values – log10 transformed?

    length(sse1$summary_stats)
    names(sse1$summary_stats[[1]])
    sse1$summary_stats[[1]]$Pvalue

# Exercises

## Infrastructure

- Define an interface to this subset of 10k loci that supports queries like
  - has disease x been studied in UK Biobank?
  - how many phenotypes have been studied in K populations, K=2, 3, …?
  - how consistent is the annotation – are numbers of controls and cases always recorded?
- Do the UK Biobank portals produce information to resolve these questions?
  - if so, what are the API calls to obtain answers?
  - if not, what is missing from the portals to allow answers to be obtained?

## Substantive

- List the genes that are near the loci collected in the 10k random sample
- (Hard) What is the most significant finding (position, phenotype, population) in the 10k subset?

# Installing `BiocHail`

`BiocHail` should be installed as follows:

    if (!require("BiocManager"))
        install.packages("BiocManager")
    BiocManager::install("BiocHail")

# SessionInfo

    sessionInfo()

    ## R version 4.6.0 (2026-04-24)
    ## Platform: x86_64-pc-linux-gnu
    ## Running under: Ubuntu 24.04.4 LTS
    ##
    ## Matrix products: default
    ## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
    ## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
    ##
    ## locale:
    ##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
    ##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
    ##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
    ##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
    ##  [9] LC_ADDRESS=C               LC_TELEPHONE=C
    ## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
    ##
    ## time zone: Etc/UTC
    ## tzcode source: system (glibc)
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] ggplot2_4.0.3    BiocHail_1.12.0  BiocStyle_2.40.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] rappdirs_0.3.4      sass_0.4.10         generics_0.1.4
    ##  [4] RSQLite_2.4.6       lattice_0.22-9      digest_0.6.39
    ##  [7] magrittr_2.0.5      RColorBrewer_1.1-3  evaluate_1.0.5
    ## [10] grid_4.6.0          fastmap_1.2.0       blob_1.3.0
    ## [13] jsonlite_2.0.0      Matrix_1.7-5        DBI_1.3.0
    ## [16] BiocManager_1.30.27 scales_1.4.0        httr2_1.2.2
    ## [19] jquerylib_0.1.4     cli_3.6.6           rlang_1.2.0
    ## [22] dbplyr_2.5.2        bit64_4.8.0         withr_3.0.2
    ## [25] cachem_1.1.0        yaml_2.3.12         otel_0.2.0
    ## [28] tools_4.6.0         dir.expiry_1.20.0   parallel_4.6.0
    ## [31] memoise_2.0.1       dplyr_1.2.1         filelock_1.0.3
    ## [34] basilisk_1.24.0     BiocGenerics_0.58.0 curl_7.1.0
    ## [37] reticulate_1.46.0   png_0.1-9           buildtools_1.0.0
    ## [40] vctrs_0.7.3         R6_2.6.1            BiocFileCache_3.2.0
    ## [43] lifecycle_1.0.5     bit_4.6.0           pkgconfig_2.0.3
    ## [46] gtable_0.3.6        pillar_1.11.1       bslib_0.10.0
    ## [49] glue_1.8.1          Rcpp_1.1.1-1.1      xfun_0.57
    ## [52] tibble_3.3.1        tidyselect_1.2.1    sys_3.4.3
    ## [55] knitr_1.51          farver_2.1.2        htmltools_0.5.9
    ## [58] rmarkdown_2.31      maketools_1.3.2     compiler_4.6.0
    ## [61] S7_0.2.2