Search in sources :

Example 21 with IBitFingerprint

use of org.openscience.cdk.fingerprint.IBitFingerprint in project cdk by cdk.

the class TemplateHandler3DTest method testAnonFingerprints.

@Test
public void testAnonFingerprints() throws Exception {
    BitSet[] expected = new BitSet[] { parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}"), parseBitSet("{148, 206, 392, 542, 637, 742, 752, 830}") };
    String filename = "fingerprints_from_modelbuilder3d.sdf";
    InputStream ins = this.getClass().getResourceAsStream(filename);
    List<IBitFingerprint> data = new TemplateExtractor().makeFingerprintsFromSdf(true, true, new HashMap<>(), new BufferedReader(new InputStreamReader(ins)), 10);
    QueryChemObject obj = new QueryChemObject(DefaultChemObjectBuilder.getInstance());
    obj.getBuilder();
    for (int i = 0; i < data.size(); i++) {
        IBitFingerprint bs = data.get(i);
        Assert.assertEquals(expected[i], bs.asBitSet());
    }
}
Also used : IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BitSet(java.util.BitSet) BufferedReader(java.io.BufferedReader) QueryChemObject(org.openscience.cdk.isomorphism.matchers.QueryChemObject) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) Test(org.junit.Test)

Example 22 with IBitFingerprint

use of org.openscience.cdk.fingerprint.IBitFingerprint in project cdk by cdk.

the class TemplateExtractor method makeFingerprintsFromSdf.

public List<IBitFingerprint> makeFingerprintsFromSdf(boolean anyAtom, boolean anyAtomAnyBond, Map<String, Integer> timings, BufferedReader fin, int limit) throws Exception {
    HybridizationFingerprinter fingerPrinter = new HybridizationFingerprinter(HybridizationFingerprinter.DEFAULT_SIZE, HybridizationFingerprinter.DEFAULT_SEARCH_DEPTH);
    fingerPrinter.setHashPseudoAtoms(true);
    IAtomContainer m;
    IteratingSDFReader imdl = null;
    // QueryAtomContainer query=null;
    IAtomContainer query;
    List<IBitFingerprint> data = new ArrayList<>();
    ILoggingTool logger = LoggingToolFactory.createLoggingTool(getClass());
    try {
        logger.info("Read data file in ...");
        imdl = new IteratingSDFReader(fin, builder);
        // fin.close();
        logger.info("ready");
    } catch (Exception exc) {
        System.out.println("Could not read Molecules from file" + " due to: " + exc.getMessage());
    }
    int moleculeCounter = 0;
    int fingerprintCounter = 0;
    logger.info("Generated Fingerprints: " + fingerprintCounter + "    ");
    while (imdl.hasNext() && (moleculeCounter < limit || limit == -1)) {
        m = imdl.next();
        moleculeCounter++;
        if (anyAtom && !anyAtomAnyBond) {
            query = QueryAtomContainerCreator.createAnyAtomContainer(m, false);
        } else {
            query = AtomContainerManipulator.anonymise(m);
        }
        try {
            long time = -System.currentTimeMillis();
            // query fp for anyAtom is probably not useful
            data.add(fingerPrinter.getBitFingerprint(query));
            fingerprintCounter = fingerprintCounter + 1;
            time += System.currentTimeMillis();
            // store the time
            String bin = Integer.toString((int) Math.floor(time / 10.0));
            if (timings.containsKey(bin)) {
                timings.put(bin, (timings.get(bin)) + 1);
            } else {
                timings.put(bin, 1);
            }
        } catch (Exception exc1) {
            logger.info("QueryFingerprintError: from molecule:" + moleculeCounter + " due to:" + exc1.getMessage());
            // OK, just adds a fingerprint with all ones, so that any
            // structure will match this template, and leave it up
            // to substructure match to figure things out
            IBitFingerprint allOnesFingerprint = new BitSetFingerprint(fingerPrinter.getSize());
            for (int i = 0; i < fingerPrinter.getSize(); i++) {
                allOnesFingerprint.set(i, true);
            }
            data.add(allOnesFingerprint);
            fingerprintCounter = fingerprintCounter + 1;
        }
        if (fingerprintCounter % 2 == 0)
            logger.info("\b" + "/");
        else
            logger.info("\b" + "\\");
        if (fingerprintCounter % 100 == 0)
            logger.info("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" + "Generated Fingerprints: " + fingerprintCounter + "   \n");
    }
    // while
    try {
        imdl.close();
    } catch (Exception exc2) {
        LoggingToolFactory.createLoggingTool(TemplateExtractor.class).warn("Unexpected Exception:", exc2);
    }
    logger.info("...ready with:" + moleculeCounter + " molecules\nWrite data...of data vector:" + data.size() + " fingerprintCounter:" + fingerprintCounter);
    return data;
}
Also used : HybridizationFingerprinter(org.openscience.cdk.fingerprint.HybridizationFingerprinter) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) ILoggingTool(org.openscience.cdk.tools.ILoggingTool) BitSetFingerprint(org.openscience.cdk.fingerprint.BitSetFingerprint) ArrayList(java.util.ArrayList) IteratingSDFReader(org.openscience.cdk.io.iterator.IteratingSDFReader) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) BitSetFingerprint(org.openscience.cdk.fingerprint.BitSetFingerprint) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint)

Example 23 with IBitFingerprint

use of org.openscience.cdk.fingerprint.IBitFingerprint in project cdk by cdk.

the class TemplateExtractor method makeFingerprintFromRingSystems.

public void makeFingerprintFromRingSystems(String dataFileIn, String dataFileOut, boolean anyAtom, boolean anyAtomAnyBond) throws Exception {
    Map<String, Integer> timings = new HashMap<>();
    System.out.println("Start make fingerprint from file:" + dataFileIn + " ...");
    BufferedReader fin = new BufferedReader(new FileReader(dataFileIn));
    List<IBitFingerprint> data = makeFingerprintsFromSdf(anyAtom, anyAtomAnyBond, timings, fin, -1);
    BufferedWriter fout = null;
    try {
        fout = new BufferedWriter(new FileWriter(dataFileOut));
    } catch (Exception exc3) {
        System.out.println("Could not write Fingerprint in file " + dataFileOut + " due to: " + exc3.getMessage());
        return;
    }
    for (IBitFingerprint datum : data) {
        try {
            fout.write(datum.toString());
            fout.write('\n');
        } catch (Exception exc4) {
        }
    }
    System.out.println("\nFingerprints:" + data.size() + " are written...ready");
    System.out.println("\nComputing time statistics:\n" + timings);
    try {
        fout.close();
    } catch (Exception exc5) {
    }
}
Also used : IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) CDKException(org.openscience.cdk.exception.CDKException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 24 with IBitFingerprint

use of org.openscience.cdk.fingerprint.IBitFingerprint in project MetFragRelaunched by ipb-halle.

the class TopDownFragmenterAssignerScorer method addFingerPrintsToArrayList.

protected void addFingerPrintsToArrayList(IMolecularStructure precursorMolecule, ArrayList<AbstractTopDownBitArrayFragment> fragments, ArrayList<String> fingerprints, ArrayList<IBitFingerprint> fps) {
    for (int i = 0; i < fragments.size(); i++) {
        int index = 0;
        IBitFingerprint fp = TanimotoSimilarity.calculateFingerPrint(fragments.get(i).getStructureAsIAtomContainer(precursorMolecule));
        String fingerprint = MoleculeFunctions.fingerPrintToString(fp);
        int compareResult = -1;
        while (index < fingerprints.size()) {
            compareResult = fingerprints.get(index).compareTo(fingerprint);
            if (compareResult < 0)
                index++;
            else
                break;
        }
        if (compareResult != 0) {
            fingerprints.add(index, fingerprint);
            fps.add(index, fp);
        }
    }
}
Also used : IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint)

Example 25 with IBitFingerprint

use of org.openscience.cdk.fingerprint.IBitFingerprint in project MetFragRelaunched by ipb-halle.

the class OfflineMetFusionSpectralSimilarityScore method calculate.

public void calculate() {
    this.value = 0.0;
    // beta = -9, gamma = 0.6
    try {
        SpectralPeakListCollection spectralPeakLists = (SpectralPeakListCollection) settings.get(VariableNames.OFFLINE_METFUSION_MONA_SPECTRAL_SIMILARITY_PEAK_LIST_COLLECTION_NAME);
        // fingerprint of the current candidate
        IBitFingerprint f2 = TanimotoSimilarity.calculateFingerPrint(this.candidate.getAtomContainer());
        // calculate similarity score
        java.util.Hashtable<String, java.util.ArrayList<SortedSimilarityTandemMassPeakList>> inchikey1ToPeakList = spectralPeakLists.getInchikey1ToPeakList();
        java.util.Hashtable<String, Double> inchikey1ToSimScore = spectralPeakLists.getInchikey1ToSimScore();
        java.util.Enumeration<?> it = inchikey1ToPeakList.keys();
        while (it.hasMoreElements()) {
            String inchikey1 = (String) it.nextElement();
            double tanimoto_sim = TanimotoSimilarity.calculateSimilarity(inchikey1ToPeakList.get(inchikey1).get(0).getFingerprint(), f2);
            double val = tanimoto_sim * inchikey1ToSimScore.get(inchikey1);
            this.value += this.signum(-9.0, 0.6, val);
        }
    } catch (Exception e) {
        this.value = 0.0;
    }
    this.calculationFinished = true;
}
Also used : SpectralPeakListCollection(de.ipbhalle.metfraglib.collection.SpectralPeakListCollection) IBitFingerprint(org.openscience.cdk.fingerprint.IBitFingerprint)

Aggregations

IBitFingerprint (org.openscience.cdk.fingerprint.IBitFingerprint)29 IAtomContainer (org.openscience.cdk.interfaces.IAtomContainer)10 BitSet (java.util.BitSet)7 Test (org.junit.Test)6 CDKException (org.openscience.cdk.exception.CDKException)6 IOException (java.io.IOException)4 ICountFingerprint (org.openscience.cdk.fingerprint.ICountFingerprint)4 AverageLinkageStrategy (com.apporiented.algorithm.clustering.AverageLinkageStrategy)3 ClusteringAlgorithm (com.apporiented.algorithm.clustering.ClusteringAlgorithm)3 PDistClusteringAlgorithm (com.apporiented.algorithm.clustering.PDistClusteringAlgorithm)3 BufferedReader (java.io.BufferedReader)3 CircularFingerprinter (org.openscience.cdk.fingerprint.CircularFingerprinter)3 SmilesParser (org.openscience.cdk.smiles.SmilesParser)3 CandidateList (de.ipbhalle.metfraglib.list.CandidateList)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 List (java.util.List)2 BitSetFingerprint (org.openscience.cdk.fingerprint.BitSetFingerprint)2