src/hg/instinct/hgBamBam/hgBamBam.c 1.3

1.3 2010/05/26 08:02:55 jsanborn
updated
Index: src/hg/instinct/hgBamBam/hgBamBam.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/hgBamBam/hgBamBam.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -b -B -U 1000000 -r1.2 -r1.3
--- src/hg/instinct/hgBamBam/hgBamBam.c	26 May 2010 05:20:33 -0000	1.2
+++ src/hg/instinct/hgBamBam/hgBamBam.c	26 May 2010 08:02:55 -0000	1.3
@@ -1,311 +1,384 @@
 /* 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;
 }
 
 
 struct breaks *getBreaks(struct sqlConnection *conn, char *tableName, char *pos, boolean intra)
 {
 /* get the data from the database */
 char **row = NULL;
 char query[512];
 query[0] = '\0';
     
 if (!pos)
     {
     if (intra)
 	safef(query, sizeof(query), "select * from %s where chrom = chrom2", tableName);
     else
 	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;
 
     if (intra)
     safef(query, sizeof(query),
 	  "select * from %s where chrom = chrom2 and chrom = '%s' and chromStart<%d and chromEnd>%d",
 	  tableName, chrom, stop, start);
     else
 	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 breaks *br, *brList = NULL;
 
 /* all copy number tracks are bed 4 */
 while ((row = sqlNextRow(sr)) != NULL)
     {
     AllocVar(br);
 
     br->chrom = cloneString(row[0]);
     br->chromStart = atoi(row[1]);
     br->chromEnd = atoi(row[2]);
     strcpy(br->strand, row[3]);
     br->chrom2 = cloneString(row[4]);
     br->chromStart2 = atoi(row[5]);
     br->chromEnd2 = atoi(row[6]);
     strcpy(br->strand2, row[7]);
     br->support = atoi(row[8]);
     br->qual = atof(row[9]);
 
     slAddHead(&brList, br);
     }
 slReverse(&brList);
 sqlFreeResult(&sr);
 return brList;
 }
 
 
 /****** 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);
 if (!filename)
     return 0.0;
 
 char jsName[256];
 safef(jsName, sizeof(jsName), "image_%s", suffix);
 jsonAddString(js, jsName, filename);
 
 bedFree(&bed);
 
 return tmpMax;
 }
 
 void drawBreaks(struct sqlConnection *conn, struct json *js, 
 	       char *pos, int width, char *name, char *suffix)
 {
 char tableName[256];
 safef(tableName, sizeof(tableName), "%s_%s", name, suffix);
 
 int height = DEFAULT_LABEL_HEIGHT;
 struct breaks *breaks = getBreaks(conn, tableName, pos, TRUE);  // TRUE = intra
 
 struct settings *settings = initSettings(conn, pos, width, height, 0, 0);
 
 char *filename = breaksGif(conn, breaks, settings);
 if (!filename)
     return;
 
 jsonAddString(js, suffix, filename);
 jsonAddInt(js, "breaksHeight", settings->height);
 }
 
 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);
 if (!filename)
     return;
 
 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");
 if (!filename)
     return;
 
 jsonAddString(js, "genes", filename);
 jsonAddInt(js, "genesHeight", settings->height);
 }
 
 
 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);
 if (!filename)
     return;
 
 jsonAddString(js, "scale", filename);
 }
 
+char *getPosition(struct genoLayChrom *glList)
+{
+char *pos = cartUsualString(cart, bbPos, NULL);
+
+if (!pos)
+    return NULL;
+
+char *chrom;
+int start = -1, stop = -1;
+if (!hgParseChromRange(NULL, pos, &chrom, &start, &stop))
+    return NULL;
+
+struct genoLayChrom *gl;
+for (gl = glList; gl; gl = gl->next)
+    {
+    if (!sameString(gl->fullName, chrom))
+	continue;
+    break;
+    }
+if (!gl)
+    return NULL;
+
+if (start <= 0 && stop <= 0)
+    {
+    start = 1;
+    stop  = gl->size - 1;
+    }
+
+if (start <= 0)
+    start = 1;
+else if (start > gl->size - 1)
+    start = gl->size - 1;
+
+if (stop <= 0)
+    stop = 1;
+else if (stop > gl->size - 1)
+    stop = gl->size - 1;
+
+char newPos[256];
+safef(newPos, sizeof(newPos), "%s:%d-%d", chrom, start, stop);
+
+return cloneString(newPos);
+}
 
 void draw()
 {
 struct sqlConnection *conn = hAllocConnProfile(localDb, db);
 
-char *pos = cartUsualString(cart, bbPos, NULL);
+struct genoLayChrom *glList = genoLayDbChroms(conn, FALSE);
+
+char *pos = getPosition(glList);
+
 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);
 
 drawBreaks(conn, js, pos, width, name, "breaks");
 
 drawIdeo(conn, js, pos, width);
 drawGenes(conn, js, pos, width);
 drawScale(conn, js, pos, width);
 
-hFreeConn(&conn);
+if (pos)
+    jsonAddString(js, "pos", pos);
+else
+    jsonAddString(js, "pos", "");
 
+struct settings *settings = initSettings(conn, pos, width, height, 0, 0);
+
+if (settings)
+    {
+    struct json *new, *list = jsonAddContainerList(js, "chroms");
+    new = list;
+    
+    struct chromLay *cl;
+    for (cl = settings->layoutList; cl; cl = cl->next)
+	{
+	jsonAddString(new, "name", cl->chrom);
+	jsonAddDouble(new, "baseStart", cl->baseStart);
+	jsonAddDouble(new, "baseEnd", cl->baseEnd);
+	jsonAddDouble(new, "baseWidth", cl->baseWidth);
+	jsonAddInt(new, "pxStart", cl->pxStart);
+	jsonAddInt(new, "pxStop", cl->pxStart + cl->pxWidth);
+	jsonAddInt(new, "pxWidth", cl->pxWidth);
+	
+	if (cl->next)
+	    new = jsonAddContainerToList(&list);
+	}
+    }
 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;
 }