-
Notifications
You must be signed in to change notification settings - Fork 9
/
io.R
75 lines (62 loc) · 1.68 KB
/
io.R
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
#' Read an EIGENSTRAT ind/snp/geno file.
#'
#' These functions each read one part of the EIGENSTRAT dataset trio.
#'
#' Note that \code{read_geno()} will only read plain-text geno files, not compressed ones.
#'
#' @param data EIGENSTRAT data object.
#'
#' @return A data.frame object.
#'
#' @export
read_ind <- function(data) {
path <- ifelse(is.null(data$group), data$ind, data$group)
utils::read.table(path, col.names = c("id", "sex", "label"), stringsAsFactors = FALSE) %>%
tibble::as_tibble(.name_repair = "minimal")
}
#' @rdname read_ind
#' @param exclude Read the list of excluded SNPs?
#'
#' @export
read_snp <- function(data, exclude = FALSE) {
readr::read_table(
ifelse(exclude, data$exclude, data$snp),
col_names = c("id", "chrom", "gen", "pos", "ref", "alt"),
col_types = "ccdicc",
progress = FALSE
)
}
#' @rdname read_ind
#' @export
read_geno <- function(data) {
ind <- read_ind(data)$id
# get the number of samples in the geno file
n <- nchar(readLines(data$geno, 1))
readr::read_fwf(
data$geno,
col_positions = readr::fwf_widths(rep(1, n), ind),
col_types = readr::cols(.default = "i"),
progress = FALSE
) %>%
replace(., . == 9, NA)
}
#' Write an EIGENSTRAT ind/snp/geno file.
#'
#' @param df A data.frame object.
#' @param file Path to an output file.
#'
#' @export
write_ind <- function(df, file) {
readr::write_tsv(df, file, col_names = FALSE)
}
#' @rdname write_ind
#' @export
write_snp <- function(df, file) {
readr::write_tsv(df, file, col_names = FALSE)
}
#' @rdname write_ind
#' @export
write_geno <- function(df, file) {
df <- replace(df, is.na(df), 9)
writeLines(apply(df, 1, paste, collapse = ""), con = file)
}