DT2Mat_genos: Convert a data table of genotypes into a matrix (or vice...

View source: R/DT2Mat_genos.R

DT2Mat_genosR Documentation

Convert a data table of genotypes into a matrix (or vice versa)

Description

Takes a data table of genotypes in long format and converts it into a matrix in wide format (loci in columns and individuals in rows). The reverse can also be done. See also DT2Mat_freqs for converting matrix of frequencies.

Usage

DT2Mat_genos(
  dat,
  sampCol = "SAMPLE",
  locusCol = "LOCUS",
  genoCol = "GT",
  flip = FALSE
)

Arguments

dat

Data table or Matrix: The object to transform. If this is a long data table of genotypes coded as per VCF specifications ('0/0', '0/1', '1/1'), or counts of the Alt alleles (0, 1, 2, repsectively). Three columns are required:

  1. The sampled individual ID (see param sampCol).

  2. The locus ID (see param locusCol).

  3. The genotype (see param genoCol).

The sampled individual ID column serves as the pivot point to convert the long data table into a wide matrix. If converting from a genotypes matrix to a data table, see argument flip.

sampCol

Character: The column name with the sampled individual information. Default is 'SAMPLE'.

locusCol

Character: The column name with the locus information. Default is 'LOCUS'.

genoCol

Character: The column name with the genotype information. Default is 'GT'.

flip

Logical: Instead of converting a (long) data table to a (wide) matrix, should a (wide) matrix be converted into a (long) data table? Default = FALSE. If TRUE, then param dat must be a matrix, with loci names as column headers, sample IDs in the row names, and genotypes in the cells. When TRUE, params sampCol, locusCol, and genoCol are used to structure the new matrix.

Value

When flip=FALSE, converts a data table into a genotype matrix. When flip=TRUE, converts a matrix into a data table with three columns: (1) $SAMPLE, the sample ID; (2) $LOCUS, the locus ID; and (3) $GT, the Ref allele frequency.

Examples

library(genomalicious)
data(data_Genos)

# Convert a long data table to a wide matrix
genoMat <- DT2Mat_genos(
   data_Genos,
   sampCol='SAMPLE',
   locusCol='LOCUS',
   genoCol='GT',
   flip=FALSE)

genoMat


j-a-thia/genomalicious documentation built on Oct. 19, 2024, 7:51 p.m.