Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ep_merger_mask.py
diff options
context:
space:
mode:
Diffstat (limited to 'ep_merger_mask.py')
-rw-r--r--ep_merger_mask.py131
1 files changed, 131 insertions, 0 deletions
diff --git a/ep_merger_mask.py b/ep_merger_mask.py
new file mode 100644
index 0000000..910ab4d
--- /dev/null
+++ b/ep_merger_mask.py
@@ -0,0 +1,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