de438b6041d00b72be32bcdec70436a9d201baf2
braney
  Sat May 21 12:17:40 2016 -0700
libify the guts of ave so I can use it in hgc

diff --git src/hg/hgc/hgc.c src/hg/hgc/hgc.c
index a96ecbb..7d3c019 100644
--- src/hg/hgc/hgc.c
+++ src/hg/hgc/hgc.c
@@ -241,30 +241,31 @@
 #include "jsHelper.h"
 #include "virusClick.h"
 #include "gwasCatalog.h"
 #include "parClick.h"
 #include "mdb.h"
 #include "yaleGencodeAssoc.h"
 #include "itemDetailsHtml.h"
 #include "trackVersion.h"
 #include "numtsClick.h"
 #include "geneReviewsClick.h"
 #include "bigBed.h"
 #include "bigPsl.h"
 #include "bedTabix.h"
 #include "longRange.h"
 #include "hmmstats.h"
+#include "aveStats.h"
 
 static char *rootDir = "hgcData";
 
 #define LINESIZE 70  /* size of lines in comp seq feature */
 
 struct cart *cart;	/* User's settings. */
 char *seqName;		/* Name of sequence we're working on. */
 int winStart, winEnd;   /* Bounds of sequence. */
 char *database;		/* Name of mySQL database. */
 char *organism;		/* Colloquial name of organism. */
 char *genome;		/* common name, e.g. Mouse, Human */
 char *scientificName;	/* Scientific name of organism. */
 
 struct hash *trackHash;	/* A hash of all tracks - trackDb valued */
 
@@ -3920,65 +3921,66 @@
     }
 }
 
 
 static void doLongTabix(struct trackDb *tdb, char *item)
 {
 char *bigDataUrl = hashFindVal(tdb->settingsHash, "bigDataUrl");
 struct bedTabixFile *btf = bedTabixFileMayOpen(bigDataUrl, NULL, 0, 0);
 char *chromName = cartString(cart, "c");
 struct bed *list = bedTabixReadBeds(btf, chromName, winStart, winEnd, bedLoad5);
 bedTabixFileClose(btf);
 unsigned maxWidth;
 struct longRange *longRangeList = parseLongTabix(list, &maxWidth, 0);
 struct longRange *longRange, *ourLongRange = NULL;
 unsigned itemNum = sqlUnsigned(item);
-double min = DBL_MAX, max = -DBL_MAX;
-double total = 0, average;
-unsigned count = 0;
-double sumSquares = 0;
+unsigned count = slCount(longRangeList);
+double *doubleArray;
 
-for(longRange = longRangeList; longRange; longRange = longRange->next)
+AllocArray(doubleArray, count);
+
+int ii = 0;
+for(longRange = longRangeList; longRange; longRange = longRange->next, ii++)
     {
     if (longRange->id == itemNum)
         {
         ourLongRange = longRange;
         }
-    if (longRange->score < min)
-        min = longRange->score;
-    if (longRange->score > max)
-        max = longRange->score;
-    total += longRange->score;
-    sumSquares += longRange->score * longRange->score;
-
-    count++;
+    doubleArray[ii] = longRange->score;
     }
-average = total/count;
-double stdDev = calcStdFromSums(total, sumSquares, count);
-
 
 if (ourLongRange == NULL)
     errAbort("cannot find long range item with id %d\n", itemNum);
 
+struct aveStats *as = aveStatsCalc(doubleArray, count);
+
 printf("Item you clicked on:<BR>\n");
 printf("<B>Score:</B> %g<BR>\n", ourLongRange->score);
 printf("<B>ID:</B> %u<BR>\n", ourLongRange->id);
 //printf("<A HREF=\"hgTracks?position=%s:%d-%d\" TARGET=_BLANK><B>Link to other block </A><BR>\n",  otherChrom, s, e);
 
 printf("<BR>All items in window:\n");
-printf("<BR><B>Min:</B> %g <BR><B>Max:</B> %g <BR><B>Average:</B> %g <BR><B>Count:</B> %d <BR><B>Total:</B> %g  <BR><B>StDev:</B> %g\n", min,max,average,count, total, stdDev);
 
+printf("<BR>Q1 %f\n", as->q1);
+printf("<BR>median %f\n", as->median);
+printf("<BR>Q3 %f\n", as->q3);
+printf("<BR>average %f\n", as->average);
+printf("<BR>min %f\n", as->minVal);
+printf("<BR>max %f\n", as->maxVal);
+printf("<BR>count %d\n", as->count);
+printf("<BR>total %f\n", as->total);
+printf("<BR>standard deviation %f\n", as->stdDev);
 }
 
 void genericClickHandlerPlus(
         struct trackDb *tdb, char *item, char *itemForUrl, char *plus)
 /* Put up generic track info, with additional text appended after item. */
 {
 char *dupe, *type, *words[16], *headerItem;
 int wordCount;
 int start = cartInt(cart, "o");
 int end = cartInt(cart, "t");
 struct sqlConnection *conn = NULL;
 char *imagePath = trackDbSetting(tdb, ITEM_IMAGE_PATH);
 char *container = trackDbSetting(tdb, "container");
 
 if (!trackHubDatabase(database))