Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_merger_mask.py
blob: 910ab4df2e9fe285c67a977d97ac5e3e5b5d53ef (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
124
125
126
127
128
129
130
131
# 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 math
import random
import cairo
import ka_random
import model_allele
import exon_position
import exon_direction

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

    def __init__(self, trunk):
        """Constructor for a simple merger."""
        super(MaskMerger, self).__init__(trunk)
        self.alfa1 = 1.0
        self.alfa2 = 0.0
        self.center = exon_position.Position(self.path, 0.0, 0.0)
        self.direction = exon_direction.Direction(self.path, 0.0, 0.0)
        self.constraint_alfa = (0.0, 1.0)

    def __eq__(self, other):
        """Equality based on same instance."""
        equal = isinstance(other, MaskMerger) \
                and self.alfa1 == other.alfa1 \
                and self.alfa2 == other.alfa2 \
                and self.center == other.center \
                and self.direction == other.direction
        return equal

    def randomize(self):
        """
        """
        self.alfa1 = random.uniform(self.constraint_alfa[0], self.constraint_alfa[1])
        self.alfa2 = random.uniform(self.constraint_alfa[0], self.constraint_alfa[1])
        self.center.randomize()
        self.direction.randomize()

    def mutate(self):
        """
        """
        if ka_random.is_mutating():
            self.alfa1 += ka_random.jitter_constrained(self.constraint_alfa)
            self.alfa1 = ka_random.limitate_range(self.alfa1, self.constraint_alfa[0], self.constraint_alfa[1])
        if ka_random.is_mutating():
            self.alfa2 += ka_random.jitter_constrained(self.constraint_alfa)
            self.alfa2 = ka_random.limitate_range(self.alfa2, self.constraint_alfa[0], self.constraint_alfa[1])
        self.center.mutate()
        self.direction.mutate()

    def shuffle(self):
        """
        """
        pass

    def crossingover(self, other):
        """
        pre: isinstance(other, MaskMerger)
        pre: isinstance(self, MaskMerger)
        # check for distinct references, needs to copy content, not references
        post: __return__ is not self
        post: __return__ is not other
        """
        new_one = MaskMerger(self.get_trunk())
        crossing = ka_random.crossing_sequence(2)
        new_one.alfa1 = self.alfa1 if crossing[0] else other.alfa1
        new_one.alfa2 = self.alfa2 if crossing[0] else other.alfa2
        new_one.center = self.center if crossing[0] else other.center
        new_one.direction = self.direction if crossing[1] else other.direction
        return new_one

    def draw_single_layer(self, single_layer, mask_layer, ctx, width, height):
        """
        pre: single_layer is not None
        pre: ctx is not None
        pre: width > 0
        pre: height > 0
        pre: width == height
        """
        # paint one layer masked by an linear gradient
        ctx.save()

        dx = self.direction.offset * math.cos(self.direction.radian)
        dy = self.direction.offset * math.sin(self.direction.radian)
        linear = cairo.LinearGradient(self.center.x_pos + dx,
                                      self.center.y_pos + dy,
                                      self.center.x_pos - dx,
                                      self.center.y_pos - dy)
        linear.add_color_stop_rgba(1.0, 1.0, 1.0, 1.0, self.alfa1)
        linear.add_color_stop_rgba(0.0, 0.0, 0.0, 0.0, self.alfa2)
        ctx.mask(linear)
     
        single_layer.draw(ctx, width, height)
        ctx.restore()

    def explain(self):
        """
        post len(__return__) == 3
        """
        return u'Mask merger, center=' + self.center.explain() \
               + u' direction=' + self.direction.explain(), \
               None, \
               None

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