class BeadCalibrationPluginOp(PluginOpMixin, BeadCalibrationOp): handler_factory = Callable(BeadCalibrationHandler) beads_name = Str(estimate=True) beads = Dict(Str, List(Float), transient=True) beads_file = File(filter=["*.fcs"], estimate=True) units_list = List(_Unit, estimate=True) units = Dict(Str, Str, transient=True) bead_peak_quantile = CInt(80, estimate=True) bead_brightness_threshold = CFloat(100.0, estimate=True) bead_brightness_cutoff = util.CFloatOrNone(None, estimate=True) @on_trait_change('units_list_items,units_list.+', post_init=True) def _controls_changed(self, obj, name, old, new): self.changed = (Changed.ESTIMATE, ('units_list', self.units_list)) def default_view(self, **kwargs): return BeadCalibrationPluginView(op=self, **kwargs) def apply(self, experiment): if not self.beads_name: raise util.CytoflowOpError( "Specify which beads to calibrate with.") for i, unit_i in enumerate(self.units_list): for j, unit_j in enumerate(self.units_list): if unit_i.channel == unit_j.channel and i != j: raise util.CytoflowOpError( "Channel {0} is included more than once".format( unit_i.channel)) self.units = {} for unit in self.units_list: self.units[unit.channel] = unit.unit self.beads = self.BEADS[self.beads_name] return BeadCalibrationOp.apply(self, experiment) def estimate(self, experiment): if not self.beads_name: raise util.CytoflowOpError( "Specify which beads to calibrate with.") for i, unit_i in enumerate(self.units_list): for j, unit_j in enumerate(self.units_list): if unit_i.channel == unit_j.channel and i != j: raise util.CytoflowOpError( "Channel {0} is included more than once".format( unit_i.channel)) self.units = {} for unit in self.units_list: self.units[unit.channel] = unit.unit self.beads = self.BEADS[self.beads_name] BeadCalibrationOp.estimate(self, experiment) self.changed = (Changed.ESTIMATE_RESULT, self) def should_clear_estimate(self, changed, payload): if changed == Changed.ESTIMATE: return True return False def clear_estimate(self): self._calibration_functions.clear() self._peaks.clear() self._mefs.clear() self._histograms.clear() self.changed = (Changed.ESTIMATE_RESULT, self) def get_notebook_code(self, idx): op = BeadCalibrationOp() op.copy_traits(self, op.copyable_trait_names()) for unit in self.units_list: op.units[unit.channel] = unit.unit op.beads = self.BEADS[self.beads_name] return dedent(""" # Beads: {beads} op_{idx} = {repr} op_{idx}.estimate(ex_{prev_idx}) ex_{idx} = op_{idx}.apply(ex_{prev_idx}) """.format(beads=self.beads_name, repr=repr(op), idx=idx, prev_idx=idx - 1))
class TasbePluginOp(PluginOpMixin): handler_factory = Callable(TasbeHandler) id = Constant('edu.mit.synbio.cytoflowgui.op_plugins.bleedthrough_piecewise') friendly_id = Constant("Quantitative Pipeline") name = Constant("TASBE") channels = List(Str, estimate = True) blank_file = File(filter = ["*.fcs"], estimate = True) bleedthrough_list = List(_BleedthroughControl, estimate = True) beads_name = Str(estimate = True) beads_file = File(filter = ["*.fcs"], estimate = True) beads_unit = Str(estimate = True) bead_peak_quantile = CInt(80, estimate = True) bead_brightness_threshold = CFloat(100.0, estimate = True) bead_brightness_cutoff = util.CFloatOrNone(None, estimate = True) to_channel = Str(estimate = True) translation_list = List(_TranslationControl, estimate = True) mixture_model = Bool(False, estimate = True) _af_op = Instance(AutofluorescenceOp, (), transient = True) _bleedthrough_op = Instance(BleedthroughLinearOp, (), transient = True) _bead_calibration_op = Instance(BeadCalibrationOp, (), transient = True) _color_translation_op = Instance(ColorTranslationOp, (), transient = True) subset_list = List(ISubset, estimate = True) subset = Property(Str, depends_on = "subset_list.str") # MAGIC - returns the value of the "subset" Property, above def _get_subset(self): return " and ".join([subset.str for subset in self.subset_list if subset.str]) @on_trait_change("subset_list.str") def _subset_changed(self, obj, name, old, new): self.changed = (Changed.ESTIMATE, ('subset_list', self.subset_list)) @on_trait_change('channels[]', post_init = True) def _channels_changed(self, obj, name, old, new): for channel in self.channels: if channel not in [control.channel for control in self.bleedthrough_list]: self.bleedthrough_list.append(_BleedthroughControl(channel = channel)) to_remove = [] for control in self.bleedthrough_list: if control.channel not in self.channels: to_remove.append(control) for control in to_remove: self.bleedthrough_list.remove(control) for c in self.channels: if c == self.to_channel: continue if channel not in [control.from_channel for control in self.translation_list]: self.translation_list.append(_TranslationControl(from_channel = c, to_channel = self.to_channel)) to_remove = [] for control in self.translation_list: if control.from_channel not in self.channels: to_remove.append(control) for control in to_remove: self.translation_list.remove(control) self.changed = (Changed.ESTIMATE, ('translation_list', self.translation_list)) self.changed = (Changed.ESTIMATE, ('bleedthrough_list', self.bleedthrough_list)) @on_trait_change('to_channel', post_init = True) def _to_channel_changed(self, obj, name, old, new): self.translation_list = [] if self.to_channel: for c in self.channels: if c == self.to_channel: continue self.translation_list.append(_TranslationControl(from_channel = c, to_channel = self.to_channel)) self.changed = (Changed.ESTIMATE, ('translation_list', self.translation_list)) @on_trait_change("bleedthrough_list_items, bleedthrough_list.+", post_init = True) def _bleedthrough_controls_changed(self, obj, name, old, new): self.changed = (Changed.ESTIMATE, ('bleedthrough_list', self.bleedthrough_list)) @on_trait_change("translation_list_items, translation_list.+", post_init = True) def _translation_controls_changed(self, obj, name, old, new): self.changed = (Changed.ESTIMATE, ('translation_list', self.translation_list)) def estimate(self, experiment, subset = None): if not self.subset: warnings.warn("Are you sure you don't want to specify a subset " "used to estimate the model?", util.CytoflowOpWarning) if experiment is None: raise util.CytoflowOpError("No valid result to estimate with") # TODO - don't actually need to apply these operations to data in estimate experiment = experiment.clone() self._af_op.channels = self.channels self._af_op.blank_file = self.blank_file try: self._af_op.estimate(experiment, subset = self.subset) except: raise finally: self.changed = (Changed.ESTIMATE_RESULT, self) experiment = self._af_op.apply(experiment) self._bleedthrough_op.controls.clear() for control in self.bleedthrough_list: self._bleedthrough_op.controls[control.channel] = control.file try: self._bleedthrough_op.estimate(experiment, subset = self.subset) except: raise finally: self.changed = (Changed.ESTIMATE_RESULT, self) experiment = self._bleedthrough_op.apply(experiment) self._bead_calibration_op.beads = BeadCalibrationOp.BEADS[self.beads_name] self._bead_calibration_op.beads_file = self.beads_file self._bead_calibration_op.bead_peak_quantile = self.bead_peak_quantile self._bead_calibration_op.bead_brightness_threshold = self.bead_brightness_threshold self._bead_calibration_op.bead_brightness_cutoff = self.bead_brightness_cutoff self._bead_calibration_op.units.clear() # this is the old way # for channel in self.channels: # self._bead_calibration_op.units[channel] = self.beads_unit # this way matches TASBE better self._bead_calibration_op.units[self.to_channel] = self.beads_unit try: self._bead_calibration_op.estimate(experiment) except: raise finally: self.changed = (Changed.ESTIMATE_RESULT, self) experiment = self._bead_calibration_op.apply(experiment) self._color_translation_op.mixture_model = self.mixture_model self._color_translation_op.controls.clear() for control in self.translation_list: self._color_translation_op.controls[(control.from_channel, control.to_channel)] = control.file try: self._color_translation_op.estimate(experiment, subset = self.subset) except: raise finally: self.changed = (Changed.ESTIMATE_RESULT, self) def should_clear_estimate(self, changed, payload): if changed == Changed.ESTIMATE: return True return False def clear_estimate(self): self._af_op = AutofluorescenceOp() self._bleedthrough_op = BleedthroughLinearOp() self._bead_calibration_op = BeadCalibrationOp() self._color_translation_op = ColorTranslationOp() self.changed = (Changed.ESTIMATE_RESULT, self) def apply(self, experiment): if experiment is None: raise util.CytoflowOpError("No experiment was specified") experiment = self._af_op.apply(experiment) experiment = self._bleedthrough_op.apply(experiment) experiment = self._bead_calibration_op.apply(experiment) experiment = self._color_translation_op.apply(experiment) return experiment def default_view(self, **kwargs): return TasbePluginView(op = self, **kwargs) def get_notebook_code(self, idx): self._af_op.channels = self.channels self._af_op.blank_file = self.blank_file self._bleedthrough_op.controls.clear() for control in self.bleedthrough_list: self._bleedthrough_op.controls[control.channel] = control.file self._bead_calibration_op.beads = BeadCalibrationOp.BEADS[self.beads_name] self._bead_calibration_op.beads_file = self.beads_file self._bead_calibration_op.bead_peak_quantile = self.bead_peak_quantile self._bead_calibration_op.bead_brightness_threshold = self.bead_brightness_threshold self._bead_calibration_op.bead_brightness_cutoff = self.bead_brightness_cutoff self._bead_calibration_op.units.clear() self._bead_calibration_op.units[self.to_channel] = self.beads_unit self._color_translation_op.mixture_model = self.mixture_model self._color_translation_op.controls.clear() for control in self.translation_list: self._color_translation_op.controls[(control.from_channel, control.to_channel)] = control.file return dedent(""" # the TASBE-style calibration is not a single Cytoflow module. Instead, it # is a specific sequence of four calibrations: autofluorescence correction, # bleedthrough, bead calibration and color translation. # autofluorescence op_{idx}_af = {af_repr} op_{idx}_af.estimate(ex_{prev_idx}{subset}) ex_{idx}_af = op_{idx}_af.apply(ex_{prev_idx}) # bleedthrough op_{idx}_bleedthrough = {bleedthrough_repr} op_{idx}_bleedthrough.estimate(ex_{idx}_af{subset}) ex_{idx}_bleedthrough = op_{idx}_bleedthrough.apply(ex_{idx}_af) # bead calibration # beads: {beads} op_{idx}_beads = {beads_repr} op_{idx}_beads.estimate(ex_{idx}_bleedthrough) ex_{idx}_beads = op_{idx}_beads.apply(ex_{idx}_bleedthrough) # color translation op_{idx}_color = {color_repr} op_{idx}_color.estimate(ex_{idx}_beads{subset}) ex_{idx} = op_{idx}_color.apply(ex_{idx}_beads) """ .format(idx = idx, prev_idx = idx - 1, af_repr = repr(self._af_op), bleedthrough_repr = repr(self._bleedthrough_op), color_repr = repr(self._color_translation_op), beads = self.beads_name, beads_repr = repr(self._bead_calibration_op), subset = ", subset = " + repr(self.subset) if self.subset else ""))