src/hg/instinct/hgHeatmap2/drawingCode.c 1.67

1.67 2009/06/22 21:35:49 jzhu
add feature value output to file, fix bug in feature summary view
Index: src/hg/instinct/hgHeatmap2/drawingCode.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/hgHeatmap2/drawingCode.c,v
retrieving revision 1.66
retrieving revision 1.67
diff -b -B -U 4 -r1.66 -r1.67
--- src/hg/instinct/hgHeatmap2/drawingCode.c	4 Jun 2009 03:50:36 -0000	1.66
+++ src/hg/instinct/hgHeatmap2/drawingCode.c	22 Jun 2009 21:35:49 -0000	1.67
@@ -1212,14 +1212,38 @@
 
 int sampleHeight = hl->sampleHeight;
 int height = heatmapHeight(gh) * sampleHeight;
 
-struct hmElement *hEl;
+int nCols=0; //number of features 
+int nSamples=0; //number of samples
+struct hmElement *hEl=NULL;
+for (hEl = hl->elements; hEl; hEl = hEl->next)
+    nCols++;
+struct slName *sl = NULL;
+for (sl = gh->sampleList; sl ; sl = sl->next)
+    nSamples++;
+char ***featureArray= NULL; //an array with dim(nSamples , nCols+2)
+
+boolean output = FALSE; //TRUE; /* variable used to control generate numerical output of the features */
+if (output)
+    {
+    featureArray=  (char***)(malloc(sizeof (char**)*nSamples));
+    int i,j;
+    for (i=0; i<nSamples; i++)
+	{
+	featureArray[i]=(char**)(malloc(sizeof (char*)*(nCols+2)));
+	for (j=0; j<nCols+2; j++)
+	    featureArray[i][j]=NULL;
+	}
+    }
+nCols=1;
+
 for (hEl = hl->elements; hEl; hEl = hEl->next)
     {
     int chromX = hEl->pixelStart, chromY = 0;
     int width = hEl->pixelEnd - hEl->pixelStart;
     int h = sampleHeight;
+    nCols++;
 
     for (col = colList; col != NULL; col = col->next)
 	if (sameString(col->name, hEl->name))
 	    break;
@@ -1280,9 +1304,9 @@
     else    /* single color */
 	colorScale = COLOR_SCALE / (maxVal - minVal);
     
     struct slName *sl = NULL;
-
+    nSamples=0;
     for (sl = gh->sampleList; sl ; sl = sl->next)
 	{
 	int orderId = hashIntValDefault(gh->sampleOrder, sl->name, -1);
 	
@@ -1290,8 +1314,27 @@
 	    continue;
 	id = slNameNew(getId(conn, labTable, key, sl->name, value));
 
 	char *cellVal = col->cellVal(col, id, conn);
+	if (output)
+	    {
+	    if (cellVal)
+		{
+		featureArray[nSamples][nCols]=(char*)(malloc(sizeof(int)*(strlen(cellVal)+1)));
+		strcpy(featureArray[nSamples][nCols],cellVal);
+		featureArray[nSamples][nCols][strlen(cellVal)]='\0';
+		}
+	    if (nCols==2)
+		{
+		featureArray[nSamples][0]=(char*)(malloc(sizeof(int)*8));
+		sprintf(featureArray[nSamples][0],"%d", orderId);
+
+		featureArray[nSamples][1]=(char*)(malloc(sizeof(int)*(strlen(sl->name)+1)));
+		strcpy(featureArray[nSamples][1],sl->name);
+		featureArray[nSamples][1][strlen(sl->name)]='\0';
+		}
+	    nSamples++;
+	    }
 		
 	if (!cellVal)
 	    continue;
 
@@ -1333,8 +1376,36 @@
 	}
     vgUnclip(vg);
     }
 
+/* write out feartureArray to output file, can be used for feature download function */
+if (output)
+    {
+    int i,j;
+    // the feature value output file name in tmp directory , consider change the file name
+    FILE* fout=fopen("/tmp/featureOutput.txt","w");
+    for (i=0; i<nSamples; i++)
+	{
+	for (j=0; j<nCols; j++)
+	    {
+	    if (featureArray[i][j])
+		fprintf(fout,"%s",featureArray[i][j]);
+	    fprintf(fout,"\t");
+	    }
+	fprintf(fout,"\n");
+	}
+    fclose(fout);
+    }
+
+/* clean up memory of featureArray */
+if (output) 
+    {
+    int i;
+    for (i=0; i<nSamples; i++)
+	free(featureArray[i]);
+    free(featureArray);
+    }
+
 hFreeConn(&conn);
 }
 
 int hmPixelCmpVal(const void *va, const void *vb)
@@ -1568,15 +1638,15 @@
 	}
 
     /* Sort column by value */
     slSort(&hmList, hmPixelCmpVal);
+    float yOff = 0;    
 
-    int yOff = 0;    
     /* Draw sorted data */
     for (hm = hmList; hm; hm = hm->next)
 	{
-	vgBox(vg, hm->x, yOff, hm->w, hm->h, hm->color);
-	yOff += ceil(h);
+	vgBox(vg, hm->x, ceil(yOff), hm->w, hm->h, hm->color);
+	yOff +=h;
 	}
     vgUnclip(vg);
     slFreeList(&hmList);
     }