Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/exon_direction.py
blob: efd435ff96a932fcc970126493cfcc4777ec5b77 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# coding: UTF8
# Copyright 2009 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import random
import math
import cairo
import ka_extensionpoint
import ka_random
import model_constraintpool
import model_allele

EPSILON = 0.00001
DIRECTION_CONSTRAINT = 'directionconstraint'

class Direction(model_allele.Allele):
    """Direction
    """

    cdef = [{'bind'  : DIRECTION_CONSTRAINT,
             'name'  : 'Direction constraint',
             'domain': model_constraintpool.STRING_1_OF_N,
             'enum'  : ka_extensionpoint.list_extensions(DIRECTION_CONSTRAINT)
            },
           ]

    def __init__(self, trunk, radian, offset):
        """Direction constructor
        """
        super(Direction, self).__init__(trunk)
        cpool = model_constraintpool.ConstraintPool.get_pool()
        constraint_name = cpool.get(self, DIRECTION_CONSTRAINT)[0]
        self.constraint = ka_extensionpoint.create(constraint_name, self.path)
        self.radian, self.offset = self.constraint.filter(radian, offset)
        
    def __eq__(self, other):
        """Equality based on radian and offset."""
        return isinstance(other, Direction) \
               and abs(self.radian - other.radian) < EPSILON \
               and abs(self.offset - other.offset) < EPSILON

#    def __ne__(self, other):
#        return not self.__eq__(other)
#
#    def __hash__(self):
#        raise TypeError("Genome objects are unhashable")

    def randomize(self):
        """Set radian and offset to random values."""
        cpool = model_constraintpool.ConstraintPool.get_pool()
        constraints = cpool.get(self, DIRECTION_CONSTRAINT)
        self.constraint = ka_extensionpoint.create(random.choice(constraints), self.path)
        self.radian, self.offset = self.constraint.randomize()

    def mutate(self):
        """Make small random changes in radian and offset."""
        if ka_random.is_mutating():
            if ka_random.is_mutating():
                cpool = model_constraintpool.ConstraintPool.get_pool()
                constraints = cpool.get(self, DIRECTION_CONSTRAINT)
                self.constraint = ka_extensionpoint.create(random.choice(constraints), self.path)
            self.radian, self.offset = self.constraint.mutate(self.radian, self.offset)

    def shuffle(self):
        """Not implemented."""
        pass

    def crossingover(self, other):
        """Returns either a copy of self or a copy of other.
        pre: isinstance(other, Direction)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        post: __return__ is not other
        """
        randseq = ka_random.crossing_sequence(1)
        return self.copy() if randseq[0] else other.copy()

    def copy(self):
        """A direction copy constructor
        post: isinstance(__return__, Direction)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        """
        new_one = Direction(self.get_trunk(), 0.0, 0.0)
        new_one.radian, new_one.offset = self.radian, self.offset
        new_one.constraint = self.constraint
        return new_one

    def explain(self):
        return u'%4.3f, %4.3f' % (self.radian, self.offset)

    @staticmethod
    def make_icon(direction_list, width, height):
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
        ctx = cairo.Context(surface)
        ctx.scale(width, height)
        # paint background
        ctx.set_operator(cairo.OPERATOR_OVER)
        ctx.set_source_rgb(1.0, 1.0, 1.0)
        ctx.paint()
        ctx.set_line_width(0.02)
        for direction in direction_list:
            # paint a arrow for each direction
            dx = direction.offset * math.cos(direction.radian)
            dy = direction.offset * math.sin(direction.radian)
            ctx.set_source_rgb(0.0, 0.0, 0.0)
            ctx.move_to(0.5, 0.5)
            ctx.line_to(0.5+dx, 0.5+dy)
            ctx.stroke()
        return surface