Beispiel #1
0
    def test_setting_reaction(self):
        widget = ReactionAttributesDisplayWidget()
        reaction = Reaction(id="id",
                            name="name",
                            subsystem="subsystem",
                            lower_bound=-1000.,
                            upper_bound=1000.)
        model = Model()
        model.add_reactions((reaction, ))
        reaction.objective_coefficient = 1.

        assert widget.idLineEdit.text() == ""
        assert widget.nameLineEdit.text() == ""
        assert widget.subsystemLineEdit.text() == ""
        assert widget.lowerBoundInput.value() == 0.
        assert widget.upperBoundInput.value() == 0.
        assert widget.objectiveCoefficientInput.value() == 0.
        assert widget.content_changed is False
        assert widget.valid_inputs() is False

        widget.set_item(reaction, model)
        assert widget.idLineEdit.text() == reaction.id
        assert widget.nameLineEdit.text() == reaction.name
        assert widget.subsystemLineEdit.text() == reaction.subsystem
        assert widget.lowerBoundInput.value() == reaction.lower_bound
        assert widget.upperBoundInput.value() == reaction.upper_bound
        assert widget.objectiveCoefficientInput.value(
        ) == reaction.objective_coefficient
        assert widget.content_changed is False
        assert widget.valid_inputs() is True
Beispiel #2
0
    def copy_reaction(self, move=False):
        """ Copy or move reactions between compartmsntes
        
        Parameters
        ----------
        move : bool

        Returns
        -------

        """

        # Get selected items
        rows = self.dataView.get_selected_rows()
        reactions = [self.dataTable.item_from_row(i) for i in rows]

        # Only continue if rows are selected
        if not rows:
            return

        # Collect compartments of metabolites in the selected reactions
        compartments = set()
        for reaction in reactions:
            compartments.update([metabolite.compartment for metabolite in reaction.metabolites])

        # Get compartment mapping from user
        compartment_mapping = dict()
        for compartment in compartments:
            value, status = QInputDialog().getItem(self, "Choose target compartment", 'Choose target for compartment "{}":'.format(compartment), list(self.model.gem_compartments.keys()), 0, False)
            if not status:
                return
            else:
                compartment_mapping[compartment] = value

        progress = QProgressDialog("{} reactions".format("Moving" if move else "Copying"),
                                         "Cancel", 0, len(reactions), self)

        # Copy reactions using matching metabolite from other compartment
        for n, reaction in enumerate(reactions):

            # Check user input and update progress dialog
            if progress.wasCanceled():
                progress.close()
                return
            else:
                progress.setValue(n)
                QApplication.processEvents()

            metabolites = dict()
            # Collect corresponding metabolites in target compartment
            for key, value in reaction.metabolites.items():
                target_compartment = compartment_mapping[key.compartment]

                putative_target_metabolits = find_duplicate_metabolite(key,
                                                                       [x for x in self.model.metabolites if x.compartment == target_compartment],
                                                                       same_compartment=False)

                corresponding_metabolite = None
                if putative_target_metabolits:
                    best_hit, score = putative_target_metabolits[0]
                    if best_hit.compartment == target_compartment and score >= 2.:
                        corresponding_metabolite = best_hit

                if not corresponding_metabolite:
                    corresponding_metabolite = self.model.copy_metabolite(key, target_compartment)

                metabolites[corresponding_metabolite] = value

            # Create new reaction if the move flag is false
            if not move:
                new_reaction = Reaction(id=generate_copy_id(reaction.id, self.model.reactions),
                                        name=reaction.name,
                                        subsystem=reaction.subsystem,
                                        lower_bound=reaction.lower_bound,
                                        upper_bound=reaction.upper_bound,
                                        comment=reaction.comment)
                new_reaction.annotation = reaction.annotation.copy()
                new_reaction.add_metabolites(metabolites)
                self.model.add_reactions([new_reaction])

                # Set objective coefficient after adding reaction to model
                new_reaction.objective_coefficient = reaction.objective_coefficient
                self.model.QtReactionTable.update_row_from_item(new_reaction)

            # Update existing reactions with new metabolites if moving
            else:
                reaction.clear_metabolites()
                reaction.add_metabolites(metabolites)
                self.model.QtReactionTable.update_row_from_link(rows[n])

        progress.close()