As per Should I ask my question about which data structure to use here? I'm asking this here. Hopefully this isn't too implementation specific.
I'm currently developing a program that will represent chemical structures (and eventually allow manipulations/reactions of them). I've hashed out two different systems of storing this data but I'm not sure how to pick which one.
Option 1: Nested matrices
Chemicals have (more or less) definite structures and relative positions of items, and it seems as though it would be easier to represent something like that with a matrix - then I can assign definite 'coordinates' to each atom or bond. Unfortunately, you end up with some overlap if you have a structure that branches out a fair bit. To solve this I ended up working out a solution that used nested, 5x5 matrices.
[[None, None, [CH3], None, None ],
[None, None, Bond, None, None ],
[[CH3], Bond, C, Bond, [CH3]],
[None, None, Bond, None, None ],
[None, None, [CH3], None, None ]]
Here C
represents the element carbon (this could be represented a couple different ways, and isn't particularly relevant here). Bond
represents a bond, and [CH3]
represents another 5x5 matrix. This is the uppermost ([0][2]
) CH3
[[None, None, H, None, None],
[None, None, Bond, None, None],
[H, Bond, C, Bond, H ],
[None, None, Bond, None, None],
[None, None, Root, None, None]]
Here the lowermost Bond
is a reference to the same object as the uppermost Bond
in the first structure, while Root
is (some undetermined) way of relating that this is where the matrix connects to the next level up. If this were a more complex structure, one of the H
s would be replaced by another matrix, such as an additional [CH3]
.
Advantages of this method:
- Everything is explicitly laid out, including structure, which is good according to The Zen of Python.
- When these chemicals are (eventually) represented graphically it will (theoretically) be much easier to make sure everything is in its place
Disadvantages:
- This isn't readable at all if you have a more complex structure
- This is nested, not flat
- This is going to require a lot of recursion to walk through
Option 2: Dictionaries
Another way I could represent these with significantly less nesting and better readability would be using dictionaries. Each molecule would be a key in a dictionary {Molecule1 : Data, Molecule2 : Data}
and the data would be dictionaries as well Atoms: Data, Bonds: Data
. This requires quite a bit less nesting, but would require a little more work when I eventually want to display them.
Closing thoughts
Before asking this question, and indeed while writing it, I've been pretty set on dictionaries. It seems like a cleaner and easier method of storing this data, and won't require figuring out a really nasty recursive technique to walk through a molecule. However I wouldn't be surprised if I missed something which is why I'm asking here.
If this seems too implementation specific please let me know what you think I should change and I'll do my best to make it more general