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 1000000 -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
@@ -1,268 +1,304 @@
/********************************************************************************/
/* Copyright 2007-2009 -- The Regents of the University of California */
/********************************************************************************/
#include <limits.h>
#include "common.h"
#include "bed.h"
#include "cart.h"
#include "customTrack.h"
#include "errCatch.h"
#include "genoLay.h"
#include "trackLayout.h"
#include "hash.h"
#include "hdb.h"
#include "hgColors.h"
#include "hPrint.h"
#include "htmshell.h"
#include "jsHelper.h"
#include "psGfx.h"
#include "trashDir.h"
#include "vGfx.h"
#include "hvGfx.h"
#include "web.h"
#include "hgHeatmapLib.h"
#include "heatmapUtility.h"
#include "featuresLib.h"
#include "hgStatsLib.h"
#include "filterFeatures.h"
#include "hgGenesets.h"
#define EXPR_DATA_SHADES 16
#define DEFAULT_MAX_DEVIATION 0.7
#define COLOR_SCALE 1.0
/***** BEGIN HELPER *****/
void vgMakeColorGradient(struct vGfx *vg,
struct rgbColor *start, struct rgbColor *end,
int steps, Color *colorIxs)
/* Make a color gradient that goes smoothly from start
* to end colors in given number of steps. Put indicesgl->chromLis
* in color table in colorIxs */
{
double scale = 0, invScale;
double invStep;
int i; int r,g,b;
steps -= 1; /* Easier to do the calculation in an inclusive way. */
invStep = 1.0/steps;
for (i=0; i<=steps; ++i)
{
invScale = 1.0 - scale;
r = invScale * start->r + scale * end->r;
g = invScale * start->g + scale * end->g;
b = invScale * start->b + scale * end->b;
colorIxs[i] = vgFindColorIx(vg, r, g, b);
scale += invStep;
}
}
char *cartSettingsString(char *prefix, char *functionName)
{
if (!prefix || !functionName)
return NULL;
struct hashEl *hEl = cartFindPrefix(cart, prefix);
if (!hEl)
return NULL;
struct dyString *dy = newDyString(1000);
while (hEl)
{
char *name = hEl->name;
char *val = hEl->val;
dyStringPrintf(dy, "%s=%s,", name, val);
hEl = hEl->next;
}
dyStringPrintf(dy, "%s,%s", functionName, VERSION);
char *str = dyStringCannibalize(&dy);
return str;
}
/***** END HELPER *****/
void drawRawData(struct vGfx *vg, struct rawData *rdList,
struct mapSettings *settings)
{
double gain = settings->gain;
double colorScale = COLOR_SCALE / settings->max_deviation;
double val;
double absVal;
Color valCol;
Color upShades[EXPR_DATA_SHADES];
Color downShades[EXPR_DATA_SHADES];
vgMakeColorGradient(vg, settings->zero, settings->high, EXPR_DATA_SHADES, upShades);
vgMakeColorGradient(vg, settings->zero, settings->low, EXPR_DATA_SHADES, downShades);
Color missingColor = vgFindColorIx(vg, settings->missing->r,
settings->missing->g, settings->missing->b);
vgSetClip(vg, 0, 0, settings->width, settings->height);
vgBox(vg, 0, 0, settings->width, settings->height, missingColor);
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)
{
safef(id, sizeof (id), "%d", rd->feature_id);
i = hashIntValDefault(settings->x_index, id, -1);
prevFeatureId = rd->feature_id;
}
if (rd->sample_id != prevSampleId)
{
safef(id, sizeof (id), "%d", rd->sample_id);
j = hashIntValDefault(settings->y_index, id, -1);
prevSampleId = rd->sample_id;
}
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);
}
void mapSettingsDefaultColor(struct mapSettings *settings)
{
struct rgbColor *low = AllocA(struct rgbColor);
struct rgbColor *zero = AllocA(struct rgbColor);
struct rgbColor *high = AllocA(struct rgbColor);
struct rgbColor *missing = AllocA(struct rgbColor);
// Default "Patriotic" Red, White, Blue
zero->r = 255;
zero->g = 255;
zero->b = 255;
high->r = 255;
high->g = 0;
high->b = 0;
low->r = 0;
low->g = 0;
low->b = 255;
// Light Gray for missing vals
missing->r = 200;
missing->g = 200;
missing->b = 200;
// Set colors in structure
settings->low = low;
settings->zero = zero;
settings->high = high;
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);
struct hashEl *el;
struct slName *sl;
int numFeatures = 0;
for (sl = features; sl; sl = sl->next)
{
if ((el = hashLookup(settings->x_index, sl->name)) != NULL)
continue;
hashAddInt(settings->x_index, sl->name, numFeatures);
numFeatures += 1;
}
int numSamples = 0;
for (sl = samples; sl; sl = sl->next)
{
if ((el = hashLookup(settings->y_index, sl->name)) != NULL)
continue;
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;
}
char *heatmapGif(struct sqlConnection *conn, struct rawData *rdList,
struct mapSettings *settings)
/* Create genome GIF file and HT that includes it. */
{
if (!rdList || !settings)
return NULL;
if (settings->height * settings->width == 0)
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)
{
vg = hvGfxOpenGif(settings->width, settings->height, md5Tn.forCgi, FALSE);
drawRawData(vg->vg, rdList, settings);
hvGfxClose(&vg);
}
char *filename = replaceChars(md5Tn.forHtml, "..", "");
return filename;
}