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

1.19 2009/04/27 22:05:48 jsanborn
added drill down into meta-genes
Index: src/hg/instinct/bioInt2/bioIntUI.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntUI.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -b -B -U 4 -r1.18 -r1.19
--- src/hg/instinct/bioInt2/bioIntUI.c	27 Apr 2009 16:44:08 -0000	1.18
+++ src/hg/instinct/bioInt2/bioIntUI.c	27 Apr 2009 22:05:48 -0000	1.19
@@ -380,8 +380,62 @@
 for (sp = spList; sp; sp = sp->next)
     jsonAddString(js, sp->name, sp->type); 
 }
 
+void sendRawFeatureData(struct sqlConnection *conn, struct json *js, 
+			struct datasets *da, struct analysisFeatures *af)
+{
+char query[512];
+safef(query, sizeof(query), 
+      "select DISTINCT %s.name, %s.conf from %s join %s on %s.sample_id = %s.id "
+      "where %s.feature_id = %d;",
+      SA_TABLE, da->data_table, da->data_table, SA_TABLE, da->data_table, SA_TABLE,
+      da->data_table, af->id);
+if (!sqlExists(conn, query))
+    return;
+
+struct json *data = jsonAddContainer(js, "data");
+
+setAnalysisFeatureDesc(conn, data, af);
+setAnalysisFeatureLink(conn, data, af);  // e.g. 'hgg_gene' = kgId
+    
+struct sqlResult *sr = sqlGetResult(conn, query);
+char **row = NULL;
+while ((row = sqlNextRow(sr)) != NULL)
+    { 
+    char *name = row[0];
+    double val = atof(row[1]);
+    jsonAddDouble(data, name, val);
+    }
+sqlFreeResult(&sr);
+}
+
+void sendRawFeatures(struct sqlConnection *conn,
+		     int cohort_id, struct analysisFeatures *af)
+{
+struct datasets *da, *daList = getDatasetsByCohortId(conn, cohort_id);
+if (!daList)
+    {
+    hFreeConn(&conn);
+    errAbort("No datasets with cohort_id = %d.\n", cohort_id);
+    }
+
+int count = 0;
+char name[256];
+struct json *js = newJson();
+for (da = daList; da; da = da->next)
+    {
+    safef(name, sizeof(name), "%s-%d", af->feature_name, count);
+    struct json *container = jsonAddContainer(js, name);
+    sendRawFeatureData(conn, container, da, af);
+    count++;
+    }
+
+if (js)
+    hPrintf("%s", js->print(js));
+}
+
+
 void sendAnalysisFeatureData(struct sqlConnection *conn, struct json *js, 
 			     int cohort_id, char *feature_name)
 {
 struct analysisFeatures *af = getAnalysisFeaturesByName(conn, feature_name);
@@ -548,12 +602,8 @@
 
 void getFeatureData()
 {
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
-if (cohort_id == -1)
-    cohort_id = 2;  // hard code for first analysis during testing!
-
-/* feature source = gene,geneset,clinical... */
 char *source = cartOptionalString(cart, bioIntSourceName);
 char *feature_name = cartOptionalString(cart, bioIntFeatureName);
 if (!feature_name || !source)
     errAbort("%s and %s must be set for mode=getFeatureData\n", bioIntFeature, bioIntSourceName);
@@ -613,10 +663,8 @@
 
 void getClinicalFeatures()
 {
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
-if (cohort_id == -1)
-    cohort_id = 2;  // hard code for first analysis during testing!
 
 struct sqlConnection *conn = hAllocConnProfile(localDb, db);
 
 struct datasets *da, *daList = getDatasetsByCohortId(conn, cohort_id);
@@ -820,57 +868,18 @@
 
 hFreeConn(&conn);
 }
 
-void expandFeature()
+void sendContainedFeatures(struct sqlConnection *conn, int cohort_id, 
+			   struct analysisFeatures *af, char *input_table, int takeTop)
 {
-int takeTop = cartUsualInt(cart, bioIntTakeTop, 5);
-int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
-char *feature_name = cartOptionalString(cart, bioIntFeatureName);
-
-if (!feature_name)
-    errAbort("%s must be set for mode=getGenesInGeneset\n", bioIntFeatureName);
-
-struct sqlConnection *conn = hAllocConnProfile(localDb, db);
-
-struct analysisFeatures *af = getAnalysisFeaturesByName(conn, feature_name);
-if (!af)
-    {
-    hFreeConn(&conn);
-    errAbort("Could not find analysisFeature in db");
-    }
-
-struct analyses *an, *anList = getAnalysesByCohortId(conn, cohort_id);
-if (!anList)
-    {
-    hFreeConn(&conn);
-    errAbort("No analyses with cohort_id = %d.\n", cohort_id);
-    }
-
-char tmpQ[512];
-for (an = anList; an; an = an->next)
-    {
-    safef(tmpQ, sizeof(tmpQ),
-	  "select * from %s where feature_id = %d;",
-	  an->result_table, af->id);
-    if (sqlExists(conn, tmpQ))
-	break;
-    }
-
-if (!an)
-    {
-    hFreeConn(&conn);
-    errAbort("No analysis feature with id=%d in any analysis table in cohort with id = %d", 
-	     af->id, cohort_id);
-    }
+if (!input_table)
+    return;
 
-// input_tables may have comma-separated list of raw datasets
-//    we don't want to expand into those yet.
-char *input_tables = an->input_tables;
-if (!sqlTableExists(conn, input_tables))
+if (!sqlTableExists(conn, input_table))
     {
     hFreeConn(&conn);
-    errAbort("Table does not exist, %s.\n", input_tables);
+    errAbort("Table does not exist, %s.\n", input_table);
     }
 
 uglyTime(NULL);
 struct dyString *dy = newDyString(100);
@@ -884,9 +893,9 @@
 dy = newDyString(100);
 dyStringPrintf(dy, 
 	       "select feature_name, type, sum(abs(conf)) as s from %s "
 	       "join %s on feature_id=id where feature_id in (",
-	       AF_TABLE, input_tables);
+	       AF_TABLE, input_table);
 for (si = siList; si; si = si->next)
     {
     dyStringPrintf(dy, "%d", si->val);
     if (si->next)
@@ -917,8 +926,58 @@
 sendUniqueMatch(conn, js, cohort_id, feature_names, types, NULL);  
 
 if (js)
     hPrintf("%s", js->print(js));
+}
+
+void expandFeature()
+{
+int takeTop = cartUsualInt(cart, bioIntTakeTop, 5);
+int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
+char *feature_name = cartOptionalString(cart, bioIntFeatureName);
+
+if (!feature_name)
+    errAbort("%s must be set for mode=getGenesInGeneset\n", bioIntFeatureName);
+
+struct sqlConnection *conn = hAllocConnProfile(localDb, db);
+
+struct analysisFeatures *af = getAnalysisFeaturesByName(conn, feature_name);
+if (!af)
+    {
+    hFreeConn(&conn);
+    errAbort("Could not find analysisFeature in db");
+    }
+
+struct analyses *an, *anList = getAnalysesByCohortId(conn, cohort_id);
+if (!anList)
+    {
+    hFreeConn(&conn);
+    errAbort("No analyses with cohort_id = %d.\n", cohort_id);
+    }
+
+char tmpQ[512];
+for (an = anList; an; an = an->next)
+    {
+    safef(tmpQ, sizeof(tmpQ),
+	  "select * from %s where feature_id = %d;",
+	  an->result_table, af->id);
+    if (sqlExists(conn, tmpQ))
+	break;
+    }
+
+if (!an)
+    {
+    hFreeConn(&conn);
+    errAbort("No analysis feature with id=%d in any analysis table in cohort with id = %d", 
+	     af->id, cohort_id);
+    }
+
+struct slName *tables = slNameListFromComma(an->input_tables);
+
+if (slCount(tables) > 1)
+    sendRawFeatures(conn, cohort_id, af);
+else
+    sendContainedFeatures(conn, cohort_id, af, an->input_tables, takeTop);
 
 hFreeConn(&conn);
 }