use of org.openscience.cdk.AtomContainerSet in project cdk by cdk.
the class MoSSOutputReaderTest method testExampleFile_MolReading.
@Test
public void testExampleFile_MolReading() throws Exception {
String filename = "TKO.mossoutput";
InputStream ins = this.getClass().getResourceAsStream(filename);
MoSSOutputReader reader = new MoSSOutputReader(ins);
IAtomContainerSet moleculeSet = new AtomContainerSet();
moleculeSet = reader.read(moleculeSet);
Assert.assertEquals(19, moleculeSet.getAtomContainerCount());
for (IAtomContainer mol : moleculeSet.atomContainers()) {
Assert.assertEquals(Integer.valueOf(mol.getProperty("atomCount").toString()).intValue(), mol.getAtomCount());
Assert.assertEquals(Integer.valueOf(mol.getProperty("bondCount").toString()).intValue(), mol.getBondCount());
}
}
use of org.openscience.cdk.AtomContainerSet in project cdk by cdk.
the class SMILESReaderTest method testReading.
@Test
public void testReading() throws Exception {
String filename = "smiles.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getResourceAsStream(filename);
SMILESReader reader = new SMILESReader(ins);
IAtomContainerSet som = reader.read(new AtomContainerSet());
Assert.assertEquals(8, som.getAtomContainerCount());
}
use of org.openscience.cdk.AtomContainerSet in project cdk by cdk.
the class SMILESReaderTest method testReadingSmiFile_2.
@Test
public void testReadingSmiFile_2() throws Exception {
String filename = "smiles.smi";
logger.info("Testing: " + filename);
InputStream ins = this.getClass().getResourceAsStream(filename);
SMILESReader reader = new SMILESReader(ins);
IAtomContainerSet som = reader.read(new AtomContainerSet());
IAtomContainer thisMol = som.getAtomContainer(1);
Assert.assertNull(thisMol.getProperty("SMIdbNAME"));
}
use of org.openscience.cdk.AtomContainerSet in project MetFragRelaunched by ipb-halle.
the class SmilesDeuteriumGeneration method main.
public static void main(String[] args) throws Exception {
boolean withAromaticRings = false;
boolean combinatorial = false;
/*
* read input inchis
*/
ArrayList<String> smiles = new ArrayList<String>();
ArrayList<Integer> numberToAddDeuteriums = new ArrayList<Integer>();
ArrayList<String> identifiers = new ArrayList<String>();
File input = new File(args[0]);
BufferedReader breader = null;
if (!input.exists()) {
smiles.add(args[0]);
identifiers.add("1");
} else
breader = new BufferedReader(new FileReader(input));
String line = "";
int identifier = 1;
if (breader != null)
while ((line = breader.readLine()) != null) {
String[] tmp = line.trim().split("\\s+");
smiles.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++;
}
if (breader != null)
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 < smiles.size(); j++) {
/*
* build the atom container
*/
IAtomContainer its = MoleculeFunctions.parseSmilesImplicitHydrogen(smiles.get(j));
AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(its);
try {
adder.addImplicitHydrogens(its);
} catch (Exception e) {
System.out.println("missed " + smiles.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;
IAtomContainer itsNew = MoleculeFunctions.parseSmilesImplicitHydrogen(smiles.get(j));
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++) {
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));
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)));
}
}
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);
if (numberToAddDeuteriums.size() == 0 || numberDeuteriums == numberToAddDeuteriums.get(j)) {
InChIGenerator gen = InChIGeneratorFactory.getInstance().getInChIGenerator(its);
String inchi = gen.getInchi();
String inchiKey = gen.getInchiKey();
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());
double mass = MathTools.round(formula.getMonoisotopicMass());
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);
its.setProperty("M+H", MathTools.round(mass + Constants.getMonoisotopicMassOfAtom("H") - Constants.ELECTRON_MASS));
its.setProperty("M+D", MathTools.round(mass + Constants.getMonoisotopicMassOfAtom("D") - Constants.ELECTRON_MASS));
its.setProperty("M-H", MathTools.round(mass - Constants.getMonoisotopicMassOfAtom("H") + Constants.ELECTRON_MASS));
its.setProperty("M-D", MathTools.round(mass - Constants.getMonoisotopicMassOfAtom("D") + Constants.ELECTRON_MASS));
IAtomContainer con = MoleculeFunctions.removeHydrogens(its, toExchange);
its.setProperty("DeuteratedSMILES", MoleculeFunctions.generateSmiles(con).replaceAll("\\[H\\]", "[2H]"));
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();
}
}
use of org.openscience.cdk.AtomContainerSet in project MetFragRelaunched by ipb-halle.
the class CandidateListWriterSDF method writeFile.
public boolean writeFile(File file, IList list, Settings settings) throws Exception {
IAtomContainerSet set = new AtomContainerSet();
CandidateList candidateList = null;
int numberOfPeaksUsed = 0;
if (list instanceof ScoredCandidateList || list instanceof SortedScoredCandidateList) {
candidateList = (ScoredCandidateList) list;
numberOfPeaksUsed = ((ScoredCandidateList) list).getNumberPeaksUsed();
}
if (list instanceof CandidateList) {
candidateList = (CandidateList) list;
}
if (candidateList == null)
return false;
for (int i = 0; i < candidateList.getNumberElements(); i++) {
ICandidate candidate = candidateList.getElement(i);
if (settings != null)
candidate.setUseSmiles((Boolean) settings.get(VariableNames.USE_SMILES_NAME));
try {
candidate.initialisePrecursorCandidate();
} catch (Exception e) {
continue;
}
IAtomContainer candidateAtomContainer = null;
try {
candidateAtomContainer = candidate.getAtomContainer();
} catch (Exception e1) {
System.err.println("Error saving: " + candidateList.getElement(i).getIdentifier());
continue;
}
ICandidate scoredCandidate = candidateList.getElement(i);
if (scoredCandidate.getMatchList() != null) {
MatchList matchList = scoredCandidate.getMatchList();
int countExplainedPeaks = 0;
for (int l = 0; l < matchList.getNumberElements(); l++) {
try {
matchList.getElement(l).getMatchedPeak().getIntensity();
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
countExplainedPeaks++;
}
candidateAtomContainer.setProperty("NumberPeaksExplained", countExplainedPeaks);
}
String peaksExplained = "";
String sumFormulasOfFragmentsExplainedPeaks = "";
String fragmentAtomArrays = "";
String fragmentBondArrays = "";
String fragmentBrokenBondArrays = "";
if (scoredCandidate.getMatchList() != null) {
for (int ii = 0; ii < scoredCandidate.getMatchList().getNumberElements(); ii++) {
try {
double intensity = scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getIntensity();
peaksExplained += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + "_" + intensity + ";";
} catch (RelativeIntensityNotDefinedException e1) {
continue;
}
String formula = scoredCandidate.getMatchList().getElement(ii).getModifiedFormulaStringOfBestMatchedFragment(scoredCandidate.getPrecursorMolecule());
sumFormulasOfFragmentsExplainedPeaks += scoredCandidate.getMatchList().getElement(ii).getMatchedPeak().getMass() + ":" + formula + ";";
fragmentAtomArrays += ((DefaultBitArrayFragment) scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment()).getAtomsFastBitArray() + ";";
fragmentBondArrays += ((DefaultBitArrayFragment) scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment()).getBondsFastBitArray() + ";";
fragmentBrokenBondArrays += ((DefaultBitArrayFragment) scoredCandidate.getMatchList().getElement(ii).getBestMatchedFragment()).getBrokenBondsFastBitArray() + ";";
}
if (sumFormulasOfFragmentsExplainedPeaks.length() != 0)
sumFormulasOfFragmentsExplainedPeaks = sumFormulasOfFragmentsExplainedPeaks.substring(0, sumFormulasOfFragmentsExplainedPeaks.length() - 1);
if (peaksExplained.length() != 0)
peaksExplained = peaksExplained.substring(0, peaksExplained.length() - 1);
if (peaksExplained.length() == 0)
peaksExplained = "NA";
if (fragmentAtomArrays.length() != 0)
fragmentAtomArrays = fragmentAtomArrays.substring(0, fragmentAtomArrays.length() - 1);
if (fragmentBondArrays.length() != 0)
fragmentBondArrays = fragmentBondArrays.substring(0, fragmentBondArrays.length() - 1);
if (fragmentBrokenBondArrays.length() != 0)
fragmentBrokenBondArrays = fragmentBrokenBondArrays.substring(0, fragmentBrokenBondArrays.length() - 1);
if (sumFormulasOfFragmentsExplainedPeaks.length() == 0)
sumFormulasOfFragmentsExplainedPeaks = "NA";
candidateAtomContainer.setProperty("PeaksExplained", peaksExplained);
candidateAtomContainer.setProperty("FragmentMolecularFormulas", sumFormulasOfFragmentsExplainedPeaks);
}
for (java.util.Enumeration<String> keys = candidate.getProperties().keys(); keys.hasMoreElements(); ) {
String key = keys.nextElement();
String propertyValue = checkEmptyProperty(candidate.getProperty(key)).toString();
if (key.equals(VariableNames.IDENTIFIER_NAME))
propertyValue = propertyValue.replaceAll("\\|[0-9]+", "");
candidateAtomContainer.setProperty(key, propertyValue);
}
candidateAtomContainer.setProperty("NumberPeaksUsed", numberOfPeaksUsed);
MoleculeFunctions.prepareAtomContainer(candidateAtomContainer, true);
MoleculeFunctions.convertExplicitToImplicitHydrogens(candidateAtomContainer);
set.addAtomContainer(candidateAtomContainer);
}
SDFWriter writer = new SDFWriter(new FileWriter(file));
writer.write(set);
writer.close();
return true;
}
Aggregations