src/hg/utils/tdbQuery/tdbQuery.c 1.9

1.9 2009/12/03 08:59:33 kent
Adding in visibility.ra and priority.ra handling.
Index: src/hg/utils/tdbQuery/tdbQuery.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/utils/tdbQuery/tdbQuery.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -b -B -U 4 -r1.8 -r1.9
--- src/hg/utils/tdbQuery/tdbQuery.c	3 Dec 2009 04:42:28 -0000	1.8
+++ src/hg/utils/tdbQuery/tdbQuery.c	3 Dec 2009 08:59:33 -0000	1.9
@@ -64,9 +64,10 @@
    {"alpha", OPTION_BOOLEAN},
    {NULL, 0},
 };
 
-#define glParentField "subTrack"
+#define glKeyField "track"	 /* The field that has the record ID */
+#define glParentField "subTrack" /* The field that points to the parent. */
 
 void recordLocationReport(struct tdbRecord *rec, FILE *out)
 /* Write out where record ends. */
 {
@@ -331,9 +332,9 @@
 {
 struct tdbRecord *record;
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
 char *lmFileName = lmCloneString(lm, fileName);
-while ((record = tdbRecordReadOne(lf, "track", lm)) != NULL)
+while ((record = tdbRecordReadOne(lf, glKeyField, lm)) != NULL)
     {
     struct tdbField *firstField = record->fieldList;
     if (sameString(firstField->name, "include"))
 	{
@@ -405,8 +406,72 @@
     }
 old->posList = slCat(old->posList, record->posList);
 }
 
+static void overrideFieldFromFile(struct tdbRecord *recordList, 
+	char *raFile, char *fieldName, struct lm *lm)
+/* Look for raFile in assembly and organism directories in that order.  Use the first
+ * file that you find to override the given field inside of recordList. */
+{
+/* Build up hash of recordList. */
+struct hash *hash = hashNew(0);
+struct tdbRecord *record;
+for (record = recordList; record != NULL; record = record->next)
+    hashAdd(hash, record->key, record);
+
+struct lineFile *lf = lineFileOpen(raFile, TRUE);
+while ((record = tdbRecordReadOne(lf, glKeyField, lm)) != NULL)
+    {
+    struct tdbRecord *oldRecord = hashFindVal(hash, record->key);
+    if (oldRecord == NULL)
+        {
+	continue;
+	}
+    if (slCount(record->fieldList) != 2)
+        {
+	errAbort("Expecting just two fields, track and %s, got %d in record ending line %d of %s",
+		fieldName, slCount(record->fieldList), lf->lineIx, lf->fileName);
+	}
+    struct tdbField *field = tdbRecordField(record, fieldName);
+    if (field == NULL)
+        {
+	errAbort("Missing %s tag in record ending line %d of %s", fieldName,
+		lf->lineIx, lf->fileName);
+	}
+    mergeRecords(oldRecord, record, glKeyField, lm);
+    }
+lineFileClose(&lf);
+hashFree(&hash);
+}
+
+static void overrideFieldFromFileOnPath(struct tdbRecord *recordList, struct dbPath *p,
+	char *raFile, char *field, struct lm *lm)
+/* Look for raFile in assembly and organism directories in that order.  Use the first
+ * file that you find to override the given field inside of recordList. */
+{
+/* Find raFile. */
+char path[PATH_LEN];
+safef(path, sizeof(path), "%s/%s", p->dir, raFile);
+if (!fileExists(path))
+    {
+    char orgDir[PATH_LEN];
+    splitPath(p->dir, orgDir, NULL, NULL);
+    safef(path, sizeof(path), "%s%s", orgDir, raFile);
+    if (!fileExists(path))
+        return;		/* Nothing to do. */
+    }
+
+overrideFieldFromFile(recordList, path, field, lm);
+}
+
+static void overridePrioritiesAndVisibilities(struct tdbRecord *recordList, struct dbPath *p,
+	struct lm *lm)
+/* Look for visibility.ra and priority.ra files and layer them onto recordList. */
+{
+overrideFieldFromFileOnPath(recordList, p, "visibility.ra", "visibility", lm);
+overrideFieldFromFileOnPath(recordList, p, "priority.ra", "priority", lm);
+}
+
 static int parentChildFileDistance(struct tdbRecord *parent, struct tdbRecord *child)
 /* Return distance of two records.  If they're in different files the
  * distance gets pretty big.  Would be flaky on records split across
  * different files, hence the ad-hoc in the name.  Not worth implementing
@@ -544,9 +609,9 @@
 		oldRecord->view = record->view;
 		oldRecord->viewHash = record->viewHash;
 		}
 	    else
-		mergeRecords(oldRecord, record, key, lm);
+		mergeRecords(oldRecord, record, glKeyField, lm);
 	    }
 	else
 	    {
 	    hashAdd(recordHash, record->key, record);
@@ -734,8 +799,10 @@
     recordList = filterOnRelease(recordList, clAlpha);
     verbose(2, "After filterOnRelease %d records\n", slCount(recordList));
     checkDupeKeys(recordList, FALSE);
 
+    overridePrioritiesAndVisibilities(recordList, p, lm);
+
     struct tdbRecord *record;
     boolean doSelect = sameString(rql->command, "select");
     for (record = recordList; record != NULL; record = record->next)
 	{