Browse Source

Added support in the CV lookup tool when vocabularies are not ontologies or are not fully loaded

Stephen Ficklin 7 years ago
parent
commit
537d50085d

+ 39 - 0
tripal/api/tripal.terms.api.inc

@@ -153,6 +153,20 @@ function hook_vocab_import_form_submit($form, &$form_state) {
 function hook_vocab_get_term($vocabulary, $accession) {
   // See the tripal_chado_vocab_get_term() function for an example.
 
+}
+/**
+ * Retrieves a paged list of terms from a vocabulary.
+ *
+ * @param $vocabulary
+ *   The short name of the vocabulary.
+ * @param $limit
+ *   The number of results to return.
+ * @param $element
+ *   The pager element. This is equivalent to the element from the
+ *   pager_default_initialize() function of Drupal.
+ */
+function hook_vocab_get_terms($vocabulary, $limit = 25, $element = 0) {
+
 }
 /**
  * Hook used by the default term storage backend to provide children for a term.
@@ -433,6 +447,7 @@ function tripal_get_term_children($vocabulary, $accession) {
  *       position of the short_name and the term accession will be
  *       specified with the {db} and {accession} tags respectively.
  *     - sw_url: The URL for mapping terms via the semantic web.
+ *     - num_terms: The number of terms loaded in the vocabulary.
  *
  * @ingroup tripal_terms_api
  */
@@ -451,6 +466,30 @@ function tripal_get_vocabulary_details($vocabulary) {
   }
 }
 
+
+/**
+ * Retrieves a paged list of terms from a vocabulary.
+ *
+ * @param $vocabulary
+ *   The short name of the vocabulary.
+ * @param $limit
+ *   The number of results to return.
+ * @param $element
+ *   The pager element. This is equivalent to the element from the
+ *   pager_default_initialize() function of Drupal.
+ */
+function tripal_get_vocabulary_terms($vocabulary, $limit = 25, $element = 0) {
+  $stores = module_invoke_all('vocab_storage_info');
+  if (is_array($stores) and count($stores) > 0) {
+    $keys = array_keys($stores);
+    $module = $stores[$keys[0]]['module'];
+    $function = $module . '_vocab_get_terms';
+    if (function_exists($function)) {
+      return $function($vocabulary, $limit, $element);
+    }
+  }
+}
+
 /**
  * Retrieves the list of vocabularies that are available on the site.
  *

+ 39 - 7
tripal/includes/tripal.term_lookup.inc

@@ -16,10 +16,14 @@ function tripal_vocabulary_lookup_page() {
       l($vocabulary['short_name'], 'cv/lookup/' . $vocabulary['short_name']),
       $vocabulary['name'],
       $vocabulary['description'],
+      array(
+        'data' => number_format($vocabulary['num_terms']),
+        'style' => 'text-align: right;'
+      ),
     );
   }
 
-  $headers = array('Short Name', 'Vocabulary Name(s)', 'Description');
+  $headers = array('Short Name', 'Vocabulary Name(s)', 'Description', 'Loaded Terms');
   $table = array(
     'header' => $headers,
     'rows' => $rows,
@@ -56,7 +60,7 @@ function tripal_vocabulary_lookup_vocab_page($vocabulary) {
 
   $vocab = tripal_get_vocabulary_details($vocabulary);
   if ($vocab['description']) {
-    drupal_set_title($vocab['description']);
+    drupal_set_title($vocabulary . ': ' . $vocab['description']);
   }
   else {
     drupal_set_title($vocabulary);
@@ -79,19 +83,19 @@ function tripal_vocabulary_lookup_vocab_page($vocabulary) {
   $vocab_desc = $vocab['description'];
   $rows[] = array(
     array(
-      'data' => 'Vocabulary Name(s)',
+      'data' => 'Short Name',
       'header' => TRUE,
       'width' => '20%',
     ),
-    $vocab_name,
+    $short_name,
   );
   $rows[] = array(
     array(
-      'data' => 'Short Name',
+      'data' => 'Vocabulary Name(s)',
       'header' => TRUE,
       'width' => '20%',
     ),
-    $short_name,
+    $vocab_name,
   );
   $rows[] = array(
     array(
@@ -101,6 +105,14 @@ function tripal_vocabulary_lookup_vocab_page($vocabulary) {
     ),
     $vocab_desc,
   );
+  $rows[] = array(
+    array(
+      'data' => 'Number of Loaded Terms',
+      'header' => TRUE,
+      'width' => '20%',
+    ),
+    number_format($vocab['num_terms']),
+  );
 
   $table = array(
     'header' => $headers,
@@ -112,7 +124,15 @@ function tripal_vocabulary_lookup_vocab_page($vocabulary) {
     'empty' => '',
   );
 
+  $has_root = TRUE;
   $root_terms = tripal_get_vocabulary_root_terms($vocabulary);
+  // If this vocabulary doesn't have root terms then it's either not an
+  // ontology or not all of the terms are loaded. In this case, let's get
+  // a paged list of all terms
+  if (count($root_terms) == 0) {
+    $root_terms = tripal_get_vocabulary_terms($vocabulary, 25);
+    $has_root = FALSE;
+  }
   $items = tripal_vocabulary_lookup_term_children_format($root_terms);
 
   if (count($root_terms) == 0) {
@@ -142,6 +162,12 @@ function tripal_vocabulary_lookup_vocab_page($vocabulary) {
       '#markup' => $items,
     ),
   );
+  if (!$has_root) {
+    $content['pager']= array(
+      '#type' => 'markup',
+      '#markup' => theme('pager'),
+    );
+  }
 
   // Add support for our custom tree viewer
   drupal_add_css(drupal_get_path('module', 'tripal') . '/theme/css/tripal.cv_lookup.css');
@@ -171,7 +197,13 @@ function tripal_vocabulary_lookup_term_children_format($children) {
       $class = 'tree-node-single';
     }
     $items .= '<i class = "tree-node-icon ' . $class . '"></i>';
-    $items .= l($child['name'], 'cv/lookup/' . $child['vocabulary']['short_name'] . '/' . $child['accession'], array('attributes' => array('target' => '_blank'))) . ' (' . $num_grand . ')';
+    $items .= l($child['name'], 'cv/lookup/' . $child['vocabulary']['short_name'] . '/' . $child['accession'], array('attributes' => array('target' => '_blank')));
+    if ($child['accession'] != $child['name']) {
+      $items .= ' [' . $child['vocabulary']['short_name'] . ':' . $child['accession'] . '] ';
+    }
+    if ($num_grand > 0) {
+      $items .= ' (' . $num_grand . ')';
+    }
     $items .= '</li>';
   }
   $items .= '</ul>';

+ 8 - 1
tripal_chado/includes/setup/tripal_chado.chado_vx_x.inc

@@ -438,6 +438,10 @@ function tripal_chado_add_db2cv_mview_mview() {
         'length' => '255',
         'not null' => true,
       ),
+      'num_terms' => array (
+        'type' => 'int',
+        'not null' => true,
+      ),
     ),
     'indexes' => array (
       'cv_id_idx'  => array ('cv_id'),
@@ -448,11 +452,14 @@ function tripal_chado_add_db2cv_mview_mview() {
   );
 
   $sql = "
-   SELECT DISTINCT CV.cv_id, CV.name as cvname, DB.db_id, DB.name as dbname
+   SELECT DISTINCT CV.cv_id, CV.name as cvname, DB.db_id, DB.name as dbname,
+     SUM(CVT.cvterm_id) as num_terms
    FROM cv CV
      INNER JOIN cvterm CVT on CVT.cv_id = CV.cv_id
      INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
      INNER JOIN db DB on DB.db_id = DBX.db_id
+   GROUP BY CV.cv_id, CV.name, DB.db_id, DB.name
+   ORDER BY DB.name
   ";
 
   // Create the MView

+ 44 - 10
tripal_chado/includes/tripal_chado.vocab_storage.inc

@@ -40,6 +40,7 @@ function tripal_chado_vocab_get_vocabularies() {
 
   $sql = "
      SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
+       SUM(DBCVM.num_terms) as num_terms,
        array_to_string(array_agg(DBCVM.cvname), ', ') as name
      FROM {db} DB
       INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
@@ -85,6 +86,7 @@ function tripal_chado_vocab_get_vocabulary($vocabulary) {
 
   $sql = "
      SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
+       SUM(DBCVM.num_terms) as num_terms,
        array_to_string(array_agg(DBCVM.cvname), ', ') as name
      FROM {db} DB
       INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
@@ -125,7 +127,6 @@ function tripal_chado_vocab_get_root_terms($vocabulary) {
     return FALSE;
   }
 
-
   // Get the list of CV's that belong to this vocabulary and get their
   // roots.
   $sql = "
@@ -148,6 +149,46 @@ function tripal_chado_vocab_get_root_terms($vocabulary) {
   }
   return $terms;
 }
+
+/**
+ * Implements hook_vocab_get_terms().
+ *
+ * This hook is created by the Tripal module and is not a Drupal hook.
+ */
+function tripal_chado_vocab_get_terms($vocabulary, $limit = 25, $element = 0) {
+  // It's possible that Chado is not available (i.e. it gets renamed
+  // for copying) but Tripal has already been prepared and the
+  // entities exist.  If this is the case we don't want to run the
+  // commands below.
+  if (!chado_table_exists('cvterm')) {
+    return FALSE;
+  }
+
+  $sql = "
+    SELECT CVT.cvterm_id
+    FROM {cvterm} CVT
+      INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
+      INNER JOIN {db} DB on DB.db_id = DBX.db_id
+    WHERE db.name = :dbname
+    ORDER BY CVT.name
+  ";
+  $csql = "
+    SELECT COUNT(CVT.cvterm_id)
+    FROM {cvterm} CVT
+      INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
+      INNER JOIN {db} DB on DB.db_id = DBX.db_id
+    WHERE db.name = :dbname
+  ";
+  $results = chado_pager_query($sql, array(':dbname' => $vocabulary), $limit, $element, $csql);
+
+  $terms = array();
+  while($cvterm_id = $results->fetchField()) {
+    $match = array('cvterm_id' => $cvterm_id);
+    $cvterm = chado_generate_var('cvterm', $match);
+    $terms[] = _tripal_chado_format_term_description($cvterm);
+  }
+  return $terms;
+}
 /**
  * Implements hook_vocab_get_term_children().
  *
@@ -243,16 +284,9 @@ function _tripal_chado_format_term_description($cvterm) {
     $sw_url = preg_replace('/{accession}/', '', $sw_url);
     $sw_url = url($sw_url, array('absolute' => TRUE));
   }
-
+  $vocabulary = tripal_chado_vocab_get_vocabulary($cvterm->dbxref_id->db_id->name);
   $term = array(
-    'vocabulary' => array(
-      'name' => $cvterm->cv_id->name,
-      'short_name' => $cvterm->dbxref_id->db_id->name,
-      'description' =>  $cvterm->dbxref_id->db_id->description,
-      'url' => $url,
-      'urlprefix' => $urlprefix,
-      'sw_url' => $sw_url,
-    ),
+    'vocabulary' => $vocabulary,
     'accession'  => $cvterm->dbxref_id->accession,
     'name'       => $cvterm->name,
     'url'        => tripal_get_dbxref_url($cvterm->dbxref_id),

+ 24 - 1
tripal_chado/tripal_chado.install

@@ -1488,6 +1488,29 @@ function tripal_chado_update_7323() {
     throw new DrupalUpdateException('Could not perform update: '. $error);
   }
 }
-
+/**
+ * Updating the db2cv materialized view.
+ */
+function tripal_chado_update_7324() {
+  try {
+    if (chado_table_exists(db2cv_mview) and !chado_column_exists('db2cv_mview', 'num_terms')) {
+      module_load_include('inc', 'tripal_chado', 'includes/setup/tripal_chado.chado_vx_x');
+
+      // Remove the old mview.
+      $mview_id = tripal_get_mview_id('db2cv_mview');
+      tripal_delete_mview($mview_id);
+
+      // Readd the mview.
+      tripal_chado_add_db2cv_mview_mview();
+      drupal_set_message('Populating materialized view db2cv_mview...');
+      $mview_id = tripal_get_mview_id('db2cv_mview');
+      tripal_populate_mview($mview_id);
+    }
+  }
+  catch (\PDOException $e) {
+    $error = $e->getMessage();
+    throw new DrupalUpdateException('Could not perform update: '. $error);
+  }
+}