0704feba1b95da51d9bb71b54833196d0954695f
kate
  Wed May 28 13:54:55 2014 -0700
DNase Combined track from ENCODE Analysis, with new data format (BED5Sources).  Similar to factorSource, but more general and lighter-weight. refs #13230.
diff --git src/hg/lib/bed5Sources.c src/hg/lib/bed5Sources.c
new file mode 100644
index 0000000..dbc4506
--- /dev/null
+++ src/hg/lib/bed5Sources.c
@@ -0,0 +1,208 @@
+/* bed5Sources.c was originally generated by the autoSql program, which also 
+ * generated bed5Sources.h and bed5Sources.sql.  This module links the database and
+ * the RAM representation of objects. */
+
+#include "common.h"
+#include "linefile.h"
+#include "dystring.h"
+#include "jksql.h"
+#include "bed5Sources.h"
+
+
+
+char *bed5SourcesCommaSepFieldNames = "bin,chrom,chromStart,chromEnd,name,score,floatScore,sourceCount,sourceIds";
+
+struct bed5Sources *bed5SourcesLoadByQuery(struct sqlConnection *conn, char *query)
+/* Load all bed5Sources from table that satisfy the query given.  
+ * Where query is of the form 'select * from example where something=something'
+ * or 'select example.* from example, anotherTable where example.something = 
+ * anotherTable.something'.
+ * Dispose of this with bed5SourcesFreeList(). */
+{
+struct bed5Sources *list = NULL, *el;
+struct sqlResult *sr;
+char **row;
+
+sr = sqlGetResult(conn, query);
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    el = bed5SourcesLoad(row);
+    slAddHead(&list, el);
+    }
+slReverse(&list);
+sqlFreeResult(&sr);
+return list;
+}
+
+void bed5SourcesSaveToDb(struct sqlConnection *conn, struct bed5Sources *el, char *tableName, int updateSize)
+/* Save bed5Sources as a row to the table specified by tableName. 
+ * As blob fields may be arbitrary size updateSize specifies the approx size
+ * of a string that would contain the entire query. Arrays of native types are
+ * converted to comma separated strings and loaded as such, User defined types are
+ * inserted as NULL. This function automatically escapes quoted strings for mysql. */
+{
+struct dyString *update = newDyString(updateSize);
+char  *sourceIdsArray;
+sourceIdsArray = sqlUnsignedArrayToString(el->sourceIds, el->sourceCount);
+sqlDyStringPrintf(update, "insert into %s values ( %u,'%s',%u,%u,'%s',%u,%g,%u,'%s')", 
+	tableName,  el->bin,  el->chrom,  el->chromStart,  el->chromEnd,  el->name,  el->score,  el->floatScore,  el->sourceCount,  sourceIdsArray );
+sqlUpdate(conn, update->string);
+freeDyString(&update);
+freez(&sourceIdsArray);
+}
+
+struct bed5Sources *bed5SourcesLoad(char **row)
+/* Load a bed5Sources from row fetched with select * from bed5Sources
+ * from database.  Dispose of this with bed5SourcesFree(). */
+{
+struct bed5Sources *ret;
+
+AllocVar(ret);
+ret->sourceCount = sqlUnsigned(row[7]);
+ret->bin = sqlUnsigned(row[0]);
+ret->chrom = cloneString(row[1]);
+ret->chromStart = sqlUnsigned(row[2]);
+ret->chromEnd = sqlUnsigned(row[3]);
+ret->name = cloneString(row[4]);
+ret->score = sqlUnsigned(row[5]);
+ret->floatScore = sqlFloat(row[6]);
+{
+int sizeOne;
+sqlUnsignedDynamicArray(row[8], &ret->sourceIds, &sizeOne);
+assert(sizeOne == ret->sourceCount);
+}
+return ret;
+}
+
+struct bed5Sources *bed5SourcesLoadAll(char *fileName) 
+/* Load all bed5Sources from a whitespace-separated file.
+ * Dispose of this with bed5SourcesFreeList(). */
+{
+struct bed5Sources *list = NULL, *el;
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+char *row[9];
+
+while (lineFileRow(lf, row))
+    {
+    el = bed5SourcesLoad(row);
+    slAddHead(&list, el);
+    }
+lineFileClose(&lf);
+slReverse(&list);
+return list;
+}
+
+struct bed5Sources *bed5SourcesLoadAllByChar(char *fileName, char chopper) 
+/* Load all bed5Sources from a chopper separated file.
+ * Dispose of this with bed5SourcesFreeList(). */
+{
+struct bed5Sources *list = NULL, *el;
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+char *row[9];
+
+while (lineFileNextCharRow(lf, chopper, row, ArraySize(row)))
+    {
+    el = bed5SourcesLoad(row);
+    slAddHead(&list, el);
+    }
+lineFileClose(&lf);
+slReverse(&list);
+return list;
+}
+
+struct bed5Sources *bed5SourcesCommaIn(char **pS, struct bed5Sources *ret)
+/* Create a bed5Sources out of a comma separated string. 
+ * This will fill in ret if non-null, otherwise will
+ * return a new bed5Sources */
+{
+char *s = *pS;
+
+if (ret == NULL)
+    AllocVar(ret);
+ret->bin = sqlUnsignedComma(&s);
+ret->chrom = sqlStringComma(&s);
+ret->chromStart = sqlUnsignedComma(&s);
+ret->chromEnd = sqlUnsignedComma(&s);
+ret->name = sqlStringComma(&s);
+ret->score = sqlUnsignedComma(&s);
+ret->floatScore = sqlFloatComma(&s);
+ret->sourceCount = sqlUnsignedComma(&s);
+{
+int i;
+s = sqlEatChar(s, '{');
+AllocArray(ret->sourceIds, ret->sourceCount);
+for (i=0; i<ret->sourceCount; ++i)
+    {
+    ret->sourceIds[i] = sqlUnsignedComma(&s);
+    }
+s = sqlEatChar(s, '}');
+s = sqlEatChar(s, ',');
+}
+*pS = s;
+return ret;
+}
+
+void bed5SourcesFree(struct bed5Sources **pEl)
+/* Free a single dynamically allocated bed5Sources such as created
+ * with bed5SourcesLoad(). */
+{
+struct bed5Sources *el;
+
+if ((el = *pEl) == NULL) return;
+freeMem(el->chrom);
+freeMem(el->name);
+freeMem(el->sourceIds);
+freez(pEl);
+}
+
+void bed5SourcesFreeList(struct bed5Sources **pList)
+/* Free a list of dynamically allocated bed5Sources's */
+{
+struct bed5Sources *el, *next;
+
+for (el = *pList; el != NULL; el = next)
+    {
+    next = el->next;
+    bed5SourcesFree(&el);
+    }
+*pList = NULL;
+}
+
+void bed5SourcesOutput(struct bed5Sources *el, FILE *f, char sep, char lastSep) 
+/* Print out bed5Sources.  Separate fields with sep. Follow last field with lastSep. */
+{
+fprintf(f, "%u", el->bin);
+fputc(sep,f);
+if (sep == ',') fputc('"',f);
+fprintf(f, "%s", el->chrom);
+if (sep == ',') fputc('"',f);
+fputc(sep,f);
+fprintf(f, "%u", el->chromStart);
+fputc(sep,f);
+fprintf(f, "%u", el->chromEnd);
+fputc(sep,f);
+if (sep == ',') fputc('"',f);
+fprintf(f, "%s", el->name);
+if (sep == ',') fputc('"',f);
+fputc(sep,f);
+fprintf(f, "%u", el->score);
+fputc(sep,f);
+fprintf(f, "%g", el->floatScore);
+fputc(sep,f);
+fprintf(f, "%u", el->sourceCount);
+fputc(sep,f);
+{
+int i;
+if (sep == ',') fputc('{',f);
+for (i=0; i<el->sourceCount; ++i)
+    {
+    fprintf(f, "%u", el->sourceIds[i]);
+    fputc(',', f);
+    }
+if (sep == ',') fputc('}',f);
+}
+fputc(lastSep,f);
+}
+
+/* -------------------------------- End autoSql Generated Code -------------------------------- */
+