src/hg/instinct/bioInt2/bioIntDb.c 1.3

1.3 2009/03/22 01:07:28 jsanborn
updated
Index: src/hg/instinct/bioInt2/bioIntDb.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioIntDb.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -b -B -U 4 -r1.2 -r1.3
--- src/hg/instinct/bioInt2/bioIntDb.c	21 Mar 2009 19:54:10 -0000	1.2
+++ src/hg/instinct/bioInt2/bioIntDb.c	22 Mar 2009 01:07:28 -0000	1.3
@@ -9,31 +9,8 @@
 #include "bioIntDb.h"
 
 static char const rcsid[] = "$Id$";
 
-void sqlFloatDynamicArrayFixedSize(char *s, float **retArray, int size)
-/* Convert comma separated list of numbers to an dynamically allocated
- * array, which should be freeMem()'d when done. */
-{
-float *array = NULL;
-unsigned count = 0;
-
-AllocArray(array, size);
-for (;;)
-    {
-    char *e;
-    if (s == NULL || s[0] == 0 || count == size)
-	break;
-    e = strchr(s, ',');
-    if (e != NULL)
-	*e++ = 0;
-
-    array[count++] = atof(s);
-    s = e;
-    }
-*retArray = array;
-}  
-
 void pathwaysStaticLoad(char **row, struct pathways *ret)
 /* Load a row from pathways table into ret.  The contents of ret will
  * be replaced at the next call to this function. */
 {
@@ -2084,12 +2061,11 @@
 AllocVar(ret);
 ret->sample_count = sqlUnsigned(row[1]);
 ret->probe_id = sqlUnsigned(row[0]);
 {
-sqlFloatDynamicArrayFixedSize(row[2], &ret->sample_data, ret->sample_count);
-//int sizeOne;
-//sqlFloatDynamicArray(row[2], &ret->sample_data, &sizeOne);
-//assert(sizeOne == ret->sample_count);
+int sizeOne;
+sqlFloatDynamicArray(row[2], &ret->sample_data, &sizeOne);
+assert(sizeOne == ret->sample_count);
 }
 return ret;
 }
 
@@ -2820,10 +2796,11 @@
 {
 
 ret->id = sqlUnsigned(row[0]);
 ret->cohort_id = sqlUnsigned(row[1]);
-ret->module = row[2];
+ret->module_id = sqlUnsigned(row[2]);
 ret->result_table = row[3];
+ret->input_tables = row[4];
 }
 
 struct analyses *analysesLoad(char **row)
 /* Load a analyses from row fetched with select * from analyses
@@ -2833,10 +2810,11 @@
 
 AllocVar(ret);
 ret->id = sqlUnsigned(row[0]);
 ret->cohort_id = sqlUnsigned(row[1]);
-ret->module = cloneString(row[2]);
+ret->module_id = sqlUnsigned(row[2]);
 ret->result_table = cloneString(row[3]);
+ret->input_tables = cloneString(row[4]);
 return ret;
 }
 
 struct analyses *analysesLoadAll(char *fileName) 
@@ -2844,9 +2822,9 @@
  * Dispose of this with analysesFreeList(). */
 {
 struct analyses *list = NULL, *el;
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
-char *row[4];
+char *row[5];
 
 while (lineFileRow(lf, row))
     {
     el = analysesLoad(row);
@@ -2862,9 +2840,9 @@
  * Dispose of this with analysesFreeList(). */
 {
 struct analyses *list = NULL, *el;
 struct lineFile *lf = lineFileOpen(fileName, TRUE);
-char *row[4];
+char *row[5];
 
 while (lineFileNextCharRow(lf, chopper, row, ArraySize(row)))
     {
     el = analysesLoad(row);
@@ -2906,10 +2884,10 @@
  * For example "autosql's features include" --> "autosql\'s features include" 
  * If worried about this use analysesSaveToDbEscaped() */
 {
 struct dyString *update = newDyString(updateSize);
-dyStringPrintf(update, "insert into %s values ( %u,%u,'%s','%s')", 
-	tableName,  el->id,  el->cohort_id,  el->module,  el->result_table);
+dyStringPrintf(update, "insert into %s values ( %u,%u,%u,'%s',%s)", 
+	tableName,  el->id,  el->cohort_id,  el->module_id,  el->result_table,  el->input_tables);
 sqlUpdate(conn, update->string);
 freeDyString(&update);
 }
 
@@ -2922,18 +2900,18 @@
  * "autosql's features include" --> "autosql\'s features include" 
  * before inserting into database. */ 
 {
 struct dyString *update = newDyString(updateSize);
-char  *module, *result_table;
-module = sqlEscapeString(el->module);
+char  *result_table, *input_tables;
 result_table = sqlEscapeString(el->result_table);
+input_tables = sqlEscapeString(el->input_tables);
 
-dyStringPrintf(update, "insert into %s values ( %u,%u,'%s','%s')", 
-	tableName,  el->id,  el->cohort_id,  module,  result_table);
+dyStringPrintf(update, "insert into %s values ( %u,%u,%u,'%s','%s')", 
+	tableName,  el->id,  el->cohort_id,  el->module_id,  result_table,  input_tables);
 sqlUpdate(conn, update->string);
 freeDyString(&update);
-freez(&module);
 freez(&result_table);
+freez(&input_tables);
 }
 
 struct analyses *analysesCommaIn(char **pS, struct analyses *ret)
 /* Create a analyses out of a comma separated string. 
@@ -2945,10 +2923,11 @@
 if (ret == NULL)
     AllocVar(ret);
 ret->id = sqlUnsignedComma(&s);
 ret->cohort_id = sqlUnsignedComma(&s);
-ret->module = sqlStringComma(&s);
+ret->module_id = sqlUnsignedComma(&s);
 ret->result_table = sqlStringComma(&s);
+ret->input_tables = sqlStringComma(&s);
 *pS = s;
 return ret;
 }
 
@@ -2958,10 +2937,10 @@
 {
 struct analyses *el;
 
 if ((el = *pEl) == NULL) return;
-freeMem(el->module);
 freeMem(el->result_table);
+freeMem(el->input_tables);
 freez(pEl);
 }
 
 void analysesFreeList(struct analyses **pList)
@@ -2983,14 +2962,191 @@
 fprintf(f, "%u", el->id);
 fputc(sep,f);
 fprintf(f, "%u", el->cohort_id);
 fputc(sep,f);
+fprintf(f, "%u", el->module_id);
+fputc(sep,f);
 if (sep == ',') fputc('"',f);
-fprintf(f, "%s", el->module);
+fprintf(f, "%s", el->result_table);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
-fprintf(f, "%s", el->result_table);
+fprintf(f, "%s", el->input_tables);
+if (sep == ',') fputc('"',f);
+fputc(lastSep,f);
+}
+
+void analysisModulesStaticLoad(char **row, struct analysisModules *ret)
+/* Load a row from analysisModules table into ret.  The contents of ret will
+ * be replaced at the next call to this function. */
+{
+
+ret->id = sqlUnsigned(row[0]);
+ret->name = row[1];
+ret->type = row[2];
+}
+
+struct analysisModules *analysisModulesLoad(char **row)
+/* Load a analysisModules from row fetched with select * from analysisModules
+ * from database.  Dispose of this with analysisModulesFree(). */
+{
+struct analysisModules *ret;
+
+AllocVar(ret);
+ret->id = sqlUnsigned(row[0]);
+ret->name = cloneString(row[1]);
+ret->type = cloneString(row[2]);
+return ret;
+}
+
+struct analysisModules *analysisModulesLoadAll(char *fileName) 
+/* Load all analysisModules from a whitespace-separated file.
+ * Dispose of this with analysisModulesFreeList(). */
+{
+struct analysisModules *list = NULL, *el;
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+char *row[3];
+
+while (lineFileRow(lf, row))
+    {
+    el = analysisModulesLoad(row);
+    slAddHead(&list, el);
+    }
+lineFileClose(&lf);
+slReverse(&list);
+return list;
+}
+
+struct analysisModules *analysisModulesLoadAllByChar(char *fileName, char chopper) 
+/* Load all analysisModules from a chopper separated file.
+ * Dispose of this with analysisModulesFreeList(). */
+{
+struct analysisModules *list = NULL, *el;
+struct lineFile *lf = lineFileOpen(fileName, TRUE);
+char *row[3];
+
+while (lineFileNextCharRow(lf, chopper, row, ArraySize(row)))
+    {
+    el = analysisModulesLoad(row);
+    slAddHead(&list, el);
+    }
+lineFileClose(&lf);
+slReverse(&list);
+return list;
+}
+
+struct analysisModules *analysisModulesLoadByQuery(struct sqlConnection *conn, char *query)
+/* Load all analysisModules 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 analysisModulesFreeList(). */
+{
+struct analysisModules *list = NULL, *el;
+struct sqlResult *sr;
+char **row;
+
+sr = sqlGetResult(conn, query);
+while ((row = sqlNextRow(sr)) != NULL)
+    {
+    el = analysisModulesLoad(row);
+    slAddHead(&list, el);
+    }
+slReverse(&list);
+sqlFreeResult(&sr);
+return list;
+}
+
+void analysisModulesSaveToDb(struct sqlConnection *conn, struct analysisModules *el, char *tableName, int updateSize)
+/* Save analysisModules 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 analysisModulesSaveToDbEscaped() */
+{
+struct dyString *update = newDyString(updateSize);
+dyStringPrintf(update, "insert into %s values ( %u,'%s','%s')", 
+	tableName,  el->id,  el->name,  el->type);
+sqlUpdate(conn, update->string);
+freeDyString(&update);
+}
+
+void analysisModulesSaveToDbEscaped(struct sqlConnection *conn, struct analysisModules *el, char *tableName, int updateSize)
+/* Save analysisModules 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 analysisModulesSaveToDb().
+ * For example automatically copies and converts: 
+ * "autosql's features include" --> "autosql\'s features include" 
+ * before inserting into database. */ 
+{
+struct dyString *update = newDyString(updateSize);
+char  *name, *type;
+name = sqlEscapeString(el->name);
+type = sqlEscapeString(el->type);
+
+dyStringPrintf(update, "insert into %s values ( %u,'%s','%s')", 
+	tableName,  el->id,  name,  type);
+sqlUpdate(conn, update->string);
+freeDyString(&update);
+freez(&name);
+freez(&type);
+}
+
+struct analysisModules *analysisModulesCommaIn(char **pS, struct analysisModules *ret)
+/* Create a analysisModules out of a comma separated string. 
+ * This will fill in ret if non-null, otherwise will
+ * return a new analysisModules */
+{
+char *s = *pS;
+
+if (ret == NULL)
+    AllocVar(ret);
+ret->id = sqlUnsignedComma(&s);
+ret->name = sqlStringComma(&s);
+ret->type = sqlStringComma(&s);
+*pS = s;
+return ret;
+}
+
+void analysisModulesFree(struct analysisModules **pEl)
+/* Free a single dynamically allocated analysisModules such as created
+ * with analysisModulesLoad(). */
+{
+struct analysisModules *el;
+
+if ((el = *pEl) == NULL) return;
+freeMem(el->name);
+freeMem(el->type);
+freez(pEl);
+}
+
+void analysisModulesFreeList(struct analysisModules **pList)
+/* Free a list of dynamically allocated analysisModules's */
+{
+struct analysisModules *el, *next;
+
+for (el = *pList; el != NULL; el = next)
+    {
+    next = el->next;
+    analysisModulesFree(&el);
+    }
+*pList = NULL;
+}
+
+void analysisModulesOutput(struct analysisModules *el, FILE *f, char sep, char lastSep) 
+/* Print out analysisModules.  Separate fields with sep. Follow last field with lastSep. */
+{
+fprintf(f, "%u", el->id);
+fputc(sep,f);
+if (sep == ',') fputc('"',f);
+fprintf(f, "%s", el->name);
+if (sep == ',') fputc('"',f);
+fputc(sep,f);
+if (sep == ',') fputc('"',f);
+fprintf(f, "%s", el->type);
 if (sep == ',') fputc('"',f);
 fputc(lastSep,f);
 }
 
@@ -2999,9 +3155,9 @@
  * be replaced at the next call to this function. */
 {
 
 ret->analysis_id = sqlUnsigned(row[0]);
-ret->key = row[1];
+ret->name = row[1];
 ret->val = row[2];
 }
 
 struct analysisParams *analysisParamsLoad(char **row)
@@ -3011,9 +3167,9 @@
 struct analysisParams *ret;
 
 AllocVar(ret);
 ret->analysis_id = sqlUnsigned(row[0]);
-ret->key = cloneString(row[1]);
+ret->name = cloneString(row[1]);
 ret->val = cloneString(row[2]);
 return ret;
 }
 
@@ -3085,9 +3241,9 @@
  * If worried about this use analysisParamsSaveToDbEscaped() */
 {
 struct dyString *update = newDyString(updateSize);
 dyStringPrintf(update, "insert into %s values ( %u,'%s','%s')", 
-	tableName,  el->analysis_id,  el->key,  el->val);
+	tableName,  el->analysis_id,  el->name,  el->val);
 sqlUpdate(conn, update->string);
 freeDyString(&update);
 }
 
@@ -3100,17 +3256,17 @@
  * "autosql's features include" --> "autosql\'s features include" 
  * before inserting into database. */ 
 {
 struct dyString *update = newDyString(updateSize);
-char  *key, *val;
-key = sqlEscapeString(el->key);
+char  *name, *val;
+name = sqlEscapeString(el->name);
 val = sqlEscapeString(el->val);
 
 dyStringPrintf(update, "insert into %s values ( %u,'%s','%s')", 
-	tableName,  el->analysis_id,  key,  val);
+	tableName,  el->analysis_id,  name,  val);
 sqlUpdate(conn, update->string);
 freeDyString(&update);
-freez(&key);
+freez(&name);
 freez(&val);
 }
 
 struct analysisParams *analysisParamsCommaIn(char **pS, struct analysisParams *ret)
@@ -3122,9 +3278,9 @@
 
 if (ret == NULL)
     AllocVar(ret);
 ret->analysis_id = sqlUnsignedComma(&s);
-ret->key = sqlStringComma(&s);
+ret->name = sqlStringComma(&s);
 ret->val = sqlStringComma(&s);
 *pS = s;
 return ret;
 }
@@ -3135,9 +3291,9 @@
 {
 struct analysisParams *el;
 
 if ((el = *pEl) == NULL) return;
-freeMem(el->key);
+freeMem(el->name);
 freeMem(el->val);
 freez(pEl);
 }
 
@@ -3159,9 +3315,9 @@
 {
 fprintf(f, "%u", el->analysis_id);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
-fprintf(f, "%s", el->key);
+fprintf(f, "%s", el->name);
 if (sep == ',') fputc('"',f);
 fputc(sep,f);
 if (sep == ',') fputc('"',f);
 fprintf(f, "%s", el->val);