src/hg/instinct/hgGeneset/drawingCode.c 1.2

1.2 2010/01/22 05:11:28 jsanborn
updated drawing code
Index: src/hg/instinct/hgGeneset/drawingCode.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/hgGeneset/drawingCode.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -b -B -U 4 -r1.1 -r1.2
--- src/hg/instinct/hgGeneset/drawingCode.c	21 Jan 2010 23:51:13 -0000	1.1
+++ src/hg/instinct/hgGeneset/drawingCode.c	22 Jan 2010 05:11:28 -0000	1.2
@@ -108,28 +108,25 @@
 int i = -1, j = -1;
 int prevSampleId  = -1;
 int prevFeatureId = -1;
 char id[128];
+char pixelStr[128];
 
+struct hmPixel *hm, *hmList = NULL;
+struct hashEl *el;
+struct hash *pixelHash = hashNew(0);
+
+int w = round(settings->x_scale);
+if (w < 1)
+    w = 1;
+int h = round(settings->y_scale);
+if (h < 1)
+    h = 1;
 
 struct rawData *rd;
 for (rd = rdList; rd; rd = rd->next)
     {
     val    = rd->val;    
-    absVal = fabs(val) * gain;
-
-    int colorIndex = (int)(absVal * (EXPR_DATA_SHADES-1.0) * colorScale);
-    /* Clip color index to fit inside of array, since we may have brightened it. */
-    if (colorIndex < 0)
-	colorIndex = 0;
-    if (colorIndex >= EXPR_DATA_SHADES)
-	colorIndex = EXPR_DATA_SHADES-1;
-    
-    if(val >= 0)
-	valCol = upShades[colorIndex];
-    else
-	valCol = downShades[colorIndex];
-    
     // Attempt to reduce number of hash calls by checking if we *just* searched 
     // for sample or feature id. 
     if (rd->feature_id != prevFeatureId)
 	{
@@ -147,12 +144,51 @@
 
     if (i < 0 || j < 0)
 	continue;
 
-    int x = i * settings->x_scale;
-    int y = j * settings->y_scale;
+    int x1 = round((double) i * settings->x_scale);
+    int y1 = round((double) j * settings->y_scale);
+    int x2 = round((double) (i + 1) * settings->x_scale);
+    int y2 = round((double) (j + 1) * settings->y_scale);
+    
+    safef(pixelStr, sizeof(pixelStr), "%d,%d", x1, y1);
+    if ((el = hashLookup(pixelHash, pixelStr)) == NULL)
+	{
+	hm = AllocA(struct hmPixel);
+	hm->x = x1;
+	hm->y = y1;
+	hm->w = ((x2 - x1) > 0) ? (x2 - x1) : 1;
+	hm->h = ((y2 - y1) > 0) ? (y2 - y1) : 1;
+	hm->val = 0.0;
+	hm->count = 0;
+	slAddHead(&hmList, hm);
+	hashAdd(pixelHash, pixelStr, hm);
+	}
+    else
+	hm = el->val;
+
+    hm->val += val;
+    hm->count += 1.0;
+    }
+
+for (hm = hmList; hm ; hm = hm->next)
+    {
+    val = hm->val / (double) hm->count;
+    absVal = fabs(val) * gain;
+
+    int colorIndex = (int)(absVal * (EXPR_DATA_SHADES-1.0) * colorScale);
+    /* Clip color index to fit inside of array, since we may have brightened it. */
+    if (colorIndex < 0)
+	colorIndex = 0;
+    if (colorIndex >= EXPR_DATA_SHADES)
+	colorIndex = EXPR_DATA_SHADES-1;
+    
+    if(val >= 0)
+	valCol = upShades[colorIndex];
+    else
+	valCol = downShades[colorIndex];
 	
-    vgBox(vg, x, y, settings->x_scale, settings->y_scale, valCol);
+    vgBox(vg, hm->x, hm->y, hm->w, hm->h, valCol);
     }
     
 vgUnclip(vg);
 }
@@ -189,21 +225,21 @@
 settings->missing = missing;
 }
 
 struct mapSettings *initMapSettings(struct slName *samples, struct slName *features, 
-				    int x_scale, int y_scale)
+				    int width, int height)
 {
 if (!samples || !features)
     return NULL;
 
 struct mapSettings *settings = AllocA(struct mapSettings);
-settings->x_scale = x_scale;
-settings->y_scale = y_scale;
+settings->width  = width;
+settings->height = height;
 
 mapSettingsDefaultColor(settings);
     
 settings->max_deviation = DEFAULT_MAX_DEVIATION;
-settings->gain = 10.0;
+settings->gain = 1.0;
 
 settings->x_index = hashNew(0);
 settings->y_index = hashNew(0);
 
@@ -226,10 +262,10 @@
     hashAddInt(settings->y_index, sl->name, numSamples);    
     numSamples += 1;
     }
 	 
-settings->width  = numFeatures * settings->x_scale;
-settings->height = numSamples * settings->y_scale;
+settings->x_scale = (double) width / (double) numFeatures;
+settings->y_scale = (double) height / (double) numSamples;
 
 return settings;
 }
 
@@ -244,10 +280,10 @@
     return NULL;
 
 struct hvGfx *vg;
 struct tempName md5Tn;
-char *strToHash = cartSettingsString(hgh3Prefix, "heatmapGif");
-trashDirMD5File(&md5Tn, "hgh3", ".gif", strToHash);
+char *strToHash = cartSettingsString(hghPrefix, "heatmapGif");
+trashDirMD5File(&md5Tn, "hgg", ".gif", strToHash);
 
 off_t size = fileSize(md5Tn.forCgi);
 if (!fileExists(md5Tn.forCgi) || (size == 0) || DEBUG)
     {