Debian Codegraph @

src/dr/evomodel/antigenic/AntigenicTraitLikelihood.java

raw symbols SCIP
1 /*
2 * AntigenicTraitLikelihood.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.antigenic;
27
28 import dr.inference.model.*;
29 import dr.math.MathUtils;
30 import dr.util.Author;
31 import dr.util.Citable;
32 import dr.util.Citation;
33 import dr.util.DataTable;
34 import dr.xml.*;
35
36 import java.io.BufferedReader;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.util.*;
40 import java.util.logging.Logger;
41
42 /**
43 * @author Andrew Rambaut
44 * @author Marc Suchard
45 * @version $Id$
46 */
47 @Deprecated // for the moment at least
48 public abstract class AntigenicTraitLikelihood extends MultidimensionalScalingLikelihood implements Citable {
49
50
51 public AntigenicTraitLikelihood(String name) {
52 super(name);
53 }
54
55 protected void initalizeTable(DataTable<String[]> dataTable, double[][] observationValueTable, ObservationType[][] observationTypeTable, boolean log2Transform) {
56 // the largest measured value for any given column of data
57 double[] maxColumnValue = new double[dataTable.getColumnCount()];
58 // the largest measured value over all
59 double maxAssayValue = 0.0;
60
61 for (int i = 0; i < dataTable.getRowCount(); i++) {
62 String[] dataRow = dataTable.getRow(i);
63
64 for (int j = 0; j < dataTable.getColumnCount(); j++) {
65 Double value = null;
66 ObservationType type = null;
67
68 if (dataRow[j].startsWith("<")) {
69 // is a lower bound
70 value = convertString(dataRow[j].substring(1));
71 if (Double.isNaN(value)) {
72 throw new RuntimeException("Illegal value in table as a threshold");
73 }
74 type = ObservationType.LOWER_BOUND;
75 } else if (dataRow[j].startsWith(">")) {
76 // is a lower bound
77 value = convertString(dataRow[j].substring(1));
78 if (Double.isNaN(value)) {
79 throw new RuntimeException("Illegal value in table as a threshold");
80 }
81 type = ObservationType.UPPER_BOUND;
82 } else {
83 value = convertString(dataRow[j]);
84 type = Double.isNaN(value) ? ObservationType.MISSING : ObservationType.POINT;
85 }
86
87 observationValueTable[i][j] = value;
88 observationTypeTable[i][j] = type;
89
90 if (!Double.isNaN(value)) {
91 if (value > maxColumnValue[j]) {
92 maxColumnValue[j] = value;
93 }
94 if (value > maxAssayValue) {
95 maxAssayValue = value;
96 }
97
98 }
99
100 }
101 }
102
103 if (log2Transform) {
104 // transform and normalize the data...
105 for (int i = 0; i < dataTable.getRowCount(); i++) {
106 for (int j = 0; j < dataTable.getColumnCount(); j++) {
107 observationValueTable[i][j] = transform(observationValueTable[i][j], maxColumnValue[j], 2);
108 // the transformation reverses the bounds
109 if (observationTypeTable[i][j] == ObservationType.UPPER_BOUND) {
110 observationTypeTable[i][j] = ObservationType.LOWER_BOUND;
111 } else if (observationTypeTable[i][j] == ObservationType.LOWER_BOUND) {
112 observationTypeTable[i][j] = ObservationType.UPPER_BOUND;
113 }
114 }
115 }
116 }
117 }
118
119 private double convertString(String value) {
120 try {
121 return java.lang.Double.valueOf(value);
122 } catch (NumberFormatException nfe) {
123 return java.lang.Double.NaN;
124 }
125 }
126
127 protected double transform(final double value, final double maxValue, final double base) {
128 // log2(maxValue / value)
129 return (Math.log(maxValue) - Math.log(value)) / Math.log(base);
130 }
131
132
133 }