function tripal_feature_load_organism_feature_counts

2.x tripal_feature.theme.inc tripal_feature_load_organism_feature_counts($organism)
3.x tripal_feature.theme.inc tripal_feature_load_organism_feature_counts($organism)
1.x tripal_feature.module tripal_feature_load_organism_feature_counts($organism)

Load the arguments for the organism feature counts browser

Parameters

$organism: The organism of interest

1 call to tripal_feature_load_organism_feature_counts()

File

legacy/tripal_feature/theme/tripal_feature.theme.inc, line 600

Code

function tripal_feature_load_organism_feature_counts($organism) {

  $args = array();
  $order = array();
  $names = array();

  // We should not assume this table is present since it is a materialized view.
  if (!chado_table_exists('organism_feature_count')) {
    return NULL;
  }

  // build the where clause for the SQL statement if we have a custom term list
  // we'll also keep track of the names the admin provided (if any) and the
  // order that the terms should appear.
  $is_custom = 0;
  $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  $where = '';
  if ($temp) {
    $is_custom = 1;
    $temp = explode("\n", $temp);
    $i = 0;
    foreach ($temp as $value) {
      // separate the key value pairs
      $temp2 = explode("=", $value);
      $feature_type = rtrim($temp2[0]);
      $order[] = $feature_type; // save the order of the these terms
      $where .= " OFC.feature_type = :name$i OR ";
      $args[":name$i"] = rtrim($temp2[0]);

      // if the admin specified a new name then store that otherwise use the
      // the default sequence ontology term name
      if (count($temp2) == 2) {
        $names[] = rtrim($temp2[1]);
      }
      else {
        $names[] = $feature_type;
      }
      $i++;
    }
    if ($where) {
      $where = drupal_substr($where, 0, -4); # remove OR from the end
      $where = "($where) AND";
    }
  }

  // get the feature counts.  This is dependent on a materialized view
  // installed with the organism module
  $sql = "
    SELECT OFC.num_features,OFC.feature_type,CVT.definition
    FROM {organism_feature_count} OFC
      INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
    WHERE $where organism_id = :organism_id
    ORDER BY num_features desc
  ";
  $args[':organism_id'] = $organism->organism_id;
  $org_features = chado_query($sql, $args);

  // iterate through the types
  $types = array();
  while ($type = $org_features->fetchObject()) {
    $types[$type->feature_type] = $type;
    // if we don't have an order this means we didn't go through the loop
    // above to set the names, so do that now
    if (!$is_custom) {
      $names[] = $type->feature_type;
      $order[] = $type->feature_type;
    }
  }

  // now reorder the types
  $ordered_types = array();
  foreach ($order as $type) {
    if (array_key_exists($type, $types)) {
      $ordered_types[] = $types[$type];
    }
  }
  return array(
    'types' => $ordered_types,
    'names' => $names
  );
}