static struct bed *getChromAsBed( struct sqlConnection *conn, char *db, char *table, /* Database and table. */ char *chrom, struct lm *lm, int *retFieldCount) /* Where to allocate memory. */ /* Return a bed list of all items in the given range in table. * Cleanup result via lmCleanup(&lm) rather than bedFreeList. */ { char *fields = NULL; struct sqlResult *sr; struct hTableInfo *hti; struct bed *bedList=NULL, *bed; char **row; int fieldCount; boolean isPsl, isGenePred, isBedWithBlocks; boolean pslKnowIfProtein = FALSE, pslIsProtein = FALSE; hti = hFindTableInfoDb(db, chrom, table); if (hti == NULL) errAbort("Could not find table info for table %s", table); bedSqlFieldsExceptForChrom(hti, &fieldCount, &fields); isPsl = htiIsPsl(hti); isGenePred = sameString("exonEnds", hti->endsSizesField); isBedWithBlocks = ( (sameString("chromStarts", hti->startsField) || sameString("blockStarts", hti->startsField)) && sameString("blockSizes", hti->endsSizesField)); /* All beds have at least chrom,start,end. We omit the chrom * from the query since we already know it. */ sr = hExtendedChromQuery(conn, table, chrom, NULL, FALSE, fields, NULL); while (sr != NULL && (row = sqlNextRow(sr)) != NULL) { bed = bedFromRow(chrom, row, fieldCount, isPsl, isGenePred, isBedWithBlocks, &pslKnowIfProtein, &pslIsProtein, lm); slAddHead(&bedList, bed); } freez(&fields); sqlFreeResult(&sr); slReverse(&bedList); *retFieldCount = fieldCount; return(bedList); }
static struct bed *joinedTablesToBed(struct joinedTables *joined, struct hTableInfo *hti, int bedFieldCount, struct lm *lm) /* Make a bedList from the joining query results. */ { struct joinedRow *jr; struct bed *bedList = NULL; boolean isPsl = sameString("tStarts", hti->startsField); boolean isGenePred = sameString("exonEnds", hti->endsSizesField); boolean isBedWithBlocks = ((sameString("chromStarts", hti->startsField) || sameString("blockStarts", hti->startsField)) && sameString("blockSizes", hti->endsSizesField)); boolean pslKnowIfProtein, pslIsProtein; int *bedFieldIndices = getBedFieldIndices(joined, hti); /* The reserved field is skipped, so adjust count for row creation: */ int adjBedFieldCount = (bedFieldCount > 8) ? bedFieldCount-1 : bedFieldCount; char *chrom = NULL; for (jr = joined->rowList; jr != NULL; jr = jr->next) { if (jr->passedFilter) { int i; struct bed *bed = NULL; char *row[16]; for (i=0; i < adjBedFieldCount; i++) { if (bedFieldIndices[i] < 0) row[i] = "0"; else row[i] = jr->fields[bedFieldIndices[i]]->name; } if ((chrom == NULL) || (! sameString(chrom, row[0]))) chrom = lmCloneString(lm, row[0]); bed = bedFromRow(chrom, row+1, bedFieldCount, isPsl, isGenePred, isBedWithBlocks, &pslKnowIfProtein, &pslIsProtein, lm); slAddHead(&bedList, bed); } } freeMem(bedFieldIndices); slReverse(&bedList); return bedList; }
struct bed *getRegionAsBed( char *db, char *table, /* Database and table. */ struct region *region, /* Region to get data for. */ char *filter, /* Filter to add to SQL where clause if any. */ struct hash *idHash, /* Restrict to id's in this hash if non-NULL. */ struct lm *lm, /* Where to allocate memory. */ int *retFieldCount) /* Number of fields. */ /* Return a bed list of all items in the given range in table. * Cleanup result via lmCleanup(&lm) rather than bedFreeList. */ { char *fields = NULL; struct sqlResult *sr; struct hTableInfo *hti; struct bed *bedList=NULL, *bed; char **row; int fieldCount; boolean isPsl, isGenePred, isBedWithBlocks; boolean pslKnowIfProtein = FALSE, pslIsProtein = FALSE; struct sqlConnection *conn = NULL; char *dbTable = NULL; if (isCustomTrack(table)) { struct customTrack *ct = ctLookupName(table); dbTable = ct->dbTableName; conn = hAllocConn(CUSTOM_TRASH); hti = hFindTableInfo(CUSTOM_TRASH, region->chrom, dbTable); } else { dbTable = table; struct trackDb *tdb; if(sameWord(db, database)) tdb = tdbForTrack(db, table, &fullTrackList); else tdb = hTrackDbForTrack(db, table); conn = (tdb ? hAllocConnTrack(db, tdb) : hAllocConn(db)); hti = hFindTableInfo(db, region->chrom, table); } if (hti == NULL) errAbort("Could not find table info for table %s.%s", db,table); if (isWiggle(db, table)) { bedList = getWiggleAsBed(db, table, region, filter, idHash, lm, conn); fieldCount = 4; } else { bedSqlFieldsExceptForChrom(hti, &fieldCount, &fields); isPsl = htiIsPsl(hti); isGenePred = sameString("exonEnds", hti->endsSizesField); isBedWithBlocks = ( (sameString("chromStarts", hti->startsField) || sameString("blockStarts", hti->startsField)) && sameString("blockSizes", hti->endsSizesField)); /* All beds have at least chrom,start,end. We omit the chrom * from the query since we already know it. */ sr = regionQuery(conn, dbTable, fields, region, TRUE, filter); while (sr != NULL && (row = sqlNextRow(sr)) != NULL) { /* If have a name field apply hash filter. */ if (fieldCount >= 4 && idHash != NULL) if (!hashLookup(idHash, row[2])) continue; bed = bedFromRow(region->chrom, row, fieldCount, isPsl, isGenePred, isBedWithBlocks, &pslKnowIfProtein, &pslIsProtein, lm); slAddHead(&bedList, bed); } freez(&fields); sqlFreeResult(&sr); slReverse(&bedList); } hFreeConn(&conn); if (retFieldCount) *retFieldCount = fieldCount; return(bedList); }