コード例 #1
0
ファイル: MDMoleculeTest.java プロジェクト: jonalv/cdk
  /** Test an MDMolecule with residues and charge groups */
  @Test
  public void testMDMolecule() {

    MDMolecule mol = new MDMolecule();
    mol.addAtom(new Atom("C")); // 0
    mol.addAtom(new Atom("C")); // 1
    mol.addAtom(new Atom("C")); // 2
    mol.addAtom(new Atom("C")); // 3
    mol.addAtom(new Atom("C")); // 4
    mol.addAtom(new Atom("C")); // 5

    mol.addBond(0, 1, IBond.Order.SINGLE); // 1
    mol.addBond(1, 2, IBond.Order.DOUBLE); // 2
    mol.addBond(2, 3, IBond.Order.SINGLE); // 3
    mol.addBond(3, 4, IBond.Order.DOUBLE); // 4
    mol.addBond(4, 5, IBond.Order.SINGLE); // 5
    mol.addBond(5, 0, IBond.Order.DOUBLE); // 6

    // Create 2 residues
    AtomContainer ac = new AtomContainer();
    ac.addAtom(mol.getAtom(0));
    ac.addAtom(mol.getAtom(1));
    ac.addAtom(mol.getAtom(2));
    Residue res1 = new Residue(ac, 0, mol);
    res1.setName("myResidue1");
    mol.addResidue(res1);

    AtomContainer ac2 = new AtomContainer();
    ac2.addAtom(mol.getAtom(3));
    ac2.addAtom(mol.getAtom(4));
    ac2.addAtom(mol.getAtom(5));
    Residue res2 = new Residue(ac2, 1, mol);
    res2.setName("myResidue2");
    mol.addResidue(res2);

    // Test residue creation
    Assert.assertEquals(res1.getParentMolecule(), mol);
    Assert.assertEquals(res2.getParentMolecule(), mol);
    Assert.assertEquals(res1.getAtomCount(), 3);
    Assert.assertEquals(res2.getAtomCount(), 3);
    Assert.assertEquals(res1.getName(), "myResidue1");
    Assert.assertEquals(res2.getName(), "myResidue2");
    Assert.assertNotNull(mol.getResidues());
    Assert.assertEquals(mol.getResidues().size(), 2);
    Assert.assertEquals(mol.getResidues().get(0), res1);
    Assert.assertEquals(mol.getResidues().get(1), res2);

    // Create 2 chargegroups
    AtomContainer ac3 = new AtomContainer();
    ac3.addAtom(mol.getAtom(0));
    ac3.addAtom(mol.getAtom(1));
    ChargeGroup chg1 = new ChargeGroup(ac3, 0, mol);
    mol.addChargeGroup(chg1);

    AtomContainer ac4 = new AtomContainer();
    ac4.addAtom(mol.getAtom(2));
    ac4.addAtom(mol.getAtom(3));
    ac4.addAtom(mol.getAtom(4));
    ac4.addAtom(mol.getAtom(5));
    ChargeGroup chg2 = new ChargeGroup(ac4, 1, mol);
    mol.addChargeGroup(chg2);

    // Test chargegroup creation
    Assert.assertEquals(chg1.getParentMolecule(), mol);
    Assert.assertEquals(chg2.getParentMolecule(), mol);
    Assert.assertEquals(chg1.getAtomCount(), 2);
    Assert.assertEquals(chg2.getAtomCount(), 4);

    Assert.assertNotNull(mol.getChargeGroups());
    Assert.assertEquals(mol.getChargeGroups().size(), 2);
    Assert.assertEquals(mol.getChargeGroups().get(0), chg1);
    Assert.assertEquals(mol.getChargeGroups().get(1), chg2);
  }
コード例 #2
0
ファイル: MDMoleculeTest.java プロジェクト: jonalv/cdk
  @Test
  public void testMDMoleculeCustomizationRoundtripping() throws Exception {
    StringWriter writer = new StringWriter();

    CMLWriter cmlWriter = new CMLWriter(writer);
    cmlWriter.registerCustomizer(new MDMoleculeCustomizer());
    MDMolecule molecule = makeMDBenzene();
    cmlWriter.write(molecule);

    String serializedMol = writer.toString();
    logger.debug("****************************** testMDMoleculeCustomizationRoundtripping()");
    logger.debug(serializedMol);
    logger.debug("******************************");
    logger.debug("****************************** testMDMoleculeCustomization Write first");
    logger.debug(serializedMol);
    logger.debug("******************************");

    CMLReader reader = new CMLReader(new ByteArrayInputStream(serializedMol.getBytes()));
    reader.registerConvention("md:mdMolecule", new MDMoleculeConvention(new ChemFile()));
    IChemFile file = (IChemFile) reader.read(new ChemFile());
    List containers = ChemFileManipulator.getAllAtomContainers(file);
    Assert.assertEquals(1, containers.size());

    Object molecule2 = containers.get(0);
    Assert.assertTrue(molecule2 instanceof MDMolecule);
    MDMolecule mdMol = (MDMolecule) molecule2;

    Assert.assertEquals(6, mdMol.getAtomCount());
    Assert.assertEquals(6, mdMol.getBondCount());

    List residues = mdMol.getResidues();
    Assert.assertEquals(2, residues.size());
    Assert.assertEquals(3, ((Residue) residues.get(0)).getAtomCount());
    Assert.assertEquals(3, ((Residue) residues.get(1)).getAtomCount());
    Assert.assertEquals("myResidue1", ((Residue) residues.get(0)).getName());
    Assert.assertEquals("myResidue2", ((Residue) residues.get(1)).getName());
    Assert.assertEquals(0, ((Residue) residues.get(0)).getNumber());
    Assert.assertEquals(1, ((Residue) residues.get(1)).getNumber());

    List chargeGroup = mdMol.getChargeGroups();
    Assert.assertEquals(2, chargeGroup.size());
    Assert.assertEquals(2, ((ChargeGroup) chargeGroup.get(0)).getAtomCount());
    Assert.assertEquals(4, ((ChargeGroup) chargeGroup.get(1)).getAtomCount());
    Assert.assertNotNull(((ChargeGroup) chargeGroup.get(0)).getSwitchingAtom());
    Assert.assertEquals("a2", ((ChargeGroup) chargeGroup.get(0)).getSwitchingAtom().getID());
    Assert.assertNotNull(((ChargeGroup) chargeGroup.get(1)).getSwitchingAtom());
    Assert.assertEquals("a5", ((ChargeGroup) chargeGroup.get(1)).getSwitchingAtom().getID());

    Assert.assertEquals(2, ((ChargeGroup) chargeGroup.get(0)).getNumber());
    Assert.assertEquals(3, ((ChargeGroup) chargeGroup.get(1)).getNumber());

    writer = new StringWriter();

    cmlWriter = new CMLWriter(writer);
    cmlWriter.registerCustomizer(new MDMoleculeCustomizer());
    cmlWriter.write(mdMol);

    String serializedMDMol = writer.toString();
    logger.debug("****************************** testMDMoleculeCustomizationRoundtripping()");
    logger.debug(serializedMol);
    logger.debug("******************************");
    logger.debug("****************************** testMDMoleculeCustomization Write second");
    logger.debug(serializedMDMol);
    logger.debug("******************************");

    Assert.assertEquals(serializedMol, serializedMDMol);
  }