-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
153 lines (136 loc) · 5.16 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Text;
using GraphMolWrap;
using System.IO;
namespace RDKitCSharpTest
{
class Program
{
static void ErrorHandle()
{
var invalid = RWMol.MolFromSmiles("dfep3js2g");
}
static void Demo()
{
var toluene = RWMol.MolFromSmiles("Cc1ccccc1");
var mol1 = RWMol.MolFromMolFile(Path.Combine("Data", "input.mol"));
var stringWithMolData = new StreamReader(Path.Combine("Data", "input.mol")).ReadToEnd();
var mol2 = RWMol.MolFromMolBlock(stringWithMolData);
using (var suppl = new SDMolSupplier(Path.Combine("Data", "5ht3ligs.sdf")))
{
while (!suppl.atEnd())
{
var mol = suppl.next();
if (mol == null)
continue;
Console.WriteLine(mol.getAtoms().Count);
using (var maccs = RDKFuncs.MACCSFingerprintMol(mol))
{
Console.WriteLine(ToString(maccs));
}
}
}
using (var gzsuppl = new ForwardSDMolSupplier(new gzstream("Data/actives_5ht3.sdf.gz")))
{
while (!gzsuppl.atEnd())
{
var mol = gzsuppl.next();
if (mol == null)
continue;
Console.WriteLine(mol.getAtoms().Count);
using (var maccs = RDKFuncs.MACCSFingerprintMol(mol))
{
Console.WriteLine(ToString(maccs));
}
}
}
}
static string ToString(BitVect vector)
{
var sb = new StringBuilder();
var n = vector.size();
for (uint i = 0; i < n; i++)
sb.Append(vector.getBit(i) ? '1' : '0');
return sb.ToString();
}
static void CreateSomeObjects()
{
// ----- Object creation -----
Console.WriteLine("Creating some objects:");
ROMol m1 = RWMol.MolFromSmiles("c1ccccc1");
Console.WriteLine(" mol: " + m1 + " " + m1.getNumAtoms());
ROMol m2 = RWMol.MolFromSmiles("c1ccccn1");
Console.WriteLine(" smi: " + m1 + " " + m1.MolToSmiles());
Console.WriteLine(" smi2: " + m2 + " " + m2.MolToSmiles());
ExplicitBitVect fp1 = RDKFuncs.LayeredFingerprintMol(m1);
ExplicitBitVect fp2 = RDKFuncs.LayeredFingerprintMol(m2);
Console.WriteLine(" sim: " + RDKFuncs.TanimotoSimilarityEBV(fp1, fp2));
const string SMILES_benzene = "c1ccccc1";
ROMol m3 = RWMol.MolFromSmiles(SMILES_benzene);
uint nAtoms = m3.getNumAtoms(true);
Console.WriteLine($"The number of atoms in {SMILES_benzene} is {nAtoms}.");
}
static void BulkMemoryLeakTest()
{
Console.WriteLine("Bulk memory leak test");
for (uint i = 0; i < 10000; ++i)
{
ROMol m4 = RWMol.MolFromSmiles("Clc1cccc(N2CCN(CCC3CCC(CC3)NC(=O)c3cccs3)CC2)c1Cl");
if ((i % 1000) == 0)
Console.WriteLine(" Done: " + i);
m4.Dispose();
//GC.Collect();
}
}
static void MakePicture(string smiles, string filename)
{
int width = 200;
int height = 200;
RWMol mol = null;
mol = RWMol.MolFromSmiles(smiles);
if (mol == null)
mol = RWMol.MolFromSmarts(smiles);
if (mol == null)
throw new Exception($"Cannot recognize: '{smiles}'");
RDKFuncs.prepareMolForDrawing(mol);
if (filename.EndsWith(".svg"))
{
var view = new MolDraw2DSVG(width, height);
view.drawMolecule(mol);
view.finishDrawing();
using (var w = new StreamWriter(filename))
{
w.Write(view.getDrawingText());
Console.WriteLine($"{filename} is drawn.");
}
}
else if (filename.EndsWith(".png"))
{
var view = new MolDraw2DCairo(width, height);
view.drawMolecule(mol, Path.GetFileNameWithoutExtension(filename));
view.finishDrawing();
view.writeDrawingText(filename);
}
else
{
throw new Exception($"Not supported: {filename}");
}
}
static void MakePictures()
{
MakePicture("c1ccccc1", "benzene.png");
MakePicture("c1ccccc1", "benzene.svg");
MakePicture("Clc1cccc(N2CCN(CCC3CCC(CC3)NC(=O)c3cccs3)CC2)c1Cl", "mol.png");
MakePicture("Clc1cccc(N2CCN(CCC3CCC(CC3)NC(=O)c3cccs3)CC2)c1Cl", "mol.svg");
}
static void Main(string[] args)
{
ErrorHandle();
Demo();
CreateSomeObjects();
BulkMemoryLeakTest();
MakePictures();
Console.WriteLine("Goodbye");
}
}
}