Browse Source

Checking in updates to example module

Stephen Ficklin 11 years ago
parent
commit
d4636b4fc1

+ 9 - 16
tripal_analysis/tripal_analysis.install

@@ -142,11 +142,12 @@ function tripal_get_cv_select_options() {
  * Retrieves a chado controlled vocabulary term variable
  *
  * @param $identifier
- *   An array with the key stating what the identifier is. Supported keys (only on of the
- *   following unique keys is required):
+ *   An array with the key stating what the identifier is. Supported keys (only one of the
+ *   following unique keys are required):
  *    - cvterm_id: the chado cv.cvterm_id primary key
- *    - name: the chado cv.name field (assume unique)
+ *    - name: the chado cvterm.name field (assume unique)
  *   There are also some specially handled keys. They are:
+ *    - cv_id:  an integer indicating the cv_id or an array with 'name' => the name of the cv.
  *    - synonym: an array with 'name' => the name of the synonym of the cvterm you want
  *        returned; 'cv_id' => the cv_id of the synonym; 'cv_name' => the name of the cv
  *        of the synonym
@@ -178,26 +179,18 @@ function tripal_get_cvterm($identifiers, $options = array()) {
 
   // Error Checking of parameters
   if (!is_array($identifiers)) {
-    tripal_report_error(
-      'tripal_cv_api',
-      TRIPAL_ERROR,
+    tripal_report_error('tripal_cv_api', TRIPAL_ERROR,
       "tripal_get_cvterm: The identifier passed in is expected to be an array with the key
         matching a column name in the cvterm table (ie: cvterm_id or name). You passed in %identifier.",
-      array(
-        '%identifier'=> print_r($identifiers, TRUE)
-      )
+      array('%identifier'=> print_r($identifiers, TRUE))
     );
   }
   elseif (empty($identifiers)) {
-    tripal_report_error(
-      'tripal_cv_api',
-      TRIPAL_ERROR,
+    tripal_report_error('tripal_cv_api', TRIPAL_ERROR,
       "tripal_get_cvterm: You did not pass in anything to identify the cvterm you want. The identifier
         is expected to be an array with the key matching a column name in the cvterm table
         (ie: cvterm_id or name). You passed in %identifier.",
-      array(
-        '%identifier'=> print_r($identifiers, TRUE)
-      )
+      array('%identifier'=> print_r($identifiers, TRUE))
     );
   }
 
@@ -437,7 +430,7 @@ function tripal_insert_cv($name, $definition) {
  *
  * @ingroup tripal_cv_api
  */
-function tripal_insert_cvterm($term, $options) {
+function tripal_insert_cvterm($term, $options = array()) {
 
   // Set Defaults
   if (isset($term['cv_name'])) {

+ 54 - 12
tripal_example/includes/tripal_example.chado_node.inc

@@ -139,15 +139,24 @@ function chado_example_form($node, &$form_state) {
 
 
   // SET FORM DEFAULTS
-  //---------------------------------------------
+  //---------------------------------------------  
+  $example      = null;   // holds the example object record
+  $example_id   = null;   // when editing an example record we'll have an example_id
+  
+  // initialize the defaults for the form fields
+  $uniquename   = '';
+  $example_type = '';
+  $organism_id  = '';
+  $description  = '';
   
   // if we are editing an existing node then the 'example' record from Chado
   // is already part of the node, so we set the defaults from that object
   if (property_exists($node, 'example')) {
     $example = $node->example;
-    $example = chado_expand_var($example, 'field', 'example.residues');
     $example_id   = $example->example_id;
     $uniquename   = $example->uniquename;
+    $description  = $example->description;
+    $organism_id  = $example->organism_id;
 
     // keep track of the example id
     $form['example_id'] = array(
@@ -160,19 +169,21 @@ function chado_example_form($node, &$form_state) {
   if (array_key_exists('values', $form_state)) {
      $uniquename   = $form_state['values']['uniquename'];
      $example_type = $form_state['values']['example_type'];
-
+     $description  = $form_state['values']['description'];
+     $organism_id  = $form_state['values']['organism_id'];
   }
   // if we are re building the form from after submission (from ajax call) then
   // the values are in the $form_state['input'] array
   if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
      $uniquename   = $form_state['input']['uniquename'];
      $example_type = $form_state['input']['example_type'];
+     $organism_id  = $form_state['input']['organism_id'];
   }
     
   
   // FORM ELEMENTS 
   //---------------------------------------------
-  $form['uniquename']= array(
+  $form['uniquename'] = array(
     '#type' => 'textfield',
     '#title' => t('Unique Name'),
     '#required' => TRUE,
@@ -186,9 +197,9 @@ function chado_example_form($node, &$form_state) {
   $type_cv = tripal_get_default_cv('example', 'type_id');
   $cv_id = $type_cv->cv_id;
   $form['example_type'] = array(
-    '#title'       => t('Feature Type'),
+    '#title'       => t('Example Type'),
     '#type'        => 'textfield',
-    '#description' => t("Choose the example type."),
+    '#description' => t("Choose the example type (e.g. Test Type)."),
     '#required'    => TRUE,
     '#default_value' => $example_type,
     '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
@@ -204,6 +215,14 @@ function chado_example_form($node, &$form_state) {
     '#default_value' => $organism_id,
     '#options'     => $organisms,
   );
+  
+  $form['description'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Description'),
+    '#required' => TRUE,
+    '#default_value' => $description,
+    '#description' => t('Enter a description for this example.'),
+  );
 
   // PROPERTIES FORM
   //---------------------------------------------
@@ -284,19 +303,38 @@ function chado_example_validate($node, $form, &$form_state) {
 
   // be sure to always trim text fields
   $node->uniquename   = trim($node->uniquename);
+  $node->description  = trim($node->description);
   
   // Validating for an update. If the 'nid' property is present in the node then
   // this is an update and validation can be different for updates
   if (property_exists($node, 'nid')) {
 
-    // if there is a problem with a field then you can set an error on the form
-    form_set_error('uniquename', t("example update cannot proceed. The example name '$node->uniquename' is not unique for this organism. Please provide a unique name for this example."));
+    // make sure the feature type is an allowed term
+    $type_cv = tripal_get_default_cv('example', 'type_id');
+    $type = tripal_get_cvterm(array(
+      'name' => $node->example_type,
+      'cv_id' => $type_cv->cv_id,
+    ));
+    if (!$type) {
+      form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
+    }
+    
+    // TODO: also we should check that the unique constraint is not invalidated by
+    // changing either the type_id, organism_id or uniquename. 
   }
   // Validating for an insert
   else {
-
-    // if there is a problem with a field then you can set an error on the form
-    form_set_error('uniquename', t("example insert cannot proceed. The example name '$node->uniquename' already exists for this organism. Please provide a unique name for this example."));
+    // make sure the feature type is an allowed term
+    $type_cv = tripal_get_default_cv('example', 'type_id');
+    $type = tripal_get_cvterm(array(
+      'name' => $node->example_type,
+      'cv_id' => $type_cv->cv_id,
+    ));
+    if (!$type) {
+      form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
+    }
+    
+    // TODO: also we should check that the unique constraint doesn't already exist
   }
 }
 
@@ -318,6 +356,7 @@ function chado_example_validate($node, $form, &$form_state) {
 function chado_example_insert($node) {
   // be sure to always trim text fields
   $node->uniquename   = trim($node->uniquename);
+  $node->description  = trim($node->description);
 
   // if there is an example_id in the $node object then this must be a sync so
   // we can skip adding the example as it is already there, although
@@ -327,7 +366,9 @@ function chado_example_insert($node) {
     // perform the insert using the tripal_core_chado_insert function();
     $values = array(
       'uniquename' => $node->uniquename,
-      'residues' => $residues,
+      'description' => $node->description,
+      'type_id' => $node->example_type,
+      'organism_id' => $node->organism_id,
     );
     $example = chado_select_record('example', array('*'), $values);
     if (!$example) {
@@ -401,6 +442,7 @@ function chado_example_insert($node) {
 function chado_example_update($node) {
   // be sure to always trim text fields
   $node->uniquename   = trim($node->uniquename);
+  $node->description  = trim($node->description);
 
   // use the chado_update_record() function to update the record
   $match = array(

+ 361 - 3
tripal_example/tripal_example.install

@@ -98,6 +98,10 @@ function tripal_example_install() {
   // then set it as a default vocabulary (see below).
   tripal_example_add_cvs();
   
+  
+  // add any controlled vocabulary terms
+  tripal_example_add_cvterms();
+  
   // EXPLANATION: Many tables in Chado have a 'type_id' column which allows for
   // association of controlled vocabulries to describe the record.   Chado
   // places no restrictions on which vocabularies can be used, but Tripal can
@@ -105,11 +109,16 @@ function tripal_example_install() {
   // example, the feature.type_id column will typically use the Sequence Ontology
   // In that case, we can use the tripal_set_default_cv() function to specify
   // the Sequence Ontology (sequence) as the default vocabulary.  
-  tripal_set_default_cv('example', 'type_id', 'sequence');
+  tripal_set_default_cv('example', 'type_id', 'example_type');
   tripal_set_default_cv('exampleprop', 'type_id', 'example_property');
   tripal_set_default_cv('example_relationship', 'type_id', 'example_relationship');
+  
+  // add any custom tables. For this case we will add an 'example' table to the
+  // chado schema
+  tripal_example_add_custom_tables();
 }
 
+
 /**
  * Implements hook_uninstall().
  * 
@@ -196,15 +205,364 @@ function tripal_example_add_mviews() {
 /**
  * Add cvs related to publications
  *
- * @ingroup tripal_pub
+ * @ingroup tripal_example
+ */
+function tripal_example_add_dbs() {
+  // EXPLANATION: use the tripal_db_add_db() function to add any
+  // external databases needed by your module.  If the database already
+  // exists then the function will gracefully return.
+  
+  tripal_db_add_db(
+    'example_db',
+    'An example database.'
+  );
+}
+/**
+ * Add cvs related to publications
+ *
+ * @ingroup tripal_example
  */
 function tripal_example_add_cvs() {
 
   // EXPLANATION: use the tripal_cv_add_cv() function to add any 
   // controlled vocabularies needed by your module.  If the vocabulary already
-  // exists then the function will gracefully return.  
+  // exists then the function will gracefully return.  Chado convensions
+  // use a singluar name for CV names (not plural)/
+  
+  tripal_cv_add_cv(
+    'example_property',
+    'Contains property terms for examples.'
+  );
+  
+  tripal_cv_add_cv(
+    'example_type',
+    'Contains terms describing types of examples.'
+  );
+  
+  tripal_cv_add_cv(
+   'example_relationship',
+   'Contains terms for describing relationship types between examples.'
+  );
+  
+}
+
+/**
+ * Adds controlled vocabulary terms needed by this module.
+ *
+ * @ingroup tripal_example
+ */
+function tripal_example_add_cvterms() {
+  
+  // EXPLANATION:  for our test module to work we need to add some terms to our example_type
+  // controlled vocabulary.  Ideally we should have a full OBO file for loading
+  // but sometimes we just have a small list that won't really change so
+  // we can add those terms here.
+  tripal_insert_cvterm(array(
+    'id'         => 'test',         // the term accession
+    'name'       => 'Test type',    // the human readable term name
+    'cv_name'    => 'example_type', // the CV name this term belongs to.
+    'definition' => 'A test type for the example modlue.',
+    'db_name'    => 'example_db',   // the database in which the term is found.
+  ));
+}
+
+/**
+ * Add custom tables to Chado that are required by this module
+ * 
+ * @ingroup tripal_example
+ */
+function tripal_example_add_custom_tables() {
+  
+  // EXPLANATION: for this example module we will create a set of example tables
+  // that mimic Chado tables. These tables are:
+  //
+  //   1) example     (for storing the primary example records)
+  //   2) exampleprop (for sorting properties about the example)
+  //   3) example_relationship (for storing relationships about examples)
+  //   4) example_dbxref (for storing cross-references about an example)
+  //
+  // To make the code easier to read, each table is created by a separte 
+  // function called here:
+  
+  tripal_example_add_example_table();
+  tripal_example_add_exampleprop_table();
+  tripal_example_add_example_relationship_table();
+  tripal_example_add_example_dbxref_table();
+}
+/**
+ * Adds the 'example' custom table to Chado.
+ * 
+ * @ingroup tripal_example
+ */
+function tripal_example_add_example_table() {
+  // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
+  // add the table using the chado_create_custom_table() function.
+  $schema = array(
+    'table' => 'example',
+    'fields' => array(
+      'example_id' => array(
+        'type' => 'serial',
+        'not null' => true,
+      ),
+      'uniquename' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+      ),
+      'type_id' => array(
+        'type' => 'int',
+        'not null' => true,
+      ),
+      'organism_id' => array(
+        'type' => 'int',
+        'not null' => true,
+      ),
+      'description' => array(
+        'type' => 'text',
+      ),
+    ),
+    'primary key' => array(
+      0 => 'example_id',
+    ),
+    'unique keys' => array(
+      'example_uq1' => array(
+        0 => 'uniquename',
+        1 => 'type_id',
+        2 => 'organism_id',
+      ),
+    ),
+    'indexes' => array(
+      'example_idx1' => array(
+        0 => 'example_id',
+      ),
+      'example_idx2' => array(
+        0 => 'uniquename',
+      ),
+    ),
+    'foreign keys' => array(
+      'cvterm' => array(
+        'table' => 'cvterm',
+        'columns' => array(
+          'type_id' => 'cvterm_id',
+        ),
+      ),
+      'organism' => array(
+        'table' => 'organism',
+        'columns' => array(
+          'organism_id' => 'organism_id',
+        ),
+      ),
+    ),
+  );
+  chado_create_custom_table('example', $schema, TRUE);
+}
+/**
+ * Adds the 'example_relationship' custom table to Chado.
+ * 
+ * @ingroup tripal_example
+ */
+function tripal_example_add_exampleprop_table() {
+  // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
+  // add the table using the chado_create_custom_table() function.
+  
+  // Add the exampleprop table
+  $schema =  array(
+    'table' => 'exampleprop',
+    'fields' => array(
+      'exampleprop_id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+      ),
+      'example_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'type_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'value' => array(
+        'type' => 'text',
+        'not null' => FALSE,
+      ),
+      'rank' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+    ),
+    'primary key' => array(
+      0 => 'exampleprop_id',
+    ),
+    'unique keys' => array(
+      'example_id_type_id_rank' => array(
+        0 => 'example_id',
+        1 => 'type_id',
+        2 => 'rank',
+      ),
+    ),
+    'foreign keys' => array(
+      'cvterm' => array(
+        'table' => 'cvterm',
+        'columns' => array(
+          'type_id' => 'cvterm_id',
+        ),
+      ),
+      'example' => array(
+        'table' => 'example',
+        'columns' => array(
+          'example_id' => 'example_id',
+        ),
+      ),
+    ),
+  );
+  chado_create_custom_table('exampleprop', $schema, TRUE);
 }
 
+/**
+ * Adds the 'example_relationship' custom table to Chado.
+ * 
+ * @ingroup tripal_example
+ */
+function tripal_example_add_example_relationship_table() {
+  // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
+  // add the table using the chado_create_custom_table() function.
+  
+  $schema =  array(
+    'table' => 'example_relationship',
+    'fields' => array(
+      'example_relationship_id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+      ),
+      'subject_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'object_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'type_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'value' => array(
+        'type' => 'text',
+        'not null' => FALSE,
+      ),
+      'rank' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'primary key' => array(
+      0 => 'example_relationship_id',
+    ),
+    'unique keys' => array(
+      'example_relationship_c1' => array(
+        0 => 'subject_id',
+        1 => 'object_id',
+        2 => 'type_id',
+        3 => 'rank',
+      ),
+    ),
+    'indexes' => array(
+      'example_relationship_idx1' => array(
+        0 => 'subject_id',
+      ),
+      'example_relationship_idx2' => array(
+        0 => 'object_id',
+      ),
+      'example_relationship_idx3' => array(
+        0 => 'type_id',
+      ),
+    ),
+    'foreign keys' => array(
+      'cvterm' => array(
+        'table' => 'cvterm',
+        'columns' => array(
+          'type_id' => 'cvterm_id',
+        ),
+      ),
+      'example' => array(
+        'table' => 'example',
+        'columns' => array(
+          'subject_id' => 'example_id',
+          'object_id' => 'example_id',
+        ),
+      ),
+    ),
+  );
+  chado_create_custom_table('example_relationship', $schema, TRUE);
+}
+
+/**
+ * Adds the 'example_dbxref' custom table to Chado.
+ * 
+ * @ingroup tripal_example
+ */
+function tripal_example_add_example_dbxref_table() {
+  
+  // EXPLANATION: use the Drupal Schema API to describe the custom table. Then
+  // add the table using the chado_create_custom_table() function.
+
+  $description =  array(
+    'table' => 'example_dbxref',
+    'fields' => array(
+      'example_dbxref_id' => array(
+        'type' => 'serial',
+        'not null' => TRUE,
+      ),
+      'example_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'dbxref_id' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+      ),
+      'is_current' => array(
+        'type' => 'int',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'default' => 1,
+      ),
+    ),
+    'primary key' => array(
+      0 => 'example_dbxref_id',
+    ),
+    'unique keys' => array(
+      'example_dbxref_unq1' => array(
+        0 => 'example_id',
+        1 => 'dbxref_id',
+      ),
+    ),
+    'indexes' => array(
+      'example_dbxref_idx1' => array(
+        0 => 'example_id',
+      ),
+      'example_dbxref_idx2' => array(
+        0 => 'dbxref_id',
+      ),
+    ),
+    'foreign keys' => array(
+      'dbxref' => array(
+        'table' => 'dbxref',
+        'columns' => array(
+          'dbxref_id' => 'dbxref_id',
+        ),
+      ),
+      'example' => array(
+        'table' => 'example',
+        'columns' => array(
+          'example_id' => 'example_id',
+        ),
+      ),
+    ),
+  );
+  chado_create_custom_table('example_dbxref', $schema, TRUE);
+}
 /**
  * This is the required update for tripal_example.
  */

+ 8 - 0
tripal_example/tripal_example.views.inc

@@ -1 +1,9 @@
 <?php
+/**
+ *  @file
+ *  This file contains the basic functions for views integration of
+ *  chado/tripal example tables
+ */
+
+// EXPLANATION: see another tripal module (e.g. tripal_feature) for example
+// functions that can be used in this file.

+ 24 - 0
tripal_example/tripal_example.views_default.inc

@@ -1 +1,25 @@
 <?php
+/**
+ * @file
+* Describe default example views
+*/
+
+/**
+ * Implements hook_views_default_views().
+*
+* @ingroup tripal_example
+*/
+function tripal_example_views_default_views() {
+  $views = array();
+  
+  // EXPLANATION:  use this function to programmatically define any default
+  // Drupal views that your module will provide automatically.  Typically
+  // there are two default views: 1) one for the administrator to quickly
+  // find data managed by your module, or 2) one for users to be able to
+  // search for public data.  
+  //
+  // See another Tripal module such as tripal_feature for an example 
+  // of how to setup a default views.
+  
+  return $views;
+}

+ 0 - 0
tripal_featuremap/tripal_featuremap.module