Browse Source

Add maximum record limit to sbo__database_cross_reference

dsenalik 4 years ago
parent
commit
43cac0a978

+ 22 - 11
tripal_chado/includes/TripalFields/sbo__database_cross_reference/sbo__database_cross_reference.inc

@@ -39,6 +39,8 @@ class sbo__database_cross_reference extends ChadoField {
     'term_fixed' => FALSE,
     // The number of items to show on a page.
     'items_per_page' => 10,
+    // Limit to the number of items to show in cases of large number of cross references.
+    'max_items' => 10000,
   ];
 
   // The default widget for this field.
@@ -142,29 +144,38 @@ class sbo__database_cross_reference extends ChadoField {
     $options = ['return_array' => 1];
 
     // Build the SQL to find records associated with this publication.
+    $max_items = array_key_exists('max_items', $this->instance['settings']) ? $this->instance['settings']['max_items'] : 10000;
     $sql = "SELECT REF.accession, DB.name, DB.urlprefix "
          . "FROM {".$linker_table."} LINK "
          . "INNER JOIN {dbxref} REF on LINK.dbxref_id = REF.dbxref_id "
          . "INNER JOIN {db} DB on REF.db_id = DB.db_id "
-         . "WHERE LINK.$fkey_lcolumn = :id";
-    $args = [':id' => $entity->{'chado_record_id'}];
+         . "WHERE LINK.$fkey_lcolumn = :id "
+         // Ignore the GFF_source database. This is a weird thing required by
+         // GBrowse and is added by the GFF loader. We don't want to show it.
+         . "AND NOT DB.name = 'GFF_source' "
+         . "ORDER BY REF.accession "  // if we hit the limit, the subset should be consistent
+         . "LIMIT :limit";
+    $args = [':id' => $entity->{'chado_record_id'},
+             ':limit' => $max_items + 1];
     $records = chado_query($sql, $args);
 
     // Store the query results
     $delta = 0;
     while($record = $records->fetchObject()) {
-      // Ignore the GFF_source database. This is a weird thing required by
-      // GBrowse and is added by the GFF loader. We don't want to show it.
-      if ($record->name == 'GFF_source') {
-        continue;
+      // Need this check to detect the case where the limit exactly equals the number of records
+      if ($delta < $max_items) {
+        $entity->{$field_name}['und'][$delta] = [];
+        $entity->{$field_name}['und'][$delta]['value'][$dbname_term] = $record->name;
+        $entity->{$field_name}['und'][$delta]['value'][$accession_term] = $record->accession;
+        $entity->{$field_name}['und'][$delta]['value'][$dburl_term] = $record->urlprefix;
       }
-      $entity->{$field_name}['und'][$delta] = [];
-      $entity->{$field_name}['und'][$delta]['value'][$dbname_term] = $record->name;
-      $entity->{$field_name}['und'][$delta]['value'][$accession_term] = $record->accession;
-      $entity->{$field_name}['und'][$delta]['value'][$dburl_term] = $record->urlprefix;
       $delta++;
     }
-
+    // Display a warning if we have exceeded the maximum number of cross references
+    if ( $delta > $max_items ) {
+      $entity->{$field_name}['und'][$delta]['value'][$dbname_term] = 'Note';
+      $entity->{$field_name}['und'][$delta]['value'][$accession_term] = "Only the first $max_items cross references are shown";
+    }
   }
 
   /**

+ 3 - 3
tripal_chado/includes/TripalFields/sbo__database_cross_reference/sbo__database_cross_reference_formatter.inc

@@ -36,11 +36,11 @@ class sbo__database_cross_reference_formatter extends ChadoFieldFormatter {
     foreach ($items as $delta => $item) {
       $db = $item['value'][$dbname_term];
       $accession = $item['value'][$accession_term];
-      // It is possible that a database does not have a url, in which case no link can be generated
-      if ($item['value'][$dburl_term]) {
+      // It is possible that a database does not have a url, in which case no link can be generated.
+      if (array_key_exists($dburl_term, $item['value']) and $item['value'][$dburl_term]) {
         $url = $item['value'][$dburl_term];
 
-        // this emulates chado_get_dbxref_url() but implemented inline here for better performance
+        // This emulates chado_get_dbxref_url() but implemented inline here for better performance.
         $db_count = 0;
         $acc_count = 0;
         $url = preg_replace('/\{db\}/', $db, $url, -1, $acc_count);