src/hg/instinct/bioInt2/bioController.c 1.6
1.6 2009/03/24 03:07:55 jsanborn
updated
Index: src/hg/instinct/bioInt2/bioController.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioController.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -b -B -U 1000000 -r1.5 -r1.6
--- src/hg/instinct/bioInt2/bioController.c 23 Mar 2009 18:19:29 -0000 1.5
+++ src/hg/instinct/bioInt2/bioController.c 24 Mar 2009 03:07:55 -0000 1.6
@@ -1,358 +1,499 @@
/* mapProbesToGenes - Will maps probes in BED format to overlapping gene(s). */
#include "common.h"
#include "linefile.h"
#include "hash.h"
#include "options.h"
#include "jksql.h"
#include "hPrint.h"
#include "hdb.h"
#include "dystring.h"
#include "bioIntDb.h"
#include "bioIntDriver.h"
#include "cprob.h"
#include "hgStatsLib.h"
#include "bioController.h"
void usage()
/* Explain usage and exit. */
{
errAbort(
"bioController - controller for bioIntegrator pipeline\n"
"usage:\n"
" bioIntegrator datasets\n"
" -datasets = comma-separated list of datasets\n"
);
}
#define BIOINT_DB "bioInt"
static struct optionSpec options[] = {
{NULL, 0}
};
void createAnalysisModulesTable(struct sqlConnection *biConn, char *tableName)
{
struct dyString *dy = newDyString(1024);
dyStringPrintf(dy, "CREATE TABLE %s (\n", tableName);
dyStringPrintf(dy, "id int unsigned not null,\n");
dyStringPrintf(dy, "name varchar(255) not null,\n");
dyStringPrintf(dy, "type varchar(255) not null,\n");
dyStringPrintf(dy, "PRIMARY KEY(id)\n");
dyStringPrintf(dy, ")\n");
sqlUpdate(biConn,dy->string);
dyStringFree(&dy);
}
void createAnalysesTable(struct sqlConnection *biConn, char *tableName)
{
struct dyString *dy = newDyString(1024);
dyStringPrintf(dy, "CREATE TABLE %s (\n", tableName);
dyStringPrintf(dy, "id int unsigned not null,\n");
dyStringPrintf(dy, "cohort_id int unsigned not null,\n");
dyStringPrintf(dy, "module_id int unsigned not null,\n");
dyStringPrintf(dy, "result_table varchar(255) not null,\n");
dyStringPrintf(dy, "input_tables longblob not null,\n");
dyStringPrintf(dy, "PRIMARY KEY(id)\n");
dyStringPrintf(dy, ")\n");
sqlUpdate(biConn,dy->string);
dyStringFree(&dy);
}
void createAnalysisParamsTable(struct sqlConnection *biConn, char *tableName)
{
struct dyString *dy = newDyString(1024);
dyStringPrintf(dy, "CREATE TABLE %s (\n", tableName);
dyStringPrintf(dy, "analysis_id int unsigned not null,\n");
dyStringPrintf(dy, "name varchar(255) not null,\n");
dyStringPrintf(dy, "val varchar(255) not null,\n");
dyStringPrintf(dy, "KEY(analysis_id)\n");
dyStringPrintf(dy, ")\n");
sqlUpdate(biConn,dy->string);
dyStringFree(&dy);
}
char *getTableName(struct datasets *daList, char *module)
{
if (!daList)
return NULL;
struct dyString *dy = dyStringNew(10);
dyStringPrintf(dy, "%s", module);
struct datasets *da;
for (da = daList; da; da = da->next)
dyStringPrintf(dy, "_%s", da->data_table);
return dyStringCannibalize(&dy);
}
-
boolean analysesExists(struct sqlConnection *biConn, struct analyses *an)
{
char query[256];
safef(query, sizeof(query),
"select * from analyses where id = %d "
"and cohort_id = %d "
"and module_id = %d "
"and result_table = \"%s\" "
"and input_tables = \"%s\" ",
an->id, an->cohort_id, an->module_id, an->result_table, an->input_tables);
return sqlExists(biConn, query);
}
-void createAnalyses(struct sqlConnection *biConn, int cohort_id, int module_id,
- char *result_table, char *input_tables)
+boolean analysesWithResultTableExists(struct sqlConnection *biConn, char *result_table)
{
-int id = findIdInTable(biConn, "analyses", "id", "result_table", result_table);
-
-struct analyses *an;
-AllocVar(an);
-an->id = id;
-an->cohort_id = cohort_id;
-an->module_id = module_id;
-an->result_table = cloneString(result_table);
-an->input_tables = cloneString(input_tables);
-
-if (!analysesExists(biConn, an))
- analysesSaveToDbEscaped(biConn, an, "analyses", 20);
+char query[256];
+safef(query, sizeof(query),
+ "select * from analyses where result_table = \"%s\" ",
+ result_table);
-analysesFree(&an);
+return sqlExists(biConn, query);
}
-struct datasets *datasetsInCohort(struct sqlConnection *biConn, int cohort_id)
+struct hash *analysisParamsHash(struct sqlConnection *biConn, struct analyses *an)
{
+struct hash *hash = hashNew(0);
char query[256];
safef(query, sizeof(query),
- "select * from datasets join datasetCohort on datasets.id = datasetCohort.dataset_id "
- "where datasetCohort.cohort_id = %d;",
- cohort_id);
+ "select * from analysisParams where analysis_id = %d",
+ an->id);
+struct analysisParams *ap, *apList = analysisParamsLoadByQuery(biConn, query);
-return datasetsLoadByQuery(biConn, query);
+for (ap = apList; ap; ap = ap->next)
+ hashAdd(hash, ap->name, ap->val);
+
+return hash;
}
-struct analysisModules *analysisModulesOfType(struct sqlConnection *biConn,
- char *type)
+void storeAnalysisParams(struct sqlConnection *biConn,
+ struct analyses *an, struct hash *params)
{
-char query[128];
-safef(query, sizeof(query),
- "select * from analysisModules where type = \"%s\" ",
- type);
+struct hashEl *el;
+struct hashCookie cookie = hashFirst(params);
+while ((el = hashNext(&cookie)) != NULL)
+ {
+ char *name = el->name;
+ char *val = el->val;
-return analysisModulesLoadByQuery(biConn, query);
+ struct analysisParams *ap;
+ AllocVar(ap);
+ ap->analysis_id = an->id;
+ ap->name = cloneString(name);
+ ap->val = cloneString(val);
+
+ analysisParamsSaveToDbEscaped(biConn, ap, "analysisParams", 50);
+
+ analysisParamsFree(&ap);
+ }
}
-void createGeneLevelAnalyses(struct sqlConnection *biConn, int cohort_id)
+boolean matchingParams(struct sqlConnection *biConn, int analysis_id, struct hash *params)
{
-struct datasets *da, *daList = datasetsInCohort(biConn, cohort_id);
+char query[256];
+safef(query, sizeof(query),
+ "select * from analysisParams where analysis_id = %d",
+ analysis_id);
-struct dyString *dy = dyStringNew(10);
-for (da = daList; da; da = da->next)
+struct analysisParams *ap, *apList = analysisParamsLoadByQuery(biConn, query);
+
+if (!apList) // no analysis params for this id
{
- dyStringPrintf(dy, "%s", da->data_table);
- if (da->next)
- dyStringPrintf(dy, ",");
+ if (hashNumEntries(params) == 0)
+ return TRUE; // none here either, a match
+ else
+ return FALSE; // params exist in hash, no match
}
-char *input_tables = dyStringCannibalize(&dy);
-/* Gene analysis modules */
-struct analysisModules *am, *amList = analysisModulesOfType(biConn, "gene");
+/* apList not NULL, run through list check that every analysisParam matches
+ * key-value to element in hash */
+struct hashEl *el;
+char *name, *val;
+boolean matching = TRUE;
+for (ap = apList; ap; ap = ap->next)
+ {
+ el = hashLookup(params, ap->name);
+ if (!el)
+ {
+ matching = FALSE;
+ continue;
+ }
+
+ val = el->val;
+ if (!sameString(val, ap->val))
+ matching = FALSE;
+ }
-for (am = amList; am; am = am->next)
+/* need to check reverse direction */
+struct hashCookie cookie = hashFirst(params);
+while ((el = hashNext(&cookie)) != NULL)
{
- char *result_table = getTableName(daList, am->name);
- createAnalyses(biConn, cohort_id, am->id, result_table, input_tables);
+ name = el->name;
+ val = el->val;
+
+ boolean foundMatch = FALSE;
+ for (ap = apList; ap; ap = ap->next)
+ if (sameString(ap->name, name) && sameString(ap->val, val))
+ foundMatch = TRUE;
+
+ if (!foundMatch)
+ matching = FALSE;
}
+
+return matching;
}
-void createSetLevelAnalyses(struct sqlConnection *biConn, int cohort_id)
+char *createAnalysesResultTableName(struct sqlConnection *biConn, int cohort_id,
+ struct analysisModules *am, char *input_tables,
+ struct hash *params)
{
-/* Get all gene-level analyses */
-char query[256];
-safef(query, sizeof(query),
- "select * from analyses "
- "join analysisModules on analyses.module_id = analysisModules.id "
- "where analyses.cohort_id = %d and analysisModules.type = \"%s\" ",
- cohort_id, "gene");
+if (!input_tables)
+ return NULL;
-struct analyses *an, *anList = analysesLoadByQuery(biConn, query);
+struct slName *sl, *slList = slNameListFromComma(input_tables);
+struct dyString *dy = dyStringNew(10);
-if (!anList)
+/* Create prefix, e.g. "moduleName_table1_table2" */
+dyStringPrintf(dy, "%s", am->name);
+for (sl = slList; sl; sl = sl->next)
+ dyStringPrintf(dy, "_%s", sl->name);
+char *prefix = dyStringCannibalize(&dy);
+
+/* Initial table name is simply prefix */
+char name[256];
+safef(name, sizeof(name), "%s", prefix);
+
+int count = 0;
+boolean foundName = FALSE;
+while (!foundName && count < 100)
{
- fprintf(stdout, "No set level analysis to perform.\n");
- return;
+ count++;
+ if (!analysesWithResultTableExists(biConn, name))
+ {
+ foundName = TRUE;
+ break;
}
-/* Set (geneset/pathway) analysis modules */
-struct analysisModules *am, *amList = analysisModulesOfType(biConn, "set");
+ int id = findIdInTable(biConn, "analyses", "id", "result_table", name);
+ if (matchingParams(biConn, id, params))
+ {
+ foundName = TRUE;
+ break;
+ }
-for (am = amList; am; am = am->next)
- { // loop through modules
- for (an = anList; an; an = an->next)
- { // loop through gene-level results (one from each gene module)
- char result_table[128];
- safef(result_table, sizeof(result_table),
- "%s_%s", am->name, an->result_table);
- createAnalyses(biConn, cohort_id, am->id, result_table, an->result_table);
+ /* If we got here, params don't matching existing analysis id,
+ * append a number to it and try again*/
+ safef(name, sizeof(name), "%s_%d", prefix, count);
}
+
+if (!foundName)
+ errAbort("Could not find a unique table name after 100 attempts\n");
+
+char *result_table = cloneString(name);
+return result_table;
+}
+
+struct analyses *createAnalyses(struct sqlConnection *biConn, int cohort_id,
+ struct analysisModules *am, char *input_tables,
+ struct hash *params)
+{
+if (!input_tables)
+ return NULL;
+
+/* Get unique name or name already in database for exact matching analysis */
+char *result_table = createAnalysesResultTableName(biConn, cohort_id, am, input_tables, params);
+int id = findIdInTable(biConn, "analyses", "id", "result_table", result_table);
+
+struct analyses *an;
+AllocVar(an);
+an->id = id;
+an->cohort_id = cohort_id;
+an->module_id = am->id;
+an->result_table = cloneString(result_table);
+an->input_tables = cloneString(input_tables);
+
+if (!analysesExists(biConn, an))
+ {
+ analysesSaveToDbEscaped(biConn, an, "analyses", 20);
+ storeAnalysisParams(biConn, an, params);
}
+
+return an;
}
+struct datasets *datasetsInCohort(struct sqlConnection *biConn, int cohort_id)
+{
+char query[256];
+safef(query, sizeof(query),
+ "select * from datasets join datasetCohort on datasets.id = datasetCohort.dataset_id "
+ "where datasetCohort.cohort_id = %d;",
+ cohort_id);
+
+return datasetsLoadByQuery(biConn, query);
+}
+
+struct analysisModules *analysisModulesMatching(struct sqlConnection *biConn,
+ char *field, char *val)
+{
+char query[128];
+safef(query, sizeof(query),
+ "select * from analysisModules where %s = \"%s\" ",
+ field, val);
+
+return analysisModulesLoadByQuery(biConn, query);
+}
void biAnalysisAddModule(struct sqlConnection *biConn,
struct biAnalysis *ba, int module_id)
{
if (!ba)
return;
char query[128];
safef(query, sizeof(query),
- "select name from analysisModules where id = %d",
+ "select * from analysisModules where id = %d",
module_id);
-if (!sqlExists(biConn, query))
+struct analysisModules *am = analysisModulesLoadByQuery(biConn, query);
+if (!am)
errAbort("No module with id = %d", module_id);
-char *module = sqlQuickString(biConn, query);
-
-if (sameString(module, "meta"))
- {
+/* Set pipeline by module type (gene, set, ...) */
+if (sameString(am->type, "gene"))
ba->pipeline = geneLevelPipeline;
- ba->analyze = metaGene;
- }
-else if (sameString(module, "metaGeneset"))
- {
+else if (sameString(am->type, "set"))
ba->pipeline = genesetLevelPipeline;
- ba->analyze = metaGeneset;
- }
else
- {
ba->pipeline = NULL;
+
+/* Set analysis algorithm by module name*/
+if (sameString(am->name, "meta"))
+ ba->analyze = metaGene;
+else if (sameString(am->name, "metaGeneset"))
+ ba->analyze = metaGeneset;
+else
ba->analyze = NULL;
- }
}
struct biAnalysis *biAnalysisListForCohort(struct sqlConnection *biConn,
char *db, int cohort_id)
{
struct biAnalysis *ba, *baList = NULL;
char query[128];
safef(query, sizeof(query),
"select * from analyses where cohort_id = %d",
cohort_id);
struct analyses *an, *anList = analysesLoadByQuery(biConn, query);
if (!anList)
errAbort("No analyses for cohort = %d", cohort_id);
for (an = anList; an; an = an->next)
{
AllocVar(ba);
ba->db = cloneString(db);
ba->tableName = cloneString(an->result_table);
- ba->parameters = hashNew(0);
+ ba->parameters = analysisParamsHash(biConn, an);
ba->inputTables = slNameListFromComma(an->input_tables);
biAnalysisAddModule(biConn, ba, an->module_id);
slAddHead(&baList, ba);
}
slReverse(&baList);
analysesFreeList(&anList);
return baList;
}
boolean cohortExists(struct sqlConnection *biConn, int cohort_id)
{
char query[128];
safef(query, sizeof(query), "select * from cohorts where id = %d", cohort_id);
return sqlExists(biConn, query);
}
boolean analysisModuleExists(struct sqlConnection *biConn, struct analysisModules *am)
{
char query[128];
safef(query, sizeof(query),
"select * from analysisModules where id = %d "
"and name = \"%s\" "
"and type = \"%s\" ",
am->id, am->name, am->type);
return sqlExists(biConn, query);
}
void storeAnalysisModule(struct sqlConnection *biConn, char *name, char *type)
{
struct analysisModules *am;
AllocVar(am);
am->name = cloneString(name);
am->type = cloneString(type);
am->id = findIdInTable(biConn, "analysisModules", "id", "name", am->name);
if (!analysisModuleExists(biConn, am))
analysisModulesSaveToDb(biConn, am, "analysisModules", 10);
analysisModulesFree(&am);
}
void setupAnalysisModules(struct sqlConnection *biConn)
{
/* Meta-Gene module */
storeAnalysisModule(biConn, "meta", "gene");
/* Set up more modules below, similar to above */
storeAnalysisModule(biConn, "metaGeneset", "set");
}
+void createStandardPipeline(struct sqlConnection *biConn, int cohort_id)
+{
+struct datasets *da, *daList = datasetsInCohort(biConn, cohort_id);
+
+struct dyString *dy = dyStringNew(10);
+for (da = daList; da; da = da->next)
+ {
+ dyStringPrintf(dy, "%s", da->data_table);
+ if (da->next)
+ dyStringPrintf(dy, ",");
+ }
+char *input_tables = dyStringCannibalize(&dy);
+
+struct analysisModules *am;
+
+/* Gene analysis modules */
+am = analysisModulesMatching(biConn, "name", "meta");
+if (!am)
+ errAbort("No analysis module named meta");
+
+struct hash *params = hashNew(0);
+hashAdd(params, "fxn", cloneString("fishers"));
+struct analyses *gene = createAnalyses(biConn, cohort_id, am, input_tables, params);
+char *gene_result_table = cloneString(gene->result_table);
+analysesFree(&gene);
+analysisModulesFree(&am);
+hashFree(¶ms);
+
+/* Set (geneset/pathway) analysis modules */
+am = analysisModulesMatching(biConn, "name", "metaGeneset");
+if (!am)
+ errAbort("No analysis module named metaGeneset");
+
+params = hashNew(0);
+hashAdd(params, "fxn", cloneString("fishers"));
+struct analyses *set = createAnalyses(biConn, cohort_id, am, gene_result_table, params);
+analysesFree(&set);
+analysisModulesFree(&am);
+hashFree(¶ms);
+}
+
+
void prepareDatabase(struct sqlConnection *biConn)
{
if (!sqlTableExists(biConn, "analyses"))
createAnalysesTable(biConn, "analyses");
if (!sqlTableExists(biConn, "analysisParams"))
createAnalysisParamsTable(biConn, "analysisParams");
if (!sqlTableExists(biConn, "analysisModules"))
createAnalysisModulesTable(biConn, "analysisModules");
setupAnalysisModules(biConn);
}
void bioController(char *db, int cohort_id)
{
struct sqlConnection *biConn = hAllocConnProfile("localDb", db);
if (!cohortExists(biConn, cohort_id))
{
hFreeConn(&biConn);
errAbort("Cohort %d does not exist.", cohort_id);
}
-
prepareDatabase(biConn);
-createGeneLevelAnalyses(biConn, cohort_id);
-
-createSetLevelAnalyses(biConn, cohort_id);
+createStandardPipeline(biConn, cohort_id);
struct biAnalysis *baList = biAnalysisListForCohort(biConn, db, cohort_id);
hFreeConn(&biConn);
runAnalysisPipeline(baList);
}
int main(int argc, char *argv[])
/* Process command line. */
{
optionInit(&argc, argv, options);
if (argc != 2)
usage();
bioController(BIOINT_DB, atoi(argv[1]));
return 0;
}