-
Notifications
You must be signed in to change notification settings - Fork 0
/
OPALArray.cs
364 lines (338 loc) · 14.6 KB
/
OPALArray.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PeSA.Engine
{
public class OPALArray: BaseArray
{
public bool PermutationXAxis { get; set; }
public double NormalizedMatrixMax { get; set; }
public double NormalizedMatrixMin { get; set; }
public double[] NormBy;
public NormalizationMode NormMode = NormalizationMode.Max;
public char[] Permutation;
public string[] PositionCaptions;
public bool PositionYAxisTopToBottom { get; set; }
/// <summary>
/// In OPAL arrays, wild type array is set as XXXXXXXX to be used by the motif class
/// </summary>
public string WildTypePeptide { get; set; }
public Motif Motif { get; set; }
override protected void Upgrade(string mode)
{
base.Upgrade(mode);
if (mode == "NormalizedMatrixRange")
{
if (NormalizedPeptideWeights == null || NormalizedPeptideWeights.Count == 0)
GenerateNormalizedPeptideWeights();
else
{
NormalizedMatrixMin = double.MaxValue;
NormalizedMatrixMax = double.MinValue;
for (int iRow = 0; iRow < RowCount; iRow++)
for (int iCol = 0; iCol < ColCount; iCol++)
{
if (NormalizedMatrix[iRow, iCol] < NormalizedMatrixMin)
NormalizedMatrixMin = NormalizedMatrix[iRow, iCol];
if (NormalizedMatrix[iRow, iCol] > NormalizedMatrixMax)
NormalizedMatrixMax = NormalizedMatrix[iRow, iCol];
}
}
}
}
private void GenerateNormalizedPeptideWeights()
{
NormalizedPeptideWeights.Clear();
NormalizedMatrixMin = double.MaxValue;
NormalizedMatrixMax = double.MinValue;
for (int iRow = 0; iRow < RowCount; iRow++)
for (int iCol = 0; iCol < ColCount; iCol++)
{
int pos = PermutationXAxis ? iRow : iCol;
double normby = NormMode == NormalizationMode.Max ? NormalizationValue :
NormBy == null ? 1 : NormBy[pos];
if (normby != 0)
NormalizedMatrix[iRow, iCol] = QuantificationMatrix[iRow, iCol] / normby;
if (NormalizedMatrix[iRow, iCol] < NormalizedMatrixMin)
NormalizedMatrixMin = NormalizedMatrix[iRow, iCol];
if (NormalizedMatrix[iRow, iCol] > NormalizedMatrixMax)
NormalizedMatrixMax = NormalizedMatrix[iRow, iCol];
double weight = NormalizedMatrix[iRow, iCol];
if (PermutationXAxis)
{
if (this.PositionYAxisTopToBottom)
AddNormalizedPeptideWeight(weight, iRow, Permutation[iCol]);
else //if Position Matrix is read from bottom to top, rowind should be reversed
AddNormalizedPeptideWeight(weight, RowCount - iRow - 1, Permutation[iCol]);
}
else
{
AddNormalizedPeptideWeight(weight, iCol, Permutation[iRow]);
}
}
}
private void GenerateMatrices(string[,] values, out string error)
{
error = "";
try
{
if (values == null) return;
RowCount = values.GetLength(0) - 1;
ColCount = values.GetLength(1) - 1;
QuantificationMatrix = new double[RowCount, ColCount];
NormalizedMatrix = new double[RowCount, ColCount];
WildTypePeptide = new string('X', PermutationXAxis ? RowCount : ColCount);
#region Generate permutation, position, and NormBy arrays
if (PermutationXAxis)
{
NormBy = new double[RowCount];
for (int iRow = 1; iRow <= RowCount; iRow++)
NormBy[iRow - 1] = 0;
Permutation = new char[ColCount];
PositionCaptions = new string[RowCount];
for (int iCol = 1; iCol <= ColCount; iCol++)
{
string s = values[0, iCol].Trim();
if (AminoAcids.GetAminoAcid(s[0]) == null)
{
error = "Not a valid permutation string";
return;
}
Permutation[iCol - 1] = s[0];
}
//PositionYAxisTopToBottom is not used in setting the position captions. What user sees should not change. Use it only during the motif generation
for (int iRow = 1; iRow <= RowCount; iRow++)
{
string s = values[iRow, 0].Trim();
PositionCaptions[iRow - 1] = s;
}
}
else //if(PermutationYAxis)
{
NormBy = new double[ColCount];
for (int iCol = 1; iCol <= ColCount; iCol++)
NormBy[iCol - 1] = 0;
Permutation = new char[RowCount];
PositionCaptions = new string[ColCount];
for (int iRow = 1; iRow <= RowCount; iRow++)
{
string s = values[iRow, 0].Trim();
if (AminoAcids.GetAminoAcid(s[0]) == null)
{
error = "Not a valid permutation string";
return;
}
Permutation[iRow - 1] = s[0];
}
for (int iCol = 1; iCol <= ColCount; iCol++)
{
string s = values[0, iCol].Trim();
PositionCaptions[iCol - 1] = s;
}
}
#endregion
//Generate Matrices and Normalization values
for (int iRow = 0; iRow < RowCount; iRow++)
for (int iCol = 0; iCol < ColCount; iCol++)
{
double d = 0;
if (double.TryParse(values[iRow + 1, iCol + 1], out d))
QuantificationMatrix[iRow, iCol] = d;
else if (string.IsNullOrEmpty(values[iRow + 1, iCol + 1]))
d = 0;
else
{
error = "Wrongly formatted data.";
return;
}
if (PermutationXAxis)
NormBy[iRow] = Math.Max(d, NormBy[iRow]);
else
NormBy[iCol] = Math.Max(d, NormBy[iCol]);
}
for (int iRow = 0; iRow < RowCount; iRow++)
for (int iCol = 0; iCol < ColCount; iCol++)
{
double normby = PermutationXAxis ? NormBy[iRow] : NormBy[iCol];
if (normby != 0)
NormalizedMatrix[iRow, iCol] = QuantificationMatrix[iRow, iCol] / normby;
}
NormalizationValue = NormBy.Max();
GenerateNormalizedPeptideWeights();
}
catch (Exception exc)
{
error = "Unhandled exception: " + exc.Message;
}
}
public void Renormalize()
{
GenerateNormalizedPeptideWeights();
}
public OPALArray(string[,] values, bool permutationXAxisIn, bool positionYAxisTopToBottom, out string error)
{
error = "";
try
{
PermutationXAxis = permutationXAxisIn;
PositionYAxisTopToBottom = positionYAxisTopToBottom;
NormalizedPeptideWeights = new Dictionary<string, double>();
GenerateMatrices(values, out error);
if (error != "") return;
}
catch (Exception exc)
{
error = "Unhandled exception: " + exc.Message;
}
}
public static void CheckPermutationAxis(string[,] values, ref bool xPossible, ref bool yPossible)
{
xPossible = yPossible = true;
if (values == null) return;
int rowCount = values.GetLength(0) - 1;
int colCount = values.GetLength(1) - 1;
List<char> aaList = new List<char>();
for (int iCol = 1; iCol <= colCount; iCol++)
{
string s = values[0, iCol].Trim();
if (aaList.Contains(s[0]))
{
xPossible = false;
break;
}
aaList.Add(s[0]);
}
aaList.Clear();
for (int iRow = 1; iRow <= rowCount; iRow++)
{
string s = values[iRow, 0].Trim();
char aa = s.Length > 0 ? s[0] : ' ';
if (aaList.Contains(aa))
{
yPossible = false;
break;
}
aaList.Add(aa);
}
}
public static OPALArray ReadFromFile(string filename)
{
try
{
OPALArray OA = null;
if (File.Exists(filename))
{
OA = JsonConvert.DeserializeObject<OPALArray>(File.ReadAllText(filename));
if (OA.Version == "")
{
OA.Version = "Old version";
OA.Upgrade("PositiveThreshold");
OA.Upgrade("NormalizedMatrixRange");
}
}
return OA;
}
catch { return null; }
}
public static bool SaveToFile(string filename, OPALArray OA)
{
try
{
OA.Version = typeof(Analyzer).Assembly.GetName().Version.ToString();
string json = JsonConvert.SerializeObject(OA);
File.WriteAllText(filename, json);
return true;
}
catch { return false; }
}
private void AddNormalizedPeptideWeight(double weight, int pos, char replaceBy)
{
if (string.IsNullOrEmpty(WildTypePeptide))
return;
string s = "", e = "";
if (pos > 0)
s = WildTypePeptide.Substring(0, pos);
if (pos < WildTypePeptide.Length - 1)
e = WildTypePeptide.Substring(pos + 1);
string peptide = s + replaceBy + e;
if (/*peptide == WildTypePeptide && */NormalizedPeptideWeights.ContainsKey(peptide))
return;
NormalizedPeptideWeights.Add(peptide, weight);
}
/// <summary>
/// Dictionary of weight for each char at every position
/// </summary>
/// <param name="PA"></param>
/// <returns></returns>
public Dictionary<int, Dictionary<char, double>> GenerateWeights()
{
try
{
int motifsize = PermutationXAxis ? RowCount : ColCount;
WildTypePeptide = new string('X', motifsize);
NormalizedPeptideWeights.Clear();
Dictionary<int, Dictionary<char, double>> weights;
Dictionary<int, double> totalWeightsPerPos;
weights = new Dictionary<int, Dictionary<char, double>>();
totalWeightsPerPos = new Dictionary<int, double>();
for (int i = 0; i < motifsize; i++)
{
totalWeightsPerPos.Add(i, 0);
weights.Add(i, new Dictionary<char, double>());
}
for (int rowind = 0; rowind < RowCount; rowind++)
{
for (int colind = 0; colind < ColCount; colind++)
{
double weight = NormalizedMatrix[rowind, colind];
if (PermutationXAxis)
{
if (this.PositionYAxisTopToBottom)
{
if (weight > PositiveThreshold)
{
weights[rowind].Add(Permutation[colind], weight);
totalWeightsPerPos[rowind] += weight;
}
AddNormalizedPeptideWeight(weight, rowind, Permutation[colind]);
}
else //if Position Matrix is read from bottom to top, rowind should be reversed
{
if (weight > PositiveThreshold)
{
weights[RowCount - rowind - 1].Add(Permutation[colind], weight);
totalWeightsPerPos[RowCount - rowind - 1] += weight;
}
AddNormalizedPeptideWeight(weight, RowCount - rowind - 1, Permutation[colind]);
}
}
else
{
if (weight > PositiveThreshold)
{
weights[colind].Add(Permutation[rowind], weight);
totalWeightsPerPos[colind] += weight;
}
AddNormalizedPeptideWeight(weight, colind, Permutation[rowind]);
}
}
}
for (int i = 0; i < motifsize; i++)
{
List<char> charlist = weights[i].Keys.ToList();
foreach (char c in charlist)
weights[i][c] /= totalWeightsPerPos[i];
}
return weights;
}
catch
{
return null;
}
}
}
}