write_snp: Write Eigenstrat *.snp files

View source: R/write_snp.R

write_snpR Documentation

Write Eigenstrat *.snp files

Description

This function writes a tibble with the right columns into a standard Eigenstrat *.snp file. It uses readr::write_tsv() to do it efficiently.

Usage

write_snp(file, tib, verbose = TRUE)

Arguments

file

Output file (whatever is accepted by readr::write_tsv()). If file is missing the expected *.snp extension, the function adds it.

tib

The tibble or data.frame to write. It must contain these columns: id, chr, posg, pos, ref, alt. Throws an error if any of these columns are missing. Additional columns are ignored. Columns are automatically reordered in output as expected in format.

verbose

If TRUE (default), function reports the path of the file being written (after autocompleting the extension).

Value

The output tib invisibly (what readr::write_tsv() returns).

See Also

Eigenstrat SNP format reference: https://github.com/DReichLab/EIG/tree/master/CONVERTF

Examples

# to write an existing table `snp` into file "data.snp", run like this:
# write_snp("data", snp)
# this also works
# write_snp("data.snp", snp)

# The following example is more detailed but also more awkward
# because (only for these examples) the package must create the file in a *temporary* location

# create a dummy tibble with the right columns
library(tibble)
tib <- tibble(
    id = 1:3,
    chr = 1:3,
    posg = 0,
    pos = 1:3,
    ref = 'A',
    alt = 'B'
)
# a dummy file
file_out <- tempfile('delete-me-example', fileext = '.snp') # will also work without extension
# write the table out in *.snp format (no header, columns in right order)
write_snp(file_out, tib)
# delete output when done
file.remove(file_out)


genio documentation built on Jan. 7, 2023, 1:12 a.m.