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());
}
}
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;
}
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) {
}
}
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);
}
}
}
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;
}
Aggregations