コード例 #1
0
ファイル: import_op.py プロジェクト: nathan2wong/cytoflow
    def get_notebook_code(self, idx):
        op = ImportOp()
        op.copy_traits(self, op.copyable_trait_names())

        return dedent("""
            op_{idx} = {repr}
            
            ex_{idx} = op_{idx}.apply()""".format(repr=repr(op), idx=idx))
コード例 #2
0
    def apply(self, experiment):

        if self.blank_file != self._blank_exp_file:
            self._blank_exp = ImportOp(tubes=[Tube(
                file=self.blank_file)]).apply()
            self._blank_exp_file = self.blank_file
            self._blank_exp_channels = self._blank_exp.channels
            self.changed = (Changed.PREV_RESULT, None)
            return

        out_dir = Path(self.output_directory)
        for path in self.input_files:
            in_file_path = Path(path)
            out_file_path = out_dir / in_file_path.name
            if out_file_path.exists():
                raise util.CytoflowOpError(
                    None, "File {} already exists".format(out_file_path))

        tubes = [
            Tube(file=path, conditions={'filename': Path(path).stem})
            for path in self.input_files
        ]

        for tube in tubes:
            self.status = "Converting " + Path(tube.file).stem
            experiment = ImportOp(tubes=[tube],
                                  conditions={
                                      'filename': 'category'
                                  }).apply()

            experiment = self._af_op.apply(experiment)
            experiment = self._bleedthrough_op.apply(experiment)
            experiment = self._bead_calibration_op.apply(experiment)

            if self.do_color_translation:
                experiment = self._color_translation_op.apply(experiment)

            ExportFCS(path=self.output_directory,
                      by=['filename'],
                      _include_by=False).export(experiment)

        self.input_files = []
        self.status = "Done converting!"
コード例 #3
0
    def get_notebook_code(self, idx):
        op = ImportOp()
        op.copy_traits(self, op.copyable_trait_names())
        op.channels = {c.channel: c.name for c in self.channels_list}

        return dedent("""
            op_{idx} = {repr}
            
            ex_{idx} = op_{idx}.apply()""".format(repr=repr(op), idx=idx))
コード例 #4
0
    def _on_add_tubes(self):
        """
        Handle "Add tubes..." button.  Add tubes to the experiment.
        """

        file_dialog = FileDialog()
        file_dialog.wildcard = "Flow cytometry files (*.fcs *.lmd)|*.fcs *.lmd|"
        file_dialog.action = 'open files'
        file_dialog.open()

        if file_dialog.return_code != PyfaceOK:
            return

        for path in file_dialog.paths:
            try:
                metadata, _ = parse_tube(path, metadata_only=True)
            except Exception as e:
                raise RuntimeError("FCS reader threw an error on tube {0}: {1}"\
                                   .format(path, e.value))

            # if we're the first tube loaded, create a dummy experiment
            # and setup default metadata columns
            if not self.model.dummy_experiment:
                self.model.dummy_experiment = \
                    ImportOp(tubes = [CytoflowTube(file = path)]).apply(metadata_only = True)

            # check the next tube against the dummy experiment
            try:
                check_tube(path, self.model.dummy_experiment)
            except util.CytoflowError as e:
                error(None, e.__str__(), "Error importing tube")
                return

            metadata['CF_File'] = Path(path).stem
            tube = Tube(file=path,
                        parent=self.model,
                        metadata=sanitize_metadata(metadata))

            self.model.tubes.append(tube)

            for trait in self.model.tube_traits:
                if trait.type != 'metadata' and trait.name:
                    tube.on_trait_change(self._try_multiedit, trait.name)
コード例 #5
0
ファイル: import_op.py プロジェクト: Weiss-Lab/cytoflow
 def get_operation(self):
     ret = ImportOp()
     ret.add_trait("handler_factory", Callable)
     ret.handler_factory = ImportHandler
     return ret
コード例 #6
0
 def _on_add_tubes(self):
     """
     Handle "Add tubes..." button.  Add tubes to the experiment.
     """
     
     file_dialog = FileDialog()
     file_dialog.wildcard = "Flow cytometry files (*.fcs)|*.fcs|"
     file_dialog.action = 'open files'
     file_dialog.open()
     
     if file_dialog.return_code != PyfaceOK:
         return
     
     for path in file_dialog.paths:
         try:
             tube_meta = fcsparser.parse(path, 
                                         meta_data_only = True, 
                                         reformat_meta = True)
             #tube_channels = tube_meta["_channels_"].set_index("$PnN")
         except Exception as e:
             raise RuntimeError("FCS reader threw an error on tube {0}: {1}"\
                                .format(path, e.value))
             
             
         # if we're the first tube loaded, create a dummy experiment
         if not self.model.dummy_experiment:
             self.model.dummy_experiment = ImportOp(tubes = [CytoflowTube(file = path)],
                                                    coarse_events = 1).apply()
                                                    
         # check the next tube against the dummy experiment
         try:
             check_tube(path, self.model.dummy_experiment)
         except util.CytoflowError as e:
             error(None, e.__str__(), "Error importing tube")
             return
             
         tube = Tube()
         
         for trait_name, trait in self.model.tube_traits.items():
             tube.add_trait(trait_name, trait)
             
             # this magic makes sure the trait is actually defined
             # in tube.__dict__, so it shows up in trait_names etc.
             tube.trait_set(**{trait_name : trait.default_value})
             if trait.condition:
                 tube.on_trait_change(self._try_multiedit, trait_name)
         
         tube.trait_set(file = path, parent = self.model)
         
         if '$SRC' in tube_meta:    
             self._add_metadata("$SRC", "$SRC", Str(condition = False))
             tube.trait_set(**{"$SRC" : tube_meta['$SRC']})
             
         if 'TUBE NAME' in tube_meta:
             self._add_metadata("TUBE NAME", "TUBE NAME", Str(condition = False))
             tube.trait_set(**{"TUBE NAME" : tube_meta['TUBE NAME']})
             
         if '$SMNO' in tube_meta:
             self._add_metadata("$SMNO", "$SMNO", Str(condition = False))
             tube.trait_set(**{"$SMNO" : tube_meta['$SMNO']})
             
         if 'WELL ID' in tube_meta:
             self._add_metadata("Row", "Row", Str(condition = False))
             self._add_metadata("Col", "Col", Int(condition = False))
             
             pos = tube_meta['WELL ID']
             row = pos[0]
             col = int(pos[1:3])
             
             tube.trait_set(**{"Row" : row, "Col" : col})
             
         
         self.model.tubes.append(tube)
コード例 #7
0
    def init_model(self, op):
        
        dtype_to_trait = {"category" : Str,
                          "float" : Float,
                          "bool" : Bool,
                          "int" : Int}
        
        for op_tube in op.tubes:
            tube = Tube(file = op_tube.file,
                        parent = self)
            
            # first load the tube's metadata and set special columns
            try:
                tube_meta = fcsparser.parse(op_tube.file, 
                                            meta_data_only = True, 
                                            reformat_meta = True)
                #tube_channels = tube_meta["_channels_"].set_index("$PnN")
            except Exception as e:
                error(None, "FCS reader threw an error on tube {0}: {1}"\
                            .format(op_tube.file, e.value),
                      "Error reading FCS file")
                return
            
            # if we're the first tube loaded, create a dummy experiment
            if not self.dummy_experiment:
                self.dummy_experiment = ImportOp(tubes = [op_tube],
                                                 conditions = op.conditions,
                                                 coarse_events = 1).apply()
                
            if '$SRC' in tube_meta:    
                self.tube_traits["$SRC"] = Str(condition = False)
                tube.add_trait("$SRC", Str(condition = False))
                tube.trait_set(**{"$SRC" : tube_meta['$SRC']})
                
            if 'TUBE NAME' in tube_meta:
                self.tube_traits["TUBE NAME"] = Str(condition = False)
                tube.add_trait("TUBE NAME", Str(condition = False))
                tube.trait_set(**{"TUBE NAME" : tube_meta['TUBE NAME']})
                
            if '$SMNO' in tube_meta:
                self.tube_traits["$SMNO"] = Str(condition = False)
                tube.add_trait("$SMNO", Str(condition = False))
                tube.trait_set(**{"$SMNO" : tube_meta['SMNO']})
                
            if 'WELL ID' in tube_meta:                
                pos = tube_meta['WELL ID']
                row = pos[0]
                col = int(pos[1:3])
                
                self.tube_traits["Row"] = Str(condition = False)
                self.tube_traits["Col"] = Int(condition = False)
                tube.add_trait("Row", Str(condition = False))
                tube.add_trait("Col", Int(condition = False))
                tube.trait_set(**{"Row" : row, "Col" : col})

            # next set conditions
            try:
                conditions_list = op_tube.conditions_list
            except:
                conditions_list = op_tube.conditions.keys()
                
            for condition in conditions_list:
                condition_dtype = op.conditions[condition]
                condition_trait = \
                    dtype_to_trait[condition_dtype](condition = True)
                tube.add_trait(condition, condition_trait)
                if not condition in self.tube_traits:
                    self.tube_traits[condition] = condition_trait
            tube.trait_set(**op_tube.conditions)
            
            self.tubes.append(tube)
コード例 #8
0
ファイル: tasbe_calibration.py プロジェクト: roshih/cytoflow
    def apply(self, experiment):

        if self.blank_file != self._blank_exp_file:
            self._blank_exp = ImportOp(tubes=[Tube(
                file=self.blank_file)]).apply()
            self._blank_exp_file = self.blank_file
            self._blank_exp_channels = self._blank_exp.channels
            self.changed = (Changed.PREV_RESULT, None)
            return

        out_dir = Path(self.output_directory)
        for path in self.input_files:
            in_file_path = Path(path)
            out_file_path = out_dir / in_file_path.name
            if out_file_path.exists():
                raise util.CytoflowOpError(
                    None, "File {} already exists".format(out_file_path))

        tubes = [
            Tube(file=path, conditions={'filename': Path(path).stem})
            for path in self.input_files
        ]

        for tube in tubes:
            self.status = "Converting " + Path(tube.file).stem
            experiment = ImportOp(tubes=[tube],
                                  conditions={
                                      'filename': 'category'
                                  }).apply()

            experiment = self._af_op.apply(experiment)
            experiment = self._bleedthrough_op.apply(experiment)
            experiment = self._bead_calibration_op.apply(experiment)

            if self.do_color_translation:
                experiment = self._color_translation_op.apply(experiment)

            tube_meta = fcsparser.parse(
                tube.file,
                channel_naming=experiment.metadata["name_metadata"],
                meta_data_only=True,
                reformat_meta=False)

            del tube_meta['__header__']

            tube_meta = {
                k: v
                for (k, v) in tube_meta.items()
                if not k.startswith("flowCore_")
            }
            tube_meta = {
                k: v
                for (k, v) in tube_meta.items()
                if not (k.startswith("$P") and k[2].isdigit())
            }

            drop_kws = [
                "$BEGINANALYSIS", "$ENDANALYSIS", "$BEGINSTEXT", "$ENDSTEXT",
                "$BEGINDATA", "$ENDDATA", "$BYTEORD", "$DATATYPE", "$MODE",
                "$NEXTDATA", "$TOT", "$PAR"
            ]

            tube_meta = {
                k: v
                for (k, v) in tube_meta.items() if not k in drop_kws
            }

            ExportFCS(path=self.output_directory,
                      by=['filename'],
                      _include_by=False,
                      keywords=tube_meta).export(experiment)

        self.input_files = []
        self.status = "Done converting!"