2e75814ac8468bdfb14d01cabe7455b63154a64b kent Thu Feb 10 16:59:09 2011 -0800 Adding example lines to BAM schema page in table browser. Pushing forward on getting identifiers to work there, but still a ways to go. (It no longer bombs at the paste identifiers page, but it does shortly thereafter.) diff --git src/hg/hgTables/bam.c src/hg/hgTables/bam.c index 0dc1d9b..26cdf2e 100644 --- src/hg/hgTables/bam.c +++ src/hg/hgTables/bam.c @@ -99,31 +99,30 @@ /* Get fields of bigBed as list of sqlFieldType. */ { struct asObject *as = bamAsObj(); struct sqlFieldType *list = sqlFieldTypesFromAs(as); return list; } #define BAM_NUM_BUF_SIZE 256 void samAlignmentToRow(struct samAlignment *sam, char *numBuf, char *row[SAMALIGNMENT_NUM_COLS]) /* Convert samAlignment data structure to an array of strings, using numBuf to store * ascii versions of numbers temporarily */ { char *numPt = numBuf; char *numBufEnd = numBuf + BAM_NUM_BUF_SIZE; - row[0] = sam->qName; row[1] = numPt; numPt += sprintf(numPt, "%u", sam->flag); numPt += 1; row[2] = sam->rName; row[3] = numPt; numPt += sprintf(numPt, "%u", sam->pos); numPt += 1; row[4] = numPt; numPt += sprintf(numPt, "%u", sam->mapQ); numPt += 1; row[5] = sam->cigar; row[6] = sam->rNext; row[7] = numPt; numPt += sprintf(numPt, "%d", sam->pNext); numPt += 1; row[8] = numPt; numPt += sprintf(numPt, "%d", sam->tLen); numPt += 1; row[9] = sam->seq; row[10] = sam->qual; row[11] = sam->tagTypeVals; assert(numPt < numBufEnd); } @@ -273,52 +272,112 @@ { /* Figure out bam file name get column info and filter. */ char *fileName = bamFileName(table, conn); struct asObject *as = bamAsObj(); struct asFilter *filter = asFilterFromCart(cart, db, table, as); /* Get beds a region at a time. */ struct bed *bedList = NULL; struct region *region; for (region = regionList; region != NULL; region = region->next) addFilteredBedsOnRegion(fileName, region, table, filter, lm, &bedList); slReverse(&bedList); return bedList; } +struct slName *randomBamIds(char *table, struct sqlConnection *conn, int count) +/* Return some semi-random qName based IDs from a BAM file. */ +{ +/* Read 10000 items from bam file, or if they ask for a big list, then 4x what they ask for. */ +char *fileName = bamFileName(table, conn); +samfile_t *fh = bamOpen(fileName, NULL); +struct lm *lm = lmInit(0); +int orderedCount = count * 4; +if (orderedCount < 10000) + orderedCount = 10000; +struct samAlignment *sam, *samList = bamReadNextSamAlignments(fh, orderedCount, lm); + +/* Shuffle list and extract qNames from first count of them. */ +shuffleList(&samList, 1); +struct slName *randomIdList = NULL; +int i; +for (i=0, sam = samList; i<count && sam != NULL; ++i, sam = sam->next) + slNameAddHead(&randomIdList, sam->qName); + +/* Clean up and go home. */ +lmCleanup(&lm); +bamClose(&fh); +freez(&fileName); +return randomIdList; +} + void showSchemaBam(char *table) /* Show schema on bam. */ { struct sqlConnection *conn = hAllocConn(database); char *fileName = bamFileName(table, conn); struct asObject *as = bamAsObj(); hPrintf("<B>Database:</B> %s", database); hPrintf(" <B>Primary Table:</B> %s<br>", table); hPrintf("<B>BAM File:</B> %s", fileName); hPrintf("<BR>\n"); hPrintf("<B>Format description:</B> %s<BR>", as->comment); hPrintf("See the <A HREF=\"%s\" target=_blank>SAM Format Specification</A> for more details<BR>\n", "http://samtools.sourceforge.net/SAM-1.3.pdf"); /* Put up table that describes fields. */ hTableStart(); hPrintf("<TR><TH>field</TH>"); hPrintf("<TH>description</TH> "); puts("</TR>\n"); struct asColumn *col; int colCount = 0; for (col = as->columnList; col != NULL; col = col->next) { hPrintf("<TR><TD><TT>%s</TT></TD>", col->name); hPrintf("<TD>%s</TD></TR>", col->comment); ++colCount; } hTableEnd(); -/* In a perfect world would print sample rows here. Maybe later.... */ +/* Put up another section with sample rows. */ +webNewSection("Sample Rows"); +hTableStart(); + +/* Print field names as column headers for example */ +hPrintf("<TR>"); +int colIx = 0; +for (col = as->columnList; col != NULL; col = col->next) + { + hPrintf("<TH>%s</TH>", col->name); + ++colIx; + } +hPrintf("</TR>\n"); + +/* Fetch sample rows. */ +samfile_t *fh = bamOpen(fileName, NULL); +struct lm *lm = lmInit(0); +struct samAlignment *sam, *samList = bamReadNextSamAlignments(fh, 10, lm); + +/* Print sample lines. */ +char *row[SAMALIGNMENT_NUM_COLS]; +char numBuf[BAM_NUM_BUF_SIZE]; +for (sam=samList; sam != NULL; sam = sam->next) + { + samAlignmentToRow(sam, numBuf, row); + hPrintf("<TR>"); + for (colIx=0; colIx<colCount; ++colIx) + { + writeHtmlCell(row[colIx]); + } + hPrintf("</TR>\n"); + } +hTableEnd(); /* Clean up and go home. */ +bamClose(&fh); +lmCleanup(&lm); freeMem(fileName); hFreeConn(&conn); }