/*
* Class ImmunoAssay
*
* Contains methods for fitting immunoassay results to a choice of equations,
* for comparing the fit between equations and for obtaining the
* analyte concentration for a given assay response
*
* This class is a subclass of Regression
*
* WRITTEN BY: Dr Michael Thomas Flanagan
*
* DATE: 20 January - 7 February 2011
* MODIFIED: 17-28 February 2011, 1 March 2011, 11 March 2011, 30 March 2011, 1 April 2012
*
* DOCUMENTATION:
* See Michael Thomas Flanagan's Java library on-line web page:
* http://www.ee.ucl.ac.uk/~mflanaga/java/ImmunoAssay.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/Regression.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/
*
* Copyright (c) 2011 Michael Thomas Flanagan
*
* PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/
package flanagan.physchem;
import flanagan.physchem.ImmunoChemistry;
import flanagan.math.Conv;
import flanagan.math.Fmath;
import flanagan.math.ArrayMaths;
import flanagan.analysis.Regression;
import flanagan.analysis.RegressionFunction;
import flanagan.analysis.RegressionFunction3;
import flanagan.analysis.Stat;
import flanagan.interpolation.CubicSpline;
import flanagan.interpolation.CubicInterpolation;
import flanagan.interpolation.LinearInterpolation;
import flanagan.plot.PlotGraph;
import flanagan.io.FileInput;
import flanagan.io.FileOutput;
import flanagan.io.FileChooser;
import flanagan.io.Db;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.io.File;
public class ImmunoAssay extends Regression{
private double[] analyteConcns = null; // analyte concentrations
private double[] log10AnalyteConcns = null; // log10(analyte concentrations)
private double[] logeAnalyteConcns = null; // loge(analyte concentrations)
private boolean analyteEntered = false; // = true when analyte concentrations have been entered
private int analyteConcnFlag = 0; // = 0: analyte entered as analyte concentrations
// = 1: analyte entered as log10(analyte concentrations)
// = 2: analyte entered as loge(analyte concentrations)
private int nAnalyteConcns = 0; // number of entered standard analyte concentrations
private double[] responses = null; // standard curve responses
private double[] log10Responses = null; // log10(standard curve responses)
private double[] logeResponses = null; // loge(standard curve responses)
private boolean responsesEntered = false; // = true when standard curve responses have been entered
private int nResponses = 0; // number of entered responses
private int responsesFlag = 0; // = 0: responses entered as responses
// = 1: responses as log10(responses)
// = 2: responses as loge(responses)
private boolean responsesPlot = true; // = false if log needed and responses are <= 0
private double responsesMax = 0; // maximum response value
private double responsesMin = 0; // minimum response value
private double responsesRange = 0; // response value range
private int nWeights = 0; // number of entered weights
private double weightsMean = 0.0; // mean of the weights
private double weightsSD = 0.0; // standard deviation of the weights
private int weightOption = 0; // = 0: no weights or weights as entered
// = 1: weights proportional to the responses
// = 2: weights proportional to the square root of the responses
private boolean setDataOneDone = false; // = true when setDataOne has been successfully called
private double[] interpolationConcns = null; // analyte concentrations for standard curve responses after fitting or interpolation
private double[] calculatedResponses = null; // calculated standard curve responses after fitting or interpolation
private String[] propagatedErrors = null; // propagated errors in the calculation of concentrations for given responses
private int nInterp = 1000; // number of points in the fitted or interpolation response curve
private CubicSpline interp = null; // Cubic spline instance for getSampleConcn method
private LinearInterpolation linterp = null; // LinearInterpolation instance for getSampleConcn method
private CubicSpline errorp = null; // Cubic spline instance for getSampleConcn error method
private CubicSpline cs = null; // Cubic spline instance for CubicSpline method
private CubicInterpolation ci = null; // Cubic interpolation instance for CubicInterpolation method
private LinearInterpolation li = null; // Linear interpolation instance for LinearInterpolation method
private String titleZero = null; // Assay title
private String titleOne = null; // Plot and print title - line two
private String filename = "ImmunoAssayOutput.txt"; // Default output file name
private String dataFilename = null; // Data input file name
private boolean dataRead = false; // = true if data read from a file
private double interpAnalyteStart = 0.0; // start of the non-ambiguous analyte concentration range of the fitted standard curve
private double interpAnalyteEnd = 0.0; // end of the non-ambiguous analyte concentration range of the fitted standard curve
private double interpResponseStart = 0.0; // start of the non-ambiguous range of the fitted standard curve
private double interpResponseEnd = 0.0; // end of the non-ambiguous response range of the fitted standard curve
private int interpStartIndex = 0; // index of the start of the non-ambiguous response range of the fitted standard curve
private int interpEndIndex = 0; // index of the end of the non-ambiguous range of the fitted standard curve
private boolean ambigCheck = false; // = true if the fitted standard curve is not monotonic
private boolean curveDirection = true; // = true if the responses increase with increasing analyte concentration
// = false if the responses decrease with increasing analyte concentration
private double workingResponseMin = 0.0; // minimum of the working range of the fitted standard curve
private double workingResponseMax = 0.0; // maximum of the working range of the fitted standard curve
private double minimumAerror = 0.0; // minimum propagated sample analyte concentration error
private double maximumAerror = 0.0; // maximum propagated sample analyte concentration error
private double meanAerror = 0.0; // mean propagated sample analyte concentration error
private double sdAerror = 0.0; // standard deviation of the propagated sample analyte concentration error
private int nTurningPoints = 0; // number of turning points in the standard curve
private int[] turnIndices = null; // indices of turning points in the standard curve
private int nWorking = 0; // number of data points in the unambiguous range
private int polyDegree = 0; // degree of polynomial in polynomial fitting
private int bestPolyDegree = 0; // degree of polynomial in best polynomial fitting
private int polyNterms = 0; // number of terms in the non-integer polynomial fitting
private String compFilename = "ImmunoAssayComparison.txt"; // Default comparison output file name
private int resultFlag = -1; // comparison result flag
private double significance = 0.05; // probability significance level in statistical tests
private double sampleResponse = 0.0; // last entered sample response of unkown concentration
private double sampleConcn = 0.0; // analyte concn found for last entered sample response of unkown concentration
private double sampleError = 0.0; // error of the analyte concn found for last entered sample response of unkown concentration
private int nPlot = 0; // Number of entered data points included in the plotting
private int plotOptions = 0; // = 0: analyte concn v. response
// = 1: log10(analyte concn) v. response
// = 2: loge(analyte concn) v. response
// = 3: log10(analyte concn) v. log10(response)
private boolean supressPlot = false; // = true: plot display supressed
// = 4: loge(analyte concn) v. loge(response)
private int nMethods = 15; // Number of methods available
private String[] methodNames = new String[nMethods]; // Method names array
private int[] methodIndices = new int[nMethods]; ; // Indices linking methodUsed (below) to displayed names
private int methodUsed = 8; // Fitting or interpolation method used
// = 0: Cubic spline
// = 1: Cubic interpolation
// = 2: Polynomial of user supplied degree n
// = 3: Best fit polynomial
// = 4: Non-integer polynomial
// = 5: Sigmoid threshold function
// = 6: Sips sigmoid function
// = 7: Four parameter logistic function (EC50 dose response curve)
// = 8: Five parameter logistic function
// = 9: Shifted rectangular hyperbola
// = 10: Rectangular hyperbola
// = 11: Amersham equation
// = 12: Four parameter logistic function (top and bottom fixed)
// = 13: Five parameter logistic function (top and bottom fixed)
// = 14: Linear interpolation
private boolean sampleErrorFlag = true; // = false if method cannot calculate a sample concentration err9or
private boolean amershamFlag1 = true; // = true: Amersham model initial estimates calculated
// = false: Amersham model initial estimates supplied
private double bottom = 0.0; // Logistic fixed method bottom value
private double top = 0.0; // Logistic fixed method top value
private boolean compWindow = false; // = true if comparison choice window used
private String[] outliers = null; // list of outlier indicator messages
private double residualsMean = 0.0; // mean of the unweighted residuals
private double residualsSD = 0.0; // standard deviation of the unweighted residuals
private double confidenceLevel = 0.95; // Confidence level of the confidence interval used on outlier testing
// Outlier test critical values
// Sample size
private double[] critSize = {3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 30, 40, 50, 60, 100, 120};
// 99% level
private double[] critValuesOne = {1.15, 1.49, 1.75, 1.94, 2.10, 2.22, 2.32, 2.41, 2.55, 2.66, 2.71, 2.75, 2.82, 2.88, 3.10, 3.24, 3.34, 3.41, 3.60, 3.66};
// 95% level
private double[] critValuesFive = {1.15, 1.46, 1.67, 1.82, 1.94, 2.03, 2.11, 2.18, 2.29, 2.37, 2.41, 2.44, 2.50, 2.56, 2.74, 2.87, 2.96, 3.03, 3.21, 3.27};
private CubicSpline critValues = null; // critical value interpolation function
private double anscombeC = 0.0; // Calculated critical value
private boolean outlierFlag = false; // = true if possible outliers found
private boolean degSet = false; // = true if polynomial degree set
private boolean nTermsSet = false; // = true if polynomial nterms set
private boolean fourBotTopSet = false; // = true if four parameter top and bottom set
private boolean fiveBotTopSet = false; // = true if five parameter top and bottom set
// CONSTRUCTORS
// Constructor without title
public ImmunoAssay(){
this.titleZero = "Program ImmunoAssay";
super.supressErrorMessages = true;
super.trueErrors = false;
this.methodList();
}
// Constructor with title
public ImmunoAssay(String title){
this.titleZero = "Program ImmunoAssay: " + title;
super.supressErrorMessages = true;
super.trueErrors = false;
this.methodList();
}
// Constructor without title
// with total analyte concns and arbitrary unit responses entered
public ImmunoAssay(double[] analytes, double[] responses){
this.titleZero = "Program ImmunoAssay";
this.nAnalyteConcns = analytes.length;
this.analyteConcns = Conv.copy(analytes);
this.analyteConcnFlag = 0;
this.analyteEntered = true;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
this.responsesEntered = true;
super.xErrorsEntered = false;
super.yErrorsEntered = false;
super.supressErrorMessages = true;
super.trueErrors = false;
this.methodList();
}
// Constructor without title
// with total analyte concns, arbitrary unit responses and weights entered
public ImmunoAssay(double[] analytes, double[] responses, double[] yWeights){
this.titleZero = "Program ImmunoAssay";
this.nAnalyteConcns = analytes.length;
this.analyteConcns = Conv.copy(analytes);
this.analyteConcnFlag = 0;
this.analyteEntered = true;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
this.responsesEntered = true;
this.nWeights = yWeights.length;
super.yErrors = super.checkForZeroWeights(yWeights);
if(super.weightOpt)super.weightFlag = 1;
super.xErrorsEntered = false;
super.yErrorsEntered = true;
super.supressErrorMessages = true;
super.trueErrors = false;
this.methodList();
}
// Constructor without title
// with total analyte concns, arbitrary unit responses and both analyte and response errors entered
public ImmunoAssay(double[] analytes, double[] responses, double[] xWeights, double[] yWeights){
this.titleZero = "Program ImmunoAssay";
this.nAnalyteConcns = analytes.length;
this.analyteConcns = Conv.copy(analytes);
this.analyteConcnFlag = 0;
this.analyteEntered = true;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
this.responsesEntered = true;
this.nWeights = yWeights.length;
super.yErrors = yWeights;
double[][] xErrors = new double[1][this.nWeights];
xErrors[0] = xWeights;
super.xErrors = xErrors;
super.jointZeroCheck(xErrors, yWeights);
if(super.weightOpt)super.weightFlag = 1;
super.xErrorsEntered = true;
super.yErrorsEntered = true;
super.weightOpt = true;
super.weightFlag = 1;
super.supressErrorMessages = true;
super.trueErrors = true;
this.methodList();
}
// Constructor without title
// with total analyte concns, arbitrary unit responses and weights entered
public ImmunoAssay(double[] analytes, double[] responses, double[] yWeights, String title){
this.titleZero = "Program ImmunoAssay: " + title;
this.nAnalyteConcns = analytes.length;
this.analyteConcns = Conv.copy(analytes);
this.analyteConcnFlag = 0;
this.analyteEntered = true;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
this.responsesEntered = true;
this.nWeights = yWeights.length;
super.yErrors = super.checkForZeroWeights(yWeights);
if(super.weightOpt)super.weightFlag = 1;
super.xErrorsEntered = false;
super.yErrorsEntered = true;
super.supressErrorMessages = true;
super.trueErrors = false;
this.methodList();
}
// Constructor with title
// with total analyte concns and arbitrary unit responses entered
public ImmunoAssay(double[] analytes, double[] responses, String title){
this.titleZero = "Program ImmunoAssay: " + title;
this.nAnalyteConcns = analytes.length;
this.analyteConcns = Conv.copy(analytes);
this.analyteConcnFlag = 0;
this.analyteEntered = true;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
super.xErrorsEntered = false;
super.yErrorsEntered = false;
this.responsesEntered = true;
super.supressErrorMessages = true;
super.trueErrors = false;
this.methodList();
}
// Constructor with title
// with total analyte concns, arbitrary unit responses and yWeights entered
public ImmunoAssay(double[] analytes, double[] responses, double[] xWeights, double[] yWeights, String title){
this.titleZero = "Program ImmunoAssay: " + title;
this.nAnalyteConcns = analytes.length;
this.analyteConcns = Conv.copy(analytes);
this.analyteEntered = true;
this.analyteConcnFlag = 0;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
this.responsesEntered = true;
this.nWeights = yWeights.length;
super.yErrors = yWeights;
double[][] xErrors = new double[1][this.nWeights];
xErrors[0] = xWeights;
super.xErrors = xErrors;
super.jointZeroCheck(xErrors, yWeights);
super.xErrorsEntered = true;
super.yErrorsEntered = true;
super.weightOpt = true;
super.weightFlag = 1;
super.supressErrorMessages = true;
super.trueErrors = true;
this.methodList();
}
// Construct an indexed method list
private void methodList(){
// initialise method names and method name indices
// methodUsed
// = 0: Cubic spline
// = 1: Cubic interpolation
// = 2: Polynomial of user supplied degree n
// = 3: Best fit polynomial
// = 4: Non-integer polynomial
// = 5: Sigmoid threshold function
// = 6: Sips sigmoid function
// = 7: Four parameter logistic function (EC50 dose response curve)
// = 8: Five parameter logistic function
// = 9: Shifted rectangular hyperbola
// = 10: Rectangular hyperbola
// = 11: Amersham equation
// = 12: Four parameter logistic function (top and bottom fixed)
// = 13: Five parameter logistic function (top and bottom fixed)
// = 14: Linear interpolation
this.methodNames[0] = "CubicSpline";
this.methodIndices[0] = 0;
this.methodNames[1] = "Five parameter logistic function";
this.methodIndices[1] = 13;
this.methodNames[2] = "Five parameter logistic function (top and bottom fixed)";
this.methodIndices[2] = 6;
this.methodNames[3] = "Four parameter logistic function";
this.methodIndices[3] = 5;
this.methodNames[4] = "Four parameter logistic function (top and bottom fixed)";
this.methodIndices[4] = 7;
this.methodNames[5] = "Best fit polynomial";
this.methodIndices[5] = 8;
this.methodNames[6] = "Polynomial of user supplied degree ";
this.methodIndices[6] = 9;
this.methodNames[7] = "Non-integer polynomial";
this.methodIndices[7] = 3;
this.methodNames[8] = "Sigmoid threshold function";
this.methodIndices[8] = 1;
this.methodNames[9] = "Sips sigmoid function";
this.methodIndices[9] = 10;
this.methodNames[10] = "Shifted rectangular hyperbola";
this.methodIndices[10] = 11;
this.methodNames[11] = "Rectangular hyperbola";
this.methodIndices[11] = 12;
this.methodNames[12] = "Amersham equation";
this.methodIndices[12] = 4;
this.methodNames[13] = "Cubic interpolation";
this.methodIndices[13] = 2;
this.methodNames[14] = "Linear interpolation";
this.methodIndices[14] = 14;
}
// ENTER DATA METHODS
// Enter standard analyte concentrations
public void enterAnalyteConcns(double[] concn){
this.setDataOneDone = false;
this.nAnalyteConcns = concn.length;
this.analyteConcns = Conv.copy(concn);
this.analyteConcnFlag = 0;
this.analyteEntered = true;
}
// Enter standard analyte concentrations as log10(concn)
public void enterAnalyteConcnsAsLog10(double[] logconcn){
this.setDataOneDone = false;
this.nAnalyteConcns = logconcn.length;
this.log10AnalyteConcns = Conv.copy(logconcn);
this.analyteConcns = this.antiLog10(logconcn);
this.analyteConcnFlag = 1;
this.analyteEntered = true;
}
// Enter standard analyte concentrations as loge(concn)
public void enterAnalyteConcnsAsLogE(double[] logconcn){
this.setDataOneDone = false;
this.nAnalyteConcns = logconcn.length;
this.logeAnalyteConcns = Conv.copy(logconcn);
this.analyteConcns = this.antiLoge(logconcn);
this.analyteConcnFlag = 2;
this.analyteEntered = true;
}
// Enter standard curve responses
public void enterResponses(double[] responses){
this.setDataOneDone = false;
this.nResponses = responses.length;
this.responses = Conv.copy(responses);
this.responsesFlag = 0;
this.responsesEntered = true;
}
// Enter standard curve responses as log10
public void enterResponsesAsLog10(double[] responses){
this.setDataOneDone = false;
this.nResponses = responses.length;
this.log10Responses = Conv.copy(responses);
this.responses = this.antiLog10(responses);
this.responsesFlag = 1;
this.responsesEntered = true;
}
// Enter standard curve responses as loge
public void enterResponsesAsLogE(double[] responses){
this.setDataOneDone = false;
this.nResponses = responses.length;
this.logeResponses = Conv.copy(responses);
this.responses = this.antiLoge(responses);
this.responsesFlag = 2;
this.responsesEntered = true;
}
// Enter weighting factors
public void enterWeights(double[] yWeights){
this.setDataOneDone = false;
this.nWeights = yWeights.length;
super.yErrors = super.checkForZeroWeights(yWeights);
super.yErrorsEntered = true;
if(super.weightOpt)super.weightFlag = 1;
}
// Enter weighting factors as log10
public void enterWeightsAslog10(double[] yWeights){
this.setDataOneDone = false;
this.nWeights = yWeights.length;
super.yErrors = this.antiLog10(super.checkForZeroWeights(yWeights));
if(super.weightOpt)super.weightFlag = 1;
super.yErrorsEntered = true;
}
// Enter weighting factors as loge
public void enterWeightsAslogE(double[] yWeights){
this.setDataOneDone = false;
this.nWeights = yWeights.length;
super.yErrors = this.antiLoge(super.checkForZeroWeights(yWeights));
if(super.weightOpt)super.weightFlag = 1;
super.yErrorsEntered = true;
}
// Convert multiplicative weighting factors to uncertainty estimate yWeights
public void enterMultiplicativeWeights(double[] yWeights){
this.setDataOneDone = false;
this.nWeights = yWeights.length;
super.yErrors = Conv.copy(super.checkForZeroWeights(yWeights));
for(int i=0; i0){
super.yErrors = new double[this.nResponses];
for(int i=0; i0){
super.yErrors = new double[this.nResponses];
for(int i=0; i0){
pos3 = this.separatorPosition(line);
if(pos3==-1){
word = line.trim();
}
else{
word = (line.substring(0, pos3)).trim();
}
super.yErrors[i] = Double.parseDouble(word);
if(super.yErrors[i]==1.0)weightCheck++;
}
}
}
}
this.analyteConcnFlag = 0;
this.analyteEntered = true;
this.responsesEntered = true;
if(weightCheck!=this.nAnalyteConcns){
this.nWeights = this.nAnalyteConcns;
super.yErrors = super.checkForZeroWeights(super.yErrors);
if(super.weightOpt)super.weightFlag = 1;
super.yErrorsEntered = true;
}
this.dataRead = true;
}
// Return entered analyte concentration
public double[] getAnalyteConcns(){
return this.analyteConcns;
}
// Return entered responses
public double[] getResponses(){
return this.responses;
}
// Return entered title
public String getTitle(){
return this.titleZero;
}
// INTERPOLATION METHODS
// CubicSpline interpolation
public void cubicSpline(){
if(this.nAnalyteConcns<3)throw new IllegalArgumentException("Method cubicSpline requres at least 3 data points; only " + this.nAnalyteConcns + " were supplied");
this.methodUsed = 0;
this.sampleErrorFlag = false;
this.titleOne = "Cubic spline ";
if(!this.setDataOneDone)this.setDataOne();
this.cs = new CubicSpline(this.analyteConcns, this.responses);
for(int i=0; i al = super.bestPolynomial();
this.bestPolyDegree = ((Integer)al.get(0)).intValue();
this.titleOne += " " + this.bestPolyDegree;
for(int i=0; i=this.nAnalyteConcns/2){
if(this.responses[this.nAnalyteConcns-1]>=this.responses[0]){
throw new IllegalArgumentException("The data appears incompatible with a competitive assay");
}
else{
System.out.println("The data has been queried as that of a competitive assay but the fitting has not been aborted");
}
}
}
}
// CHOOSE A FITTING EQUATION
// Select a single fitting equation
public void selectEquation(){
ArrayList
Ads help maintain this website.