private static Dictionary <string, ulong[]> GetFingerprints(string path) { var files = Directory.EnumerateFiles(path, "*.mp3", SearchOption.AllDirectories); var fingerprints = new Dictionary <string, ulong[]>(); foreach (var file in files) { var fileName = Path.GetFileName(file); if (fingerprints.ContainsKey(fileName)) { continue; } Write($"Fingerprinting {fileName}... "); var stopwatch = new Stopwatch(); stopwatch.Start(); var fingerprint = Fingerprinter.GetFingerprint(file); stopwatch.Stop(); WriteLine($"{stopwatch.ElapsedMilliseconds:##0,000}ms"); fingerprints.Add(Path.GetFileName(file), fingerprint); } return(fingerprints); }
public ChromaContext(int algorithm) { this.algorithm = algorithm; var config = FingerprinterConfiguration.CreateConfiguration(algorithm); this.fingerprinter = new Fingerprinter(config); }
public void TestCalculate_BitFingerprint() { IAtomContainer mol1 = TestMoleculeFactory.MakeIndole(); IAtomContainer mol2 = TestMoleculeFactory.MakePyrrole(); Fingerprinter fp = new Fingerprinter(1024, 8); double similarity = Tanimoto.Calculate(fp.GetBitFingerprint(mol1), fp.GetBitFingerprint(mol2)); Assert.AreEqual(0.3939, similarity, 0.01 * 2); }
/// <summary> /// Initializes a new instance of the <see cref="ChromaContext" /> class. /// </summary> /// <param name="algorithm">The algorithm to use, see <see cref="ChromaprintAlgorithm" /> (default = TEST2)</param> /// <param name="fftService">The FFT service.</param> public ChromaContext(ChromaprintAlgorithm algorithm, IFFTService fftService) { this.Algorithm = (int)algorithm; this.fftService = fftService; var config = FingerprinterConfiguration.CreateConfiguration(algorithm); this.fingerprinter = new Fingerprinter(config, fftService); }
void Main() { IAtomContainer molecule1 = null; IAtomContainer molecule2 = null; Fingerprinter fingerprinter = null; #region 1 BitArray fingerprint1 = fingerprinter.GetBitFingerprint(molecule1).AsBitSet(); BitArray fingerprint2 = fingerprinter.GetBitFingerprint(molecule2).AsBitSet(); double tanimoto_coefficient = Tanimoto.Calculate(fingerprint1, fingerprint2); #endregion }
public void TestTanimoto2() { IAtomContainer mol1 = TestMoleculeFactory.MakeIndole(); IAtomContainer mol2 = TestMoleculeFactory.MakeIndole(); Fingerprinter fingerprinter = new Fingerprinter(); BitArray bs1 = fingerprinter.GetBitFingerprint(mol1).AsBitSet(); BitArray bs2 = fingerprinter.GetBitFingerprint(mol2).AsBitSet(); var tanimoto = Tanimoto.Calculate(bs1, bs2); if (standAlone) { System.Console.Out.WriteLine("Tanimoto: " + tanimoto); } if (!standAlone) { Assert.AreEqual(1.0, tanimoto, 0.001); } }
public void TestCompareBitSetandBitFingerprintTanimoto() { IAtomContainer mol1 = TestMoleculeFactory.Make123Triazole(); IAtomContainer mol2 = TestMoleculeFactory.MakeImidazole(); Fingerprinter fingerprinter = new Fingerprinter(1024, 8); BitArray bs1 = fingerprinter.GetBitFingerprint(mol1).AsBitSet(); BitArray bs2 = fingerprinter.GetBitFingerprint(mol2).AsBitSet(); var tanimoto = Tanimoto.Calculate(bs1, bs2); BitSetFingerprint fp1 = new BitSetFingerprint(bs1); BitSetFingerprint fp2 = new BitSetFingerprint(bs2); double tanimoto2 = Tanimoto.Calculate(fp1, fp2); Assert.AreEqual(tanimoto, tanimoto2, 0.01); IntArrayFingerprint ifp1 = new IntArrayFingerprint(fp1); IntArrayFingerprint ifp2 = new IntArrayFingerprint(fp2); tanimoto2 = Tanimoto.Calculate(ifp1, ifp2); Assert.AreEqual(tanimoto, tanimoto2, 0.01); }
public void KeggR00258() { var sp = CDK.SmilesParser; string smiles1 = "O=C(O)CCC(=O)C(=O)O"; string smiles2 = "O=C(O)C(N)CCC(=O)O"; string smiles3 = "O=C(O)C(N)C"; string smiles4 = "CC(=O)C(=O)O"; var molecule1 = sp.ParseSmiles(smiles1); var molecule2 = sp.ParseSmiles(smiles2); var molecule3 = sp.ParseSmiles(smiles3); var molecule4 = sp.ParseSmiles(smiles4); Fingerprinter fingerprinter = new Fingerprinter(1024, 6); BitArray bs1 = fingerprinter.GetBitFingerprint(molecule1).AsBitSet(); BitArray bs2 = fingerprinter.GetBitFingerprint(molecule2).AsBitSet(); BitArray bs3 = fingerprinter.GetBitFingerprint(molecule3).AsBitSet(); BitArray bs4 = fingerprinter.GetBitFingerprint(molecule4).AsBitSet(); Assert.AreEqual(0.75, (double)Tanimoto.Calculate(bs1, bs2), 0.1); Assert.AreEqual(0.46, (double)Tanimoto.Calculate(bs1, bs3), 0.1); Assert.AreEqual(0.52, (double)Tanimoto.Calculate(bs1, bs4), 0.1); Assert.AreEqual(0.53, (double)Tanimoto.Calculate(bs2, bs3), 0.1); Assert.AreEqual(0.42, (double)Tanimoto.Calculate(bs2, bs4), 0.1); Assert.AreEqual(0.8, (double)Tanimoto.Calculate(bs3, bs4), 0.1); }
public void Test() { Fingerprinter.GetFingerprint("C:\\Users\\Stevo\\Music\\Iron Maiden\\Seventh Son of a Seventh Son\\02 Infinite Dreams.mp3"); }
/// <summary> /// Build fingerprint /// </summary> /// <param name="mol"></param> public static BitSetFingerprint BuildBitSetFingerprint( IAtomContainer mol, FingerprintType fpType, int fpSubtype = -1, int fpLen = -1) { // Data for Tanimoto similarity using various fingerprint types for CorpId 123456 query. // Cart - Standard MDL Oracle Cartridge scores // // Similarity Score // ------------------------------------------------ // Size -> 192 896 1024 1024 128 1024 320 // CorpId Cart MACCS PbChm ECFP4 EXT EState Basic Sbstr // ------ ---- ---- ---- ---- ---- ---- ---- ---- // 123456 0.99 1.00 1.00 1.00 1.00 1.00 1.00 1.00 // 123456 0.99 0.98 0.96 0.77 0.95 1.00 0.95 1.00 // 123456 0.99 0.98 0.96 0.77 0.95 1.00 0.94 1.00 // 123456 0.99 1.00 1.00 1.00 1.00 1.00 1.00 1.00 // 123456 0.99 1.00 1.00 1.00 1.00 1.00 1.00 1.00 // 123456 0.99 0.91 1.00 0.81 1.00 1.00 1.00 1.00 // 123456 0.98 0.95 1.00 0.74 0.92 1.00 0.93 0.94 // 123456 0.98 1.00 1.00 1.00 1.00 1.00 1.00 1.00 // 123456 0.98 1.00 1.00 1.00 1.00 1.00 1.00 1.00 // 123456 0.98 1.00 0.83 0.76 0.77 0.90 0.76 0.94 // LSH Bin Count - The number of LSH bins (of 25) that match the query bin values //-------------- // CorpId MAC PbC ECFP EX // ------ --- --- --- --- // 123456 25 25 25 25 // 123456 25 20 7 16 // 123456 25 20 9 19 // 123456 25 25 25 25 // 123456 25 25 25 25 // 123456 20 25 9 25 // 123456 21 25 11 17 // 123456 25 25 25 25 // 123456 25 25 25 25 // 123456 25 9 6 11 // Data for Tanimoto similarity using various Circular fingerprint types. // Using 2 molecules where the 2nd just has an added methyl group. // // Measure Score // -------- ----- // ECFP0 1.00 // ECFP2 .88 // ECFP4 .75 // ECFP6 .64 // FCFP0 1.00 // FCFP2 .92 // FCFP4 .84 // FCFP6 .74 IFingerprinter ifptr = null; IBitFingerprint ibfp = null; BitSetFingerprint bfp = null; IAtomContainer mol2; string s = ""; DateTime t0 = DateTime.Now; double getFptrTime = 0, buildFpTime = 0; if (fpType == FingerprintType.Basic) // size = 1024 { ifptr = new Fingerprinter(); } else if (fpType == FingerprintType.Circular) // size variable { CircularFingerprinterClass cfpClass = (CircularFingerprinterClass)fpSubtype; if (cfpClass < CircularFingerprinterClass.ECFP0 || cfpClass > CircularFingerprinterClass.ECFP6) { cfpClass = (CircularFingerprinterClass)CircularFingerprintType.DefaultCircularClass; // default class } if (fpLen < 0) { fpLen = CircularFingerprintType.DefaultCircularLength; // default length } ifptr = new CircularFingerprinter(cfpClass, fpLen); //CircularFingerprinter cfp = (CircularFingerprinter)ifptr; //ICountFingerprint cntFp = cfp.getCountFingerprint(mol); // debug //s = CircularFpToString(cfp); // debug } else if (fpType == FingerprintType.Extended) // size = 1024 { ifptr = new ExtendedFingerprinter(); // use DEFAULT_SIZE and DEFAULT_SEARCH_DEPTH } else if (fpType == FingerprintType.EState) // size = 128 { ifptr = new EStateFingerprinter(); // use DEFAULT_SIZE and DEFAULT_SEARCH_DEPTH } else if (fpType == FingerprintType.MACCS) // size = 192 { if (MACCSFp == null) { MACCSFp = new MACCSFingerprinter(); } ifptr = MACCSFp; } else if (fpType == FingerprintType.PubChem) // size = 896 { //IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance(); ifptr = new PubchemFingerprinter(); } else if (fpType == FingerprintType.ShortestPath) // size = { ifptr = new ShortestPathFingerprinter(); // fails with atom type issue for many structures (e.g. 123456) } else if (fpType == FingerprintType.Signature) // size = { ifptr = new SignatureFingerprinter(); // can't convert array fingerprint to bitsetfingerprint } else if (fpType == FingerprintType.Substructure) // size = 320 { ifptr = new SubstructureFingerprinter(); } else { throw new Exception("Invalid CdkFingerprintType: " + fpType); } getFptrTime = TimeOfDay.Delta(ref t0); ibfp = ifptr.GetBitFingerprint(mol); bfp = (BitSetFingerprint)ibfp; buildFpTime = TimeOfDay.Delta(ref t0); //long size = bfp.size(); //int card = bfp.Cardinality; return(bfp); }
public static void Main() { const string path = "C:\\Users\\Stevo\\Music\\Iron Maiden"; Dictionary <string, ulong[]> fingerprints; if (!File.Exists("cache.json")) { var stopwatch = new Stopwatch(); stopwatch.Start(); WriteLine("Cache not found, generating fingerprints."); fingerprints = GetFingerprints(path); stopwatch.Stop(); WriteLine($"{fingerprints.Count} fingerprints generated in {(int) stopwatch.Elapsed.TotalSeconds}s"); File.WriteAllText("cache.json", JsonConvert.SerializeObject(fingerprints)); } else { WriteLine("Cache found."); fingerprints = JsonConvert.DeserializeObject <Dictionary <string, ulong[]> >(File.ReadAllText("cache.json")); } if (!File.Exists("temp.wav")) { WriteLine("Press enter to start listening..."); ReadLine(); using var source = new WaveInEvent(); var buffer = new byte[source.WaveFormat.AverageBytesPerSecond * 20]; var stream = new WaveFileWriter("temp.wav", source.WaveFormat); source.DataAvailable += (s, a) => { if (stream.Position + a.BytesRecorded > buffer.Length) { return; } stream.Write(a.Buffer, 0, a.BytesRecorded); }; source.StartRecording(); for (var i = 0; i < 20; i++) { WriteLine(i); Thread.Sleep(1000); } source.StopRecording(); stream.Close(); stream.Dispose(); } //var fingerprint = Fingerprinter.GetFingerprint("temp.wav"); var fingerprint = Fingerprinter.GetFingerprint("04 Blood Brothers.m4a"); var results = new List <Tuple <int, string> >(); foreach (var print in fingerprints) { var matches = 0; foreach (var key in print.Value) { foreach (var subKey in fingerprint) { if (key == subKey) { matches++; } } } WriteLine($"{print.Key} gets {matches} matches."); results.Add(new Tuple <int, string>(matches, print.Key)); } WriteLine("\n\n\n"); results.OrderBy(t => t.Item1).ToList().ForEach(t => { if (t.Item2 == "04 Blood Brothers.mp3") { ForegroundColor = ConsoleColor.Cyan; } WriteLine($"{t.Item1,5} : {t.Item2}"); ForegroundColor = ConsoleColor.Green; }); }