Exemplo n.º 1
0
/***********************************************************************
 * Discard motifs that are not connected.
 ***********************************************************************/
static void throw_out_unused_motifs
  (MATRIX_T* transp_freq,
   MATRIX_T* spacer_ave,
   int*      num_motifs,
   MOTIF_T*  motifs)
{
  int i_motif, j_motif;
  ARRAY_T* row_sums;
  ARRAY_T* col_sums;

  // Extract the margins of the transition matrix.
  row_sums = get_matrix_row_sums(transp_freq);
  col_sums = get_matrix_col_sums(transp_freq);

  for (i_motif = 0; i_motif < *num_motifs; i_motif++) {

    // Is this row or column empty?
    if ((get_array_item(i_motif + 1, row_sums) == 0.0) ||
      (get_array_item(i_motif + 1, col_sums) == 0.0)) {

      if (verbosity >= NORMAL_VERBOSE) {
        fprintf(stderr, "Removing unused motif %s. No occurrences of this motif were found.\n",
        get_motif_id(&(motifs[i_motif])));
      }

      // Remove the row and column from the transition matrix.
      remove_matrix_row(i_motif + 1, transp_freq);
      remove_matrix_col(i_motif + 1, transp_freq);
      assert(get_num_rows(transp_freq) == get_num_cols(transp_freq));

      remove_matrix_row(i_motif + 1, spacer_ave);
      remove_matrix_col(i_motif + 1, spacer_ave);
      assert(get_num_rows(spacer_ave) == get_num_cols(spacer_ave));

      // Remove the motif from the array.
      for (j_motif = i_motif + 1; j_motif < *num_motifs; j_motif++) {
        free_motif(&(motifs[j_motif - 1]));
        copy_motif(&(motifs[j_motif]), &(motifs[j_motif - 1]));
      }
      free_motif(&(motifs[j_motif - 1]));
      (*num_motifs)--;
      i_motif--;

      // Recalculate the row and column sums.
      free_array(row_sums);
      free_array(col_sums);
      row_sums = get_matrix_row_sums(transp_freq);
      col_sums = get_matrix_col_sums(transp_freq);

    }
  }

  free_array(row_sums);
  free_array(col_sums);
}
Exemplo n.º 2
0
/******************************************************************************
  * This function sets the transistion prob. matrix for the ith element of 
  a transition matrix array to a copy of the provided matrix.
******************************************************************************/
static void set_substmatrix_matrix(
  SUBSTMATRIX_ARRAY_T *a, 
  int i,
  MATRIX_T* src
) {
  assert(a != NULL);
  assert(i < a->length);
  MATRIX_T* dst = NULL;
  if (src != NULL) {
    int num_rows = get_num_rows(src);
    int num_cols = get_num_rows(src);
    dst = allocate_matrix(num_rows, num_cols);
    copy_matrix(src, dst);
  }
  a->substmatrix[i] = dst;
}
Exemplo n.º 3
0
strvec MtxLP::getRowNames() const{
    strvec rv;
    int nrows = get_num_rows();
    for (int i = 1; i <= nrows; i++)
        rv.push_back(get_row_name(i));
    return rv;
}
Exemplo n.º 4
0
/******************************************************************************
  * This function returns a copy of the matrix for time t of a transition 
  * matrix array. Returns NUL is no matrix is found matching time t.
  * Caller is responsible for free the returned copy.
******************************************************************************/
MATRIX_T* get_substmatrix_for_time(
  SUBSTMATRIX_ARRAY_T *a, 
  double t
) {

  assert(a != NULL);
  // Create a copy of our matrix
  int i;
  MATRIX_T* src = NULL;
  for (i = 0; i < a->length; i++) {
    if (a->t[i] == t) {
      src = a->substmatrix[i];
      break;
    }
  }
  MATRIX_T* dst = NULL;
  if (src != NULL) {
    int num_rows = get_num_rows(src);
    int num_cols = get_num_cols(src);
    dst = allocate_matrix(num_rows, num_cols);
    copy_matrix(src, dst);
  }

  return dst;
}
Exemplo n.º 5
0
/***********************************************************************
 * allocate memory for a motif
 ***********************************************************************/
MOTIF_T* allocate_motif(char *id, MATRIX_T* freqs){
	MOTIF_T* motif = mm_malloc(sizeof(MOTIF_T));

	assert(id != NULL);
	assert(freqs != NULL);

	motif->length = 0;
	motif->alph_size = 0;
	motif->ambigs = 0;
	motif->evalue = 0.0;
	motif->num_sites = 0.0;
	motif->complexity = 0.0;
	motif->freqs = duplicate_matrix(freqs);
  motif->scores = NULL;
	// Set required fields.
	int length = strlen(id) + 1;
	strncpy(motif->id, id, min(length,MAX_MOTIF_ID_LENGTH));

	motif->alph_size = get_num_cols(motif->freqs);
	motif->length = get_num_rows(motif->freqs);
  motif->url = NULL;
  motif->trim_left = 0;
  motif->trim_right = 0;
	return motif;
}
Exemplo n.º 6
0
/***********************************************************************
 * Compute the complexity of a motif as a number between 0 and 1.
 *
 * Motif complexity is the average K-L distance between the "motif
 * background distribution" and each column of the motif.  The motif
 * background is just the average distribution of all the columns.  The
 * K-L distance, which measures the difference between two
 * distributions, is the same as the information content:
 *
 *  \sum_i p_i log(p_i/f_i)
 *
 * This value increases with increasing complexity.
 ***********************************************************************/
double compute_motif_complexity
  (MOTIF_T* a_motif)
{
  double return_value;
  ARRAY_T* motif_background;  // Mean emission distribution.
  int num_rows;
  int i_row;
  int num_cols;
  int i_col;

  num_cols = get_alph_size(ALPH_SIZE);
  num_rows = get_num_rows(a_motif->freqs);

  // Compute the mean emission distribution.
  motif_background = get_matrix_col_sums(a_motif->freqs);
  scalar_mult(1.0 / (double)num_rows, motif_background);

  // Compute the K-L distance w.r.t. the background.
  return_value = 0;
  for (i_row = 0; i_row < num_rows; i_row++) {
    ARRAY_T* this_emission = get_matrix_row(i_row, a_motif->freqs);
    for (i_col = 0; i_col < num_cols; i_col++) {
      ATYPE this_item = get_array_item(i_col, this_emission);
      ATYPE background_item = get_array_item(i_col, motif_background);

      // Use two logs to avoid handling divide-by-zero as a special case.
      return_value += this_item 
	* (my_log(this_item) - my_log(background_item));
    }
  }

  free_array(motif_background);
  return(return_value / (double)num_rows);
}
Exemplo n.º 7
0
/***********************************************************************
 * Copy a motif from one place to another.
 ***********************************************************************/
void copy_motif
  (MOTIF_T* source,
   MOTIF_T* dest)
{
  strcpy(dest->id, source->id);
  strcpy(dest->id2, source->id2);
  dest->length = source->length;
  dest->alph_size = source->alph_size;
  dest->ambigs = source->ambigs;
  dest->evalue = source->evalue;
  dest->num_sites = source->num_sites;
  dest->complexity = source->complexity;
  dest->trim_left = source->trim_left;
  dest->trim_right = source->trim_right;
  if (source->freqs) {
    // Allocate memory for the matrix.
    dest->freqs = allocate_matrix(dest->length, dest->alph_size + dest->ambigs);
    // Copy the matrix.
    copy_matrix(source->freqs, dest->freqs);
  } else {
    dest->freqs = NULL;
  }
  if (source->scores) {
    // Allocate memory for the matrix. Note that scores don't contain ambigs.
    dest->scores = allocate_matrix(get_num_rows(source->scores), get_num_cols(source->scores));
    // Copy the matrix.
    copy_matrix(source->scores, dest->scores);
  } else {
    dest->scores = NULL;
  }
  copy_string(&(dest->url), source->url);
}
Exemplo n.º 8
0
/**************************************************************************
*
*	reverse_complement_pssm_matrix
*
*	Turn a pssm matrix into its own reverse complement.
*
 *************************************************************************/
static void reverse_complement_pssm (
  ALPH_T alph,
  MATRIX_T* pssm_matrix
)
{
  int i;
  ARRAY_T* left_scores;
  ARRAY_T* right_scores;
  ARRAY_T* temp_scores;   // Temporary space during swap.
  int length = get_num_rows(pssm_matrix);

  // Allocate space.
  temp_scores = allocate_array(alph_size(alph, ALL_SIZE));

  // Consider each row (position) in the motif.
  for (i = 0; i < (int)((length+1) / 2); i++) {
    left_scores = get_matrix_row(i, pssm_matrix);
    right_scores = get_matrix_row(length - (i + 1), pssm_matrix);

    // Make a temporary copy of one row.
    copy_array(left_scores, temp_scores);

    // Compute reverse complements in both directions.
    complement_dna_freqs(right_scores, left_scores);
    complement_dna_freqs(temp_scores, right_scores);
  }
  free_array(temp_scores);
} // reverse_complement_pssm_matrix
Exemplo n.º 9
0
/**************************************************************************
*
	hash_pssm_matrix_pos

	Recursively create a single position of a hashed PSSM.

*
**************************************************************************/
static void hash_pssm_matrix_pos(
  MATRIX_T *pssm, 		// pssm to hash
  MATRIX_T *hashed_pssm, 	// hashed pssm
  int  pos,			// position in pssm
  int  hashed_pos,		// position in hashed pssm
  int  n,			// number of columns to hash together
  double score,			// cumulative score; call with 0
  int index			// cumulative index; call with 0
)
{
  int i;
  int alen = get_num_cols(pssm);	// alphabet length
  int w = get_num_rows(pssm);		// pssm width

  if (n==0) {				// done, set hashed_pssm entry
    set_matrix_cell(hashed_pos, index, score, hashed_pssm);
  } else {				// combine next column of pssm
    for (i=0; i<=alen; i++) {		// letters + blank
      // not past right edge of motif and not blank?
      double s = (pos<w && i!=alen) ? get_matrix_cell(pos, i, pssm) : 0;
      hash_pssm_matrix_pos(pssm,
		    hashed_pssm,
		    pos+1, 		// position in old pssm
		    hashed_pos, 	// position working on
		    n-1, 		// positions remaining to hash
		    score+s, 		// score so far
		    index*(alen+1)+i);	// hashed alphabet index so far
    } // leter
  }
} // hash_pssm_matrix_pos
Exemplo n.º 10
0
/**
 * Given a matrix, find the row idx and column idx of the minimum value
 * that passes the score and count threshold. If none is found, then
 * row_idx, col_idx, and lowest will not be modified.
 */
void find_most_significant_within_matrix(MATRIX_T* binomial,
                                         MATRIX_T* phospho_count,
                                         int* row_idx,
                                         int* col_idx,
                                         double* lowest,
                                         MOMO_OPTIONS_T* options) {
  int i;
  int j;
  for (i = 0; i < get_num_rows(binomial); ++i) {
    for (j = 0; j < get_num_cols(binomial); ++j) {
      double binomial_value = get_matrix_cell_defcheck(i, j, binomial);
      double count_value = get_matrix_cell_defcheck(i, j, phospho_count);
      // 1. value cannot be nan/inf
      // 2. log(value) < log(options->score_threshold)
      // 3. value should not be the modified location
      if (i != options->width/2 && !isnan(binomial_value) && !isinf(binomial_value) && binomial_value < log(options->score_threshold) && count_value >= options->count_threshold) {
        if (binomial_value < *lowest || isnan(*lowest)) {
          *row_idx = i;
          *col_idx = j;
          *lowest = get_matrix_cell_defcheck(i, j, binomial);
        }
      }
      
    }
  }
}
Exemplo n.º 11
0
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);
  }
}
Exemplo n.º 12
0
/***********************************************************************
 * Convert transition counts to transition probabilities, and compute
 * average spacer lengths.
 *
 * Each matrix is indexed 0 ... n+1, where n is the number of motifs.
 * The entry at [i,j] corresponds to the transition from motif i to
 * motif j.  Hence, after normalization, each row in the transition
 * matrix should sum to 1.
 ***********************************************************************/
static void normalize_spacer_counts(
  double    trans_pseudo,
  double    spacer_pseudo,    // Pseudocount for self-loop.
  BOOLEAN_T keep_unused,
  MATRIX_T* transp_freq,
  MATRIX_T* spacer_ave
) {
  int i_row;
  int i_col;
  int num_rows;
  double total_spacer;
  double num_transitions;
  double ave_spacer;
  
  /* Divide the spacer lengths by the number of occurrences. */
  num_rows = get_num_rows(transp_freq);
  for (i_row = 0; i_row < num_rows; i_row++) {
    for (i_col = 0; i_col < num_rows; i_col++) {
      total_spacer = get_matrix_cell(i_row, i_col, spacer_ave) + spacer_pseudo;
      num_transitions = get_matrix_cell(i_row, i_col, transp_freq);
      if (spacer_pseudo > 0) num_transitions++;
      if (num_transitions != 0.0) {
        ave_spacer = total_spacer / num_transitions;
        set_matrix_cell(i_row, i_col, ave_spacer, spacer_ave);
      }
    }
  }

  // Add pseudocounts.
  for (i_row = 0; i_row < num_rows; i_row++) {
    for (i_col = 0; i_col < num_rows; i_col++) {

      // Force some transitions to zero.
      if (// No transitions to the start state.
        (i_col == 0) || 
        // No transitions from the end state.
        (i_row == num_rows - 1) ||
        // No transition from start to end.
        ((i_row == 0) && (i_col == num_rows - 1))) {
        set_matrix_cell(i_row, i_col, 0.0, transp_freq);
      }
      else {
        // Only increment the used transitions.
        if ((keep_unused) || 
            (get_matrix_cell(i_row, i_col, transp_freq) > 0.0)) {
          incr_matrix_cell(i_row, i_col, trans_pseudo, transp_freq);
        }
      }
    }
  }

  // Normalize rows.
  for (i_row = 0; i_row < num_rows - 1; i_row++) {
    if (array_total(get_matrix_row(i_row, transp_freq)) > 0.0) {
      normalize(SLOP, get_matrix_row(i_row, transp_freq));
    }
  }
}
Exemplo n.º 13
0
void
MILPP::initialize(int nr_cols, int nr_rows)
{
  cons_matrix_.resize(nr_rows, nr_cols);

  col_to_row_mapping_.resize(nr_cols);
  for (int i = 0; i < get_num_cols(); i++)
    {
      col_to_row_mapping_[i] = new set<int>();
    }
  row_to_col_mapping_.resize(nr_rows);
  for (int i = 0; i < get_num_rows(); i++)
    {
      row_to_col_mapping_[i] = new set<int>();
    }

  /* Compute problem parameters and set default values if required */
  obj_coefs_.resize(nr_cols);
  cols_lower_bounds_.resize(nr_cols);
  cols_upper_bounds_.resize(nr_cols);

  /* Allocate rows related variables */
  rows_lower_bounds_.resize(nr_rows);
  rows_upper_bounds_.resize(nr_rows);
  rows_senses_.resize(nr_rows);

  /* Initialize cols related arrays */
  for (int i = 0; i < get_num_cols(); i++)
    {
      set_col_lower_bound(i, 0.);
      set_col_upper_bound(i, 1.);
      set_obj_coef(i, 0.);
    }

  /* Initialize rows related arrays */
  for (int i = 0; i < get_num_rows(); i++)
    {
      set_row_lower_bound(i, 0.);
      /* The actual upper bound values will be assigned later. */
      set_row_upper_bound(i, 0.);
      /* Set default row sense */
      set_row_sense(i, ROW_SENSE_LOWER);
    }
}
Exemplo n.º 14
0
 void init_domain (float ** domain_ptr,int rank) {
   int i,j,start,end,rows;
   start = get_start(rank);
   end = get_end(rank);
   rows = get_num_rows(rank);

   for (j=start;j<end;j++) {
     for (i=0;i<(int)floor(WIDTH/H);i++) {
       domain_ptr[j-start][i] = 0.0;
     }
   }
 }
Exemplo n.º 15
0
KARLIN_INPUT_T *make_karlin_input(
  MATRIX_T *matrix,			/* scoring matrix */
  ARRAY_T *probs			/* letter freq distribution */
)
{
  int i, j;
  double escore;
  long lowest, highest;
  ARRAY_T *score_probs; 
  int nscores;
  int alen = get_num_rows(matrix);	/* size of alphabet */
  KARLIN_INPUT_T *karlin_input;		/* data to return */

  /*  find the highest and lowest scores in the scoring matrix */
  lowest = 1;
  highest = -1;
  for (i=0; i<alen; i++) {
    for (j=0; j<alen; j++) {
      double s = get_matrix_cell(i, j, matrix);
      if (s < lowest) lowest = s;
      if (s > highest) highest = s;
    }
  }
  if (lowest >= 0) die("Lowest score in scoring matrix must be negative, is %f.", (double)lowest);
  if (highest<= 0) die("Highest score in scoring matrix must be positve, is %f.", (double)highest);

  /* allocate the array of score probabilities and set to 0 */
  nscores = highest - lowest + 1;
  score_probs = allocate_array(nscores);
  init_array(0, score_probs);
  
  /* compute the probabilities of different scores */ 
  escore = 0;
  for (i=0; i<alen; i++) {
    for (j=0; j<alen; j++) {
      int s = get_matrix_cell(i, j, matrix);
      double pi = get_array_item(i, probs);
      double pj = get_array_item(j, probs);
      double sp = get_array_item(s-lowest, score_probs); 
      set_array_item(s-lowest, sp + pi*pj, score_probs);	/* cumulative prob. of score */
      escore += pi*pj*s;
      /*printf("i %d j %d s %d pi %f pj %f sp %f escore %f\n",i,j,s, pi, pj, sp, escore);*/
    }
  }

  karlin_input = (KARLIN_INPUT_T *)mm_malloc(sizeof(KARLIN_INPUT_T));
  karlin_input->low = lowest;
  karlin_input->high = highest;
  karlin_input->escore = escore;
  karlin_input->prob = score_probs;

  return(karlin_input);
} /* make_karlin_input */
Exemplo n.º 16
0
 /**
  * If autoresize is enabled and col or row doesn't exist, the matrix
  * will be resized.
  */
 inline void set_coefficient(int i, int j, double v,
                             bool autoresize=true)
 {
   if (autoresize)
     {
       int nr = ((i + 1) > get_num_rows()) ? (i + 1) : -1;
       int nc = ((j + 1) > get_num_cols()) ? (j + 1) : -1;
       if (nr || nc)
         resize(nr, nc);
     }
   slave_matrix_.modifyCoefficient(i, j, v);
 }
Exemplo n.º 17
0
/**
 * Given a bg frequency matrix and a phospho count matrix, we will convert the bg
 * freq matrix into a binomial matrix. No reason to keep a bg freq matrix, so we will recycle.
 */
void convert_bg_freqs_to_binomial(MATRIX_T* phospho_count, MATRIX_T* bg_freqs, int num_phospho_seqs) {
  int i;
  int j;
  for (i = 0; i < get_num_rows(bg_freqs); ++i) {
    for (j = 0; j < get_num_cols(bg_freqs); ++j) {
      double p = get_matrix_cell_defcheck(i, j, bg_freqs);
      double cxj = get_matrix_cell_defcheck(i, j, phospho_count);
      int n = num_phospho_seqs;
      double log_value = log_betai(cxj, n-cxj+1, p);
      set_matrix_cell_defcheck(i, j, log_value, bg_freqs);
    }
  }
}
// function that adds twos around the movement range where a player can attack
// THIS FUNCTION ONLY WORKS PROPERLY FOR ATTACK RANGES 1 AND 2.
void Valid_board::add_attack_spots(int range){

  for(int i = 0; i < valid_tiles.size(); i++){		// nested loops go through each element
    for(int j = 0; j < valid_tiles[0].size(); j++){
      if(valid_tiles[i][j]==1){				// if the tile is a 1,
        for( int k = range; k >= 1; k--){		// for each time in the range
	// Checks k tiles in each of the four directions, and if the tile is a 0, makes it a 2 to indicate that you can attack there
          if(i-k >= 0 && valid_tiles[i-k][j] == 0) valid_tiles[i-k][j] = 2;
          if(i+k < get_num_rows() && valid_tiles[i+k][j] == 0) valid_tiles[i+k][j] = 2;
          if(j-k >= 0 && valid_tiles[i][j-k] == 0) valid_tiles[i][j-k] = 2;
          if(j+k < get_num_cols() && valid_tiles[i][j+k] == 0) valid_tiles[i][j+k] = 2;
        }
	// if the range is 2, you need to add diagonal spots as well
        if(range > 1){	
          if(i-1 >= 0 && j-1 >= 0 && valid_tiles[i-1][j-1] == 0) valid_tiles[i-1][j-1] = 2; 
          if(i-1 >= 0 && j+1 < get_num_cols() && valid_tiles[i-1][j+1] == 0) valid_tiles[i-1][j+1] = 2; 
          if(i+1 < get_num_rows() && j-1 >= 0 && valid_tiles[i+1][j-1] == 0) valid_tiles[i+1][j-1] = 2; 
          if(i+1 < get_num_rows() && j+1 < get_num_cols() && valid_tiles[i+1][j+1] == 0) valid_tiles[i+1][j+1] = 2; 
        }
      }
    }
  }
}
Exemplo n.º 19
0
/*************************************************************************
 * Converts the motif frequency matrix into a odds matrix: taken from old ama-scan.c
 *************************************************************************/
void convert_to_odds_matrix(MOTIF_T* motif, ARRAY_T* bg_freqs){
  const int asize = alph_size(get_motif_alph(motif), ALPH_SIZE);
  int motif_position_index,alph_index;
  MATRIX_T *freqs;
  freqs = get_motif_freqs(motif);

  const int num_motif_positions = get_num_rows(freqs);
  for (alph_index=0;alph_index<asize;++alph_index){
    double bg_likelihood = get_array_item(alph_index, bg_freqs);
    for (motif_position_index=0;motif_position_index<num_motif_positions;++motif_position_index){
      freqs->rows[motif_position_index]->items[alph_index] /= bg_likelihood;
    }
  }
}
Exemplo n.º 20
0
void
MILPP::set_cons_matrix(const PackedMatrix* matrix)
{
  cons_matrix_ = *matrix;

  for (int i = 0; i < get_num_rows(); i++)
    {
      PackedVector* row = get_row(i);
      for (int j = 0; j < row->get_num_elements(); j++)
        {
          col_to_row_mapping_[ row->get_index_by_pos(j) ]->insert(i);
          row_to_col_mapping_[i]->insert( row->get_index_by_pos(j) );
        }
    }
}
Exemplo n.º 21
0
/*************************************************************************
 * Copies the motif frequency matrix and converts it into a odds matrix
 *************************************************************************/
MATRIX_T* create_odds_matrix(MOTIF_T *motif, ARRAY_T* bg_freqs){
  const int asize = alph_size(get_motif_alph(motif), ALPH_SIZE);
  int pos, aidx;
  MATRIX_T *odds;
  
  odds = duplicate_matrix(get_motif_freqs(motif));
  const int num_pos = get_num_rows(odds);
  for (aidx = 0; aidx < asize; ++aidx) {
    double bg_likelihood = get_array_item(aidx, bg_freqs);
    for (pos = 0; pos < num_pos; ++pos) {
      odds->rows[pos]->items[aidx] /= bg_likelihood;
    }
  }
  return odds;
}
Exemplo n.º 22
0
 float get_convergence_sqd (float ** current_ptr,float ** next_ptr,int rank) {
    int i,j,my_start,my_end,my_num_rows;
    float sum;
    
    my_start = get_start(rank);
    my_end = get_end(rank);
    my_num_rows = get_num_rows(rank);
 
    sum = 0.0;
    for (j=my_start;j<=my_end;j++) {
      for (i=0;i<(int)floor(WIDTH/H);i++) {
        sum += pow(next_ptr[global_to_local(rank,j)][i]-current_ptr[global_to_local(rank,j)][i],2);
      }
    }
    return sum;   
 }
Exemplo n.º 23
0
/******************************************************************************
  * This function returns a copy of the ith matrix of a transition matrix array.
  * Caller is responsible for freeing the returned copy.
******************************************************************************/
static MATRIX_T* get_substmatrix_matrix(
  SUBSTMATRIX_ARRAY_T *a, 
  int i
) {
  assert(a != NULL);
  assert(i < a->length);
  // Create a copy of our matrix
  MATRIX_T* src = a->substmatrix[i];
  MATRIX_T* dst = NULL;
  if (src != NULL) {
    int num_rows = get_num_rows(src);
    int num_cols = get_num_cols(src);
    dst = allocate_matrix(num_rows, num_cols);
    copy_matrix(src, dst);
  }
  return dst;
}
Exemplo n.º 24
0
stomap* MtxLP::getColSto(string name) const{
    stomap* rv = new stomap;
    int n = ncol(name);
    if (n == 0) throw runtime_error(name + ": no such row!");
    int len = get_num_rows();
    int *inds = new int[len + 1];
    double *vals = new double[len + 1];
    len = get_mat_col(n, inds, vals);

    for (int i = 1; i <= len; i++){
        if (inds[i] <= mtxlen)
            (*rv)[get_row_name(inds[i])] = vals[i];
    }

    delete inds;
    delete vals;

    return rv;
}
void Board::display() const {
	for (int r = get_num_rows() - 1; r >= 0; r--) {
		std::cout << r << ":| ";
		for (int c = 0; c < get_num_cols(); c++) {
			std::cout << get_cell(c, r) << " ";
		}
		std::cout << std::endl;
	}
	std::cout << "   -";
	for (int c = 0; c < get_num_cols(); c++) {
		std::cout << "--";
	}
	std::cout << "\n";
	std::cout << "    ";
	for (int c = 0; c < get_num_cols(); c++) {
		std::cout << c << " ";
	}
	std::cout << "\n\n";
}
Exemplo n.º 26
0
/***********************************************************************
 * Allocate memory for a MEME motif
 ***********************************************************************/
MOTIF_T* allocate_motif(
    char *id,
    ALPH_T alph,
    MATRIX_T* scores,
    MATRIX_T* freqs
) {
    MOTIF_T* motif = mm_malloc(sizeof(MOTIF_T));

    assert(id != NULL);

    if (freqs == NULL && scores == NULL) {
        die(
            "A matrix of scores, or frequencies, or both, "
            "must be provided when allocating a motif\n"
        );
    }

    motif->length = get_num_rows(freqs);
    motif->alph = alph;
    motif->flags = 0;
    motif->evalue = 0.0;
    motif->log_evalue = -HUGE_VAL;
    motif->num_sites = 0.0;
    motif->complexity = 0.0;

    int length = strlen(id) + 1;
    strncpy(motif->id, id, MIN(length, MAX_MOTIF_ID_LENGTH));

    if (scores != NULL) {
        motif->scores = duplicate_matrix(scores);
    }
    if (freqs != NULL) {
        motif->freqs = duplicate_matrix(freqs);
    }

    motif->url = NULL;
    motif->trim_left = 0;
    motif->trim_right = 0;

    return motif;
}
Exemplo n.º 27
0
Arquivo: motif.c Projeto: CPFL/gmeme
/***********************************************************************
 * Takes a matrix of meme scores and converts them into letter 
 * probabilities.
 *
 * The probablility can be got by:
 * p = (2 ^ (s / 100)) * bg
 *
 ***********************************************************************/
MATRIX_T* convert_scores_into_freqs
  (ALPH_T alph,
   MATRIX_T *scores,
   ARRAY_T *bg,
   int site_count,
   double pseudo_count)
{
  int asize, length;
  double freq, score, total_count, counts, bg_freq;
  MATRIX_T *freqs;
  int row, col;

  assert(alph != INVALID_ALPH);
  assert(scores != NULL);
  assert(bg != NULL);

  length = get_num_rows(scores);
  asize = alph_size(alph, ALPH_SIZE);

  freqs = allocate_matrix(length, asize);
  total_count = site_count + pseudo_count;

  for (col = 0; col < asize; ++col) {
    bg_freq = get_array_item(col, bg);
    for (row = 0; row < length; ++row) {
      score = get_matrix_cell(row, col, scores);
      // convert to a probability
      freq = pow(2.0, score / 100.0) * bg_freq;
      // remove the pseudo count
      freq = ((freq * total_count) - (bg_freq * pseudo_count)) / site_count;
      if (freq < 0) freq = 0;
      else if (freq > 1) freq = 1;
      set_matrix_cell(row, col, freq, freqs);
    }
  }
  for (row = 0; row < length; ++row) {
    normalize_subarray(0, asize, 0.0, get_matrix_row(row, freqs));
  }

  return freqs;
}
Exemplo n.º 28
0
/***********************************************************************
 * Converts a TRANSFAC motif to a MEME motif.
 * Caller is responsible for freeing the returned MOTIF_T.
 ***********************************************************************/
MOTIF_T *convert_transfac_motif_to_meme_motif(
    char *id,
    int pseudocount,
    ARRAY_T *bg,
    TRANSFAC_MOTIF_T *motif
) {
    MATRIX_T *counts = get_transfac_counts(motif);
    if (counts == NULL) {
        die(
            "Unable to convert TRANSFAC motif %s to MEME motif: "
            "missing counts matrix.",
            id
        );
    };

    // Convert the motif counts to frequencies.
    int num_bases = get_num_cols(counts);
    int motif_width = get_num_rows(counts);
    int motif_position = 0;
    MATRIX_T *freqs = allocate_matrix(motif_width, num_bases);
    for (motif_position = 0; motif_position < motif_width; ++motif_position) {
        int i_base = 0;
        int num_seqs = 0; // motif columns may have different counts
        for (i_base = 0; i_base < num_bases; i_base++) {
            num_seqs += get_matrix_cell(motif_position, i_base, counts);
        }
        for (i_base = 0; i_base < num_bases; i_base++) {
            double freq =
                (get_matrix_cell(motif_position, i_base, counts)
                 + (pseudocount * get_array_item(i_base, bg))) / (num_seqs + pseudocount);
            set_matrix_cell(motif_position, i_base, freq, freqs);
        }
    }

    MOTIF_T *meme_motif = allocate_motif(id, DNA_ALPH, NULL, freqs);
    calc_motif_ambigs(meme_motif);
    return meme_motif;
}
Exemplo n.º 29
0
bool edt_server::write_opengl_texture_to_quad()
{
  double xfrac = static_cast<double>(get_num_columns()) / _opengl_texture_size_x;
  double yfrac = static_cast<double>(get_num_rows()) / _opengl_texture_size_y;

  // Flip over while writing.
  // Set the texture and vertex coordinates and write the quad to OpenGL.
  glBegin(GL_QUADS);
    glTexCoord2d(0.0, yfrac);
    glVertex2d(-1.0, -1.0);

    glTexCoord2d(xfrac, yfrac);
    glVertex2d(1.0, -1.0);

    glTexCoord2d(xfrac, 0.0);
    glVertex2d(1.0, 1.0);

    glTexCoord2d(0.0, 0.0);
    glVertex2d(-1.0, 1.0);
  glEnd();

  return true;
}
Exemplo n.º 30
0
MATRIX_T *convert_score_to_target(
  MATRIX_T *score,			/* score matrix */
  ARRAY_T *prob				/* letter frequencies */
)
{
  int i, j;
  KARLIN_INPUT_T *karlin_input;
  double lambda, K, H;
  MATRIX_T *target;			/* target freq. matrix */
  int alen = get_num_rows(score);	/* alphabet length */

  /* make input for karlin() */
  karlin_input = make_karlin_input(score, prob);
  
  /* get lambda */
  karlin(karlin_input->low, karlin_input->high, karlin_input->prob->items,
    &lambda, &K, &H);
  /*printf("lambda %f K %f H %f\n", lambda, K, H);*/

  /* calculate target frequencies */
  target = allocate_matrix(alen, alen);
  for (i=0; i<alen; i++) {
    for (j=0; j<alen; j++) {
      double pi = get_array_item(i, prob);
      double pj = get_array_item(j, prob);
      double sij = get_matrix_cell(i, j, score);
      double f = pi * pj * exp(lambda * sij);
      set_matrix_cell(i, j, f, target);
    }
  }

  // Free local dynamic memory.
  free_array(karlin_input->prob);
  myfree(karlin_input);

  return(target);
} /* convert_score_to_target */