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

1.13 2009/04/07 03:41:09 jsanborn
updated
Index: src/hg/instinct/bioInt2/bioIntUI.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntUI.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -b -B -U 4 -r1.12 -r1.13
--- src/hg/instinct/bioInt2/bioIntUI.c	5 Apr 2009 21:08:07 -0000	1.12
+++ src/hg/instinct/bioInt2/bioIntUI.c	7 Apr 2009 03:41:09 -0000	1.13
@@ -424,8 +424,37 @@
     }
 sqlFreeResult(&sr);
 }
 
+struct samples *getOverlappingSamples(struct sqlConnection *conn, 
+				      struct datasets *daList)
+{
+struct dyString *dy = dyStringNew(100);
+int count = 1;
+dyStringPrintf(dy, "select * from %s as t%d ", SA_TABLE, count);
+count++;
+
+struct datasets *da = daList->next;
+for (da = daList; da; da = da->next)
+    {
+    dyStringPrintf(dy, 
+		   "join %s as t%d on t1.id = t%d.id ", 
+		   SA_TABLE, count, count);
+    count++;
+    }
+count = 1;
+dyStringPrintf(dy, "where ");
+for (da = daList; da; da = da->next)
+    {
+    dyStringPrintf(dy, "t%d.dataset_id=%d ", count, da->id);
+    if (da->next)
+	dyStringPrintf(dy, "and ");
+    count++;
+    }
+char *query = dyStringCannibalize(&dy);
+return samplesLoadByQuery(conn, query);
+}
+
 void sendClinicalData(struct sqlConnection *conn, struct json *js,
 		      int cohort_id, char *feature_name, struct datasets *daList)
 {
 struct features *fe = getFeaturesByName(conn, feature_name);
@@ -435,22 +464,20 @@
     hFreeConn(&conn);
     errAbort("Could not find clinical feature in db");
     }
 
+struct samples *sa, *samples = getOverlappingSamples(conn, daList);
 struct dyString *dy = dyStringNew(100);
 dyStringPrintf(dy, 
-	       "select DISTINCT %s.name, %s.val from %s join %s on %s.sample_id = %s.id ", 
-	       SA_TABLE, CD_TABLE, CD_TABLE, SA_TABLE, CD_TABLE, SA_TABLE);
-dyStringPrintf(dy, 
-	       "where %s.feature_id = %d and %s.dataset_id in (", 
+	       "select DISTINCT %s.name, %s.val, %s.code from %s "
+	       "join %s on %s.sample_id = %s.id " 
+	       "where %s.feature_id = %d and %s.id in (", 
+	       SA_TABLE, CD_TABLE, CD_TABLE, CD_TABLE, SA_TABLE, CD_TABLE, SA_TABLE,
 	       CD_TABLE, fe->id, SA_TABLE);
-
-
-struct datasets *da;
-for (da = daList; da; da = da->next)
+for (sa = samples; sa; sa = sa->next)
     {
-    dyStringPrintf(dy, "%d", da->id);
-    if (da->next)
+    dyStringPrintf(dy, "%d", sa->id);
+    if (sa->next)
 	dyStringPrintf(dy, ",");
     }
 dyStringPrintf(dy, ")");
 char *query = dyStringCannibalize(&dy);
@@ -465,9 +492,14 @@
 while ((row = sqlNextRow(sr)) != NULL)
     { 
     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);
     }
 }
 
 void sendUniqueMatch(struct sqlConnection *conn, struct json *js,