Hey everyone,
Here is an outline of my problem:
I have a working code which defines a class, GeneDict, which reads in data from a special type of file, and stores it as a dictionary. What it is really doing is taking in millions of lines of biological data, and storing chromosomes as keys. To each key, there is not just one value, but a list of values. Anyway, that part works fine, so after that, I define a bunch of methods to act on the dictionary (like get the dictionary length for example).
What I want to do now is define a second dictionary. Let's call it, FamilyDict. FamilyDict will have the same keys as GeneDict, but less values. So, what I seek to do is write a subclass that will inherit from GeneDict, take in all the keys, then filter out some of the values that I don't need to keep, and append these values to the new FamilyDict. I seek to use subclasses because I want to be able to use all of the pre-written methods on both the GeneDict and the FamilyDict.
Below I will post the working GeneDict. It is not crucial that you understand everything about this diction, just know that it works:
class GeneDict:
'''Class to build a dictionary with chromosones as keys, with several genes as values, each gene/value being a freaking list of information.'''
def __init__(self, file=None): #Will read from file, but file will be defined later at instantiation
'''Reads in file like rep_element.bed, stores chromosones as keys, and all other info as values'''
self.dictionary = {}
infile = open(file)
for line in infile:
if not re.match("#", line): #If the line isn't a header
line = line.strip()
sline = line.split()
if sline[5] not in self.dictionary.keys():
self.dictionary[sline[5]] = []; #key is added
value=RepeatingElement( int(sline[0]), int(sline[1]), int(sline[2]), int(sline[3]), int(sline[4]), sline[5],
int(sline[6]), int(sline[7]), sline[8],
sline[9], sline[10], sline[11], sline[12],
sline[13], int(sline[14]),
int(sline[15]), sline[16] )
self.dictionary[sline[5]].append(value)
Now, here is my attempt at the new dictionary:
class FamilyDict(GeneDict):
def __init__(self, file=None):
GeneDict.__init__(self, file=None)
self.Family_dict= {}
infile = open(file)
for key in dictionary.keys():
self.Family_dict.append(key)
The terminal is already complaining, and all I've tried to do is copy over the keys. In the end, I need to tell FamilyDict to take certain bunch of elements from GeneDict's values. I plan to do it with an expression match, something like:
if str(element.repFamily) == str(family):
"""Check to see if read matches desired family""" etc...
But just for now, can you guys see where my subclass has already gone wrong?