Exemplo n.º 1
0
void BitsCountPerInterval::CountOverlapsPerInterval() 
{
	vector<struct interval> A, B;
	read_and_map_files(_genome,
			           &_offsets,
					   _bedA,
					   _bedB,
					   &A,
					   &B);


	uint32_t *R = (uint32_t *) malloc (A.size() * sizeof(uint32_t));
	uint32_t tot_overlaps =
			per_interval_count_intersections_bsearch_seq(&A[0],
														 A.size(),
														 &B[0],
														 B.size(),
														 R); 
    for (size_t i = 0; i < _bedA->bedList.size(); ++i) {
		cout << _bedA->bedList[i].chrom <<  "\t" <<
				_bedA->bedList[i].start << "\t" <<
				_bedA->bedList[i].end   << "\t" <<
				R[i] << endl;
	}
}
int main(int argc, char *argv[])
{

    if (argc < 8) {
        fprintf(stderr, "usage:\t%s <size A> <len A> <size B> <len B> "
                "<seed> <P> <size I>\n",
                argv[0]);
        return 1;
    }

    unsigned int size_A = atoi(argv[1]);
    unsigned int len_A = atoi(argv[2]);
    unsigned int size_B = atoi(argv[3]);
    unsigned int len_B = atoi(argv[4]);
    unsigned int seed = atoi(argv[5]);
    unsigned int P = atoi(argv[6]);
    unsigned int size_I = atoi(argv[7]);
    unsigned int size_T = size_I;

    if (size_I > size_B) {
        fprintf(stderr, "Index larger than DB.\n");
        return 1;
    }

    struct interval *A = (struct interval *)
                         malloc(size_A * sizeof(struct interval));
    struct interval *B = (struct interval *)
                         malloc(size_B * sizeof(struct interval));

    unsigned int *R = (unsigned int *)
                      malloc(size_A * sizeof(unsigned int));


    init_genrand(seed);
    generate_interval_sets(A, size_A, len_A, B, size_B, len_B, P);

    start();
    unsigned int O = per_interval_count_intersections_bsearch_seq(
                         A, size_A, B, size_B, R);


    stop();
    unsigned long bsearch_time = report();

    /*
    start();
    unsigned int OB = count_intersections_brute_force_seq(A, size_A, B, size_B);
    stop();
    unsigned long brute_force_time = report();
    */

    int i;
    for (i = 0; i < size_A; i++)
        if (R[i] != 0)
            printf("%d\t%u\n", i, R[i]);
    printf("b:%u,%lu\n",
           O, bsearch_time);
}
Exemplo n.º 3
0
/**
  * @param A intervals in set A
  * @param size_A size of set A
  * @param B intervals in set B
  * @param size_B size of set B
  * @param R prefix sum of the intersection between A and B, A[0] is the number
  * of intervals in B that intersect A[0], A[1] is A[0] + the number of
  * intervals in B that intersect A[1], and so on
  * @param E array that will hold the enumberated interval intersections
  * @param size_E 
  */
unsigned int enumerate_intersections_bsearch_seq(struct interval *A,
												 unsigned int size_A,
												 struct interval *B,
												 unsigned int size_B,
												 unsigned int **R,
												 unsigned int **E)
{

	//{{{ i_do
	*R = (unsigned int *) malloc(size_A * sizeof(unsigned int));

	unsigned int O = per_interval_count_intersections_bsearch_seq(A,
																  size_A,
																  B,
																  size_B,
																  *R);
	//}}} i_do
	
	//{{{ e_scan
	int i;
	// Scan R, the resulting array will define the location within E of the
	// enumerated intersecting intervals for each interval in A 
	// A[i] is allocated E[ R[i-1] ] ... E[ R[i] ]
	for (i = 1; i < size_A; i++)
		(*R)[i] = (*R)[i] + (*R)[i-1];
	//}}}

	//{{{ e_sort
	for (i = 0; i < size_B; i++)
		B[i].order = i;

	qsort(B, size_B, sizeof(struct interval), compare_interval_by_start); 
	//}}} e_sort

	//{{{ e_mem
	unsigned int *B_starts =
			(unsigned int *) malloc(size_B * sizeof(unsigned int));
	for (i = 0; i < size_B; i++)
		B_starts[i] = B[i].start;

	*E = (unsigned int *) malloc(O * sizeof(unsigned int));
	//}}} e_mem

	//{{{ e_do
	unsigned int start = 0, end;
	for (i = 0; i < size_A; i++) {
		if (i != 0)
			start = (*R)[i - 1];

		end = (*R)[i];
		if (end - start > 0) {
			unsigned int from = bsearch_seq(A[i].end,
											B_starts,
											size_B,
											-1,
											size_B);

			while ( ( B_starts[from] == A[i].end) && from < size_B)
				++from;

			while (  (end - start) > 0 ) {
				if ( (A[i].start <= B[from].end) &&
					 (A[i].end >= B[from].start) ) {
					(*E)[start] = B[from].order;
					start++;
				} 

				--from;
			}
		}
	}
	//}}} e_do

	//{{{ e_mem
	free(B_starts);
	//}}} e_mem
	
	return O;
}