Explorar o código

Added Views Integration for tripal_feature

laceysanderson %!s(int64=14) %!d(string=hai) anos
pai
achega
068b975f28

+ 6 - 57
tripal_core/views_handlers/views_handler_field_tf_boolean.inc

@@ -17,66 +17,15 @@
  *
  * @ingroup views_field_handlers
  */
-class views_handler_field_tf_boolean extends views_handler_field {
-  function option_definition() {
-    $options = parent::option_definition();
-    $options['type'] = array('default' => 'yes-no');
-    $options['not'] = array('definition bool' => 'reverse');
-
-    return $options;
-  }
-
-  function init(&$view, $options) {
-    parent::init($view, $options);
-
-    $default_formats = array(
-      'yes-no' => array(t('Yes'), t('No')),
-      'true-false' => array(t('True'), t('False')),
-      'on-off' => array(t('On'), t('Off')),
-    );
-    $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
-    $this->formats = array_merge($default_formats, $output_formats);
-  }
-
-  function options_form(&$form, &$form_state) {
-    parent::options_form($form, $form_state);
-    foreach ($this->formats as $key => $item) {
-      $options[$key] = implode('/', $item);
-    }
-
-    $form['type'] = array(
-      '#type' => 'select',
-      '#title' => t('Output format'),
-      '#options' => $options,
-      '#default_value' => $this->options['type'],
-    );
-    $form['not'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Reverse'),
-      '#description' => t('If checked, true will be displayed as false.'),
-      '#default_value' => $this->options['not'],
-    );
-  }
+class views_handler_field_tf extends views_handler_field {
 
   function render($values) {
     $value = $values->{$this->field_alias};
-    if (!empty($this->options['not'])) {
-      $value = !$value;
-    }
-
-    if (isset($this->formats[$this->options['type']])) {
-      if (preg_match('/^t$/', $value)) {
-        return $this->formats[$this->options['type']][0];
-      } else {
-        return $this->formats[$this->options['type']][1];
-      }
-    }
-    else {
-      if (preg_match('/^t$/', $value)) {
-        return $this->formats['yes-no'][0];
-      } else {
-        return $this->formats['yes-no'][1];
-      } 
+    
+    if (preg_match('/^t$/', $value)) {
+      return 'Yes';
+    } else {
+      return 'No';
     }
   }
 }

+ 13 - 0
tripal_feature/tripal_feature.module

@@ -16,6 +16,19 @@ function tripal_feature_init(){
    drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js'); 
    drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js'); 
 }
+
+/*************************************************************************
+ * Implements hook_views_api()
+ * Purpose: Essentially this hook tells drupal that there is views support for
+ *  for this module which then includes tripal_db.views.inc where all the
+ *  views integration code is
+ */ 
+function tripal_feature_views_api() {
+   return array(
+      'api' => 2.0,
+   );
+}
+
 /************************************************************************
  *
  */

+ 84 - 0
tripal_feature/tripal_feature.views.inc

@@ -0,0 +1,84 @@
+<?php
+
+/**
+ *  @file
+ *  This file contains the basic functions for views integration of
+ *  chado/tripal organism tables. Supplementary functions can be found in
+ *  ./views/
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+ 
+/*************************************************************************
+ * Implements hook_views_data()
+ * Purpose: Describe chado/tripal tables & fields to views
+ *
+ * @return: a data array which follows the structure outlined in the
+ *   views2 documentation for this hook. Essentially, it's an array of table
+ *   definitions keyed by chado/tripal table name. Each table definition 
+ *   includes basic details about the table, fields in that table and
+ *   relationships between that table and others (joins)
+ */
+require_once('views/feature.views.inc');
+function tripal_feature_views_data()  {
+  $data = array();
+  
+  $data = array_merge($data, retrieve_feature_views_data());
+  
+  return $data;
+}
+
+/*************************************************************************
+ * Implements hook_views_handlers()
+ * Purpose: Register all custom handlers with views
+ *   where a handler describes either "the type of field", 
+ *   "how a field should be filtered", "how a field should be sorted"
+ *
+ * @return: An array of handler definitions
+ */
+function tripal_feature_views_handlers() {
+ return array(
+   'info' => array(
+     'path' => drupal_get_path('module', 'tripal_feature') . '/views/handlers',
+   ),
+   'handlers' => array(
+     'views_handler_field_feature_nid' => array(
+       'parent' => 'views_handler_field_numeric',
+     ),
+     'views_handler_field_readable_date' => array(
+       'parent' => 'views_handler_field',
+     ),
+   ),
+ );
+}
+
+/**
+ * Implements hook_views_pre_render
+ * Purpose: Intercepts the view after the query has been executed
+ *   All the results are stored in $view->result
+ *   Looking up the NID here ensures the query is only executed once
+ *   for all features in the table.
+ *
+ * @todo add if !<chado/drupal same db> around NID portion
+ */
+function tripal_feature_views_pre_render	(&$view) {
+	if (preg_match('/feature/', $view->base_table)) {
+		
+		// retrieve the feature_id for each record in the views current page
+		$feature_ids = array();
+		foreach ($view->result as $row_num => $row) {
+			$feature_ids[$row_num] = $row->feature_id;
+		}
+
+		// Using the list of feature_ids from the view
+		// lookup the NIDs from drupal
+		// and add that to the results of the view
+		$sql = "SELECT nid, feature_id FROM chado_feature WHERE feature_id IN (".implode(',',$feature_ids).")";
+		$resource = db_query($sql);
+		while ($r = db_fetch_object($resource)) {
+			$key = array_search($r->feature_id, $feature_ids);
+			$view->result[$key]->nid = $r->nid;
+		}
+	}
+}

+ 22 - 0
tripal_feature/views/README

@@ -0,0 +1,22 @@
+This folder contains all supplementary code needed for views integration
+of the chado feature and related tables.
+
+File Descriptions:
+========================================================================
+<chado table name>.views.inc:
+	contains a single function retrieve_<chado table name>_views_data()
+	which describes that table to views. This function is called by
+	<tripal module>_views_data() in ../<tripal module>.views.inc.
+	For more information on the form of this data array look up the
+	views2 documentation for hook_views_data() 
+	-http://views2.logrus.com/doc/html/index.html
+	
+handlers/
+	A folder which contains symbolic links to tripal_core/views_handlers/ files.
+	Each file contained within this folder defines a views handler. Only custom
+	handlers are included in this folder and each must be described in 
+	hook_views_handlers() in ../<tripal module>.views.inc.
+	A views handler does one of the following:
+		1) describe the type of a field and how it should be displayed
+		2) describe a method to sort this field
+		3) describe a method to filter this field

+ 216 - 0
tripal_feature/views/feature.views.inc

@@ -0,0 +1,216 @@
+<?php
+
+/**
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the feature table, it's fields and any joins between it and other tables
+ * @see tripal_feature_views_data() --in tripal_feature.views.inc
+ *
+ * @todo Add better handler for is_analysis, is_obsolete: something which changes the t/f to a true boolean
+ * @todo Add support for the following tables: featureprop, featureloc, featurepos, feature_synonym, feature_relationship
+ * @todo Add join to node table within if <chado/drupal same db>; also addd if not around nid field
+ *
+ * BASE TABLE: feature
+ * @code
+ *   create table feature (
+ *        feature_id serial not null,
+ *        primary key (feature_id),
+ *        dbxref_id int,
+ *        foreign key (dbxref_id) references dbxref (dbxref_id) on delete set null INITIALLY DEFERRED,
+ *        organism_id int not null,
+ *        foreign key (organism_id) references organism (organism_id) on delete cascade INITIALLY DEFERRED,
+ *        name varchar(255),
+ *        uniquename text not null,
+ *        residues text,
+ *        seqlen int,
+ *        md5checksum char(32),
+ *        type_id int not null,
+ *        foreign key (type_id) references cvterm (cvterm_id) on delete cascade INITIALLY DEFERRED,
+ *        is_analysis boolean not null default 'false',
+ *        is_obsolete boolean not null default 'false',
+ *        timeaccessioned timestamp not null default current_timestamp,
+ *        timelastmodified timestamp not null default current_timestamp,
+ *        constraint feature_c1 unique (organism_id,uniquename,type_id)
+ *   );
+ * @endcode
+ */
+ function retrieve_feature_views_data() {
+
+  // Basic table definition
+  $data['feature']['table']['group'] = 'Chado Feature';
+  $data['feature']['table']['base'] = array(
+    'field' => 'feature_id',
+    'title' => 'Chado Features',
+    'help' => 'Features are Sequence Data Records in Chado.',
+    'database' => 'chado'
+  );
+
+  // Table Field Definitions----------------------
+  // Field: feature_id (primary key)
+  $data['feature']['feature_id'] = array(
+    'title' => 'Feature ID',
+    'help' => 'The primary key of a feature',
+    'field' => array(
+     	'handler' => 'views_handler_field_numeric',
+ 		  'click sortable' => TRUE,
+    ),
+    'filter' => array(
+     	'handler' => 'views_handler_filter_numeric',
+    ),
+    'sort' => array(
+     	'handler' => 'views_handler_sort',
+    ),
+  );
+
+  // Calculated Field: Node ID
+  //  use custom field handler to query drupal for the node ID
+  //  this is only needed if chado is in a separate database from drupal
+  $data['feature']['nid'] = array(
+    'title' => 'Node ID',
+    'help' => 'This is the node ID of this feature. It can be used as a link to the node.',
+    'field' => array(
+      'handler' => 'views_handler_field_feature_nid',
+      ),
+  );
+
+  // Field: organism_id (forgeign key)
+  //  join between organism table and this one in tripal_organism/views/organism.views.inc
+  
+  // Field: dbxref_id (forgeign key)
+  //  join between dbxref table and this one in tripal_db/views/dbxref.views.inc
+  
+  // Field: type_id (forgeign key)
+  //  join between cvterm table and this one in tripal_cv/views/cvterm.views.inc
+  
+  // Field: name (varchar 255)
+  $data['feature']['name'] = array(
+    'title' => 'Name',
+    'help' => 'The human-readable, non-unique name of a feature.',
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  // Field: unique name (text)
+  $data['feature']['uniquename'] = array(
+    'title' => 'Unique Name',
+    'help' => 'The unique name of a feature.',
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  // Field: residues (text)
+  $data['feature']['residues'] = array(
+    'title' => 'Residues',
+    'help' => 'The sequence of a feature.',
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  // Field: sequence length (integer)
+  $data['feature']['seqlen'] = array(
+    'title' => 'Sequence Length',
+    'help' => 'The length of the sequence',
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  // Field: is analysis (boolean -t/f)
+  $data['feature']['is_analysis'] = array(
+    'title' => 'Is Analysis',
+    'help' => 'A boolean indicating whether this feature was annotated by means of automated analysis.',
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  // Field: is obsolete (boolean -t/f)
+  $data['feature']['is_obsolete'] = array(
+    'title' => 'Is Obsolete',
+    'help' => 'A boolean indicating whether this feature is obsolete.',
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  // Field: time accessioned (datetime)
+  $data['feature']['timeaccessioned'] = array(
+    'title' => 'Time Accessioned',
+    'help' => 'The date & time when this feature was accessioned (added into the database)',
+    'field' => array(
+      'handler' => 'views_handler_field_readable_date',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+  );
+
+  // Field: time last modified (datetime)
+  $data['feature']['timelastmodified'] = array(
+    'title' => 'Time Last Modified',
+    'help' => 'The date & time when this feature was last modified.',
+    'field' => array(
+      'handler' => 'views_handler_field_readable_date',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+  );
+  
+  return $data;
+ }

+ 1 - 0
tripal_feature/views/handlers/views_handler_field_feature_nid.inc

@@ -0,0 +1 @@
+../../../tripal_core/views_handlers/views_handler_field_feature_nid.inc

+ 1 - 0
tripal_feature/views/handlers/views_handler_field_readable_date.inc

@@ -0,0 +1 @@
+../../../tripal_core/views_handlers/views_handler_field_readable_date.inc

+ 28 - 24
tripal_library/views/library.views.inc

@@ -34,29 +34,25 @@ function retrieve_library_views_data() {
 	);
 	
 	// Define relationships between this table and others
-	$data['library']['table']['join'] = array(
-	  'library_feature' => array(
-	    'left_field' => 'library_id',
-	    'field' => 'library_id',
-	  ),
-	  'feature' => array(
-	    'left_table' => 'library_feature',
-	    'left_field' => 'feature_id',
-	    'field' => 'library_id',
-	  ),
-	);
-	
-	// Describe the joins with the library_feature table
-	$data['library_feature']['table']['join'] = array(
-	  'feature' => array(
-	    'left_field' => 'feature_id',
-	    'field' => 'feature_id',
-	  ),
-	  'library' => array(
-	    'left_field' => 'library_id',
-	    'field' => 'library_id',
-	  ),   
-	);
+  $data['library']['table']['join'] = array(
+    'library_feature' => array(
+      'left_field' => 'library_id',
+      'field' => 'library_id',
+    ),
+    'feature' => array(
+      'left_table' => 'library_feature',
+      'left_field' => 'library_id',
+      'field' => 'library_id',
+    ),
+  );
+
+  // Describe the joins with the library_feature table
+  $data['library_feature']['table']['join'] = array(
+    'feature' => array(
+      'left_field' => 'feature_id',
+      'field' => 'feature_id',
+    ),
+  );
 	
 	// Table Field Definitions----------------------
 	// Field: library_id (primary key)
@@ -75,7 +71,9 @@ function retrieve_library_views_data() {
 	  ),
 	);
 	
-	// Field: Node ID (foreign key)
+  // Calculated Field: Node ID
+  //  use custom field handler to query drupal for the node ID
+  //  this is only needed if chado is in a separate database from drupal
 	$data['library']['nid'] = array(
 	  'title' => 'Node ID',
 	  'help' => 'The node ID for the current library',
@@ -84,6 +82,12 @@ function retrieve_library_views_data() {
 	  ),
 	);
 	
+	// Field: organism_id (forgeign key)
+  //  join between organism table and this one in tripal_organism/views/organism.views.inc
+	
+	// Field: type_id (forgeign key)
+  //  join between cvterm table and this one in tripal_cv/views/cvterm.views.inc
+  
 	// Field: Name (varchar 255)
 	$data['library']['name'] = array(
 	  'title' => 'Name',