예제 #1
0
파일: find.c 프로젝트: CRG-Barcelona/bwtool
void bwtool_find_extrema(struct hash *options, char *favorites, char *regions, unsigned decimals, double fill, char *bigfile, char *tmp_dir, char *outputfile)
/* find local extrema */
{
    unsigned min_sep = sqlUnsigned((char *)hashOptionalVal(options, "min-sep", "0"));
    char *other_bigfile = (char *)hashOptionalVal(options, "against", NULL);
    enum ex_removal rem = get_removal(options);
    struct metaBig *main_big = metaBigOpen_check(bigfile, tmp_dir, regions);
    struct metaBig *other_big = NULL;
    struct extrema *main_list;
    struct extrema *other_list = NULL;
    struct extrema *ex;
    unsigned shift = 0;
    FILE *out;
    if (other_bigfile)
    {
	char *num;
	if (rem == no_removal)
	    errAbort("must specify either -maxima or -minima with -against");
	if (!strchr(other_bigfile, ','))
	    errAbort("must specify shift limit in -against option");
	num = chopPrefixAt(other_bigfile, ',');
	shift = sqlUnsigned(num);
	other_big = metaBigOpen_check(other_bigfile, tmp_dir, regions);
    }
    if (!main_big || (!other_big && other_bigfile))
	errAbort("could not open bigWig file");
    main_list = extrema_find(main_big, min_sep, rem);
    slReverse(&main_list);
    if (other_bigfile)
    {
	other_list = extrema_find(other_big, min_sep, rem);
	extrema_find_shifts(main_list, other_list, shift);
    }
    metaBigClose(&main_big);
    if (other_big)
	metaBigClose(&other_big);
    out = mustOpen(outputfile, "w");
    if (other_bigfile)
	for (ex = main_list; ex != NULL; ex = ex->next)
	    fprintf(out, "%s\t%d\t%d\t%d\t1000\t%c\n", ex->chrom, ex->chromStart, ex->chromStart+1, (int)ex->val, ex->min_or_max);
    else
    {
	slSort(&main_list, extrema_bed_cmp);
	for (ex = main_list; ex != NULL; ex = ex->next)
	    fprintf(out, "%s\t%d\t%d\t%0.*f\t1000\t%c\n", ex->chrom, ex->chromStart, ex->chromStart+1, decimals, ex->val, ex->min_or_max);
    }
    carefulClose(&out);
    extrema_free_list(&main_list);
}
예제 #2
0
void bwtool_shift(struct hash *options, char *favorites, char *regions, unsigned decimals, enum wigOutType wot,
		  boolean condense, char *val_s, char *bigfile, char *tmp_dir, char *outputfile)
/* bwtool_shift - main for shifting program */
{
    const double na = NANUM;
    int shft = sqlSigned(val_s);
    int abs_shft = abs(shft);
    struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, regions);
    if (!mb)
	errAbort("problem opening %s", bigfile);
    char wigfile[512];
    safef(wigfile, sizeof(wigfile), "%s.tmp.wig", outputfile);
    FILE *out = mustOpen(wigfile, "w");
    struct bed *section;
    boolean up = TRUE;
    if (shft > 0)
	up = FALSE;
    if (shft == 0)
	errAbort("it doesn't make sense to shift by zero.");
    for (section = mb->sections; section != NULL; section = section->next)
    {
	struct perBaseWig *pbw = perBaseWigLoadSingleContinue(mb, section->chrom, section->chromStart,
							      section->chromEnd, FALSE, na);
	int i;
	/* if the shift size is bigger than the section, NA the entire thing */
	int size = pbw->len;
	if (abs_shft >= size)
	    for (i = 0; i < size; i++)
		pbw->data[i] = na;
	else
	{
	    if (!up)
	    {
		for (i = size-1; i >= abs_shft; i--)
		    pbw->data[i] = pbw->data[i - abs_shft];
		for (; i >= 0; i--)
		    pbw->data[i] = na;
	    }
	    else
	    {
		for (i = 0; i < size - abs_shft; i++)
		    pbw->data[i] = pbw->data[i + abs_shft];
		for (; i < size; i++)
		    pbw->data[i] = na;
	    }
	}
	perBaseWigOutputNASkip(pbw, out, wot, decimals, NULL, FALSE, condense);
	perBaseWigFree(&pbw);
    }
    carefulClose(&out);
    writeBw(wigfile, outputfile, mb->chromSizeHash);
    remove(wigfile);
    metaBigClose(&mb);
}
예제 #3
0
파일: sax.c 프로젝트: hjanime/bwtool
void bwtool_sax(struct hash *options, char *favorites, char *regions, unsigned decimals, char *alpha_s, char *bigfile,
		char *outputfile)
/* bwtool_sax - main for the sax symbol program */
{
    struct metaBig *mb = metaBigOpen_check(bigfile, regions);
    struct bed *bed;
    int alpha = (alpha_s != NULL) ? sqlUnsigned(alpha_s) : 8;
    unsigned itStart = sqlUnsigned((char *)hashOptionalVal(options, "iterate-start", (alpha_s != NULL) ? alpha_s : "8"));
    unsigned itEnd = sqlUnsigned((char *)hashOptionalVal(options, "iterate-end", (alpha_s != NULL) ? alpha_s : "8"));
    unsigned window = sqlUnsigned((char *)hashOptionalVal(options, "sax-window", "0"));
    char *mean_s = (char *)hashOptionalVal(options, "mean", NULL);
    char *std_s = (char *)hashOptionalVal(options, "std", NULL);
    if (mb->type != isaBigWig)
	errAbort("%s doesn't seem to be a bigWig", bigfile);
    double mean = bigWigMean(mb->big.bbi);
    double std = bigWigStd(mb->big.bbi);
    if (mean_s)
	mean = sqlDouble(mean_s);
    if (std_s)
	std = sqlDouble(std_s);
    FILE *out;
    boolean do_std = (hashLookup(options, "std") != NULL);
    boolean do_mean = (hashLookup(options, "mean") != NULL);
    boolean bed4 = (hashLookup(options, "bed4") != NULL);
    boolean wig_out = (hashLookup(options, "add-wig-out") != NULL);
    if (do_mean || do_std)
    {
	if (!do_std || !do_mean)
	    errAbort("if -mean is specified, -std is required, and vice versa");
	else if (std <= 0)
	    errAbort("-std must be > 0");
    }
    out = mustOpen(outputfile, "w");
    for (bed = mb->sections; bed != NULL; bed = bed->next)
    {
	/* print a header */
	if ((itStart == itEnd) && !bed4)
	{
	    if (bed == mb->sections)
		fprintf(out, "# alphabet size = %d\n", alpha);
	    wigsax_fasta(out, mb, bed, alpha, window, mean, std);
	}
	else
	{
	    if (bed == mb->sections)
		fprintf(out, "# alphabet size = %d\n", alpha);
	    wigsax_bed4(out, mb, bed, alpha, window, mean, std, wig_out);
	}
    }
    metaBigClose(&mb);
    carefulClose(&out);
}
예제 #4
0
파일: find.c 프로젝트: CRG-Barcelona/bwtool
void bwtool_find_thresh(struct hash *options, char *favorites, char *regions, double fill,
			char *thresh_type, char *thresh_s, char *bigfile, char *tmp_dir, char *outputfile)
/* the other kind of finding, based on thresholding. */
{
    boolean inverse = (hashFindVal(options, "inverse") != NULL) ? TRUE : FALSE;
    enum bw_op_type op= get_bw_op_type(thresh_type, inverse);
    struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, regions);
    double thresh = sqlDouble(thresh_s);
    FILE *out = mustOpen(outputfile, "w");
    struct bed out_bed;
    struct bed *section;
    for (section = mb->sections; section != NULL; section = section->next)
    {
	struct perBaseWig *pbwList = perBaseWigLoadContinue(mb, section->chrom, section->chromStart,
							      section->chromEnd);
	struct perBaseWig *pbw;
	int i, len;
	if (pbwList)
	{
	    out_bed.chrom = pbwList->chrom;
	    for (pbw = pbwList; pbw != NULL; pbw = pbw->next)
	    {
		i = 0;
		len = pbw->chromEnd - pbw->chromStart;
		out_bed.chromStart = out_bed.chromEnd = 0;
		while (i < len)
		{
		    while ((i < len) && (!fit_thresh(pbw->data[i], thresh, op)))
			i++;
		    out_bed.chromStart = i + pbw->chromStart;
		    while ((i < len) && (fit_thresh(pbw->data[i], thresh, op)))
			i++;
		    out_bed.chromEnd = i + pbw->chromStart;
		    if (out_bed.chromEnd > out_bed.chromStart)
			bedTabOutN(&out_bed, 3, out);
		}
	    }
	perBaseWigFree(&pbwList);
	}
    }
    metaBigClose(&mb);
    carefulClose(&out);
}
예제 #5
0
파일: lift.c 프로젝트: hjanime/bwtool
void bwtool_lift(struct hash *options, char *favorites, char *regions, unsigned decimals,
		 enum wigOutType wot, char *bigfile, char *chainfile, char *outputfile)
/* bwtool_lift - main for lifting program */
{
    struct hash *sizeHash = NULL;
    struct hash *chainHash = readLiftOverMapChainHash(chainfile);
    struct hash *gpbw = NULL;
    char *size_file = hashFindVal(options, "sizes");
    char *bad_file = hashFindVal(options, "unlifted");
    if (size_file)
	sizeHash = readCsizeHash(size_file);
    else
	sizeHash = qSizeHash(chainfile);
    gpbw = genomePbw(sizeHash);
    struct metaBig *mb = metaBigOpen_check(bigfile, regions);
    char wigfile[512];
    safef(wigfile, sizeof(wigfile), "%s.tmp.wig", outputfile);
    FILE *out = mustOpen(wigfile, "w");
    struct hashEl *elList = hashElListHash(gpbw);
    struct hashEl *el;
    verbose(2,"starting first pass\n");
    do_pass1(mb, chainHash, gpbw);
    verbose(2, "starting second pass\n");
    do_pass2(mb, chainHash, gpbw);
    verbose(2,"starting final pass\n");
    do_final_pass(mb, chainHash, gpbw, bad_file);
    slSort(&elList, pbwHashElCmp);
    for (el = elList; el != NULL; el = el->next)
    {
	struct perBaseWig *pbw = (struct perBaseWig *)el->val;
	perBaseWigOutputNASkip(pbw, out, wot, decimals, NULL, FALSE, FALSE);
    }
    hashElFreeList(&elList);
    carefulClose(&out);
    hashFreeWithVals(&chainHash, freeChainHashMap);
    hashFreeWithVals(&gpbw, perBaseWigFree);
    writeBw(wigfile, outputfile, sizeHash);
    hashFree(&sizeHash);
    remove(wigfile);
    metaBigClose(&mb);
}
예제 #6
0
파일: fill.c 프로젝트: CRG-Barcelona/bwtool
void bwtool_fill(struct hash *options, char *favorites, char *regions, unsigned decimals, enum wigOutType wot,
		 boolean condense, char *val_s, char *bigfile, char *tmp_dir, char *outputfile)
/* bwtool_fill - main for filling program */
{
    double val = sqlDouble(val_s);
    struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, regions);
    char wigfile[512];
    safef(wigfile, sizeof(wigfile), "%s.tmp.wig", outputfile);
    FILE *out = mustOpen(wigfile, "w");
    struct bed *section;
    int i;
    for (section = mb->sections; section != NULL; section = section->next)
    {
	struct perBaseWig *pbw = perBaseWigLoadSingleContinue(mb, section->chrom, section->chromStart,
							      section->chromEnd, FALSE, val);
	perBaseWigOutput(pbw, out, wot, decimals, NULL, FALSE, condense);
	perBaseWigFree(&pbw);
    }
    carefulClose(&out);
    writeBw(wigfile, outputfile, mb->chromSizeHash);
    remove(wigfile);
    metaBigClose(&mb);
}
예제 #7
0
파일: find.c 프로젝트: CRG-Barcelona/bwtool
void bwtool_find_max(struct hash *options, char *favorites, char *regions, double fill,
		     char *bigfile, char *tmp_dir, char *outputfile)
/* find max points in a range */
{
    boolean med_base = (hashFindVal(options, "median-base") != NULL) ? TRUE : FALSE;
    boolean with_max = (hashFindVal(options, "with-max") != NULL) ? TRUE : FALSE;
    struct metaBig *mb = metaBigOpen_check(bigfile, tmp_dir, NULL);
    FILE *out = mustOpen(outputfile, "w");
    struct bed6 *sections6 = readBed6Soft(regions);
    struct bed *sections = bed12FromBed6(&sections6);
    struct bed *section;
    for (section = sections; section != NULL; section = section->next)
    {
	struct perBaseWig *pbwList = perBaseWigLoadContinue(mb, section->chrom, section->chromStart,
							      section->chromEnd);
	struct perBaseWig *pbw;
	struct slInt *ii;
	int i, size;
	double max = -DBL_MAX;
	struct slInt *list = NULL;
	for (pbw = pbwList; pbw != NULL; pbw = pbw->next)
	{
	    int pbw_off = pbw->chromStart - section->chromStart;
	    for (i = 0; i < pbw->len; i++)
	    {
		if (pbw->data[i] > max)
		{
		    slFreeList(&list);
		    struct slInt *new_int = slIntNew(i + pbw_off);
		    slAddHead(&list, new_int);
		    max = pbw->data[i];
		}
		else if (pbw->data[i] == max)
		{
		    struct slInt *new_int = slIntNew(i + pbw_off);
		    slAddHead(&list, new_int);
		}
	    }
	}
	slReverse(&list);
	if (list)
	{
	    size = slCount(list);
	    if (med_base)
	    {
		section->blockCount = 1;
		AllocArray(section->blockSizes, sizeof(int));
		AllocArray(section->chromStarts, sizeof(int));
		section->blockSizes[0] = 1;
		section->chromStarts[0] = median_base_calc(&list);
	    }
	    else
	    {
		section->blockCount = size;
		AllocArray(section->blockSizes, sizeof(int) * size);
		AllocArray(section->chromStarts, sizeof(int) * size);
		for (i = 0, ii = list; (i < size) && (ii != NULL); i++, ii = ii->next)
		{
		    section->blockSizes[i] = 1;
		    section->chromStarts[i] = ii->val;
		}
	    }
	    if (!with_max)
		bedTabOutN(section, 12, out);
	    else
	    {
		bedOutputN(section, 12, out, '\t', '\t');
		fprintf(out, "%f\n", max);
	    }
	    slFreeList(&list);
	}
	perBaseWigFree(&pbwList);
    }
    metaBigClose(&mb);
    bedFreeList(&sections);
    carefulClose(&out);
}