예제 #1
0
int main(int argc, char *argv[])
/* Process command line. */
{
    if (argc != 6)
	errAbort("bad running of bwmake");
    struct metaBig *mb = metaBigOpen(argv[1], NULL);
    FILE *out = mustOpen(argv[2], "w");
    int decimals = sqlSigned(argv[3]);
    struct bed *section;
    double na = NANUM;
    boolean condense = FALSE;
    enum wigOutType wot = get_wig_out_type(argv[4]);
    if (sameString(argv[5], "condense"))
	condense = TRUE;
    for (section = mb->sections; section != NULL; section = section->next)
    {
	struct perBaseWig *pbw = perBaseWigLoadSingleContinue(mb, section->chrom, section->chromStart, 
							      section->chromEnd, FALSE, na);
	perBaseWigOutputNASkip(pbw, out, wot, decimals, NULL, FALSE, condense);
	perBaseWigFree(&pbw);
    }
    carefulClose(&out);
    metaBigClose(&mb);
    return 0;
}
예제 #2
0
void perBaseWigFreeList(struct perBaseWig** pList)
/* Free up a list of perBaseWigs */
{
    struct perBaseWig* reg;
    while ((reg = slPopHead(pList)) != NULL)
        perBaseWigFree(&reg);
    freez(pList);
}
예제 #3
0
void bwtool_extract(struct hash *options, char *regions, unsigned decimals, double fill,
		  char *style_s, char *bigfile, char *tmp_dir, char *outputfile)
/* bwtool_extract - main for the extract program */
{
    boolean tabs = (hashFindVal(options, "tabs") != NULL) ? TRUE : FALSE;
    boolean locus_name = (hashFindVal(options, "locus-name") != NULL) ? TRUE : FALSE;
    int orig_size = 0;
    struct bed6 *region_list = readBed6SoftAndSize(regions, &orig_size);
    struct metaBig *mb = metaBigOpenWithTmpDir(bigfile, tmp_dir, NULL);
    if (!mb)
	errAbort("problem opening %s", bigfile);
    FILE *out = mustOpen(outputfile, "w");
    struct bed6 *section;
    enum style_type style = nothing;
    if (sameWord(style_s, "bed"))
	style = bed;
    else if (sameWord(style_s, "jsp"))
	style = jsp;
    else
	errAbort("please specify a valid style");
    int section_num = 1;
    /* loop through each region */
    for (section = region_list; section != NULL; section = section->next)
    {
	struct perBaseWig *pbw = perBaseWigLoadSingleContinue(mb, section->chrom, section->chromStart,
							      section->chromEnd, (section->strand[0] == '-') ? TRUE : FALSE, fill);
	if (style == bed)
	    /* for bed there is no name manipulation */
	    extractOutBed(out, section, orig_size, decimals, pbw, tabs);
	else
	{
	    /* for jsp output there is some name manipulation that could be done prior to outputting */
	    char buf[128];
	    if (locus_name)
	    {
		safef(buf, sizeof(buf), "%s:%d-%d", section->chrom, section->chromStart+1, section->chromEnd);
		if (section->name)
		    freeMem(section->name);
		section->name = cloneString(buf);
	    }
	    else if ((orig_size < 4) || sameString(section->name, "."))
	    {
		safef(buf, sizeof(buf), "region_%d", section_num);
		if (section->name)
		    freeMem(section->name);
		section->name = cloneString(buf);
	    }
	    extractOutJsp(out, section, decimals, pbw);
	}
	perBaseWigFree(&pbw);
	section_num++;
    }
    metaBigClose(&mb);
    bed6FreeList(&region_list);
    carefulClose(&out);
}
예제 #4
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);
}
예제 #5
0
void free_perBaseMatrix(struct perBaseMatrix** ppMat)
/* freeing up a wig matrix */
{
    struct perBaseMatrix* pmat = *ppMat;
    int i;
    for (i = 0; i < pmat->nrow; i++) {
        struct perBaseWig* pbw = pmat->array[i];
        perBaseWigFree(&pbw);
    }
    freeMem(pmat->array);
    freeMem(pmat->matrix);
    freez(ppMat);
}
예제 #6
0
struct perBaseMatrix* load_ave_perBaseMatrix(struct metaBig* mb, struct bed6* regions, int tile_size, double fill)
/* the matrix is tiled averages instead the values at each base */
{
    struct perBaseMatrix* pmat;
    struct bed6* item;
    int i, j, k;
    double na = NANUM;
    if (!mb->sections)
        return NULL;
    item = regions;
    if (!regions || (((regions->chromEnd - regions->chromStart) % tile_size) != 0))
        return NULL;
    AllocVar(pmat);
    pmat->nrow = slCount(regions);
    pmat->ncol = item->chromEnd - item->chromStart;
    pmat->ncol /= tile_size;
    AllocArray(pmat->array, pmat->nrow);
    AllocArray(pmat->matrix, pmat->nrow);
    for (i = 0; (i < pmat->nrow) && (item != NULL); i++, item = item->next) {
        struct perBaseWig* pbw;
        struct perBaseWig* small = alloc_fill_perBaseWig(item->chrom, item->chromStart, item->chromStart + pmat->ncol, fill);
        small->chromEnd = item->chromEnd;
        pbw = perBaseWigLoadSingleContinue(mb, item->chrom, item->chromStart, item->chromEnd, item->strand[0] == '-', fill);
        for (j = 0; j < pmat->ncol; j++) {
            double sum = 0, ave;
            int n = 0;
            for (k = j * tile_size; k < j * tile_size + tile_size; k++)
                if (!isnan(pbw->data[k])) {
                    sum += pbw->data[k];
                    n++;
                }
            if (n > 0)
                ave = sum / n;
            else
                ave = na;
            small->data[j] = ave;
        }
        small->name = cloneString(item->name);
        small->score = item->score;
        small->strand[0] = item->strand[0];
        pmat->array[i] = small;
        pmat->matrix[i] = small->data;
        perBaseWigFree(&pbw);
    }
    return pmat;
}
예제 #7
0
struct perBaseWig* perBaseWigLoadSingleMetaContinue(struct metaBig* mb, char* chrom,
    int start, int end, boolean reverse, double fill, int size)
/* Load all the regions into one perBaseWig, but with gaps filled  */
/* in with NA value */
{
    struct perBaseWig* pbw = perBaseWigLoadSingleContinue(mb, chrom, start, end, reverse, fill);
    struct perBaseWig* meta = alloc_fill_perBaseWig(chrom, start, start + size, fill);
    float step = (float)pbw->len / meta->len;
    int i;
    for (i = 0; i < meta->len; i++) {
        float pbw_s = step * i;
        float pbw_e = pbw_s + step;
        if ((int)pbw_e - (int)pbw_s == 0)
            meta->data[i] = pbw->data[(int)pbw_s];
        else {
            double area = 0;
            double len = 0;
            int j;
            int firstix = floor(pbw_s);
            int between_s = ceil(pbw_s);
            int between_e = floor(pbw_e);
            int lastixp1 = ceil(pbw_e);
            if ((between_s > firstix) && (!isnan(pbw->data[firstix]))) {
                area += ((float)between_s - pbw_s) * pbw->data[firstix];
                len += (float)between_s - pbw_s;
            }
            for (j = between_s; j < between_e; j++)
                if (!isnan(pbw->data[j])) {
                    area += pbw->data[j];
                    len += 1.0;
                }
            if ((between_e < pbw->len) && (lastixp1 > between_e) && (!isnan(pbw->data[between_e]))) {
                area += (pbw_e - (float)between_e) * pbw->data[between_e];
                len += (pbw_e - (float)between_e);
            }
            if (len > 0)
                meta->data[i] = area / len;
            else
                meta->data[i] = fill;
        }
    }
    perBaseWigFree(&pbw);
    return meta;
}
예제 #8
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);
}
예제 #9
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);
}
예제 #10
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);
}
예제 #11
0
파일: roll.c 프로젝트: CRG-Barcelona/bwtool
void bwtool_roll(struct hash *options, char *favorites, char *regions, unsigned decimals, double fill,
		 enum wigOutType wot, char *command, char *size_s, char *bigfile, char *tmp_dir, char *outputfile)
/* bwtool_roll - main for the rolling-mean program */
/* this function is too long. it'd be nice to break it up some time. */
{
    struct metaBig *mb = metaBigOpenWithTmpDir(bigfile, tmp_dir, regions);
    int step = (int)sqlUnsigned((char *)hashOptionalVal(options, "step", "1"));
    int max_na = (int)sqlSigned((char *)hashOptionalVal(options, "max-NA", "-1"));
    char *min_mean_s = (char *)hashOptionalVal(options, "min-mean", "unused");
    double min_mean = -DBL_MAX;
    if (!sameString(min_mean_s,"unused"))
	min_mean = sqlDouble(min_mean_s);
    int size = sqlSigned(size_s);
    if (max_na == -1)
	max_na = size/2;
    else if (max_na > size)
	max_na = size - 1;
    if (size < 1)
	errAbort("size must be >= 1 for bwtool window");
    FILE *out = (outputfile) ? mustOpen(outputfile, "w") : stdout;
    struct bed *section;
    boolean broken = TRUE;  /* for headers */
    enum roll_command com;
    if (sameWord(command, "mean"))
	com = roll_mean;
    else if (sameWord(command, "total"))
	com = roll_total;
    else
	errAbort("Pick a roll command: mean or total");
    for (section = mb->sections; section != NULL; section = section->next)
    {
	if (size <= section->chromEnd - section->chromStart)
	{
	    struct perBaseWig *pbw = perBaseWigLoadSingleContinue(mb, section->chrom, section->chromStart,
								  section->chromEnd, FALSE, fill);
	    int i, j;
	    int len = section->chromEnd - section->chromStart;
	    double total = 0;
	    int num_na = 0;
	    /* load data */
	    for (i = 0; i < size; i++)
		add_to_tots(pbw->data[i], &num_na, &total);
	    i = 0;
	    do
	    {
		int s = pbw->chromStart + i;
		int e = pbw->chromStart + i + size;
 		int st = step;
		double mean = total/(size - num_na);
		/* the next two calculations center it */
		s += size/2 - step/2;
		e = s + step;
		/* output */
		if ((num_na <= max_na) && (mean >= min_mean))
		{
		    double out_val;
		    if (com == roll_mean)
			out_val = mean;
		    else
			out_val = total;
		    if (wot == fixStepOut)
		    {
			if (broken)
			    fprintf(out, "fixedStep chrom=%s start=%d step=%d span=%d\n", pbw->chrom, s+1, step, step);
			fprintf(out, "%0.*f\n", decimals, out_val);
		    }
		    else if (wot == varStepOut)
		    {
			if (broken)
			    fprintf(out, "variableStep chrom=%s span=%d\n", pbw->chrom, step);
			fprintf(out, "%d\t%0.*f\n", s+1, decimals, out_val);
		    }
		    else
			fprintf(out, "%s\t%d\t%d\t%0.*f\n", pbw->chrom, s, e, decimals, out_val);
		    broken = FALSE;
		}
		else
		    broken = TRUE;
		/* move */
		while (st > 0)
		{
		    if (i + size <= pbw->len)
		    {
			add_to_tots(pbw->data[i+size], &num_na, &total);
			sub_from_tots(pbw->data[i], &num_na, &total);
		    }
		    else
			break;
		    i++;
		    st--;
		}
	    } while (i <= pbw->len - size);
	    perBaseWigFree(&pbw);
	}
    }
    metaBigClose(&mb);
    carefulClose(&out);
}
예제 #12
0
파일: window.c 프로젝트: hjanime/bwtool
void bwtool_window(struct hash *options, char *favorites, char *regions, unsigned decimals, 
                   double fill, char *size_s, char *bigfile, char *output_file)
/* bwtool_window - main for the windowing program */
{
    struct metaBig *mb = metaBigOpen(bigfile, regions);
    boolean skip_na = (hashFindVal(options, "skip-NA") != NULL) ? TRUE : FALSE;
    if (!isnan(fill) && skip_na)
	errAbort("cannot use -skip_na with -fill");
    boolean center = (hashFindVal(options, "center") != NULL) ? TRUE : FALSE;
    int step = (int)sqlUnsigned((char *)hashOptionalVal(options, "step", "1"));
    int size = sqlSigned(size_s);
    if (size < 1)
	errAbort("size must be >= 1 for bwtool window");
    FILE *out = (output_file) ? mustOpen(output_file, "w") : stdout;
    struct bed *section;
    for (section = mb->sections; section != NULL; section = section->next)
    {
	if (size <= section->chromEnd - section->chromStart)
	{
	    struct perBaseWig *pbw = perBaseWigLoadSingleContinue(mb, section->chrom, section->chromStart,
								  section->chromEnd, FALSE, fill);
	    int i, j;
	    for (i = 0; i <= pbw->len - size; i += step)
	    {
		int s = pbw->chromStart + i;
		int e = pbw->chromStart + i + size;
		if (center)
		{
		    s += size/2 - step/2;
		    e = s + step;
		}
		boolean has_NA = FALSE;
		if (skip_na)
		{
		    for (j = i; j < i + size; j++)
			if (isnan(pbw->data[j]))
			{
			    i = j-step+1;
			    has_NA = TRUE;
			    break;
			}
		}
		if (!has_NA)
 		{
		    fprintf(out, "%s\t%d\t%d\t", pbw->chrom, s, e);
		    for (j = i; j < i + size; j++)
			if (isnan(pbw->data[j]) && (j == i + size - 1))
			    fprintf(out, "NA\n");
			else if (isnan(pbw->data[j]))
			    fprintf(out, "NA,");
			else if (j == i + size - 1)
			    fprintf(out, "%0.*f\n", decimals, pbw->data[j]);
			else
			    fprintf(out, "%0.*f,", decimals, pbw->data[j]);
		}
	    }
	    perBaseWigFree(&pbw);
	}
    }
    metaBigClose(&mb);
    carefulClose(&out);
}