巨大な辞書をロードしようとすると非常に遅くなるため、コードを最適化しようとしています。辞書でキーを検索するためだと思います。私はpythonについて読んでいてdefaultdict
、それは良い改善かもしれないと思いますが、ここでは実装できません。ご覧のとおり、階層的な辞書構造です。ヒントをいただければ幸いです。
class Species:
'''This structure contains all the information needed for all genes.
One specie have several genes, one gene several proteins'''
def __init__(self, name):
self.name = name #name of the GENE
self.genes = {}
def addProtein(self, gene, protname, len):
#Converting a line from the input file into a protein and/or an exon
if gene in self.genes:
#Gene in the structure
self.genes[gene].proteins[protname] = Protein(protname, len)
self.genes[gene].updateProts()
else:
self.genes[gene] = Gene(gene)
self.updateNgenes()
self.genes[gene].proteins[protname] = Protein(protname, len)
self.genes[gene].updateProts()
def updateNgenes(self):
#Updating the number of genes
self.ngenes = len(self.genes.keys())
遺伝子とタンパク質の定義は次のとおりです。
class Protein:
#The class protein contains information about the length of the protein and a list with it's exons (with it's own attributes)
def __init__(self, name, len):
self.name = name
self.len = len
class Gene:
#The class gene contains information about the gene and a dict with it's proteins (with it's own attributes)
def __init__(self, name):
self.name = name
self.proteins = {}
self.updateProts()
def updateProts(self):
#Update number of proteins
self.nproteins = len(self.proteins)