| 1 |
\name{pgenlibr-package} |
| 2 |
\alias{pgenlibr-package} |
| 3 |
\alias{pgenlibr} |
| 4 |
\docType{package} |
| 5 |
\title{ |
| 6 |
PLINK 2 Binary (.pgen) Reader |
| 7 |
} |
| 8 |
\description{ |
| 9 |
A thin wrapper over PLINK 2's core libraries which provides an R interface |
| 10 |
for reading .pgen files. A minimal .pvar loader is also included. |
| 11 |
} |
| 12 |
\details{ |
| 13 |
\code{NewPvar} and \code{NewPgen} initialize the respective readers. Then, |
| 14 |
you can either iterate through one variant at a time (\code{Read}, |
| 15 |
\code{ReadAlleles}) or perform a multi-variant matrix load |
| 16 |
(\code{ReadIntList}, \code{ReadList}). When you're done, \code{ClosePgen} |
| 17 |
and \code{ClosePvar} free resources. |
| 18 |
} |
| 19 |
\author{ |
| 20 |
Christopher Chang \email{[email protected]} |
| 21 |
} |
| 22 |
\references{ |
| 23 |
Chang, C.C. and Chow, C.C. and Tellier, L.C.A.M. and Vattikuti, S. and |
| 24 |
Purcell, S.M. and Lee J.J. (2015) Second-generation PLINK: rising to the |
| 25 |
challenge of larger and richer datasets. \emph{Gigascience} 4:7. |
| 26 |
\doi{10.1186/s13742-015-0047-8}. |
| 27 |
} |
| 28 |
\keyword{ package } |
| 29 |
\examples{ |
| 30 |
# This is modified from https://yosuketanigawa.com/posts/2020/09/PLINK2 . |
| 31 |
library(pgenlibr) |
| 32 |
|
| 33 |
# These files are subsetted from downloads available at |
| 34 |
# https://www.cog-genomics.org/plink/2.0/resources#phase3_1kg . |
| 35 |
# Note that, after downloading the original files, the .pgen file must be |
| 36 |
# decompressed before use; but both pgenlibr and the PLINK 2 program can |
| 37 |
# handle compressed .pvar files. |
| 38 |
pvar_path <- system.file("extdata", "chr21_phase3_start.pvar.zst", package="pgenlibr") |
| 39 |
pgen_path <- system.file("extdata", "chr21_phase3_start.pgen", package="pgenlibr") |
| 40 |
|
| 41 |
pvar <- pgenlibr::NewPvar(pvar_path) |
| 42 |
pgen <- pgenlibr::NewPgen(pgen_path, pvar=pvar) |
| 43 |
|
| 44 |
# Check the number of variants and samples. |
| 45 |
pgenlibr::GetVariantCt(pgen) |
| 46 |
pgenlibr::GetRawSampleCt(pgen) |
| 47 |
|
| 48 |
# Get the chromosome, position, and ID of the first variant. |
| 49 |
GetVariantChrom(pvar, 1) |
| 50 |
GetVariantPos(pvar, 1) |
| 51 |
GetVariantId(pvar, 1) |
| 52 |
|
| 53 |
# Read the 14th variant. |
| 54 |
buf <- pgenlibr::Buf(pgen) |
| 55 |
pgenlibr::Read(pgen, buf, 14) |
| 56 |
|
| 57 |
# Get the index of the variant with ID "rs569225703". |
| 58 |
var_id <- pgenlibr::GetVariantsById(pvar, "rs569225703") |
| 59 |
|
| 60 |
# Get allele count. |
| 61 |
pgenlibr::GetAlleleCt(pvar, var_id) |
| 62 |
|
| 63 |
# It has three alleles, i.e. two ALT alleles. |
| 64 |
# Read first-ALT-allele dosages for that variant. |
| 65 |
pgenlibr::Read(pgen, buf, var_id) |
| 66 |
|
| 67 |
# Read second-ALT-allele dosages. |
| 68 |
pgenlibr::Read(pgen, buf, var_id, allele_num=3) |
| 69 |
|
| 70 |
# Read a matrix with both variants. Note that, for the multiallelic variant, |
| 71 |
# the dosages of both ALT alleles are summed here. |
| 72 |
geno_mat <- pgenlibr::ReadList(pgen, c(14, var_id)) |
| 73 |
|
| 74 |
pgenlibr::ClosePgen(pgen) |
| 75 |
pgenlibr::ClosePvar(pvar) |
| 76 |
} |