static struct slName *getBedFieldSlNameList(struct hTableInfo *hti,
        char *db, char *table)
/* Return the bed-compat field list for the given table, as
 * slName list of "$db.$table.$field". */
{
    struct slName *snList = NULL, *sn = NULL;
    int fieldCount = 0;
    char *fields = NULL;
    char *words[16];
    char dtf[256];
    int i;
    if (hti == NULL)
        errAbort("Can't find table info for table %s.%s", db, table);
    bedSqlFieldsExceptForChrom(hti, &fieldCount, &fields);
    /* Update our notion of fieldCount -- the chrom field is omitted, and
     * (if applicable) the reserved field is omitted too: */
    fieldCount = chopCommas(fields, words);
    for (i=fieldCount-1;  i >= 0;  i--)
    {
        if (sameString(words[i], "0"))
            continue;
        safef(dtf, sizeof(dtf), "%s.%s.%s", db, table, words[i]);
        sn = slNameNew(dtf);
        slAddHead(&snList, sn);
    }
    safef(dtf, sizeof(dtf), "%s.%s.%s", db, table, hti->chromField);
    sn = slNameNew(dtf);
    slAddHead(&snList, sn);
    freez(&fields);
    return snList;
}
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);
}
Esempio n. 3
0
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);
}