Search in sources :

Example 1 with IAtomContainerSet

use of org.openscience.cdk.interfaces.IAtomContainerSet in project MetFragRelaunched by ipb-halle.

the class SDFDeuteriumGeneration method main.

public static void main(String[] args) throws Exception {
    boolean withAromaticRings = false;
    boolean combinatorial = true;
    /*
		 * read input inchis
		 */
    ArrayList<String> inchis = new ArrayList<String>();
    ArrayList<Integer> numberToAddDeuteriums = new ArrayList<Integer>();
    ArrayList<String> identifiers = new ArrayList<String>();
    BufferedReader breader = new BufferedReader(new FileReader(new File(args[0])));
    String line = "";
    int identifier = 1;
    while ((line = breader.readLine()) != null) {
        String[] tmp = line.trim().split("\\s+");
        inchis.add(tmp[0].trim());
        if (tmp.length >= 2)
            numberToAddDeuteriums.add(Integer.parseInt(tmp[1].trim()));
        if (tmp.length == 3)
            identifiers.add(tmp[2].trim());
        else
            identifiers.add(identifier + "");
        identifier++;
    }
    breader.close();
    /*
		 * generate deuterated version of inchi
		 */
    IAtomContainerSet set = new AtomContainerSet();
    SmilesGenerator sg = new SmilesGenerator(SmiFlavor.Generic);
    CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
    for (int j = 0; j < inchis.size(); j++) {
        /*
			 * build the inchi atom container
			 */
        IAtomContainer its = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance()).getAtomContainer();
        AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(its);
        try {
            adder.addImplicitHydrogens(its);
        } catch (Exception e) {
            System.out.println("missed " + inchis.get(j) + " " + identifiers.get(j));
            continue;
        }
        AtomContainerManipulator.convertImplicitToExplicitHydrogens(its);
        int numberDeuteriums = 0;
        int numberDeuteriumsEasilyExchanged = 0;
        int numberDeuteriumsAromaticExchanged = 0;
        int[] toExchange = searchForDeuteriumExchangeablePositions(new String[] { "O", "N", "S" }, its);
        if (!combinatorial || (numberToAddDeuteriums.size() == 0 || toExchange.length <= numberToAddDeuteriums.get(j))) {
            for (int i = 0; i < toExchange.length; i++) {
                int numberExchanged = setAllExplicitDeuteriums(its, i);
                numberDeuteriums += numberExchanged;
                numberDeuteriumsEasilyExchanged += numberExchanged;
            }
        } else if (toExchange.length > numberToAddDeuteriums.get(j)) {
            ArrayList<IAtomContainer> deuteratedStrutures = new ArrayList<IAtomContainer>();
            ArrayList<Integer> numberDeuteriumsVec = new ArrayList<Integer>();
            ArrayList<Integer> numberDeuteriumsEasilyExchangedVec = new ArrayList<Integer>();
            // get all possible combinations of exchanges with given number
            // of exchangeable hydrogens
            int[][] combs = getCombinations(toExchange, numberToAddDeuteriums.get(j));
            for (int k = 0; k < combs.length; k++) {
                numberDeuteriumsEasilyExchanged = 0;
                numberDeuteriums = 0;
                IAtomContainer itsNew = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance()).getAtomContainer();
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(itsNew);
                adder.addImplicitHydrogens(itsNew);
                AtomContainerManipulator.convertImplicitToExplicitHydrogens(itsNew);
                for (int l = 0; l < combs[k].length; l++) {
                    addExplicitDeuterium(itsNew, combs[k][l]);
                    numberDeuteriumsEasilyExchanged++;
                    numberDeuteriums++;
                }
                numberDeuteriumsVec.add(numberDeuteriums);
                numberDeuteriumsEasilyExchangedVec.add(numberDeuteriumsEasilyExchanged);
                deuteratedStrutures.add(itsNew);
            }
            for (int k = 0; k < deuteratedStrutures.size(); k++) {
                String inchi = inchis.get(j);
                IAtomContainer con = deuteratedStrutures.get(k);
                HDByteMolecularFormula formula = null;
                try {
                    formula = new HDByteMolecularFormula(inchi.split("/")[1]);
                } catch (Exception e) {
                    System.err.println(identifiers.get(j));
                    e.printStackTrace();
                    System.exit(1);
                }
                formula.setNumberHydrogens((short) formula.getNumberHydrogens());
                formula.setNumberDeuterium((short) (int) numberDeuteriumsVec.get(k));
                InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(con);
                String inchiKey = gen.getInchiKey();
                con.setProperty(VariableNames.IDENTIFIER_NAME, identifiers.get(j) + "-" + (k + 1));
                con.setProperty(VariableNames.INCHI_NAME, inchi);
                // con.setProperty(VariableNames.SMILES_NAME, sg.create(deuteratedStrutures.get(k)));
                con.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, formula.toString());
                con.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, formula.getMonoisotopicMass());
                con.setProperty(VariableNames.INCHI_KEY_1_NAME, inchiKey.split("-")[0]);
                con.setProperty(VariableNames.INCHI_KEY_2_NAME, inchiKey.split("-")[1]);
                con.setProperty("OSN-Deuteriums", numberDeuteriumsEasilyExchangedVec.get(k));
                con.setProperty("AromaticDeuteriums", 0);
                con.setProperty("MissedDeuteriums", (toExchange.length - numberToAddDeuteriums.get(j)));
                set.addAtomContainer(con);
                System.out.println(identifiers.get(j) + "-" + (k + 1) + "|" + inchi + "|" + sg.create(deuteratedStrutures.get(k)) + "|" + formula.toString() + "|" + formula.getMonoisotopicMass() + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchangedVec.get(k) + "|0|" + (toExchange.length - numberToAddDeuteriums.get(j)));
            }
            continue;
        }
        if (withAromaticRings || (numberToAddDeuteriums.size() != 0 && numberToAddDeuteriums.get(j) > numberDeuteriums)) {
            int numberMissing = numberToAddDeuteriums.get(j) - numberDeuteriums;
            numberDeuteriums += numberMissing;
            numberDeuteriumsAromaticExchanged += numberMissing;
        }
        String inchi = inchis.get(j);
        InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(its);
        String inchiKey = gen.getInchiKey();
        HDByteMolecularFormula formula = null;
        try {
            formula = new HDByteMolecularFormula(inchi.split("/")[1]);
        } catch (Exception e) {
            System.err.println(identifiers.get(j));
            e.printStackTrace();
            System.exit(1);
        }
        formula.setNumberHydrogens((short) formula.getNumberHydrogens());
        formula.setNumberDeuterium((short) numberDeuteriums);
        if (numberToAddDeuteriums.size() == 0 || numberDeuteriums == numberToAddDeuteriums.get(j)) {
            System.out.println(identifiers.get(j) + "|" + inchi + "|" + sg.create(its) + "|" + formula.toString() + "|" + formula.getMonoisotopicMass() + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchanged + "|" + numberDeuteriumsAromaticExchanged + "|0");
            its.setProperty(VariableNames.IDENTIFIER_NAME, identifiers.get(j));
            its.setProperty(VariableNames.INCHI_NAME, inchi);
            // its.setProperty(VariableNames.SMILES_NAME, sg.create(its));
            its.setProperty(VariableNames.MOLECULAR_FORMULA_NAME, formula.toString());
            its.setProperty(VariableNames.MONOISOTOPIC_MASS_NAME, formula.getMonoisotopicMass());
            its.setProperty(VariableNames.INCHI_KEY_1_NAME, inchiKey.split("-")[0]);
            its.setProperty(VariableNames.INCHI_KEY_2_NAME, inchiKey.split("-")[1]);
            its.setProperty("OSN-Deuteriums", numberDeuteriumsEasilyExchanged);
            its.setProperty("AromaticDeuteriums", numberDeuteriumsAromaticExchanged);
            its.setProperty("MissedDeuteriums", 0);
            set.addAtomContainer(its);
        } else if (!combinatorial)
            System.err.println("discarded to many easy exchangeable hydrogens " + numberDeuteriums + " " + identifiers.get(j));
    }
    try {
        SDFWriter writer = new SDFWriter(new java.io.FileWriter(new java.io.File(args[1])));
        writer.write(set);
        writer.close();
    } catch (java.io.IOException e) {
        e.printStackTrace();
    } catch (CDKException e) {
        e.printStackTrace();
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ArrayList(java.util.ArrayList) SDFWriter(org.openscience.cdk.io.SDFWriter) SmilesGenerator(org.openscience.cdk.smiles.SmilesGenerator) FileReader(java.io.FileReader) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) CDKException(org.openscience.cdk.exception.CDKException) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) CDKException(org.openscience.cdk.exception.CDKException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) BufferedReader(java.io.BufferedReader) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) HDByteMolecularFormula(de.ipbhalle.metfraglib.molecularformula.HDByteMolecularFormula) File(java.io.File)

Example 2 with IAtomContainerSet

use of org.openscience.cdk.interfaces.IAtomContainerSet in project MetFragRelaunched by ipb-halle.

the class FragmentListWriterSDF method writeSingleMatchList.

private boolean writeSingleMatchList(ICandidate candidate, MatchList matchList, String completeFilePath) {
    IAtomContainerSet set = new AtomContainerSet();
    for (int i = 0; i < matchList.getNumberElements(); i++) {
        IAtomContainer fragmentAtomContainer = matchList.getElement(i).getBestMatchedFragment().getStructureAsIAtomContainer(candidate.getPrecursorMolecule());
        fragmentAtomContainer.setProperty("MolecularFormula", matchList.getElement(i).getModifiedFormulaStringOfBestMatchedFragment(candidate.getPrecursorMolecule()).toString());
        String brokenBonds = "";
        int[] brokenBondIndeces = matchList.getElement(i).getBestMatchedFragment().getBrokenBondIndeces();
        String bondStrings = "";
        for (int index : brokenBondIndeces) {
            brokenBonds += index + ";";
            bondStrings += ((BitArrayPrecursor) candidate.getPrecursorMolecule()).getBondAsString((short) index) + ";";
        }
        if (bondStrings.length() != 0)
            bondStrings = bondStrings.substring(0, bondStrings.length() - 1);
        if (brokenBonds.length() != 0)
            brokenBonds = brokenBonds.substring(0, brokenBonds.length() - 1);
        fragmentAtomContainer.setProperty("BrokenBondsStrings", bondStrings);
        fragmentAtomContainer.setProperty("BrokenBonds", brokenBonds);
        fragmentAtomContainer.setProperty("Match", matchList.getElement(i).toString());
        set.addAtomContainer(fragmentAtomContainer);
    }
    try {
        SDFWriter sdfWriter = new SDFWriter(new FileWriter(new File(completeFilePath)));
        sdfWriter.write(set);
        sdfWriter.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (CDKException e) {
        e.printStackTrace();
        return false;
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) CDKException(org.openscience.cdk.exception.CDKException) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) FileWriter(java.io.FileWriter) SDFWriter(org.openscience.cdk.io.SDFWriter) IOException(java.io.IOException) File(java.io.File) AtomContainerSet(org.openscience.cdk.AtomContainerSet) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) BitArrayPrecursor(de.ipbhalle.metfraglib.precursor.BitArrayPrecursor)

Example 3 with IAtomContainerSet

use of org.openscience.cdk.interfaces.IAtomContainerSet in project ambit-mirror by ideaconsult.

the class MoleculeTools method readCMLMolecule.

public static IAtomContainer readCMLMolecule(InputStream in) throws Exception {
    IAtomContainer mol = null;
    CMLReader reader = new CMLReader(in);
    IChemFile obj = null;
    obj = (IChemFile) reader.read(newChemFile(SilentChemObjectBuilder.getInstance()));
    int n = obj.getChemSequenceCount();
    if (n > 1)
        logger.finest("> 1 sequence in a record");
    for (int j = 0; j < n; j++) {
        IChemSequence seq = obj.getChemSequence(j);
        int m = seq.getChemModelCount();
        if (m > 1)
            logger.finest("> 1 model in a record");
        for (int k = 0; k < m; k++) {
            IChemModel mod = seq.getChemModel(k);
            IAtomContainerSet som = mod.getMoleculeSet();
            if (som.getAtomContainerCount() > 1)
                logger.finest("> 1 molecule in a record");
            for (int l = 0; l < som.getAtomContainerCount(); l++) {
                mol = som.getAtomContainer(l);
                return mol;
            }
        }
    }
    reader = null;
    return mol;
}
Also used : CMLReader(org.openscience.cdk.io.CMLReader) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IChemFile(org.openscience.cdk.interfaces.IChemFile) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) IChemModel(org.openscience.cdk.interfaces.IChemModel) IChemSequence(org.openscience.cdk.interfaces.IChemSequence)

Example 4 with IAtomContainerSet

use of org.openscience.cdk.interfaces.IAtomContainerSet in project ambit-mirror by ideaconsult.

the class FragmentProcessor method process.

@Override
public IAtomContainer process(IAtomContainer container) throws Exception {
    if (container == null)
        return container;
    int atomsOriginal = container.getAtomCount();
    if (atomtypeasproperties && container.getProperty(at_property) != null) {
        if (!isSparseproperties()) {
            if (atomtypeasproperties) {
                if (container.getProperty(at_property_removed) == null)
                    container.setProperty(at_property_removed, null);
                if (container.getProperty(at_property_added) == null)
                    container.setProperty(at_property_added, null);
            }
        }
        if (container.getProperty(at_property) == null)
            container = atomtypes2property(at_property, container, null, isSparseproperties());
    }
    IAtomContainerSet fragments = ConnectivityChecker.partitionIntoMolecules(container);
    IAtomContainer largest = null;
    if (fragments.getAtomContainerCount() > 1)
        for (IAtomContainer mol : fragments.atomContainers()) {
            if ((largest == null) || (mol.getAtomCount() > largest.getAtomCount()))
                largest = mol;
        }
    if (largest != null && (largest.getAtomCount() < atomsOriginal)) {
        if (isAtomtypeasproperties())
            largest = atomtypes2property(largest, container.getProperty(at_property), isSparseproperties());
        return largest;
    } else
        return container;
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet)

Example 5 with IAtomContainerSet

use of org.openscience.cdk.interfaces.IAtomContainerSet in project ambit-mirror by ideaconsult.

the class Structure2DBuilder method process.

@Override
public IAtomContainer process(IAtomContainer molecule) throws AmbitException {
    if (molecule == null)
        return null;
    if (molecule.getAtomCount() == 1) {
        molecule.getAtom(0).setPoint2d(new Point2d(new double[] { 0.0001, 0.0001 }));
        return molecule;
    } else {
        IAtomContainerSet molecules = ConnectivityChecker.partitionIntoMolecules(molecule);
        IAtomContainer newmol = MoleculeTools.newAtomContainer(molecules.getBuilder());
        for (int i = 0; i < molecules.getAtomContainerCount(); i++) {
            IAtomContainer c = molecules.getAtomContainer(i);
            sdg.setMolecule((IAtomContainer) c, false);
            try {
                sdg.generateCoordinates(new Vector2d(0, 1));
                newmol.add(sdg.getMolecule());
            } catch (Exception x) {
                for (IAtom atom : c.atoms()) atom.setPoint2d(new Point2d(new double[] { 0.0001, 0.0001 }));
                newmol.add(c);
            }
        }
        return newmol;
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) Vector2d(javax.vecmath.Vector2d) Point2d(javax.vecmath.Point2d) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) AmbitException(net.idea.modbcum.i.exceptions.AmbitException) IAtom(org.openscience.cdk.interfaces.IAtom)

Aggregations

IAtomContainerSet (org.openscience.cdk.interfaces.IAtomContainerSet)596 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)535 Test (org.junit.Test)327 IAtom (org.openscience.cdk.interfaces.IAtom)284 ArrayList (java.util.ArrayList)223 IReactionSet (org.openscience.cdk.interfaces.IReactionSet)217 IParameterReact (org.openscience.cdk.reaction.type.parameters.IParameterReact)192 IReactionProcess (org.openscience.cdk.reaction.IReactionProcess)166 SetReactionCenter (org.openscience.cdk.reaction.type.parameters.SetReactionCenter)160 ReactionProcessTest (org.openscience.cdk.reaction.ReactionProcessTest)158 CDKException (org.openscience.cdk.exception.CDKException)129 IBond (org.openscience.cdk.interfaces.IBond)57 IReaction (org.openscience.cdk.interfaces.IReaction)56 IChemModel (org.openscience.cdk.interfaces.IChemModel)52 IQueryAtomContainer (org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer)45 InputStream (java.io.InputStream)41 UniversalIsomorphismTester (org.openscience.cdk.isomorphism.UniversalIsomorphismTester)35 QueryAtomContainer (org.openscience.cdk.isomorphism.matchers.QueryAtomContainer)35 IChemSequence (org.openscience.cdk.interfaces.IChemSequence)34 ChemFile (org.openscience.cdk.ChemFile)31