src/hg/instinct/bioInt2/grabData.c 1.2

1.2 2009/04/06 18:51:43 jsanborn
added transpose option
Index: src/hg/instinct/bioInt2/grabData.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/grabData.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -b -B -U 4 -r1.1 -r1.2
--- src/hg/instinct/bioInt2/grabData.c	5 Apr 2009 21:08:07 -0000	1.1
+++ src/hg/instinct/bioInt2/grabData.c	6 Apr 2009 18:51:43 -0000	1.2
@@ -15,24 +15,90 @@
 {
 errAbort(
 	 "grabData \n"
 	 "usage:\n"
-	 "   grabData table\n"
+	 "   grabData [options] table\n"
+	 "      -clinical    get clinical data corresponding to table\n"
+	 "      -transpose   samples as rows, features as columns\n"
 	 );
 }
 
 char db[128] = "bioInt";
 char localDb[128] = "localDb";
 
 boolean getClinical = FALSE;
+boolean transpose = FALSE;
 
 static struct optionSpec options[] = {
     {"clinical", OPTION_BOOLEAN},
+    {"transpose", OPTION_BOOLEAN},
     {NULL, 0}
 }; 
 
-void spHashListPrint(struct slPair *spList, struct slName *slList, char *tableName)
+void spHashListTranspose(struct slPair *spList, struct slName *slList, struct slPair **retSpList, struct slName **retSlList)
 {
+// Transpose data in spList. So, sp->name = sample name, sp->hash keyed by feature name 
+
+struct slName *sl, *outSlList = NULL;
+struct slPair *sp, *outSp, *outSpList = NULL;
+
+struct hash *featureHash = hashNew(0);
+struct hash *sampleHash = hashNew(0);
+for (sl = slList; sl; sl = sl->next)
+    {
+    struct hash *dataHash;
+    struct hashEl *el = hashLookup(sampleHash, sl->name);
+    if (!el)
+	{
+	AllocVar(outSp);
+	outSp->name = cloneString(sl->name);
+	dataHash = hashNew(0);
+	outSp->val = dataHash;
+	slAddHead(&outSpList, outSp);
+	hashAdd(sampleHash, sl->name, outSp);
+	}
+    else
+	outSp = el->val;
+
+    for (sp = spList; sp; sp = sp->next)
+	{
+	el = hashLookup(featureHash, sp->name);
+	if (!el)
+	    {
+	    slNameAddHead(&outSlList, sp->name);
+	    hashAddInt(featureHash, sp->name, 1);
+	    }
+
+	struct hash *hash = sp->val;
+	el = hashLookup(hash, sl->name);
+	if (el)
+	    {
+	    struct slDouble *sd = el->val;
+	    hash = outSp->val;
+	    hashAdd(hash, sp->name, sd);
+	    }
+	}
+    }
+
+*retSpList = outSpList;
+*retSlList = outSlList;
+}
+
+void spHashListPrint(struct slPair *inSpList, struct slName *inSlList, char *tableName)
+{
+struct slPair *spList = NULL;
+struct slName *slList = NULL;
+if (transpose)
+    {
+    fprintf(stdout, "transposing hash\n");
+    spHashListTranspose(inSpList, inSlList, &spList, &slList);
+    fprintf(stdout, "finished transposing\n");
+    }
+else
+    {
+    spList = inSpList;
+    slList = inSlList;
+    }
 char filename[256];
 safef(filename, sizeof(filename), "%s_data.tab", tableName);
 FILE *f = mustOpen(filename, "w");
 
@@ -405,8 +472,11 @@
 
 if (optionExists("clinical"))
     getClinical = TRUE;
 
+if (optionExists("transpose"))
+    transpose = TRUE;
+
 grabData(argv[1]);
 
 return 0;
 }