View Javadoc
1   /*** MetabolicAssayDataAnalyzer.java - part of the MirkE (say murky) application for colormetric analysis emphesizing 
2   kinetics.
3   
4   Created by: Scott Menor on 21 July, 2004.
5   
6   Copyright (c) 2004,2005 Arizona State University - Cancer Research Institute. All rights reserved.
7   
8   MirkE is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12  
13  MirkE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17  
18  You should have received a copy of the GNU General Public License
19  along with MirkE; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
21  */
22  
23  package edu.asu.cri.MirkE.util;
24  
25  import edu.asu.cri.MirkE.*;
26  import edu.asu.cri.MirkE.exceptions.MirkEApplicationException;
27  import edu.asu.cri.MirkE.dataStructures.*;
28  
29  import javax.swing.*;
30  import java.io.*;
31  import java.util.*;
32  
33  /***
34  
35  */ // TODO - extend JPanel and implement DataAnalyzer (perhaps all of the common stuff really belongs in a separate decorator class)
36  public class MetabolicAssayDataAnalyzer extends DataAnalyzer {
37  	
38      /*** parameter free constructor
39       * @throws MirkEApplicationException
40       *
41       */
42  	public MetabolicAssayDataAnalyzer() throws MirkEApplicationException {
43  		scheduleInitialization();
44  	}
45  	
46  	/***
47  	 * @param mirke
48  	 * @throws MirkEApplicationException
49  	 *
50  	 */
51  	public MetabolicAssayDataAnalyzer(MirkE mirke) throws MirkEApplicationException {
52  		setMirke(mirke);
53  		
54  		scheduleInitialization();
55  	}
56  	
57  	/***
58  	 * @throws MirkEApplicationException
59  	*/
60  	public void scheduleInitialization() throws MirkEApplicationException{
61  		javax.swing.SwingUtilities.invokeLater(new Runnable() {
62  			public void run(){
63  			    try{
64  			        createAndShow();
65  			    }catch (MirkEApplicationException e) {
66  			        
67  			    }
68  			}
69  		});
70  	}
71  	
72  	private JFrame metabolicAssayFrame;
73  	
74  	/***
75  	 * @throws MirkEApplicationException
76  	 */
77  	public void createAndShow() throws MirkEApplicationException {
78  		metabolicAssayFrame = new JFrame("MirkE - Metabolic assays"); // TODO - do we want a JFrame or a Dialog? (think that dialog might be better)
79  		
80  		JPanel mainPanel = new JPanel();
81  		
82  		metabolicAssayFrame.getContentPane().add(mainPanel);
83  		
84  		for (int i=0;i<10;i++) {
85  			mainPanel.add(new JLabel("" + i));
86  			
87  		}
88  		
89  		computeMetabolicPercentConverted();
90  		
91  		// display the window
92  		metabolicAssayFrame.pack();
93  		metabolicAssayFrame.show();
94  	}
95  	
96  	/***
97  	 * @throws MirkEApplicationException
98  	 */
99  	public void computeMetabolicPercentConverted() throws MirkEApplicationException {
100 		DataSet dataSet = getMirke().getDataSet();
101 		
102 		java.util.Set categoryNames =  dataSet.getCategoryNames();
103 		
104 		// TODO - categorize categories so we can automagically select only experimental / control / strain categories
105 		
106 		java.util.Set controlSet = new HashSet(categoryNames);
107 		
108 		java.util.Set concentrationCategoriesSet = new HashSet(categoryNames);
109 		concentrationCategoriesSet.remove("Drug Control");
110 		concentrationCategoriesSet.remove("Media Control");
111 		concentrationCategoriesSet.remove("Growth Control");
112 		
113 		java.util.Set strainCategoriesSet = new HashSet();
114 		
115 		{
116 			Iterator concentrationCategoriesSetIterator = concentrationCategoriesSet.iterator();
117 			while (concentrationCategoriesSetIterator.hasNext()) {
118 				String element = concentrationCategoriesSetIterator.next().toString();
119 				
120 				try {
121 					Double.parseDouble(element);
122 					// if element can be parsed into a double, assuming that it is a concentration (TODO - ¬°¬°¬°REALLY!!! need better categories so this guessing is unnecessary)
123 					
124 				} catch (Exception exception) {
125                     exception.printStackTrace();
126 					// element couldn't be parsed into a Double and isn't one of the three controls we're allowing - assume it's a strain  (TODO - ¬°¬°¬°REALLY!!! need better categories so this guessing is unnecessary)
127 					
128 					strainCategoriesSet.add(element);
129 					concentrationCategoriesSetIterator.remove();
130 				}
131 			}			
132 		}
133 		
134 		controlSet.removeAll(concentrationCategoriesSet);
135 		controlSet.removeAll(strainCategoriesSet);
136 		
137 		PrintStream dataFilePrintStream = System.out;
138 		
139 		{			
140 			Iterator strainCategoriesSetIterator = strainCategoriesSet.iterator();
141 			while (strainCategoriesSetIterator.hasNext()) {
142 				String speciesOrStrain = strainCategoriesSetIterator.next().toString();
143 				
144 				dataFilePrintStream.print(speciesOrStrain + "\t");
145 			}
146 		}
147 		
148 		{ // TODO - need an easy way to sort the categories in canonical order (perhaps using a TreeSet with a comparator that compares the equivilant Double values (a GUI to allow the user to manually rearrange the order would be nice too :-) )
149 			Iterator concentrationCategoriesSetIterator = concentrationCategoriesSet.iterator();
150 			while (concentrationCategoriesSetIterator.hasNext()) {
151 				String concentrationCategory = concentrationCategoriesSetIterator.next().toString();
152 				
153 				Iterator strainCategoriesSetIterator = strainCategoriesSet.iterator();
154 				while (strainCategoriesSetIterator.hasNext()) {
155 					String speciesOrStrain = strainCategoriesSetIterator.next().toString();
156 					
157 					dataFilePrintStream.print(computeFractionConverted(concentrationCategory,
158 																	   speciesOrStrain));
159 					
160 					dataFilePrintStream.print("\t");
161 				}
162 				
163 				dataFilePrintStream.println();
164 			}
165 		}
166 	}
167 	
168 	/***
169 	 *
170 	 * @param categoryName the category being measured (typically a dilution like 1:256; 1:512; etc represented by the denominator)
171 	 * @param strain the strain (or species/etc) being measured
172 	 *
173 	 * @return fractionConverted percentConverted = 100% * fractionConverted - amount of indicator converted by metabolism (relative to the growth control, if present; absolute, otherwise)
174 	 * 
175 	 * Developers - this method should be overridden in subclasses (TODO - really need a better design and implementation for this)
176 	 * @throws MirkEApplicationException
177 	 */
178 	double computeFractionConverted(String categoryName,
179 	        String strain) 
180 	throws MirkEApplicationException {
181 	    double fractionConverted = Double.NaN;
182 	    MeasuredValue fractionConvertedMeasuredValue = null;
183 	    
184 	    java.util.Set observables = getMirke().getDataSet().getObservables();
185 	    
186 	    // if there is only one observable and we assume that it is linearly related to concentration, then there is no need for considering molar extinction coefficients / anything so fancy (can do the calculations directly here)
187 	    if (observables.size() == 1) {
188 			Iterator observablesIterator = observables.iterator();
189 			String observableName = observablesIterator.next().toString();
190 			
191 			// select the measuredValue (and associated error(s)) for (this categoryName and strain and not the controls - mediaControl for this categoryName) then divid that by (the growth control - media control for no categoryName)
192 			java.util.List strainMeasuredValue = getMirke().getDataSet().find("select avg(p.observedValue) from PlateWellCategory c, PlateWellCategory strainCategory, PlateWellDataPoint p " +
193 																			  "where c.categoryName='"+categoryName+"' and p.plateRow= c.plateRow and " +
194 																			  "p.plateColumn = c.plateColumn and p.plateIdentifier=c.plateIdentifier and c.plateIdentifier = strainCategory.plateIdentifier and " +
195 																			  "strainCategory.categoryName='"+strain+"' and strainCategory.plateColumn = c.plateColumn and " +
196 																			  "strainCategory.plateRow = c.plateRow and p.observableName = '"+observableName+"'"); 
197 			
198 			double measuredValue = 0;
199 			if (strainMeasuredValue.size()==1) {
200 				measuredValue = ((Double)strainMeasuredValue.get(0)).doubleValue();	
201 				java.util.List strainMeasuredValueList = getMirke().getDataSet().find("select p.observedValue from PlateWellCategory c, PlateWellCategory strainCategory, PlateWellDataPoint p " +
202 																					  "where c.categoryName='"+categoryName+"' and p.plateRow= c.plateRow and " +
203 																					  "p.plateColumn = c.plateColumn and p.plateIdentifier=c.plateIdentifier and c.plateIdentifier = strainCategory.plateIdentifier and " +
204 																					  "strainCategory.categoryName='"+strain+"' and strainCategory.plateColumn = c.plateColumn and " +
205 																					  "strainCategory.plateRow = c.plateRow and p.observableName = '"+observableName+"'"); 
206 				
207 				double strainStandardDeviation = stdevFromListAndMean(strainMeasuredValueList, measuredValue);
208 				
209 				MeasuredValue strainMeasuredValueObject = new MeasuredValue(measuredValue,
210 																			strainStandardDeviation,
211 																			Unit.createUnit(""));
212 				fractionConvertedMeasuredValue = strainMeasuredValueObject;
213 				
214 				/* concentrationCategoriesSet.remove("Drug Control");
215 				concentrationCategoriesSet.remove("Media Control");
216 				concentrationCategoriesSet.remove("Growth Control");*/
217 				
218 				// select the drugControlMeasuredBackground (and associated error(s)) for (this categoryName and strain and not the controls - mediaControl for this categoryName) then divide that by (the growth control - media control for no categoryName)
219 				java.util.List drugControlMeasuredValue = getMirke().getDataSet().find("select avg(p.observedValue) from PlateWellCategory c, PlateWellDataPoint p " +
220 																					   "where c.categoryName='Drug Control' and p.plateRow= c.plateRow and " +
221 																					   "p.plateColumn = c.plateColumn and p.plateIdentifier=c.plateIdentifier and p.observableName = '"+observableName+"'"); 
222 				
223 				if (drugControlMeasuredValue.size() == 1) {
224 					if (drugControlMeasuredValue.get(0) != null) {
225 						double drugControlBackground = ((Double)drugControlMeasuredValue.get(0)).doubleValue();	
226 						measuredValue -= drugControlBackground;
227 						
228 						java.util.List drugControlMeasuredValueList = getMirke().getDataSet().find("select p.observedValue from PlateWellCategory c, PlateWellDataPoint p " +
229 																								   "where c.categoryName='Drug Control' and p.plateRow= c.plateRow and " +
230 																								   "p.plateColumn = c.plateColumn and p.plateIdentifier=c.plateIdentifier and p.observableName = '"+observableName+"'"); 
231 						
232 						double drugControlBackgroundStandardDeviation = stdevFromListAndMean(drugControlMeasuredValueList, drugControlBackground);
233 						
234 						MeasuredValue drugControlBackgroundMeasuredValue = new MeasuredValue(drugControlBackground,
235 																							 drugControlBackgroundStandardDeviation,
236 																							 Unit.createUnit(""));
237 						
238 						fractionConvertedMeasuredValue.subtract(drugControlBackgroundMeasuredValue);
239 						
240 					}
241 				}
242 				
243 				// select the growthControl (and associated error(s)) for (this categoryName and strain and not the controls - mediaControl for this categoryName) then divide that by (the growth control - media control for no categoryName)
244 				java.util.List growthControlAverage = getMirke().getDataSet().find("select avg(p.observedValue) from PlateWellCategory c, PlateWellCategory strainCategory, PlateWellDataPoint p " +
245 																				   "where c.categoryName='Growth Control' and p.plateRow= c.plateRow and " +
246 																				   "p.plateColumn = c.plateColumn and p.plateIdentifier=c.plateIdentifier and c.plateIdentifier = strainCategory.plateIdentifier and " +
247 																				   "strainCategory.categoryName='"+strain+"' and strainCategory.plateColumn = c.plateColumn and " +
248 																				   "strainCategory.plateRow = c.plateRow and p.observableName = '"+observableName+"'"); 
249 				
250 				double growthControl = 1d;
251 				double growthControlStandardDeviation = 0d;
252 				
253 				if (growthControlAverage.size() == 1) {
254 					if (growthControlAverage.get(0) != null) {
255 						growthControl = ((Double)growthControlAverage.get(0)).doubleValue();	
256 						
257 						java.util.List growthControlList = getMirke().getDataSet().find("select p.observedValue from PlateWellCategory c, PlateWellCategory strainCategory, PlateWellDataPoint p " +
258 																						"where c.categoryName='Growth Control' and p.plateRow= c.plateRow and " +
259 																						"p.plateColumn = c.plateColumn and p.plateIdentifier=c.plateIdentifier and c.plateIdentifier = strainCategory.plateIdentifier and " +
260 																						"strainCategory.categoryName='"+strain+"' and strainCategory.plateColumn = c.plateColumn and " +
261 																						"strainCategory.plateRow = c.plateRow and p.observableName = '"+observableName+"'");
262 						
263 						growthControlStandardDeviation = stdevFromListAndMean(growthControlList, growthControl);
264 					}
265 				} 
266 				
267 				MeasuredValue growthControlMeasuredValue = new MeasuredValue(growthControl, 
268 																			 growthControlStandardDeviation,
269 																			 Unit.createUnit(""));
270 				
271 				
272 				// select the mediaControlMeasuredValueList (and associated error(s)) for (this categoryName and strain and not the controls - mediaControl for this categoryName) then divid that by (the growth control - media control for no categoryName)
273 				java.util.List mediaControlMeanValueList = getMirke().getDataSet().find("select avg(p.observedValue) from PlateWellCategory c, PlateWellDataPoint p where " +
274 																						"c.categoryName='Media Control' and p.plateColumn=c.plateColumn and p.plateRow=c.plateRow and " +
275 																						"p.observableName = '"+observableName+"'"); 
276 				
277 				if (mediaControlMeanValueList.size() == 1) {
278 					if (mediaControlMeanValueList.get(0) != null) {
279 						double mediaControlBackground = ((Double)mediaControlMeanValueList.get(0)).doubleValue();	
280 						java.util.List mediaControlMeasuredValueList = getMirke().getDataSet().find("select p.observedValue from PlateWellCategory c, PlateWellDataPoint p where " +
281 																									"c.categoryName='Media Control' and p.plateColumn=c.plateColumn and p.plateRow=c.plateRow and " +
282 																									"p.observableName = '"+observableName+"'"); 
283 						
284 						double mediaControlBackgroundStandardDeviation = stdevFromListAndMean(mediaControlMeasuredValueList, mediaControlBackground);
285 						
286 						MeasuredValue mediaControlMeasuredValue = new MeasuredValue(mediaControlBackground,
287 																					mediaControlBackgroundStandardDeviation,
288 																					Unit.createUnit(""));
289 						
290 						growthControl -= mediaControlBackground;
291 						
292 						growthControlMeasuredValue.subtract(mediaControlMeasuredValue);
293 					}
294 				}
295 				
296 				fractionConvertedMeasuredValue.divide(growthControlMeasuredValue);
297 				fractionConverted =  measuredValue / growthControl;
298 				System.out.println(fractionConvertedMeasuredValue + "\t" + fractionConverted);
299 				
300 			}
301 		}
302 		
303 		return fractionConverted;
304 	}
305 	
306 }