Exemplo n.º 1
0
/*************************************************************************
 * 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;
}
Exemplo n.º 2
0
/**************************************************************************
*
*	get_unscaled_pssm_score
*
**************************************************************************/
double get_unscaled_pssm_score(
  double score,
  PSSM_T* pssm
)
{
  return scaled_to_raw(
           score, 
	   get_pssm_w(pssm), 
           get_pssm_scale(pssm), 
           get_pssm_offset(pssm)
    );
}
Exemplo n.º 3
0
/**************************************************************************
*	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;

}
Exemplo n.º 4
0
/**
 * @brief Set event threshold value
 *
 * @param hal       Address of an initialized sensor hardware descriptor.
 * @param threshold Address of threshold descriptor.
 * @return bool     true if the call succeeds, else false is returned.
 */
static bool bma250_set_threshold
	(sensor_hal_t *hal, sensor_threshold_desc_t *threshold)
{
	/* Threshold values will be passed in milli-g units (assumed). */
	int32_t value = scaled_to_raw(hal, threshold->value);

	switch (threshold->type) {
	default:
		return false;

	case SENSOR_THRESHOLD_MOTION:

		/*
		 * Any-Motion (slope) Threshold
		 *
		 * The slope interrupt threshold value LSB corresponds to an LSB
		 * of acceleration data for the selected g-range. The default
		 * value of 14h for the default 2mg range implies a threshold of
		 * 312.5mg.
		 */
		sensor_bus_put(hal, BMA250_SLOPE_THRESHOLD, (uint8_t)value);
		break;

	case SENSOR_THRESHOLD_TAP:

		/*
		 * Single-Tap or Double-Tap Threshold
		 *
		 * An LSB of tap threshold depends upon the selected g-range
		 * where an acceleration delta of 62.5mg in 2-g, 125mg in 4-g,
		 *etc.
		 * will apply. The default 0ah raw value corresponds to the
		 *default
		 * 2mg range.
		 */
	{
		int8_t const mask = BMA250_TAP_TH_FIELD;
		sensor_reg_fieldset(hal, BMA250_TAP_CONFIG, mask,
				(uint8_t)value);
	}
	break;

	case SENSOR_THRESHOLD_LOW_G:

		/*
		 * Low-G Threshold
		 *
		 * An LSB of low-g threshold always corresponds to an
		 * acceleration of 7.81mg; namely, the increment is independent
		 * of the g-range. Divide the requested threshold in milli-g
		 * by 7.81mg (781/100) to calculate the register value.
		 */
		value = (threshold->value * 100) / 781;
		sensor_bus_put(hal, BMA250_LOW_G_THRESHOLD, (uint8_t)value);
		break;

	case SENSOR_THRESHOLD_HIGH_G:

		/*
		 * High-G Threshold
		 *
		 * An LSB of high-g threshold depends upon the selected g-range
		 * where an acceleration delta of 62.5mg in 2-g, 125mg in 4-g,
		 * etc. will apply. The default 0ah raw value corresponds to the
		 * default 2mg range.
		 */
		sensor_bus_put(hal, BMA250_HIGH_G_THRESHOLD, (uint8_t)value);
		break;
	}

	return (STATUS_OK == hal->bus.status);
}