-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
278 lines (228 loc) · 7.06 KB
/
Utility.cs
File metadata and controls
278 lines (228 loc) · 7.06 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VinaCarbResults
{
public struct Vector3
{
float x;
float y;
float z;
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static float SqDistance(Vector3 a, Vector3 b)
{
float diff_x = a.x - b.x;
float diff_y = a.y - b.y;
float diff_z = a.z - b.z;
return (float)(diff_x * diff_x + diff_y * diff_y + diff_z * diff_z);
}
public static float Distance(Vector3 a, Vector3 b)
{
float diff_x = a.x - b.x;
float diff_y = a.y - b.y;
float diff_z = a.z - b.z;
return (float)Math.Sqrt(diff_x * diff_x + diff_y * diff_y + diff_z * diff_z);
}
public float sqrMagnitude => x * x + y * y + z * z;
}
public struct Sphere
{
public Vector3 position;
float radius;
public float sqRadius;
public Sphere(Vector3 pos, float rad)
{
position = pos;
radius = rad;
sqRadius = (float)Math.Pow(rad, 2);
}
}
public class Pose
{
public const int linesAddedByResults = 3;
public int lineNumberPerEntryPDBQT;
public int linePerResult => lineNumberPerEntryPDBQT + linesAddedByResults;
//public Dictionary<string, List<AtomModes>> AtomsForEnzyme; //Key: enzyme name; Val: list of atoms
public List<AtomProps> atoms = new List<AtomProps>(); //Links [i] to atomNr, atomName, groupNr, groupName
/// <summary>
/// Key: 3klk 3hz3 3klkG; Value: Mode
/// </summary>
public Dictionary<string, List<Mode>> EnzymeModes = new Dictionary<string, List<Mode>>();
public List<Mode> BestModes(string enzyme)
{
var list = new List<Mode>(atoms.Count);
if (EnzymeModes.TryGetValue(enzyme, out var modes))
{
for (int i = 0; i < atoms.Count; i++)
{
var knownAtoms = modes.Where(x => x.atomIndex == i); //Ascending order so lowest is first
if (knownAtoms.Count() > 0)
list.Add(knownAtoms.OrderBy(x => x.carbScore).First());
else
list.Add(default);
}
}
return list;
}
public override string ToString()
{
var t = "\t";
var builder = new StringBuilder();
BestModes(ref builder);
//AllModes(ref builder);
return builder.ToString();
}
public void AllModes(ref StringBuilder builder)
{
var t = "\t";
if (!EnzymeModes.Any())
{
builder.AppendLine("No results");
return;
}
/* x x x x x x CREATE BANNER x x x x x x x */
builder.AppendLine("Enzyme" + t + "Label" + t + "Name" + t + "Run" + t + "Mode" + t + "VC" + t + "AV");
/* Enzyme Label Name Run Mode VC AV
* (nr) (id) nr nr kcal/mol */
builder.AppendLine(t + "(nr)" + t + "(id)" + t + "nr" + t + "nr" + t + "kcal/mol");
/* x x x x x x LIST OF ALL MODES x x x x x x x */
foreach (var kvp in EnzymeModes)
{
builder.AppendLine().Append(kvp.Key);
foreach (var mode in kvp.Value.Where(x => x.atomIndex > -1).OrderBy(x => x.carbScore))
{
var atom = atoms[mode.atomIndex];
builder.AppendLine(t + atom.ToString() + t + mode.ToString());
/* 3klkG GLC1(1) O1(16) 1 16 -7.5 -8.2
* GLC2(2) O6(13) 1 15 -6.0 -8.0
* .(.) .(.) . . . .
*/
}
}
}
public void BestModes(ref StringBuilder builder)
{
var t = "\t";
/* x x x x x x CREATE THE BANNER x x x x x x x */
//List<string> columns = new List<string>(atoms.Count);
//foreach (var atom in atoms.GroupBy(x => x.groupNr+"("+x.atomNr)).ToList())
//{
if (atoms.Count < 1)
{
builder.AppendLine("No oxygens");
return;
}
string line1 = string.Empty;
string line2 = string.Empty;
for (int i = 0; i < atoms.Count; i++)
{
/*
* group(label) | group(label)
* id(name) id(name) id(name) | id(name) id(name)
*/
line1 += t + atoms[i].groupName + "(" + atoms[i].groupNr + ")";
line2 += t + atoms[i].atomName + "(" + atoms[i].atomNr + ")";
// TODO: Make optimal ordering of atom indices for display (group labels)
}
builder.AppendLine(line1).AppendLine(line2);
/* x x x x x x BEST BINDING MODES x x x x x x x */
foreach (var kvp in EnzymeModes)
{
string line3 = kvp.Key;
string line4 = string.Empty;
string line5 = string.Empty;
string line6 = string.Empty;
/*
* Enz -7.0 -6.5 -3.0 -4.5 kcal/mol
* 1 3 16 10 nr
* 1 2 1 3 run
*/
bool[] p = new bool[atoms.Count]; //Store whether VinaCarb score != AutoDock Vina score
var bests = BestModes(kvp.Key);
for (int i = 0; i < atoms.Count; i++)
{
if (bests[i].bindingNr != default) //&& bests[i].run != default)
{
line3 += t + bests[i].carbScore;
p[i] = bests[i].carbScore != bests[i].vinaScore;
line4 += t + (string)(p[i] ? bests[i].vinaScore.ToString() : "");
line5 += t + bests[i].bindingNr;
line6 += t + bests[i].run;
}
else
{
line3 += t;
line4 += t;
line5 += t;
line6 += t;
}
}
var anyVC = p.Any(x => x);
builder.AppendLine().AppendLine(line3 + t + "kcal/mol" + (anyVC ? " (VC)" : ""));
if (anyVC) builder.AppendLine(line4 + t + "kcal/mol (AV)");
builder.AppendLine(line5 + t + "mode").AppendLine(line6 + t + "run");
}
}
}
public struct AtomProps
{
public int lineNrPDBQT;
public int atomNr;
public string atomName;
public int groupNr;
public string groupName;
public AtomProps(int lineNrPDBQT, int atomNr, string atomName, int groupNr, string groupName)
{
this.lineNrPDBQT = lineNrPDBQT;
this.atomNr = atomNr;
this.atomName = atomName;
this.groupNr = groupNr;
this.groupName = groupName;
}
public override string ToString()
{
return groupName + "(" + groupNr + ")\t" + atomName + "("+ atomNr + ")";
}
}
public class AtomModes
{
public AtomProps props;
public List<Mode> modes;
public Mode BestMode => modes.OrderBy(x => x.carbScore).LastOrDefault();
}
public struct Mode
{
public int run; //If this value is zero, then the Mode has never been instantiated
public int bindingNr; //If this value is zero, then the Mode has never been instantiated
public float vinaScore;
public float carbScore;
public int atomIndex;
public Mode (int run, int bindingNr, float vinaScore, float carbScore, int atomIndex = -1)
{
this.run = run;
this.bindingNr = bindingNr;
this.vinaScore = vinaScore;
this.carbScore = carbScore;
this.atomIndex = atomIndex;
}
public Mode (Mode mode, int atomIndex)
{
this.run = mode.run;
this.bindingNr = mode.bindingNr;
this.vinaScore = mode.vinaScore;
this.carbScore = mode.carbScore;
this.atomIndex = atomIndex;
}
public override string ToString()
{
return run + "\t" + bindingNr + "\t" + carbScore + (carbScore != vinaScore ? "\t" + vinaScore : "");
}
}
}