struct wigSection *wigSectionRead(struct lineFile *lf)
/* Parse out next section of wig. */
{
    static double *vals = NULL;
    static int valAlloc = 0;

    /* Get "fixedStep" line and parse it. */
    char *line;
    if (!lineFileNextReal(lf, &line))
        return NULL;
    char *pattern = "fixedStep ";
    int patSize = 10;
    if (!startsWith(pattern, line))
        errAbort("Expecting fixedStep line %d of %s", lf->lineIx, lf->fileName);
    line += patSize;
    struct hash *varHash = hashVarLine(line, lf->lineIx);
    int step = sqlUnsigned(requiredVal(lf, varHash, "step"));
    int start = sqlUnsigned(requiredVal(lf, varHash, "start"));
    char *chrom = cloneString(requiredVal(lf, varHash, "chrom"));
    hashFree(&varHash);

    /* Parse out numbers until next fixedStep. */
    int valCount = 0;
    int i;
    for (;;)
    {
        if (!lineFileNextReal(lf, &line))
            break;
        if (startsWith(pattern, line))
        {
            lineFileReuse(lf);
            break;
        }
        for (i=0; i<step; ++i)
        {
            if (valCount >= valAlloc)
            {
                int newAlloc = valAlloc + 1024;
                ExpandArray(vals, valAlloc, newAlloc);
                valAlloc = newAlloc;
            }
            vals[valCount] = lineFileNeedDouble(lf, &line, 0);
            ++valCount;
        }
    }

    /* Create wigSection. */
    struct wigSection *section;
    AllocVar(section);
    section->chrom = chrom;
    section->chromStart = start;
    section->chromEnd = start + valCount;
    section->vals = CloneArray(vals, valCount);
    return section;
}
void userSettingsUseNamed(struct userSettings *us, char *setName)
/* Use named collection of settings. */
{
struct cart *cart = us->cart;
char *varName = settingsVarName(us->savePrefix, setName);
char *settings = cartOptionalString(cart, varName);
if (settings != NULL)
    {
    struct hash *hash = hashVarLine(settings, 1);
    struct hashEl *list = hashElListHash(hash);
    struct hashEl *el;
    for (el = list; el != NULL; el = el->next)
	cartSetString(cart, el->name, el->val);
    slFreeList(&list);
    hashFree(&hash);
    }
freez(&varName);
}
예제 #3
0
void wigToBedGraph(char *wigIn, char *bedOut)
/* wigToBedGraph - Convert wig files to bedGraph, merging adjacent items with identical values 
 * when possible.. */
{
struct lineFile *lf = lineFileOpen(wigIn, TRUE);
FILE *f = mustOpen(bedOut, "w");
struct bgOut *out = bgOutNew(f);
char *line;
while (lineFileNextReal(lf, &line))
    {
    char *firstWord = nextWord(&line);
    struct hash *vars = hashVarLine(line, lf->lineIx);
    if (sameString("fixedStep", firstWord))
        convertFixedStepSection(lf, vars, out);
    else if (sameString("variableStep", firstWord))
        convertVariableStepSection(lf, vars, out);
    else
        errAbort("Expecting fixedStep or variableStep line %d of %s, got:\n\t%s", lf->lineIx,
		lf->fileName, line);
    freeHashAndVals(&vars);
    }
bgOutFree(&out);
carefulClose(&f);
}