The Nightman
The Nightman

Reputation: 5759

Properly declaring unique dictionaries with copy()

I am trying to read through sequencing data and classify the contained mutations. The problem I think I am having is not properly declaring each of the nested dictionaries such that they are unique.

This is how I am creating my data structure:

baseDict = {'A':0, 'T':0, 'G':0, 'C':0}
varDict = {'A':baseDict.copy(), 'T':baseDict.copy(), 'G':baseDict.copy(), 'C':baseDict.copy()}
fullDict = {'oncoSites':varDict.copy(), 'oncoGenes':varDict.copy(), 'TIIIRegions':varDict.copy()}

Then I am adding any particular mutation I read in like this:

fullDict['oncoSites'][j][k] += 1

The problem is that when I add a mutation it is added to multiple dictionaries. As an example Take if I read in a reference base of T and variant of C that is found in oncoSites then add it as:

fullDict['oncoSites'][T][C] += 1

The output I get is this:

{'TIIIRegions': {'A': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
                 'C': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
                 'G': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
                 'T': {'A': 0, 'C': 1, 'G': 0, 'T': 0}},
 'oncoGenes': {'A': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
               'C': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
               'G': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
               'T': {'A': 0, 'C': 1, 'G': 0, 'T': 0}},
 'oncoSites': {'A': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
               'C': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
               'G': {'A': 0, 'C': 0, 'G': 0, 'T': 0},
               'T': {'A': 0, 'C': 1, 'G': 0, 'T': 0}}}

How can I increment only a single dictionary?

Upvotes: 0

Views: 37

Answers (1)

UltraInstinct
UltraInstinct

Reputation: 44454

You'd need a deepcopy.

Use:

{'oncoSites':deepcopy(varDict), 'oncoGenes':deepcopy(varDict), 'TIIIRegions':deepcopy(varDict)}

What was happening is: when you did varDict.copy(..) you were copying the references of the copy of baseDict

Upvotes: 2

Related Questions