src/hg/lib/chromInfo.c 1.7

1.7 2009/03/24 15:51:04 mikep
make library function to read all chromInfo, and add chromDb option to validateFiles to get chromInfo from DB
Index: src/hg/lib/chromInfo.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/lib/chromInfo.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -b -B -U 4 -r1.6 -r1.7
--- src/hg/lib/chromInfo.c	26 Nov 2007 22:39:44 -0000	1.6
+++ src/hg/lib/chromInfo.c	24 Mar 2009 15:51:04 -0000	1.7
@@ -5,8 +5,9 @@
 #include "common.h"
 #include "linefile.h"
 #include "dystring.h"
 #include "jksql.h"
+#include "hdb.h"
 #include "chromInfo.h"
 
 static char const rcsid[] = "$Id$";
 
@@ -159,4 +160,50 @@
         }
     }
 return exists;
 }
+
+
+struct chromInfo *createChromInfoList(char *name, char *database)
+/* Load up chromosome information for chrom 'name'.
+ * If name is NULL or "all" then load all chroms.
+ * Similar to featureBits.c - could be moved to library */
+{
+struct sqlConnection *conn = hAllocConn(database);
+struct sqlResult *sr = NULL;
+char **row;
+int loaded=0;
+struct chromInfo *ret = NULL;
+unsigned totalSize = 0;
+/* do the query */
+if (!name || sameWord(name, "all"))
+    sr = sqlGetResult(conn, "select * from chromInfo");
+else
+    {
+    char select[256];
+    safef(select, ArraySize(select), "select * from chromInfo where chrom='%s'", name);
+    sr = sqlGetResult(conn, select);
+    }
+/* read the rows and build the chromInfo list */
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    struct chromInfo *el;
+    struct chromInfo *ci;
+    AllocVar(ci);
+    el = chromInfoLoad(row);
+    ci->chrom = cloneString(el->chrom);
+    ci->size = el->size;
+    totalSize += el->size;
+    slAddHead(&ret, ci);
+    ++loaded;
+    }
+if (loaded < 1)
+    errAbort("ERROR: can not find chrom name: '%s'\n", name ? name : "NULL");
+slReverse(&ret);
+if (!name || sameWord(name, "all"))
+    verbose(2, "#\tloaded size info for %d chroms, total size: %u\n",
+        loaded, totalSize);
+sqlFreeResult(&sr);
+hFreeConn(&conn);
+return ret;
+}
+