src/lib/common.c 1.144

1.144 2010/03/10 19:44:59 kent
Adding doubleBoxWhiskerCalc to library.
Index: src/lib/common.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/common.c,v
retrieving revision 1.143
retrieving revision 1.144
diff -b -B -U 4 -r1.143 -r1.144
--- src/lib/common.c	4 Jan 2010 19:12:41 -0000	1.143
+++ src/lib/common.c	10 Mar 2010 19:44:59 -0000	1.144
@@ -456,8 +456,27 @@
     }
 return median;
 }
 
+void doubleBoxWhiskerCalc(int count, double *array, double *retMin, 
+	double *retQ1, double *retMedian, double *retQ3, double *retMax)
+/* Calculate what you need to draw a box and whiskers plot from an array of doubles. */
+{
+double median;
+doubleSort(count, array);
+*retMin = array[0];
+*retQ1 = array[(count+2)/4];
+int halfCount = count>>1;
+if ((count&1) == 1)
+    *retMedian = array[halfCount];
+else
+    {
+    *retMedian = (array[halfCount] + array[halfCount-1]) * 0.5;
+    }
+*retQ3 = array[(3*count+2)/4];
+*retMax = array[count-1];
+}
+
 struct slDouble *slDoubleNew(double x)
 /* Return a new double. */
 {
 struct slDouble *a;
@@ -495,8 +514,24 @@
 freeMem(array);
 return median;
 }
 
+void slDoubleBoxWhiskerCalc(struct slDouble *list, double *retMin, 
+	double *retQ1, double *retMedian, double *retQ3, double *retMax)
+/* Calculate what you need to draw a box and whiskers plot from a list of slDoubles. */
+{
+int i,count = slCount(list);
+struct slDouble *el;
+double *array, minVal, q1, median, q3, maxVal;
+if (count == 0)
+    errAbort("Can't take do slDoubleBoxWhiskerCalc of empty list");
+AllocArray(array,count);
+for (i=0, el=list; i<count; ++i, el=el->next)
+    array[i] = el->val;
+doubleBoxWhiskerCalc(count, array, retMin, retQ1, retMedian, retQ3, retMax);
+freeMem(array);
+}
+
 static int intCmp(const void *va, const void *vb)
 /* Compare function to sort array of ints. */
 {
 const int *a = va;