11c0c24c7441325f1823730a87ed3266e67215b3
kate
  Wed Oct 21 11:37:11 2015 -0700
Checking in graph exploratory work (JS D3) to save it (in a branch) before whacking. refs #15645

diff --git src/hg/hgGtexApi/hgGtexApi.c src/hg/hgGtexApi/hgGtexApi.c
new file mode 100644
index 0000000..1dcc43f
--- /dev/null
+++ src/hg/hgGtexApi/hgGtexApi.c
@@ -0,0 +1,78 @@
+/* Copyright (C) 2015 The Regents of the University of California 
+ * See README in this or parent directory for licensing information. */
+
+/* hgGtexApi - a web API to selected GTEx resources
+ *
+ * usage:  http://hgwdev-kate.cse.ucsc.edu/cgi-bin/hgGtexApi?gene=ENSG00000223972.4
+ *
+ * returns JSON in the format:
+ *      { "id": <donor>, "tissue": <tissue>, "rpkm": <rpkm> }
+*/
+
+#include "common.h"
+#include "hdb.h"
+#include "hPrint.h"
+#include "dystring.h"
+#include "api.h"
+#include "gtexSampleData.h"
+
+/* Requests */
+
+// TODO: Move to lib
+
+void gtexSampleDataJson(struct dyString *json, struct gtexSampleData *data)
+/* Print out gtexSampleData in JSON format. */
+{
+dyStringPrintf(json, "{");
+dyStringPrintf(json, "\"sample\":\"%s\"", data->sample);
+dyStringPrintf(json, ", ");
+dyStringPrintf(json, "\"tissue\":\"%s\"", data->tissue);
+dyStringPrintf(json, ", ");
+dyStringPrintf(json, "\"rpkm\":%.3f", data->score);
+dyStringPrintf(json, "}");
+}
+
+void geneResource(struct dyString *output)
+/* Retrieve gene expression for specified gene, and create JSON to output. 
+   Initially supporting only cellType, dataType, and antibody (type= parameter).
+   Allows specifying cv file with file= parameter
+        e.g. http://genome.ucsc.edu/cgi-bin/hgApi?db=hg19&cmd=cv&file=cv.ra&type=dataType
+*/
+{
+char query[256];
+struct sqlResult *sr;
+char **row;
+dyStringPrintf(output, "[\n");
+struct sqlConnection *conn = sqlConnect("hgFixed");
+char *gene = cgiString("gene");
+sqlSafef(query, sizeof(query), "select * from gtexSampleData where geneId='%s'", gene);
+sr = sqlGetResult(conn, query);
+struct gtexSampleData *data;
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    data = gtexSampleDataLoad(row);
+    gtexSampleDataJson(output, data);
+    dyStringAppend(output,",\n");
+    }
+output->string[dyStringLen(output)-2] = '\n';
+output->string[dyStringLen(output)-1] = ']';
+dyStringPrintf(output, "\n");
+sqlDisconnect(&conn);
+}
+
+int main(int argc, char *argv[])
+/* Process command line */
+{
+long enteredMainTime = clock1000();
+struct dyString *output = newDyString(10000);
+
+cgiSpoof(&argc, argv);
+pushWarnHandler(apiWarnAbortHandler);
+pushAbortHandler(apiWarnAbortHandler);
+
+char *jsonp = cgiOptionalString("jsonp");
+geneResource(output);
+apiOut(dyStringContents(output), jsonp);
+cgiExitTime("hgGtexApi", enteredMainTime);
+return 0;
+}