GtBittab* gt_golomb_encode(GtGolomb *golomb, unsigned long x) { unsigned long quotient, remain, mask, idx_i; GtBittab *code; gt_assert(golomb); quotient = x / golomb->median; remain = x - quotient * golomb->median; if (golomb->len == 0) { code = gt_bittab_new((quotient + 1) + 1); } else if (remain < golomb->two_pow_len - golomb->median) { code = gt_bittab_new((quotient + 1) + (golomb->len - 1)); mask = 1UL << (golomb->len - 2); for (idx_i = 0; idx_i < golomb->len - 1; idx_i++) { if (remain & mask) gt_bittab_set_bit(code, quotient + 1 + idx_i); mask >>= 1; } }
void gt_string_matching_shift_and(const char *s, unsigned long n, const char *p, unsigned long m, ProcessMatch process_match, void *data) { GtBittab *D, *B[UCHAR_MAX] = { NULL }; unsigned long i, j; gt_assert(s && p); if (m > n || !m || !n) /* no match possible */ return; /* preprocessing */ for (j = 0; j < m; j++) { if (!B[(unsigned) p[j]]) B[(unsigned) p[j]] = gt_bittab_new(m); gt_bittab_set_bit(B[(unsigned) p[j]], j); } /* searching */ D = gt_bittab_new(m); for (i = 0; i < n; i++) { gt_bittab_shift_left_equal(D); gt_bittab_set_bit(D, 0); if (B[(unsigned) s[i]]) gt_bittab_and_equal(D, B[(unsigned) s[i]]); else gt_bittab_unset(D); if (gt_bittab_bit_is_set(D, m - 1) && process_match) { if (process_match(i - m + 1, data)) break; } } /* free */ for (i = 0; i < UCHAR_MAX; i++) gt_bittab_delete(B[i]); gt_bittab_delete(D); }
static bool splice_form_is_valid(GtBittab *SA_p, const ConsensusSA *csa) { GtBittab *SA_p_complement; /* SA \ SA_p */ unsigned long sa, sa_prime; bool incompatible_found, valid = true; SA_p_complement = gt_bittab_new(csa->number_of_sas); gt_bittab_complement(SA_p_complement, SA_p); for (sa_prime = gt_bittab_get_first_bitnum(SA_p_complement); sa_prime != gt_bittab_get_last_bitnum(SA_p_complement); sa_prime = gt_bittab_get_next_bitnum(SA_p_complement, sa_prime)) { incompatible_found = false; for (sa = gt_bittab_get_first_bitnum(SA_p); sa != gt_bittab_get_last_bitnum(SA_p); sa = gt_bittab_get_next_bitnum(SA_p, sa)) { if (!compatible(csa, sa, sa_prime)) { incompatible_found = true; break; } } if (!incompatible_found) { valid = false; break; } } gt_bittab_delete(SA_p_complement); return valid; }
/* necessary to call consensus_sa() */ static void process_splice_form_func(GtArray *spliced_alignments_in_form, GT_UNUSED const void *set_of_sas, GT_UNUSED GtUword number_of_sas, GT_UNUSED size_t size_of_sa, void *userdata) { GthPGL *pgl = (GthPGL*) userdata; GthAGS *ags; GtBittab *assemblytab; GtUword i; ags = gth_ags_new(pgl); assemblytab = gt_bittab_new(gt_array_size(pgl->saclusters)); for (i = 0; i < gt_array_size(spliced_alignments_in_form); i++) { gt_bittab_set_bit(assemblytab, *(GtUword*) gt_array_get(spliced_alignments_in_form, i)); } gth_build_AGS_from_assembly(ags, assemblytab, pgl->saclusters); gt_bittab_delete(assemblytab); gt_array_add(pgl->assemblies, ags); }
int encdesc_read_header(GtEncdesc *encdesc, FILE *fp, GtError *err) { int had_err = 0; GtUword cur_field_num; DescField *cur_field; had_err = encdesc_header_io_basics(encdesc, fp, gt_io_error_fread, err); if (!had_err) encdesc->fields = gt_calloc((size_t) encdesc->num_of_fields, sizeof (DescField)); for (cur_field_num = 0; !had_err && cur_field_num < encdesc->num_of_fields; cur_field_num++) { cur_field = &encdesc->fields[cur_field_num]; /* cur_field->bits_per_num = 0; */ /* cur_field->prev_value = 0; */ had_err = io_field_sep_and_is_cons(cur_field, fp, gt_io_error_fread, err); if (!had_err && cur_field->is_cons) { had_err = io_cons_field_header(cur_field, fp, gt_io_error_fread, err); } else if (!had_err) { had_err = gt_io_error_fread_one(cur_field->is_numeric, fp, err); if (!had_err && cur_field->is_numeric) { had_err = io_numeric_field_header(cur_field, fp, gt_io_error_fread, err); } else if (!had_err) { had_err = io_field_len_header(cur_field, fp, gt_io_error_fread, err); if (!had_err) { cur_field->bittab = gt_bittab_new(cur_field->len); cur_field->data = gt_calloc((size_t) (cur_field->len + 1), sizeof (char)); had_err = gt_io_error_fread(cur_field->data, sizeof (char), (size_t) cur_field->len, fp, err); } if (!had_err) had_err = read_field_header_bittab(cur_field, fp, err); if (!had_err) { cur_field->chars = gt_calloc((size_t) (cur_field->max_len + 1), sizeof (cur_field->chars)); had_err = read_field_char_dists(cur_field, fp, err); } } } } return had_err; }
GtClusteredSet* gt_clustered_set_union_find_new(GtUword num_of_elems, GT_UNUSED GtError *err) { GtClusteredSet *cs; GtClusteredSetUF *cs_uf; cs = gt_clustered_set_create(gt_clustered_set_union_find_class()); cs_uf = (GtClusteredSetUF*) cs; cs_uf->num_of_elems = num_of_elems; cs_uf->cluster_elems = gt_calloc(num_of_elems, sizeof (GtClusteredSetUFElem)); cs_uf->cluster_info = gt_array_new(sizeof (GtClusteredSetUFClusterInfo)); cs_uf->in_cluster = gt_bittab_new(num_of_elems); cs_uf->next_free_cluster_info = (GtUword) 0; return cs; }
static void compute_R(GtBittab **R, GtBittab **C, GtBittab **right, unsigned long number_of_sas) { unsigned long sa_1, sa_2, sa_1_size = 0, sa_2_size; long sa; GtBittab *tmpset = gt_bittab_new(number_of_sas); for (sa = number_of_sas-1; sa >= 0; sa--) { sa_1 = GT_UNDEF_ULONG; if (!gt_bittab_is_true(right[sa])) { /* bittab is empty */ gt_bittab_equal(R[sa], C[sa]); } else { for (sa_2 = gt_bittab_get_first_bitnum(right[sa]); sa_2 != gt_bittab_get_last_bitnum(right[sa]); sa_2 = gt_bittab_get_next_bitnum(right[sa], sa_2)) { if (sa_1 == GT_UNDEF_ULONG) { sa_1 = sa_2; gt_bittab_or(tmpset, R[sa_1], C[sa]); sa_1_size = gt_bittab_count_set_bits(tmpset); } else { gt_bittab_or(tmpset, R[sa_2], C[sa]); sa_2_size = gt_bittab_count_set_bits(tmpset); if (sa_2_size > sa_1_size) { sa_1 = sa_2; sa_1_size = sa_2_size; } } } gt_assert(sa_1 != GT_UNDEF_ULONG); gt_bittab_or(R[sa], R[sa_1], C[sa]); } } gt_bittab_delete(tmpset); }
static void compute_L(GtBittab **L, GtBittab **C, GtBittab **left, unsigned long number_of_sas) { unsigned long sa, sa_1, sa_2, sa_1_size = 0, sa_2_size; GtBittab *tmpset = gt_bittab_new(number_of_sas); for (sa = 0; sa < number_of_sas; sa++) { sa_1 = GT_UNDEF_ULONG; if (!gt_bittab_is_true(left[sa])) { /* bittab is empty */ gt_bittab_equal(L[sa], C[sa]); } else { for (sa_2 = gt_bittab_get_first_bitnum(left[sa]); sa_2 != gt_bittab_get_last_bitnum(left[sa]); sa_2 = gt_bittab_get_next_bitnum(left[sa], sa_2)) { if (sa_1 == GT_UNDEF_ULONG) { sa_1 = sa_2; gt_bittab_or(tmpset, L[sa_1], C[sa]); sa_1_size = gt_bittab_count_set_bits(tmpset); } else { gt_bittab_or(tmpset, L[sa_2], C[sa]); sa_2_size = gt_bittab_count_set_bits(tmpset); if (sa_2_size > sa_1_size) { sa_1 = sa_2; sa_1_size = sa_2_size; } } } gt_assert(sa_1 != GT_UNDEF_ULONG); gt_bittab_or(L[sa], L[sa_1], C[sa]); } } gt_bittab_delete(tmpset); }
int gt_canvas_cairo_visit_line_pre(GtCanvas *canvas, GtLine *line, GtError *err) { int had_err = 0; double lheight, bar_vspace = BAR_VSPACE_DEFAULT, theight = gt_graphics_get_text_height(canvas->pvt->g), captionspace = CAPTION_BAR_SPACE_DEFAULT; bool show_block_captions = true; gt_assert(canvas && line); if (gt_style_get_num(canvas->pvt->sty, "format", "block_caption_space", &captionspace, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_bool(canvas->pvt->sty, "format", "show_block_captions", &show_block_captions, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } had_err = gt_line_get_height(line, &lheight, canvas->pvt->sty, err); if (!had_err) { if (gt_style_get_num(canvas->pvt->sty, "format", "bar_vspace", &bar_vspace, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_style_get_num(canvas->pvt->sty, "format", "block_caption_font_size", &theight, NULL, err) == GT_STYLE_QUERY_ERROR) { return -1; } if (gt_line_has_captions(line) && show_block_captions) canvas->pvt->y += theight + captionspace; canvas->pvt->bt = gt_bittab_new(canvas->pvt->width); gt_bittab_unset(canvas->pvt->bt); canvas->pvt->y += lheight/2; } return had_err; }
static void compute_csas(ConsensusSA *csa) { unsigned long i, sa_i, sa_i_size = 0, sa_prime, sa_prime_size; GtArray *splice_form; GtBittab **C, **left, **right, **L, **R, *U_i, *SA_i, *SA_prime; #ifndef NDEBUG unsigned long u_i_size, u_i_minus_1_size; gt_assert(csa && csa->set_of_sas); #endif /* init sets */ C = gt_malloc(sizeof (GtBittab*) * csa->number_of_sas); left = gt_malloc(sizeof (GtBittab*) * csa->number_of_sas); right = gt_malloc(sizeof (GtBittab*) * csa->number_of_sas); L = gt_malloc(sizeof (GtBittab*) * csa->number_of_sas); R = gt_malloc(sizeof (GtBittab*) * csa->number_of_sas); for (i = 0; i < csa->number_of_sas; i++) { C[i] = gt_bittab_new(csa->number_of_sas); left[i] = gt_bittab_new(csa->number_of_sas); right[i] = gt_bittab_new(csa->number_of_sas); L[i] = gt_bittab_new(csa->number_of_sas); R[i] = gt_bittab_new(csa->number_of_sas); } U_i = gt_bittab_new(csa->number_of_sas); SA_i = gt_bittab_new(csa->number_of_sas); SA_prime = gt_bittab_new(csa->number_of_sas); splice_form = gt_array_new(sizeof (unsigned long)); /* compute sets */ compute_C(C, csa); compute_left(left, csa); compute_right(right, csa); compute_L(L, C, left, csa->number_of_sas); compute_R(R, C, right, csa->number_of_sas); /* U_0 = SA */ for (i = 0; i < csa->number_of_sas; i++) gt_bittab_set_bit(U_i, i); #ifndef NDEBUG /* preparation for assertion below */ u_i_minus_1_size = gt_bittab_count_set_bits(U_i); #endif while (gt_bittab_is_true(U_i)) { sa_i = GT_UNDEF_ULONG; for (sa_prime = gt_bittab_get_first_bitnum(U_i); sa_prime != gt_bittab_get_last_bitnum(U_i); sa_prime = gt_bittab_get_next_bitnum(U_i, sa_prime)) { if (sa_i == GT_UNDEF_ULONG) { sa_i = sa_prime; gt_bittab_or(SA_i, L[sa_i], R[sa_i]); sa_i_size = gt_bittab_count_set_bits(SA_i); } else { gt_bittab_or(SA_prime, L[sa_prime], R[sa_prime]); sa_prime_size = gt_bittab_count_set_bits(SA_prime); if (sa_prime_size > sa_i_size) { sa_i = sa_prime; sa_i_size = sa_prime_size; gt_bittab_equal(SA_i, SA_prime); } } } /* make sure the computed splice form is maximal w.r.t. to compatibility */ gt_assert(splice_form_is_valid(SA_i, csa)); /* process splice form */ if (csa->process_splice_form) { gt_array_reset(splice_form); gt_bittab_get_all_bitnums(SA_i, splice_form); csa->process_splice_form(splice_form, csa->set_of_sas, csa->number_of_sas, csa->size_of_sa, csa->userdata); } /* U_i = U_i-1 \ SA_i */ gt_bittab_nand(U_i, U_i, SA_i); #ifndef NDEBUG /* ensure that |U_i| < |U_i-1| */ u_i_size = gt_bittab_count_set_bits(U_i); gt_assert(u_i_size < u_i_minus_1_size); u_i_minus_1_size = u_i_size; #endif } /* free sets */ for (i = 0; i < csa->number_of_sas; i++) { gt_bittab_delete(C[i]); gt_bittab_delete(left[i]); gt_bittab_delete(right[i]); gt_bittab_delete(L[i]); gt_bittab_delete(R[i]); } gt_free(C); gt_free(left); gt_free(right); gt_free(L); gt_free(R); gt_bittab_delete(U_i); gt_bittab_delete(SA_i); gt_bittab_delete(SA_prime); gt_array_delete(splice_form); }