Index: src/hg/instinct/raToDb/raToDb.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/raToDb/raToDb.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -b -B -U 4 -r1.4 -r1.5
--- src/hg/instinct/raToDb/raToDb.c	9 Apr 2010 23:56:03 -0000	1.4
+++ src/hg/instinct/raToDb/raToDb.c	10 Apr 2010 03:53:54 -0000	1.5
@@ -10,9 +10,11 @@
 #include "obscure.h"
 #include "hdb.h"
 #include "sqlList.h"
 #include "hPrint.h"
+#include "hgConfig.h"
 #include "ra.h"
+#include "featuresLib.h"
 #include "hgHeatmapLib.h"
 #include "raDb.h"
 
 static char const rcsid[] = "$Id$";
@@ -311,8 +313,76 @@
     }
 return 1;
 }
 
+int checkClinicalData(struct raDb *ra)
+{
+struct sqlConnection *conn = sqlMayConnectProfile(ra->profile, ra->patDb);
+if (!conn)
+    {
+    printf("FAIL\t%s\tClinical database '%s' does not exist.\n", ra->name, ra->patDb);
+    return 0;
+    }
+
+if (!sqlTableExists(conn, ra->patTable))
+    {
+    printf("FAIL\t%s\tClinical table '%s' does not exist.\n", ra->name, ra->patTable);
+    return 0;
+    }
+
+struct slName *sl, *slList = sqlFieldNames(conn, ra->patTable);
+
+boolean pfExists = FALSE;
+boolean sfExists = FALSE;
+for (sl = slList; sl; sl = sl->next)
+    {
+    if (sameString(sl->name, ra->patField))
+	pfExists = TRUE;
+    if (sameString(sl->name, ra->sampleField))
+	sfExists = TRUE;
+    }
+if (!pfExists)
+    {
+    printf("FAIL\t%s\tPatient Field '%s' not in table '%s'.\n", 
+	   ra->name, ra->patField, ra->patTable);
+    return 0;
+    }
+if (!sfExists)
+    {
+    printf("FAIL\t%s\tSample Field '%s' not in table '%s'.\n", 
+	   ra->name, ra->patField, ra->patTable);
+    return 0;
+    }
+
+struct hash *raHash, *raHashList = readRaFile(ra->raFile);
+
+char query[512];
+for (raHash = raHashList; raHash; raHash = raHash->next)
+    {
+    char *name = hashMustFindVal(raHash, "name");
+    char *queryType = hashMustFindVal(raHash, "type");
+
+    char *type     = cloneString(nextWord(&queryType));
+    if (!sameString(type, "lookup"))
+	continue;  // only checking lookup columns
+    char *table    = cloneString(nextWord(&queryType));
+    char *keyField = cloneString(nextWord(&queryType));
+    char *valField = cloneString(nextWord(&queryType));
+
+    safef(query, sizeof(query), "select %s,%s from %s limit 1", 
+	  keyField, valField, table);
+
+    if (!sqlExists(conn, query))
+	{
+	printf("FAIL\t%s\tValue field '%s' for column %s does not exist.\n", 
+	       ra->name, valField, name);
+	return 0;
+	}
+    }
+
+sqlDisconnect(&conn);
+return 1;
+}
 
 struct raDb *validateRa(struct hash *raHash)
 {
 struct raDb *ra = initRA();
@@ -383,9 +453,14 @@
 ra->patDb       = getOptionalString(raHash, "patDb", NULL);
 ra->patTable    = getOptionalString(raHash, "patTable", NULL);
 ra->patField    = getOptionalString(raHash, "patField", NULL);
 ra->sampleField = getOptionalString(raHash, "sampleField", NULL);
-/*** TODO CHECK PATIENT DB ***/
+
+if (!checkClinicalData(ra))
+    {
+    printf("FAIL\t%s\tClinical data incorrect or missing.\n", ra->name);
+    return NULL;
+    }
 
 /* Platform setting, currently defaults to expression */
 ra->platform   = getOptionalString(raHash, "platform", "expression");
 
@@ -457,9 +532,9 @@
     i++;
     }
 }
 
-void raToDb(char *db, char *tableName, char *raName)
+void raToDb(char *db, char *raName)
 /* raToDb - RA to database table converter RA to database converter. */
 {
 struct hash *raHash, *raHashList = readRaFile(raName);
 
@@ -475,18 +550,19 @@
     }
 
 struct sqlConnection *conn = hAllocConnProfile(localDbProfile, db);
 
-putDataToTable(conn, tableName, raList);
+char *raDbName = raDbPath();
+putDataToTable(conn, raDbName, raList);
 
 hFreeConn(&conn);
 }
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 optionInit(&argc, argv, options);
-if (argc != 4)
+if (argc != 3)
     usage();
-raToDb(argv[1], argv[2], argv[3]);
+raToDb(argv[1], argv[2]);
 return 0;
 }

