src/hg/instinct/bioInt2/bioIntUI.c 1.14

1.14 2009/04/07 19:01:01 jsanborn
changed way codes were sent
Index: src/hg/instinct/bioInt2/bioIntUI.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntUI.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -b -B -U 4 -r1.13 -r1.14
--- src/hg/instinct/bioInt2/bioIntUI.c	7 Apr 2009 03:41:09 -0000	1.13
+++ src/hg/instinct/bioInt2/bioIntUI.c	7 Apr 2009 19:01:01 -0000	1.14
@@ -485,8 +485,10 @@
 jsonAddString(js, "description", fe->longLabel);
 jsonAddString(js, "name", fe->name);
 jsonAddString(js, "source", "N/A");
 
+struct slDouble *sd;
+struct hash *codeHash = hashNew(0);
 struct json *data = jsonAddContainer(js, "data");
 struct sqlResult *sr = sqlGetResult(conn, query);
 char **row = NULL;
 while ((row = sqlNextRow(sr)) != NULL)
@@ -494,13 +496,30 @@
     char *name = row[0];
     double val = atof(row[1]);
     char *code = row[2];
        
-    if (sameString(code, "(null)")) // eventually need to fix this.
 	jsonAddDouble(data, name, val);
-    else
-	jsonAddString(data, name, code);
+    if (!sameString(code, "(null)")) // eventually need to fix this.
+	{
+	if (hashLookup(codeHash, code))
+	    continue;
+	sd = slDoubleNew(val);
+	hashAdd(codeHash, code, sd);
+	}
+    }
+if (hashNumEntries(codeHash) == 0)
+    return;
+
+struct json *codes = jsonAddContainer(js, "codes");
+struct hashEl *el, *elList = hashElListHash(codeHash);
+for (el = elList; el != NULL; el = el->next)
+    {
+    char *name = el->name;
+    sd = el->val;
+
+    jsonAddDouble(codes, name, sd->val);
     }
+hashElFreeList(&elList);
 }
 
 void sendUniqueMatch(struct sqlConnection *conn, struct json *js, 
 		     int cohort_id, char *feature_name, char *source, 
@@ -777,8 +796,108 @@
 
 hFreeConn(&conn);
 }
 
+boolean runStat(struct analysisVals *avList, double *prob)
+{
+if (!avList)
+    return FALSE;
+
+*prob = slCount(avList);
+return TRUE;
+}
+
+void getMostDiff()
+{
+int takeTop = cartUsualInt(cart, bioIntTakeTop, 5);
+
+int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
+if (cohort_id == -1)
+    cohort_id = 2;  // hard code for first analysis during testing!
+
+char *feature_name = cartOptionalString(cart, bioIntFeatureName);
+if (!feature_name)
+    errAbort("%s must be set for mode=getMostDiff\n", bioIntFeatureName);
+
+struct sqlConnection *conn = hAllocConnProfile(localDb, db);
+
+struct analyses *an, *analyses = getAnalysesByCohortId(conn, cohort_id);
+char query[512];
+
+an = analyses;
+safef(query, sizeof(query), "select * from %s order by feature_id", an->result_table);
+
+uglyTime(NULL);
+
+//struct analysisVals *avList = analysisValsLoadByQuery(conn, query);
+struct slPair *sp, *spList = NULL;
+struct slDouble *sd;
+double prob;
+
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **row = NULL;
+struct analysisVals *av, *avList = NULL;
+int currentId = -1;
+
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    av = analysisValsLoad(row);
+    if (currentId == -1)
+	currentId = av->feature_id;
+
+    if (av->feature_id == currentId)
+	slAddHead(&avList, av);
+    else
+	{
+	prob = -1.0;
+	if (runStat(avList, &prob))
+	    {
+	    sd = slDoubleNew(prob);
+	    AllocVar(sp);
+	    sp->name = cloneString("test");
+	    sp->val = sd;
+	    slAddHead(&spList, sp);
+	    }	
+	analysisValsFreeList(&avList);
+	avList = NULL;
+	currentId = av->feature_id;
+	slAddHead(&avList, av);
+	}
+    }
+sqlFreeResult(&sr);
+
+prob = -1.0;
+if (runStat(avList, &prob))
+    {
+    sd = slDoubleNew(prob);
+    AllocVar(sp);
+    sp->name = cloneString("test");
+    sp->val = sd;
+    slAddHead(&spList, sp);
+    }	
+
+uglyTime("finished");
+analysisValsFreeList(&avList);
+uglyTime("free'd");
+
+int count = 0;
+struct json *js = newJson();
+for (sp = spList; sp; sp = sp->next)
+    {
+    sd = sp->val;
+    jsonAddDouble(js, sp->name, sd->val);
+    if (count > takeTop)
+	break;
+    count++;
+    }
+
+if (js)
+    hPrintf("%s", js->print(js));
+
+
+hFreeConn(&conn);
+}
+
 void dispatchRoutines()
 /* Look at command variables in cart and figure out which
  * page to draw. */
 {
@@ -800,8 +919,10 @@
 else if (sameString(mode, "getClinicalFeatures"))
     getClinicalFeatures();
 else if (sameString(mode, "getMostCorrelated"))
     getMostCorrelated();
+else if (sameString(mode, "getMostDiff"))
+    getMostDiff();
 else
     errAbort("Incorrect mode = %s", mode);
 
 cartRemovePrefix(cart, bioIntPrefix);