Debian Codegraph @

src/dr/app/mapper/application/MapperDocument.java

raw symbols SCIP
1 /*
2 * MapperDocument.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.app.mapper.application;
27
28 import dr.evolution.util.*;
29 import dr.evolution.util.Date;
30 import dr.inference.trace.TraceList;
31 import dr.util.DataTable;
32
33 import java.util.*;
34
35 /**
36 * A class to store the current mapper document (i.e. all the data)
37 *
38 * @author Andrew Rambaut
39 */
40 public final class MapperDocument {
41
42 public enum MeasurementType {
43 INTERVAL,
44 POINT,
45 THRESHOLD,
46 MISSING
47 }
48
49 public MapperDocument() {
50 }
51
52 public void addTaxa(Collection<Taxon> newTaxa) {
53 for (Taxon taxon : newTaxa) {
54 addTaxon(taxon);
55 }
56 fireTaxaChanged();
57 }
58
59 private void addTaxon(Taxon taxon) {
60 taxa.add(taxon);
61 taxonMap.put(taxon.getId(), taxon);
62 }
63
64 public Taxon getTaxon(String id) {
65 return taxonMap.get(id);
66 }
67
68 public List<Taxon> getTaxa() {
69 return taxa;
70 }
71
72 public void addTable(DataTable<String[]> dataTable) {
73 // column indices in table
74 final int COLUMN_LABEL = 0;
75 final int SERUM_STRAIN = 2;
76 final int ROW_LABEL = 1;
77 final int VIRUS_STRAIN = 3;
78 final int SERUM_DATE = 4;
79 final int VIRUS_DATE = 5;
80 final int TITRE = 6;
81
82 List<String> strainNames = new ArrayList<String>();
83
84 List<String> virusNames = new ArrayList<String>();
85 List<String> serumNames = new ArrayList<String>();
86 Map<String, Double> strainDateMap = new HashMap<String, Double>();
87
88 int thresholdCount = 0;
89
90 double earliestDate = Double.POSITIVE_INFINITY;
91 for (int i = 0; i < dataTable.getRowCount(); i++) {
92 String[] values = dataTable.getRow(i);
93 String columnLabel = values[COLUMN_LABEL];
94
95 String columnStrainName = values[COLUMN_LABEL];
96
97 Taxon columnStrain = getTaxon(columnStrainName);
98 if (columnStrain == null) {
99 columnStrain = new Taxon(columnStrainName);
100 // double date = Double.parseDouble(values[SERUM_DATE]);
101 // columnStrain.setDate(new Date(date, false, new Date(0.0, Units.Type.YEARS, false)));
102 // addTaxon(columnStrain);
103 }
104
105 String rowLabel = values[ROW_LABEL];
106 String rowStrainName = values[VIRUS_STRAIN];
107
108 Taxon rowStrain = getTaxon(rowStrainName);
109 if (rowStrain == null) {
110 rowStrain = new Taxon(rowStrainName);
111 // double date = Double.parseDouble(values[VIRUS_DATE]);
112 // rowStrain.setDate(new Date(date, false, new Date(0.0, Units.Type.YEARS, false)));
113 addTaxon(rowStrain);
114 }
115
116 boolean isThreshold = false;
117 double rawTitre = Double.NaN;
118 if (values[TITRE].length() > 0) {
119 try {
120 rawTitre = Double.parseDouble(values[TITRE]);
121 } catch (NumberFormatException nfe) {
122 // check if threshold below
123 if (values[TITRE].contains("<")) {
124 rawTitre = Double.parseDouble(values[TITRE].replace("<",""));
125 isThreshold = true;
126 thresholdCount++;
127 }
128 // check if threshold above
129 if (values[TITRE].contains(">")) {
130 throw new IllegalArgumentException("Error in measurement: unsupported greater than threshold at row " + (i+1));
131 }
132 }
133 }
134
135 MeasurementType type = (isThreshold ? MeasurementType.THRESHOLD : MeasurementType.POINT);
136 Measurement measurement = new Measurement(columnLabel, columnStrain, rowLabel, rowStrain, type, rawTitre);
137
138 measurements.add(measurement);
139
140 }
141
142 fireTaxaChanged();
143 }
144
145 public List<Measurement> getMeasurements() {
146 return measurements;
147 }
148
149 private final List<Taxon> taxa = new ArrayList<Taxon>();
150 private final Map<String, Taxon> taxonMap = new HashMap<String, Taxon>();
151
152 private final List<TraceList> traceLists = new ArrayList<TraceList>();
153
154 private final List<Measurement> measurements = new ArrayList<Measurement>();
155 private final List<String> columnLabels = new ArrayList<String>();
156 private final List<String> rowLabels = new ArrayList<String>();
157
158 public class Measurement {
159 private Measurement(final String column, final Taxon columnStrain, final String row, final Taxon rowStrain, final MeasurementType type, final double titre) {
160 this.column = column;
161 this.columnStrain = columnStrain;
162 this.row = row;
163 this.rowStrain = rowStrain;
164
165 this.type = type;
166 this.titre = titre;
167 this.log2Titre = Math.log(titre) / Math.log(2);
168 }
169
170 final String column;
171 final String row;
172 final Taxon columnStrain;
173 final Taxon rowStrain;
174
175 final MeasurementType type;
176 final double titre;
177 final double log2Titre;
178
179 };
180
181 // Listeners and broadcasting
182 public void addListener(Listener listener) {
183 listeners.add(listener);
184 }
185
186 public void removeListener(Listener listener) {
187 listeners.remove(listener);
188 }
189
190 public void fireTaxaChanged() {
191 for (Listener listener : listeners) {
192 listener.taxaChanged();
193 }
194 }
195
196 private final List<Listener> listeners = new ArrayList<Listener>();
197
198 public interface Listener {
199 void taxaChanged();
200 }
201
202 }