496e6d0ac50bc1eab5f38ebaa0da717119f712d4
tdreszer
  Tue Nov 2 14:45:35 2010 -0700
Added raReadWithFilter to read an ra file and collect a set of related stanza.  Useful for cv.ra processing to get all antibodies or all cells
diff --git src/lib/ra.c src/lib/ra.c
index 86d5bd9..92b9034 100644
--- src/lib/ra.c
+++ src/lib/ra.c
@@ -245,15 +245,49 @@
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
 struct hash *bigHash = hashNew(0);
 struct hash *hash;
 while ((hash = raNextRecord(lf)) != NULL)
     {
     char *key = hashFindVal(hash, keyField);
     if (key == NULL)
         errAbort("Couldn't find key field %s line %d of %s",
 		keyField, lf->lineIx, lf->fileName);
     hashAdd(bigHash, key, hash);
     }
 lineFileClose(&lf);
 return bigHash;
 }
 
+struct hash *raReadWithFilter(char *fileName, char *keyField,char *filterKey,char *filterValue)
+/* Return hash that contains all filtered ra records in file keyed by given field, which must exist.
+ * The values of the hash are themselves hashes.  The filter is a key/value pair that must exist.
+ * Example raReadWithFilter(file,"term","type","antibody"): returns hash of hashes of every term with type=antibody */
+{
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+struct hash *bigHash = hashNew(0);
+struct hash *hash;
+while ((hash = raNextRecord(lf)) != NULL)
+    {
+    char *key = hashFindVal(hash, keyField);
+    if (key == NULL)
+        errAbort("Couldn't find key field %s line %d of %s",
+                keyField, lf->lineIx, lf->fileName);
+    if (filterKey != NULL)
+        {
+        char *filter = hashFindVal(hash, filterKey);
+        if (filter == NULL)
+            {
+            hashFree(&hash);
+            continue;
+            }
+        if (filterValue != NULL && differentString(filterValue,filter))
+            {
+            hashFree(&hash);
+            continue;
+            }
+        }
+        hashAdd(bigHash, key, hash);
+    }
+lineFileClose(&lf);
+return bigHash;
+}
+