/************************************************************************* * Calculate the log odds score for a single motif-sized window. *************************************************************************/ static inline BOOLEAN_T score_motif_site( ALPH_T alph, char *seq, PSSM_T *pssm, double *score // OUT ) { int asize = alph_size(alph, ALPH_SIZE); MATRIX_T* pssm_matrix = pssm->matrix; double scaled_log_odds = 0.0; // For each position in the site int motif_position; for (motif_position = 0; motif_position < pssm->w; motif_position++) { char c = seq[motif_position]; int aindex = alph_index(alph, c); // Check for gaps and ambiguity codes at this site if(aindex == -1 || aindex >= asize) return FALSE; scaled_log_odds += get_matrix_cell(motif_position, aindex, pssm_matrix); } *score = get_unscaled_pssm_score(scaled_log_odds, pssm); // Handle scores that are out of range if ((int) scaled_log_odds >= get_array_length(pssm->pv)) { scaled_log_odds = (float)(get_array_length(pssm->pv) - 1); *score = scaled_to_raw(scaled_log_odds, pssm->w, pssm->scale, pssm->offset); } return TRUE; }
/*********************************************************************** * Check to be sure that two arrays have the same length. ***********************************************************************/ static BOOLEAN_T check_array_dimensions (BOOLEAN_T die_on_mismatch, ARRAY_T* array1, ARRAY_T* array2) { /* Check to see that the two arrays are of the same length. */ if (get_array_length(array1) != get_array_length(array2)) { if (die_on_mismatch) { die("Arrays have differing lengths (%d != %d).\n", get_array_length(array1), get_array_length(array2)); } return(FALSE); } return(TRUE); }
/* * Callback to be used to put a section of configuration * into a dictionary */ int settings_into_dict(void *settings, void *data) { void *elem; int count, i; char name[80], value[80]; struct dict *dictionary = (struct dict *) data; count = get_array_length(LIBCFG_PARSER, settings); for(i = 0; i < count; ++i) { elem = get_elem_from_idx(LIBCFG_PARSER, settings, i); if (!elem) continue; if(!(exist_field_string(LIBCFG_PARSER, elem, "name"))) continue; if(!(exist_field_string(LIBCFG_PARSER, elem, "value"))) continue; GET_FIELD_STRING(LIBCFG_PARSER, elem, "name", name); GET_FIELD_STRING(LIBCFG_PARSER, elem, "value", value); dict_set_value(dictionary, name, value); TRACE("Identify for configData: %s --> %s\n", name, value); } return 0; }
/***************************************************************************** * MEME > model > /background_frequencies ****************************************************************************/ void mxml_end_background(void *ctx) { CTX_T *data; int i; bool error; double sum, delta; data = (CTX_T*)ctx; sum = 0; error = false; for (i = 0; i < get_array_length(data->nums); i++) { if (get_array_item(i, data->nums) == -1) { local_error(data, "Background frequency was not provided for letter %c.\n", alph_char(data->alph, i)); error = true; } else { sum += get_array_item(i, data->nums); } } delta = sum - 1.0; if (delta < 0) delta = -delta; if (delta > 0.01) { local_error(data, "The background frequencies summed to %f but they should sum to 1.0.\n", sum); error = true; } if (error) { free_array(data->nums); } else { data->fscope.background = data->nums; data->nums = NULL; } }
/*********************************************************************** * Normalize an array in log space. ***********************************************************************/ void log_normalize (ATYPE close_enough, ARRAY_T* array) { int i_item; int num_items; ATYPE total; ATYPE this_value; /* Get the sum of the elements. */ total = log_array_total(array); /* If the array already sums to zero, don't bother. */ if (almost_equal(total, 0.0, close_enough)) { return; } /* If there's nothing in the array, then return all zeroes. */ if (total < LOG_SMALL) { init_array(LOG_ZERO, array); return; } num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { this_value = get_array_item(i_item, array) - total; /* If this value is small enough, just make it zero. */ if (this_value < LOG_SMALL) { set_array_item(i_item, LOG_ZERO, array); } else { set_array_item(i_item, this_value, array); } } }
/*********************************************************************** * Mix two arrays in log space. ***********************************************************************/ void mix_log_arrays (float mixing, /* Percent of array2 that will be retained. */ ARRAY_T* array1, ARRAY_T* array2) { int i_item; int num_items; ATYPE mixed_value; check_null_array(array1); check_null_array(array2); /* Verify that the arrays are of the same length. */ check_array_dimensions(TRUE, array1, array2); /* Verify that we've got a reasonable mixing parameter. */ if ((mixing > 1.0) || (mixing < 0.0)) { die("Invalid mixing parameter (%g).\n", mixing); } num_items = get_array_length(array1); for (i_item = 0; i_item < num_items; i_item++) { mixed_value = LOG_SUM(my_log2(1.0 - mixing) + get_array_item(i_item, array1), my_log2(mixing) + get_array_item(i_item, array2)); set_array_item(i_item, mixed_value, array2); } }
/*********************************************************************** * Make all the elements of an array positive by adding a constant to * each. ***********************************************************************/ void all_positive (ARRAY_T* array) { int i_item; int num_items; ATYPE min; check_null_array(array); /* Find the minimum value. */ min = get_array_item(0, array); num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { if (get_array_item(i_item, array) < min) { min = get_array_item(i_item, array); } } /* If there are negative elements ... */ if (min < 0.0) { /* ... then subtract the minimum from all elements. */ for (i_item = 0; i_item < num_items; i_item++) { incr_array_item(i_item, -min, array); } } }
/*********************************************************************** * Determine whether two arrays are equal, within a given bound. ***********************************************************************/ BOOLEAN_T equal_arrays (ATYPE close_enough, ARRAY_T* array1, ARRAY_T* array2) { int i_item; int num_items; check_null_array(array1); check_null_array(array2); /* Verify that the arrays are of the same length. */ if (!check_array_dimensions(FALSE, array1, array2)) { return(FALSE); } /* Check to be sure that each value is the same. */ num_items = get_array_length(array1); for (i_item = 0; i_item < num_items; i_item++) { if (!almost_equal(get_array_item(i_item, array1) - get_array_item(i_item, array2), 0.0, close_enough)) { return(FALSE); } } return(TRUE); }
// // get_array_length // static VariableType::Reference get_array_length(std::vector<bigsint> const &len, size_t &depth, VariableType *type) { if (type->getBasicType() != VariableType::BT_ARR) return static_cast<VariableType::Reference>(type); if (static_cast<biguint>(depth) >= len.size()) Error_p("err"); if (type->getWidth()) return get_array_length(len, depth, type->getReturn()) ->getArray((depth++, type->getWidth())); else return get_array_length(len, depth, type->getReturn()) ->getArray(len[depth++]); }
// Executes a method declaration on the given fragment. static value_t apply_method_declaration(value_t ambience, value_t decl, value_t fragment) { CHECK_FAMILY(ofMethodDeclarationAst, decl); CHECK_FAMILY(ofModuleFragment, fragment); runtime_t *runtime = get_ambience_runtime(ambience); // Look for the :builtin annotation on this method. value_t annots = get_method_declaration_ast_annotations(decl); value_t builtin_name = new_not_found_condition(); for (size_t i = 0; i < get_array_length(annots); i++) { value_t annot = get_array_at(annots, i); TRY_DEF(value, run_expression_until_condition(ambience, fragment, annot)); if (in_family(ofBuiltinMarker, value)) builtin_name = get_builtin_marker_name(value); } // Compile the method whether it's a builtin or not. This way we can reuse // the compilation code for both cases and just patch up the result after // the fact if it's a builtin. value_t method_ast = get_method_declaration_ast_method(decl); TRY_DEF(method, compile_method_ast_to_method(runtime, method_ast, fragment)); if (!in_condition_cause(ccNotFound, builtin_name)) { // This is a builtin so patch the method with the builtin implementation. TRY_DEF(impl, runtime_get_builtin_implementation(runtime, builtin_name)); value_t impl_code = get_builtin_implementation_code(impl); TRY(validate_builtin_method_binding(method, impl)); set_method_code(method, impl_code); value_t impl_flags = get_builtin_implementation_method_flags(impl); set_method_flags(method, impl_flags); } value_t methodspace = get_module_fragment_methodspace(fragment); TRY(add_methodspace_method(runtime, methodspace, method)); return success(); }
static char * array_length_as_string(char *field_type) { static char array_size[IDENTIFIER_SIZE]; if (!strchr(field_type, '[')) return ""; snprintf(array_size, IDENTIFIER_SIZE, "[%d]", get_array_length(field_type)); return array_size; }
BOOLEAN_T dxml_get_bg(void *data, ARRAY_T **bg) { DXML_T *parser; parser = (DXML_T*)data; if (parser->data->fscope.background == NULL) return FALSE; *bg = resize_array(*bg, get_array_length(parser->data->fscope.background)); copy_array(parser->data->fscope.background, *bg); return TRUE; }
void check_sq_matrix(MATRIX_T *sq, int expected_size) { int i; assert(get_num_rows(sq) == expected_size); assert(get_num_cols(sq) == expected_size); for (i = 0; i < expected_size; ++i) { assert(get_array_length(get_matrix_row(i, sq)) == expected_size); } }
/************************************************************************** * Implement bounds checking. **************************************************************************/ static void array_bounds_check (int index, ARRAY_T* array) { #ifdef BOUNDS_CHECK if (index < 0) { die("Invalid array index (%d).\n", index); } else if (index >= get_array_length(array)) { die("Array index out of bounds (%d >= %d).\n", index, get_array_length(array)); } #else /* Avoid compiler warning. */ array += index; #endif }
value_t new_heap_array_buffer_with_contents(runtime_t *runtime, value_t elements) { CHECK_FAMILY(ofArray, elements); size_t size = kArrayBufferSize; TRY_DEF(result, alloc_heap_object(runtime, size, ROOT(runtime, mutable_array_buffer_species))); set_array_buffer_elements(result, elements); set_array_buffer_length(result, get_array_length(elements)); return post_create_sanity_check(result, size); }
// Iteratively apply the elements of the unbound fragment to the partially // initialized bound fragment. static value_t apply_module_fragment_elements(binding_context_t *context, value_t unbound_fragment, value_t bound_fragment) { value_t elements = get_unbound_module_fragment_elements(unbound_fragment); for (size_t i = 0; i < get_array_length(elements); i++) { value_t element = get_array_at(elements, i); TRY(apply_unbound_fragment_element(context->ambience, element, bound_fragment)); } return success(); }
static void print_array_type_cmp(FILE *code_file, char *type, int index) { fprintf(code_file, "\tif (field_array_%s_cmp(t1->match_types[%d], t2->match_types[%d],\n" "\t\t\tt1->value%d, t2->value%d, %d) == FALSE)\n" "\t\treturn FALSE;\n", get_basic_type(type), index, index, index, index, get_array_length(type)); }
static void print_array_type_copy(FILE *code_file, char *type, int index) { int i, n = get_array_length(type); for (i = 0; i < n; i++) fprintf(code_file, "\tdest->value%d[%d] = src->value%d[%d];\n", index, i, index, i); fprintf(code_file, "\tdest->match_types[%d] = src->match_types[%d];\n", index, index); }
value_t module_loader_process_options(runtime_t *runtime, value_t self, value_t options) { CHECK_FAMILY(ofIdHashMap, options); value_t libraries = get_id_hash_map_at_with_default(options, RSTR(runtime, libraries), ROOT(runtime, empty_array)); for (size_t i = 0; i < get_array_length(libraries); i++) { value_t library_path = get_array_at(libraries, i); TRY(module_loader_read_library(runtime, self, library_path)); } return success(); }
/* * Get the background used to assign pseudo-counts. * If the background has not yet been determined it will return null. */ ARRAY_T *mread_get_background(MREAD_T *mread) { ARRAY_T *bg; ALPH_T alph; attempt_read_motif(mread); if (mread->pseudo_bg) { bg = allocate_array(get_array_length(mread->pseudo_bg)); copy_array(mread->pseudo_bg, bg); return bg; } return NULL; }
/*********************************************************************** * Initialize a given array with a given value. ***********************************************************************/ void init_array (ATYPE value, ARRAY_T* array) { int i_item; int num_items; num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { set_array_item(i_item, value, array); } }
/* * Load uniform frequencies into the array. */ ARRAY_T* get_uniform_frequencies(ALPH_T alph, ARRAY_T *freqs) { int i, n; n = ALPH_ASIZE[alph]; if (freqs == NULL) freqs = allocate_array(alph_size(alph, ALL_SIZE)); assert(get_array_length(freqs) >= alph_size(alph, ALL_SIZE)); for (i = 0; i < n; i++) { set_array_item(i, 1.0/n, freqs); } calc_ambigs(alph, FALSE, freqs); return freqs; }
/*********************************************************************** * Fill an array with a given raw array of values. ***********************************************************************/ void fill_array (ATYPE* raw_array, ARRAY_T* array) { int i_item; int num_items; num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { set_array_item(i_item, raw_array[i_item], array); } }
/*********************************************************************** * Add a scalar value to each element of an array. ***********************************************************************/ void scalar_add (ATYPE value, ARRAY_T* array) { int i_item; int num_items; num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { incr_array_item(i_item, value, array); } }
/************************************************************************ * Copy one state of an MHMM. ************************************************************************/ static void copy_state (MHMM_STATE_T * a_state, MHMM_STATE_T * new_state) { new_state->type = a_state->type; /* Allocate memory. */ new_state->itrans_out = (int *)mm_malloc(a_state->ntrans_out * sizeof(int)); new_state->trans_out = allocate_array(a_state->ntrans_out); new_state->itrans_in = (int *)mm_malloc(a_state->ntrans_in * sizeof(int)); new_state->trans_in = allocate_array(a_state->ntrans_in); new_state->emit = allocate_array(get_array_length(a_state->emit)); new_state->emit_odds = allocate_array(get_array_length(a_state->emit_odds)); /* Outgoing transitions. */ new_state->ntrans_out = a_state->ntrans_out; copy_int_array(a_state->ntrans_out, a_state->itrans_out, new_state->itrans_out); copy_array(a_state->trans_out, new_state->trans_out); /* Incoming transitions. */ new_state->ntrans_in = a_state->ntrans_in; copy_int_array(a_state->ntrans_in, a_state->itrans_in, new_state->itrans_in); copy_array(a_state->trans_in, new_state->trans_in); /* Emissions. */ copy_array(a_state->emit, new_state->emit); copy_array(a_state->emit_odds, new_state->emit_odds); new_state->num_sites = a_state->num_sites; // Descriptive information. new_state->i_motif = a_state->i_motif; new_state->w_motif = a_state->w_motif; strcpy(new_state->motif_id, a_state->motif_id); new_state->i_position = a_state->i_position; new_state->id_char = a_state->id_char; }
void unlog_array (ARRAY_T* array) { int i_item; int num_items; check_null_array(array); num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { set_array_item(i_item, EXP2(get_array_item(i_item, array)), array); } }
/***************************************************************************** * MEME > motifs > motif > probabilities > alphabet_matrix > /alphabet_array * Check that all letters have a probability and update the current matrix row. ****************************************************************************/ void mxml_end_probability_pos(void *ctx) { CTX_T *data; ARRAY_T *pos; int i; data = (CTX_T*)ctx; pos = get_matrix_row(data->current_pos, data->mscope.motif->freqs); for (i = 0; i < get_array_length(pos); i++) { if (get_array_item(i, pos) == -1) { local_error(data, "Probability for letter %c in position %d is missing.\n", alph_char(data->alph, i), i + 1); } } data->current_pos++; }
/************************************************************************** * get_scaled_lo_prior_dist * * Takes a scaled distribution of priors and creates a scaled distribution of * log odds priors. The parameters for the scaling of the input priors are * in the PRIOR_DIST_T data structure. The output distribution of log odss * priors are scaled to be in the same range as the PSSM log odds using * the input parameters pssm_range, pssm_scale, and pssm_offset. * * Special handling is required for a uniform distribution of priors. * In that case the max_prior == min_prior, and the distribution only * contains one bin. * * Returns a new array containing the scaled log odds priors **************************************************************************/ ARRAY_T *get_scaled_lo_prior_dist( PRIOR_DIST_T *prior_dist, double alpha, int pssm_range, double pssm_scale, double pssm_offset ) { assert(prior_dist != NULL); // Alocate enought space for elements in [0 ... pssm_range] ARRAY_T *scaled_lo_prior_dist = allocate_array(pssm_range + 1); if (prior_dist != NULL) { ARRAY_T *dist_array = get_prior_dist_array(prior_dist); int len_prior_dist = get_array_length(dist_array); double max_prior = get_prior_dist_maximum(prior_dist); double min_prior = get_prior_dist_minimum(prior_dist); double prior_dist_scale = get_prior_dist_scale(prior_dist); double prior_dist_offset = get_prior_dist_offset(prior_dist); init_array(0.0L, scaled_lo_prior_dist); if (max_prior == min_prior) { // Special case for uniform priors double value = 1.0; double lo_prior = my_log2(alpha * max_prior / (1.0L - (alpha * max_prior))); // Convert lo_prior to PSSM scale int scaled_index = raw_to_scaled(lo_prior, 1.0L, pssm_scale, pssm_offset); set_array_item(scaled_index, value, scaled_lo_prior_dist); } else { int prior_index = 0; for (prior_index = 0; prior_index < len_prior_dist; ++prior_index) { double value = get_array_item(prior_index, dist_array); // Convert index giving scaled prior to raw prior. double scaled_prior = ((double) prior_index) + 0.5L; double prior \ = scaled_to_raw(scaled_prior, 1, prior_dist_scale, prior_dist_offset); double lo_prior = my_log2(alpha * prior / (1.0L - (alpha * prior))); // Scale raw lo_prior using parameters from PSSM. int scaled_index = raw_to_scaled(lo_prior, 1.0L, pssm_scale, pssm_offset); if (scaled_index < pssm_range) { double old_value = get_array_item(scaled_index, scaled_lo_prior_dist); set_array_item(scaled_index, value + old_value, scaled_lo_prior_dist); } } } } return scaled_lo_prior_dist; }
/*********************************************************************** * Make an array sum to zero by subtracting the mean from each element. ***********************************************************************/ void sum_to_zero (ARRAY_T* array) { int num_items; int i_item; ATYPE ave; ave = ave_array(array); num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { incr_array_item(i_item, -ave, array); } }
/*********************************************************************** * Multiply each element of an array by a scalar value. ***********************************************************************/ void scalar_mult (ATYPE value, ARRAY_T* array) { int i_item; int num_items; check_null_array(array); num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { set_array_item(i_item, get_array_item(i_item, array) * value, array); } }