1
#' Read an EIGENSTRAT ind/snp/geno file.
2
#'
3
#' These functions each read one part of the EIGENSTRAT dataset trio.
4
#'
5
#' Note that \code{read_geno()} will only read plain-text geno files, not compressed ones.
6
#'
7
#' @param data EIGENSTRAT data object.
8
#'
9
#' @return A data.frame object.
10
#'
11
#' @export
12
read_ind <- function(data) {
13 1
  path <- ifelse(is.null(data$group), data$ind, data$group)
14 1
  utils::read.table(path, col.names = c("id", "sex", "label"), stringsAsFactors = FALSE) %>%
15 1
    tibble::as_tibble(.name_repair = "minimal")
16
}
17

18

19
#' @rdname read_ind
20
#' @param exclude Read the list of excluded SNPs?
21
#'
22
#' @export
23
read_snp <- function(data, exclude = FALSE) {
24 1
  readr::read_table2(
25 1
    ifelse(exclude, data$exclude, data$snp),
26 1
    col_names = c("id", "chrom", "gen", "pos", "ref", "alt"),
27 1
    col_types = "ccdicc",
28 1
    progress = FALSE
29
  )
30
}
31

32

33
#' @rdname read_ind
34
#' @export
35
read_geno <- function(data) {
36 1
  ind <- read_ind(data)$id
37

38
  # get the number of samples in the geno file
39 1
  n <- nchar(readLines(data$geno, 1))
40 1
  readr::read_fwf(
41 1
    data$geno,
42 1
    col_positions = readr::fwf_widths(rep(1, n), ind),
43 1
    col_types = readr::cols(.default = "i"),
44 1
    progress = FALSE
45
  ) %>%
46 1
    replace(., . == 9, NA)
47
}
48

49

50

51
#' Write an EIGENSTRAT ind/snp/geno file.
52
#'
53
#' @param df A data.frame object.
54
#' @param file Path to an output file.
55
#'
56
#' @export
57
write_ind <- function(df, file) {
58 1
  readr::write_tsv(df, file, col_names = FALSE)
59
}
60

61

62
#' @rdname write_ind
63
#' @export
64
write_snp <- function(df, file) {
65 1
  readr::write_tsv(df, file, col_names = FALSE)
66
}
67

68

69
#' @rdname write_ind
70
#' @export
71
write_geno <- function(df, file) {
72 1
  df <- replace(df, is.na(df), 9)
73 1
  writeLines(apply(df, 1, paste, collapse = ""), con = file)
74
}
75

Read our documentation on viewing source code .

Loading