I'm very new to object-oriented programing, and I'm having trouble wrapping my head around how to manage the relationship between classes that have related data.
I'm trying to represent some genomic information about a series of organisms, which I am eventually going to compare. So I have a Species
class:
class Species(object):
def __init__(self, species_name, genes={}, contigs={}):
self.species_name = species_name
self.genes = genes
self.contigs = contigs
Then I'd like to have a Gene
class,
class Gene(object):
def __init__(self, locus_tag, annotation, dna_seq, aa_seq):
self.locus_tag = locus_tag
self.annotation = annotation
self.dna_seq = dna_seq
self.aa_seq = aa_seq
The idea is that the genes
method of Species
will be a dictionary of Gene
objects, where the key is the locus_tag
of the Gene
. I think I know how to implement this part.
But one of the things I'd like to be able to do is call a function on a Gene
object that requires knowing what Species
it's in (it would output a file structure that includes the species_name
and locus_tag
, but these are coming from two different classes. The easiest thing I can think of would be to add a species_name
directly to the Gene
class, but since each Gene
will be within a Species
, this seems redundant.
I'm not even sure where to begin - I did quite a bit of searching, but the other questions I've found are either not relevant, or I don't understand enough to grasp the relevance.
Create the Gene
objects, then create the Species
object by passing the dictionary for genes
and then update the Gene
objects by setting the Species
instance to it, like so:
for gene in genes:
gene.species = species
After that each Gene
instance ( gene
) can refer to its Species
object by doing self.species
or, for example, self.species.species_name
to get the name.
I would generally not do what you've described. It creates a circular reference, which should be avoided where possible. Objects should generally have a hierarchical relationship. You should be try to find a way to keep a gene and a species reference together where you might need to serialise it.
To be able to serialise a gene you could write a function that takes a species and a gene as arguments. eg.
# if using python 2 (I think you might be)
from __future__ import print_function
class Species(object):
def __init__(self, species_name, genes={}, contigs={}):
self.species_name = species_name
self.genes = genes
self.contigs = contigs
def serialise(self, fileobj):
for gene in self.genes.values():
self.serialise_gene(gene, fileobj)
def serialise_gene(self, gene, fileobj):
print(self.name, gene.locus_tag, sep=", ", file=fileobj)
# prints "name, locus_tag" to the file
s = Species("homo erectus")
with open("some_file.txt", "w") as f:
s.serialise(f)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.