440d84ee43c6a5f8a22bfe7886681db4ee7cc202 braney Thu Aug 28 16:55:27 2014 -0700 adding more support for bigGenePred in hgc and hgTables. diff --git src/hg/hgc/hgc.c src/hg/hgc/hgc.c index db9ef8a..b4215ca 100644 --- src/hg/hgc/hgc.c +++ src/hg/hgc/hgc.c @@ -232,30 +232,31 @@ #include "chromInfo.h" #include "gbWarn.h" #include "lsSnpPdbChimera.h" #include "mammalPsg.h" #include "net.h" #include "jsHelper.h" #include "virusClick.h" #include "gwasCatalog.h" #include "parClick.h" #include "mdb.h" #include "yaleGencodeAssoc.h" #include "itemDetailsHtml.h" #include "trackVersion.h" #include "numtsClick.h" #include "geneReviewsClick.h" +#include "bigBed.h" static char *rootDir = "hgcData"; #define LINESIZE 70 /* size of lines in comp seq feature */ struct cart *cart; /* User's settings. */ char *seqName; /* Name of sequence we're working on. */ int winStart, winEnd; /* Bounds of sequence. */ char *database; /* Name of mySQL database. */ char *organism; /* Colloquial name of organism. */ char *genome; /* common name, e.g. Mouse, Human */ char *scientificName; /* Scientific name of organism. */ struct hash *trackHash; /* A hash of all tracks - trackDb valued */ @@ -7998,46 +7999,101 @@ char *table = cartString(cart, "o"); /* checks both gbSeq and table */ aaSeq *seq = hGenBankGetPep(database, pepName, table); cartHtmlStart("Protein Translation"); if (seq == NULL) { warn("Predicted peptide %s is not avaliable", pepName); } else { displayProteinPrediction(pepName, seq->dna); dnaSeqFree(&seq); } } -void htcTranslatedPredMRna(struct trackDb *tdb, char *geneName) -/* Translate virtual mRNA defined by genePred to protein and display it. */ +static struct genePred *getGenePredForPositionSql(char *table, char *geneName) +/* find the genePred for the current gene using an SQL table*/ { +struct genePred *gpList = NULL; +char query[512]; struct sqlConnection *conn = hAllocConn(database); +struct sqlResult *sr; +char **row; +struct genePred *gp; +int rowOffset = hOffsetPastBin(database, seqName, table); + +sqlSafef(query, sizeof(query), "select * from %s where name = \"%s\"", table, geneName); +sr = sqlGetResult(conn, query); +while ((row = sqlNextRow(sr)) != NULL) + { + gp = genePredLoad(row+rowOffset); + slAddHead(&gpList, gp); + } + +sqlFreeResult(&sr); +hFreeConn(&conn); +return gpList; +} + +struct genePred *getGenePredForPositionBigBed(char *table, char *geneName) +/* find the genePred to the current gene using a bigGenePred */ +{ +struct trackDb *tdb = hubConnectAddHubForTrackAndFindTdb( database, table, NULL, trackHash); +struct bbiFile *bbi; +char *fileName = cloneString(trackDbSetting(tdb, "bigDataUrl")); +bbi = bigBedFileOpen(fileName); +struct lm *lm = lmInit(0); +struct bigBedInterval *bb, *bbList = bigBedIntervalQuery(bbi, seqName, winStart, winEnd, 0, lm); +struct genePred *gpList = NULL; +for (bb = bbList; bb != NULL; bb = bb->next) + { + struct genePred *gp = genePredFromBigGenePred(seqName, bb); + if (sameString(gp->name, geneName)) + slAddHead(&gpList, gp); + } +lmCleanup(&lm); + +return gpList; +} + +static struct genePred *getGenePredForPosition(char *table, char *geneName) +{ +struct genePred *gpList = NULL; + +if (isHubTrack(table)) + gpList = getGenePredForPositionBigBed(table, geneName); +else + gpList = getGenePredForPositionSql(table, geneName); + +return gpList; +} + +void htcTranslatedPredMRna(char *geneName) +/* Translate virtual mRNA defined by genePred to protein and display it. */ +{ +char *table = cartString(cart, "table"); struct genePred *gp = NULL; -char where[256]; char protName[256]; char *prot = NULL; cartHtmlStart("Protein Translation from Genome"); -sqlSafefFrag(where, sizeof(where), "name = \"%s\"", geneName); -gp = genePredReaderLoadQuery(conn, tdb->table, where); -hFreeConn(&conn); +gp = getGenePredForPosition(table, geneName); + if (gp == NULL) errAbort("%s not found in %s when translating to protein", - geneName, tdb->table); + geneName, table); else if (gp->cdsStart == gp->cdsEnd) errAbort("No CDS defined: no protein translation for %s", geneName); prot = getPredMRnaProtSeq(gp); safef(protName, sizeof(protName), "%s_prot", geneName); displayProteinPrediction(protName, prot); freez(&prot); genePredFree(&gp); } void htcTranslatedMRna(struct trackDb *tdb, char *acc) /* Translate mRNA to protein and display it. */ { struct sqlConnection *conn = hAllocConn(database); struct genbankCds cds = getCds(conn, acc); @@ -8158,65 +8214,57 @@ if (gp->strand[0] == '+' && gp->cdsStartStat != cdsComplete) offset = (3 - gp->exonFrames[0]) % 3; else if (gp->strand[0] == '-' && gp->cdsEndStat != cdsComplete) offset = (3 - gp->exonFrames[gp->exonCount-1]) % 3; } /* NOTE: this fix will not handle the case in which frame is shifted * internally or at multiple exons, as when frame-shift gaps occur in * an alignment of an mRNA to the genome. Going to have to come back * and address that later... (acs) */ dnaTranslateSome(cdsDna->dna+offset, prot, protBufSize); dnaSeqFree(&cdsDna); return prot; } + + void htcGeneMrna(char *geneName) /* Display cDNA predicted from genome */ { char *table = cartString(cart, "o"); -char query[512]; -struct sqlConnection *conn = hAllocConn(database); -struct sqlResult *sr; -char **row; -struct genePred *gp; -struct dnaSeq *seq; +cartHtmlStart("Predicted mRNA from Genome"); +struct genePred *gp, *gpList = getGenePredForPosition(table, geneName); int cdsStart, cdsEnd; -int rowOffset = hOffsetPastBin(database, seqName, table); +struct dnaSeq *seq; -cartHtmlStart("Predicted mRNA from Genome"); -sqlSafef(query, sizeof(query), "select * from %s where name = \"%s\"", table, geneName); -sr = sqlGetResult(conn, query); -while ((row = sqlNextRow(sr)) != NULL) +for(gp = gpList; gp; gp = gp->next) { - gp = genePredLoad(row+rowOffset); seq = getCdnaSeq(gp); getCdsInMrna(gp, &cdsStart, &cdsEnd); toUpperN(seq->dna + cdsStart, cdsEnd - cdsStart); if (gp->strand[0] == '-') { reverseComplement(seq->dna, seq->size); } printf("
"); printf(">%s\n", geneName); faWriteNext(stdout, NULL, seq->dna, seq->size); printf(""); genePredFree(&gp); freeDnaSeq(&seq); } -sqlFreeResult(&sr); -hFreeConn(&conn); } void htcRefMrna(char *geneName) /* Display mRNA associated with a refSeq gene. */ { /* check both gbSeq and refMrna */ struct dnaSeq *seq = hGenBankGetMrna(database, geneName, "refMrna"); if (seq == NULL) errAbort("RefSeq mRNA sequence %s not found", geneName); cartHtmlStart("RefSeq mRNA"); printf("
"); faWriteNext(stdout, seq->name, seq->dna, seq->size); printf(""); dnaSeqFree(&seq); @@ -8308,40 +8356,81 @@ { s = gp->exonStarts[exonIx]; e = gp->exonEnds[exonIx]; if (s < seqStart) s = seqStart; if (e > seqEnd) e = seqEnd; if ((size = e - s) > 0) { s -= startOffset; if (s < 0 || s + size > seq->size) errAbort("Out of range! %d-%d not in %d-%d", s, s+size, 0, size); toUpperN(seq->dna + s, size); } } } + +static struct bed *getBedsFromBigBedRange(char *table, char *geneName) +/* get a list of beds from a bigBed in the current range */ +{ +struct trackDb *tdb = hubConnectAddHubForTrackAndFindTdb( database, table, NULL, trackHash); +struct bbiFile *bbi; +char *fileName = cloneString(trackDbSetting(tdb, "bigDataUrl")); +bbi = bigBedFileOpen(fileName); +struct lm *lm = lmInit(0); +struct bigBedInterval *bb, *bbList = bigBedIntervalQuery(bbi, seqName, winStart, winEnd, 0, lm); +struct bed *bedList = NULL; +char *bedRow[32]; +char startBuf[16], endBuf[16]; +for (bb = bbList; bb != NULL; bb = bb->next) + { + bigBedIntervalToRow(bb, seqName, startBuf, endBuf, bedRow, ArraySize(bedRow)); + struct bed *bed = bedLoadN(bedRow, 12); + if (sameString(bed->name, geneName)) + slAddHead(&bedList, bed); + } +lmCleanup(&lm); + +return bedList; +} + void htcDnaNearGene( char *geneName) /* Fetch DNA near a gene. */ { char *table = cartString(cart, "o"); -char constraints[256]; int itemCount; char *quotedItem = makeQuotedString(geneName, '\''); -safef(constraints, sizeof(constraints), "name = %s", quotedItem); puts("
"); +if (isHubTrack( table)) + { + struct hTableInfo *hti; + AllocVar(hti); + hti->hasCDS = TRUE; + hti->hasBlocks = TRUE; + hti->rootName = table; + + struct bed *bedList = getBedsFromBigBedRange(table, geneName); + itemCount = hgSeqBed(database, hti, bedList); + freez(&hti); + bedFreeList(&bedList); + } +else + { + char constraints[256]; + safef(constraints, sizeof(constraints), "name = %s", quotedItem); itemCount = hgSeqItemsInRange(database, table, seqName, winStart, winEnd, constraints); + } if (itemCount == 0) printf("\n# No results returned from query.\n\n"); puts(""); freeMem(quotedItem); } void htcTrackHtml(struct trackDb *tdb) /* Handle click to display track html */ { cartWebStart(cart, database, "%s", tdb->shortLabel); printTrackHtml(tdb); } void doViralProt(struct trackDb *tdb, char *geneName) /* Handle click on known viral protein track. */ @@ -25017,31 +25106,31 @@ } else if (sameWord(table, "htcBlatXeno")) { htcBlatXeno(item, cartString(cart, "aliTable")); } else if (sameWord(table, "htcExtSeq")) { htcExtSeq(item); } else if (sameWord(table, "htcTranslatedProtein")) { htcTranslatedProtein(item); } else if (sameWord(table, "htcTranslatedPredMRna")) { - htcTranslatedPredMRna(tdbForTableArg(), item); + htcTranslatedPredMRna(item); } else if (sameWord(table, "htcTranslatedMRna")) { htcTranslatedMRna(tdbForTableArg(), item); } else if (sameWord(table, "htcGeneMrna")) { htcGeneMrna(item); } else if (sameWord(table, "htcRefMrna")) { htcRefMrna(item); } else if (sameWord(table, "htcDisplayMrna")) {