11. Object Oriented Programming






We start this chapter by an exercise.

Try to model students and classrooms:

  • A student must have at least two properties: a name and scores.
  • A classroom must have a name and students.

Implement also:

  • a function that computes the average of a student’s scores;
  • an other one to compute the average of all students’ scores.














We can use a tuple to pack together “name” and “notes”, “name” and “students”:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

def student_average(student):
    return sum(student[1]) / len(student[1])


def class_average(classroom):
    notes = []
    for student in classroom[1]:
        notes += student[1]
    return sum(notes) / len(notes)


if __name__ == '__main__':
    john = ("John smith", [12, 13])
    jim = ("Jim Morison", [13, 15, 18])
    math = ("math", [john, jim])

    print(john[0] + " average is " + str(student_average(john)))
    print("{} average is {:2.2f}".format(jim[0], student_average(jim)))
    print(class_average(math))

    print(type(john), type(math))

But when we try to guess the type of jim and math python said tuple. We cannot distinguish programmatically what john and math model without reading all the code. Furthermore, the code for the average functions are not really readable.

To improve readability we can use namedtuples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import namedtuple

Student = namedtuple("Student", ("name", "scores"))


def student_average(student):
    return sum(student.scores) / len(student.scores)


Classroom = namedtuple("Classroom", ("name",  "students"))


def class_average(classroom):
    scores = []
    for student in classroom.students:
        scores += student.scores
    return sum(scores) / len(scores)


if __name__ == '__main__':
    john = Student("John smith", [12, 13])
    jim = Student("Jim Morison", [13, 15, 18])
    math = Classroom("math", [john, jim])

    print(john.name + " average is " + str(student_average(john)))
    print("{} average is {:2.2f}".format(jim.name, student_average(jim)))
    print(class_average(math))

    print(type(john), type(math))

This solves both problems of readability and data type.

But now we want to add a new property to a student: it’s phone number. The problem is that the phone number can vary during the student’s career. Which is not allowed by the tuple.

So we can try to use a mutable data structure: a dict:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


def student_average(student):
    return sum(student["scores"]) / len(student["scores"])


def class_average(classroom):
    scores = []
    for student in classroom["students"]:
        scores += student["scores"]
    return sum(scores) / len(scores)


if __name__ == '__main__':
    john = {"name": "John smith",
            "scores": [12, 13],
            "phone": "0102030405"
            }
    jim = {"name": "Jim Morison",
           "scores": [13, 15, 18],
           "phone": "0506070809"
           }
    math = {"name": "math",
            "students": [john, jim]
            }

    print(john["name"] + " average is " + str(student_average(john)))
    print("{} average is {:2.2f}".format(jim["name"], student_average(jim)))
    print(class_average(math))

    print(john["phone"])
    john["phone"] = "0101010101"
    print(john["phone"])

    print(type(john), type(math))

It’s not so bad but be loose the data type feature. Furthermore, we must have two different names for the average of classroom and students.

If we model student and classroom with OOP (“Object Oriented Programming”) we can pack together properties and functions that are applied on this data type:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

class Student:

    def __init__(self, name, phone, scores=None):
        self.name = name
        self.phone = phone
        if scores:
            self.scores = scores
        else:
            self.scores = []


    def average(self):
        return sum(self.scores) / len(self.scores)


class Classroom:

    def __init__(self, name, students=None):
        self.name = name
        self.students = students if students else []

    def average(self):
        scores = []
        for student in self.students:
            scores += student.scores
        return sum(scores) / len(scores)


if __name__ == '__main__':
    john = Student("John smith", "0102030405", scores=[12, 13])
    jim = Student("Jim Morison", "0506070809", scores=[13, 15, 18])
    math = Classroom("math", students=[john, jim])

    print(john.name + " average is " + str(john.average()))
    print("{} average is {:2.2f}.".format(jim.name, jim.average()))
    print("The average of '{}' class is {}.".format(math.name, math.average()))

    print(john.phone)
    john.phone = "0101010101"
    print(john.phone)

    print(type(john), type(math))

A class, for instance Student, is the recipe to build an object.

  • john is an object, it’s an instance of Student.
  • john and jim have the same data type, even they have distinct values.
  • average is called a method. It’s a function which is applied on a specific data type.

The concept of packing together data (attributes) and behavior (methods) is called encapsulation.

11.1. Concepts and Terminology

11.1.1. What is an Object ?

In programming an object is a concept. Calling elements of our programs objects is a metaphor, a useful way of thinking about them. In Python the basic elements of programming are things like strings, dictionaries, integers, functions, and so on ... They are all objects. This means they have certain things in common.

In previous chapter we use the procedural style of programming. This divides your program into reusable ‘chunks’ called procedures or functions.

As much as possible you try to keep your code in these modular chunks using logic to decide which chunk is called. This makes it less mental strain to visualise what your program is doing. It also makes it easier to maintain your code. you can see which parts does what job. Improving one function (which is reused) can improve performance in several places of your program.

An object can also modeling an non real life object. For instance a parser there is no equivalent object in our lives but we need a parser to read a file in fasta format and create a sequence object so we can modeling a parser, idem with a database connection it’s not real life object but it’s very useful to think a connection as an object with properties like the port of the connection, the host of destination, ... and some behaviors: connect, disconnect ...

The object is very simple idea in the computing world. The objects allow us to organize code in a programs and cut things in small chunk to ease thinking about complexes ideas.

11.1.2. Classes

A class definition can be compared to the recipe to bake a cake. A recipe is needed to bake a cake. The main difference between a recipe (class) and a cake (an instance or an object of this class) is obvious. A cake can be eaten when it is baked, but you can’t eat a recipe, unless you like the taste of printed paper. Like baking a cake, an OOP program constructs objects according to the class definitions of the program program. A class contains variables and methods. If you bake a cake you need ingredients and instructions to bake the cake.

In Python lot of people use class, data type and type interchangeably.

To create a custom class we have to use the keyword class followed by the name of the class the code belonging to a class in in the same block of code (indentation):

class ClassName:
    suite

class Sequence:
    code ...

Some positional or keyword parameters can be add between parenthesis (these have to do with a more advanced concept in OOP: inheritance):

class ClassName(base_classes, meta=MyMetaClass):
    suite

Note

PEP-8: Class names should normally use the CapWords convention.

11.1.3. Objects

A class is a model, a template, an object is an instance of this model. We can use the metaphor of the cake and the recipe.

You bake two cakes by following a recipe. The class is the recipe, you have two objects, the two cakes which are the instances of the same recipe.

Each cake has been made with the same ingredients but there are two independent cakes, a part of the first can be eaten whereas the second is still in the fridge:

# The model
>>> class Cake:
...     pass
...
# apple_pie is an instance of Cake.
>>> apple_pie = Cake()
>>> type(apple_pie)
<class '__main__.Cake'>
# pear_pie is an instance of Cake.
>>> pear_pie = Cake()
>>> type(pear_pie)
<class '__main__.Cake'>
# The two objects are not the same.
>>> apple_pie is pear_pie
False

11.1.4. Attributes

Data attributes (often called simply attributes) are references to data associated to an object.

There are two kinds of attributes: instance variables, or class variables.

An instance variable is directly associated to a particular object whereas a class variable is associated to a class then all objects which are instances of this class share the same variables (to more details see section about environments). We will not encounter lot of class variables.

We can access to instance variable by its fully qualified name using the name of the instance and the name of attribute separated by a dot.

We can access to the class variables using the fully qualified name through the class or through the instances of this class.

Objects are mutable. You can change the state of an object by making an assignment to one of its attributes:

>>> class Sequence:
...     # class variable
...     alphabet = 'ATGC'
...
...     def __init__(self, seq):
...         """
...         :param seq: the sequence
...         :type seq: string
...         """
...         # instance variable
...         self.sequence = seq
...
>>> ecor_1 = Sequence('GAATTC')
>>> bamh_1 = Sequence('GGATCC')
>>> print(ecor_1.sequence)
GAATTC
>>> print(bamh_1.sequence)
GGATCC
>>> print(Sequence.alphabet)
ATGC
>>> print(ecor_1.alphabet)
ATGC
>>> print(bamh_1.alphabet)
ATGC
>>> ecor_1 is bamh_1
False
>>> ecor_1.alphabet is bamh_1.alphabet
True
>>> Sequence.alphabet = 'ATGCN'
>>> print(ecor_1.alphabet)
ATGCN

11.1.5. Methods

In Python, methods are just attributes. They are special in the sense that they are attributes which can be executed. In Python we say callable.

A method is bound to an object. That means that this function is evaluated in the namespace of the object (see further).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3

import string


class Sequence:

    # the order of the nucleotide in alphabet is important
    # to easily compute the reverse complement
    # class variable
    alphabet = 'AGCT'

    def __init__(self, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        # instance variable
        self.sequence = seq

    def reverse_comp(self):
        """
        :return: the reverse complement of this sequence
        :rtype: :class:`Sequence` instance
        """
        rev = self.sequence[::-1]
        # Create a dict to translate nucleotides into their complement
        # See https://docs.python.org/3/library/stdtypes.html#str.maketrans
        table = str.maketrans(self.alphabet, self.alphabet[::-1])
        # Use the translation dict
        # See https://docs.python.org/3/library/stdtypes.html#str.translate
        rev_comp = str.translate(rev, table)
        return Sequence(rev_comp)


my_seq = Sequence('GAATTCC')
rev_comp = my_seq.reverse_comp()
print(my_seq.sequence)
print(rev_comp.sequence)

You may have notices the self parameter in function definition inside the class. But we called the method simply as ob.func() without any arguments. It still worked. This is because, whenever an object calls its method, the object itself is pass as the first argument. So, my_seq.reverse_comp() translates into Sequence.reverse_comp(my_seq). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument. For these reasons, the first argument of the function in class must be the object itself. This is conventionally called self. It can be named otherwise but we highly recommend to follow the convention.

11.1.6. Special methods

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define their own behavior with respect to language operators.

One of the biggest advantages of using Python’s magic methods is that they provide a simple way to make objects behave like built-in types. That means you can avoid ugly, counter-intuitive, and nonstandard ways of performing basic operators. In some languages, it’s common to do something like this:

if my_obj.equals(other_obj):
    # do something

You could certainly do this in Python, too, but this adds confusion and is unnecessarily verbose. Different libraries might use different names for the same operations, making the client do way more work than necessary. With the power of magic methods, however, we can define one method (__eq__, in this case), and say what we mean instead:

if instance == other_instance:
   #do something

The specials methods are defined by the language. They’re always surrounded by double underscores. There are a ton of special functions in Python.

11.1.6.1. Overloading the + Operator

To overload the + sign, we will need to implement the __add__ function in the class. With great power comes great responsibility. We can do whatever we like, inside this function. But it is sensible to return a Point object of the coordinate sum:

class Point:
    # previous definitions...

    def __add__(self,other):
        x = self.x + other.x
        y = self.y + other.y
        return Point(x,y)

Now let’s try that addition again:

>>> p1 = Point(2,3)
>>> p2 = Point(-1,2)
>>> print(p1 + p2)
(1,5)

11.1.6.2. Overloading Comparison Operators in Python

Python does not limit operator overloading to arithmetic operators only. We can overload comparison operators as well. Suppose, we wanted to implement the less than symbol < symbol in our Point class. Let us compare the magnitude of these points from the origin and return the result for this purpose. It can be implemented as follows:

class Point:
    # previous definitions...

    def __lt__(self,other):
        self_mag = (self.x ** 2) + (self.y ** 2)
        other_mag = (other.x ** 2) + (other.y ** 2)
        return self_mag < other_mag

Some sample runs:

>>> Point(1,1) < Point(-2,-3)
True
>>> Point(1,1) < Point(0.5,-0.2)
False
>>> Point(1,1) < Point(1,1)
False

http://www.programiz.com/python-programming/operator-overloading

11.1.6.3. Comparison magic methods

Python provide a set of special methods to compare object: to use >, >=, ==, !=, =<, <, you have to implements the comparisons special methods (__gt__, __ge__, __eq__, __neq__, __le__, __lt__) .

__eq__(self, other)
Defines behavior for the equality operator, ==.
__ne__(self, other)
Defines behavior for the inequality operator, !=.
__lt__(self, other)
Defines behavior for the less-than operator, <.
__gt__(self, other)
Defines behavior for the greater-than operator, >.
__le__(self, other)
Defines behavior for the less-than-or-equal-to operator, <=.
__ge__(self, other)
Defines behavior for the greater-than-or-equal-to operator, >=.

http://www.python-course.eu/python3_magic_methods.php

11.1.6.3.1. __init__ method

To create an object, two steps are necessary. First a raw or uninitialized object must be created, and then the object must be initialized, ready for use. Some object-oriented languages (such as C++ and Java) combine these two steps into one, but Python keeps them separate.

When an object is created (e.g., ecor_1 = Sequence('GAATTC'), first the special method __new__() is called to create the object, and then the special method __init__() is called implicitly to initialize it. In practice almost every Python class we create will require us to reimplement only the __init__() method, since default __new__() method is al- most always sufficient and is automatically called if we don’t provide our own __new__() method.

Although we can create an attribute in any method, it is a good practice to do this in the __init__ method. Thus, it is easy to know what attributes have an object without being to read the entire code of a class:

class Sequence:

      alphabet = 'ATGC'

   def __init__(self, name, seq):
      """
      :param seq: the sequence
      :type seq: string
      """
      self.name = name
      self.sequence = seq
      self.nucleic = True
      for char in self.sequence:
         if char not in self.alphabet:
            self.nucleic = False
            break

11.2. Namespace and attributes lookup

The LEGB rule (Local, Enclosing, Global, Built-in) still applied. But when a class is created a namespace is created. Furthermore, for each instance of this a class a new namespace corresponding to this instance is created. There exist a link between the namespace of the instance and the namespace of it’s corresponding class. For instance:

class namespace

when a class is created a namespace is created.

class Student:

    school = 'Pasteur'

    def __init__(self, name):
        self.name  = name
        self.scores = []

    def add_score(self, val):
        self.scores.append(val)

    def average(self):
        av = sum(self.scores)/len(self.scores)
        return av
class namespace
foo = Student('foo')

When an object is created, a namespace is created. This namespace is linked to its respective class namespace.

class namespace
foo = Student('foo')
bar = Student('bar')

Each object have it’s own namespace which are linked to the class namespace.

class namespace
foo.add_score(12)
bar.add_score(13)
foo.add_score(15)
foo.add_score(14)
bar.add_score(11)

During method execution a namespace is created which have a link to the object instance. This namespace is destroyed a the end of the method (return)

To see it in action, you can play with the code below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18

class TestEnv:

    class_att = 1

    def __init__(self):
        self.inst_att = 2

    def test(self):
        loc_var = 3
        print("locals:", locals())
        print("globals:", globals())
        print("my self:", self)
        print("my class:", self.__class__)

t = TestEnv()

t.test()

test_namespace.py

11.2.1. Control the access to the attributes

11.2.1.1. with underscore

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

11.3. Deleting object

Any attribute of an object can be deleted anytime, using the del statement. We can even delete the object itself, using the del statement.

Actually, it is more complicated than that. When we do s1 = Sequence('ecor1', 'GAATTC'), a new instance object is created in memory and the name s1 binds with it. On the command del s1, this binding is removed and the name s1 is deleted from the corresponding namespace. The object however continues to exist in memory and if no other name is bound to it, it is later automatically destroyed. This automatic destruction of unreferenced objects in Python is also called garbage collection.

11.4. Inheritance

In the introduction of I mentioned that one of the objectives of OOP is to address some of the issues of software quality.

What we have seen so far, object-based programming, consists in designing programs with objects, that are built with classes. In most object-oriented programming languages, you also have a mechanism that enables classes to share code. The idea is very simple: whenever there are some commonalities between classes, why having them repeat the same code, thus leading to maintenance complexity and potential inconsistency? So the principle of this mechanism is to define some classes as being the same as other ones, with some differences.

In the example below we have design two classes to represent to kind of sequences DNA or AA. As we can see the 2 kinds of sequence have the same attributes (name and sequence) have some common methods (len, to_fasta) but have also some specific methods gc_percent and molecular_weight.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class DNASequence(object):

    alphabet = 'ATGC'

    def __init__(self, name, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        self.name = name
        self.sequence = seq


    def __len__(self):
        return len(self.sequence)


    def to_fasta(self):
        id_ = self.name.replace(' ', '_') 
        fasta = '>{}\n'.format(id_)
        start = 0
        while start < len(self.sequence):
            end = start + 80
            fasta += self.sequence[start: end + 1] + '\n'
            start = end
        return fasta

    
    def gc_percent(self):
        return float(self.sequence.count('G') + self.sequence.count('C')) / len(self.sequence)


class AASequence(object):
   
    _water = 18.0153
    _weight_table = {'A': 89.0932, 'C': 121.1582, 'E': 147.1293,
                     'D': 133.1027, 'G': 75.0666, 'F': 165.1891,
                     'I': 131.1729, 'H': 155.1546, 'K': 146.1876,
                     'M': 149.2113, 'L': 131.1729, 'O': 255.3134,
                     'N': 132.1179, 'Q': 146.1445, 'P': 115.1305,
                     'S': 105.0926, 'R': 174.201, 'U': 168.0532,
                     'T': 119.1192, 'W': 204.2252, 'V': 117.1463,
                     'Y': 181.1885}

    def __init__(self, name, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        self.name = name
        self.sequence = seq


    def __len__(self):
        return len(self.sequence)


    def to_fasta(self):
        id_ = self.name.replace(' ', '_')
        fasta = '>{}\n'.format(id_)
        start = 0
        while start < len(self.sequence):
            end = start + 80
            fasta += self.sequence[start: end + 1] + '\n'
            start = end
        return fasta


    def molecular_weight(self):
        return sum([self._weight_table[aa] for aa in self.sequence]) - (len(self.sequence) - 1) * self._water

The problem with this implementation is that a large part of code is the same in the 2 classes. It’s bad because if I have to modify a part of common code I have to do it twice. If in future I’ll need a new type of Sequence as RNA sequence I will have to duplicate code again on so on. the code will be hard to maintain. I need to keep together the common code, and be able to specify only what is specific for each type of Sequences

So we keep our two classes to deal with DNA and protein sequences, and we add a new class: Sequence, which will be a common class to deal with general sequence functions. In order to specify that a DNA (or a Protein) is a Sequence in Python is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Sequence(object):

    def __init__(self, name, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        self.name = name
        self.sequence = seq

    def __len__(self):
        return len(self.sequence)

    def to_fasta(self):
        id_ = self.name.replace(' ', '_') 
        fasta = '>{}\n'.format(id_)
        start = 0
        while start < len(self.sequence):
            end = start + 80
            fasta += self.sequence[start: end + 1] + '\n'
            start = end
        return fasta


class DNASequence(Sequence):

    alphabet = 'ATGC'

    def gc_percent(self):
        return float(self.sequence.count('G') + self.sequence.count('C')) / len(self.sequence)


class AASequence(Sequence):

    _water = 18.0153
    _weight_table = {'A': 89.0932, 'C': 121.1582, 'E': 147.1293, 
                     'D': 133.1027, 'G': 75.0666, 'F': 165.1891, 
                     'I': 131.1729, 'H': 155.1546, 'K': 146.1876, 
                     'M': 149.2113, 'L': 131.1729, 'O': 255.3134, 
                     'N': 132.1179, 'Q': 146.1445, 'P': 115.1305, 
                     'S': 105.0926, 'R': 174.201, 'U': 168.0532, 
                     'T': 119.1192, 'W': 204.2252, 'V': 117.1463, 
                     'Y': 181.1885}

    def molecular_weight(self):
        return sum([self._weight_table[aa] for aa in self.sequence]) - (len(self.sequence) - 1) * self._water
inheritance namespace
_images/spacer.png
  • Each object have it’s own namespace which are linked to the class namespace via the special attribute: __class__
  • Each class is link to it’s parents namespace via the special attribute __bases__ on so on until the object class namespace
pep_1 = AASequence('pep_1', 'GIVQE')
bar = DNASequence('Ecor I', 'GAATTC')
_images/spacer.png

11.4.1. Overloading

Overloading an attribute or a method is to redefine at a subclass level an attribute or method that exists in upper classes of a class hierarchy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Sequence(object):

    _water = 18.0153

    def __init__(self, name, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        self.name = name
        self.sequence = seq

    def __len__(self):
        return len(self.sequence)

    def to_fasta(self):
        id_ = self.name.replace(' ', '_') 
        fasta = '>{}\n'.format(id_)
        start = 0
        while start < len(self.sequence):
            end = start + 80
            fasta += self.sequence[start: end + 1] + '\n'
            start = end
        return fasta

    def molecular_weight(self):
        return  sum([self._weight_table[x] for x in self.sequence]) - (len(self.sequence) - 1) * self._water


class DNASequence(Sequence):

    alphabet = 'ATGC'
    _weight_table = {'A': 331.2218, 'C': 307.1971, 'G': 347.2212, 'T': 322.2085}
    
    def gc_percent(self):
        return float(self.sequence.count('G') + self.sequence.count('C')) / len(self.sequence)

    def _one_strand_molec_weight(self, seq):
        return sum([self._weight_table[base] for base in seq]) - (len(seq) - 1) * self._water

    def molecular_weight(self):
        direct_weight = self._one_strand_molec_weight(self.sequence)
        rev_comp  = self.rev_comp()
        rev_comp_weight = self._one_strand_molec_weight(rev_comp.sequence)
        return  direct_weight + rev_comp_weight


class RNASequence(Sequence)

    alphabet = 'AUGC'
    _weight_table = {'A': 347.2212, 'C': 323.1965, 'G': 363.2206, 'U': 324.1813}


class AASequence(Sequence):
   

    _weight_table = {'A': 89.0932, 'C': 121.1582, 'E': 147.1293, 
                     'D': 133.1027, 'G': 75.0666, 'F': 165.1891, 
                     'I': 131.1729, 'H': 155.1546, 'K': 146.1876, 
                     'M': 149.2113, 'L': 131.1729, 'O': 255.3134, 
                     'N': 132.1179, 'Q': 146.1445, 'P': 115.1305, 
                     'S': 105.0926, 'R': 174.201, 'U': 168.0532, 
                     'T': 119.1192, 'W': 204.2252, 'V': 117.1463, 
                     'Y': 181.1885}

We we overload a method sometimes we just want to add something to the parent’s method. in this case we can call explicitly the parent’s method by using the keywords super. The syntax of this method is lit bit tricky. the first argument must be the class that you want to retrieve the parent (usually the class you are coding), the second argument is the object you want to retrieve the parent class (usual self) and it return a proxy to the parent so you just have to call the method. see it in action, in the example below we overload the __init__ method and just add 2 attribute but for the name and sequence we call the Sequence __init__ method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

class Sequence(object):

    _water = 18.0153

    def __init__(self, name, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        self.name = name
        self.sequence = seq

    def __len__(self):
        return len(self.sequence)

    def to_fasta(self):
        id_ = self.name.replace(' ', '_') 
        fasta = '>{}\n'.format(id_)
        start = 0
        while start < len(self.sequence):
            end = start + 80
            fasta += self.sequence[start: end + 1] + '\n'
            start = end
        return fasta

    def molecular_weight(self):
        return sum([self._weight_table[x] for x in self.sequence]) - (len(self.sequence) - 1) * self._water


class RestrictionEnzyme(Sequence):

    def __init__(self, name, seq, cut, end):
        # the line below is in python3 only
        super().__init__(name, seq)
        # in python2 the syntax is
        # super(RestrictionEnzyme, self).__init__(name, seq)
        # this syntax is also available in python3
        self.cut = cut
        self.end = end


ecor1 = DNASequence('ecor I', 'GAATTC', 1, "sticky")
print(ecor1.name, len(ecor1))

In python3 the syntax has been simplified. we can just call super() that’s all.

11.5. Polymorphism

The term polymorphism, in the OOP lingo, refers to the ability of an object to adapt the code to the type of the data it is processing.

Polymorphism has two major applications in an OOP language. The first is that an object may provide different implementations of one of its methods depending on the type of the input parameters. The second is that code written for a given type of data may be used on other data with another datatype as long as the other data have compatible behavior.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def my_sum(a, b):
    return a + b


print("my_sum(3, 4) =", my_sum(3, 4))
print("my_sum('three', 'four') =", my_sum('three', 'four'))


class Sequence(object):

    _water = 18.0153

    def __init__(self, name, seq):
        """
        :param seq: the sequence
        :type seq: string
        """
        self.name = name
        self.sequence = seq

    def __len__(self):
        return len(self.sequence)

    def __str__(self):
        return ">{}\n{}".format(self.name, self.sequence)

    def __add__(self, other):
        return Sequence('{}/{}'.format(self.name, other.name),
                        self.sequence + other.sequence)


ecor_1 = Sequence('Ecor_I', 'GAATTC')
bamh_1 = Sequence('Bamh I', 'GGATCC')

print("my_sum(ecor_1, bamh_1) =", my_sum(ecor_1, bamh_1))

Albeit data are type, my method my_sum work equally on different type as integer, string or sequence. The my_sum method is called polymorph.

11.6. Exercises

Modeling a sequence with few attributes and methods

11.6.1. Exercise

Instantiate 2 sequences using your Sequence class, and draw a schema representing the namespaces.

11.6.2. Exercise

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MyClass(object):

    class_attr = 'foo'

    def __init__(self, val):
        self.inst_attr = val




a = MyClass(1)
b = MyClass(2)

print a.inst_attr

print b.inst_attr


print a.class_attr == b.class_attr

print a.class_attr is b.class_attr


b.class_attr = 4

print a.class_attr

Can you explain this result (draw namespaces to explain) ? how to modify the class variable class_attr

11.6.3. Exercise

Write the definition of a Point class. Objects from this class should have a

  • a method show to display the coordinates of the point
  • a method move to change these coordinates.
  • a method dist that computes the distance between 2 points.

Note

the distance between 2 points A(x0, y0) and B(x1, y1) can be compute

\[d(AB) = \sqrt{(x1-x0))^2 + (y1-y0)^2}\]

(http://www.mathwarehouse.com/algebra/distance_formula/index.php)

The following Python code provides an example of the expected behaviour of objects belonging to this class:

>>> p1 = Point(2, 3)
>>> p2 = Point(3, 3)
>>> p1.show()
(2, 3)
>>> p2.show()
(3, 3)
>>> p1.move(10, -10)
>>> p1.show()
(12, -7)
>>> p2.show()
(3, 3)
>>> p1.dist(p2)
1.0

11.6.4. Exercise

Use biopython to read a fasta file (sv40.fasta) and display the following attributes:

  • id
  • name
  • description
  • seq

Use the module SeqIO from biopython. A tutorial is available here: https://biopython.org/wiki/SeqIO

Biopython is not part of the default Python distribution. You will likely need to install it:

$ pip install biopython

Note

The “$” character is here to indicate that the above command is to be typed in a command-line interface (shell). Do not type the “$” itself.

11.6.5. Exercise

Translate the sequence in phase 1, 2, -2

11.6.6. Exercise

  • Create a sequence with the first 42 nucleotides
  • Translate this sequence
  • Mutate the nucleotide in position 18 ‘A’ -> ‘C’
  • and translate the mutated sequence

see tutorial http://biopython.org/DIST/docs/tutorial/Tutorial.html#htoc28

11.6.7. Exercise

Open the file abcd.fasta (abcd.fasta) and convert it in genbank format.

Hint: The seq alphabet attribute must be set to extended_protein (see the Bio.Alphabet.IUPAC module).

11.6.8. Exercice

Open the file abcd.fasta (abcd.fasta) and filter out sequences of lenght <= 700. Write the results in a fasta file.

11.6.9. Exercise

Use OOP to model restriction enzymes and sequences.

The sequence must implement the following methods:

  • enzyme_filter which takes a list of enzymes as argument and returns a new list containing the enzymes which have binding site in the sequence

The restriction enzyme must implement the following methods:

  • binds which takes a sequence as argument and returns True if the sequence contains a binding site, False otherwise.

Solve the exercise Exercise using this new implementation.

11.6.10. Exercise

Refactor your code of Exercise in OOP style programming. Implement only:

  • size: return the number of rows, and number of columns
  • get_cell: that take the number of rows, the number of columns as parameters, and returns the content of cell corresponding to row number col number
  • set_cell: that take the number of rows, the number of columns as parameters, and a value and set the value val in cell specified by row number x column number
  • to_str: return a string representation of the matrix
  • mult: that take a scalar and return a new matrix which is the scalar product of matrix x val

You can change the name of the methods to be more “pythonic”.

11.6.11. Exercise

Use the code to read multiple sequences fasta file in procedural style and refactor it in OOP style. Use the file abcd.fasta to test your code.

What is the benefit to use OOP style instead of procedural style?