protected void configureParameters()
{
this.rhsSolution = new ComplexBufferPredictor();
this.declareParameter("sigma",new DblMatrix(1.e-3)); // the overall MSE
//System.out.println("Configuring parameters...");
Vector exps = this.bufferData.getExperimentNames();
String expname;
Vector subsample,obs;
String ssid;
this.declareParameter("smoothing_param",new DblMatrix(0.2));
this.declareParameter("aliquot_sigma",new DblMatrix(0.0001)); // Assumed to be the std error of measurement on 1 L.
this.declareParameter("Vo_sigma",new DblMatrix(0.01)); // Assumed to be the std error of measurement on 1 L.
HashMap divisors = new HashMap();
// if (exps.size() > 0)
// {
// expname = (String)exps.get(0);
//
// subsample = this.bufferData.getBufferCoefficientsSubsampleIDs(expname);
// //We will accumulate a sum of pKa and pKb. At the end we'll divide.
// if (subsample.size() >0) //Buffer coefficients are given for this buffer.
// {
// if (this.bufferData.getNickname() == null)
// {
// this.bufferData.setNickname("com.root");
// //System.out.println(this.bufferData.getClass().toString());
// //throw new RuntimeException("See I told you so!");
// }
// }
// }
BufferSolution sol = new BufferSolution();
sol.setConnection(this.connection);
sol.setRepository(this.repository);
sol.initialize();
System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
sol.show("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
System.out.println("NICKNAME IS::"+this.bufferData.getNickname());
sol.add(this.bufferData,new DblMatrix(1.0));
sol.show("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
this.referenceSolution = sol;
//HashMap containedCnts = sol.getContents();
String[] comps = sol.getComponents();
this.containedcomponents = comps;
//Set contbuffers = containedCnts.keySet();
//Iterator iter2 = contbuffers.iterator();
//while (iter2.hasNext())
for (int c=0;c<comps.length;c++)
{
//Put in concentration and dissociation constant parameters for every contained buffer.
//
//String bname = (String)iter2.next();
System.out.println("Contained component "+c+"="+comps[c]);
String bname = comps[c];
if (bname != null)
{
bname = bname.trim();
}
if (!bname.equals(""))
{
//DblMatrix avgconc = (DblMatrix)containedCnts.get(bname);
DblMatrix avgconc = sol.getConcentration(bname);
this.initializeCoefficientsFor(bname,avgconc);
}
//this.declareParameter(bname+"_C",avgconc);
//this.avgcoefficients.setParam(bname+"_C",avgconc);
//this.stdcoefficients.setParam(bname+"_C",new DblMatrix(0.01));
}
for (int x=0;x<exps.size();x++)
{
expname = (String)exps.get(x);
subsample = this.bufferData.getTitrationSubsampleIDs(expname);
HashSet seenAlready = new HashSet();
for (int s=0;s<subsample.size();s++)
{
ssid = (String)subsample.get(s);
obs = this.bufferData.getTitrationObservationNumbers(expname,ssid);
for (int k=0;k<obs.size();k++) //For each titration
{
String obsnum = (String)obs.get(k);
String titrant = this.bufferData.getTitrant(expname,ssid,obsnum);
//Only add new parameters once for each titrant that we encounter during a titration.
if (!seenAlready.contains(titrant))
{
//The concentration parameter that gets created here is set to 1
//and represents the fraction of the declared concentration that
//is used for the aliquot (i.e. we assume that a single stock of
//titrant was used).
this.initializeCoefficientsFor(titrant,new DblMatrix(1.0));
seenAlready.add(titrant);
}
}
}
//
//Set pKa/pKb buffer variance components
//Here there are possible gotcha's -- A user has a buffer with 3 pKa but they only record 2
//Then in another experiment another user records 3 -- Solution: These kinds of conflicts will need to
//be resolved at the database level when creating the experiment and adding it's support.
//Assume it's been taken care of by the time we get here. We don't want to use the numerical
//value of the pKa as this is the thing that is changing over the subsamples.
//It will be important to have a separate variance component for each pKa. Consider citric acid
// where the second pKa was recorded incorrectly by 1 pKa unit. Alternatively, in brines, this will
//impart automatic "smoothing" when Davies or other methods don't do a good job at adjusting the pKa/pKb
// --Let the data speak for themselves!!--
//
subsample = this.bufferData.getBufferCoefficientsSubsampleIDs(expname);
//We will accumulate a sum of pKa and pKb. At the end we'll divide.
if (subsample.size() >0) //Buffer coefficients are given for this buffer.
{
//Declare a nominal concentration...
// The nominal concentration of **this** solution.
//By default this concentration is 1.0 which is the accepted
//standard for buffers that are simply mixtures.
//Tyipcally the user will need to imput the actual value in the
//edit parameters input. Probably we will need to make this a standard
//data entry field. Note that we only create the one parameter.
this.declareParameter("C",new DblMatrix(1.0));
int s = 0;
ssid = (String)subsample.get(s);
DblMatrix[] Ka = this.parseKa(this.bufferData.getpKa(expname,ssid));
//Set the nominal salt type for this.
this.nominalSaltType = this.parseSaltType(this.bufferData.getSaltType(expname,ssid));
this.nominalpKa = this.parseSaltType(this.bufferData.getpKa(expname,ssid));
this.nominalpKb = this.parseSaltType(this.bufferData.getpKa(expname,ssid));
for (int i=0;i<Ka.length;i++) //For each ionizable group
{
for (int k=0;k<Ka[i].getN();k++)//For each dissociation this.declareParameter(param,thisKaSum); site on the ith ion.
{
String param = "pKa_"+i+"_"+k;
DblMatrix thisKaSum = null;
if (this.hasParameter(param))
{
thisKaSum = this.getParam(param);
thisKaSum = thisKaSum.plus(DblMatrix.log10(Ka[i].getDblAt(k)).times(-1.0));
this.setParam(param,thisKaSum);
int N = ((Integer)divisors.get(param)).intValue();
divisors.put(param,new Integer(N+1));
}
else
{
thisKaSum = DblMatrix.log10(Ka[i].getDblAt(k)).times(-1.0);
this.declareParameter(param,thisKaSum);
divisors.put(param,new Integer(1));
}
}
}
DblMatrix[] Kb = this.parseKa(this.bufferData.getpKb(expname,ssid));
for (int i=0;i<Kb.length;i++)//For each ionizable group
{
for (int k=0;k<Kb[i].getN();k++)//For each dissociation site on the ith ion.
{
String param = "pKb_"+i+"_"+k;
DblMatrix thisKbSum = null;
if (this.hasParameter(param))
{
thisKbSum = this.getParam(param);
thisKbSum = thisKbSum.plus(DblMatrix.log10(Kb[i].getDblAt(k)).times(-1.0));
this.setParam(param,thisKbSum);
int N = ((Integer)divisors.get(param)).intValue();
divisors.put(param,new Integer(N+1));
}
else
{
thisKbSum = DblMatrix.log10(Kb[i].getDblAt(k)).times(-1.0);
this.declareParameter(param,thisKbSum);
divisors.put(param,new Integer(1));
}
}
}
// ** Assuming no errors in SaltType and that SaltType/ion orderings were the same in each subsample ...
}
//
subsample = this.bufferData.getTitrationSubsampleIDs(expname);
for (int s=0;s<subsample.size();s++)
{
ssid = (String)subsample.get(s);
String pHprobe = this.bufferData.getpHProbe(expname,ssid);
if (pHprobe.trim().equals("")) // pH probe was not recorded
{
pHprobe = "unknown"; //All empty pHprobe then are assumed to be the "same" probe -- typically this just means that the variance component will be larger than if pHprobes were identified properly.
}
this.declareParameter("pHProbe_"+pHprobe+"_sigma",new DblMatrix(0.01)); // the measurement error associated with reading pH with a given electrode.
obs = this.bufferData.getTitrationObservationNumbers(expname,ssid);
for (int k=0;k<obs.size();k++) //For each row of titration table...
{
String obsnum = (String)obs.get(k);
String titrant = this.bufferData.getTitrant(expname,ssid,obsnum);
this.declareParameter("titrant_"+titrant+"_sigma",new DblMatrix(0.01)); // Addumed to be the standard deviation of calibration of a 1.00 Molar stock prep.
}
}
}
////// PRIORS //////
//We'll construct standard priors (mean and std) for the parameters. We can get fancy later on...
Set params = this.getDeclaredParameters();
Iterator iter = params.iterator();
DblParamSet priors = new DblParamSet();
String meanname = null;
String varname = null;
DblMatrix var_val;
DblMatrix mean_val;
while (iter.hasNext())
{
String mainparam = (String)iter.next();
if (mainparam.endsWith("sigma")) //Then it is a variance component.
{
meanname = "mean_log_"+mainparam;
varname = "var_log_"+mainparam;
priors.put(meanname,this.getParam(mainparam));
priors.put(varname,new DblMatrix(0.1));
}
else
{
String[] parts = mainparam.split("_");
if (parts.length >= 3)
{
String r = parts[parts.length-3];
if ((r.equals("pKa")) || (r.equals("pKb")))
{
Integer ion = new Integer(parts[parts.length-1]);
if (ion.intValue() > 0) // Order-restriction priors.
{
meanname = "mean_log_delta_"+mainparam;
varname = "var_log_delta_"+mainparam;
DblMatrix delta = this.getParam(mainparam).minus(this.getpKaLeftOf(mainparam));
priors.put(meanname,DblMatrix.log(delta));
//Set variance to 1/10 the mean.
priors.put(varname,new DblMatrix(0.01));
}
else
{
meanname = "mean_"+mainparam;
varname = "var_"+mainparam;
priors.put(meanname,this.getParam(mainparam));
priors.put(varname,new DblMatrix(0.01));
}
}
else
{
meanname = "mean_"+mainparam;
varname = "var_"+mainparam;
priors.put(meanname,this.getParam(mainparam));
priors.put(varname,new DblMatrix(1.0));
}
}
else
{
meanname = "mean_"+mainparam;
varname = "var_"+mainparam;
priors.put(meanname,this.getParam(mainparam));
priors.put(varname,new DblMatrix(1.0));
}
}
}
this.setPriorParams(priors);
/// Load titrant's BufferStorage into memory by creating BufferSolution's. This should speed
//things up by reducing communication with the repository (file I/O, SQL or whatever the current
//protocol being used does). Just do it once here rather than for every line in the titration table.
//System.out.println("PPPPPPPPPPPPPPPPPPPPPP");
this.titrant_solutions = new HashMap();
String[] pname = this.Params.parameterSet();
for (int m=0;m<pname.length;m++)
{
if (pname[m].startsWith("titrant_"))
{
//Extract the titrant name from the parameter for the variance component.
String titrant_name = pname[m].replace("titrant_","");
titrant_name = titrant_name.replace("_sigma","");
sol = new BufferSolution();
sol.setRepository(this.repository);
sol.setConnection(this.connection);
sol.initialize();
sol.add(titrant_name,new DblMatrix(1.0));
this.titrant_solutions.put(titrant_name,sol);
System.out.println("ADDING THE TITRANT:"+titrant_name);
}
}