Browse Source

Added organism-stock count materialized view to mimic feature one -still need to add Stock summary tpl to display it

Lacey Sanderson 11 years ago
parent
commit
63432675d2
2 changed files with 170 additions and 6 deletions
  1. 87 6
      tripal_stock/tripal_stock.install
  2. 83 0
      tripal_stock/tripal_stock.module

+ 87 - 6
tripal_stock/tripal_stock.install

@@ -49,11 +49,14 @@ function tripal_stock_requirements($phase) {
 function tripal_stock_install() {
   // create the module's data directory
   tripal_create_files_dir('tripal_stock');
-  
+
   // set the default vocabularies
   tripal_set_default_cv('stock', 'type_id', 'stock_type');
   tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
   tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
+
+  // add the materialized view
+  tripal_stock_add_organism_count_mview();
 }
 
 /**
@@ -102,6 +105,74 @@ function tripal_stock_schema() {
   return $schema;
 }
 
+/**
+ * Creates a materialized view that stores the type & number of stocks per organism
+ *
+ * @ingroup tripal_stock
+ */
+function tripal_stock_add_organism_count_mview() {
+  $view_name = 'organism_stock_count';
+  $comment = 'Stores the type and number of stocks per organism';
+
+  $schema = array(
+    'description' => $comment,
+    'table' => $view_name,
+    'fields' => array(
+      'organism_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'genus' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+      ),
+      'species' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+      ),
+      'common_name' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => FALSE,
+      ),
+      'num_stocks' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'cvterm_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'stock_type' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+      ),
+    ),
+    'indexes' => array(
+      'organism_stock_count_idx1' => array('organism_id'),
+      'organism_stock_count_idx2' => array('cvterm_id'),
+      'organism_stock_count_idx3' => array('stock_type'),
+    ),
+  );
+
+  $sql = "
+    SELECT
+        O.organism_id, O.genus, O.species, O.common_name,
+        count(S.stock_id) as num_stocks,
+        CVT.cvterm_id, CVT.name as stock_type
+     FROM organism O
+        INNER JOIN stock S  ON O.Organism_id = S.organism_id
+        INNER JOIN cvterm CVT ON S.type_id     = CVT.cvterm_id
+     GROUP BY
+        O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
+  ";
+
+  tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
+}
+
 /**
  * Add cvs related to publications
  *
@@ -141,7 +212,7 @@ function tripal_stock_update_7200() {
   // add the new CVs.  We can't use the Tripal API because during
   // an upgrade from D6 to D7 Tripal is disable. So, we have to manually add these
   // new vocabularies.
-  
+
   // add the stock_relationshp CV
   try {
     $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_relationship'")->fetchField();
@@ -169,7 +240,7 @@ function tripal_stock_update_7200() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Failed to add stock_relationship vocabulary: '. $error);
   }
- 
+
   // add the stock_property CV
   try {
     $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_property'")->fetchField();
@@ -197,8 +268,8 @@ function tripal_stock_update_7200() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Failed to add stock_property vocabulary: '. $error);
   }
-  
-  
+
+
   // add the stock_type CV
   try {
     $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'stock_type'")->fetchField();
@@ -226,7 +297,17 @@ function tripal_stock_update_7200() {
     $error = $e->getMessage();
     throw new DrupalUpdateException('Failed to add stock_type vocabulary: '. $error);
   }
-  
+
+}
+
+/**
+ * Upgrade to 7.x-2.0.1-alpha
+ */
+function tripal_stock_update_7201() {
+
+  // add the materialized view
+  tripal_stock_add_organism_count_mview();
+
 }
 
 /**

+ 83 - 0
tripal_stock/tripal_stock.module

@@ -528,3 +528,86 @@ function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
     $form['actions']['preview']['#access'] = FALSE;
   }
 }
+
+/**
+ * Load the arguments for the organism stock counts browser
+ *
+ * @param $organism
+ *  The organism of interest
+ *
+ * @ingroup tripal_stock
+ */
+function tripal_stock_load_organism_stock_counts($organism) {
+
+  $args = array();
+  $order = array();
+  $names = array();
+
+  // 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_stock_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);
+      $stock_type = rtrim($temp2[0]);
+      $order[] = $stock_type;  // save the order of the these terms
+      $where .= " OFC.stock_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[] = $stock_type;
+      }
+      $i++;
+    }
+    if ($where) {
+      $where = drupal_substr($where, 0, -4);  # remove OR from the end
+      $where = "($where) AND";
+    }
+  }
+
+  // get the stock counts.  This is dependent on a materialized view
+  // installed with the organism module
+  $sql = "
+    SELECT OFC.num_stocks,OFC.stock_type,CVT.definition
+    FROM {organism_stock_count} OFC
+      INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
+    WHERE $where organism_id = :organism_id
+    ORDER BY num_stocks desc
+  ";
+  $args[':organism_id'] = $organism->organism_id;
+  $org_stocks = chado_query($sql, $args);
+
+  // iterate through the types
+  $types = array();
+  while ($type = $org_stocks->fetchObject()) {
+    $types[$type->stock_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->stock_type;
+      $order[] = $type->stock_type;
+    }
+  }
+
+  // now reorder the types
+  $ordered_types = array();
+  foreach ($order as $type) {
+    $ordered_types[] = $types[$type];
+  }
+  return array(
+    'types' => $ordered_types,
+    'names' => $names
+  );
+}