Learn R Programming

haplotypes (version 1.1.3.1)

as.dna-methods: Coerces an object to a Dna object

Description

Coerces an object that contains DNA sequences to an object of Class Dna.

Usage

# S4 method for matrix
as.dna(x)
# S4 method for data.frame
as.dna(x)
# S4 method for list
as.dna(x)
# S4 method for character
as.dna(x)
# S4 method for Haplotype
as.dna(x)
# S4 method for DNAbin
as.dna(x)
# S4 method for phyDat
as.dna(x)

Value

an object of class Dna.

Arguments

x

a matrix, a data.frame, a list, a character, an object of class Haplotype, DNAbin {ape} or phyDat {phangorn} object containing the DNA sequences.

Methods

signature(x = "matrix")

coerces matrix to a Dna object.

signature(x = "data.frame")

coerces data.frame to a Dna object.

signature(x = "list")

coerces list to a Dna object.

signature(x = "character")

coerces characters to a Dna object.

signature(x = "Haplotype")

coerces a Haplotype object to a Dna object.

signature(x = "DNAbin")

coerces a DNAbin object to a Dna object.

signature(x = "phyDat")

coerces a phyDat object to a Dna object.

Details

Elements of the list must be vectors. Each element of the list contains a single DNA sequence. If the sequence lengths differ, the longest sequence is taken into account and gaps are introduced to the shorter sequences at the end of the matrix in the slot sequence. Sequence length information is stored in the slot seqlengths.

Valid characters for the slot sequence are "A","C","G","T","a","c","g","t","-","?". During the conversion of the object to the class Dna, integers 0,1,2,3,4,5 or characters "0","1","2","3","4","5" are converted to "?","A","C","G","T","-", respectively. Invalid characters are replaced with "?" with a warning message.

Examples

Run this code

## Coercing a matrix to a 'Dna' object.
# all valid characters
x<-matrix(c("?","A","C","g","t","-","0","1","2","3","4","5"),4,6)
rownames(x)<-c("seq1","seq2","seq3","seq4") 
dna.obj<-as.dna(x) 
dna.obj
# the sequence matrix
dna.obj@sequence

if (FALSE) {
# includes invalid characters
x<-matrix(c("X","y","*","?","t","-","0","1","2","3","4","5"),4,6) 
rownames(x)<-c("seq1","seq2","seq3","seq4") 
dna.obj<-as.dna(x) 
dna.obj
dna.obj@sequence

# all valid integers
x<-matrix(c(0,1,2,3,4,5,0,1,2,3,4,5),4,6) 
rownames(x)<-c("seq1","seq2","seq3","seq4") 
dna.obj<-as.dna(x) 
dna.obj
dna.obj@sequence

## Coercing a data.frame to a 'Dna' object.
x<-data.frame(matrix(c("?","A","C","g","t","-","0","1","2","3","4","5"),4,6)) 
rownames(x)<-c("seq1","seq2","seq3","seq4") 
dna.obj<-as.dna(x) 
dna.obj
dna.obj@sequence

## Coercing a list to a 'Dna' object.
seq1<-c("?","A","C","g","t","-","0","1")
seq2<-c("?","A","C","g","t","-","0","1","2")
seq3<-c("?","A","C","g","t","-","0","1","2","3")
x<-list(seq1=seq1,seq2=seq2,seq3=seq3) 
dna.obj<-as.dna(x)

# sequence lengths differ 
dna.obj@seqlengths
dna.obj@sequence

## Coercing a character vector to a Dna object.
seq<-c("?","A","C","g","t","-","0","1")
x<-as.dna(seq)
x


## Coercing a Haplotype object to a Dna object.
data("dna.obj")
x<-dna.obj
h<-haplotype(x)

# DNA Sequences of unique haplotypes  
dna.obj<-as.dna(h)
dna.obj

d<-distance(x)

# if 'Haplotype' object does not contain 'DNA' Sequences
h<-haplotype(d)

# returns an error
as.dna(h)

## Coercing a DNAbin object to a Dna object.
require(ape)
data(woodmouse)
x<-as.dna(woodmouse)
x

}

Run the code above in your browser using DataLab