Skip to content

Instantly share code, notes, and snippets.

@agrueneberg
Last active April 30, 2023 21:57
Show Gist options
  • Save agrueneberg/812564cbe860db4ee864d019be940aaf to your computer and use it in GitHub Desktop.
Save agrueneberg/812564cbe860db4ee864d019be940aaf to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript
#
# Converts the mice dataset to a BED file that can be used with PLINK and
# BEDMatrix.
#
library(BGLR)
data(mice)
X <- mice.X
X <- 2 - X # flip homozygous alleles
X[X == 2] <- 3 # rewrite homozygous minor allele from 2 to 3
invisible(write_bed(X, n = nrow(X), p = ncol(X), bed_file = "mice.bed"))
fam <- data.frame(FID = as.character(mice.pheno$SUBJECT.NAME), IID = as.character(mice.pheno$SUBJECT.NAME), PAT = rep(0L, nrow(mice.pheno)), MAT = rep(0L, nrow(mice.pheno)), SEX = ifelse(mice.pheno$GENDER == "M", 1L, 2L), PHENOTYPE = rep(-9L, nrow(mice.pheno)),stringsAsFactors = FALSE)
write.table(fam, file = "mice.fam", sep = " ", row.names = FALSE, col.names = FALSE, quote = FALSE)
bim <- data.frame(chromosome = rep(0L, ncol(mice.X)), name = sapply(strsplit(colnames(mice.X), "_"), function(x) paste(x[seq_len(length(x) - 1)], collapse = "_")), dist = rep(0L, ncol(mice.X)), bp = rep(0L, ncol(mice.X)), allele1 = sapply(strsplit(colnames(mice.X), "_"), function(x) x[length(x)]), allele2 = rep(0L, ncol(mice.X)), stringsAsFactors = FALSE)
write.table(bim, file = "mice.bim", sep = " ", row.names = FALSE, col.names = FALSE, quote = FALSE)
alternatePhenotypes <- data.frame(FID = as.character(mice.pheno$SUBJECT.NAME), IID = as.character(mice.pheno$SUBJECT.NAME),stringsAsFactors = FALSE)
alternatePhenotypes <- cbind(alternatePhenotypes, mice.pheno[, -1], stringsAsFactors = FALSE)
write.table(alternatePhenotypes, file = "mice.pheno", sep = " ", row.names = FALSE, quote = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment