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

1.11 2009/04/04 00:39:22 jsanborn
added cohorts api
Index: src/hg/instinct/bioInt2/bioIntUI.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntUI.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -b -B -U 4 -r1.10 -r1.11
--- src/hg/instinct/bioInt2/bioIntUI.c	31 Mar 2009 23:36:00 -0000	1.10
+++ src/hg/instinct/bioInt2/bioIntUI.c	4 Apr 2009 00:39:22 -0000	1.11
@@ -107,8 +107,19 @@
 
 return featuresLoadByQuery(conn, query);
 }
 
+struct tissues *getTissuesById(struct sqlConnection *conn, 
+			       int id)
+{
+char query[256];
+safef(query, sizeof(query), 
+      "select * from %s where id = %d", 
+      TI_TABLE, id);
+
+return tissuesLoadByQuery(conn, query);
+}
+
 struct datasets *getDatasetsByCohortId(struct sqlConnection *conn, int cohort_id)
 {
 char query[256];
 safef(query, sizeof(query), 
@@ -218,8 +232,49 @@
 
 hFreeConn(&conn);
 }
 
+void getCohorts()
+{
+char query[256];
+safef(query, sizeof(query),
+      "select * from %s;", CO_TABLE);
+
+struct sqlConnection *conn = hAllocConnProfile(localDb, db);
+struct cohorts *co, *coList = cohortsLoadByQuery(conn, query);
+
+struct json *js = newJson();
+struct json *cohort, *cohorts = jsonAddContainerList(js, "cohorts");
+cohort = cohorts;
+for (co = coList; co; co = co->next)
+    {
+    jsonAddInt(cohort, "cohort_id", co->id);
+    jsonAddString(cohort, "name", co->name);
+
+    struct datasets *da, *daList = getDatasetsByCohortId(conn, co->id);
+
+    struct tissues *ti = getTissuesById(conn, daList->tissue_id);
+    jsonAddString(cohort, "tissue", ti->name);
+    tissuesFree(&ti);
+
+    struct json *data = jsonAddContainer(cohort, "datasets");
+
+    for (da = daList; da; da = da->next)
+	{
+	jsonAddString(data, "name", da->name);
+	jsonAddInt(data, "num_samples", da->num_samples);
+	}
+
+    if (co->next)
+	cohort = jsonAddContainerToList(&cohorts);    
+    }
+
+if (js)
+    hPrintf("%s\n", js->print(js));
+
+hFreeConn(&conn);
+}
+
 struct searchResults {
     struct searchResults *next;
     char *name;
     char *source;
@@ -425,9 +480,9 @@
 void getFeatureData()
 {
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
 if (cohort_id == -1)
-    cohort_id = 1;  // hard code for first analysis during testing!
+    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);
@@ -452,9 +507,9 @@
 void getSuggestions()
 {
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
 if (cohort_id == -1)
-    cohort_id = 1;  // hard code for first analysis during testing!
+    cohort_id = 2;  // hard code for first analysis during testing!
 
 /* feature source = gene,geneset,clinical... */
 //char *source = cartOptionalString(cart, bioIntSourceName);
 
@@ -490,9 +545,9 @@
 void getClinicalFeatures()
 {
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
 if (cohort_id == -1)
-    cohort_id = 1;  // hard code for first analysis during testing!
+    cohort_id = 2;  // hard code for first analysis during testing!
 
 struct sqlConnection *conn = hAllocConnProfile(localDb, db);
 
 struct datasets *da, *daList = getDatasetsByCohortId(conn, cohort_id);
@@ -542,9 +597,9 @@
 void getClinicalData()
 {
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
 if (cohort_id == -1)
-    cohort_id = 1;  // hard code for first analysis during testing!
+    cohort_id = 2;  // hard code for first analysis during testing!
 
 char *feature_name = cartOptionalString(cart, bioIntFeatureName);
 if (!feature_name)
     errAbort("%s or %s must be set for mode=getClinicalData\n", bioIntFeature, bioIntFeatureId);
@@ -603,9 +658,9 @@
 int takeTop = cartUsualInt(cart, bioIntTakeTop, 5);
 
 int cohort_id = cartUsualInt(cart, bioIntCohortId, -1);
 if (cohort_id == -1)
-    cohort_id = 1;  // hard code for first analysis during testing!
+    cohort_id = 2;  // hard code for first analysis during testing!
 
 char *feature_name = cartOptionalString(cart, bioIntFeatureName);
 if (!feature_name)
     errAbort("%s or %s must be set for mode=getMostCorrelated\n", bioIntFeature, bioIntFeatureId);
@@ -698,8 +753,10 @@
     errAbort("%s is required.", bioIntMode);
 
 if (sameString(mode, "getAnalyses"))
     getAnalyses();
+else if (sameString(mode, "getCohorts"))
+    getCohorts();
 else if (sameString(mode, "getFeatureData"))
     getFeatureData();
 else if (sameString(mode, "getSuggestions"))
     getSuggestions();