src/hg/instinct/bioInt2/bioIntDb.c 1.8
1.8 2009/04/30 19:54:28 jsanborn
added factor graph pipeline and code
Index: src/hg/instinct/bioInt2/bioIntDb.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntDb.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -b -B -U 4 -r1.7 -r1.8
--- src/hg/instinct/bioInt2/bioIntDb.c 27 Apr 2009 06:15:48 -0000 1.7
+++ src/hg/instinct/bioInt2/bioIntDb.c 30 Apr 2009 19:54:28 -0000 1.8
@@ -3134,8 +3134,181 @@
fprintf(f, "%g", el->conf);
fputc(lastSep,f);
}
+void pathwayValsStaticLoad(char **row, struct pathwayVals *ret)
+/* Load a row from pathwayVals table into ret. The contents of ret will
+ * be replaced at the next call to this function. */
+{
+
+ret->pathway_id = sqlUnsigned(row[0]);
+ret->sample_id = sqlUnsigned(row[1]);
+ret->feature_id = sqlUnsigned(row[2]);
+ret->val = sqlFloat(row[3]);
+ret->conf = sqlFloat(row[4]);
+}
+
+struct pathwayVals *pathwayValsLoad(char **row)
+/* Load a pathwayVals from row fetched with select * from pathwayVals
+ * from database. Dispose of this with pathwayValsFree(). */
+{
+struct pathwayVals *ret;
+
+AllocVar(ret);
+ret->pathway_id = sqlUnsigned(row[0]);
+ret->sample_id = sqlUnsigned(row[1]);
+ret->feature_id = sqlUnsigned(row[2]);
+ret->val = sqlFloat(row[3]);
+ret->conf = sqlFloat(row[4]);
+return ret;
+}
+
+struct pathwayVals *pathwayValsLoadAll(char *fileName)
+/* Load all pathwayVals from a whitespace-separated file.
+ * Dispose of this with pathwayValsFreeList(). */
+{
+struct pathwayVals *list = NULL, *el;
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+char *row[5];
+
+while (lineFileRow(lf, row))
+ {
+ el = pathwayValsLoad(row);
+ slAddHead(&list, el);
+ }
+lineFileClose(&lf);
+slReverse(&list);
+return list;
+}
+
+struct pathwayVals *pathwayValsLoadAllByChar(char *fileName, char chopper)
+/* Load all pathwayVals from a chopper separated file.
+ * Dispose of this with pathwayValsFreeList(). */
+{
+struct pathwayVals *list = NULL, *el;
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+char *row[5];
+
+while (lineFileNextCharRow(lf, chopper, row, ArraySize(row)))
+ {
+ el = pathwayValsLoad(row);
+ slAddHead(&list, el);
+ }
+lineFileClose(&lf);
+slReverse(&list);
+return list;
+}
+
+struct pathwayVals *pathwayValsLoadByQuery(struct sqlConnection *conn, char *query)
+/* Load all pathwayVals 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 pathwayValsFreeList(). */
+{
+struct pathwayVals *list = NULL, *el;
+struct sqlResult *sr;
+char **row;
+
+sr = sqlGetResult(conn, query);
+while ((row = sqlNextRow(sr)) != NULL)
+ {
+ el = pathwayValsLoad(row);
+ slAddHead(&list, el);
+ }
+slReverse(&list);
+sqlFreeResult(&sr);
+return list;
+}
+
+void pathwayValsSaveToDb(struct sqlConnection *conn, struct pathwayVals *el, char *tableName, int updateSize)
+/* Save pathwayVals 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. Note that strings must be escaped to allow insertion into the database.
+ * For example "autosql's features include" --> "autosql\'s features include"
+ * If worried about this use pathwayValsSaveToDbEscaped() */
+{
+struct dyString *update = newDyString(updateSize);
+dyStringPrintf(update, "insert into %s values ( %u,%u,%u,%g,%g)",
+ tableName, el->pathway_id, el->sample_id, el->feature_id, el->val, el->conf);
+sqlUpdate(conn, update->string);
+freeDyString(&update);
+}
+
+void pathwayValsSaveToDbEscaped(struct sqlConnection *conn, struct pathwayVals *el, char *tableName, int updateSize)
+/* Save pathwayVals 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. Automatically
+ * escapes all simple strings (not arrays of string) but may be slower than pathwayValsSaveToDb().
+ * For example automatically copies and converts:
+ * "autosql's features include" --> "autosql\'s features include"
+ * before inserting into database. */
+{
+struct dyString *update = newDyString(updateSize);
+dyStringPrintf(update, "insert into %s values ( %u,%u,%u,%g,%g)",
+ tableName, el->pathway_id, el->sample_id, el->feature_id, el->val, el->conf);
+sqlUpdate(conn, update->string);
+freeDyString(&update);
+}
+
+struct pathwayVals *pathwayValsCommaIn(char **pS, struct pathwayVals *ret)
+/* Create a pathwayVals out of a comma separated string.
+ * This will fill in ret if non-null, otherwise will
+ * return a new pathwayVals */
+{
+char *s = *pS;
+
+if (ret == NULL)
+ AllocVar(ret);
+ret->pathway_id = sqlUnsignedComma(&s);
+ret->sample_id = sqlUnsignedComma(&s);
+ret->feature_id = sqlUnsignedComma(&s);
+ret->val = sqlFloatComma(&s);
+ret->conf = sqlFloatComma(&s);
+*pS = s;
+return ret;
+}
+
+void pathwayValsFree(struct pathwayVals **pEl)
+/* Free a single dynamically allocated pathwayVals such as created
+ * with pathwayValsLoad(). */
+{
+struct pathwayVals *el;
+
+if ((el = *pEl) == NULL) return;
+freez(pEl);
+}
+
+void pathwayValsFreeList(struct pathwayVals **pList)
+/* Free a list of dynamically allocated pathwayVals's */
+{
+struct pathwayVals *el, *next;
+
+for (el = *pList; el != NULL; el = next)
+ {
+ next = el->next;
+ pathwayValsFree(&el);
+ }
+*pList = NULL;
+}
+
+void pathwayValsOutput(struct pathwayVals *el, FILE *f, char sep, char lastSep)
+/* Print out pathwayVals. Separate fields with sep. Follow last field with lastSep. */
+{
+fprintf(f, "%u", el->pathway_id);
+fputc(sep,f);
+fprintf(f, "%u", el->sample_id);
+fputc(sep,f);
+fprintf(f, "%u", el->feature_id);
+fputc(sep,f);
+fprintf(f, "%g", el->val);
+fputc(sep,f);
+fprintf(f, "%g", el->conf);
+fputc(lastSep,f);
+}
+
void cohortCorrStaticLoad(char **row, struct cohortCorr *ret)
/* Load a row from cohortCorr table into ret. The contents of ret will
* be replaced at the next call to this function. */
{