From 3f30afe0fc734e99953bc088dfeb99991688d3e3 Mon Sep 17 00:00:00 2001 From: T47 Date: Tue, 15 Mar 2016 22:30:04 -0700 Subject: [PATCH] unite result classes to Design_Signle and Design_Plate --- primerize/__init__.py | 6 +-- primerize/primerize_1d.py | 10 +++-- primerize/primerize_2d.py | 31 ++++++++------- primerize/primerize_3d.py | 83 ++++++--------------------------------- primerize/util.py | 3 ++ 5 files changed, 42 insertions(+), 91 deletions(-) diff --git a/primerize/__init__.py b/primerize/__init__.py index 5e51954..94129ef 100644 --- a/primerize/__init__.py +++ b/primerize/__init__.py @@ -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' diff --git a/primerize/primerize_1d.py b/primerize/primerize_1d.py index 4233c17..8f9b90c 100644 --- a/primerize/primerize_1d.py +++ b/primerize/primerize_1d.py @@ -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'])) @@ -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}) diff --git a/primerize/primerize_2d.py b/primerize/primerize_2d.py index e9ecef6..c121bd9 100644 --- a/primerize/primerize_2d.py +++ b/primerize/primerize_2d.py @@ -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'])) @@ -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: @@ -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 @@ -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)) @@ -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() @@ -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}) diff --git a/primerize/primerize_3d.py b/primerize/primerize_3d.py index a0653c0..02fa634 100644 --- a/primerize/primerize_3d.py +++ b/primerize/primerize_3d.py @@ -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): @@ -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 @@ -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)) @@ -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() @@ -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 @@ -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}) diff --git a/primerize/util.py b/primerize/util.py index 95e8c6c..943db8c 100644 --- a/primerize/util.py +++ b/primerize/util.py @@ -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 + + +