| as.dna-methods {haplotypes} | R Documentation | 
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 signature 'matrix'
as.dna(x)
## S4 method for signature 'data.frame'
as.dna(x)
## S4 method for signature 'list'
as.dna(x)
## S4 method for signature 'character'
as.dna(x)
## S4 method for signature 'Haplotype'
as.dna(x)
## S4 method for signature 'DNAbin'
as.dna(x)
## S4 method for signature 'phyDat'
as.dna(x)
Arguments
x | 
  a matrix, a data.frame, a list, a character, an object of class   | 
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.  
Value
an object of class Dna.
Methods
signature(x = "matrix")- 
coerces matrix to a
Dnaobject. signature(x = "data.frame")- 
coerces data.frame to a
Dnaobject. signature(x = "list")- 
coerces list to a
Dnaobject. signature(x = "character")- 
coerces characters to a
Dnaobject. signature(x = "Haplotype")- 
coerces a
Haplotypeobject to aDnaobject. signature(x = "DNAbin")- 
coerces a
DNAbinobject to aDnaobject. signature(x = "phyDat")- 
coerces a
phyDatobject to aDnaobject. 
Examples
## 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
## Not run: 
# 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
## End(Not run)