-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution.py
98 lines (80 loc) · 3.97 KB
/
Solution.py
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
'''
Created on Nov 1, 2011
@author: jcg
'''
import sys
from random import choice
from Functions import randomMutationOperator
from uuid import uuid4
class Solution:
'''
A Solution encapsulates a sequence and their inherent attributes:
sol_id - ID for Solution
seqence - sequence for Solution
cds_region - a tuple indicating the location of (Begin,End) of CDS sequence (this will be necessary in the design mode if one want to contrain mutations).
mutable_region - a list with all positions that can be mutated
parent - Solution from which the current Solution was derived
'''
def __init__(self, sol_id = 0, sequence="", cds_region = None, keep_aa = False, mutable_region = None, parent=None, design = None):
if sequence == None:
sys.stderr.write("Tried to create a solution with sequence NULL\n")
self.sequence = None
return None
#check if solution is in DB
self.mutable_region = mutable_region
self.cds_region = cds_region
self.keep_aa = keep_aa
self.solid = sol_id
self.parent = parent
self.sequence = sequence.lower()
self.scores = {}
self.levels = {}
self.features = {}
self.designMethod = design
self.valid = True
def add_feature(self,feature):
featureLabel=feature.label+feature.__class__.__name__
if not self.features.has_key(featureLabel):
self.features[featureLabel] = feature
#update scores
self.scores.update(feature.scores)
#update levels
if feature.level != None:
self.levels[featureLabel+"Level"] = feature.level
for subfeature in feature.subfeatures.values():
self.add_feature(subfeature)
else:
sys.stderr.write("Feature label already exists!")
return
def checkSolution(self, desiredSolution):
if desiredSolution == None:
return False
same = True
for feature in self.designMethod.features.keys():
key = feature+"Level"
same = same & (desiredSolution[key]==0 or desiredSolution[key]==self.levels[key])
return same
def mutate(self,desiredSolution=None,random=False):
if desiredSolution==None or random or self.designMethod.listDesigns == [] or self.features == {}:
return self.randomMutation()
else:
# get features with targets
mutable = []
for feature in self.features.values():
if feature.defineTarget(desiredSolution):
mutable.append(feature)
if mutable == []:
return None
rm = choice(mutable)
#tomutatefeatures = [k.label+k.__class__.__name__ for k in mutable]
#print tomutatefeatures
#print [self.scores[k] for k in tomutatefeatures]
#print [self.levels[k+"Level"] for k in tomutatefeatures]
#print [desiredSolution[k+"Level"] for k in tomutatefeatures]
#print "mutating... " + rm.label+rm.__class__.__name__
return rm.mutate()
#return choice(mutable).randomMutation()
#return self.randomMutation()
def randomMutation(self,pos=None,n_mut=[1,2]):
new_seq = randomMutationOperator(self.sequence, self.keep_aa, self.mutable_region, self.cds_region, pos,n_mut)
return Solution(sol_id=str(uuid4().int), sequence=new_seq, cds_region = self.cds_region, keep_aa = self.keep_aa, mutable_region = self.mutable_region, parent=self, design=self.designMethod)