src/utils/raSqlQuery/raSqlQuery.c 1.14

1.14 2009/11/20 20:25:54 kent
Doing some stuff to cope with track names having additional fields after the first word.
Index: src/utils/raSqlQuery/raSqlQuery.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/utils/raSqlQuery/raSqlQuery.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -b -B -U 4 -r1.13 -r1.14
--- src/utils/raSqlQuery/raSqlQuery.c	20 Nov 2009 20:04:45 -0000	1.13
+++ src/utils/raSqlQuery/raSqlQuery.c	20 Nov 2009 20:25:54 -0000	1.14
@@ -103,8 +103,31 @@
 	}
     }
 }
 
+struct raField *makeKeyField(struct raRecord *record, char *key, struct lm *lm)
+/* Make up key field if possible from field with name of key.  If not possible then
+ * return NULL.  May have to munge keyField to just include first word. */
+{
+/* See if can find key at all. */
+struct raField *fullKey = raRecordField(record, key);
+if (fullKey == NULL)
+    return NULL;
+
+/* See if it has more than one word by looking for spaces. */
+char *fullKeyVal = fullKey->val;
+char *endFirstWord = skipToSpaces(fullKeyVal);
+if (endFirstWord == NULL)
+    return fullKey;
+
+/* If it does have more than one word, make up a new key. */
+struct raField *shortKey;
+lmAllocVar(lm, shortKey);
+shortKey->name = fullKey->name;
+shortKey->val = lmCloneStringZ(lm, fullKeyVal, endFirstWord - fullKeyVal);
+return shortKey;
+}
+
 static struct raRecord *readRaRecords(int inCount, char *inNames[], 
 	char *mergeField, boolean addFile, struct lm *lm)
 /* Scan through files, merging records on mergeField if it is non-NULL. */
 {
@@ -123,9 +146,9 @@
 	while ((record = raRecordReadOne(lf, lm)) != NULL)
 	    {
 	    if (addFile)
 	        record->posList = raFilePosNew(lm, fileName, lf->lineIx);
-	    struct raField *keyField = raRecordField(record, mergeField);
+	    struct raField *keyField = makeKeyField(record, mergeField, lm);
 	    if (keyField != NULL)
 		{
 		struct raRecord *oldRecord = hashFindVal(recordHash, keyField->val);
 		if (oldRecord != NULL)
@@ -214,16 +237,16 @@
     }
 fprintf(out, "\n");
 }
 
-static void addMissingKeys(struct raRecord *list, char *keyField)
+static void addMissingKeys(struct raRecord *list, char *keyField, struct lm *lm)
 /* Add key to all raRecords that don't already have it. */
 {
 struct raRecord *rec;
 for (rec = list; rec != NULL; rec = rec->next)
     {
     if (rec->key == NULL)
-        rec->key = raRecordField(rec, keyField);
+        rec->key = makeKeyField(rec, keyField, lm);
     }
 }
 
 static struct raRecord *findParent(struct raRecord *rec, 
@@ -280,9 +303,9 @@
 {
 struct raRecord *raList = readRaRecords(inCount, inNames, mergeField, clAddFile, lm);
 if (parentField != NULL)
     {
-    addMissingKeys(raList, clKey);
+    addMissingKeys(raList, clKey, lm);
     inheritFromParents(raList, parentField, noInheritField, lm);
     }
 struct rqlStatement *rql = rqlStatementParse(query);
 verbose(2, "Got %d records in raFiles\n", slCount(raList));