An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1...

14
1 An introduction to object-oriented programming in R Giorgio Valentini e –mail: [email protected] DSI – Dipartimento di Scienze dell’ Informazione Università degli Studi di Milano Università degli Studi di Milano Laurea Specialistica in Genomica Funzionale e Bioinformatica Corso di Linguaggi di Programmazione per la Bioinformatica

Transcript of An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1...

Page 1: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

1

An introduction to object­oriented 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

Page 2: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

2

Why OO­programming in R?

2. Modeling and programming reasons– OO­modeling and OO­programming are close to the 

problem domain– OO­programming 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

Page 3: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

3

OO programming in R: classes and methods

Classes: “ideas” of objects

Methods: functions defined for specific classes

Objects

Page 4: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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.

Page 5: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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

Page 6: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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

Page 7: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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"));

Page 8: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

8

Relations between classes and objects

Object inclusion. Objects of class B can be included in slots of class A (relation part­of)

Class inheritance. Class B extends class A (relation is)

AB B

A B

Page 9: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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"));

Page 10: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

10

OO programming in R: classes and methods

Classes: “ideas” of objects

Methods: functions defined for specific classes

Objects

Page 11: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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 ?

Page 12: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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

Page 13: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

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 …

Page 14: An introduction to objectoriented programming in Rvalentini/SlideCorsi/LPbio... · 2009-04-04 · 1 An introduction to objectoriented programming in R Giorgio Valentini e –mail:

14

What you can do with R OO­programming

• 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