CIPLigandRule implements ISequenceSubRule<ILigand> { @Override public int compare(ILigand ligand1, ILigand ligand2) { int numberComp = numberRule.compare(ligand1, ligand2); if (numberComp != 0) return numberComp; ILigand[] ligand1Ligands = CIPTool.getLigandLigands(ligand1); ILigand[] ligand2Ligands = CIPTool.getLigandLigands(ligand2); if (ligand1Ligands.length == 0 && ligand2Ligands.length == 0) return 0; if (ligand1Ligands.length == 0) return -1; if (ligand2Ligands.length == 0) return 1; int minLigandCount = Math.min(ligand1Ligands.length, ligand2Ligands.length); if (ligand1Ligands.length > 1) ligand1Ligands = order(ligand1Ligands); if (ligand2Ligands.length > 1) ligand2Ligands = order(ligand2Ligands); for (int i = 0; i < minLigandCount; i++) { int comparison = numberRule.compare(ligand1Ligands[i], ligand2Ligands[i]); if (comparison != 0) return comparison; } if (ligand1Ligands.length == ligand2Ligands.length) { for (int i = 0; i < minLigandCount; i++) { int comparison = compare(ligand1Ligands[i], ligand2Ligands[i]); if (comparison != 0) return comparison; } } if (ligand1Ligands.length > ligand2Ligands.length) return 1; else if (ligand1Ligands.length < ligand2Ligands.length) return -1; else return 0; }  @Override int compare(ILigand ligand1, ILigand ligand2);  }
@Test public void testCBrIFCl() throws Exception { IAtomContainer molecule = smiles.parseSmiles("FC(Br)(Cl)I"); ILigand ligandF = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(0)); ILigand ligandBr = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(2)); ILigand ligandCl = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(3)); ILigand ligandI = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(4)); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(-1, rule.compare(ligandF, ligandI)); Assert.assertEquals(-1, rule.compare(ligandF, ligandBr)); Assert.assertEquals(-1, rule.compare(ligandF, ligandCl)); Assert.assertEquals(-1, rule.compare(ligandCl, ligandI)); Assert.assertEquals(-1, rule.compare(ligandCl, ligandBr)); Assert.assertEquals(-1, rule.compare(ligandBr, ligandI)); List<ILigand> ligands = new ArrayList<ILigand>(); ligands.add(ligandI); ligands.add(ligandBr); ligands.add(ligandF); ligands.add(ligandCl); Collections.sort(ligands, new CIPLigandRule()); Assert.assertEquals("F", ligands.get(0).getLigandAtom().getSymbol()); Assert.assertEquals("Cl", ligands.get(1).getLigandAtom().getSymbol()); Assert.assertEquals("Br", ligands.get(2).getLigandAtom().getSymbol()); Assert.assertEquals("I", ligands.get(3).getLigandAtom().getSymbol()); }
@Test public void testCompare_Identity() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CC(Br)([13C])[H]"); ILigand ligand = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(0)); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(0, rule.compare(ligand, ligand)); }
@Test public void testCompare() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CC(Br)([13C])[H]"); ILigand ligand1 = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(0)); ILigand ligand2 = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(2)); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(-1, rule.compare(ligand1, ligand2)); Assert.assertEquals(1, rule.compare(ligand2, ligand1)); }
@Test public void testSideChains() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CC(C)C([H])(C)CC"); ILigand ligand1 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 3, 6); ILigand ligand2 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 3, 1); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(-1, rule.compare(ligand1, ligand2)); Assert.assertEquals(1, rule.compare(ligand2, ligand1)); }
@Test public void testSideChains_Recursive() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CCCC([H])(C)CC"); ILigand ligand1 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 3, 6); ILigand ligand2 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 3, 1); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(-1, rule.compare(ligand1, ligand2)); Assert.assertEquals(1, rule.compare(ligand2, ligand1)); }
@Test public void testTwoVersusDoubleBondedOxygen() throws Exception { IAtomContainer molecule = smiles.parseSmiles("OC(O)C([H])(C)C=O"); ILigand ligand1 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 3, 1); ILigand ligand2 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 3, 6); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(-1, rule.compare(ligand1, ligand2)); Assert.assertEquals(1, rule.compare(ligand2, ligand1)); }
@Test public void testDeepRecursion() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CC([H])(CCCCCCCCCC)CCCCCCCCC"); ILigand ligand1 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 1, 3); ILigand ligand2 = CIPTool.defineLigand(molecule, new VisitedAtoms(), 1, 13); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(1, rule.compare(ligand1, ligand2)); Assert.assertEquals(-1, rule.compare(ligand2, ligand1)); }
@Test public void testImplicitHydrogen_Same() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CC(Br)([13C])[H]"); ILigand ligand1 = new ImplicitHydrogenLigand(molecule, new VisitedAtoms(), molecule.getAtom(1)); ILigand ligand2 = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(4)); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(0, rule.compare(ligand1, ligand2)); Assert.assertEquals(0, rule.compare(ligand2, ligand1)); }
@Test public void testImplicitHydrogen() throws Exception { IAtomContainer molecule = smiles.parseSmiles("CC(Br)([2H])[H]"); ILigand ligand1 = new ImplicitHydrogenLigand(molecule, new VisitedAtoms(), molecule.getAtom(1)); ILigand ligand2 = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(3)); ISequenceSubRule<ILigand> rule = new CIPLigandRule(); Assert.assertEquals(-1, rule.compare(ligand1, ligand2)); Assert.assertEquals(1, rule.compare(ligand2, ligand1)); ligand2 = new Ligand(molecule, new VisitedAtoms(), molecule.getAtom(1), molecule.getAtom(2)); Assert.assertEquals(-1, rule.compare(ligand1, ligand2)); Assert.assertEquals(1, rule.compare(ligand2, ligand1)); }