Search in sources :

Example 1 with InChITautomerGenerator

use of org.openscience.cdk.tautomers.InChITautomerGenerator in project MetFragRelaunched by ipb-halle.

the class InChIDeuteriumGeneration 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
		 */
    InChITautomerGenerator tg = new InChITautomerGenerator();
    java.io.BufferedWriter bwriter = null;
    if (args.length == 2) {
        bwriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(args[1])));
    }
    CDKHydrogenAdder adder = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
    for (int j = 0; j < inchis.size(); j++) {
        System.out.println(inchis.get(j));
        /*
			 * build the 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, toExchange[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;
                InChIToStructure itsNew = InChIGeneratorFactory.getInstance().getInChIToStructure(inchis.get(j), SilentChemObjectBuilder.getInstance());
                IAtomContainer con = itsNew.getAtomContainer();
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(con);
                java.util.List<IAtomContainer> tautos = tg.getTautomers(con);
                SmilesGenerator sg = new SmilesGenerator(SmiFlavor.Generic);
                for (IAtomContainer tautomer : tautos) {
                    System.out.println(sg.create(tautomer));
                }
                IAtomContainer jiosNew = itsNew.getAtomContainer();
                AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(jiosNew);
                adder.addImplicitHydrogens(jiosNew);
                AtomContainerManipulator.convertImplicitToExplicitHydrogens(jiosNew);
                // System.out.println(Arrays.toString(combs[k]));
                for (int l = 0; l < combs[k].length; l++) {
                    addExplicitDeuterium(jiosNew, combs[k][l]);
                    numberDeuteriumsEasilyExchanged++;
                    numberDeuteriums++;
                }
                // System.out.println("JniInchiOutputStructure");
                // printInfo(jiosNew);
                numberDeuteriumsVec.add(numberDeuteriums);
                numberDeuteriumsEasilyExchangedVec.add(numberDeuteriumsEasilyExchanged);
                deuteratedStrutures.add(jiosNew);
            }
            for (int k = 0; k < deuteratedStrutures.size(); k++) {
                IAtomContainer con = deuteratedStrutures.get(k);
                InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(con);
                String inchi = gen.getInchi();
                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) (int) numberDeuteriumsVec.get(k));
                double mass = formula.getMonoisotopicMass();
                System.out.println(identifiers.get(j) + "-" + (k + 1) + "|" + inchi + "|" + formula.toString() + "|" + mass + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchangedVec.get(k) + "|0|" + (toExchange.length - numberToAddDeuteriums.get(j)) + "|" + (mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS) + "|" + (mass - Constants.getMonoisotopicMassOfAtom("D") + Constants.ELECTRON_MASS));
            }
            continue;
        }
        if (withAromaticRings || (numberToAddDeuteriums.size() != 0 && numberToAddDeuteriums.get(j) > numberDeuteriums)) {
            int numberMissing = numberToAddDeuteriums.get(j) - numberDeuteriums;
            numberDeuteriums += numberMissing;
            numberDeuteriumsAromaticExchanged += numberMissing;
        }
        HDByteMolecularFormula formula = null;
        try {
            formula = new HDByteMolecularFormula(MolecularFormulaManipulator.getString(MolecularFormulaManipulator.getMolecularFormula(its)));
        } catch (Exception e) {
            System.err.println(identifiers.get(j));
            e.printStackTrace();
            System.exit(1);
        }
        formula.setNumberHydrogens((short) formula.getNumberHydrogens());
        formula.setNumberDeuterium((short) numberDeuteriums);
        // Identifier|InChI|MolecularFormula|MonoisotopicMass|InChIKey1|InChIKey2|OSN-Deuteriums|AromaticDeuteriums
        if (numberToAddDeuteriums.size() == 0 || numberDeuteriums == numberToAddDeuteriums.get(j)) {
            InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(its);
            String inchi = gen.getInchi();
            String inchiKey = gen.getInchiKey();
            double mass = formula.getMonoisotopicMass();
            double negMass = mass - Constants.getMonoisotopicMassOfAtom("D") + Constants.ELECTRON_MASS;
            if (numberDeuteriumsEasilyExchanged + numberDeuteriumsAromaticExchanged == 0)
                negMass = mass - Constants.getMonoisotopicMassOfAtom("H") + Constants.ELECTRON_MASS;
            System.out.println(identifiers.get(j) + "|" + inchi + "|" + formula.toString() + "|" + mass + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchanged + "|" + numberDeuteriumsAromaticExchanged + "|0" + "|" + (mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS) + "|" + negMass);
            if (bwriter != null) {
                bwriter.write(identifiers.get(j) + "|" + inchi + "|" + formula.toString() + "|" + mass + "|" + inchiKey.split("-")[0] + "|" + inchiKey.split("-")[1] + "|" + numberDeuteriumsEasilyExchanged + "|" + numberDeuteriumsAromaticExchanged + "|0" + "|" + (mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS) + "|" + negMass);
                bwriter.newLine();
            }
        } else if (!combinatorial)
            System.err.println("discarded to many easy exchangeable hydrogens " + numberDeuteriums + " " + identifiers.get(j));
    /*
			 * String[] masses = getAllFormulas(formula);
			 * 
			 * 
			 * for(int i = 0; i < masses.length; i++) { System.out.print(masses[i] + " "); }
			 * System.out.println();
			 */
    }
    if (bwriter != null)
        bwriter.close();
}
Also used : InChITautomerGenerator(org.openscience.cdk.tautomers.InChITautomerGenerator) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ArrayList(java.util.ArrayList) SmilesGenerator(org.openscience.cdk.smiles.SmilesGenerator) FileReader(java.io.FileReader) ArrayList(java.util.ArrayList) InChIGenerator(org.openscience.cdk.inchi.InChIGenerator) File(java.io.File) CDKException(org.openscience.cdk.exception.CDKException) BufferedReader(java.io.BufferedReader) CDKHydrogenAdder(org.openscience.cdk.tools.CDKHydrogenAdder) InChIToStructure(org.openscience.cdk.inchi.InChIToStructure) HDByteMolecularFormula(de.ipbhalle.metfraglib.molecularformula.HDByteMolecularFormula) File(java.io.File)

Aggregations

HDByteMolecularFormula (de.ipbhalle.metfraglib.molecularformula.HDByteMolecularFormula)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 CDKException (org.openscience.cdk.exception.CDKException)1 InChIGenerator (org.openscience.cdk.inchi.InChIGenerator)1 InChIToStructure (org.openscience.cdk.inchi.InChIToStructure)1 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)1 SmilesGenerator (org.openscience.cdk.smiles.SmilesGenerator)1 InChITautomerGenerator (org.openscience.cdk.tautomers.InChITautomerGenerator)1 CDKHydrogenAdder (org.openscience.cdk.tools.CDKHydrogenAdder)1