Class MoleculeAnalyzer
- java.lang.Object
-
- edu.illinois.cs.cs125.mp5.lib.MoleculeAnalyzer
-
public class MoleculeAnalyzer extends java.lang.Object
Analyzes a given organic molecule.
-
-
Constructor Summary
Constructors Constructor Description MoleculeAnalyzer(BondedAtom molecule)
Creates an MoleculeAnalyzer for analyzing a given molecule.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
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)
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.boolean
isRing()
Returns whether the molecule is a ring.java.util.List<BondedAtom>
rotateRing(java.util.List<BondedAtom> ring)
Rotate a backbone ring into the correct position for naming.
-
-
-
Constructor Detail
-
MoleculeAnalyzer
public MoleculeAnalyzer(BondedAtom molecule)
Creates an MoleculeAnalyzer for analyzing a given molecule.- Parameters:
molecule
- an atom belonging to the molecule that will be analyzed.
-
-
Method Detail
-
getAllAtoms
public java.util.List<BondedAtom> getAllAtoms()
Return the list of all atoms in this molecule.This is a convenience method used by the test suite.
- Returns:
- a list of all atoms in this molecule.
-
findAllAtoms
public java.util.ArrayList<BondedAtom> findAllAtoms(BondedAtom current, java.util.ArrayList<BondedAtom> atoms)
Recursively adds connected atoms to the allAtoms list.This is recursive graph traversal.
- Parameters:
current
- the atom we're currently examiningatoms
- list of all atoms we've found so far- Returns:
- all atoms found in the molecule
- See Also:
- Graph Traversal
-
getMolecularWeight
public double getMolecularWeight()
Determines the total molecular weight of this molecule.Computes molecular weight by summing the weights of all the atoms that comprise the molecule.
- Returns:
- the molecular weight of the molecule in grams per mole
-
hasChargedAtoms
public boolean hasChargedAtoms()
Determines whether this molecule contains any charged atoms.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.
- Returns:
- true if there is at least one charged atom in the molecule, false otherwise
-
isRing
public boolean isRing()
Returns whether the molecule is a ring.Used by the test suite for debugging.
- Returns:
- true if the molecule is a ring and false otherwise
-
getRing
public java.util.List<BondedAtom> getRing()
Searches the molecule for a ring.This is cycle detection.
- Returns:
- a list containing the atoms in the ring if a ring exists, null otherwise
- See Also:
- Cycle Detection
-
getRing
public java.util.List<BondedAtom> getRing(BondedAtom current, java.util.List<BondedAtom> visited)
Search the molecule for a ring from a specific starting point.This is cycle detection.
- Parameters:
current
- the current atom we are examining.visited
- a list of previously-visited atom. The previous atom is the last in the list.- Returns:
- a list containing the atoms in the ring if a ring exists, null otherwise
- See Also:
- Cycle Detection
-
getLinearBackbone
public java.util.List<BondedAtom> getLinearBackbone()
Identify the linear backbone of the molecule.- Returns:
- the list of atoms constituting the linear backbone of this atom
-
getTips
public java.util.List<BondedAtom> getTips()
Find all atoms that are molecule tips: carbons that are bonded to at most one other carbon.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 Set.
This is searching for leaf vertices in a graph.
- Returns:
- a list of all BondedAtoms that are tips of this molecule, which may be empty if it is a simple ring.
- See Also:
- Leaf Vertex
-
getBackbones
public java.util.List<java.util.List<BondedAtom>> getBackbones()
Find all possible backbones in a linear molecule.To do this, first find all tip carbons, and then find all paths between them. So this function uses both getTips and findPath.
- Returns:
- a list of all possible backbones, each itself a list of atoms
-
findPath
public java.util.List<BondedAtom> findPath(BondedAtom start, BondedAtom end)
Find a path between two atoms in the molecule.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.
- Parameters:
start
- the atom to start fromend
- the atom to end at- Returns:
- the path from the start atom to the end atom
- See Also:
- Graph Pathfinding
-
findPath
public java.util.List<BondedAtom> findPath(BondedAtom current, BondedAtom end, java.util.List<BondedAtom> path)
Recursively find a path between two atoms in the molecule.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.
- Parameters:
current
- the current atom we are examiningend
- the atom to end atpath
- the atoms we've already visited on our way to the current atom- Returns:
- the path from the current atom to the end atom
- See Also:
- Graph Pathfinding
-
rotateRing
public java.util.List<BondedAtom> rotateRing(java.util.List<BondedAtom> ring)
Rotate a backbone ring into the correct position for naming.This doesn't necessarily have a strong analogy to graphs, but it's pretty fun.
- Parameters:
ring
- the backbone ring to rotate- Returns:
- the backbone ring rotated into the correct position
-
getIupacName
public java.lang.String getIupacName()
Names the molecule according to IUPAC rules for organic compounds.See the MP page for information on naming. This function will not work until you complete the functions above: getRing, getLinearBackbone, and rotateRing.
- Returns:
- The systematic IUPAC name of the molecule.
-
getFormula
public java.lang.String getFormula()
Gets a chemical formula for this molecule. This function is optional and not tested by the test suite; it is only used by the app. You may use any formula format that you like.- Returns:
- A chemical formula indicating the allAtoms the molecule contains.
-
-