An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1...
Transcript of An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1...
1
An introduction to objectoriented programming in R
Giorgio Valentinie –mail: [email protected]
DSI – Dipartimento di Scienze dell’ InformazioneUniversità degli Studi di Milano
Università degli Studi di Milano
Laurea Specialistica in Genomica Funzionale e Bioinformatica
Corso di Linguaggi di Programmazione per la Bioinformatica
2
Why OOprogramming in R?
2. Modeling and programming reasons– OOmodeling and OOprogramming are close to the
problem domain– OOprogramming paradigm provides a unified view
of data structures and procedures– It is easy to use, modifiy and extends OO sw libraries
3. Practical reasons in bioinformatics domain– Most of Bioconductor libraries adopt the OO
programming paradigm– Many useful R packages are OO
3
OO programming in R: classes and methods
Classes: “ideas” of objects
Methods: functions defined for specific classes
Objects
4
Classes
• In R all software entities are objects• Each object belong to a class • A class is a general scheme for objects:
from a general standpoint it represents an “idea”, i.e. the general structure of a given object
• Objects are realizations or instances of a class.
5
Classes in R
• Two kinds of information about a class are commonly stored in R:1. Representation of the class2. Relation of the class to other classes
6
Representation of the class
Classes are represented in R in two (non mutually exclusive) ways:
3. Slots: – a slot contains an object of another class– complicated classes can be defined in terms of simpler
ones4. Prototype:
• A definition of a “prototypical” object for that class
7
Example of class representation and object generation
# Class representing codons (triplets of nucleotides)
setClass("triplets", representation(x="character"), prototype(x="UAG"))
# Construction of objects of the “triplets” class
seq0 < new("triplets"); # prototype is calledseq < new("triplets",
x = c("AUG","CCA","CCA","GAA","UGA","CCA"));
8
Relations between classes and objects
Object inclusion. Objects of class B can be included in slots of class A (relation partof)
Class inheritance. Class B extends class A (relation is)
AB B
A B
9
Example of class inheritance
# a class storing codons and the corresponding aasetClass("Pairedseq",representation(y="character"), prototype(x="CCA", y=character("Pro")),
contains=“triplets");
# construction of an object of class Pairedseqpaired < new("Pairedseq", x=c("UUC", "CCA", "GCA"),
y=c("Phe", "Pro", "Ala"));
10
OO programming in R: classes and methods
Classes: “ideas” of objects
Methods: functions defined for specific classes
Objects
11
Methods
Can we make general functions, that behave differently with different objects ?
In R we need:• A generic function: what we wanto to do ?• A method: how we do it with an object of a
specific class ?
12
Methods: an example
# we need a method to compute the number of# codons that are present in a given object of # class triplets.
> length(seq) # wrong result!
setMethod("length", "triplets", function(object) { l < length(object@x); return(l); })
> seq < new("triplets", x = c("AUG","CCA","GGA","UGA","CCA"));> length(seq); # correct
13
Methods in OO programming
• A generic function behaves differently on objects belonging to specific classes (the corresponding method is called)
• With the inheritance mechanism, extended classes inherit all the methods defined for its “parent” classes
• Examples in the “laboratory” session …
14
What you can do with R OOprogramming
• You can easily define complex data structures, starting from simpler ones (using the inclusion and inheritance relations)
• You can define methods that behave differently according to the objects they are applied to (polymorphism)
• The inheritance mechanism permits to extend to the derived classes the methods defined for “parent” classes (reusage of software)
• Modular programming is easier• Modelling real problems is simpler