/*********************************************************************** * 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); } } }
/*********************************************************************** * 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); } }
/*********************************************************************** * 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); } }
/*********************************************************************** * Add two arrays. ***********************************************************************/ void sum_array (ARRAY_T* array1, ARRAY_T* array2) { int i_item; int num_items; check_null_array(array1); check_null_array(array2); check_array_dimensions(TRUE, array1, array2); num_items = get_array_length(array1); for (i_item = 0; i_item < num_items; i_item++) { incr_array_item(i_item, get_array_item(i_item, array1), array2); } }
/*********************************************************************** * Add random noise to an array. ***********************************************************************/ void add_noise (ATYPE magnitude, /* Magnitude of the noise. */ ARRAY_T* array) { int i_item; int num_items; ATYPE noise; check_null_array(array); num_items = get_array_length(array); for (i_item = 0; i_item < num_items; i_item++) { noise = magnitude * (2 * my_drand() - 1); incr_array_item(i_item, noise, array); } }
/************************************************************************* * Build array containing the counts of columns in the alignment * Caller is responsible for freeing the returned array. * If input parameter "freqs" is NULL, allocates the array. * Otherwise, the counts are added to the existing counts in the counts * array. Ignores all columns containing gaps or ambiguity characters: * [.-nNxX] *************************************************************************/ static ARRAY_T* build_alignment_column_counts( ALPH_T alph, ALIGNMENT_T* alignment, ARRAY_T* counts ) { assert(alignment != NULL); int asize = alph_size(alph, ALPH_SIZE); // Calculate number of possible alignment columns // and create storage for counting occurences. int num_seqs = get_num_aligned_sequences(alignment); int num_alignment_cols = (int) pow((double) asize, (double) num_seqs); if (counts == NULL) { counts = allocate_array(num_alignment_cols); } // Count how many examples of each column occur in the alignment. // Skip columns that contain gaps or ambiguity characters. int alignment_length = get_alignment_length(alignment); char* alignment_col = mm_malloc(sizeof(char) * (num_seqs + 1)); alignment_col[num_seqs] = 0; int i, h; for(i = 0; i < alignment_length; i++) { get_alignment_col(i, alignment_col, alignment); if (strchr(alignment_col, '-') != NULL) { continue; } if (strchr(alignment_col, '.') != NULL) { continue; } if (strchr(alignment_col, 'N') != NULL) { continue; } if (strchr(alignment_col, 'n') != NULL) { continue; } if (strchr(alignment_col, 'X') != NULL) { continue; } if (strchr(alignment_col, 'x') != NULL) { continue; } h = hash_alignment_col(alph, alignment_col, num_seqs); incr_array_item(h, 1, counts); } return counts; } // build_alignment_column_counts