src/hg/instinct/hgBamBam/hgBamBam.c 1.1

1.1 2010/05/25 20:22:45 jsanborn
initial commit
Index: src/hg/instinct/hgBamBam/hgBamBam.c
===================================================================
RCS file: src/hg/instinct/hgBamBam/hgBamBam.c
diff -N src/hg/instinct/hgBamBam/hgBamBam.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/hg/instinct/hgBamBam/hgBamBam.c	25 May 2010 20:22:45 -0000	1.1
@@ -0,0 +1,222 @@
+/* hgBamBam */
+#include "common.h"
+#include "bed.h"
+#include "cart.h"
+#include "linefile.h"
+#include "genoLay.h"
+#include "hash.h"
+#include "hCommon.h"
+#include "hdb.h"
+#include "hPrint.h"
+#include "htmshell.h"
+#include "hui.h"
+#include "trackLayout.h"
+#include "web.h"
+#include "json.h"
+#include "hgBamBam.h"
+
+static char const rcsid[] = "$Id$";
+/* ---- Global variables. ---- */
+struct cart *cart;	         /* This holds cgi and other variables between clicks. */
+struct hash *oldVars;	         /* Old cart hash. */
+
+#define FLOAT_NULL -9999
+
+char *db = "pdata";
+char *localDb = "localDb";
+
+void usage()
+/* Explain usage and exit. */
+{
+errAbort(
+  "hgBamBam\n"
+  "usage:\n"
+  "   hgBamBam\n"
+  );
+}
+
+/****** BEGIN HELPER FUNCTIONS *******/
+
+
+struct bed *getBed(struct sqlConnection *conn, char *tableName, char *pos, int nField, 
+		   float *retMaxVal)
+{
+/* get the data from the database */
+char **row = NULL;
+char query[512];
+query[0] = '\0';
+    
+if (!pos)
+    safef(query, sizeof(query), "select * from %s", tableName);
+else
+    {
+    char *chrom = NULL;
+    int start = 0;
+    int stop = 0;
+    if (!hgParseChromRange(NULL, pos, &chrom, &start, &stop))
+	return NULL;
+    
+    safef(query, sizeof(query),
+	  "select * from %s where chrom = '%s' and chromStart<%d and chromEnd>%d",
+	  tableName, chrom, stop, start);
+    }
+
+struct sqlResult *sr = sqlGetResult(conn, query);
+struct bed *tuple = NULL;
+struct bed *tupleList = NULL;
+
+float val, maxVal = -1;
+
+/* all copy number tracks are bed 4 */
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    tuple = bedLoadN(row+1, nField);
+    slAddHead(&tupleList, tuple);
+
+    val = sqlFloat(tuple->name);
+    if (val > maxVal)
+	maxVal = val;
+    }
+
+*retMaxVal = maxVal;
+
+slReverse(&tupleList);
+sqlFreeResult(&sr);
+return tupleList;
+}
+
+
+/****** END HELPER FUNCTIONS *******/
+
+
+float drawCopy(struct sqlConnection *conn, struct json *js, 
+	       char *pos, int width, int height, char *name, char *suffix, 
+	       float maxVal, float medVal, Color col)
+{
+char tableName[256];
+safef(tableName, sizeof(tableName), "%s_%s", name, suffix);
+
+float tmpMax = 0;
+struct bed *bed = getBed(conn, tableName, pos, 4, &tmpMax);
+
+if (maxVal == FLOAT_NULL)
+    maxVal = tmpMax;
+
+struct settings *settings = initSettings(conn, pos, width, height, 0, maxVal);
+
+char *filename = copyNumberGif(conn, bed, settings, suffix, medVal, col);
+
+char jsName[256];
+safef(jsName, sizeof(jsName), "image_%s", suffix);
+jsonAddString(js, jsName, filename);
+
+bedFree(&bed);
+
+return tmpMax;
+}
+
+void drawIdeo(struct sqlConnection *conn, struct json *js, 
+	      char *pos, int width)
+{
+int height = DEFAULT_LABEL_HEIGHT + CYTO_HEIGHT;
+struct settings *settings = initSettings(conn, pos, width, height, 0, 0);
+
+char *filename = genomeLabelsGif(conn, settings);
+
+jsonAddString(js, "ideogram", filename);
+}
+
+
+void drawGenes(struct sqlConnection *conn, struct json *js, 
+	      char *pos, int width)
+{
+int height = DEFAULT_LABEL_HEIGHT;
+struct settings *settings = initSettings(conn, pos, width, height, 0, 0);
+
+char *filename = annotationGif(settings, "refGene");
+
+jsonAddString(js, "genes", filename);
+}
+
+
+void drawScale(struct sqlConnection *conn, struct json *js, 
+	      char *pos, int width)
+{
+int height = DEFAULT_LABEL_HEIGHT;
+struct settings *settings = initSettings(conn, pos, width, height, 0, 0);
+
+char *filename = genomeScaleGif(settings);
+
+jsonAddString(js, "scale", filename);
+}
+
+
+void draw()
+{
+struct sqlConnection *conn = hAllocConnProfile(localDb, db);
+
+char *pos = cartUsualString(cart, bbPos, NULL);
+int width  = cartUsualInt(cart, bbWidth, DEFAULT_PLOT_WIDTH);
+int height = cartUsualInt(cart, bbHeight, DEFAULT_PLOT_HEIGHT);
+
+char *name = "TCGA_13_0751";
+struct json *js = newJson();
+float maxVal = FLOAT_NULL;
+maxVal = drawCopy(conn, js, pos, width, height, name, "cnv", FLOAT_NULL, 0.8, MG_BLACK);
+drawCopy(conn, js, pos, width, height, name, "major", maxVal, 0.5, MG_RED);
+drawCopy(conn, js, pos, width, height, name, "minor", maxVal, 0.4, MG_BLUE);
+
+drawIdeo(conn, js, pos, width);
+drawGenes(conn, js, pos, width);
+drawScale(conn, js, pos, width);
+
+if (js)
+    hPrintf("%s\n", js->print(js));
+hFreeConn(&conn);
+}
+
+void dispatchRoutines()
+/* Look at command variables in cart and figure out which
+ * page to draw. */
+{
+/* retrieve cart variables, handle various modes */
+char *mode = cartOptionalString(cart, bbMode);
+if (!mode)
+    errAbort("%s is required.", bbMode);
+
+if (sameString(mode, "draw"))
+    draw();
+else
+    errAbort("Incorrect mode = %s", mode);
+
+cartRemovePrefix(cart, bbPrefix);
+}
+
+void hghDoUsualHttp()
+/* Wrap html page dispatcher with code that writes out
+ * HTTP header and write cart back to database. */
+{
+cartWriteCookie(cart, hUserCookie());
+printf("Content-Type:application/x-javascript\r\n\r\n");
+
+/* Dispatch other pages, that actually want to write HTML. */
+cartWarnCatcher(dispatchRoutines, cart, jsonEarlyWarningHandler);
+cartCheckout(&cart);
+}
+
+char *excludeVars[] = {"Submit", "submit", NULL};
+
+int main(int argc, char *argv[])
+/* Process command line. */
+{
+htmlPushEarlyHandlers();
+cgiSpoof(&argc, argv);
+htmlSetStyle(htmlStyleUndecoratedLink);
+
+oldVars = hashNew(12);
+cart = cartForSession(hUserCookie(), excludeVars, oldVars);
+
+hghDoUsualHttp();
+return 0;
+}
+