src/hg/instinct/hgBamBam/hgBamBam.c 1.4

1.4 2010/05/31 00:02:34 jsanborn
big update
Index: src/hg/instinct/hgBamBam/hgBamBam.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/hgBamBam/hgBamBam.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -b -B -U 4 -r1.3 -r1.4
--- src/hg/instinct/hgBamBam/hgBamBam.c	26 May 2010 08:02:55 -0000	1.3
+++ src/hg/instinct/hgBamBam/hgBamBam.c	31 May 2010 00:02:34 -0000	1.4
@@ -38,9 +38,9 @@
 /****** BEGIN HELPER FUNCTIONS *******/
 
 
 struct bed *getBed(struct sqlConnection *conn, char *tableName, char *pos, int nField, 
-		   float *retMaxVal)
+		   float *retMinVal, float *retMaxVal)
 {
 /* get the data from the database */
 char **row = NULL;
 char query[512];
@@ -64,9 +64,9 @@
 struct sqlResult *sr = sqlGetResult(conn, query);
 struct bed *tuple = NULL;
 struct bed *tupleList = NULL;
 
-float val, maxVal = -1;
+float val, maxVal = -1, minVal = 9999999.9;
 
 /* all copy number tracks are bed 4 */
 while ((row = sqlNextRow(sr)) != NULL)
     {
@@ -74,10 +74,13 @@
     slAddHead(&tupleList, tuple);
     val = sqlFloat(tuple->name);
     if (val > maxVal)
 	maxVal = val;
+    if (val < minVal)
+	minVal = val;
     }
 
+*retMinVal = minVal;
 *retMaxVal = maxVal;
 
 slReverse(&tupleList);
 sqlFreeResult(&sr);
@@ -96,9 +99,9 @@
     {
     if (intra)
 	safef(query, sizeof(query), "select * from %s where chrom = chrom2", tableName);
     else
-	safef(query, sizeof(query), "select * from %s", tableName);
+	safef(query, sizeof(query), "select * from %s where chrom != chrom2", tableName);
     }
 else
     {
     char *chrom = NULL;
@@ -108,14 +111,14 @@
 	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);
+	      "select * from %s where chrom = chrom2 and chrom = '%s' and (chromStart<%d and chromEnd>%d) or (chromStart2<%d and chromEnd2>%d) or (chromStart<%d and chromEnd2>%d) or (chromStart2<%d and chromEnd>%d)",
+	      tableName, chrom, stop, start, stop, start, stop, start, stop, start);
     else
 	safef(query, sizeof(query),
-	      "select * from %s where chrom = '%s' and chromStart<%d and chromEnd>%d",
-	      tableName, chrom, stop, start);
+	      "select * from %s where chrom != chrom2 and ((chrom = '%s' and chromStart<%d and chromEnd>%d) or (chrom2 = '%s' and chromStart2<%d and chromEnd2>%d))",
+	      tableName, chrom, stop, start, chrom, stop, start);
     }
 
 struct sqlResult *sr = sqlGetResult(conn, query);
 struct breaks *br, *brList = NULL;
@@ -147,23 +150,24 @@
 /****** 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 *pos, int width, int height, char *sampleName, char *suffix,
+	       char *title, float maxVal, float medVal, Color col)
 {
 char tableName[256];
-safef(tableName, sizeof(tableName), "%s_%s", name, suffix);
+safef(tableName, sizeof(tableName), "%s_%s", sampleName, suffix);
 
+float minVal = 0;
 float tmpMax = 0;
-struct bed *bed = getBed(conn, tableName, pos, 4, &tmpMax);
+struct bed *bed = getBed(conn, tableName, pos, 4, &minVal, &tmpMax);
 
-if (maxVal == FLOAT_NULL)
-    maxVal = tmpMax;
+//if (maxVal == FLOAT_NULL)
+maxVal = tmpMax;
 
-struct settings *settings = initSettings(conn, pos, width, height, 0, maxVal);
+struct settings *settings = initSettings(conn, pos, title, width, height, 0.0, maxVal);
 
-char *filename = copyNumberGif(conn, bed, settings, suffix, medVal, col);
+char *filename = copyNumberGif(conn, bed, settings, suffix, 0.0, col);
 if (!filename)
     return 0.0;
 
 char jsName[256];
@@ -174,32 +178,55 @@
 
 return tmpMax;
 }
 
-void drawBreaks(struct sqlConnection *conn, struct json *js, 
+void drawInterBreaks(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, FALSE);  // FALSE = inter
+
+struct settings *settings = initSettings(conn, pos, "Inter-Chromosomal Breaks", width, height, 0, 0);
+
+char *filename = interBreaksGif(conn, breaks, settings);
+if (!filename)
+    return;
+
+char jsName[256];
+safef(jsName, sizeof(jsName), "inter_%s", suffix);
+jsonAddString(js, jsName, filename);
+jsonAddInt(js, "interBreaksHeight", settings->height);
+}
+
+void drawIntraBreaks(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);
+struct settings *settings = initSettings(conn, pos, "Intra-Chromosomal Breaks", width, height, 0, 0);
 
-char *filename = breaksGif(conn, breaks, settings);
+char *filename = intraBreaksGif(conn, breaks, settings);
 if (!filename)
     return;
 
-jsonAddString(js, suffix, filename);
-jsonAddInt(js, "breaksHeight", settings->height);
+char jsName[256];
+safef(jsName, sizeof(jsName), "intra_%s", suffix);
+jsonAddString(js, jsName, filename);
+jsonAddInt(js, "intraBreaksHeight", 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);
+struct settings *settings = initSettings(conn, pos, NULL, width, height, 0, 0);
 
 char *filename = genomeLabelsGif(conn, settings);
 if (!filename)
     return;
@@ -210,10 +237,10 @@
 
 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);
+int height = DEFAULT_LABEL_HEIGHT + TITLE_HEIGHT;
+struct settings *settings = initSettings(conn, pos, "RefSeq Genes", width, height, 0, 0);
 
 char *filename = annotationGif(settings, "refGene");
 if (!filename)
     return;
@@ -221,14 +248,35 @@
 jsonAddString(js, "genes", filename);
 jsonAddInt(js, "genesHeight", settings->height);
 }
 
+void drawSNPs(struct sqlConnection *conn, struct json *js, 
+	      char *pos, int width, char *name, char *suffix)
+{
+int height = DEFAULT_LABEL_HEIGHT;
+struct settings *settings = initSettings(conn, pos, 
+					 "Single Nucleotide Variants", width, height, 0, 0);
+
+char tableName[256];
+safef(tableName, sizeof(tableName), "%s_%s", name, suffix);
+
+char *filename = snpsGif(settings, tableName);
+if (!filename)
+    return;
+
+char jsName[256];
+safef(jsName, sizeof(jsName), "image_%s", suffix);
+jsonAddString(js, jsName, filename);
+
+jsonAddInt(js, "snpsHeight", 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);
+struct settings *settings = initSettings(conn, pos, NULL, width, height, 0, 0);
 
 char *filename = genomeScaleGif(settings);
 if (!filename)
     return;
@@ -242,22 +290,38 @@
 
 if (!pos)
     return NULL;
 
+if (sameString(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, pos))
+	continue;
+    break;
+    }
+
+if (gl) // only single chrom (e.g. "chr7") given as input.
+    chrom = cloneString(gl->fullName);
+else
+    {
+    if (!hgParseChromRange(NULL, pos, &chrom, &start, &stop))
+	return NULL;
+
+    for (gl = glList; gl; gl = gl->next)
+	{
     if (!sameString(gl->fullName, chrom))
 	continue;
     break;
     }
-if (!gl)
+    if (!gl)
     return NULL;
+    }
 
 if (start <= 0 && stop <= 0)
     {
     start = 1;
@@ -290,16 +354,21 @@
 
 int width  = cartUsualInt(cart, bbWidth, DEFAULT_PLOT_WIDTH);
 int height = cartUsualInt(cart, bbHeight, DEFAULT_PLOT_HEIGHT);
 
-char *name = "TCGA_13_0751";
+char *name = cartUsualString(cart, bbSampleName, NULL);
+if (!name)
+    return;
+
 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");
+maxVal = drawCopy(conn, js, pos, width, height, name, "cnv", "Overall Copy Number", FLOAT_NULL, 0.0, MG_BLACK);
+drawCopy(conn, js, pos, width, height, name, "major", "Majority Copy Number", maxVal, 0.0, MG_RED);
+drawCopy(conn, js, pos, width, height, name, "minor", "Minority Copy Number", maxVal, 0.0, MG_BLUE);
+
+drawSNPs(conn, js, pos, width, name, "snps");
+drawInterBreaks(conn, js, pos, width, name, "breaks");
+drawIntraBreaks(conn, js, pos, width, name, "breaks");
 
 drawIdeo(conn, js, pos, width);
 drawGenes(conn, js, pos, width);
 drawScale(conn, js, pos, width);
@@ -308,9 +377,9 @@
     jsonAddString(js, "pos", pos);
 else
     jsonAddString(js, "pos", "");
 
-struct settings *settings = initSettings(conn, pos, width, height, 0, 0);
+struct settings *settings = initSettings(conn, pos, "settings", width, height, 0, 0);
 
 if (settings)
     {
     struct json *new, *list = jsonAddContainerList(js, "chroms");