3d9234714410b7bb76656c92b6da41429d49b48a
ceisenhart
  Mon Feb 29 10:06:31 2016 -0800
Fixing a bug with the top 10 genes feature, refs #16341

diff --git src/hg/expMatrixToJson/expMatrixToJson.c src/hg/expMatrixToJson/expMatrixToJson.c
index bf6a925..19b3938 100644
--- src/hg/expMatrixToJson/expMatrixToJson.c
+++ src/hg/expMatrixToJson/expMatrixToJson.c
@@ -337,52 +337,61 @@
     verbose(3,"Merging...\n");
     const struct bioExpVector *kid1 = (const struct bioExpVector *)item1;
     const struct bioExpVector *kid2 = (const struct bioExpVector *)item2;
     float kid1Weight = kid1->children / (float)(kid1->children + kid2->children);
     float kid2Weight = kid2->children / (float)(kid1->children + kid2->children);
     struct bioExpVector *el;
     AllocVar(el);
     AllocArray(el->vector, kid1->count);
     assert(kid1->count == kid2->count);
     el->count = kid1->count; 
     el->name = catTwoStrings(kid1->name, kid2->name);
     int i;
     int gCount = 0;
     for (i = 0; i < el->count; ++i)
 	{
-	el->vector[i] = (kid1Weight*kid1->vector[i] + kid2Weight*kid2->vector[i]);
-	double diff = abs((kid1Weight*kid1->vector[i] - kid2Weight*kid2->vector[i]));
-			
-	if (diff > 0.0){
+	if (kid1->vector[i] == kid2->vector[i]) // Were doing thousands of merges, lets cut out useless compute where we can. 
+	    {
+	    el->vector[i] = kid1->vector[i];  
+	    continue;    
+	    }
+	el->vector[i] = (kid1Weight*kid1->vector[i] + kid2Weight*kid2->vector[i]); // Weight based on number of children. 
+	float diff; 
+	//if (((float)(kid1Weight*kid1->vector[i])) > ((float)(kid2Weight*kid2->vector[i]))) 
+	//    {diff = ((float)(kid1Weight*kid1->vector[i])) - ((float)(kid2Weight*kid2->vector[i]));}
+	//else {diff = ((float)(kid2Weight*kid2->vector[i])) - ((float)(kid1Weight*kid1->vector[i]));} 
+	if (((float)(kid1->vector[i])) > ((float)(kid2->vector[i]))) 
+	    {diff = ((float)(kid1->vector[i])) - ((float)(kid2->vector[i]));}
+	else {diff = ((float)(kid2->vector[i])) - ((float)(kid1->vector[i]));} 
 	++el->contGenes; 
 	++gCount;
 	int index = i + 1; 
 	if (gCount <= 10){
 	    struct slDoubleInt *newGene = slDoubleIntNew(diff, index); 
 	    slAddHead(&el->topGeneIndeces, newGene); 
 	    slSort(&el->topGeneIndeces, slDoubleIntCmp); 
 	    }
 	else{
 	    if (el->vector[i] > el->topGeneIndeces->val){
 		slPopHead(el->topGeneIndeces); 
 		struct slDoubleInt *newGene = slDoubleIntNew(diff, index); 
 		slAddHead(&el->topGeneIndeces, newGene); 
 		slSort(&el->topGeneIndeces, slDoubleIntCmp); 
 		}
 	    }
 	}
-	}
+    slReverse(&el->topGeneIndeces); 
     el->children = kid1->children + kid2->children;
     return (struct slList *)(el);
     }
 
 void colorLeaves(struct slRef *leafList)
 /* Assign colors of rainbow to leaves. */
     {
     float total = 0.0;
     //double purplePos = 0.80;
     struct slRef *el, *nextEl;
 
     /* Loop through list once to figure out total, since we need to normalize */
     for (el = leafList; el != NULL; el = nextEl)
 	{
 	nextEl = el->next;
@@ -477,31 +486,31 @@
     chopSuffix(pageName);
     int textSize = 12 - log(nodeCount);  
     //int radius = 540 + 270*log10(nodeCount);  
     int width = 10 * nodeCount; 
     int height = 10 * nodeCount; 
     int labelLength = 10+nameSize*(15-textSize);
     if (labelLength > 100) labelLength = 100;
 
     fprintf(outputFile,"<!DOCTYPE html>\n"); 
     fprintf(outputFile,"<head>\n"); 
     fprintf(outputFile,"<title>New dendrogram tests</title>\n"); 
     fprintf(outputFile,"<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css\">\n"); 
     fprintf(outputFile,"<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>\n"); 
     fprintf(outputFile,"<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js\"></script>\n"); 
     fprintf(outputFile,"<script src=\"http://d3js.org/d3.v3.min.js\" type=\"text/javascript\"></script>\n"); 
-    fprintf(outputFile,"<script src=\"d3.dendrograms.js\" type=\"text/javascript\"></script>\n"); 
+    fprintf(outputFile,"<script src=\"/js/d3.dendrograms.js\" type=\"text/javascript\"></script>\n"); 
     fprintf(outputFile,"<div class = \"dropdown\">\n"); 
     fprintf(outputFile,"	<div id = dropdown>\n");  
     fprintf(outputFile,"</div>\n");
     fprintf(outputFile,"<script>\n"); 
     fprintf(outputFile,"	function load() {\n"); 
     fprintf(outputFile,"	var data;\n\n"); 
     fprintf(outputFile,"	d3.json(\"%s\", function(error,json){\n", jsonFile); 
     fprintf(outputFile,"		if (error) return console.warn(error);\n"); 
     fprintf(outputFile,"		data = json;\n"); 
     fprintf(outputFile,"			d3.dendrogram.makeRadialDendrogram('#dendrogram', data,{\n"); 
     fprintf(outputFile,"			});\n"); 
     fprintf(outputFile,"			d3.dendrogram.makeCartesianDendrogram('#phylogram', data, {\n"); 
     fprintf(outputFile,"			width: %i,\n", width); 
     fprintf(outputFile,"			height: %i,\n", height); 
     fprintf(outputFile,"			});\n\n");