Skip to content

Commit

Permalink
unite result classes to Design_Signle and Design_Plate
Browse files Browse the repository at this point in the history
  • Loading branch information
t47io committed Mar 16, 2016
1 parent f4d561d commit 3f30afe
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 91 deletions.
6 changes: 3 additions & 3 deletions primerize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .thermo import Nearest_Neighbor
from .util import Assembly, Plate_96Well, Mutation, Construct_List
from .primerize_1d import Primerize_1D
from .primerize_2d import Primerize_2D
from .primerize_1d import Primerize_1D, Design_Single
from .primerize_2d import Primerize_2D, Design_Plate
from .primerize_3d import Primerize_3D

__version__ = '1.2.0'
__version__ = '1.2.1'
10 changes: 6 additions & 4 deletions primerize/primerize_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from .util import *


class Design_1D(object):
def __init__(self, sequence, name, is_success, primer_set, params, data):
(self.sequence, self.name, self.is_success, self.primer_set, self._params, self._data) = (sequence, name, is_success, primer_set, params, data)
class Design_Single(object):
def __init__(self, init_dict):
for key in init_dict:
key_rename = '_' + key if key in ['params', 'data'] else key
setattr(self, key_rename, init_dict[key])

def __repr__(self):
return '\033[94m%s\033[0m {\n\033[95msequence\033[0m = \'%s\', \n\033[95mname\033[0m = \'%s\', \n\033[95mis_success\033[0m = \033[41m%s\033[0m, \n\033[95mprimer_set\033[0m = %s, \n\033[95mparams\033[0m = %s, \n\033[95mdata\033[0m = {\n \033[92m\'misprime_score\'\033[0m: %s, \n \033[92m\'assembly\'\033[0m: %s, \n \033[92m\'warnings\'\033[0m: %s\n}' % (self.__class__, self.sequence, self.name, self.is_success, repr(self.primer_set), repr(self._params), repr(self._data['misprime_score']), repr(self._data['assembly']), repr(self._data['warnings']))
Expand Down Expand Up @@ -208,7 +210,7 @@ def design(self, sequence, MIN_TM=None, NUM_PRIMERS=None, MIN_LENGTH=None, MAX_L

params = {'MIN_TM': MIN_TM, 'NUM_PRIMERS': NUM_PRIMERS, 'MIN_LENGTH': MIN_LENGTH, 'MAX_LENGTH': MAX_LENGTH, 'N_BP': N_BP, 'COL_SIZE': self.COL_SIZE, 'WARN_CUTOFF': self.WARN_CUTOFF}
data = {'misprime_score': misprime_score, 'assembly': assembly, 'warnings': warnings}
return Design_1D(sequence, name, is_success, primer_set, params, data)
return Design_Single({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'params': params, 'data': data})



Expand Down
31 changes: 17 additions & 14 deletions primerize/primerize_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

if __package__ is None or not __package__:
from util import *
from primerize_1d import Primerize_1D, Design_1D
from primerize_1d import Primerize_1D, Design_Single
else:
from .util import *
from .primerize_1d import Primerize_1D, Design_1D
from .primerize_1d import Primerize_1D, Design_Single


class Design_2D(object):
def __init__(self, sequence, name, is_success, primer_set, params, data):
(self.sequence, self.name, self.is_success, self.primer_set, self._params, self._data) = (sequence, name, is_success, primer_set, params, data)
self._data['illustration'] = draw_region(self.sequence, self._params)
class Design_Plate(object):
def __init__(self, init_dict):
for key in init_dict:
key_rename = '_' + key if key in ['params', 'data'] else key
setattr(self, key_rename, init_dict[key])
if self.get('TYPE') == 'Mutate-and-Map':
self._data['illustration'] = draw_region(self.sequence, self._params)

def __repr__(self):
return '\033[94m%s\033[0m {\n\033[95msequence\033[0m = \'%s\', \n\033[95mname\033[0m = \'%s\', \n\033[95mis_success\033[0m = \033[41m%s\033[0m, \n\033[95mprimer_set\033[0m = %s, \n\033[95mparams\033[0m = %s, \n\033[95mdata\033[0m = {\n \033[92m\'constructs\'\033[0m: %s, \n \033[92m\'assembly\'\033[0m: %s, \n \033[92m\'plates\'\033[0m: %s\n}' % (self.__class__, self.sequence, self.name, self.is_success, repr(self.primer_set), repr(self._params), repr(self._data['constructs']), repr(self._data['assembly']), repr(self._data['plates']))
Expand Down Expand Up @@ -71,7 +74,7 @@ def echo(self, key=''):
return output[:-1]
elif key == 'assembly':
return self._data['assembly'].echo()
elif key == 'region':
elif key == 'region' and self.get('TYPE') == 'Mutate-and-Map':
return '\n'.join(self._data['illustration']['lines'])

elif not key:
Expand Down Expand Up @@ -136,7 +139,7 @@ def reset(self):


def design(self, sequence, primer_set=[], offset=None, which_muts=None, which_lib=None, prefix=None, is_force=False):
if isinstance(sequence, Design_1D):
if isinstance(sequence, Design_Single):
design_1d = sequence
sequence = design_1d.sequence
primer_set = design_1d.primer_set
Expand Down Expand Up @@ -172,9 +175,9 @@ def design(self, sequence, primer_set=[], offset=None, which_muts=None, which_li
is_success = False

if not is_success:
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_BP': N_BP}
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_BP': N_BP, 'type': 'Mutate-and-Map'}
data = {'plates': [], 'assembly': [], 'constructs': []}
return Design_2D(sequence, name, is_success, primer_set, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'params': params, 'data': data})

if not which_muts:
which_muts = list(range(1 - offset, N_BP + 1 - offset))
Expand All @@ -189,9 +192,9 @@ def design(self, sequence, primer_set=[], offset=None, which_muts=None, which_li
(primers, is_success) = get_primer_index(primer_set, sequence)
if not is_success:
print('\033[41mFAIL\033[0m: \033[91mMismatch\033[0m of given \033[92mprimer_set\033[0m for given \033[92msequence\033[0m.\n')
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_PRIMER': N_primers, 'N_PLATE': N_plates, 'N_CONSTRUCT': N_constructs, 'N_BP': N_BP}
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_PRIMER': N_primers, 'N_PLATE': N_plates, 'N_CONSTRUCT': N_constructs, 'N_BP': N_BP, 'type': 'Mutate-and-Map'}
data = {'plates': [], 'assembly': [], 'constructs': []}
return Design_2D(sequence, name, is_success, primer_set, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'params': params, 'data': data})

assembly = Assembly(sequence, primers, name, self.COL_SIZE)
constructs = Construct_List()
Expand All @@ -212,9 +215,9 @@ def design(self, sequence, primer_set=[], offset=None, which_muts=None, which_li
print(traceback.format_exc())
print('\033[41mERROR\033[0m: Primerize 2D design() encountered error.\n')

params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_PRIMER': N_primers, 'N_PLATE': N_plates, 'N_CONSTRUCT': N_constructs, 'N_BP': N_BP}
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_PRIMER': N_primers, 'N_PLATE': N_plates, 'N_CONSTRUCT': N_constructs, 'N_BP': N_BP, 'type': 'Mutate-and-Map'}
data = {'plates': plates, 'assembly': assembly, 'constructs': constructs}
return Design_2D(sequence, name, is_success, primer_set, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'params': params, 'data': data})



Expand Down
83 changes: 13 additions & 70 deletions primerize/primerize_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,12 @@

if __package__ is None or not __package__:
from util import *
from primerize_1d import Primerize_1D, Design_1D
from primerize_1d import Primerize_1D, Design_Single
from primerize_2d import Design_Plate
else:
from .util import *
from .primerize_1d import Primerize_1D, Design_1D


class Design_3D(object):
def __init__(self, sequence, name, is_success, primer_set, structures, params, data):
(self.sequence, self.name, self.is_success, self.primer_set, self.structures, self._params, self._data) = (sequence, name, is_success, primer_set, structures, params, data)

def __repr__(self):
pass

def __str__(self):
return self.echo()


def get(self, key):
pass


def save(self, key='', path='./', name=None):
if self.is_success:
if name is None: name = self.name
key = key.lower()
if key == 'table':
save_plates_excel(self._data['plates'], self._params['N_PLATE'], self._params['N_PRIMER'], name, path)
elif key == 'image':
save_plate_layout(self._data['plates'], self._params['N_PLATE'], self._params['N_PRIMER'], name, path)
elif key == 'construct':
save_construct_key(self._data['constructs'], name, path, self._params['which_lib'])
elif key == 'assembly':
self._data['assembly'].save(path, name)

elif not key:
for key in ['table', 'image', 'construct', 'assembly']:
self.save(key, path, name)
else:
raise AttributeError('\033[41mERROR\033[0m: Unrecognized key \033[92m%s\033[0m for \033[94m%s.save()\033[0m.\n' % (key, self.__class__))
else:
raise UnboundLocalError('\033[41mFAIL\033[0m: Result of key \033[92m%s\033[0m unavailable for \033[94m%s\033[0m where \033[94mis_cucess\033[0m = \033[41mFalse\033[0m.\n' % (key, self.__class__))


def echo(self, key=''):
if self.is_success:
key = key.lower()
if key == 'plate':
output = ''
for i in range(len(self._data['plates'][0])):
for j in range(len(self._data['plates'])):
output += 'Plate \033[95m%d\033[0m; Primer \033[92m%d\033[0m\n' % (i + 1, j + 1)
output += self._data['plates'][j][i].echo(self.primer_set[j])
return output[:-1]
elif key == 'assembly':
return self._data['assembly'].echo()

elif not key:
return self.echo('assembly') + '\n\n' + self.echo('plate')
else:
raise AttributeError('\033[41mERROR\033[0m: Unrecognized key \033[92m%s\033[0m for \033[94m%s.echo()\033[0m.\n' % (key, self.__class__))
else:
raise UnboundLocalError('\033[41mFAIL\033[0m: Result of key \033[92m%s\033[0m unavailable for \033[94m%s\033[0m where \033[94mis_cucess\033[0m = \033[41mFalse\033[0m.\n' % (key, self.__class__))

from .primerize_1d import Primerize_1D, Design_Single
from .primerize_2d import Design_Plate


class Primerize_3D(object):
Expand Down Expand Up @@ -135,7 +78,7 @@ def reset(self):


def design(self, sequence, primer_set=[], offset=None, structures=[], N_mutations=None, which_lib=None, which_muts=[], prefix=None, is_single=None, is_fillWT=False, is_force=False):
if isinstance(sequence, Design_1D):
if isinstance(sequence, Design_Single):
design_1d = sequence
sequence = design_1d.sequence
primer_set = design_1d.primer_set
Expand Down Expand Up @@ -176,9 +119,9 @@ def design(self, sequence, primer_set=[], offset=None, structures=[], N_mutation
is_success = False

if not is_success:
params = {'offset': offset, 'N_mutations': N_mutations, 'which_lib': which_lib, 'N_BP': N_BP}
params = {'offset': offset, 'N_mutations': N_mutations, 'which_lib': which_lib, 'N_BP': N_BP, 'type': 'Mutation/Rescue'}
data = {'plates': [], 'assembly': [], 'constructs': []}
return Design_3D(sequence, name, is_success, primer_set, structures, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'structures': structures, 'params': params, 'data': data})

if not which_muts:
which_muts = list(range(1 - offset, N_BP + 1 - offset))
Expand All @@ -190,9 +133,9 @@ def design(self, sequence, primer_set=[], offset=None, structures=[], N_mutation
(primers, is_success) = get_primer_index(primer_set, sequence)
if not is_success:
print('\033[41mFAIL\033[0m: \033[91mMismatch\033[0m of given \033[92mprimer_set\033[0m for given \033[92msequence\033[0m.\n')
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_mutations': N_mutations, 'is_single': is_single, 'N_PRIMER': N_primers, 'N_BP': N_BP}
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_mutations': N_mutations, 'is_single': is_single, 'N_PRIMER': N_primers, 'N_BP': N_BP, 'type': 'Mutation/Rescue'}
data = {'plates': [], 'assembly': [], 'constructs': []}
return Design_3D(sequence, name, is_success, primer_set, structures, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'structures': structures, 'params': params, 'data': data})

assembly = Assembly(sequence, primers, name, self.COL_SIZE)
constructs = Construct_List()
Expand All @@ -203,9 +146,9 @@ def design(self, sequence, primer_set=[], offset=None, structures=[], N_mutation
bps.remove(pair)
if not bps:
print('\033[41mFAIL\033[0m: \033[91mNo\033[0m base-pairs exist within given \033[92mstructures\033[0m and \033[92mwhich_muts\033[0m.\n')
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_mutations': N_mutations, 'is_single': is_single, 'N_PRIMER': N_primers, 'N_BP': N_BP}
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_mutations': N_mutations, 'is_single': is_single, 'N_PRIMER': N_primers, 'N_BP': N_BP, 'type': 'Mutation/Rescue'}
data = {'plates': [], 'assembly': assembly, 'constructs': constructs}
return Design_3D(sequence, name, is_success, primer_set, structures, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'structures': structures, 'params': params, 'data': data})


N_constructs = (len(bps) - N_mutations + 1) * (is_single * 2 + 1) + 1
Expand Down Expand Up @@ -240,8 +183,8 @@ def design(self, sequence, primer_set=[], offset=None, structures=[], N_mutation
print(traceback.format_exc())
print('\033[41mERROR\033[0m: Primerize 3D design() encountered error.\n')

params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_mutations': N_mutations, 'is_single': is_single, 'N_PRIMER': N_primers, 'N_PLATE': N_plates, 'N_CONSTRUCT': N_constructs, 'N_BP': N_BP}
params = {'offset': offset, 'which_muts': which_muts, 'which_lib': which_lib, 'N_mutations': N_mutations, 'is_single': is_single, 'N_PRIMER': N_primers, 'N_PLATE': N_plates, 'N_CONSTRUCT': N_constructs, 'N_BP': N_BP, 'type': 'Mutation/Rescue'}
data = {'plates': plates, 'assembly': assembly, 'constructs': constructs}
return Design_3D(sequence, name, is_success, primer_set, structures, params, data)
return Design_Plate({'sequence': sequence, 'name': name, 'is_success': is_success, 'primer_set': primer_set, 'structures': structures, 'params': params, 'data': data})


3 changes: 3 additions & 0 deletions primerize/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,6 @@ def mutate_primers(plates, primers, primer_set, offset, constructs, which_lib=1,
plates[p][plate_num].set(well_tag, well_name, mut_primer)

return plates



0 comments on commit 3f30afe

Please sign in to comment.