Exemple #1
0
float statMedian(struct idVal **statList)
/* finds the median of the list, will sort list to do this */
{
boolean odd = slCount(*statList)%2;
int half = slCount(*statList)/2;
struct idVal *s=NULL, *s2=NULL;
float median = -1;
statSort(statList);

/* if odd the median is the middle value */
if(odd)
    {
    s = slElementFromIx(*statList,half);
    if(s != NULL)
	median = s->val;
    }
/* if even the median is the average of the two middle values */
else 
    {
    s = slElementFromIx(*statList,half);
    s2 = slElementFromIx(*statList,half-1);
    if(s != NULL && s2 != NULL)
	median = (s->val + s2->val)/2;
    }
return median;
}
/*
  if N is odd, the median is the element in the middle of the
  rearranged sequence, and if N is even, the median is the average of
  the two elements in the middle of the rearranged sequence.
*/
float scMedian(struct sageCounts *scList, int index)
{
struct sageCounts *sc = NULL;
struct sortable *s=NULL, *sList = NULL, *next = NULL, *s2 = NULL;
boolean odd = slCount(scList) %2;
int half = slCount(scList)/2;
float median = -1;
for(sc = scList; sc != NULL; sc = sc->next)
    {
    AllocVar(s);
    s->val = sc->expCounts[index];
    slAddHead(&sList,s);
    }
slSort(&sList,compSortable);
if(odd) 
    {
    s = slElementFromIx(sList,half);
    if(s != NULL)
	median = s->val;
    }
else 
    {
    s = slElementFromIx(sList,half);
    s2 = slElementFromIx(sList,half-1);
    if(s != NULL && s2 != NULL)
	median = (float)((float)s->val + (float)s2->val)/2;
    }
for(s=sList; s != NULL; s = next)
    {
    next = s->next;
    freez(&s);
    }
return median;
}
Exemple #3
0
int agxEvCount(struct altGraphX *ag, int v1, int v2)
/* Return the number of ESTs supporting this edge. */
{
int edgeNum = altGraphXGetEdgeNum(ag, v1, v2);
struct evidence *ev = slElementFromIx(ag->evidence, edgeNum);
return ev->evCount;
}
Exemple #4
0
struct evidence *evidenceForEdge(struct altGraphX *ag, int v1, int v2)
/* Return the evidence associated with this edge. */
{
int edgeNum = altGraphXGetEdgeNum(ag, v1, v2);
struct evidence *ev = slElementFromIx(ag->evidence, edgeNum);
return ev;
}
Exemple #5
0
void showDetailedMatch(char *bacAcc, int contig, int qStart, int qSize, int tStart, int tSize, char *repeatMask)
/* Process a click on active area. */
{
struct dnaSeq *queryList, *query, *target;
struct ffAli *ali;
boolean rc;
char needleName[128], hayName[128];

makeFileNames(bacAcc, repeatMask);
queryList = faReadAllDna(unfinBac);
target = faReadDna(finBac);
query = slElementFromIx(queryList, contig);

if (!ffFindEitherStrandN(query->dna + qStart, qSize, target->dna + tStart, tSize,
    ffCdna, &ali, &rc))
    {
    errAbort("Couldn't align %s and %s", unfinBac, finBac);
    }
sprintf(needleName, "%s contig %d %d-%d", bacAcc, contig, qStart, qStart+qSize);
sprintf(hayName, "%s finished %d-%d", bacAcc, tStart, tStart+tSize);
query->dna[qStart+qSize] = 0;
target->dna[tStart+tSize] = 0;
ffShowAli(ali, needleName, query->dna + qStart, 0,
               hayName, target->dna + tStart, 0, rc);
}
Exemple #6
0
static struct bed* regionsLoad(char* sectionsBed)
/* return a bed3 list of regions for times when -regions is used. */
/* If the filename has a comma then a number, then take just that line */
{
    struct bed* list = NULL;
    unsigned ix = 0;
    if (strchr(sectionsBed, ',')) {
        char* number_part = chopPrefixAt(sectionsBed, ',');
        if (number_part)
            ix = sqlUnsigned(number_part);
    }
    list = readAtLeastBed3(sectionsBed);
    if (list && (ix > 0)) {
        struct bed* single = slElementFromIx(list, ix - 1);
        if (single) {
            struct bed* rem;
            while ((rem = slPopHead(&list)) != single)
                bedFree(&rem);
            rem = single->next;
            bedFreeList(&rem);
            single->next = NULL;
            list = single;
        }
    }
    return list;
}
Exemple #7
0
static struct genePred *nextCcdsGenePred(struct ccdsLocationsJoin **locsList)
/* convert the next ccds/chrom in a list of locations to a genePred */
{
struct ccdsLocationsJoin *locs = popNextCcdsLocs(locsList);
struct ccdsLocationsJoin *loc, *lastLoc;
struct genePred *gp;
char buf[32];
int numExons, iExon, iGene;
if (locs == NULL)
    return NULL;  /* no more */

AllocVar(gp);
gp->optFields = genePredAllFlds;
numExons = slCount(locs);
lastLoc = slElementFromIx(locs, numExons-1);

gp->name = cloneString(ccdsMkId(locs->ccds_uid, locs->ccds_version));
safef(buf, sizeof(buf), "chr%s", locs->chrom);
gp->chrom = cloneString(buf);
gp->strand[0] = locs->strand[0];
gp->txStart = locs->start;
gp->txEnd = lastLoc->stop+1;
gp->cdsStart = locs->start;
gp->cdsEnd = lastLoc->stop+1;
gp->exonCount = numExons;

/* fill in exon blocks */
gp->exonStarts = needMem(numExons*sizeof(unsigned));
gp->exonEnds = needMem(numExons*sizeof(unsigned));
for (loc = locs, iExon = 0; loc != NULL; loc = loc->next, iExon++)
    {
    gp->exonStarts[iExon] = loc->start;
    gp->exonEnds[iExon] = loc->stop+1;
    }
gp->name2 = cloneString("");
gp->cdsStartStat = cdsComplete;
gp->cdsEndStat = cdsComplete;

/* fill in frames */
gp->exonFrames = needMem(numExons*sizeof(int));
iGene = 0;
if (gp->strand[0] == '+')
    {
    for (iExon = 0; iExon < numExons; iExon++)
        {
        gp->exonFrames[iExon] = iGene % 3;
        iGene += gp->exonEnds[iExon] - gp->exonStarts[iExon];
        }
    }
else
    {
    for (iExon = numExons-1; iExon >= 0; iExon--)
        {
        gp->exonFrames[iExon] = iGene % 3;
        iGene += gp->exonEnds[iExon] - gp->exonStarts[iExon];
        }
    }
ccdsLocationsJoinFreeList(&locs);
return gp;
}
static void aftFormatOne(struct annoFormatter *vSelf, struct annoStreamRows *primaryData,
			 struct annoStreamRows *gratorData, int gratorCount)
/* Print out tab-separated columns that we have gathered in prior calls to aftCollect,
 * and start over fresh for the next line of output. */
{
struct annoFormatTab *self = (struct annoFormatTab *)vSelf;
// Got one row from primary; what's the largest # of rows from any grator?
int maxRows = 1;
int iG;
for (iG = 0;  iG < gratorCount;  iG++)
    {
    int gratorRowCount = slCount(gratorData[iG].rowList);
    if (gratorRowCount > maxRows)
	maxRows = gratorRowCount;
    }
// Print out enough rows to make sure that all grator rows are included.
int iR;
for (iR = 0;  iR < maxRows;  iR++)
    {
    printColumns(self, primaryData->streamer, primaryData->rowList, TRUE);
    for (iG = 0;  iG < gratorCount;  iG++)
	{
	struct annoRow *gratorRow = slElementFromIx(gratorData[iG].rowList, iR);
	printColumns(self, gratorData[iG].streamer, gratorRow, FALSE);
	}
    fputc('\n', self->f);
    }
}
Exemple #9
0
static struct dyString *dsPrintStackMemberFromIx(int ix)
// Return the stack member corresponding to the ix.
// By walking stack ix's, you can walk up or down the stack
{
    if (ix < 0)
        return NULL;
    return slElementFromIx(dsStack,ix);
}
Exemple #10
0
struct slName *bigBedListExtraIndexes(struct bbiFile *bbi)
/* Return list of names of extra indexes beyond primary chrom:start-end one" */
{
struct udcFile *udc = bbi->udc;
boolean isSwapped = bbi->isSwapped;

/* See if we have any extra indexes, and if so seek to there. */
bits64 offset = bbi->extraIndexListOffset;
if (offset == 0)
   return NULL;
udcSeek(udc, offset);

/* Construct list of field that are being indexed.  List is list of 
 * field numbers within asObj. */
int i;
struct slInt *intList = NULL, *intEl;
for (i=0; i<bbi->extraIndexCount; ++i)
    {
    bits16 type,fieldCount;
    type = udcReadBits16(udc, isSwapped);
    fieldCount = udcReadBits16(udc, isSwapped);
    udcSeekCur(udc, sizeof(bits64));  // skip over fileOffset
    udcSeekCur(udc, 4);    // skip over reserved bits
    if (fieldCount == 1)
        {
	bits16 fieldId = udcReadBits16(udc, isSwapped);
	udcSeekCur(udc, 2);    // skip over reserved bits
	intEl = slIntNew(fieldId);
	slAddHead(&intList, intEl);
	}
    else
        {
	warn("Not yet understanding indexes on multiple fields at once.");
	internalErr();
	}
    }

/* Now have to make an asObject to find out name that corresponds to this field. */
struct asObject *as = bigBedAsOrDefault(bbi);

/* Make list of field names out of list of field numbers */
struct slName *nameList = NULL;
for (intEl = intList; intEl != NULL; intEl = intEl->next)
    {
    struct asColumn *col = slElementFromIx(as->columnList, intEl->val);
    if (col == NULL)
	{
        warn("Inconsistent bigBed file %s", bbi->fileName);
	internalErr();
	}
    slNameAddHead(&nameList, col->name);
    }

asObjectFree(&as);
return nameList;
}
Exemple #11
0
static struct vertex *consensusVertex(struct rbTree *vertexTree, struct sourceAndPos *list, 
	int listSize, enum ggVertexType softType)
/* Return first trusted (refSeq) vertex, or if no trusted one, then 
 * a soft vertex 1/4 of way (rounded down) through list. */
{
struct sourceAndPos *el;
for (el = list; el != NULL; el = el->next)
    {
    if (el->trustedSource)
        break;
    }
if (el == NULL)
    el = slElementFromIx(list, listSize/4);
return matchingVertex(vertexTree, el->position, softType);
}
void maxTranscriptomeExps(char *files[], int numFiles, char *outputFile, char *trackName)
{
struct sample **pSampList = NULL;
struct sample *s1;
struct sample *maxListSamp  = NULL;
struct sample *maxSamp = NULL;
int i;
int count =0;
FILE *out = NULL;
AllocArray(pSampList, numFiles);
for(i=0;i<numFiles; i++)
 {
 warn("Reading %s.", files[i]);
 pSampList[i] = sampleLoadAll(files[i]);
 }

warn("Calculating Maxes.");
count = slCount(pSampList[0]);
for(i=0;i<count;i++)
    {
    AllocVar(maxSamp);
    s1 = slElementFromIx(pSampList[0], i);
    maxSamp->chrom = cloneString(s1->chrom);
    maxSamp->chromStart = s1->chromStart;
    maxSamp->chromEnd = s1->chromEnd;
    maxSamp->name = cloneString(trackName);
    snprintf(maxSamp->strand, sizeof(maxSamp->strand), "%s", s1->strand);
    maxSamp->sampleCount = s1->sampleCount;
    maxSamp->samplePosition = CloneArray(s1->samplePosition, maxSamp->sampleCount);
    AllocArray(maxSamp->sampleHeight, maxSamp->sampleCount);
    fillInMaxVals(maxSamp, pSampList, numFiles, i);
    slAddHead(&maxListSamp, maxSamp);
    }
slReverse(&maxListSamp);
warn("Saving Maxes");
out = mustOpen(outputFile, "w");
for(maxSamp = maxListSamp; maxSamp != NULL; maxSamp = maxSamp->next)
    {
    sampleTabOut(maxSamp, out);
    }
carefulClose(&out);
warn("Cleaning up");
sampleFreeList(&maxListSamp);
for(i=0;i<numFiles; i++)
    sampleFreeList(&pSampList[i]);
freez(&pSampList);
warn("Done.");
}
void fillInMaxVals(struct sample *samp, struct sample **pSampList, int sampCount, int index)
{
int i,j;
struct sample *s1 = NULL;
for(i=0;i<sampCount; i++)
    {
    s1 = slElementFromIx(pSampList[i], index);
    for(j=0;j<samp->sampleCount; j++)
	{
	if(!(s1->samplePosition[j] == samp->samplePosition[j]))
	    errAbort("maxTranscriptomeExps::fillInMaxVales() - for probe %s:%d-%d positions at index %i don't agree. %d, %d", 
		     samp->chrom, samp->chromStart, samp->chromEnd, i, samp->samplePosition[j], s1->samplePosition[j]);
	samp->sampleHeight[j] = max(samp->sampleHeight[j], s1->sampleHeight[j]);
	}
    }
}
boolean passFilter(struct bed *bed, int blockNum, struct altGraphX *ag, int minNum, int minFlankingSize, boolean mrnaFilter)
{
struct evidence *ev = slElementFromIx(ag->evidence,bed->expIds[blockNum]);
int i =0;
boolean pass = FALSE;
if(minFlankingSize > bed->blockSizes[blockNum])
    return FALSE;
if(ev->evCount >= minNum)
    return TRUE;
if(mrnaFilter)
    {
    for(i =0; i<ev->evCount; i++)
	{
	if(hashLookup(mrnaHash, ag->mrnaRefs[ev->mrnaIds[i]]))
	    return TRUE;
	}
    }
return FALSE;
}
Exemple #15
0
struct subjInfo *getOrderedList(struct column *ord,
        struct column *colList, struct sqlConnection *conn,
        int maxCount)
/* Return sorted list of subjects. */
{
struct subjInfo *subjList = advFilterResults(colList, conn);
struct subjInfo *si;
passedFilterCount = slCount(subjList);

for (si=subjList;si;si=si->next)
    {
    char *s = ord->cellVal(ord, si, conn);
    if (sameString(ord->type,"integer"))
	{
	si->sortInteger = sqlSigned(s);
	freeMem(s);
	}
    else if (sameString(ord->type,"double"))
	{
	if (sameString(s,"."))
	    si->sortDouble = 0;
	else
	    si->sortDouble = sqlDouble(s);
	freeMem(s);
	}
    else
	{
	si->sortString = s;
	}
    }

slSort(&subjList, ord->sortCmp);

if (orderOn[0]=='-')
    slReverse(&subjList);

/* Trim list to max number. */
si = slElementFromIx(subjList, maxCount-1);
if (si != NULL)
    si->next = NULL;

return subjList;
}
Exemple #16
0
void outputControlExonBeds(struct altGraphX *ag, int v1, int v2)
{
int *vPos = ag->vPositions;
struct bed bedUp, bedDown, bedAlt;
struct evidence *ev = slElementFromIx(ag->evidence, altGraphXGetEdgeNum(ag, v1, v2));

/* Make sure that this edge has enough support 
   that we believe it. */
if(ev->evCount >= minControlConf)
    {
    /* Initialize some beds for reporting. */
    bedUp.chrom = bedDown.chrom = bedAlt.chrom = ag->tName;
    bedUp.name = bedDown.name = bedAlt.name = ag->name;
    bedUp.score = bedDown.score = bedAlt.score = altControl;
    safef(bedUp.strand, sizeof(bedUp.strand), "%s", ag->strand);
    safef(bedDown.strand, sizeof(bedDown.strand), "%s", ag->strand);
    safef(bedAlt.strand, sizeof(bedDown.strand), "%s", ag->strand);
    /* Alt spliced region. */
    bedAlt.chromStart = vPos[v1];
    bedAlt.chromEnd = vPos[v2];
    
    /* Upstream/down stream */
    if(sameString(ag->strand, "+"))
	{
	bedUp.chromStart = vPos[v1] - flankingSize;
	bedUp.chromEnd = vPos[v1];
	bedDown.chromStart = vPos[v2];
	bedDown.chromEnd = vPos[v2] + flankingSize;
	}
    else 
	{
	bedDown.chromStart = vPos[v1] - flankingSize;
	bedDown.chromEnd = vPos[v1];
	bedUp.chromStart = vPos[v2];
	bedUp.chromEnd = vPos[v2] + flankingSize;
	}
    bedOutputN(&bedAlt, 6, altRegion, '\t','\n');
    bedOutputN(&bedUp, 6, upStream100, '\t', '\n');
    bedOutputN(&bedDown, 6, downStream100, '\t', '\n');
    }
}
Exemple #17
0
void scrambleFa(char *inName, char *outName)
/* scrambleFa - scramble the order of records in an fa file. */
{
struct dnaSeq *seqList, *seq;
int seqCount;
int seqIx;
FILE *out;

seqList = faReadAllDna(inName);
out = mustOpen(outName, "w");
seqCount = slCount(seqList);
while (seqCount > 0)
    {
    seqIx = rand()%seqCount;
    seq = slElementFromIx(seqList, seqIx);
    faWriteNext(out, seq->name, seq->dna, seq->size);
    slRemoveEl(&seqList, seq);
    --seqCount;
    }
fclose(out);
}
Exemple #18
0
void *slListRandomSample(void *list, int maxCount)
/* Return a sublist of list with at most maxCount. Destroy list in process */
{
if (list == NULL)
    return list;
int initialCount = slCount(list);
if (initialCount <= maxCount)
    return list;
double reduceRatio = (double)maxCount/initialCount;
if (reduceRatio < 0.9)
    {
    double conservativeReduceRatio = reduceRatio * 1.05;
    list = slListRandomReduce(list, conservativeReduceRatio);
    }
int midCount = slCount(list);
if (midCount <= maxCount)
    return list;
shuffleList(list);
struct slList *lastEl = slElementFromIx(list, maxCount-1);
lastEl->next = NULL;
return list;
}
void outputToDiffRecord(struct bed *bed, struct slName *expNames, FILE *out)
/** Write out a bed in the original format, good for diffing 
    to make sure things are getting matched. */
{
static struct hash *unique = NULL;
char key[256];
char *tmp = "placeHolder";
int i;
struct slName *name = NULL;
if(unique == NULL)
    unique = newHash(15);
safef(key, sizeof(key), "%s", bed->name);
if(hashFindVal(unique, key) != NULL)
    return;
for(i=0; i<bed->expCount; i++)
    {
    if(bed->expScores[i] == -10000)
	continue;
    name = slElementFromIx(expNames,bed->expIds[i]);
    fprintf(out, "%s\t%s\t%f\n", bed->name, name->name, bed->expScores[i]);
    }
hashAdd(unique, key, tmp);
}
Exemple #20
0
void addSpliceSite(struct altGraphX *ag, bool **agEm, struct altSpliceSite *aSplice, int ve)
/* Add another end site to an existing splicing event. */
{
struct evidence *ev = NULL;
int edgeNum = 0;
int altBpStart =0;
int altBpEnd = 0;
if(aSplice->altCount >= aSplice->altMax) 
    {
    warn("Why the hell?");
    }
aSplice->altStarts[aSplice->altCount] = ag->vPositions[ve];
aSplice->altTypes[aSplice->altCount] = ag->vTypes[ve];
aSplice->vIndexes[aSplice->altCount] = ve;
aSplice->spliceTypes[aSplice->altCount] = altSpliceType(ag, agEm, aSplice->index, 
							aSplice->vIndexes[aSplice->altCount-1], ve,
							&altBpStart, &altBpEnd);
aSplice->altBpStarts[aSplice->altCount] = ag->vPositions[altBpStart];
aSplice->altBpEnds[aSplice->altCount] = ag->vPositions[altBpEnd];
edgeNum = getEdgeNum(ag, (int)aSplice->index, ve);
ev = slElementFromIx(ag->evidence, edgeNum);
aSplice->support[aSplice->altCount] = ev->evCount;
aSplice->altCount++;
}
void findToFixBedGraphLimits(char *input, char *output)
/* findToFixBedGraphLimits - Scan through ra file of bedGraphs and calculate limits.. */
{
struct lineFile *lf = lineFileOpen(input, TRUE);
FILE *f = mustOpen(output, "w");
struct slPair *el, *list;
while ((list = raNextRecordAsSlPairList(lf)) != NULL)
    {
    /* Find required fields for calcs. */
    char *db = mustFindVal(list, "db", lf);
    char *track = mustFindVal(list, "track", lf);
    char *type = cloneString(mustFindVal(list, "type", lf));

    /* Parse out type value, which should be "bedGraph 4" and put the 4 or whatever other number
     * in dataFieldIndex. */
    char *typeWords[3];
    int typeWordCount = chopLine(type, typeWords);
    if (typeWordCount != 2 || !sameString(typeWords[0], "bedGraph"))
           errAbort("Not well formed bedGraph type line %d of %s", lf->lineIx, lf->fileName);
    int dataFieldIndex = sqlUnsigned(typeWords[1]);

    /* Figure out field corresponding to dataFieldIndex. */
    struct sqlConnection *conn = sqlConnect(db);
    struct slName *fieldList = sqlFieldNames(conn, track);
    struct slName *pastBin = fieldList;
    if (sameString(pastBin->name, "bin"))
         pastBin = pastBin->next;
    struct slName *fieldName = slElementFromIx(pastBin, dataFieldIndex - 1);
    if (fieldName == NULL)
         errAbort("%s doesn't have enough fields", track);
    char *field = fieldName->name;
    assert(sqlFieldIndex(conn, track, field) >= 0);

    /* Print reassuring status message */
    verbose(1, "%s.%s has %d elements.  Data field is %s\n", db, track, sqlTableSize(conn, track), field);
         
    /* Get min/max dataValues in fields.  Do it ourselves rather than using SQL min/max because sometimes
     * the data field is a name column.... */
    char query[512];
    safef(query, sizeof(query), "select %s from %s", field, track);
    struct sqlResult *sr = sqlGetResult(conn, query);
    char **row;
    row = sqlNextRow(sr);
    assert(row != NULL);
    double val = sqlDouble(row[0]);
    double minLimit = val, maxLimit = val;
    while ((row = sqlNextRow(sr)) != 0)
        {
	double val = sqlDouble(row[0]);
	if (val < minLimit) minLimit = val;
	if (val > maxLimit) maxLimit = val;
	}
    sqlFreeResult(&sr);
    verbose(1, "    %g %g\n",  minLimit, maxLimit);

    /* Output original table plus new minLimit/maxLimit. */
    for (el = list; el != NULL; el = el->next)
	fprintf(f, "%s %s\n", el->name, (char *)el->val);
    fprintf(f, "minLimit %g\n", minLimit);
    fprintf(f, "maxLimit %g\n", maxLimit);
    fprintf(f, "\n");

    sqlDisconnect(&conn);
    slFreeList(&fieldList);
    slPairFreeValsAndList(&list);
    }
lineFileClose(&lf);
carefulClose(&f);
}
Exemple #22
0
struct altGraphX *mapAltGraphX(struct altGraphX *ag, struct sqlConnection *conn,
			       char *db, char *netTable )
/* Map one altGraphX record. Return NULL if can't find. This function
 is getting a bit long but it isn't easy to do...*/
{
struct altGraphX *agNew = NULL;
struct chain *chain = NULL;
struct chain *workingChain = NULL, *workingChainFree = NULL;
struct chain *subChain = NULL, *toFree = NULL;
int i,j,k;
int edgeCountNew =0;
int vCountNew=0;
bool reverse = FALSE;
int *starts = NULL, *sizes = NULL;
int blockCount =0;

/* Find the best chain (one that overlaps the most exons. */
AllocArray(starts, ag->edgeCount);
AllocArray(sizes, ag->edgeCount);
for(i=0; i<ag->edgeCount; i++)
    {
    if(getSpliceEdgeType(ag, i) == ggExon)
	{
	starts[blockCount] = ag->vPositions[ag->edgeStarts[i]];
	sizes[blockCount] = ag->vPositions[ag->edgeEnds[i]] - ag->vPositions[ag->edgeStarts[i]];
	blockCount++;
	}
    }
chain = chainForBlocks(conn, db, netTable, ag->tName, ag->tStart, ag->tEnd,
		       starts, sizes, blockCount);
freez(&starts);
freez(&sizes);

if(chain == NULL)
    return NULL;
/* Make a smaller chain to work on... */
chainSubSetForRegion(chain, ag->tStart-1, ag->tEnd+1, &workingChain, &workingChainFree);
if(workingChain == NULL)
    return NULL;
if (chain->qStrand == '-')
    reverse = TRUE;
agNew = altGraphXClone(ag);
freez(&agNew->tName);
agNew->tName = cloneString(chain->qName);
/* Map vertex positions using chain. */
for(i = 0; i < agNew->vertexCount; i++)
    {
    struct cBlock *bi = NULL;
    int targetPos = agNew->vPositions[i];
    struct chain *subChain=NULL, *toFree=NULL;
    agNew->vPositions[i] = -1;
    chainSubSetForRegion(workingChain, targetPos , targetPos, &subChain, &toFree);    
    if(subChain != NULL)
	{
	int qs, qe;
	qChainRangePlusStrand(subChain, &qs, &qe);
	agNew->vPositions[i] = qs;
	}
    chainFree(&toFree);
    }
/* Prune out edges not found. */

/* Set up to remember how many edges we have and our start and stop. */
edgeCountNew = agNew->edgeCount;
vCountNew = agNew->vertexCount;
agNew->tStart = BIGNUM;
agNew->tEnd = 0;
for(i=0; i<agNew->vertexCount && i>= 0; i++)
    {
    struct evidence *ev = NULL;
    if(agNew->vPositions[i] == -1)
	{
	/* Adjust positions, overwriting one that isn't found. */
	vCountNew--;
	for(j=i; j<agNew->vertexCount-1; j++)
	    {
	    agNew->vPositions[j] = agNew->vPositions[j+1];
	    agNew->vTypes[j] = agNew->vTypes[j+1];
	    }
	/* Remove edges associated with this vertex. */
	for(j=0; j<agNew->edgeCount && j>=0; j++)
	    {
	    if(agNew->edgeStarts[j] == i || agNew->edgeEnds[j] == i)
		{
		edgeCountNew--;
		/* Remove evidence. */
		ev = slElementFromIx(agNew->evidence, j);
		slRemoveEl(&agNew->evidence, ev);
		for(k=j; k<agNew->edgeCount -1; k++)
		    {
		    agNew->edgeStarts[k] = agNew->edgeStarts[k+1];
		    agNew->edgeEnds[k] = agNew->edgeEnds[k+1];
		    agNew->edgeTypes[k] = agNew->edgeTypes[k+1];
		    }
		j--;
		agNew->edgeCount--;
		}
	    }
	/* Subtract off one vertex from all the others. */
	for(j=0; j<agNew->edgeCount; j++)
	    {
	    if(agNew->edgeStarts[j] > i)
		agNew->edgeStarts[j]--; 
	    if(agNew->edgeEnds[j] > i)
		agNew->edgeEnds[j]--; 
	    }
	i--;
	agNew->vertexCount--;
	}
    /* Else if vertex found set agNew start and ends. */
    else
	{
	agNew->tStart = min(agNew->vPositions[i], agNew->tStart);
	agNew->tEnd = max(agNew->vPositions[i], agNew->tEnd);
	}
    }
/* Not going to worry about mRNAs that aren't used anymore. Leave them in
   for now. */
agNew->vertexCount = vCountNew;
agNew->edgeCount = edgeCountNew;
if(agNew->vertexCount == 0 || agNew->edgeCount == 0)
    {
    altGraphXFree(&agNew);
    return NULL;
    }
for(i=0; i<agNew->edgeCount; i++)
    {
    if(agNew->edgeStarts[i] >= agNew->vertexCount ||
       agNew->edgeEnds[i] >= agNew->vertexCount)
	{
	warn("For %s vertexes occur at %d when in reality there are only %d vertices.",
	     agNew->name, max(agNew->edgeStarts[i], agNew->edgeEnds[i]), agNew->vertexCount);
	}
    }
/* If it is on the other strand reverse it. */
if(reverse)
    {
    altGraphXReverse(agNew);
    }
chainFree(&workingChainFree);
return agNew;
}
Exemple #23
0
static void doThumbnails(struct sqlConnection *conn)
/* Write out list of thumbnail images. */
{
char *sidUrl = cartSidUrlString(cart);
char *listSpec = cartUsualString(cart, hgpListSpec, "");
char *matchFile = cartString(cart, hgpMatchFile);
struct visiMatch *matchList = NULL, *match;
int maxCount = 25, count = 0;
int startAt = cartUsualInt(cart, hgpStartAt, 0);
int imageCount;

htmlSetStyle(
"<STYLE TYPE=\"text/css\">\n"
"  BODY {margin: 2px}\n"
"  </STYLE>\n"
);
htmlSetBgColor(0xC0C0D6);
htmStart(stdout, "doThumbnails");
matchList = readMatchFile(matchFile);
imageCount = slCount(matchList);
if (imageCount > 0)
    {
    printf(
"    <DIV"
"    ID='perspClip'"
"    STYLE='position:absolute;left:-1000px;top:-1000px;z-index:2;visibility:visible;overflow:hidden!important;'"
"         onmouseover='this.style.left=\"-1000px\";' "
"    >"
"    <DIV"
"    ID='perspective'"
"    STYLE='position:absolute;left:0px;top:0px;'"
"    >"
"    <IMG ID='perspBox' SRC='../images/dot_clear.gif' STYLE='position:absolute;left:0px;top:0px;width:100px;height:100px;border-style:solid;border-color:#8080FF;border-width:1px' "
">"
"    </DIV>"
"    </DIV>"
    );
    printf("<TABLE>\n");
    printf("<TR><TD><B>");
    printf("%d images match<BR>\n", imageCount);
    printf("</B></TD></TR>\n");
    for (match = slElementFromIx(matchList, startAt);
	    match != NULL; match = match->next)
	{
	int id = match->imageId;
	char *imageFile = visiGeneThumbSizePath(conn, id);
	printf("<TR>");
	printf("<TD>");
	printf("<A HREF=\"%s?%s&%s=%d&%s=do\" target=\"image\" >",
	    hgVisiGeneCgiName(),
	    sidUrl, hgpId, id, hgpDoImage);
	printf("<IMG SRC=\"%s\"></A><BR>\n", imageFile);

	smallCaption(conn, id);
	printf("<BR>\n");
	printf("</TD>");
	printf("</TR>");
	if (++count >= maxCount)
	    break;
	}
    printf("</TABLE>\n");
    printf("<HR>\n");
    }
if (count != imageCount)
    {
    int start;
    int page = 0;
    printf("%d-%d of %d for:<BR>", startAt+1,
	startAt+count, imageCount);
    printf("&nbsp;%s<BR>\n", listSpec);
    printf("Page:\n");
    for (start=0; start<imageCount; start += maxCount)
	{
	++page;
	if (start != startAt)
	    {
	    printf("<A HREF=\"%s?", hgVisiGeneCgiName());
	    printf("%s&", sidUrl);
	    printf("%s=on&", hgpDoThumbnails);
	    printf("%s=%s&", hgpListSpec, listSpec);
	    if (start != 0)
	       printf("%s=%d", hgpStartAt, start);
	    printf("\">");
	    }
	printf("%d", page);
	if (start != startAt)
	   printf("</A>");
	printf(" ");
	}
    }
else
    {
    printf("%d images for:<BR>", imageCount);
    printf("&nbsp;%s<BR>\n", listSpec);
    }
cartRemove(cart, hgpStartAt);
htmlEnd();
}
struct bed *bedForEdge(struct altGraphX *ag, bool **em, int v1, int v2, int edgeCount)
{
int i;
char buff[256];
int *vPos = ag->vPositions;
int vCount = ag->vertexCount;
int pos1=0, pos2=BIGNUM;
int ev1=0, ev2=0;
struct bed *bed = NULL;
boolean found1 = FALSE, found2 = FALSE;
/* Find first exon. */
for(i=0; i<vCount; i++)
    {
    if(em[i][v1] && altGraphXEdgeVertexType(ag, i, v1) == ggExon)
	{
	int edgeNum = altGraphXGetEdgeNum(ag, i, v1);
	struct evidence *ev = slElementFromIx(ag->evidence, edgeNum);
	if(ev->evCount > ev1)
	    {
	    found1 = TRUE;
	    ev1 = ev->evCount;
	    pos1 = vPos[i];
	    }
	}
    }

/* Find second exon. */
for(i=0; i<vCount; i++)
    {
    if(em[v2][i] && altGraphXEdgeVertexType(ag, v2, i) == ggExon)
	{
	int edgeNum = altGraphXGetEdgeNum(ag, v2, i);
	struct evidence *ev = slElementFromIx(ag->evidence, edgeNum);
	if(ev->evCount > ev2)
	    {
	    found2 = TRUE;
	    ev2 = ev->evCount;
	    pos2 = vPos[i];
	    }
	}
    }
if(found1 && found2)
    {
    AllocVar(bed);
    safef(buff, sizeof(buff), "%s.%d", ag->name, edgeCount);
    bed->name = cloneString(buff);
    safef(bed->strand, sizeof(bed->strand), "%s", ag->strand);
    bed->chrom = cloneString(ag->tName);
    bed->chromStart = bed->thickStart = pos1;
    bed->chromEnd = bed->thickEnd = pos2;
    bed->score = (ev1 + ev2)/2;
    AllocArray(bed->chromStarts, 2);
    AllocArray(bed->blockSizes, 2);
    bed->blockCount = 2;
    bed->chromStarts[0] = pos1 - bed->chromStart;
    bed->chromStarts[1] = vPos[v2] - bed->chromStart;
    bed->blockSizes[0] =  vPos[v1] - pos1;
    bed->blockSizes[1] = pos2 - vPos[v2];
    }
return bed;
}
struct genoLay *genoLayNew(struct genoLayChrom *chromList,
	MgFont *font, int picWidth, int betweenChromHeight,
	int minLeftLabelWidth, int minRightLabelWidth,
	char *how)
/* Figure out layout.  For human and most mammals this will be
 * two columns with sex chromosomes on bottom.  This is complicated
 * by the platypus having a bunch of sex chromosomes. */
{
int margin = 3;
struct slRef *refList = NULL, *ref, *left, *right;
struct genoLayChrom *chrom;
struct genoLay *gl;
int autoCount, halfCount, bases, chromInLine;
int leftLabelWidth=0, rightLabelWidth=0, labelWidth;
int spaceWidth = mgFontCharWidth(font, ' ');
int extraLabelPadding = 0;
int autosomeOtherPixels=0, sexOtherPixels=0;
int autosomeBasesInLine=0;	/* Maximum bases in a line for autosome. */
int sexBasesInLine=0;		/* Bases in line for sex chromsome. */
double sexBasesPerPixel, autosomeBasesPerPixel, basesPerPixel;
int pos = margin;
int y = 0;
int fontHeight = mgFontLineHeight(font);
int chromHeight = fontHeight;
int lineHeight = chromHeight + betweenChromHeight;
boolean allOneLine = FALSE;

refList = refListFromSlList(chromList);

/* Allocate genoLay object and fill in simple fields. */
AllocVar(gl);
gl->chromList = chromList;
gl->chromHash = hashNew(0);
gl->font = font;
gl->picWidth = picWidth;
gl->margin = margin;
gl->spaceWidth = spaceWidth;
gl->lineHeight = lineHeight;
gl->betweenChromHeight = betweenChromHeight;
gl->betweenChromOffsetY = 0;
gl->chromHeight = chromHeight;
gl->chromOffsetY = lineHeight - chromHeight;


/* Save chromosomes in hash too, for easy access */
for (chrom = chromList; chrom != NULL; chrom = chrom->next)
    hashAdd(gl->chromHash, chrom->fullName, chrom);

if (sameString(how, genoLayOnePerLine))
    {
    gl->leftList = refList;
    }
else if (sameString(how, genoLayAllOneLine))
    {
    gl->bottomList = refList;
    allOneLine = TRUE;
    }
else
    {
    /* Put sex chromosomes on bottom, and rest on left. */
    separateSexChroms(refList, &refList, &gl->bottomList);
    autoCount = slCount(refList);
    gl->leftList = refList;

    /* If there are a lot of chromosomes, then move later
     * (and smaller) chromosomes to a new right column */
    if (autoCount > 12)
	{
	halfCount = (autoCount+1)/2;
	ref = slElementFromIx(refList, halfCount-1);
	gl->rightList = ref->next;
	ref->next = NULL;
	slReverse(&gl->rightList);
	}
    }

if (allOneLine)
    {
    unsigned long totalBases = 0, bStart=0, bEnd;
    int chromCount = 0, chromIx=0;
    for (ref = gl->bottomList; ref != NULL; ref = ref->next)
        {
	chrom = ref->val;
	totalBases += chrom->size;
	chromCount += 1;
	}
    int availablePixels = picWidth - minLeftLabelWidth - minRightLabelWidth
       - 2*margin - (chromCount-1);
    double basesPerPixel = (double)totalBases/availablePixels;
    gl->picHeight = 2*margin + lineHeight + fontHeight;
    for (ref = gl->bottomList; ref != NULL; ref = ref->next)
        {
	chrom = ref->val;
	bEnd = bStart + chrom->size;
	int pixStart = round(bStart / basesPerPixel);
	int pixEnd = round(bEnd / basesPerPixel);
	chrom->width = pixEnd - pixStart;
	chrom->height = lineHeight;
	chrom->x = pixStart + margin + chromIx + minLeftLabelWidth;
	chrom->y = 0;
	chromIx += 1;
	bStart = bEnd;
	}
    gl->lineCount = 1;
    gl->picHeight = 2*margin + lineHeight + fontHeight + 1;
    gl->allOneLine = TRUE;
    gl->leftLabelWidth = minLeftLabelWidth;
    gl->rightLabelWidth = minRightLabelWidth;
    gl->basesPerPixel = basesPerPixel;
    gl->pixelsPerBase = 1.0/basesPerPixel;
    }
else
    {
    /* Figure out space needed for autosomes. */
    left = gl->leftList;
    right = gl->rightList;
    while (left || right)
	{
	bases = 0;
	chromInLine = 0;
	if (left)
	    {
	    chrom = left->val;
	    labelWidth = mgFontStringWidth(font, chrom->shortName) + spaceWidth;
	    if (leftLabelWidth < labelWidth)
		leftLabelWidth = labelWidth;
	    bases = chrom->size;
	    left = left->next;
	    }
	if (right)
	    {
	    chrom = right->val;
	    labelWidth = mgFontStringWidth(font, chrom->shortName) + spaceWidth;
	    if (rightLabelWidth < labelWidth)
		rightLabelWidth = labelWidth;
	    bases += chrom->size;
	    right = right->next;
	    }
	if (autosomeBasesInLine < bases)
	    autosomeBasesInLine = bases;
	gl->lineCount += 1;
	}

    /* Figure out space needed for bottom chromosomes. */
    if (gl->bottomList)
	{
	gl->lineCount += 1;
	sexOtherPixels = spaceWidth + 2*margin;
	for (ref = gl->bottomList; ref != NULL; ref = ref->next)
	    {
	    chrom = ref->val;
	    sexBasesInLine += chrom->size;
	    labelWidth = mgFontStringWidth(font, chrom->shortName) + spaceWidth;
	    if (ref == gl->bottomList )
		{
		if (leftLabelWidth < labelWidth)
		    leftLabelWidth  = labelWidth;
		sexOtherPixels = leftLabelWidth;
		}
	    else if (ref->next == NULL)
		{
		if (rightLabelWidth < labelWidth)
		    rightLabelWidth  = labelWidth;
		sexOtherPixels += rightLabelWidth + spaceWidth;
		}
	    else
		{
		sexOtherPixels += labelWidth + spaceWidth;
		}
	    }
	}

    /* Do some adjustments if side labels are bigger than needed for
     * chromosome names. */
    if (leftLabelWidth < minLeftLabelWidth)
	{
	extraLabelPadding += (minLeftLabelWidth - leftLabelWidth);
	leftLabelWidth = minLeftLabelWidth;
	}
    if (rightLabelWidth < minRightLabelWidth)
	{
	extraLabelPadding += (minRightLabelWidth - rightLabelWidth);
	rightLabelWidth = minRightLabelWidth;
	}
    sexOtherPixels += extraLabelPadding;

    /* Figure out the number of bases needed per pixel. */
    autosomeOtherPixels = 2*margin + spaceWidth + leftLabelWidth + rightLabelWidth;
    basesPerPixel = autosomeBasesPerPixel 
	    = autosomeBasesInLine/(picWidth-autosomeOtherPixels);
    if (gl->bottomList)
	{
	sexBasesPerPixel = sexBasesInLine/(picWidth-sexOtherPixels);
	if (sexBasesPerPixel > basesPerPixel)
	    basesPerPixel = sexBasesPerPixel;
	}

    /* Save positions and sizes of some things in layout structure. */
    gl->leftLabelWidth = leftLabelWidth;
    gl->rightLabelWidth = rightLabelWidth;
    gl->basesPerPixel = basesPerPixel;
    gl->pixelsPerBase = 1.0/basesPerPixel;

    /* Set pixel positions for left autosomes */
    for (ref = gl->leftList; ref != NULL; ref = ref->next)
	{
	chrom = ref->val;
	chrom->x = leftLabelWidth + margin;
	chrom->y = y;
	chrom->width = round(chrom->size/basesPerPixel);
	chrom->height = lineHeight;
	y += lineHeight;
	}

    /* Set pixel positions for right autosomes */
    y = 0;
    for (ref = gl->rightList; ref != NULL; ref = ref->next)
	{
	chrom = ref->val;
	chrom->width = round(chrom->size/basesPerPixel);
	chrom->height = lineHeight;
	chrom->x = picWidth - margin - rightLabelWidth - chrom->width;
	chrom->y = y;
	y += lineHeight;
	}
    gl->picHeight = 2*margin + lineHeight * gl->lineCount;
    y = gl->picHeight - margin - lineHeight;

    /* Set pixel positions for sex chromosomes */
    for (ref = gl->bottomList; ref != NULL; ref = ref->next)
	{
	chrom = ref->val;
	chrom->y = y;
	chrom->width = round(chrom->size/basesPerPixel);
	chrom->height = lineHeight;
	if (ref == gl->bottomList)
	    chrom->x = leftLabelWidth + margin;
	else if (ref->next == NULL)
	    chrom->x = picWidth - margin - rightLabelWidth - chrom->width;
	else
	    chrom->x = 2*spaceWidth+mgFontStringWidth(font,chrom->shortName) + pos;
	pos = chrom->x + chrom->width;
	}
    }
return gl;
}
Exemple #26
0
boolean isRetainIntron(struct altGraphX *ag, bool **em,  int vs, int ve1, int ve2,
		       int *altBpStart, int *altBpEnd)
/* return TRUE if retained intron. 
   Looking for pattern:
   hs-->he---->hs--->he
    \---------------/

   Use edgesInArea() to investigate that encompasses the common hard
   end and common hard start. Should only be 4 edges in area defined by
   splicing.
   eseses 
   012345 
  0  
  1  + +
  2   + 
  3    +
  4   
  5
*/
{
unsigned char *vTypes = ag->vTypes;
int i=0;
int numAltVerts = 4;

/* Quick check. */
if(vTypes[vs] != ggHardStart || vTypes[ve1] != ggHardEnd || vTypes[ve2] != ggHardEnd)
    return FALSE;
if(em[vs][ve1] && em[vs][ve2]) 
    {
    /* Try to find a hard start that connects ve1 and ve2. */
    for(i=0; i<ag->vertexCount; i++)
	{
	if(vTypes[i] == ggHardStart && em[ve1][i] && em[i][ve2] &&
	edgesInArea(ag,em,ve2-1,vs+1) == numAltVerts)
	    {
	    int *vPos = ag->vPositions;
	    struct bed bedAlt;
	    /* Initialize some beds for reporting. */
	    bedAlt.chrom = ag->tName;
	    bedAlt.name = ag->name;
	    bedAlt.score = altRetInt;
	    safef(bedAlt.strand, sizeof(bedAlt.strand), "%s", ag->strand);
	    /* Alt spliced region. */
	    bedAlt.chromStart = vPos[ve1];
	    bedAlt.chromEnd = vPos[i];
	    if(optionExists("printRetIntAccs"))
		{
		int incEdge = altGraphXGetEdgeNum(ag, vs, ve2);
		int exEdge = altGraphXGetEdgeNum(ag, ve1, i);
		struct evidence *ev= slElementFromIx(ag->evidence, incEdge);
		int i;
		fprintf(stdout, "RETINT\t%s\t%d\t", ag->name,ev->evCount);
		for(i = 0; i < ev->evCount; i++)
		    {
		    fprintf(stdout, "%s,", ag->mrnaRefs[ev->mrnaIds[i]]);
		    }
		fprintf(stdout, "\t");
		ev= slElementFromIx(ag->evidence, exEdge);
		fprintf(stdout, "%d\t", ev->evCount);
		for(i = 0; i < ev->evCount; i++)
		    {
		    fprintf(stdout, "%s,", ag->mrnaRefs[ev->mrnaIds[i]]);
		    }
		fprintf(stdout, "\n");
		}
	    /* Upstream/down stream */
	    if(altRegion != NULL)
		{
		bedOutputN(&bedAlt, 6, altRegion, '\t','\n');
		}
	    *altBpStart = ve1;
	    *altBpEnd = i;
	    return TRUE;
	    }
	}
    }
return FALSE;
}	    
Exemple #27
0
struct altSpliceSite *initASplice(struct altGraphX *ag, bool **agEm,  int vs, int ve1, int ve2)
/* Initialize an altSplice site report with vlaues. */
{
struct altSpliceSite *as = NULL;
struct evidence *ev = NULL;
int edgeNum = 0;
int altBpStart=0, altBpEnd=0;
int i=0;
int vMax = 2*ag->vertexCount+ag->edgeCount;
AllocVar(as);
as->chrom = cloneString(ag->tName);
as->chromStart = ag->vPositions[vs];
as->chromEnd = ag->vPositions[vs];
as->agName = cloneString(ag->name);
safef(as->strand, sizeof(as->strand), "%s", ag->strand);
as->index = vs;
as->type = ag->vTypes[vs];
as->altCount+=2;
as->altMax = vMax;
/* Return starts of vertices. */
AllocArray(as->altStarts, vMax);
as->altStarts[0] = ag->vPositions[ve1];
as->altStarts[1] = ag->vPositions[ve2];

/* Record indices of vertices. */
AllocArray(as->vIndexes, vMax);
as->vIndexes[0] = ve1;
as->vIndexes[1] = ve2;

/* Record type of vertices. */
AllocArray(as->altTypes, vMax);
as->altTypes[0] = ag->vTypes[ve1];
as->altTypes[1] = ag->vTypes[ve2];

/* Record splice types and bases alt spliced. */
AllocArray(as->spliceTypes, vMax);
AllocArray(as->altBpEnds, vMax);
AllocArray(as->altBpStarts, vMax);
as->spliceTypes[0] = altSpliceType(ag, agEm, vs, ve1, ve1, &altBpStart, &altBpEnd);
as->altBpStarts[0] = ag->vPositions[altBpStart];
as->altBpEnds[0] = ag->vPositions[altBpEnd];

as->spliceTypes[1] = altSpliceType(ag, agEm,vs, ve1, ve2, &altBpStart, &altBpEnd);
as->altBpStarts[1] = ag->vPositions[altBpStart];
as->altBpEnds[1] = ag->vPositions[altBpEnd];

/* Look up the evidence. */
AllocArray(as->support, vMax);

edgeNum = getEdgeNum(ag, vs, ve1);
ev = slElementFromIx(ag->evidence, edgeNum);
as->support[0] = ev->evCount;

edgeNum = getEdgeNum(ag, vs, ve2);
ev = slElementFromIx(ag->evidence, edgeNum);
as->support[1] = ev->evCount;

AllocArray(as->altCons, vMax);
AllocArray(as->upStreamCons, vMax);
AllocArray(as->downStreamCons, vMax);
for(i=0; i<vMax; i++)
    as->altCons[i] = as->upStreamCons[i] = as->downStreamCons[i] = -1;

return as;
}