function tripal_feature_preprocess_tripal_feature_bar_chart_type_organism_summary

2.x tripal_feature.theme.inc tripal_feature_preprocess_tripal_feature_bar_chart_type_organism_summary(&$vars)
3.x tripal_feature.theme.inc tripal_feature_preprocess_tripal_feature_bar_chart_type_organism_summary(&$vars)

File

tripal_feature/theme/tripal_feature.theme.inc, line 923

Code

function tripal_feature_preprocess_tripal_feature_bar_chart_type_organism_summary(&$vars) {

  // Add in all the javascript/css files.
  tripal_add_d3js();
  drupal_add_css(drupal_get_path('module', 'tripal_feature') . '/theme/css/tripal_feature.css');
  drupal_add_js(drupal_get_path('module', 'tripal_feature') . '/theme/js/tripalFeature.adminChart.js');

  // Retrieve and process all the data and save it as javascript settings.
  //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  // We are using the organism_feature_count materialized view as the source for our data.
  // Thus grab all the records from this materialized view.
  $organism_feature_count = chado_select_record(
  'organism_feature_count', 
  array('*'), 
  array(), 
  array('order_by' => array('genus' => 'ASC', 'species' => 'ASC', 'feature_type' => 'ASC', 'num_features' => 'DESC'))
  );

  // Initialize variables.
  $chart = array();
  $type_names = array();
  $organism_names = array();
  $max_bar_height = 0;

  // Process each row of the materialzied view into the chart array.
  // Note: it's first keyed by type since each type will be a bar. Each type will have
  // a "bars" array with the start (y0) and end (y1) height on the bar for a given
  // organism. Finally we keep a record of the names of the types & organisms
  // for axis' and legend generation respectively.
  foreach ($organism_feature_count as $row) {

    // Build up the easy details for the current row's type. These will be overridden
    // multiple times but that's more efficient than checking each time.
    $chart[$row->cvterm_id]['cvterm_id'] = $row->cvterm_id;
    $chart[$row->cvterm_id]['name'] = str_replace('_', ' ', $row->feature_type);

    // Save the name of the type and organism into their respective arrays
    // for generation of axis' and legends for the chart.
    $type_names[$row->cvterm_id] = $row->feature_type;
    $organism_names[$row->organism_id] = $row->genus . ' ' . $row->species;

    // Save information about the current organism. This isn't actually used by the
    // chart but can be used to debug the bar generation to follow.
    $chart[$row->cvterm_id]['organisms'][] = array(
      'name' => $row->genus . ' ' . $row->species,
      'value' => (int) $row->num_features
    );

    // Now to build the bar array with the start (y0) and end (y1) height on the
    // bar for a given organism.
    // NOTE: we cannot assume the types are all in order so store y0 & y1 in the
    // $chart[type] array.
    // If y0 has not yet been set for this type then we're starting with the first
    // chunk (organism) on the bar.
    if (!isset($chart[$row->cvterm_id]['y0'])) {
      $chart[$row->cvterm_id]['y0'] = 0;
      $chart[$row->cvterm_id]['y1'] = $row->num_features;
    }
    // Otherwise, add the next chunk (organism) on top of the pre-existing bar.
    else {
      $chart[$row->cvterm_id]['y0'] = $chart[$row->cvterm_id]['y1'];
      $chart[$row->cvterm_id]['y1'] = $chart[$row->cvterm_id]['y0'] + $row->num_features;
    }
    // Now save the bar chunk we just determined.
    $chart[$row->cvterm_id]['bars'][] = array(
      'name' => $row->genus . ' ' . $row->species,
      'y0' => $chart[$row->cvterm_id]['y0'],
      'y1' => $chart[$row->cvterm_id]['y1'],
    );

    // We also need to keep track of the total number of features for a single bar (Type).
    $chart[$row->cvterm_id]['total_features'] = (int) $chart[$row->cvterm_id]['y1'];
    // And the maximum "height" for all bars.
    if ($max_bar_height < $chart[$row->cvterm_id]['total_features']) {
      $max_bar_height = (int) $chart[$row->cvterm_id]['total_features'];
    }
  }

  // Sort based on the total number of features.
  // NOTE: This changes the keys so it's no longer the organism/type_id.
  usort($chart, 'tripal_feature_admin_summary_sort');
  sort($type_names);
  sort($organism_names);

  // We also need to add information about the materialized views
  // so that admin can update it and know how recent the data is.
  $mview = db_query('
    SELECT mview_id, name, last_update
    FROM tripal_mviews
    WHERE mv_table=:mv_table', 
  array(':mv_table' => 'organism_feature_count')
  )->fetchObject();

  $vars['chart_details'] = array(
    'summary' => $chart,
    'types' => $type_names,
    'organisms' => $organism_names,
    'legendPosition' => 'top',
    'maxBarHeight' => $max_bar_height,
    'mviewUrl' => url('admin/tripal/schema/mviews/update/' . $mview->mview_id),
    'mviewTable' => $mview->name,
    'mviewLastUpdate' => $mview->last_update ? format_date($mview->last_update) : '',
  );

  // Save everything we just determined as a Drupal JS settings so that we have access to
  // it in our js script.
  drupal_add_js(array('tripalFeature' => array('admin' => $vars['chart_details'])), 'setting');
}