# Introduction to object-oriented programming in R # Classes and objects in R ################################################################### # Basic definition of classes, inheritance and object generation ################################################################### # 1. Definition of a class # Definition of a class setClass("sequence", representation(x="character"), prototype(x=character(3))); # obtaining information about the class getClass("sequence"); # 2. Construction of objects of a given class # Object construction s0<-new("sequence"); seq <- new("sequence", x = c("A","T",rep("C",4),rep("G",8),"T","A","T")); # 3. Definition of classes with inheritance setClass("Pairedseq",representation(y="character"), prototype(x=character(3), y=character(3)), contains="sequence"); # obtaining information about the class getClass("Pairedseq"); paired <- new("Pairedseq", x=c("UUC", "CCA", "GCA"), y=c("Phe", "Pro", "Ala")); # A new class that inherits from Pairedseq setClass("Threeseq",representation("Pairedseq",z="character"), prototype(x=character(3), y=character(3), z=character(3))); # obtaining information about the class getClass("Threeseq"); # 4. Definition of a constructor for a given class # Constructor Pairedseq <- function(x,y) { if (length(x)!=length(y)) stop("the two sequences must match in length."); new("Pairedseq",x=x,y=y) } # getting an object of class Pairedseq using the constructor pseq <- Pairedseq(LETTERS[1:5],LETTERS[6:10]); pseq <- Pairedseq(LETTERS[1:5],LETTERS[6:12]); # error ! ####################################################################### # Slot accession, validity checking and relationships testing ####################################################################### # Definition of the class track setClass("track", representation(x = "numeric", y = "numeric")); # Getting an object of class track pos1 <- c(156, 182, 211, 212, 218, 220, 246, 247, 251, 252, 254, 258, 261, 263); resp1 <- c(348, 325, 333, 345, 325, 334, 334, 332, 347, 335, 340, 337, 323, 327); tr1 <- new("track", x = pos1, y = resp1); # Slot accession tr1@x; slot(tr1,"x"); # getting slot names slotNames(tr1) # getting names and classes of slots of a class getSlots("track") # Validity checking methods # Validity checking for objects of class track valid.track <- function(object) { if (any(is.na(object@x)) || any(is.na(object@y))) return ("x, y slots should not have NA values"); if (!identical(length(object@x),length(object@y))) return ("x, y slots should have equal length"); return(TRUE); } # Redefinition of class track with validity checking setClass("track", representation(x = "numeric", y = "numeric"), validity=valid.track); tr2<-new("track"); tr2<-new("track", x=rep(1,5), y=c(1,1,1,1,NA)); # error tr2<-new("track", x=rep(1,5), y=rep(2,6)); # error tr2<-new("track", x=rep(1,5), y=rep(2,5)); # OK # Relations between classes # Testing the is relation is(tr2,"track"); # testing the extends relation extends("sequence","numeric") extends("Pairedseq","sequence") # Definition of a new class setClass("track.novel", "track"); getClass("track.novel"); # Removing a class removeClass("track.novel");