Beispiel #1
0
 /**
  * Performs a breadthFirstSearch in an AtomContainer starting with a particular sphere, which
  * usually consists of one start atom, and searches for a pi system.
  *
  * @param container The AtomContainer to be searched
  * @param sphere A sphere of atoms to start the search with
  * @param path A ArrayList which stores the atoms belonging to the pi system
  * @throws org.openscience.cdk.exception.CDKException Description of the Exception
  */
 private void breadthFirstSearch(IAtomContainer container, List<IAtom> sphere, List<IAtom> path)
     throws CDKException {
   IAtom atom;
   IAtom nextAtom;
   List<IAtom> newSphere = new ArrayList<IAtom>();
   // logger.debug("Start of breadthFirstSearch");
   for (int i = 0; i < sphere.size(); i++) {
     atom = sphere.get(i);
     // logger.debug("BreadthFirstSearch around atom " + (atomNr + 1));
     List<IBond> bonds = container.getConnectedBondsList(atom);
     for (IBond bond : bonds) {
       nextAtom = bond.getConnectedAtom(atom);
       if ((!nextAtom.getFlag(CDKConstants.ISAROMATIC) && !nextAtom.getFlag(CDKConstants.ISINRING))
           & !nextAtom.getFlag(CDKConstants.VISITED)) {
         // logger.debug("BDS> AtomNr:"+container.getAtomNumber(nextAtom)+"
         // maxBondOrder:"+container.getMaximumBondOrder(nextAtom)+"
         // Aromatic:"+nextAtom.getFlag(CDKConstants.ISAROMATIC)+"
         // FormalCharge:"+nextAtom.getFormalCharge()+" Charge:"+nextAtom.getCharge()+"
         // Flag:"+nextAtom.getFlag(CDKConstants.VISITED));
         path.add(nextAtom);
         // logger.debug("BreadthFirstSearch is meeting new atom " + (nextAtomNr + 1));
         nextAtom.setFlag(CDKConstants.VISITED, true);
         if (container.getConnectedBondsCount(nextAtom) > 1) {
           newSphere.add(nextAtom);
         }
       } else {
         nextAtom.setFlag(CDKConstants.VISITED, true);
       }
     }
   }
   if (newSphere.size() > 0) {
     breadthFirstSearch(container, newSphere, path);
   }
 }
 private boolean getIfBondIsNotRotatable(Molecule mol, IBond bond, IAtomContainer detected) {
   boolean isBondNotRotatable = false;
   int counter = 0;
   IAtom atom0 = bond.getAtom(0);
   IAtom atom1 = bond.getAtom(1);
   if (detected != null) {
     if (detected.contains(bond)) counter += 1;
   }
   if (atom0.getFlag(CDKConstants.ISINRING)) {
     if (atom1.getFlag(CDKConstants.ISINRING)) {
       counter += 1;
     } else {
       if (atom1.getSymbol().equals("H")) counter += 1;
       else counter += 0;
     }
   }
   if (atom0.getSymbol().equals("N") && atom1.getSymbol().equals("C")) {
     if (getIfACarbonIsDoubleBondedToAnOxygen(mol, atom1)) counter += 1;
   }
   if (atom0.getSymbol().equals("C") && atom1.getSymbol().equals("N")) {
     if (getIfACarbonIsDoubleBondedToAnOxygen(mol, atom0)) counter += 1;
   }
   if (counter > 0) isBondNotRotatable = true;
   return isBondNotRotatable;
 }
  /**
   * Initiate process. It is needed to call the addExplicitHydrogensToSatisfyValency from the class
   * tools.HydrogenAdder.
   *
   * @param reactants reactants of the reaction
   * @param agents agents of the reaction (Must be in this case null)
   * @exception CDKException Description of the Exception
   */
  @TestMethod("testInitiate_IMoleculeSet_IMoleculeSet")
  public IReactionSet initiate(IMoleculeSet reactants, IMoleculeSet agents) throws CDKException {

    logger.debug("initiate reaction: HeterolyticCleavagePBReaction");

    if (reactants.getMoleculeCount() != 1) {
      throw new CDKException("HeterolyticCleavagePBReaction only expects one reactant");
    }
    if (agents != null) {
      throw new CDKException("HeterolyticCleavagePBReaction don't expects agents");
    }

    IReactionSet setOfReactions =
        DefaultChemObjectBuilder.getInstance().newInstance(IReactionSet.class);
    IMolecule reactant = reactants.getMolecule(0);

    /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/
    IParameterReact ipr = super.getParameterClass(SetReactionCenter.class);
    if (ipr != null && !ipr.isSetParameter()) setActiveCenters(reactant);

    Iterator<IBond> bondis = reactant.bonds().iterator();
    while (bondis.hasNext()) {
      IBond bondi = bondis.next();
      IAtom atom1 = bondi.getAtom(0);
      IAtom atom2 = bondi.getAtom(1);
      if (bondi.getFlag(CDKConstants.REACTIVE_CENTER)
          && bondi.getOrder() != IBond.Order.SINGLE
          && atom1.getFlag(CDKConstants.REACTIVE_CENTER)
          && atom2.getFlag(CDKConstants.REACTIVE_CENTER)
          && (atom1.getFormalCharge() == CDKConstants.UNSET ? 0 : atom1.getFormalCharge()) == 0
          && (atom2.getFormalCharge() == CDKConstants.UNSET ? 0 : atom2.getFormalCharge()) == 0
          && reactant.getConnectedSingleElectronsCount(atom1) == 0
          && reactant.getConnectedSingleElectronsCount(atom2) == 0) {

        /**/
        for (int j = 0; j < 2; j++) {

          ArrayList<IAtom> atomList = new ArrayList<IAtom>();
          if (j == 0) {
            atomList.add(atom1);
            atomList.add(atom2);
          } else {
            atomList.add(atom2);
            atomList.add(atom1);
          }
          ArrayList<IBond> bondList = new ArrayList<IBond>();
          bondList.add(bondi);

          IMoleculeSet moleculeSet = reactant.getBuilder().newInstance(IMoleculeSet.class);
          moleculeSet.addMolecule(reactant);
          IReaction reaction = mechanism.initiate(moleculeSet, atomList, bondList);
          if (reaction == null) continue;
          else setOfReactions.addReaction(reaction);
        }
      }
    }
    return setOfReactions;
  }
Beispiel #4
0
 public boolean matches(IAtom atom) {
   if (atom.getSymbol().equals(this.getSymbol()) && atom.getFlag(CDKConstants.ISAROMATIC)) {
     return true;
   } else {
     return false;
   }
 }
 @Test
 public void testConfigure_IAtom_IAtomType() {
   IAtom atom = new NNAtom(Elements.CARBON);
   IAtomType atomType = new NNAtomType(Elements.CARBON);
   atomType.setFlag(CDKConstants.IS_HYDROGENBOND_ACCEPTOR, true);
   AtomTypeManipulator.configure(atom, atomType);
   Assert.assertEquals(
       atomType.getFlag(CDKConstants.IS_HYDROGENBOND_ACCEPTOR),
       atom.getFlag(CDKConstants.IS_HYDROGENBOND_ACCEPTOR));
 }
Beispiel #6
0
  @Test
  public void testRingFlags1() throws Exception {
    SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
    IAtomContainer molecule = sp.parseSmiles("c1ccccc1");
    new SSSRFinder(molecule).findSSSR();

    int count = 0;
    Iterator atoms = molecule.atoms().iterator();
    while (atoms.hasNext()) {
      IAtom atom = (IAtom) atoms.next();
      if (atom.getFlag(CDKConstants.ISINRING)) count++;
    }
    Assert.assertEquals("All atoms in benzene were not marked as being in a ring", 6, count);
  }
Beispiel #7
0
  /**
   * Prepare the target molecule for analysis.
   *
   * <p>We perform ring perception and aromaticity detection and set up the appropriate properties.
   * Right now, this function is called each time we need to do a query and this is inefficient.
   *
   * @throws CDKException if there is a problem in ring perception or aromaticity detection, which
   *     is usually related to a timeout in the ring finding code.
   */
  private void initializeMolecule() throws CDKException {
    // Code copied from
    // org.openscience.cdk.qsar.descriptors.atomic.AtomValenceDescriptor;
    Map<String, Integer> valencesTable = new HashMap<String, Integer>();
    valencesTable.put("H", 1);
    valencesTable.put("Li", 1);
    valencesTable.put("Be", 2);
    valencesTable.put("B", 3);
    valencesTable.put("C", 4);
    valencesTable.put("N", 5);
    valencesTable.put("O", 6);
    valencesTable.put("F", 7);
    valencesTable.put("Na", 1);
    valencesTable.put("Mg", 2);
    valencesTable.put("Al", 3);
    valencesTable.put("Si", 4);
    valencesTable.put("P", 5);
    valencesTable.put("S", 6);
    valencesTable.put("Cl", 7);
    valencesTable.put("K", 1);
    valencesTable.put("Ca", 2);
    valencesTable.put("Ga", 3);
    valencesTable.put("Ge", 4);
    valencesTable.put("As", 5);
    valencesTable.put("Se", 6);
    valencesTable.put("Br", 7);
    valencesTable.put("Rb", 1);
    valencesTable.put("Sr", 2);
    valencesTable.put("In", 3);
    valencesTable.put("Sn", 4);
    valencesTable.put("Sb", 5);
    valencesTable.put("Te", 6);
    valencesTable.put("I", 7);
    valencesTable.put("Cs", 1);
    valencesTable.put("Ba", 2);
    valencesTable.put("Tl", 3);
    valencesTable.put("Pb", 4);
    valencesTable.put("Bi", 5);
    valencesTable.put("Po", 6);
    valencesTable.put("At", 7);
    valencesTable.put("Fr", 1);
    valencesTable.put("Ra", 2);
    valencesTable.put("Cu", 2);
    valencesTable.put("Mn", 2);
    valencesTable.put("Co", 2);

    // do all ring perception
    AllRingsFinder arf = new AllRingsFinder();
    IRingSet allRings;
    try {
      allRings = arf.findAllRings(atomContainer);
    } catch (CDKException e) {
      logger.debug(e.toString());
      throw new CDKException(e.toString(), e);
    }

    // sets SSSR information
    SSSRFinder finder = new SSSRFinder(atomContainer);
    IRingSet sssr = finder.findEssentialRings();

    for (IAtom atom : atomContainer.atoms()) {

      // add a property to each ring atom that will be an array of
      // Integers, indicating what size ring the given atom belongs to
      // Add SSSR ring counts
      if (allRings.contains(atom)) { // it's in a ring
        atom.setFlag(CDKConstants.ISINRING, true);
        // lets find which ring sets it is a part of
        List<Integer> ringsizes = new ArrayList<Integer>();
        IRingSet currentRings = allRings.getRings(atom);
        int min = 0;
        for (int i = 0; i < currentRings.getAtomContainerCount(); i++) {
          int size = currentRings.getAtomContainer(i).getAtomCount();
          if (min > size) min = size;
          ringsizes.add(size);
        }
        atom.setProperty(CDKConstants.RING_SIZES, ringsizes);
        atom.setProperty(CDKConstants.SMALLEST_RINGS, sssr.getRings(atom));
      } else {
        atom.setFlag(CDKConstants.ISINRING, false);
      }

      // determine how many rings bonds each atom is a part of
      int hCount;
      if (atom.getImplicitHydrogenCount() == CDKConstants.UNSET) hCount = 0;
      else hCount = atom.getImplicitHydrogenCount();

      List<IAtom> connectedAtoms = atomContainer.getConnectedAtomsList(atom);
      int total = hCount + connectedAtoms.size();
      for (IAtom connectedAtom : connectedAtoms) {
        if (connectedAtom.getSymbol().equals("H")) {
          hCount++;
        }
      }
      atom.setProperty(CDKConstants.TOTAL_CONNECTIONS, total);
      atom.setProperty(CDKConstants.TOTAL_H_COUNT, hCount);

      if (valencesTable.get(atom.getSymbol()) != null) {
        int formalCharge =
            atom.getFormalCharge() == CDKConstants.UNSET ? 0 : atom.getFormalCharge();
        atom.setValency(valencesTable.get(atom.getSymbol()) - formalCharge);
      }
    }

    for (IBond bond : atomContainer.bonds()) {
      if (allRings.getRings(bond).getAtomContainerCount() > 0) {
        bond.setFlag(CDKConstants.ISINRING, true);
      }
    }

    for (IAtom atom : atomContainer.atoms()) {
      List<IAtom> connectedAtoms = atomContainer.getConnectedAtomsList(atom);

      int counter = 0;
      IAtom any;
      for (IAtom connectedAtom : connectedAtoms) {
        any = connectedAtom;
        if (any.getFlag(CDKConstants.ISINRING)) {
          counter++;
        }
      }
      atom.setProperty(CDKConstants.RING_CONNECTIONS, counter);
    }

    // check for atomaticity
    try {
      AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(atomContainer);
      CDKHueckelAromaticityDetector.detectAromaticity(atomContainer);
    } catch (CDKException e) {
      logger.debug(e.toString());
      throw new CDKException(e.toString(), e);
    }
  }
  /**
   * Initiate process. It is needed to call the addExplicitHydrogensToSatisfyValency from the class
   * tools.HydrogenAdder.
   *
   * @exception CDKException Description of the Exception
   * @param reactants reactants of the reaction.
   * @param agents agents of the reaction (Must be in this case null).
   */
  @TestMethod("testInitiate_IAtomContainerSet_IAtomContainerSet")
  public IReactionSet initiate(IAtomContainerSet reactants, IAtomContainerSet agents)
      throws CDKException {

    logger.debug("initiate reaction: RearrangementRadicalReaction");

    if (reactants.getAtomContainerCount() != 1) {
      throw new CDKException("RearrangementRadicalReaction only expects one reactant");
    }
    if (agents != null) {
      throw new CDKException("RearrangementRadicalReaction don't expects agents");
    }

    IReactionSet setOfReactions =
        DefaultChemObjectBuilder.getInstance().newInstance(IReactionSet.class);
    IAtomContainer reactant = reactants.getAtomContainer(0);

    /* if the parameter hasActiveCenter is not fixed yet, set the active centers*/
    IParameterReact ipr = super.getParameterClass(SetReactionCenter.class);
    if (ipr != null && !ipr.isSetParameter()) setActiveCenters(reactant);

    Iterator<IAtom> atoms = reactants.getAtomContainer(0).atoms().iterator();
    while (atoms.hasNext()) {
      IAtom atomi = atoms.next();
      if (atomi.getFlag(CDKConstants.REACTIVE_CENTER)
          && reactant.getConnectedSingleElectronsCount(atomi) == 1) {

        Iterator<IBond> bondis = reactant.getConnectedBondsList(atomi).iterator();

        while (bondis.hasNext()) {
          IBond bondi = bondis.next();

          if (bondi.getFlag(CDKConstants.REACTIVE_CENTER)
              && bondi.getOrder() == IBond.Order.SINGLE) {

            IAtom atomj = bondi.getConnectedAtom(atomi);
            if (atomi.getFlag(CDKConstants.REACTIVE_CENTER)
                && (atomj.getFormalCharge() == CDKConstants.UNSET ? 0 : atomj.getFormalCharge())
                    == 0
                && reactant.getConnectedSingleElectronsCount(atomj) == 0) {

              Iterator<IBond> bondjs = reactant.getConnectedBondsList(atomj).iterator();
              while (bondjs.hasNext()) {
                IBond bondj = bondjs.next();

                if (bondj.equals(bondi)) continue;

                if (bondj.getFlag(CDKConstants.REACTIVE_CENTER)
                    && bondj.getOrder() == IBond.Order.DOUBLE) {

                  IAtom atomk = bondj.getConnectedAtom(atomj);
                  if (atomk.getFlag(CDKConstants.REACTIVE_CENTER)
                      && (atomk.getFormalCharge() == CDKConstants.UNSET
                              ? 0
                              : atomk.getFormalCharge())
                          == 0
                      && reactant.getConnectedSingleElectronsCount(atomk) == 0) {

                    ArrayList<IAtom> atomList = new ArrayList<IAtom>();
                    atomList.add(atomi);
                    atomList.add(atomj);
                    atomList.add(atomk);
                    ArrayList<IBond> bondList = new ArrayList<IBond>();
                    bondList.add(bondi);
                    bondList.add(bondj);

                    IAtomContainerSet moleculeSet =
                        reactant.getBuilder().newInstance(IAtomContainerSet.class);
                    moleculeSet.addAtomContainer(reactant);
                    IReaction reaction = mechanism.initiate(moleculeSet, atomList, bondList);
                    if (reaction == null) continue;
                    else setOfReactions.addReaction(reaction);
                  }
                }
              }
            }
          }
        }
      }
    }
    return setOfReactions;
  }
Beispiel #9
0
  /**
   * Calculate the count of atoms of the largest chain in the supplied {@link IAtomContainer}.
   *
   * <p>
   *
   * <p>The method require two parameters:
   *
   * <ol>
   *   <li>if checkAromaticity is true, the method check the aromaticity,
   *   <li>if false, means that the aromaticity has already been checked
   * </ol>
   *
   * <p>
   *
   * <p>Same for checkRingSystem, if true the CDKConstant.ISINRING will be set
   *
   * @param atomContainer The {@link AtomContainer} for which this descriptor is to be calculated
   * @return the number of atoms in the largest chain of this AtomContainer
   * @see #setParameters
   */
  @TestMethod("testCalculate_IAtomContainer")
  public DescriptorValue calculate(IAtomContainer atomContainer) {
    IAtomContainer container;
    try {
      container = (IAtomContainer) atomContainer.clone();
    } catch (CloneNotSupportedException e) {
      return getDummyDescriptorValue(e);
    }

    // logger.debug("LargestChainDescriptor");
    boolean[] originalFlag4 = new boolean[container.getAtomCount()];
    for (int i = 0; i < originalFlag4.length; i++) {
      originalFlag4[i] = container.getAtom(i).getFlag(4);
    }
    if (checkRingSystem) {
      IRingSet rs;
      try {
        rs = new SpanningTree(container).getBasicRings();
      } catch (NoSuchAtomException e) {
        return getDummyDescriptorValue(e);
      }
      for (int i = 0; i < container.getAtomCount(); i++) {
        if (rs.contains(container.getAtom(i))) {
          container.getAtom(i).setFlag(CDKConstants.ISINRING, true);
        }
      }
    }

    if (checkAromaticity) {
      try {
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(container);
        CDKHueckelAromaticityDetector.detectAromaticity(container);
      } catch (CDKException e) {
        return getDummyDescriptorValue(e);
      }
    }

    // get rid of hydrogens in our local copy
    container = AtomContainerManipulator.removeHydrogens(container);

    int largestChainAtomsCount = 0;
    // IAtom[] atoms = container.getAtoms();
    ArrayList<IAtom> startSphere;
    ArrayList<IAtom> path;
    // Set all VisitedFlags to False
    for (int i = 0; i < container.getAtomCount(); i++) {
      container.getAtom(i).setFlag(CDKConstants.VISITED, false);
    }

    // logger.debug("Set all atoms to Visited False");
    for (int i = 0; i < container.getAtomCount(); i++) {
      IAtom atomi = container.getAtom(i);
      // chain sp3
      // logger.debug("atom:"+i+" maxBondOrder:"+container.getMaximumBondOrder(atoms[i])+"
      // Aromatic:"+atoms[i].getFlag(CDKConstants.ISAROMATIC)+"
      // Ring:"+atoms[i].getFlag(CDKConstants.ISINRING)+"
      // FormalCharge:"+atoms[i].getFormalCharge()+" Charge:"+atoms[i].getCharge()+"
      // Flag:"+atoms[i].getFlag(CDKConstants.VISITED));
      if ((!atomi.getFlag(CDKConstants.ISAROMATIC) && !atomi.getFlag(CDKConstants.ISINRING))
          & !atomi.getFlag(CDKConstants.VISITED)) {
        // logger.debug("...... -> containercepted");
        startSphere = new ArrayList<IAtom>();
        path = new ArrayList<IAtom>();
        startSphere.add(atomi);
        try {
          breadthFirstSearch(container, startSphere, path);
        } catch (CDKException e) {
          return getDummyDescriptorValue(e);
        }
        if (path.size() > largestChainAtomsCount) {
          largestChainAtomsCount = path.size();
        }
      }
    }

    return new DescriptorValue(
        getSpecification(),
        getParameterNames(),
        getParameters(),
        new IntegerResult(largestChainAtomsCount),
        getDescriptorNames());
  }
    public String perceiveSybylAtomTypes(IMolecule mol)
                        throws InvocationTargetException {
        
        ICDKMolecule cdkmol;
        
        try {
            cdkmol = cdk.asCDKMolecule(mol);
        } 
        catch (BioclipseException e) {
            System.out.println("Error converting cdk10 to cdk");
            e.printStackTrace();
            throw new InvocationTargetException(e);
        }
        
        IAtomContainer ac = cdkmol.getAtomContainer();
        CDKAtomTypeMatcher cdkMatcher 
            = CDKAtomTypeMatcher.getInstance(ac.getBuilder());
        AtomTypeMapper mapper 
            = AtomTypeMapper.getInstance(
                 "org/openscience/cdk/dict/data/cdk-sybyl-mappings.owl" );

        IAtomType[] sybylTypes = new IAtomType[ac.getAtomCount()];
        
        int atomCounter = 0;
        int a=0;
        for (IAtom atom : ac.atoms()) {
            IAtomType type;
            try {
                type = cdkMatcher.findMatchingAtomType(ac, atom);
            } 
            catch (CDKException e) {
                type = null;
            }
            if (type==null) {
//                logger.debug("AT null for atom: " + atom);
                type = atom.getBuilder().newAtomType(atom.getSymbol());
                type.setAtomTypeName("X");
            }
            AtomTypeManipulator.configure(atom, type);
            a++;
        }
        try {
            CDKHueckelAromaticityDetector.detectAromaticity(ac);
//            System.out.println("Arom: " 
//                + CDKHueckelAromaticityDetector.detectAromaticity(ac) );
		    } 
        catch (CDKException e) {
			    logger.debug("Failed to perceive aromaticity: " + e.getMessage());
		    }
        for (IAtom atom : ac.atoms()) {
            String mappedType = mapper.mapAtomType(atom.getAtomTypeName());
            if ("C.2".equals(mappedType)
                    && atom.getFlag(CDKConstants.ISAROMATIC)) {
                mappedType = "C.ar";
            } 
            else if ("N.pl3".equals(mappedType)
                    && atom.getFlag(CDKConstants.ISAROMATIC)) {
                mappedType = "N.ar";
            }
            try {
                sybylTypes[atomCounter] = factory.getAtomType(mappedType);
		        } 
            catch (NoSuchAtomTypeException e) {
                // yes, setting null's here is important
                sybylTypes[atomCounter] = null; 
			      }
            atomCounter++;
        }
        StringBuffer result = new StringBuffer();
        // now that full perception is finished, we can set atom type names:
        for (int i = 0; i < sybylTypes.length; i++) {
            if (sybylTypes[i] != null) {
                ac.getAtom(i).setAtomTypeName(sybylTypes[i].getAtomTypeName());
            } 
            else {
                ac.getAtom(i).setAtomTypeName("X");
            }
            
            result.append(i).append(':').append(ac.getAtom(i).getAtomTypeName())
                  /*.append("\n")*/;

        }
        return result.toString();
    }
 private boolean isRingAtom(IAtom atom) {
   return atom.getFlag(CDKConstants.ISINRING);
 }
 private boolean isAliphaticAtom(IAtom atom) {
   return atom.getFlag(CDKConstants.ISALIPHATIC);
 }