public class MoleculeAnalyzer
extends java.lang.Object
Constructor and Description |
---|
MoleculeAnalyzer(BondedAtom molecule)
Creates an MoleculeAnalyzer for analyzing a given molecule.
|
Modifier and Type | Method and Description |
---|---|
java.util.ArrayList<BondedAtom> |
findAllAtoms(BondedAtom current,
java.util.ArrayList<BondedAtom> atoms)
Recursively adds connected atoms to the allAtoms list.
|
java.util.List<BondedAtom> |
findPath(BondedAtom start,
BondedAtom end)
Find a path between two atoms in the molecule.
|
java.util.List<BondedAtom> |
findPath(BondedAtom current,
BondedAtom end,
java.util.List<BondedAtom> path)
Helper function to recursively find a path between two atoms in the molecule.
|
java.util.List<BondedAtom> |
getAllAtoms()
Return the list of all atoms in this molecule.
|
java.util.List<java.util.List<BondedAtom>> |
getBackbones()
Find all possible backbones in a linear molecule.
|
java.lang.String |
getFormula()
Gets a chemical formula for this molecule.
|
java.lang.String |
getIupacName()
Names the molecule according to IUPAC rules for organic compounds.
|
java.util.List<BondedAtom> |
getLinearBackbone()
Identify the linear backbone of the molecule.
|
double |
getMolecularWeight()
Determines the total molecular weight of this molecule.
|
java.util.List<BondedAtom> |
getRing()
Searches the molecule for a ring.
|
java.util.List<BondedAtom> |
getRing(BondedAtom current,
java.util.List<BondedAtom> visited)
Helper function to search the molecule for a ring from a specific starting point.
|
java.util.List<BondedAtom> |
getTips()
Find all atoms that are molecule tips: carbons that are bonded to at most one other carbon.
|
boolean |
hasChargedAtoms()
Determines whether this molecule contains any charged atoms.
|
java.util.List<BondedAtom> |
rotateRing(java.util.List<BondedAtom> ring)
Rotate a backbone ring into the correct position for naming.
|
public MoleculeAnalyzer(BondedAtom molecule)
molecule
- an atom belonging to the molecule that will be analyzed.public java.util.List<BondedAtom> getAllAtoms()
This is a convenience method used by the test suite's helper tests.
public java.util.ArrayList<BondedAtom> findAllAtoms(BondedAtom current, java.util.ArrayList<BondedAtom> atoms)
This is recursive graph traversal.
current
- the atom we're currently examiningatoms
- list of all atoms we've found so farpublic double getMolecularWeight()
Computes molecular weight by summing the weights of all the atoms that comprise the molecule.
public boolean hasChargedAtoms()
Charged atoms have a different total number of bonds than their valence. For example, an oxygen atom with three bonds is charged.
Note that this should be easy to complete once you have found all atoms in the molecule and added them to the allAtoms list.
public java.util.List<BondedAtom> getRing()
Note that if this returns non-null (indicating that there is a ring), getIupacName will
pass the list to rotateRing
and use that function's output
as the cyclic backbone.
This is cycle detection.
public java.util.List<BondedAtom> getRing(BondedAtom current, java.util.List<BondedAtom> visited)
This is cycle detection.
current
- the current atom we are examining.visited
- a list of previously-visited atom. The previous atom is the last in the list.public java.util.List<BondedAtom> getLinearBackbone()
See the chemistry tutorial in the MP writeup for how to determine the best backbone.
public java.util.List<BondedAtom> getTips()
Note that tips can only be bonded to one other carbon but may also be bonded to other atoms.
Note that this should be easy to complete once you have found all atoms in the molecule and added them to the allAtoms list.
This is similar to searching for leaf vertices in a graph.
public java.util.List<java.util.List<BondedAtom>> getBackbones()
To do this, first find all tip carbons, and then find all paths between them. So this function uses both getTips and findPath.
public java.util.List<BondedAtom> findPath(BondedAtom start, BondedAtom end)
This function will only produce a meaningful result on non-cyclic molecules where there is only one path between any two molecules.
This is graph pathfinding, simplified by being run on a non-cyclic graph.
start
- the atom to start fromend
- the atom to end atpublic java.util.List<BondedAtom> findPath(BondedAtom current, BondedAtom end, java.util.List<BondedAtom> path)
This function will only produce a meaningful result on non-cyclic molecules where there is only one path between any two molecules.
This is graph pathfinding, simplified by being run on a non-cyclic graph.
current
- the current atom we are examiningend
- the atom to end atpath
- the atoms we've already visited on our way to the current atompublic java.util.List<BondedAtom> rotateRing(java.util.List<BondedAtom> ring)
Note that if getRing
returns non-null, its output will be passed to this
function by getIupacName. Then this function's output will be used as the cyclic backbone
for naming the molecule.
This doesn't necessarily have a strong analogy to graphs, but it's pretty fun.
ring
- the backbone ring to rotate, never nullpublic java.lang.String getIupacName()
See the MP page for information on naming. This function will not work until you complete the functions above: getRing, getLinearBackbone, and rotateRing.
public java.lang.String getFormula()