-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCalibrationCurveDataSet.java
More file actions
153 lines (133 loc) · 5.53 KB
/
CalibrationCurveDataSet.java
File metadata and controls
153 lines (133 loc) · 5.53 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
/*
* Copyright (c) 2017-2019 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.targetedms.calculations.quantification;
import org.apache.commons.math3.fitting.WeightedObservedPoint;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class CalibrationCurveDataSet {
private NormalizationMethod normalizationMethod = NormalizationMethod.NONE;
private RegressionFit regressionFit = RegressionFit.NONE;
private RegressionWeighting regressionWeighting = RegressionWeighting.NONE;
private List<Replicate> replicates = new ArrayList<>();
public Replicate addReplicate(SampleType sampleType, Double analyteConcentration, double sampleDilutionFactor, boolean excludeFromCalibration) {
Replicate replicate = new Replicate(sampleType, analyteConcentration, sampleDilutionFactor, excludeFromCalibration);
replicates.add(replicate);
return replicate;
}
public NormalizationMethod getNormalizationMethod() {
return normalizationMethod;
}
public void setNormalizationMethod(NormalizationMethod normalizationMethod) {
this.normalizationMethod = normalizationMethod;
}
public RegressionFit getRegressionFit() {
return regressionFit;
}
public void setRegressionFit(RegressionFit regressionFit) {
this.regressionFit = regressionFit;
}
public RegressionWeighting getRegressionWeighting() {
return regressionWeighting;
}
public void setRegressionWeighting(RegressionWeighting regressionWeighting) {
this.regressionWeighting = regressionWeighting;
}
public CalibrationCurve getCalibrationCurve(String label) {
if (RegressionFit.NONE == this.regressionFit) {
return CalibrationCurve.forNoExternalStandards();
}
List<WeightedObservedPoint> weightedObservedPoints = new ArrayList<>();
TransitionKeys featuresToQuantifyOn = getFeaturesToQuantifyOn(label);
for (Replicate replicate : replicates) {
if (replicate.getSampleType() != SampleType.standard) {
continue;
}
if (replicate.isExcludeFromCalibration()) {
continue;
}
Double x = replicate.getAnalyteConcentration();
if (x == null) {
continue;
}
Double y = replicate.getNormalizedArea(getNormalizationMethod(), label, featuresToQuantifyOn);
if (y == null) {
continue;
}
weightedObservedPoints.add(getWeightedPoint(x, y));
}
return regressionFit.fit(weightedObservedPoints);
}
public WeightedObservedPoint getWeightedPoint(double x, double y) {
return new WeightedObservedPoint(getRegressionWeighting().getWeighting(x, y), x, y);
}
public TransitionKeys getFeaturesToQuantifyOn(String label) {
if (normalizationMethod instanceof NormalizationMethod.RatioToLabel) {
return null;
}
TransitionKeys transitionKeys = TransitionKeys.EMPTY;
for (Replicate replicate : replicates) {
if (replicate.getSampleType() == SampleType.standard && replicate.getAnalyteConcentration() != null) {
TransitionAreas transitionAreas = replicate.getTransitionAreas(label);
transitionKeys = transitionKeys.union(transitionAreas.getKeys());
}
}
return transitionKeys;
}
@Nullable
public Double getCalculatedConcentration(String label, CalibrationCurve calibrationCurve, Replicate replicate) {
if (calibrationCurve == null) {
return null;
}
Double y = replicate.getNormalizedArea(getNormalizationMethod(), label, getFeaturesToQuantifyOn(label));
if (y == null) {
return null;
}
Double x = calibrationCurve.getX(y);
if (x == null) {
return null;
}
return x * replicate.getSampleDilutionFactor();
}
/**
* Created by nicksh on 4/15/2016.
*/
public class Replicate extends ReplicateData {
private SampleType sampleType;
private Double analyteConcentration;
private double sampleDilutionFactor;
private boolean excludeFromCalibration;
public Replicate(SampleType sampleType, Double analyteConcentration, double sampleDilutionFactor, boolean excludeFromCalibration) {
this.sampleType = sampleType;
this.analyteConcentration = analyteConcentration;
this.sampleDilutionFactor = sampleDilutionFactor;
this.excludeFromCalibration = excludeFromCalibration;
}
public SampleType getSampleType() {
return sampleType;
}
public Double getAnalyteConcentration() {
return analyteConcentration == null ? null : (analyteConcentration / sampleDilutionFactor);
}
public double getSampleDilutionFactor()
{
return sampleDilutionFactor;
}
public boolean isExcludeFromCalibration()
{
return excludeFromCalibration;
}
}
}