View Javadoc
1   /*** GrowthCurveDataAnalyzer.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 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.MirkE; // TODO - refactor cyclic dependency 
26  import edu.asu.cri.MirkE.dataStructures.*;
27  import edu.asu.cri.MirkE.exceptions.MirkEApplicationException;
28  import java.io.*;
29  import java.util.*;
30  
31  /***
32  */
33  public class GrowthCurveDataAnalyzer extends edu.asu.cri.MirkE.util.DataAnalyzer {
34  	
35  	/*** parameter free constructor
36  		*/
37  	public GrowthCurveDataAnalyzer() {
38  		
39  	}
40  	
41  	/***
42  		@param mirke
43  	 */
44  	public GrowthCurveDataAnalyzer(MirkE mirke) {
45  		setMirke(mirke);
46  		
47  	}
48  	
49  	/***
50  	 * this method needs *a lot* of work (and really should be made internal / pure Java)
51  	 * @throws MirkEApplicationException
52  	 * @throws IOException
53  	 */
54  	public void computeGrowthCurves() throws MirkEApplicationException, IOException{		
55  		String dataFileName = "/tmp/MirkEgrowthCurveData." + System.currentTimeMillis();
56  		String gnuplotFileName = "/tmp/MirkEgrowthCurveCommands." + System.currentTimeMillis();
57  		
58  		PrintStream dataFilePrintStream;
59  		
60  		dataFilePrintStream = new PrintStream(new FileOutputStream(dataFileName)); // TODO - allow user to specify the file or keep in memory (this will degrade poorly on non-Unix machines)
61  		
62  		/* TODO - 
63  		plot curves for each category (need to work out a new category class / persister )
64  		*/
65  		DataSet dataSet = getMirke().getDataSet();
66  		
67  		java.util.Set categoryNames = dataSet.getCategoryNames();
68  				
69  		java.util.Set observables = dataSet.getObservables();
70  		java.util.Set plateIdentifiers = dataSet.getPlateIdentifiers();
71  		
72  		{ // print the header row of the table
73  			dataFilePrintStream.print("time (h)\t");
74  			
75  			Iterator categoryNameIterator = categoryNames.iterator();
76  			while (categoryNameIterator.hasNext()) {
77  				String categoryName = categoryNameIterator.next().toString();
78  				
79  				Iterator observableIterator = observables.iterator();
80  				while (observableIterator.hasNext()) {
81  					String observableName = observableIterator.next().toString();
82  					
83  					dataFilePrintStream.print(categoryName+ "." + observableName + "\t");
84  					dataFilePrintStream.print("stdev(" + categoryName+ "." + observableName + ")\t");
85  
86  				}
87  			}
88  		}
89  		
90  		dataFilePrintStream.println();
91  		
92  		long timeZero = 0;
93  		
94  		java.util.List minimumPlateTimestamps = dataSet.find("select min(o.timestamp) from PlateDescription as o");
95  		if (minimumPlateTimestamps.size() == 1) {
96  			java.sql.Timestamp timestamp = (java.sql.Timestamp)minimumPlateTimestamps.get(0);
97  			timeZero = timestamp.getTime();
98  			
99  		} else {
100 			timeZero = 0;
101 		}
102 		
103 		Iterator plateIdentifierIterator = plateIdentifiers.iterator();
104 		while (plateIdentifierIterator.hasNext()) {
105 			String plateIdentifier = plateIdentifierIterator.next().toString();
106 			
107 			java.util.List plateTimestamps = dataSet.find("select o.timestamp from PlateDescription as o where o.plateIdentifier='"+plateIdentifier+"' ");
108 
109 			if (plateTimestamps.size() == 1) {
110 				java.sql.Timestamp timestamp = (java.sql.Timestamp)plateTimestamps.get(0);
111 				
112 				dataFilePrintStream.print(((double)(timestamp.getTime() - timeZero))/(1000*60*60) + "\t" ); // display time in hours
113 				
114 			} else {
115 				dataFilePrintStream.println(plateIdentifier + "\t");
116 				
117 			}
118 												
119 			//String queryPrefix = "from PlateWellDataPoint as o where o.plateIdentifier='" + plateIdentifier + "' ";
120 
121 			Iterator categoryNameIterator = categoryNames.iterator();
122 			while (categoryNameIterator.hasNext()) {
123 				String categoryName = categoryNameIterator.next().toString();				
124 				
125 				Iterator observableIterator = observables.iterator();
126 				while (observableIterator.hasNext()) {
127 					String observableName = observableIterator.next().toString();
128 
129 					// TODO - find PlateWellDataPoints where plateRow = o.plateRow and plateColumn = o.plateColumn
130 										
131 					java.util.List plateWellDataPointMeanValueList = dataSet.find("select avg(p.observedValue) from PlateWellCategory c, PlateWellDataPoint p where c.categoryName='"+categoryName+"' and p.plateRow= c.plateRow and " +
132 																		 "p.plateColumn = c.plateColumn and p.plateIdentifier='"+plateIdentifier+"' and " +
133 																		 "p.observableName = '"+observableName+"'"); 
134 					
135 					if (plateWellDataPointMeanValueList.size() > 0) {	
136 						double meanValue = ((Double)plateWellDataPointMeanValueList.get(0)).doubleValue();
137 						dataFilePrintStream.print(meanValue + "\t");
138 						
139 						java.util.List plateWellDataPointList = dataSet.find("select p.observedValue from PlateWellCategory c, PlateWellDataPoint p where c.categoryName='"+categoryName+"' and p.plateRow= c.plateRow and " +
140 																						  "p.plateColumn = c.plateColumn and p.plateIdentifier='"+plateIdentifier+"' and " +
141 																						  "p.observableName = '"+observableName+"'"); 
142 						
143 						double standardDeviation = stdevFromListAndMean(plateWellDataPointList, meanValue);
144 						dataFilePrintStream.print(standardDeviation + "\t");
145 						
146 						
147 						
148 					} else {
149 						dataFilePrintStream.print("\t\t");
150 
151 					}
152 				}
153 			}
154 			
155 			dataFilePrintStream.println();
156 		}
157 		
158 		dataFilePrintStream.close();
159 
160 			
161 			// TODO - would be nice to handle the graphing internally; using gnuplot for now
162 			PrintStream gnuplotCommandPrintStream = new PrintStream(new FileOutputStream(gnuplotFileName)); // TODO - allow user to specify the file or keep in memory (this will degrade poorly on non-Unix machines)
163 
164 			// TODO - add a simple GUI to set axes, tics, titles and labels
165 			// TODO - need a simple way of combining growth curves from multiple plates / experiments into a single graph (point to multiple MirkE instances and do queries against each of their DataSets?)
166 			
167 			gnuplotCommandPrintStream.println("set xlabel 'time (h)'");
168 			
169 			String term = "aqua";
170 			String graphTitle = "";
171 			String[] ylabel = {"",""};
172 			//String xlabel = "time (h)";
173 			
174 			if ((observables.size() > 0) && (observables.size() < 3)) {
175 				int yaxis = 0;
176 				Iterator observablesIterator = observables.iterator();
177 				while (observablesIterator.hasNext()){
178 					ylabel[yaxis] = observablesIterator.next().toString();
179 					yaxis ++;
180 				}
181 				
182 			} else {
183 				gnuplotCommandPrintStream.println("set ylabel ''");
184 
185 			}
186 			
187 			gnuplotCommandPrintStream.println("set term "+term+"");
188 			gnuplotCommandPrintStream.println("set ylabel '"+ylabel[0]+"'");
189 			gnuplotCommandPrintStream.println("set y2label '"+ylabel[1]+"'");
190 			gnuplotCommandPrintStream.println("set title '"+graphTitle+"'");
191 			gnuplotCommandPrintStream.println("set key top left");
192 			
193 			gnuplotCommandPrintStream.print("plot ");
194 			
195 			int index = 0;
196 			Iterator categoryNameIterator = categoryNames.iterator();
197 			while (categoryNameIterator.hasNext()) {
198 				String categoryName = categoryNameIterator.next().toString();
199 				
200 				Iterator observableIterator = observables.iterator();
201 				while (observableIterator.hasNext()) {					
202 					//String observable = observableIterator.next().toString();
203 					
204 					gnuplotCommandPrintStream.print("'"+dataFileName+"' u 1:"+(2*(index+1))+":"+(2*(index+1) + 1)+" title '"+categoryName + "' with errorlines ");
205 					if (observableIterator.hasNext() || categoryNameIterator.hasNext()) {
206 						gnuplotCommandPrintStream.print(", ");
207 					}
208 					
209 					index ++;
210 				}
211 			}
212 			
213 			gnuplotCommandPrintStream.close();
214 			
215 			java.lang.Runtime.getRuntime().exec("/sw/bin/gnuplot " + gnuplotFileName);
216 			
217 	
218 	}
219 }