Learn R Programming

haplotypes (version 1.0)

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 Haplotype
as.dna(x)

Arguments

x

a matrix, a data.frame, a list or an object of class Haplotype containing the DNA sequences.

Value

an object of class Dna.

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 = "Haplotype")

coerces a Haplotype 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
# NOT RUN {
## 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 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)
# }

Run the code above in your browser using DataLab