src/hg/instinct/bioInt2/bioLevelI.c 1.7
1.7 2009/04/30 19:54:28 jsanborn
added factor graph pipeline and code
Index: src/hg/instinct/bioInt2/bioLevelI.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/instinct/bioInt2/bioLevelI.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -b -B -U 1000000 -r1.6 -r1.7
--- src/hg/instinct/bioInt2/bioLevelI.c 27 Apr 2009 06:15:48 -0000 1.6
+++ src/hg/instinct/bioInt2/bioLevelI.c 30 Apr 2009 19:54:28 -0000 1.7
@@ -1,229 +1,174 @@
/* 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 printSlName(struct slName *slList)
{
if (!slList)
fprintf(stdout, "nothing in slName list\n");
struct slName *sl;
fprintf(stdout, "num samples = %d\n", slCount(slList));
for (sl = slList; sl; sl = sl->next)
{
fprintf(stdout, "%s", sl->name);
if (sl->next)
fprintf(stdout, ",");
}
fprintf(stdout, "\n");
}
-void analysisResultFree(struct analysisResult **pEl)
-{
-struct analysisResult *el;
-if ((el = *pEl) == NULL) return;
-
-freeMem(el->sample);
-freeMem(el->feature);
-freez(pEl);
-}
-
-void analysisResultFreeList(struct analysisResult **pList)
-{
-struct analysisResult *el, *next;
-
-for (el = *pList; el != NULL; el = next)
- {
- next = el->next;
- analysisResultFree(&el);
- }
-*pList = NULL;
-}
-
int findIdInTable(struct sqlConnection *biConn, char *tableName,
char *idField, char *sField, char *name)
{
if (sqlTableSize(biConn, tableName) == 0) /* brand new table, return 0 */
return 0;
char query[256];
safef(query, sizeof(query),
"select DISTINCT %s from %s where %s = \"%s\";",
idField, tableName, sField, name);
if (sqlExists(biConn, query)) /* sample name found, use same id */
return sqlQuickNum(biConn, query);
/* Else, find maximum sample id and add one to it */
safef(query, sizeof(query),
"select max(%s) from %s;",
idField, tableName);
int maxId = sqlQuickNum(biConn, query);
return maxId + 1;
}
boolean analysisFeatureExists(struct sqlConnection *biConn, struct analysisFeatures *af)
{
char query[256];
safef(query, sizeof(query),
"select * from %s where id = %d "
"and feature_name = \"%s\" ",
AF_TABLE, af->id, af->feature_name);
return sqlExists(biConn, query);
}
struct hash *getAnalysisFeaturesHash(struct sqlConnection *biConn)
{
struct hash *hash = hashNew(0);
char query[128];
safef(query, sizeof(query), "select * from %s", AF_TABLE);
struct analysisFeatures *af, *afList = analysisFeaturesLoadByQuery(biConn, query);
for (af = afList; af; af = af->next)
hashAddInt(hash, af->feature_name, af->id);
analysisFeaturesFreeList(&afList);
return hash;
}
struct hash *storeAnalysisFeaturesInDb(struct sqlConnection *biConn, struct analysisResult *arList)
{
if (!sqlTableExists(biConn, AF_TABLE))
createAnalysisFeaturesTable(biConn, AF_TABLE);
/* Get existing analysis features */
struct hash *hash = getAnalysisFeaturesHash(biConn);
struct analysisFeatures *af;
struct analysisResult *ar;
for (ar = arList; ar; ar = ar->next)
{
if (hashLookup(hash, ar->feature))
continue; // already visited, ignore
int feature_id = findIdInTable(biConn, AF_TABLE, "id", "feature_name", ar->feature);
AllocVar(af);
af->id = feature_id;
af->feature_name = cloneString(ar->feature);
if (!analysisFeatureExists(biConn, af))
analysisFeaturesSaveToDb(biConn, af, AF_TABLE, 10);
hashAddInt(hash, af->feature_name, af->id);
analysisFeaturesFree(&af);
}
return hash;
}
struct hash *createHash(struct sqlConnection *biConn,
char *table, char *key_field, char *val_field)
{
struct hash *hash = hashNew(0);
char query[128];
safef(query, sizeof(query), "select %s, %s from %s", key_field, val_field, table);
struct sqlResult *sr = sqlGetResult(biConn, query);
char **row = NULL;
while ((row = sqlNextRow(sr)) != NULL)
{
char *id = row[0];
char *name = cloneString(row[1]);
hashAdd(hash, id, name);
}
return hash;
}
-
-void storeAnalysisResultsInDb(struct sqlConnection *biConn, struct biAnalysis *ba,
- struct analysisResult *arList)
-{
-if (!sqlTableExists(biConn, ba->tableName))
- createAnalysisValsTable(biConn, ba->tableName);
-
-struct hash *featureIds = getAnalysisFeaturesHash(biConn);
-struct hash *sampleIds = createHash(biConn, SA_TABLE, "name", "id");
-
-struct analysisVals *av = AllocA(struct analysisVals);
-struct analysisResult *ar;
-for (ar = arList; ar; ar = ar->next)
- {
- char *sample_id = hashMustFindVal(sampleIds, ar->sample);
- int feature_id = hashIntValDefault(featureIds, ar->feature, -1);
-
- if (!sample_id || feature_id == -1)
- continue;
-
- av->sample_id = atoi(sample_id);
- av->feature_id = feature_id;
- av->val = ar->val;
- av->conf = ar->conf;
-
- analysisValsSaveToDb(biConn, av, ba->tableName, 50);
- }
-
-hashFree(&featureIds);
-freeHashAndVals(&sampleIds);
-analysisValsFree(&av);
-}
-
boolean analysisExists(struct sqlConnection *biConn, struct biAnalysis *ba)
{
if (!sqlTableExists(biConn, ba->tableName))
return FALSE;
if (sqlTableSize(biConn, ba->tableName) == 0)
return FALSE;
return TRUE;
}
boolean analysisListExists(char *db, struct biAnalysis *baList)
{
struct sqlConnection *biConn = hAllocConnProfile("localDb", db);
boolean exists = TRUE;
struct biAnalysis *ba;
for (ba = baList; ba; ba = ba->next)
if (!analysisExists(biConn, ba))
exists = FALSE;
hFreeConn(&biConn);
return exists;
}
void runAnalysisPipeline(struct biAnalysis *baList)
{
/* If all analyses already exist, don't do anything */
if (analysisListExists(baList->db, baList))
return;
struct biAnalysis *ba;
for (ba = baList; ba; ba = ba->next)
{
char *db = ba->db;
struct sqlConnection *biConn = hAllocConnProfile("localDb", db);
boolean exists = analysisExists(biConn, ba);
hFreeConn(&biConn);
if (exists)
continue;
ba->pipeline(ba);
}
}