---
title: "Module 2: Files and formats"
format:
  html:
    toc: true
    toc-depth: 2
    toc-title: "Contents"
    toc-location: left
    toc-expand: true
    anchor-sections: true
    code-copy: true
    code-fold: false
    code-overflow: wrap
    embed-resources: true
    include-after-body:
      text: |
        <script>
        document.querySelectorAll('a[href$=".qmd"]').forEach(function (a) {
          a.setAttribute('href', a.getAttribute('href').replace(/[.]qmd$/, '.html'));
        });
        </script>
editor: visual
editor_options:
  chunk_output_type: console
course_source_sha256: "de606f5ba9e6e4e393faa4ded7f7cfc494d077cd55f6b126a4871acb2cf82dba"
---

Open `GWAS_course.Rproj` and work from the project root. Run the chunks in order in a fresh R session.
All data are simulated. Read each summary, run its chunk and compare your result with the expected result.
The linked step pages explain the ideas and provide troubleshooting.

::: {.callout-note title="In this module"}

**Time.** About 60 minutes.

**Before you start.** You need the course data from step 0.7. The first code chunk checks the files that this module needs.

**Steps.**

- [2.1 The starting files](#step-2-1)
- [2.2 Convert to PLINK 2 native format](#step-2-2)
- [2.3 Sample and variant tables](#step-2-3)
- [2.4 First overview](#step-2-4)
- [2.5 Alleles and strand](#step-2-5)
- [2.6 Genome builds and lift-over](#step-2-6)
- [2.7 Add phenotype and covariates](#step-2-7)
- [2.8 Module 2 checkpoint](#step-2-8)

**Where the module ends.** The checkpoint at step 2.8.

:::

```{r}
#| label: module2_check
if (!file.exists("config.R") && file.exists("../config.R")) setwd("..")
source("R/guided.R"); guided_check_module(2)
```

```{r}
#| label: module2_setup
source("R/guided.R")
cfg <- guided_init()
```

## Step 2.1 · The starting files {#step-2-1}

We check that the three laboratory files are present and have matching sizes. We also look at the sample identifiers so we can recognise the starting data.

[Read step 2.1 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-1).

```{r}
#| label: s2-1
print(check_bed(cfg$input))  # <1>
head(read_fam(cfg$input))  # <2>
```

1. Check the three laboratory files and show their sizes.
2. Show the first rows of sample information.

### What each option does

No PLINK options are used in this step.

::: {.callout-tip title="Expected result" collapse="true"}

You see 2,016 sample wells and 299,523 variant entries before quality control (QC). Each well has its own [FID/IID](#glossary-fid-iid) pair, the two identifiers that name a sample in the files.

:::

::: {.callout-note title="PLINK terminal equivalent"}

No PLINK equivalent

:::

## Step 2.2 · Convert to PLINK 2 native format {#step-2-2}

We convert the laboratory files to the format that PLINK 2 uses for this course. We check that the conversion keeps every sample and variant, a place in the DNA where people can differ.

[Read step 2.2 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-2).

```{r}
#| label: s2-2
out <- guided_path(cfg, "2.2")  # <1>
run_plink(cfg, c("--bfile", cfg$input, "--make-pgen", "--out", out), paste0(out, c(".pgen", ".pvar", ".psam")))  # <2>
stopifnot(all(guided_counts(out) == c(2016, 299523)))  # <3>
# Course bookkeeping: records the counts for the next step. Do not change.
guided_source_checkpoint(cfg)
```

1. Choose the name for the converted files.
2. Convert the laboratory files to the native PLINK 2 format.
3. Check that all 2,016 samples and 299,523 variants remain.

### What each option does

| Option or modifier | What it does |
| --- | --- |
| `--bfile` | Reads the BED, BIM and FAM laboratory files named sim2026_lab in data/course/lab_export. |
| `--make-pgen` | Writes the data as PGEN, PVAR and PSAM files, the native PLINK 2 format. |
| `--out` | Starts the output file names with results/guided/s2-2_state. |

Every command also uses `--threads 2` (PLINK runs at most two tasks at the same time) and `--memory 2048` (PLINK uses at most about 2 GB of memory).

::: {.callout-tip title="Expected result" collapse="true"}

The 2,016 samples and 299,523 variants are all kept. PLINK can mark the [reference allele](#glossary-reference-allele) (REF) as provisional. This happens because a BED file does not say which allele matches the genome reference.

:::

::: {.callout-note title="PLINK terminal equivalent"}

These commands are shown for reference. Run the R chunk above to follow the course.
R creates any lists needed between PLINK commands. On Windows, use `bin\plink2.exe`.

```sh
bin/plink2 --bfile data/course/lab_export/sim2026_lab --make-pgen --out results/guided/s2-2_state --threads 2 --memory 2048
```

:::

## Step 2.3 · Sample and variant tables {#step-2-3}

We read the tables that describe the samples and the DNA sites. We inspect their first rows and check the sample identifiers so we can match information to the right sample.

[Read step 2.3 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-3).

```{r}
#| label: s2-3
samples <- guided_table(paste0(guided_path(cfg, "2.2"), ".psam"))  # <1>
variants <- guided_table(paste0(guided_path(cfg, "2.2"), ".pvar"))
head(samples)  # <2>
head(variants)
check_ids(samples, "Laboratory samples", verbose = TRUE)  # <3>
```

1. Read the sample and variant tables from the converted files.
2. Show the first rows of both tables.
3. Check that the sample identifiers are valid and unique.

### What each option does

No PLINK options are used in this step.

::: {.callout-tip title="Expected result" collapse="true"}

The last line printed is `Laboratory samples: no ID problems found.` A check that prints nothing can leave you unsure whether it ran, so this message confirms that it ran and passed. The PSAM file has 2,016 rows and the PVAR file has 299,523 rows. Different assay IDs do not prove that every site is unique. Two assays can still test the same place in the genome.

:::

::: {.callout-note title="PLINK terminal equivalent"}

No PLINK equivalent

:::

## Step 2.4 · First overview {#step-2-4}

We measure the share of missing DNA results for each sample and each site. These reports help us find possible problems before we choose what to remove.

[Read step 2.4 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-4).

```{r}
#| label: s2-4
out <- guided_path(cfg, "2.4", "missing")  # <1>
run_plink(cfg, c("--pfile", guided_path(cfg, "2.2"), "--missing", "--out", out))  # <2>
smiss <- guided_table(paste0(out, ".smiss"))  # <3>
vmiss <- guided_table(paste0(out, ".vmiss"))
summary(as.numeric(smiss$F_MISS))  # <4>
summary(as.numeric(vmiss$F_MISS))
```

1. Choose the names for the reports of missing results.
2. Measure missing results for each sample and each variant.
3. Read both reports into R.
4. Summarise the share of missing results in each report.

### What each option does

| Option or modifier | What it does |
| --- | --- |
| `--pfile` | Reads the PGEN, PVAR and PSAM files named results/guided/s2-2_state. |
| `--missing` | Writes separate reports of missing DNA results for samples and variants without removing any data. |
| `--out` | Starts the report file names with results/guided/s2-4_missing. |

Every command also uses `--threads 2` (PLINK runs at most two tasks at the same time) and `--memory 2048` (PLINK uses at most about 2 GB of memory).

::: {.callout-tip title="Expected result" collapse="true"}

You get a sample report and a variant report. Some values are much worse than the main group. No samples and no variants are removed at this step.

:::

::: {.callout-note title="PLINK terminal equivalent"}

These commands are shown for reference. Run the R chunk above to follow the course.
R creates any lists needed between PLINK commands. On Windows, use `bin\plink2.exe`.

```sh
bin/plink2 --pfile results/guided/s2-2_state --missing --out results/guided/s2-4_missing --threads 2 --memory 2048
```

:::

## Step 2.5 · Alleles and strand {#step-2-5}

We compare the DNA letters at each site with the supplied reference table. We correct 400 sites read from the opposite DNA strand and remove 3,013 sites whose letters cannot establish a clear match.

[Read step 2.5 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-5).

```{r}
#| label: s2-5
bim <- read_ws(paste0(cfg$input, ".bim"), header = FALSE)  # <1>
ann <- guided_table(file.path(cfg$data_dir, "array", "sim2026_array_annotation.tsv"))
k <- match(bim[[2]], ann$Name)  # <2>
stopifnot(!anyNA(k))
pair <- function(a, b) ifelse(a < b, paste0(a, b), paste0(b, a))  # <3>
observed <- pair(bim[[5]], bim[[6]])
reference <- pair(ann$Ref_Plus[k], ann$Alt_Plus[k])
complement <- chartr("ACGT", "TGCA", reference)  # <4>
complement <- pair(substr(complement, 1, 1), substr(complement, 2, 2))
palindromic <- reference %in% c("AT", "CG")  # <5>
flip <- !palindromic & observed != reference & observed == complement
exclude <- palindromic | (observed != reference & observed != complement)
flip_file <- guided_list(bim[[2]][flip], guided_path(cfg, "2.5", "flip.txt"))  # <6>
exclude_file <- guided_list(bim[[2]][exclude], guided_path(cfg, "2.5", "exclude.txt"))
stopifnot(sum(flip) == 400L, sum(exclude) == 3013L)
run_plink(cfg, c("--pfile", guided_path(cfg, "2.2"), "--flip", flip_file, "--exclude", exclude_file,  # <7>
  "--make-pgen", "--out", guided_path(cfg, "2.5")))
stopifnot(all(guided_counts(guided_path(cfg, "2.5")) == c(2016, 296510)))  # <8>
# Course bookkeeping: records the counts for the next step. Do not change.
ops <- data.frame(option = c("--flip", "--exclude"), value = c(flip_file, exclude_file))
guided_register(cfg, "2.5", "2.2", 2016, 296510, operations = ops)
```

1. Read the laboratory variant table and the reference DNA letters.
2. Match every laboratory site to the reference table by its identifier.
3. Put each pair of DNA letters in a consistent order for comparison.
4. Find the reference letter pairs as they would appear on the opposite strand.
5. Identify sites to correct and sites with ambiguous or incompatible letters.
6. Save both lists and check their expected sizes.
7. Correct the listed sites and remove the excluded sites in a new copy.
8. Check that 2,016 samples and 296,510 variants remain.

### What each option does

| Option or modifier | What it does |
| --- | --- |
| `--pfile` | Reads the PGEN, PVAR and PSAM files named results/guided/s2-2_state. |
| `--flip` | Uses s2-5_flip.txt to replace each DNA letter by its partner on the opposite strand at 400 sites: A pairs with T, and C with G. |
| `--exclude` | Removes the 3,013 sites listed in s2-5_exclude.txt because their DNA letter pairs are ambiguous or incompatible with the reference table. |
| `--make-pgen` | Saves the corrected data as PGEN, PVAR and PSAM files. |
| `--out` | Starts the corrected file names with results/guided/s2-5_state. |

Every command also uses `--threads 2` (PLINK runs at most two tasks at the same time) and `--memory 2048` (PLINK uses at most about 2 GB of memory).

::: {.callout-tip title="Expected result" collapse="true"}

400 sites are flipped and 3,013 sites are excluded. 2,016 samples and 296,510 variants remain. The planted problem categories overlap, so do not simply add their nominal counts.

:::

::: {.callout-note title="PLINK terminal equivalent"}

These commands are shown for reference. Run the R chunk above to follow the course.
R creates any lists needed between PLINK commands. On Windows, use `bin\plink2.exe`.

```sh
bin/plink2 --pfile results/guided/s2-2_state --flip results/guided/s2-5_flip.txt --exclude results/guided/s2-5_exclude.txt --make-pgen --out results/guided/s2-5_state --threads 2 --memory 2048
```

:::

## Step 2.6 · Genome builds and lift-over {#step-2-6}

We convert the DNA positions in a separate exercise from the GRCh37 genome reference to GRCh38. We keep sites that map to the same chromosome, correct opposite-strand letters and check the new positions without changing the main dataset.

[Read step 2.6 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-6).

```{r}
#| label: s2-6
legacy <- file.path(cfg$data_dir, "grch37_exercise", "legacy_grch37")  # <1>
bim37 <- read_ws(paste0(legacy, ".bim"), header = FALSE)
chain <- file.path(cfg$data_dir, "grch37_exercise", "hg19ToHg38.over.chain.gz")  # <2>
require_files(chain)
lifted <- guided_liftover(paste0("chr", bim37[[1]]), bim37[[4]], chain)  # <3>
keep <- !is.na(lifted$position) & lifted$chromosome == paste0("chr", bim37[[1]])
map <- data.frame(ID = bim37[[2]][keep], POS = lifted$position[keep])  # <4>
map_file <- guided_path(cfg, "2.6", "update_map.tsv")
write.table(map, map_file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE)
keep_file <- guided_list(map$ID, guided_path(cfg, "2.6", "mapped.txt"))
args <- c("--bfile", legacy, "--extract", keep_file, "--update-map", map_file)  # <5>
reverse <- bim37[[2]][keep & lifted$strand == "-"]
if (length(reverse)) args <- c(args, "--flip", guided_list(reverse, guided_path(cfg, "2.6", "reverse.txt")))
run_plink(cfg, c(args, "--make-pgen", "--out", guided_path(cfg, "2.6", "grch38")))  # <6>
ann <- guided_table(file.path(cfg$data_dir, "array", "sim2026_array_annotation.tsv"))  # <7>
k <- match(map$ID, ann$Name)
stopifnot(all(map$POS[!is.na(k)] == as.numeric(ann$Position_GRCh38[k[!is.na(k)]])))
print(c(input_sites = nrow(bim37), mapped_same_chromosome = sum(keep), reverse_strand = length(reverse)))  # <8>
```

1. Read the variant positions in the separate GRCh37 exercise.
2. Find the supplied file that links GRCh37 positions to GRCh38 positions.
3. Convert the positions and keep sites that stay on the same chromosome.
4. Save the new positions and the list of sites to keep.
5. Prepare the PLINK options and add a correction for opposite-strand sites if needed.
6. Save the converted exercise separately from the main dataset.
7. Check the new positions against matching sites in the supplied reference table.
8. Show how many sites were read, kept and mapped to the opposite strand.

### What each option does

| Option or modifier | What it does |
| --- | --- |
| `--bfile` | Reads the BED, BIM and FAM files named data/course/grch37_exercise/legacy_grch37, containing 10,000 exercise sites. |
| `--extract` | Keeps the 9,970 sites listed in s2-6_mapped.txt that map to the same chromosome in GRCh38. |
| `--update-map` | Reads site identifiers and their new GRCh38 positions from the two columns of s2-6_update_map.tsv. |
| `--flip` | R adds this option when sites map to the opposite DNA strand, using s2-6_reverse.txt to correct the DNA letters at eight sites in this exercise. |
| `--make-pgen` | Saves the converted exercise as PGEN, PVAR and PSAM files. |
| `--out` | Starts the converted exercise file names with results/guided/s2-6_grch38. |

Every command also uses `--threads 2` (PLINK runs at most two tasks at the same time) and `--memory 2048` (PLINK uses at most about 2 GB of memory).

::: {.callout-tip title="Expected result" collapse="true"}

The exercise has 10,000 sites. 9,970 of them map to the same chromosome, and eight of these map to the reverse strand in this chain file. PLINK prints a warning that the base-pair positions are now unsorted. This warning is expected: the new GRCh38 positions are not in the old order. The resulting exercise variants are not sorted by position. Sort them explicitly before any operation that needs sorted input. Mapped positions whose identifier also appears in the course annotation agree with GRCh38. The main 296,510-site state from 2.5 is unchanged. A lift-over can lose sites, so record the mapping counts that are reported.

:::

::: {.callout-note title="PLINK terminal equivalent"}

These commands are shown for reference. Run the R chunk above to follow the course.
R creates any lists needed between PLINK commands. On Windows, use `bin\plink2.exe`.

```sh
bin/plink2 --bfile data/course/grch37_exercise/legacy_grch37 --extract results/guided/s2-6_mapped.txt --update-map results/guided/s2-6_update_map.tsv --flip results/guided/s2-6_reverse.txt --make-pgen --out results/guided/s2-6_grch38 --threads 2 --memory 2048
```

:::

## Step 2.7 · Add phenotype and covariates {#step-2-7}

We match disease status, age and recorded sex to each laboratory well by its identifiers. We save the information table and add disease status and sex to the DNA files so later checks use the right person's information.

[Read step 2.7 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-7).

```{r}
#| label: s2-7
metadata <- guided_metadata(cfg)  # <1>
pheno <- metadata[c("FID", "IID", "SEX", "Age", "Disease")]  # <2>
names(pheno)[1] <- "#FID"
pheno_file <- guided_path(cfg, "2.7", "phenotypes.tsv")
guided_write(pheno, pheno_file)  # <3>
run_plink(cfg, c("--pfile", guided_path(cfg, "2.5"), "--update-sex", pheno_file,  # <4>
  "--pheno", pheno_file, "--pheno-name", "Disease", "--make-pgen", "--out", guided_path(cfg, "2.7")))
stopifnot(all(guided_counts(guided_path(cfg, "2.7")) == c(2016, 296510)))  # <5>
# Course bookkeeping: records the counts for the next step. Do not change.
ops <- data.frame(option = c("--update-sex", "--pheno", "--pheno-name"), value = c(pheno_file, pheno_file, "Disease"))
guided_register(cfg, "2.7", "2.5", 2016, 296510, operations = ops)
```

1. Match the sample sheet to each person's disease status, age and recorded sex.
2. Select and name the columns for the sample information file.
3. Save the sample information table.
4. Add recorded sex and disease status to a new copy of the DNA files.
5. Check that all 2,016 samples and 296,510 variants remain.

### What each option does

| Option or modifier | What it does |
| --- | --- |
| `--pfile` | Reads the corrected PGEN, PVAR and PSAM files named results/guided/s2-5_state. |
| `--update-sex` | Loads recorded sex from s2-7_phenotypes.tsv by matching the sample identifiers. |
| `--pheno` | Reads the table s2-7_phenotypes.tsv containing the sample identifiers and disease status. |
| `--pheno-name` | Selects the Disease column as the outcome: 1 means a control without the disease and 2 means a case with the disease. |
| `--make-pgen` | Saves PGEN, PVAR and PSAM files with recorded sex and disease status in the sample table. |
| `--out` | Starts the joined file names with results/guided/s2-7_state. |

Every command also uses `--threads 2` (PLINK runs at most two tasks at the same time) and `--memory 2048` (PLINK uses at most about 2 GB of memory).

::: {.callout-tip title="Expected result" collapse="true"}

All 2,016 wells are matched, and 296,510 variants remain. There are 2,000 people before we remove low-quality wells, relatives and ancestry outliers.

:::

::: {.callout-note title="PLINK terminal equivalent"}

These commands are shown for reference. Run the R chunk above to follow the course.
R creates any lists needed between PLINK commands. On Windows, use `bin\plink2.exe`.

```sh
bin/plink2 --pfile results/guided/s2-5_state --update-sex results/guided/s2-7_phenotypes.tsv --pheno results/guided/s2-7_phenotypes.tsv --pheno-name Disease --make-pgen --out results/guided/s2-7_state --threads 2 --memory 2048
```

:::

## Step 2.8 · Module 2 checkpoint {#step-2-8}

We save a complete checked starting point for module 3, with 2,016 samples and 296,510 variants. This saved copy lets us restart the analysis without repeating module 2.

[Read step 2.8 on the course website](https://www.manuelgentiluomo.it/teaching/gwas/module-2/step-2-8).

```{r}
#| label: s2-8
# Course bookkeeping: records the counts for the next step. Do not change.
guided_snapshot(cfg, "2.8", guided_path(cfg, "2.7"), 2016, 296510)  # <1>
check_step("2.8", data_dir = cfg$out, work_dir = cfg$out)
```

1. Save the full starting point and check its files and counts.

### What each option does

No PLINK options are used in this step.

::: {.callout-tip title="Expected result" collapse="true"}

You get a full 2.8 checkpoint with 2,016 samples and 296,510 variants. The working state matches the counts and the [SHA-256](#glossary-sha-256) checks of its checkpoint. A SHA-256 check compares a short fingerprint of a file, to prove that the file has not changed.

:::

::: {.callout-note title="PLINK terminal equivalent"}

No PLINK equivalent

:::

## Glossary terms used in this notebook {#glossary}

These entries repeat the course glossary so that every link in this notebook works offline.

### FID / IID {#glossary-fid-iid .unlisted}

The identifier pair used to match samples between files.

### Reference allele {#glossary-reference-allele .unlisted}

The allele that matches the reference genome at a site. It is often written REF. The other allele is often written ALT.

### SHA-256 {#glossary-sha-256 .unlisted}

A short code calculated from the content of a file. If the file changes, the code changes, so it shows whether a file is intact.

## Where next {#where-next}

- Previous: [Module 1: From DNA to array data](module_01_from_dna_to_array_data.qmd)
- Next: [Module 3: Sample quality control](module_03_sample_quality_control.qmd)
- This module on the course website: [Module 2](https://www.manuelgentiluomo.it/teaching/gwas/module-2)
