cobra/core/model.py
def copy(self):
        """Provides a partial 'deepcopy' of the Model.  All of the Metabolite,
        Gene, and Reaction objects are created anew but in a faster fashion
        than deepcopy
        """
        new = self.__class__()
        do_not_copy_by_ref = {"metabolites", "reactions", "genes", "notes",
                              "annotation", "groups"}
        for attr in self.__dict__:
            if attr not in do_not_copy_by_ref:
                new.__dict__[attr] = self.__dict__[attr]
        new.notes = deepcopy(self.notes)
        new.annotation = deepcopy(self.annotation)

        new.metabolites = DictList()
        do_not_copy_by_ref = {"_reaction", "_model"}
        for metabolite in self.metabolites:
            new_met = metabolite.__class__()
            for attr, value in iteritems(metabolite.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_met.__dict__[attr] = copy(
                        value) if attr == "formula" else value
            new_met._model = new
            new.metabolites.append(new_met)

        new.genes = DictList()
        for gene in self.genes:
            new_gene = gene.__class__(None)
            for attr, value in iteritems(gene.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_gene.__dict__[attr] = copy(
                        value) if attr == "formula" else value
            new_gene._model = new
            new.genes.append(new_gene)

        new.reactions = DictList()
        do_not_copy_by_ref = {"_model", "_metabolites", "_genes"}
        for reaction in self.reactions:
            new_reaction = reaction.__class__()
            for attr, value in iteritems(reaction.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_reaction.__dict__[attr] = copy(value)
            new_reaction._model = new
            new.reactions.append(new_reaction)
            # update awareness
            for metabolite, stoic in iteritems(reaction._metabolites):
                new_met = new.metabolites.get_by_id(metabolite.id)
                new_reaction._metabolites[new_met] = stoic
                new_met._reaction.add(new_reaction)
            for gene in reaction._genes:
                new_gene = new.genes.get_by_id(gene.id)
                new_reaction._genes.add(new_gene)
                new_gene._reaction.add(new_reaction)

        new.groups = DictList()
        do_not_copy_by_ref = {"_model", "_members"}
        # Groups can be members of other groups. We initialize them first and
        # then update their members.
        for group in self.groups:
            new_group = group.__class__(group.id)
            for attr, value in iteritems(group.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_group.__dict__[attr] = copy(value)
            new_group._model = new
            new.groups.append(new_group)
        for group in self.groups:
            new_group = new.groups.get_by_id(group.id)
            # update awareness, as in the reaction copies
            new_objects = []
            for member in group.members:
                if isinstance(member, Metabolite):
                    new_object = new.metabolites.get_by_id(member.id)
                elif isinstance(member, Reaction):
                    new_object = new.reactions.get_by_id(member.id)
                elif isinstance(member, Gene):
                    new_object = new.genes.get_by_id(member.id)
                elif isinstance(member, Group):
                    new_object = new.genes.get_by_id(member.id)
                else:
                    raise TypeError(
                        "The group member {!r} is unexpectedly not a "
                        "metabolite, reaction, gene, nor another "
                        "group.".format(member))
                new_objects.append(new_object)
            new_group.add_members(new_objects)

        try:
            new._solver = deepcopy(self.solver)
            # Cplex has an issue with deep copies
        except Exception:  # pragma: no cover
            new._solver = copy(self.solver)  # pragma: no cover

        # it doesn't make sense to retain the context of a copied model so
        # assign a new empty context
        new._contexts = list()

        return new
def copy(self):
        """Provides a partial 'deepcopy' of the Model.  All of the Metabolite,
        Gene, and Reaction objects are created anew but in a faster fashion
        than deepcopy
        """
        new = self.__class__()
        do_not_copy_by_ref = {"metabolites", "reactions", "genes", "notes",
                              "annotation", "groups"}
        for attr in self.__dict__:
            if attr not in do_not_copy_by_ref:
                new.__dict__[attr] = self.__dict__[attr]
        new.notes = deepcopy(self.notes)
        new.annotation = deepcopy(self.annotation)

        new.metabolites = DictList()
        do_not_copy_by_ref = {"_reaction", "_model"}
        for metabolite in self.metabolites:
            new_met = metabolite.__class__()
            for attr, value in iteritems(metabolite.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_met.__dict__[attr] = copy(
                        value) if attr == "formula" else value
            new_met._model = new
            new.metabolites.append(new_met)

        new.genes = DictList()
        for gene in self.genes:
            new_gene = gene.__class__(None)
            for attr, value in iteritems(gene.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_gene.__dict__[attr] = copy(
                        value) if attr == "formula" else value
            new_gene._model = new
            new.genes.append(new_gene)

        new.reactions = DictList()
        do_not_copy_by_ref = {"_model", "_metabolites", "_genes"}
        for reaction in self.reactions:
            new_reaction = reaction.__class__()
            for attr, value in iteritems(reaction.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_reaction.__dict__[attr] = copy(value)
            new_reaction._model = new
            new.reactions.append(new_reaction)
            # update awareness
            for metabolite, stoic in iteritems(reaction._metabolites):
                new_met = new.metabolites.get_by_id(metabolite.id)
                new_reaction._metabolites[new_met] = stoic
                new_met._reaction.add(new_reaction)
            for gene in reaction._genes:
                new_gene = new.genes.get_by_id(gene.id)
                new_reaction._genes.add(new_gene)
                new_gene._reaction.add(new_reaction)

        new.groups = DictList()
        do_not_copy_by_ref = {"_model", "_members"}
        # Groups can be members of other groups. We initialize them first and
        # then update their members.
        for group in self.groups:
            new_group = group.__class__(group.id)
            for attr, value in iteritems(group.__dict__):
                if attr not in do_not_copy_by_ref:
                    new_group.__dict__[attr] = copy(value)
            new_group._model = new
            new.groups.append(new_group)
        for group in self.groups:
            new_group = new.groups.get_by_id(group.id)
            # update awareness, as in the reaction copies
            new_objects = []
            for member in group.members:
                if isinstance(member, Metabolite):
                    new_object = new.metabolites.get_by_id(member.id)
                elif isinstance(member, Reaction):
                    new_object = new.reactions.get_by_id(member.id)
                elif isinstance(member, Gene):
                    new_object = new.genes.get_by_id(member.id)
                elif isinstance(member, Group):
                    new_object = new.genes.get_by_id(member.id)
                else:
                    raise TypeError(
                        "The group member {!r} is unexpectedly not a "
                        "metabolite, reaction, gene, nor another "
                        "group.".format(member))
                new_objects.append(new_object)
            new_group.add_members(new_objects)

        try:
            new._solver = deepcopy(self.solver)
            # Cplex has an issue with deep copies
        except Exception:  # pragma: no cover
            new._solver = copy(self.solver)  # pragma: no cover

        # it doesn't make sense to retain the context of a copied model so
        # assign a new empty context
        new._contexts = list()

        return new
def copy ( self ) : new = self . __class__ ( ) do_not_copy_by_ref = { "metabolites" , "reactions" , "genes" , "notes" , "annotation" , "groups" } for attr in self . __dict__ : if attr not in do_not_copy_by_ref : new . __dict__ [ attr ] = self . __dict__ [ attr ] new . notes = deepcopy ( self . notes ) new . annotation = deepcopy ( self . annotation ) new . metabolites = DictList ( ) do_not_copy_by_ref = { "_reaction" , "_model" } for metabolite in self . metabolites : new_met = metabolite . __class__ ( ) for attr , value in iteritems ( metabolite . __dict__ ) : if attr not in do_not_copy_by_ref : new_met . __dict__ [ attr ] = copy ( value ) if attr == "formula" else value new_met . _model = new new . metabolites . append ( new_met ) new . genes = DictList ( ) for gene in self . genes : new_gene = gene . __class__ ( None ) for attr , value in iteritems ( gene . __dict__ ) : if attr not in do_not_copy_by_ref : new_gene . __dict__ [ attr ] = copy ( value ) if attr == "formula" else value new_gene . _model = new new . genes . append ( new_gene ) new . reactions = DictList ( ) do_not_copy_by_ref = { "_model" , "_metabolites" , "_genes" } for reaction in self . reactions : new_reaction = reaction . __class__ ( ) for attr , value in iteritems ( reaction . __dict__ ) : if attr not in do_not_copy_by_ref : new_reaction . __dict__ [ attr ] = copy ( value ) new_reaction . _model = new new . reactions . append ( new_reaction ) # update awareness for metabolite , stoic in iteritems ( reaction . _metabolites ) : new_met = new . metabolites . get_by_id ( metabolite . id ) new_reaction . _metabolites [ new_met ] = stoic new_met . _reaction . add ( new_reaction ) for gene in reaction . _genes : new_gene = new . genes . get_by_id ( gene . id ) new_reaction . _genes . add ( new_gene ) new_gene . _reaction . add ( new_reaction ) new . groups = DictList ( ) do_not_copy_by_ref = { "_model" , "_members" } # Groups can be members of other groups. We initialize them first and # then update their members. for group in self . groups : new_group = group . __class__ ( group . id ) for attr , value in iteritems ( group . __dict__ ) : if attr not in do_not_copy_by_ref : new_group . __dict__ [ attr ] = copy ( value ) new_group . _model = new new . groups . append ( new_group ) for group in self . groups : new_group = new . groups . get_by_id ( group . id ) # update awareness, as in the reaction copies new_objects = [ ] for member in group . members : if isinstance ( member , Metabolite ) : new_object = new . metabolites . get_by_id ( member . id ) elif isinstance ( member , Reaction ) : new_object = new . reactions . get_by_id ( member . id ) elif isinstance ( member , Gene ) : new_object = new . genes . get_by_id ( member . id ) elif isinstance ( member , Group ) : new_object = new . genes . get_by_id ( member . id ) else : raise TypeError ( "The group member {!r} is unexpectedly not a " "metabolite, reaction, gene, nor another " "group." . format ( member ) ) new_objects . append ( new_object ) new_group . add_members ( new_objects ) try : new . _solver = deepcopy ( self . solver ) # Cplex has an issue with deep copies except Exception : # pragma: no cover new . _solver = copy ( self . solver ) # pragma: no cover # it doesn't make sense to retain the context of a copied model so # assign a new empty context new . _contexts = list ( ) return new
Provides a partial 'deepcopy' of the Model.  All of the Metabolite,
        Gene, and Reaction objects are created anew but in a faster fashion
        than deepcopy
Provides a partial deepcopy of the Model . All of the Metabolite Gene and Reaction objects are created anew but in a faster fashion than deepcopy