forked from PNNL-Comp-Mass-Spec/MASIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsCorrelation.cs
820 lines (681 loc) · 29 KB
/
clsCorrelation.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
using System;
using System.Collections.Generic;
using System.Linq;
using PRISM;
namespace MASIC
{
/// <summary>
/// This class can be used to correlate two lists of numbers (typically mass spectra) to determine their similarity
/// The lists of numbers must have the same number of values
/// Use the BinData function to bin a list of X,Y pairs into bins ranging from .BinStartX to .BinEndX
///
/// These functions were originally written in VB6 and required the use of a C DLL
/// They have since been ported to VB.NET
/// </summary>
public class clsCorrelation : EventNotifier
{
#region "Classwide variables"
/// <summary>
/// Coefficients used by GammaLn
/// </summary>
private readonly double[] mCoefficients;
#endregion
/// <summary>
/// Constructor
/// </summary>
public clsCorrelation()
: this(GetDefaultBinningOptions())
{
}
public clsCorrelation(clsBinningOptions binningOptions)
{
mBinningOptions = binningOptions;
NoiseThresholdIntensity = 0;
mCoefficients = new[] { 76.180091729471457, -86.505320329416776,
24.014098240830911, -1.231739572450155,
0.001208650973866179, -0.000005395239384953 };
}
#region "Constants and Structures"
private const int MIN_NON_ZERO_ION_COUNT = 5;
public enum cmCorrelationMethodConstants
{
Pearson = 0,
Spearman = 1,
Kendall = 2
}
#endregion
#region "Local Member Variables"
private clsBinningOptions mBinningOptions;
#endregion
#region "Property Interface Functions"
public float BinStartX
{
get => mBinningOptions.StartX;
set => mBinningOptions.StartX = value;
}
public float BinEndX
{
get => mBinningOptions.EndX;
set => mBinningOptions.EndX = value;
}
public float BinSize
{
get => mBinningOptions.BinSize;
set
{
if (value <= 0)
value = 1;
mBinningOptions.BinSize = value;
}
}
public float BinnedDataIntensityPrecisionPercent
{
get => mBinningOptions.IntensityPrecisionPercent;
set
{
if (value < 0 || value > 100)
value = 1;
mBinningOptions.IntensityPrecisionPercent = value;
}
}
public float NoiseThresholdIntensity { get; set; }
public bool NormalizeBinnedData
{
get => mBinningOptions.Normalize;
set => mBinningOptions.Normalize = value;
}
public bool SumAllIntensitiesForBin
{
get => mBinningOptions.SumAllIntensitiesForBin;
set => mBinningOptions.SumAllIntensitiesForBin = value;
}
public int MaximumBinCount
{
get => mBinningOptions.MaximumBinCount;
set
{
if (value < 2)
value = 10;
if (value > 1000000)
value = 1000000;
mBinningOptions.MaximumBinCount = value;
}
}
#endregion
private double BetaCF(double a, double b, double x)
{
const int MAX_ITERATIONS = 100;
var EPS = 0.0000003;
const double FP_MIN = 1.0E-30;
int m;
var qab = a + b;
var qap = a + 1.0;
var qam = a - 1.0;
var c = 1.0;
var d = 1.0 - qab * x / qap;
if (Math.Abs(d) < FP_MIN)
d = FP_MIN;
d = 1.0 / d;
var h = d;
for (m = 1; m <= MAX_ITERATIONS; m++)
{
var m2 = 2 * m;
var aa = m * (b - m) * x / ((qam + m2) * (a + m2));
d = 1.0 + aa * d;
if (Math.Abs(d) < FP_MIN)
d = FP_MIN;
c = 1.0 + aa / c;
if (Math.Abs(c) < FP_MIN)
c = FP_MIN;
d = 1.0 / d;
h *= d * c;
aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2));
d = 1.0 + aa * d;
if (Math.Abs(d) < FP_MIN)
d = FP_MIN;
c = 1.0 + aa / c;
if (Math.Abs(c) < FP_MIN)
c = FP_MIN;
d = 1.0 / d;
var del = d * c;
h *= del;
if (Math.Abs(del - 1.0) < EPS)
break;
}
if (m > MAX_ITERATIONS)
{
throw new Exception("a or b too big, or MAX_ITERATIONS too small in clsCorrelation->BetaCF");
}
return h;
}
private double BetaI(double a, double b, double x)
{
if (x < 0.0 || x > 1.0)
{
throw new Exception("Bad x in routine clsCorrelation->BetaI; should be between 0 and 1");
}
double bt;
if (Math.Abs(x) < double.Epsilon || Math.Abs(x - 1.0) < double.Epsilon)
{
bt = 0.0;
}
else
{
bt = Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x));
}
if (x < (a + 1.0) / (a + b + 2.0))
{
return bt * BetaCF(a, b, x) / a;
}
return 1.0 - bt * BetaCF(b, a, 1.0 - x) / b;
}
/// <summary>
/// Bins the data in xData() according to startBinXValue and binSize
/// </summary>
/// <param name="xData"></param>
/// <param name="yData"></param>
/// <param name="binnedYData">Binned y data (the calling class must instantiate this)</param>
/// <param name="binnedOffsetYData">Binned y data, where the StartX value is offset by 50% of the bin size vs. binnedYData</param>
/// <returns>True if successful, otherwise false</returns>
public bool BinData(
List<float> xData,
List<float> yData,
List<float> binnedYData,
List<float> binnedOffsetYData)
{
binnedYData.Clear();
binnedOffsetYData.Clear();
try
{
var dataCount = xData.Count;
if (dataCount <= 0)
{
return false;
}
if (mBinningOptions.BinSize <= 0)
{
mBinningOptions.BinSize = 1;
}
if (mBinningOptions.StartX >= mBinningOptions.EndX)
{
mBinningOptions.EndX = mBinningOptions.StartX + mBinningOptions.BinSize * 10;
}
var binCount = (int)Math.Round((mBinningOptions.EndX - mBinningOptions.StartX) / mBinningOptions.BinSize - 1);
if (binCount < 1)
binCount = 1;
if (binCount > mBinningOptions.MaximumBinCount)
{
mBinningOptions.BinSize = (mBinningOptions.EndX - mBinningOptions.StartX) / mBinningOptions.MaximumBinCount;
binCount = (int)Math.Round((mBinningOptions.EndX - mBinningOptions.StartX) / mBinningOptions.BinSize - 1);
}
var bin2Offset = mBinningOptions.BinSize / 2;
binnedYData.Capacity = binCount;
binnedOffsetYData.Capacity = binCount;
// Initialize the bins
for (var i = 1; i <= binCount; i++)
{
binnedYData.Add(0);
binnedOffsetYData.Add(0);
}
// Fill binnedYData()
BinDataWork(xData, yData, dataCount, binnedYData, binCount, mBinningOptions, 0);
// Fill binnedOffsetYData(), using a StartX of startBinXValue + bin2Offset
BinDataWork(xData, yData, dataCount, binnedOffsetYData, binCount, mBinningOptions, bin2Offset);
}
catch (Exception ex)
{
OnErrorEvent("BinData: " + ex.Message, ex);
return false;
}
return true;
}
private void BinDataWork(IList<float> xData, IList<float> yData, int dataCount,
IList<float> binnedYData, int binCount, clsBinningOptions binningOptions, float offset)
{
try
{
var maximumIntensity = float.MinValue;
for (var index = 0; index < dataCount; index++)
{
if (yData[index] < NoiseThresholdIntensity)
continue;
var binNumber = ValueToBinNumber(xData[index], binningOptions.StartX + offset, binningOptions.BinSize);
if (binNumber >= 0 && binNumber < binCount)
{
if (binningOptions.SumAllIntensitiesForBin)
{
// Add this ion's intensity to the bin intensity
binnedYData[binNumber] += yData[index];
}
// Only change the bin's intensity if this ion's intensity is larger than the bin's intensity
// If it is, then set the bin intensity to equal the ion's intensity
else if (yData[index] > binnedYData[binNumber])
{
binnedYData[binNumber] = yData[index];
}
if (binnedYData[binNumber] > maximumIntensity)
{
maximumIntensity = binnedYData[binNumber];
}
}
else
{
// Bin is Out of range; ignore the value
}
}
if (!(maximumIntensity > float.MinValue))
maximumIntensity = 0;
if (binningOptions.IntensityPrecisionPercent > 0)
{
// Quantize the intensities to .IntensityPrecisionPercent of maximumIntensity
var intensityQuantizationValue = binningOptions.IntensityPrecisionPercent / 100 * maximumIntensity;
if (intensityQuantizationValue <= 0)
intensityQuantizationValue = 1;
if (intensityQuantizationValue > 1)
intensityQuantizationValue = (float)(Math.Round(intensityQuantizationValue, 0));
for (var index = 0; index < binCount; index++)
{
if (Math.Abs(binnedYData[index]) > float.Epsilon)
{
binnedYData[index] = (float)(Math.Round(binnedYData[index] / intensityQuantizationValue, 0)) * intensityQuantizationValue;
}
}
}
if (!binningOptions.Normalize || maximumIntensity <= 0)
return;
for (var index = 0; index < binCount; index++)
{
if (Math.Abs(binnedYData[index]) > float.Epsilon)
{
binnedYData[index] /= maximumIntensity * 100;
}
}
}
catch (Exception ex)
{
OnErrorEvent("BinDataWork: " + ex.Message, ex);
}
}
/// <summary>
/// Finds the correlation value between the two lists of data
/// The lists must have the same number of data points
/// If they have fewer than MIN_NON_ZERO_ION_COUNT non-zero values, the correlation value returned will be 0
/// </summary>
/// <param name="dataList1"></param>
/// <param name="dataList2"></param>
/// <param name="eCorrelationMethod"></param>
/// <returns>Correlation value (0 to 1), or -1 if an error</returns>
/// <remarks>If necessary, use the BinData function before calling this function to bin the data</remarks>
public float Correlate(
IReadOnlyCollection<float> dataList1,
IReadOnlyCollection<float> dataList2,
cmCorrelationMethodConstants eCorrelationMethod)
{
// ReSharper disable once NotAccessedVariable
float probabilityOfSignificance;
// var dataList1Test = new float[] {1, 2, 2, 8, 9, 0, 0, 3, 9, 0, 5, 6};
// var dataList2Test = new float[] {2, 3, 7, 7, 11, 1, 3, 2, 13, 0, 4, 10};
try
{
var dataCount = dataList1.Count;
if (dataList2.Count != dataList1.Count)
{
return -1;
}
// Determine the number of non-zero data points in the two spectra
var nonZeroDataCount = 0;
for (var index = 0; index < dataCount; index++)
{
if (dataList1.ElementAtOrDefault(index) > 0)
nonZeroDataCount += 1;
}
if (nonZeroDataCount < MIN_NON_ZERO_ION_COUNT)
return 0;
nonZeroDataCount = 0;
for (var index = 0; index < dataCount; index++)
{
if (dataList2.ElementAtOrDefault(index) > 0)
nonZeroDataCount += 1;
}
if (nonZeroDataCount < MIN_NON_ZERO_ION_COUNT)
return 0;
switch (eCorrelationMethod)
{
case cmCorrelationMethodConstants.Pearson:
CorrelatePearson(dataList1, dataList2, out var RValue, out probabilityOfSignificance, out var fishersZ);
return RValue;
case cmCorrelationMethodConstants.Spearman:
CorrelateSpearman(dataList1, dataList2, out var diffInRanks, out var ZD, out probabilityOfSignificance, out var RS, out var ProbRS);
return RS;
case cmCorrelationMethodConstants.Kendall:
// ReSharper disable once IdentifierTypo
CorrelateKendall(dataList1, dataList2, out var kendallsTau, out var Z, out probabilityOfSignificance);
return kendallsTau;
default:
return -1;
}
}
catch (Exception ex)
{
OnErrorEvent("Correlate: " + ex.Message, ex);
return -1;
}
}
/// <summary>
/// Performs a Pearson correlation (aka linear correlation) of the two lists
/// The lists must have the same number of data points
/// Code from Numerical Recipes in C
/// </summary>
/// <param name="dataList1"></param>
/// <param name="dataList2"></param>
/// <param name="RValue"></param>
/// <param name="probabilityOfSignificance"></param>
/// <param name="FishersZ"></param>
private void CorrelatePearson(
IReadOnlyCollection<float> dataList1, IReadOnlyCollection<float> dataList2,
out float RValue,
out float probabilityOfSignificance, out float FishersZ)
{
// TINY is used to "regularize" the unusual case of complete correlation
var TINY = 1.0E-20;
// Given two arrays x[1..n] and y[1..n], this routine computes their correlation coefficient
// r (returned as r), the significance level at which the null hypothesis of zero correlation is
// disproved (prob whose small value indicates a significant correlation), and Fisher's z (returned
// as z), whose value can be used in further statistical tests as described above.
var syy = 0.0;
var sxy = 0.0;
var sxx = 0.0;
var ay = 0.0;
var ax = 0.0;
RValue = 0;
probabilityOfSignificance = 0;
FishersZ = 0;
var n = dataList1.Count;
if (n != dataList2.Count)
{
throw new Exception("dataList1 and dataList2 must be lists of the same length");
}
if (n <= 0)
return;
// Find the means
for (var j = 0; j < n; j++)
{
ax += dataList1.ElementAtOrDefault(j);
ay += dataList2.ElementAtOrDefault(j);
}
ax /= n;
ay /= n;
// Compute the correlation coefficient
for (var j = 0; j < n; j++)
{
var xt = dataList1.ElementAtOrDefault(j) - ax;
var yt = dataList2.ElementAtOrDefault(j) - ay;
sxx += xt * xt;
syy += yt * yt;
sxy += xt * yt;
}
RValue = (float)(sxy / (Math.Sqrt(sxx * syy) + TINY));
// Fisher's z transformation
FishersZ = (float)(0.5 * Math.Log((1.0 + RValue + TINY) / (1.0 - RValue + TINY)));
double df = n - 2;
var t = RValue * Math.Sqrt(df / ((1.0 - RValue + TINY) * (1.0 + RValue + TINY)));
// Student's t probability
probabilityOfSignificance = (float)(BetaI(0.5 * df, 0.5, df / (df + t * t)));
}
/// <summary>
/// Performs a Kendall correlation (aka linear correlation) of the two lists
/// The lists must have the same number of data points
/// Code from Numerical Recipes in C
/// </summary>
/// <param name="dataList1"></param>
/// <param name="dataList2"></param>
/// <param name="kendallsTau"></param>
/// <param name="Z"></param>
/// <param name="probabilityOfSignificance"></param>
private void CorrelateKendall(
IReadOnlyCollection<float> dataList1,
IReadOnlyCollection<float> dataList2,
// ReSharper disable once IdentifierTypo
out float kendallsTau,
out float Z,
out float probabilityOfSignificance)
{
// Given data arrays data1[1..n] and data2[1..n], this program returns Kendall's tau as tau,
// its number of standard deviations from zero as z, and its two-sided significance level as prob.
// Small values of prob indicate a significant correlation (tau positive) or anti correlation (tau
// negative).
long n2 = 0;
long n1 = 0;
var intIS = 0;
kendallsTau = 0;
Z = 0;
probabilityOfSignificance = 0;
var n = dataList1.Count;
if (n != dataList2.Count)
{
throw new Exception("dataList1 and dataList2 must be lists of the same length");
}
if (n <= 0)
return;
for (var j = 0; j < n - 1; j++)
{
for (var k = j + 1; k < n; k++)
{
double a1 = dataList1.ElementAtOrDefault(j) - dataList1.ElementAtOrDefault(k);
double a2 = dataList2.ElementAtOrDefault(j) - dataList2.ElementAtOrDefault(k);
var aa = a1 * a2;
if (Math.Abs(aa) > double.Epsilon)
{
n1 += 1;
n2 += 1;
if (aa > 0)
{
intIS += 1;
}
else
{
intIS -= 1;
}
}
else
{
if (Math.Abs(a1) > double.Epsilon)
n1 += 1;
if (Math.Abs(a2) > double.Epsilon)
n2 += 1;
}
}
}
kendallsTau = (float)(intIS / (Math.Sqrt(n1) * Math.Sqrt(n2)));
// ReSharper disable once IdentifierTypo
var svar = (4.0 * n + 10.0) / (9.0 * n * (n - 1.0));
Z = (float)(kendallsTau / Math.Sqrt(svar));
probabilityOfSignificance = (float)(ErfCC(Math.Abs(Z) / 1.4142136));
}
/// <summary>
/// Performs a Spearman correlation of the two lists
/// The lists must have the same number of data points
/// Code from Numerical Recipes in C
/// </summary>
/// <param name="dataList1"></param>
/// <param name="dataList2"></param>
/// <param name="DiffInRanks"></param>
/// <param name="ZD"></param>
/// <param name="probabilityOfSignificance"></param>
/// <param name="RS"></param>
/// <param name="ProbRS"></param>
private void CorrelateSpearman(
IReadOnlyCollection<float> dataList1,
IReadOnlyCollection<float> dataList2,
out float DiffInRanks,
out float ZD,
out float probabilityOfSignificance,
out float RS,
out float ProbRS)
{
// Note: data1 and data2 are re-ordered by this function; thus, they are passed ByVal
// Given two data arrays, data1[0..n-1] and data2[0..n-1], this routine returns their sum-squared
// difference of ranks as D, the number of standard deviations by which D deviates from its null hypothesis
// expected value as zd, the two-sided significance level of this deviation as prob_d,
// Spearman's rank correlation rs as rs, and the two-sided significance level of its deviation from
// zero as prob_rs. The external routine CRank is used. A small value of either prob_d or prob_rs indicates
// a significant correlation (rs positive) or anti correlation (rs negative).
DiffInRanks = 0;
ZD = 0;
probabilityOfSignificance = 0;
RS = 0;
ProbRS = 0;
var n = dataList1.Count;
if (n != dataList2.Count)
{
throw new Exception("dataList1a and dataList2a must be lists of the same length");
}
if (n <= 0)
return;
// Populate arrays so that we can sort the data
var data1 = dataList1.ToArray();
var data2 = dataList2.ToArray();
// Sort data1, sorting data2 parallel to it
Array.Sort(data1, data2);
CRank(n, data1, out var sf);
// Sort data2, sorting data1 parallel to it
Array.Sort(data2, data1);
CRank(n, data2, out var sg);
var DiffInRanksWork = 0.0;
for (var j = 0; j < n; j++)
DiffInRanksWork += SquareNum(data1[j] - data2[j]);
DiffInRanks = (float)(DiffInRanksWork);
double en = n;
var en3n = en * en * en - en;
var AvgD = en3n / 6.0 - (sf + sg) / 12.0;
var fac = (1.0 - sf / en3n) * (1.0 - sg / en3n);
// ReSharper disable once IdentifierTypo
var vard = (en - 1.0) * en * en * SquareNum(en + 1.0) / 36.0 * fac;
ZD = (float)((DiffInRanks - AvgD) / Math.Sqrt(vard));
probabilityOfSignificance = (float)(ErfCC(Math.Abs(ZD) / 1.4142136));
RS = (float)((1.0 - 6.0 / en3n * (DiffInRanks + (sf + sg) / 12.0)) / Math.Sqrt(fac));
fac = (RS + 1.0) * (1.0 - RS);
if (fac > 0.0)
{
var t = RS * Math.Sqrt((en - 2.0) / fac);
var df = en - 2.0;
ProbRS = (float)(BetaI(0.5 * df, 0.5, df / (df + t * t)));
}
else
{
ProbRS = 0.0F;
}
}
/// <summary>
/// Given a zero-based sorted array w(0..n-1), replaces the elements by their rank (1 .. n), including mid-ranking of ties,
/// and returns as s the sum of f^3 - f, where f is the number of elements in each tie.
/// </summary>
/// <param name="n"></param>
/// <param name="w"></param>
/// <param name="s"></param>
private void CRank(int n, IList<float> w, out float s)
{
s = 0;
var j = 0;
while (j < n - 1)
{
if (Math.Abs(w[j + 1] - w[j]) > float.Epsilon)
{
w[j] = j + 1; // Rank = j + 1
j += 1;
}
else
{
var jt = j + 1;
while (jt < n && Math.Abs(w[jt] - w[j]) < float.Epsilon)
jt += 1;
var rank = 0.5F * (j + jt - 1) + 1;
for (var ji = j; ji < jt; ji++)
w[ji] = rank;
float t = jt - j;
s += t * t * t - t; // t^3 - t
j = jt;
}
}
if (j == n - 1)
{
w[n - 1] = n;
}
}
private double ErfCC(double x)
{
var z = Math.Abs(x);
var t = 1.0 / (1.0 + 0.5 * z);
var ans = t * Math.Exp(-z * z - 1.26551223 + t * (1.00002368 + t * (0.37409196 + t * (0.09678418 +
t * (-0.18628806 + t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 +
t * (-0.82215223 + t * 0.17087277)))))))));
if (x >= 0.0)
{
return ans;
}
return 2.0 - ans;
}
/// <summary>
/// Computes the natural logarithm of the Gamma Function
/// </summary>
/// <param name="xx"></param>
/// <returns></returns>
private double GammaLn(double xx)
{
var x = xx;
var y = x;
var tmp = x + 5.5;
tmp -= (x + 0.5) * Math.Log(tmp);
var ser = 1.0000000001900149;
for (var j = 0; j <= 5; j++)
{
y += 1;
ser += mCoefficients[j] / y;
}
return -tmp + Math.Log(2.5066282746310007 * ser / x);
}
public static clsBinningOptions GetDefaultBinningOptions()
{
var binningOptions = new clsBinningOptions
{
StartX = 50,
EndX = 2000,
BinSize = 1,
IntensityPrecisionPercent = 1,
Normalize = false,
SumAllIntensitiesForBin = true, // Sum all of the intensities for binned ions of the same bin together
MaximumBinCount = 100000
};
return binningOptions;
}
[Obsolete("Use GetDefaultBinningOptions, which returns an instance of clsBinningOptions")]
public static void InitializeBinningOptions(out clsBinningOptions binningOptions)
{
binningOptions = GetDefaultBinningOptions();
}
public void SetBinningOptions(clsBinningOptions binningOptions)
{
mBinningOptions = binningOptions;
}
private double SquareNum(double value)
{
if (Math.Abs(value) < double.Epsilon)
{
return 0;
}
return value * value;
}
private int ValueToBinNumber(float thisValue, float startValue, float histogramBinSize)
{
// First subtract StartValue from ThisValue
// For example, if StartValue is 500 and ThisValue is 500.28, then WorkingValue = 0.28
// Or, if StartValue is 500 and ThisValue is 530.83, then WorkingValue = 30.83
var workingValue = thisValue - startValue;
// Now, dividing WorkingValue by BinSize and rounding to nearest integer
// actually gives the bin
// For example, given WorkingValue = 0.28 and BinSize = 0.1, Bin = CInt(Round(2.8,0)) = 3
// Or, given WorkingValue = 30.83 and BinSize = 0.1, Bin = CInt(Round(308.3,0)) = 308
return (int)(Math.Round(workingValue / histogramBinSize, 0));
}
}
}