src/hg/instinct/hgHeatmap2/hgToolTip.c 1.9

1.9 2009/08/21 22:22:35 jzhu
add patient id to tooltip
Index: src/hg/instinct/hgHeatmap2/hgToolTip.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/hgHeatmap2/hgToolTip.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -b -B -U 1000000 -r1.8 -r1.9
--- src/hg/instinct/hgHeatmap2/hgToolTip.c	4 Jun 2009 03:50:37 -0000	1.8
+++ src/hg/instinct/hgHeatmap2/hgToolTip.c	21 Aug 2009 22:22:35 -0000	1.9
@@ -1,230 +1,255 @@
 /********************************************************************************/
 /* Copyright 2007-2009 -- The Regents of the University of California           */
 /********************************************************************************/
 
 /* hgToolTip.c -- routines that handle the frontend's tool tip requests
  */
 
 #include "common.h"
 #include "bed.h"
 #include "hash.h"
 #include "hdb.h"
 #include "hgHeatmapLib.h"
 #include "heatmapUtility.h"
 #include "featuresLib.h"
 #include "hgHeatmap2.h"
 #include "filterFeatures.h"
 #include "json.h"
 
 static char const rcsid[] = "$Id$";
 
 static char *heatMapDbProfile = "localDb";  // database profile to use
 
 char *averageBed15ForId(struct bed *bedList, int orderId)
 {
 if (!bedList)
     return NULL;
 
 struct bed *nb;
 double val = 0.0, count = 0.0;
 for (nb = bedList; nb; nb = nb->next)
     {
     if (orderId > nb->expCount)
 	return NULL;
 
     val += nb->expScores[orderId];
     count += 1.0;
     }
 
 if (count == 0.0)
     return NULL;
 
 char str[128];
 safef(str, sizeof(str), "%f", val/count);
 
 return cloneString(str);
 }
 
 char *averageBed4(struct bed *bedList)
 {
 if (!bedList)
     return NULL;
 
 struct bed *nb;
 double val = 0.0, count = 0.0;
 
 for (nb = bedList; nb; nb = nb->next)
     {
     char *name = nb->name;
     if (!name)
 	continue;
     if (!isdigit(*name))
 	continue;
     double tmp = atof(name);
     val += tmp;
     count += 1.0;
     }
 
 if (count == 0.0)
     return NULL;
 
 char str[128];
 safef(str, sizeof(str), "%f", val/count);
 return cloneString(str);
 }
 
 
 char *toolTipByChromPos(struct genoHeatmap *gh, char *sampleId, 
 		       char *chrom, int chromStart, int chromStop)
 {
 struct bed *bedList = NULL;
 if (sameString(gh->dataType, "bed 4"))
     {
     bedList = getBedGraphRange(gh, chrom, chromStart, chromStop, 4);
     return averageBed4(bedList);
     }
 else if (sameString(gh->dataType, "bed 15") && gh->sampleOrder)
     {
     int orderId = hashIntValDefault(gh->sampleOrder, sampleId, -1); 
     if (orderId == -1)
 	return NULL;
     
     bedList = getChromHeatmapRange(gh, chrom, chromStart, chromStop, FALSE);
     
     return averageBed15ForId(bedList, orderId);
     }
 
 return NULL;
 }
 
 char *toolTipByProbe(struct genoHeatmap *gh, char *sampleId, char *probeId)
 {
 if (!sameString(gh->dataType, "bed 15") || !gh->sampleOrder)
     return NULL;
 
 int orderId = hashIntValDefault(gh->sampleOrder, sampleId, -1); 
 if (orderId == -1)
     return NULL;
 
 /* remove quotes to avoid sql injection */
 probeId = replaceChars(probeId, "\"", "");
 
 char query[256];
 safef(query, sizeof(query), 
       "select * from %s where name=\"%s\"", 
       gh->name, probeId);
 
 struct sqlConnection *conn = hAllocConnProfile(heatMapDbProfile, gh->database);
 
 char **row = NULL; 
 struct sqlResult *sr = sqlGetResult(conn, query);
 struct bed *nb, *bedList = NULL;
 
 while ((row = sqlNextRow(sr)) != NULL)
     {
     nb = bedLoadN(row+1, 15);
     slAddHead(&bedList, nb);
     }
 sqlFreeResult(&sr);
 hFreeConn(&conn);
 
 return averageBed15ForId(bedList, orderId);
 }
 
 boolean subjectExists(struct sqlConnection *conn, struct column *col, char *sampleId)
 {
 if (!col)
     return FALSE;
 
 if (!col->keyField || !col->table || !sampleId)
     return FALSE;
 
 char query[128];
 safef(query, sizeof(query), "select %s from %s where %s = '%s'",
       col->keyField, col->table, col->keyField, sampleId);
 
 return sqlExists(conn, query);
 }   
 
 char *toolTipByFeature(struct genoHeatmap *gh, char *sampleId, char *feature)
 {
 /* set up connection to table with both patient and sample information */
 char *db = gh->patDb;
 char *raName = gh->raFile;
 char *patField = gh->patField;
 char *patTable = gh->patTable;
 char *sampleField = gh->sampleField;
 
-if (!raName || !db || !patField || !patTable || !sampleField)
+if (!db || !patField || !patTable || !sampleField)
     return NULL;
 struct sqlConnection *conn = hAllocConnProfile(heatMapDbProfile, db);
 
 char query[256];
 safef(query, sizeof(query), 
       "select %s from %s where %s = \"%s\"",
       patField, patTable, sampleField, sampleId);
 
 char *trackId = sqlQuickString(conn, query);
 if (!trackId)
     return NULL;
 
 struct column *col, *colList = getColumns(conn, raName, db);
 
 struct slName *id = slNameNew(trackId); 
 
 char *val = NULL;
 for (col = colList; col; col = col->next)
     {
     if (!sameString(col->name, feature))
 	continue;
 
     if (subjectExists(conn, col, trackId))
 	val = col->cellCodedVal(col, id, conn);
     break;
     }
 
 hFreeConn(&conn);
 return val;
 }
 
+char *toolTipByPatient(struct genoHeatmap *gh, char *sampleId)
+{
+/* set up connection to table with both patient and sample information */
+char *db = gh->patDb;
+//char *raName = gh->raFile;
+char *patField = gh->patField;
+char *patTable = gh->patTable;
+char *sampleField = gh->sampleField;
+
+//if (!raName || !db || !patField || !patTable || !sampleField)
+if (!db || !patField || !patTable || !sampleField)
+    return NULL;
+struct sqlConnection *conn = hAllocConnProfile(heatMapDbProfile, db);
+
+char query[256];
+safef(query, sizeof(query), 
+      "select %s from %s where %s = \"%s\"",
+      patField, patTable, sampleField, sampleId);
+
+char *trackId = sqlQuickString(conn, query);
+hFreeConn(&conn);
+
+return trackId;
+}
+
 char *toolTipGeneTrackByChromPos(char *db, char *tableName, char *chromName,
 				 int chromStart, int chromStop, int maxNumGenes)
 {
 char **row = NULL;
 char query[256];
 
 safef(query, sizeof(query),
       "select DISTINCT name2 from %s where chrom = '%s' and txStart<%d and txEnd>%d",
       tableName, chromName, chromStop, chromStart);
 
 /* get remote database connection */
 struct sqlConnection *conn = hAllocConn(db);
 
 if (!sqlExists(conn, query))
     return NULL;
 
 struct sqlResult *sr = sqlGetResult(conn, query);
 
 struct dyString *dy = newDyString(10);
 
 if ((row = sqlNextRow(sr)) != NULL)
     dyStringPrintf(dy, "%s", row[0]);
 
 int count = 1;
 while ((row = sqlNextRow(sr)) != NULL)
     {
     if (count >= maxNumGenes)
 	{
 	dyStringPrintf(dy, ", ...");
 	break;
 	}
     dyStringPrintf(dy, ", %s", row[0]);
     count++;
     }
 
 sqlFreeResult(&sr);
 hFreeConn(&conn);
 
 return dy->string;
 }