Index: src/hg/instinct/bioInt2/bioIntDriver.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntDriver.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -b -B -U 4 -r1.3 -r1.4
--- src/hg/instinct/bioInt2/bioIntDriver.c	27 Apr 2009 06:15:48 -0000	1.3
+++ src/hg/instinct/bioInt2/bioIntDriver.c	20 May 2009 20:34:36 -0000	1.4
@@ -125,15 +125,151 @@
 
 return 0;
 }  
 
+void slPairHashesFree(struct slPair **pEl)
+{
+struct slPair *el;
+
+if ((el = *pEl) == NULL) return;
+
+freeMem(el->name);
+
+struct typeHash *th, *thList = el->val;
+for (th = thList; th; th = th->next)
+    {
+    freeMem(th->type);
+    hashFreeWithVals(&th->hash, analysisValsFree);
+    }
+freez(pEl);
+} 
+
+void slPairHashesFreeList(struct slPair **pList)
+{
+struct slPair *el, *next;
+
+for (el = *pList; el != NULL; el = next)
+    {
+    next = el->next;
+    slPairHashesFree(&el);
+    }
+*pList = NULL;
+}
+
+void slPairStringFree(struct slPair **pEl)
+{
+struct slPair *el;
+
+if ((el = *pEl) == NULL) return;
+
+freeMem(el->name);
+char *name = el->val;
+freeMem(name);
+freez(pEl);
+}     
+
+void slPairStringFreeList(struct slPair **pList)
+{
+struct slPair *el, *next;
+
+for (el = *pList; el != NULL; el = next)
+    {
+    next = el->next;
+    slPairStringFree(&el);
+    }
+*pList = NULL;
+}   
+
+void analysisValsSamplesHashes(struct sqlConnection *biConn, struct hash *hash,
+			       struct slPair **spList, char *dataset, char *type)
+{
+/* Currently only looks at first dataset in slName list passed in */
+char query[128];
+safef(query, sizeof(query), "select * from %s", dataset);
+struct slPair *sp;
+
+struct typeHash *th, *thList;
+
+struct sqlResult *sr = sqlGetResult(biConn, query);
+char **row = NULL;
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    char *sample_id = row[0];
+    char *feature_id = row[1];
+    struct analysisVals *av = analysisValsLoad(row);
+
+    struct hashEl *el = hashLookup(hash, sample_id);
+    if (!el)
+	{
+	AllocVar(sp);
+	sp->name = cloneString(sample_id);
+	AllocVar(th);
+	th->type = cloneString(type);
+	th->hash = hashNew(0);
+	sp->val = th;
+	hashAdd(hash, sample_id, sp);
+	slAddTail(spList, sp);
+	}
+    else
+	sp = el->val;
+
+    thList = sp->val;
+    for (th = thList; th; th = th->next)
+	if (sameString(type, th->type))
+	    break;
+    if (!th)
+	{ // type doesn't exist yet, add
+	AllocVar(th);
+	th->type = cloneString(type);
+	th->hash = hashNew(0);
+	slAddHead(&thList, th);
+	sp->val = thList;
+	}
+
+    /* found the hash, add data for feature */
+    hashAdd(th->hash, feature_id, av);
+    }
+sqlFreeResult(&sr);
+}         
+
+char *getDatasetType(struct sqlConnection *biConn, char *dataset)
+{
+char query[256];
+safef(query, sizeof(query),
+            "select %s.name from %s join %s on %s.type_id=%s.id "
+      "where data_table=\"%s\"",
+      DT_TABLE, DA_TABLE, DT_TABLE, DA_TABLE, DT_TABLE, dataset);
+
+return sqlQuickString(biConn, query);
+}
+
+struct slPair *analysisValsSamplesHashesList(struct sqlConnection *biConn,
+					     struct slName *datasets)
+{
+struct slPair *spList = NULL;
+struct slName *sl;
+struct hash *hash = hashNew(0);
+for (sl = datasets; sl; sl = sl->next)
+    {
+    char *type = getDatasetType(biConn, sl->name);
+    if (!type)
+	type = "N/A";
+    fprintf(stderr, "adding type = %s for %s\n", type, sl->name);
+    analysisValsSamplesHashes(biConn, hash, &spList, sl->name, type);
+    }
+
+hashFree(&hash);
+return spList;
+}    
+
+
 void storeAnalysisValsInDb(struct sqlConnection *biConn, char *tableName,
 			   struct analysisVals *avList)
 {
 if (!sqlTableExists(biConn, tableName))
     createAnalysisValsTable(biConn, tableName);
 
-slSort(avList, analysisValsCmp);
+slSort(&avList, analysisValsCmp);
 
 struct analysisVals *av;
 for (av = avList; av; av = av->next)
     analysisValsSaveToDb(biConn, av, tableName, 50);

