549dcd456f5d557e15426176e487f91246363548 kate Mon Apr 10 17:21:22 2017 -0700 Better integration with bigBed cod. Fixes loading problem and allows labelFields code to operate (not yet tested). refs #18736 diff --git src/hg/lib/barChartBed.c src/hg/lib/barChartBed.c index 2ad58a9..027904a 100644 --- src/hg/lib/barChartBed.c +++ src/hg/lib/barChartBed.c @@ -1,87 +1,47 @@ /* barChartBed.c was originally generated by the autoSql program, which also * generated barChartBed.h and barChartBed.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 "barChartBed.h" char *barChartBedCommaSepFieldNames = "chrom,chromStart,chromEnd,name,score,strand,expCount,expScores"; -struct barChartBed *barChartBedLoadByQuery(struct sqlConnection *conn, char *query) -/* Load all barChartBed 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 barChartBedFreeList(). */ -{ -struct barChartBed *list = NULL, *el; -struct sqlResult *sr; -char **row; - -sr = sqlGetResult(conn, query); -while ((row = sqlNextRow(sr)) != NULL) - { - el = barChartBedLoad(row); - slAddHead(&list, el); - } -slReverse(&list); -sqlFreeResult(&sr); -return list; -} - -void barChartBedSaveToDb(struct sqlConnection *conn, struct barChartBed *el, char *tableName, int updateSize) -/* Save barChartBed 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 *expScoresArray; -expScoresArray = sqlFloatArrayToString(el->expScores, el->expCount); -sqlDyStringPrintf(update, "insert into %s values ( '%s',%u,%u,'%s',%u,'%s',%u,'%s')", - tableName, el->chrom, el->chromStart, el->chromEnd, el->name, el->score, el->strand, el->expCount, expScoresArray ); -sqlUpdate(conn, update->string); -freeDyString(&update); -freez(&expScoresArray); -} - struct barChartBed *barChartBedLoad(char **row) /* Load a barChartBed from row fetched with select * from barChartBed * from database. Dispose of this with barChartBedFree(). */ { struct barChartBed *ret; AllocVar(ret); ret->expCount = sqlUnsigned(row[6]); ret->chrom = cloneString(row[0]); ret->chromStart = sqlUnsigned(row[1]); ret->chromEnd = sqlUnsigned(row[2]); ret->name = cloneString(row[3]); ret->score = sqlUnsigned(row[4]); safecpy(ret->strand, sizeof(ret->strand), row[5]); { int sizeOne; sqlFloatDynamicArray(row[7], &ret->expScores, &sizeOne); -if (sizeOne != ret->expCount) - errAbort("Bad record in barChart track: '%s' has missing category values", ret->name); +assert(sizeOne == ret->expCount); } return ret; } struct barChartBed *barChartBedLoadAll(char *fileName) /* Load all barChartBed from a whitespace-separated file. * Dispose of this with barChartBedFreeList(). */ { struct barChartBed *list = NULL, *el; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *row[8]; while (lineFileRow(lf, row)) { el = barChartBedLoad(row); @@ -193,64 +153,89 @@ { int i; if (sep == ',') fputc('{',f); for (i=0; iexpCount; ++i) { fprintf(f, "%g", el->expScores[i]); fputc(',', f); } if (sep == ',') fputc('}',f); } fputc(lastSep,f); } /* -------------------------------- End autoSql Generated Code -------------------------------- */ +#include "basicBed.h" + void barChartBedCreateTable(struct sqlConnection *conn, char *table) /* Create barChart format table of given name. */ { char query[1024]; sqlSafef(query, sizeof(query), "CREATE TABLE %s (\n" " chrom varchar(255) not null, # Reference sequence chromosome or scaffold\n" " chromStart int unsigned not null, # Start position in chromosome\n" " chromEnd int unsigned not null, # End position in chromosome\n" " name varchar(255) not null, # Unique identifier\n" " score int unsigned not null, # Score from 0-1000\n" " strand char(1) not null, # + or - for strand\n" " expCount int unsigned not null, # Number of experiment values\n" " expScores longblob not null, # Comma separated list of experiment scores\n" "#Indices\n" " PRIMARY KEY(name)\n" ")\n", table); sqlRemakeTable(conn, table, query); } -float barChartTotalValue(struct barChartBed *bed) +struct bed *barChartSimpleBedLoad(char **row) +/* Load a bed from row containing barChart bed fields. + * This is reuses autoSql barChartBedLoad, but with a full-size bed. + Dispose of this with bedFree() */ +{ +struct bed *ret; +AllocVar(ret); +ret->expCount = sqlUnsigned(row[6]); +ret->chrom = cloneString(row[0]); +ret->chromStart = sqlUnsigned(row[1]); +ret->chromEnd = sqlUnsigned(row[2]); +ret->name = cloneString(row[3]); +ret->score = sqlUnsigned(row[4]); +safecpy(ret->strand, sizeof(ret->strand), row[5]); +{ +int sizeOne; +sqlFloatDynamicArray(row[7], &ret->expScores, &sizeOne); +assert(sizeOne == ret->expCount); +} +return ret; +} + +float barChartTotalValue(struct bed *bed) /* Return total of all category values */ { int i; float sum = 0.0; for (i=0; iexpCount; i++) sum += bed->expScores[i]; return sum; } -float barChartHighestValue(struct barChartBed *bed, int *categIdRet) +float barChartMaxValue(struct bed *bed, int *categIdRet) /* Return value and id of category with highest value for this item */ { int i; float maxScore = 0.0; assert(categIdRet); for (i=0; iexpCount; i++) { float score = bed->expScores[i]; if (score > maxScore) { maxScore = score; *categIdRet = i; } } return maxScore; } +