static void removeInt (rbtree_t* rbtreePtr, long* data) { printf("Removing: %li\n", *data); rbtree_delete(rbtreePtr, (void*)data); assert(rbtree_get(rbtreePtr, (void*)data) == NULL); assert(rbtree_verify(rbtreePtr, 0) > 0); }
static void insertInt (rbtree_t* rbtreePtr, long* data) { printf("Inserting: %li\n", *data); rbtree_insert(rbtreePtr, (void*)data, (void*)data); assert(*(long*)rbtree_get(rbtreePtr, (void*)data) == *data); assert(rbtree_verify(rbtreePtr, 0) > 0); }
/***************************************************************************** * Reads frequency attributes into the pre-allocated freqs array. ****************************************************************************/ static void parse_freq_attrs(PS_T *ps, const char* tag, const xmlChar **attrs) { int i, ncore, seen, *idx; char *end_ptr; double value, sum; RBNODE_T *node; bool seen_bad; ncore = rbtree_size(ps->alph_ids); // initilize the freqs array if (ps->freqs == NULL) ps->freqs = mm_malloc(sizeof(double) * ncore); // reset freqs array; for (i = 0; i < ncore; i++) ps->freqs[i] = -1; seen = 0; seen_bad = false; sum = 0.0; // iterate over attributes for (i = 0; attrs[i] != NULL; i += 2) { idx = (int*)rbtree_get(ps->alph_ids, attrs[i]); if (idx != NULL) { assert(*idx < ncore); if (ps->freqs[*idx] != -1) { dreme_attr_parse_error(ps, PARSE_ATTR_DUPLICATE, tag, (const char*)attrs[i], NULL); continue; } seen++; errno = 0; // reset because we're about to check it value = strtod((const char*)attrs[i+1], &end_ptr); // allow out of range values, mainly because freqs can be very close to zero if (end_ptr == (const char*)attrs[i+1] || (errno && errno != ERANGE) || value < 0 || value > 1) { dreme_attr_parse_error(ps, PARSE_ATTR_BAD_VALUE, tag, (const char*)attrs[i], (const char*)attrs[i+1]); ps->freqs[*idx] = 0; // mark frequence as seen, even though it's bad seen_bad = true; continue; } ps->freqs[*idx] = value; sum += value; } } // check we got everthing if (seen < ncore) { // identify what we're missing for (node = rbtree_first(ps->alph_ids); node != NULL; node = rbtree_next(node)) { idx = (int*)rbtree_value(node); if (ps->freqs[*idx] == -1) { dreme_attr_parse_error(ps, PARSE_ATTR_MISSING, tag, (char*)rbtree_key(node), NULL); } } } else if (!seen_bad) { // check the frequencies sum to 1 double delta = sum - 1; delta = (delta < 0 ? -delta : delta); if (delta > (0.001 * ncore)) { // dreme writes background probabilities to 3 decimal places so assuming // the error on each is at maximum 0.001 then the total error for the // sum must be less than or equal to 0.004 error(ps, "Probabilities of %s do not sum to 1, got %g .\n", tag, sum); } } }
/***************************************************************************** * MEME > scanned_sites_summary > scanned_sites ****************************************************************************/ void mxml_start_scanned_seq(void *ctx, char *seq_id, double log10pvalue, int site_count) { CTX_T *data; int *length; struct seqinfo *seq; data = (CTX_T*)ctx; if (data->options & SCANNED_SITES) { data->current_site = 0; seq = (struct seqinfo *)rbtree_get(data->sequence_lookup, seq_id); if (seq == NULL) { local_error(data, "Scanned sites references unknown sequence \"%s\".\n", seq_id); return; } arraylst_add(sseq_create(seq->name, seq->length, log10pvalue, site_count), data->fscope.scanned_sites); } }
/***************************************************************************** * MEME > scanned_sites_summary > scanned_sites > scanned_site ****************************************************************************/ void mxml_scanned_site(void *ctx, char *motif_id, char strand, int position, double log10pvalue) { CTX_T *data; SCANNED_SEQ_T *sseq; int *mindex; data = (CTX_T*)ctx; if (data->options & SCANNED_SITES) { sseq = (SCANNED_SEQ_T*)arraylst_get( arraylst_size(data->fscope.scanned_sites)-1, data->fscope.scanned_sites); mindex = (int*)rbtree_get(data->motif_lookup, motif_id); if (mindex == NULL) { local_error(data, "Scanned site references unknown motif \"%s\".\n", motif_id); return; } sseq_set(sseq, data->current_site++, (*mindex) + 1, strand, position, log10pvalue); } }
/***************************************************************************** * MEME > model > background_frequencies > alphabet_array > value ****************************************************************************/ void mxml_background_value(void *ctx, char *id, double freq) { CTX_T *data; char *symbol; int index; data = (CTX_T*)ctx; symbol = (char*)rbtree_get(data->letter_lookup, id); if (symbol == NULL) { local_error(data, "Background for unknown letter identifier \"%s\".\n", id); return; } index = alph_indexc(data->alph, symbol[0]); if (index < 0) { local_error(data, "Background for non-core letter %c.\n", symbol[0]); return; } if (data->nums == NULL) { data->nums = allocate_array(alph_size_core(data->alph)); init_array(-1, data->nums); } set_array_item(index, freq, data->nums); }
/***************************************************************************** * MEME > motifs > motif > probabilities > alphabet_matrix > alphabet_array > /value * Lookup a letter and check it exists and does not have a probability. * Set the letter's score to the passed value. ****************************************************************************/ void mxml_probability_value(void *ctx, char *letter_id, double probability) { CTX_T *data; MATRIX_T *freqs; char *symbol; int index; data = (CTX_T*)ctx; freqs = data->mscope.motif->freqs; // lookup letter ID symbol = (char*)rbtree_get(data->letter_lookup, letter_id); if (symbol == NULL) { local_error(data, "Probability is not allowed for unknown letter identifier \"%s\".\n", letter_id); return; } index = alph_indexc(data->alph, symbol[0]); if (index < 0) { local_error(data, "Probability is not allowed for non-core letter %c.\n", symbol[0]); return; } if (get_matrix_cell(data->current_pos, index, freqs) != -1) { local_error(data, "Probability for letter %c in position %d has already been set.\n", symbol[0], data->current_pos + 1); return; } set_matrix_cell(data->current_pos, index, probability, freqs); }
int main(int argc, char **argv) { void *rbtree; unsigned i = 0; int c; char buf[256]; if (parse_args(argc, argv) == -1) { usage(); exit(1); } FILE *fp = NULL; if (!infile) { fp = stdin; } else { fp = fopen(infile, "r"); if (!fp) { fprintf(stderr, "Can't open file: %s\n", infile); exit(1); } } rbtree = rbtree_new(key_cmp, free, free); while ((c = fgetc(fp)) != EOF) { if (c == '\n') { buf[i] = '\0'; rbtree_insert(rbtree, strdup(buf), strdup(buf)); memset(buf, 0, sizeof buf); i = 0; } else { buf[i++] = c; } } if (infile) fclose(fp); if (export) { fp = fopen(export, "w"); rbtree_dump(fp, rbtree, key_string); fclose(fp); } if (interactive) { void *node; while (1) { i = 0; memset(buf, 0, sizeof buf); printf("\n> "); while ((c = getc(stdin))) { if (c == EOF || c == '\n') break; if (i >= 256) { printf("buffer exceeded\n"); goto done; } buf[i++] = c; } if (c == EOF) break; if (!*buf) continue; buf[i] = '\0'; char *op = buf; while (*op && *op != ' ') op++; if (*op) { *op = '\0'; op++; } if (strncasecmp(buf, "successor", strlen("successor")) == 0) { node = rbtree_get(rbtree, op); if (node) { void *n2 = rbtree_successor(rbtree, node); if (n2) printf("successor: %s => %s\n", key_string(rbtree_node_key(n2)), (char *)rbtree_node_value(n2)); else printf("no successor\n"); } else { printf("%s (not found)\n", op); } } else if (strncasecmp(buf, "predecessor", strlen("predecessor")) == 0) { node = rbtree_get(rbtree, op); if (node) { void *n2 = rbtree_predecessor(rbtree, node); if (n2) printf("predecessor: %s => %s\n", key_string(rbtree_node_key(n2)), (char *)rbtree_node_value(n2)); else printf("no predecessor\n"); } else { printf("%s (not found)\n", op); } } else { node = rbtree_get(rbtree, buf); if (node) printf("%s => %s\n", key_string(rbtree_node_key(node)), (char *)rbtree_node_value(node)); else printf("%s (not found)\n", buf); } } } done: rbtree_free(rbtree); if (infile) free(infile); if (export) free(export); return 0; }
int main() { int i, count = 30; key_t key; RBTreeNodeHandle root = NULL; int test_addr = 0; key_t search_key = -1; key_t delete_key = -1; srand(1103515245); for (i = 1; i < count; ++i) { key = rand() % count; test_addr = key; AGILE_LOGI("key = %d", key); root = rbtree_insert(root, key, (void *)&test_addr); if (!(i % 188)){ AGILE_LOGI("[i = %d] set search key %d", i, key); search_key = key; } if (!(i % 373)){ AGILE_LOGI("[i = %d] set delete key %d", i, key); delete_key = key; } if (search_key > 0){ if (rbtree_get(root, search_key)) { AGILE_LOGI("[i = %d] search key %d success!", i, search_key); search_key = -1; } else { AGILE_LOGI("[i = %d] search key %d error!", i, search_key); return (-1); } } if (delete_key > 0) { AGILE_LOGI("****** Before delete: node number: %d, repeatKeyNum: %d", rbtree_dump(root, 0), rbtree_debug_getRepeatNum()); if (rbtree_delete(&root, delete_key) == 0) { AGILE_LOGI("[i = %d] delete key %d success (%d)", i, delete_key, ++deleteNum); delete_key = -1; } else { AGILE_LOGI("[i = %d] delete key %d error", i, delete_key); return -1; } AGILE_LOGI("****** After delete: node number: %d", rbtree_dump(root, 0)); /*return 0;*/ } /*rbtree_dump(root, 1);*/ } { RBTreeNodeHandle n; i = 0; for (n = rbtree_first(root); n; n = rbtree_next(n)) { AGILE_LOGI("index %d: key - %lld", i, n->key); i++; } } return 0; }
/************************************************************************* * Build a linear HMM. *************************************************************************/ void build_linear_hmm (ARRAY_T* background, ORDER_T* order_spacing, int spacer_states, RBTREE_T* motifs, // motifs with key as in order_spacing BOOLEAN_T fim, MHMM_T** the_hmm) { ALPH_T alph; int model_length; // Total number of states in the model. int i_state; // Index of the current state. int i_order; // Index within the order and spacing. int i_position; // Index within the current motif or spacer. int motif_i; // motif key in order spacing MOTIF_T *motif; // motif RBNODE_T *node; alph = get_motif_alph((MOTIF_T*)rbtree_value(rbtree_first(motifs))); // Calculate the total length of the model. model_length = 2; // start and end state for (i_order = 0; i_order < get_order_occurs(order_spacing); i_order++) { motif_i = get_order_motif(order_spacing, i_order); motif = (MOTIF_T*)rbtree_get(motifs, &motif_i); model_length += get_motif_length(motif); } model_length += (get_order_occurs(order_spacing) + 1) * spacer_states; // Allocate the model. *the_hmm = allocate_mhmm(alph, model_length); check_sq_matrix((*the_hmm)->trans, model_length); // Record that this is a linear model. (*the_hmm)->type = LINEAR_HMM; // Record the number of motifs in the model. // It doesn't want the distinct count (*the_hmm)->num_motifs = get_order_occurs(order_spacing); // Record the number of states in the model. (*the_hmm)->num_states = model_length; (*the_hmm)->num_spacers = get_order_occurs(order_spacing) + 1; (*the_hmm)->spacer_states = spacer_states; // Put the background distribution into the model. copy_array(background, (*the_hmm)->background); // Begin the model with a non-emitting state. i_state = 0; check_sq_matrix((*the_hmm)->trans, (*the_hmm)->num_states); build_linear_state( alph, START_STATE, i_state, get_spacer_length(order_spacing, 0), NULL, // Emissions. 0, // Number of sites. NON_MOTIF_INDEX, NON_MOTIF_POSITION, // position within state (not relevant to start state) NULL, // no motif &((*the_hmm)->states[i_state])); ++i_state; // Build the first spacer. for (i_position = 0; i_position < spacer_states; i_position++, i_state++) { check_sq_matrix((*the_hmm)->trans, (*the_hmm)->num_states); build_linear_state( alph, SPACER_STATE, i_state, get_spacer_length(order_spacing, 0), background, SPACER_NUMSITES, NON_MOTIF_INDEX, i_position, // position within spacer NULL, // no motif &((*the_hmm)->states[i_state])); } // Build each motif and subsequent spacer. for (i_order = 0; i_order < get_order_occurs(order_spacing); i_order++) { STATE_T state; int spacer_len; motif_i = get_order_motif(order_spacing, i_order); motif = (MOTIF_T*)rbtree_get(motifs, &motif_i); // Build the motif. for (i_position = 0; i_position < get_motif_length(motif); i_position++, i_state++) { if (i_position == 0) { state = START_MOTIF_STATE; spacer_len = get_spacer_length(order_spacing, i_order); } else if (i_position == (get_motif_length(motif) - 1)) { state = END_MOTIF_STATE; spacer_len = get_spacer_length(order_spacing, i_order+1); } else { state = MID_MOTIF_STATE; spacer_len = 0; } check_sq_matrix((*the_hmm)->trans, (*the_hmm)->num_states); build_linear_state( alph, state, i_state, spacer_len, // Expected spacer length. get_matrix_row(i_position, get_motif_freqs(motif)), get_motif_nsites(motif), i_order, i_position, // position within motif (middle) motif, &((*the_hmm)->states[i_state])); } // Build the following spacer. for (i_position = 0; i_position < spacer_states; i_position++, i_state++) { check_sq_matrix((*the_hmm)->trans, (*the_hmm)->num_states); build_linear_state( alph, SPACER_STATE, i_state, get_spacer_length(order_spacing, i_order+1), background, SPACER_NUMSITES, NON_MOTIF_INDEX, i_position, // position within spacer NULL, // no motif &((*the_hmm)->states[i_state])); } } check_sq_matrix((*the_hmm)->trans, (*the_hmm)->num_states); // Finish up the model with a non-emitting end state. build_linear_state( alph, END_STATE, i_state, get_spacer_length(order_spacing, i_order), NULL, // Emissions. 0, // Number of sites. NON_MOTIF_INDEX, NON_MOTIF_POSITION, // position within state (not relevant to end state) NULL, // no motif &((*the_hmm)->states[i_state])); ++i_state; assert(i_state == model_length); check_sq_matrix((*the_hmm)->trans, (*the_hmm)->num_states); // Convert spacers to FIMs if requested. if (fim) { convert_to_fims(*the_hmm); } // Fill in the transition matrix. build_transition_matrix(*the_hmm); }