Question

I have a dictionary of compound objects made up of a string, a list, and two ints.

I am looping through this dictionary, performing some tests, and then based on these tests saving the currently tested object to a new dictionary and then modify some of the attributes. This so far has been putting a reference to the object in the new dictionary so when I change the some of the attributes, then the old object changes also and I want to avoid this behaviour.

I tried using the copy.copy and copy.deepcopy methods from the copy module, but for some reason after copying I was not able to change the attributes. They would simply remain as they were in the original.

I should say I am pretty new to OOP, so aim any suggestions to moron level!

So in the code below, pathways is a dictionary of pathway objects, and variants is just a list. names() and addMutant() are methods belonging to the pathway object.

In case it helps: pathway objects hold a list of genes (also another object) that belong to that pathway. I want to find out which pathways are represented in the list of variants (a list of gene names) and then return a new (smaller) dictionary of pathways that will have been modified to record the number of mutated genes in each pathway. The addMutant() method simply increments a int attribute that belongs to the pathway object.

def method(variants, pathways):
    vpathways = {}
            check = 0
            for var in variants:
                for k,v in pathways.iteritems():
                    if(var in v.names()):
                        check +=1
                        vpathways[k] = copy.deepcopy(v)
                        vpathways[k].addMutant()
     return(vpathways)

When I did this, vpathways did contain the right pathways, and but the number of mutations for each pathway was all still 0. I added a print statement to the addMutant() method and it is definitely being called, and the number of mutations attributes is increased, but not in the final object.

Edit: Pathway class definition:

class Pathway():
    def __init__(self, pid = None, genes = [],nmut = 0 ):
        self.pid = pid
        self.genes = genes
        self.length = 0
        self.nmut = nmut
        for g in self.genes:
            self.length += g.seqlen
    def __str__(self):
        return('{0} pathway containing {1} genes, with a total sequence length of {2} and {3} mutations'.format(self.pid, len(self.genes), self.length, self.nmut))
    def __repr__(self):
        return self.__str__()   
    def addGene2Pathway(self,g):
        self.genes.append(g)
        self.length += g.seqlen
    def addMutant(self):
        self.nmut +=1  
    def names(self):
        l = []
        for g in self.genes:
            l.append(g.entrezid)
        return l
Was it helpful?

Solution

vpathways[k] = copy.deepcopy(v)

replaces whatever vpathways[k] contained before so at most one .addMutant() might work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top