Browse Source

Merge branch '6.x-1.x' of git.drupal.org:sandbox/spficklin/1337878 into 6.x-1.x

Lacey Sanderson 12 years ago
parent
commit
2b713602f8

+ 118 - 19
tripal_core/api/tripal_core.api.inc

@@ -121,6 +121,18 @@ require_once "tripal_core.schema_v1.11.api.inc";
  * @ingroup tripal_chado_api
  */
 function tripal_core_chado_insert($table, $values, $options = array()) {
+  
+  if (!is_array($values)) {
+    watchdog('tripal_core', 'Cannot pass non array as values for inserting.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;  
+  }
+  if (count($values)==0) {
+    watchdog('tripal_core', 'Cannot pass an empty array as values for inserting.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;
+  }
+  
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -462,6 +474,28 @@ function tripal_core_chado_insert($table, $values, $options = array()) {
  */
 function tripal_core_chado_update($table, $match, $values, $options = NULL) {
 
+  if (!is_array($values)) {
+    watchdog('tripal_core', 'Cannot pass non array as values for updating.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;  
+  }
+  if (count($values)==0) {
+    watchdog('tripal_core', 'Cannot pass an empty array as values for updating.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;
+  }
+  
+  if (!is_array($match)) {
+    watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;  
+  }
+  if (count($match)==0) {
+    watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;
+  }
+  
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -840,6 +874,18 @@ function tripal_core_chado_update($table, $match, $values, $options = NULL) {
  * @ingroup tripal_chado_api
  */
 function tripal_core_chado_delete($table, $match, $options = NULL) {
+  
+  if (!is_array($match)) {
+    watchdog('tripal_core', 'Cannot pass non array as values for matching.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;  
+  }
+  if (count($match)==0) {
+    watchdog('tripal_core', 'Cannot pass an empty array as values for matching.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;
+  }
+  
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -1123,6 +1169,28 @@ function tripal_core_chado_delete($table, $match, $options = NULL) {
  */
 function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
 
+  if (!is_array($values)) {
+    watchdog('tripal_core', 'Cannot pass non array as values for selecting.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;  
+  }
+  if (count($values)==0) {
+    watchdog('tripal_core', 'Cannot pass an empty array as values for selecting.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;
+  }
+  
+  if (!is_array($columns)) {
+    watchdog('tripal_core', 'Cannot pass non array as columns for selecting.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;  
+  }
+  if (count($columns)==0) {
+    watchdog('tripal_core', 'Cannot pass an empty array as columns for selecting.', array(), 
+      WATCHDOG_ERROR);
+    return FALSE;
+  }
+  
   // set defaults for options. If we don't set defaults then
   // we get memory leaks when we try to access the elements
   if (!is_array($options)) {
@@ -1346,7 +1414,7 @@ function tripal_core_chado_select($table, $columns, $values, $options = NULL) {
   }
 
   // now build the SQL and prepared SQL statements. We may not use
-  // the prepared statement if it wasn't requested in the options of if the
+  // the prepared statement if it wasn't requested in the options or if the
   // argument in a where statement has multiple values.
   if (empty($where)) {
     // sometimes want to select everything
@@ -1644,15 +1712,25 @@ function tripal_core_chado_get_foreign_key($table_desc, $field, $values, $option
  *     behavior is to return a single record if only one record exists or to return
  *     an array if multiple records exist.
  *  - include_fk:
- *     an array of FK relationships to fallow. By default, the
+ *     an array of FK relationships to follow. By default, the
  *     tripal_core_chado_select function will follow all FK relationships but this
- *     may generate more queries then is desired.  Provide an array specifying the
- *     fields to include.  For example, if including the cvterm, cv and dbxref records
- *     for a property the following array would work:
- *        array('type_id' => array('cv_id' => 1, 'dbxref_id' => 1)).
- *     Typicall, to expadn a property, this function would also include the db record
- *     through the db_id foreign key with the dbxref and db tables, but becuase
- *     it is not included in the array it will not be included in the results.
+ *     may generate more queries then is desired slowing down this function call when
+ *     there are lots of FK relationships to follow.  Provide an array specifying the
+ *     fields to include.  For example, if expanding a property table (e.g. featureprop)
+ *     and you want the CV and accession but do not want the DB the following
+ *     array would work:
+ *     
+ *        $table_options =  array(
+ *          'include_fk' => array(
+ *            'type_id' => array(
+ *              'cv_id' => 1, 
+ *              'dbxref_id' => 1,
+ *            )
+ *          )
+ *        );
+ *        
+ *     The above array will expand the 'type_id' of the property table but only 
+ *     further expand the cv_id and the dbxref_id and will go no further.
  * @return
  *   Either an object (if only one record was selected from the base table)
  *   or an array of objects (if more than one record was selected from the base table).
@@ -1936,14 +2014,35 @@ function tripal_core_generate_chado_var($table, $values, $base_options = array()
  * @param $to_expand
  *   The name of the field/table/node to be expanded
  * @param $table_options
- *   An array containing options for the base table.  For example, an
- *   option of 'order_by' may be used to sort results in the base table
- *   if more than one are returned.  The options must be compatible with
- *   the options accepted by the tripal_core_chado_select() function.
- *   Additionally,  The option 'return_array' can be provided to force
- *   the function to expand tables as an array. Default behavior is to expand
- *   a table as single record if only one record exists or to expand as an array if
- *   multiple records exist.
+ *   - order_by:
+ *     An array containing options for the base table.  For example, an
+ *     option of 'order_by' may be used to sort results in the base table
+ *     if more than one are returned.  The options must be compatible with
+ *     the options accepted by the tripal_core_chado_select() function.
+ *   - return_array: 
+ *     Additionally,  The option 'return_array' can be provided to force
+ *     the function to expand tables as an array. Default behavior is to expand
+ *     a table as single record if only one record exists or to expand as an array if
+ *     multiple records exist.
+ *   - include_fk:
+ *     an array of FK relationships to follow. By default, the
+ *     tripal_core_chado_select function will follow all FK relationships but this
+ *     may generate more queries then is desired slowing down this function call when
+ *     there are lots of FK relationships to follow.  Provide an array specifying the
+ *     fields to include.  For example, if expanding a property table (e.g. featureprop)
+ *     and you want the CV and accession but do not want the DB the following
+ *     array would work:
+ *        $table_options =  array(
+ *          'include_fk' => array(
+ *            'type_id' => array(
+ *              'cv_id' => 1, 
+ *              'dbxref_id' => 1,
+ *            )
+ *          )
+ *        );
+ *        
+ *     The above array will expand the 'type_id' of the property table but only 
+ *     further expand the cv_id and the dbxref_id and will go no further.
  * @return
  *   A chado object supplemented with the field/table/node requested to be expanded.
  *   If the type is a table and it has already been expanded no changes is made to the
@@ -3396,8 +3495,8 @@ function tripal_core_get_chado_version($exact = FALSE, $warn_if_unsupported = FA
   if (strcmp($exact_version, '1.11 or older') == 0) {
     $exact_version = "1.11";
     if ($warn_if_unsupported) {
-      drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.1.  If you are certain this is v1.1
-         of if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.1 or later"),
+      drupal_set_message(t("WARNING: Tripal does not fully support Chado version less than v1.11.  If you are certain this is v1.11
+         or if Chado was installed using an earlier version of Tripal then all is well. If not please upgrade to v1.11 or later"),
          'warning');
     }
   }

+ 8 - 2
tripal_core/includes/chado_install.php

@@ -96,6 +96,7 @@ function tripal_core_install_chado($action) {
       }
       else {
         print "Installation (Step 1 of 2) Problems!  Please check output above for errors.\n";
+        exit;
       }
       $success = tripal_core_install_sql($init_file);
       if ($success) {
@@ -103,6 +104,7 @@ function tripal_core_install_chado($action) {
       }
       else {
         print "Installation (Step 2 of 2) Problems!  Please check output above for errors.\n";
+        exit;
       }
       chado_query($vsql,'1.2'); # set the version
     }
@@ -120,6 +122,7 @@ function tripal_core_install_chado($action) {
     }
     else {
       print "Upgrade (Step 1 of 2) problems!  Please check output above for errors.\n";
+      exit;
     }
     $success = tripal_core_install_sql($init_file);
     if ($success) {
@@ -127,6 +130,7 @@ function tripal_core_install_chado($action) {
     }
     else {
       print "Upgrade (Step 2 of 2) problems!  Please check output above for errors.\n";
+      exit;
     }
     chado_query($vsql,'1.2'); # set the version
   }
@@ -140,6 +144,7 @@ function tripal_core_install_chado($action) {
       }
       else {
         print "Installation (Step 1 of 2) Problems!  Please check output above for errors.\n";
+        exit;
       }
       $success = tripal_core_install_sql($init_file);
       if ($success) {
@@ -147,6 +152,7 @@ function tripal_core_install_chado($action) {
       }
       else {
         print "Installation (Step 2 of 2) Problems!  Please check output above for errors.\n";
+        exit;
       }
     }
     else {
@@ -365,9 +371,10 @@ function tripal_core_install_sql($sql_file) {
       }
       if (!$result) {
         $error  = pg_last_error();
-        print "FAILED!!\nError Message:\nSQL $i, $in_string: $query\n$error\n";        
+        print "FAILED. Line  $i, $in_string\n$error:\n$query\n\n";        
         tripal_core_chado_install_done();
         $success = 0;
+        return $success;
       }
       $query = '';
     }
@@ -385,5 +392,4 @@ function tripal_core_chado_install_done() {
 
   // return the search path to normal
   db_query("set search_path to public");
-
 }

+ 2 - 2
tripal_core/tripal_core.module

@@ -110,7 +110,7 @@ function tripal_core_menu() {
     'description' => 'Installs the Chado database tables, views, etc., inside the current Drupal database',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_core_chado_load_form'),
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('install chado'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -238,7 +238,7 @@ function tripal_core_menu() {
  * @ingroup tripal_core
  */
 function tripal_core_perm() {
-  return array();
+  return array('install chado');
 }
 
 /**

+ 55 - 2
tripal_cv/api/tripal_cv.api.inc

@@ -123,7 +123,7 @@ function tripal_cv_get_cv_by_name($name) {
  * @see tripal_core_chado_generate_vars()
  *
  * @param $cv_id
- *   The unique identifier for the cv retrieve
+ *   The unique identifier for the cv to retrieve
  *
  * @return
  *   An object describing the cv
@@ -136,10 +136,36 @@ function tripal_cv_get_cv_by_id($cv_id) {
 
   return $r;
 }
+/**
+ * Retrieve the cv id for the specified CV by name
+ *
+ * NOTE: This function is deprecated.
+ * @see tripal_core_chado_generate_vars()
+ *
+ * @param $cv_name
+ *   The unique name for the cv to retrieve
+ *
+ * @return
+ *   The numeric cv ID
+ *
+ * @ingroup tripal_cv_api
+ */
+function tripal_cv_get_cv_id($cv_name) {
+
+  $sql = "
+    SELECT cv_id FROM {cv} WHERE name = '%s'
+  ";
+  $cv = db_fetch_object(chado_query($sql, $cv_name));
+
+  return $cv->cv_id;
+}
 
 /**
- * Create an options array to be used in a form element which provides a list of all chado cvs
+ * Create an options array to be used in a form element which provides a 
+ * list of all chado cvs
  *
+ * NOTE: This function is deprecated as of Tripal v1.0
+ * 
  * @return
  *   An array(cv_id => name) for each cv in the chado cv table
  *
@@ -238,7 +264,34 @@ function tripal_cv_get_cvterm_options($cv_id = 0) {
 
 }
 
+/**
+ * Updates the cvtermpath table of Chado for the specified CV.
+ *
+ * @param $cv_id
+ *   The chado cv_id;
+ * @param $job_id
+ *   This function is intended to be used with the Tripal Jobs API.
+ *   When this function is called as a job the $job_id is automatically
+ *   passed to this function.
+ * @return
+ *   TRUE on success FALSE on failure
+ *
+ * @ingroup tripal_cv_api
+ */
+function tripal_cv_update_cvtermpath($cvid, $job_id = NULL) {
+  // TODO: need better error checking in this function
+
+  // first get the controlled vocabulary name:
+  $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
+  print "\nUpdating cvtermpath for $cv->name...\n";
+
+  // now fill the cvtermpath table
+  // @coder-ignore: using a function rather then tablename therefore table prefixing doesn't apply
+  $sql = "SELECT * FROM fill_cvtermpath('%s')";
+  $success = chado_query($sql, $cv->name); 
 
+  return TRUE;
+}
 
 /**
  * Adds a controlled vocabular to the CV table of Chado.

+ 171 - 2
tripal_cv/includes/obo_loader.inc

@@ -1,5 +1,171 @@
 <?php
 
+/**
+ * Purpose: Provides the form to load an already existing controlled
+ *  Vocabulary into chado
+ *
+  * @ingroup tripal_obo_loader
+ */
+function tripal_cv_obo_form(&$form_state = NULL) {
+
+  // get a list of db from chado for user to choose
+  $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY name";
+  $results = db_query($sql);
+
+  $obos = array();
+  $obos[] = '';
+  while ($obo = db_fetch_object($results)) {
+//    $obos[$obo->obo_id] = "$obo->name  | $obo->path";
+    $obos[$obo->obo_id] = $obo->name;
+  }
+
+  $form['obo_existing'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Use a Saved Ontology OBO Reference')
+  );
+
+  $form['obo_new'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Use a New Ontology OBO Reference')
+  );
+
+  $form['obo_existing']['existing_instructions']= array(
+    '#value' => t('The Ontology OBO files listed in the drop down below have been automatically added upon
+                   installation of the Tripal CV module or were added from a previous upload.  Select
+                   an OBO, then click the submit button to load the vocabulary into the database.  If the
+                   vocabularies already exist then the ontology will be updated.'),
+    '#weight'        => -1
+  );
+
+  $form['obo_existing']['obo_id'] = array(
+    '#title' => t('Ontology OBO File Reference'),
+    '#type' => 'select',
+    '#options' => $obos,
+    '#weight'        => 0
+  );
+
+  $form['obo_new']['path_instructions']= array(
+    '#value' => t('Provide the name and path for the OBO file.  If the vocabulary OBO file
+                   is stored local to the server provide a file name. If the vocabulry is stored remotely,
+                   provide a URL.  Only provide a URL or a local file, not both.'),
+    '#weight'        => 0
+  );
+
+  $form['obo_new']['obo_name']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('New Vocabulary Name'),
+    '#description'   => t('Please provide a name for this vocabulary.  After upload, this name will appear in the drop down
+                           list above for use again later.'),
+    '#weight'        => 1
+  );
+
+  $form['obo_new']['obo_url']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('Remote URL'),
+    '#description'   => t('Please enter a URL for the online OBO file.  The file will be downloaded and parsed.
+                           (e.g. http://www.obofoundry.org/ro/ro.obo'),
+    '#default_value' => $default_desc,
+    '#weight'        => 2
+  );
+
+  $form['obo_new']['obo_file']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('Local File'),
+    '#description'   => t('Please enter the full system path for an OBO definition file, or a path within the Drupal
+                           installation (e.g. /sites/default/files/xyz.obo).  The path must be accessible to the
+                           server on which this Drupal instance is running.'),
+    '#default_value' => $default_desc,
+    '#weight'        => 3
+  );
+
+  $form['submit'] = array(
+    '#type'         => 'submit',
+    '#value'        => t('Submit'),
+    '#weight'       => 5,
+    '#executes_submit_callback' => TRUE,
+  );
+
+  $form['#redirect'] = 'admin/tripal/tripal_cv/obo_loader';
+
+  return $form;
+}
+
+/**
+ * Purpose: The submit function for the load ontology form. It registers a
+ *   tripal job to run the obo_loader.php script
+ *
+   * @ingroup tripal_obo_loader
+ */
+function tripal_cv_obo_form_submit($form, &$form_state) {
+  global $user;
+
+  $obo_id =  $form_state['values']['obo_id'];
+  $obo_name =  $form_state['values']['obo_name'];
+  $obo_url =  $form_state['values']['obo_url'];
+  $obo_file  = $form_state['values']['obo_file'];
+
+  $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = %d";
+  $obo = db_fetch_object(db_query($sql, $obo_id));
+
+  if ($obo_id) {
+    $args = array($obo_id);
+    tripal_add_job("Load OBO $obo->name", 'tripal_cv',
+       "tripal_cv_load_obo_v1_2_id", $args, $user->uid);
+  }
+  else {
+    if ($obo_url) {
+      $args = array($obo_name, $obo_url);
+      tripal_add_job("Load OBO $obo_name", 'tripal_cv',
+        "tripal_cv_load_obo_v1_2_url", $args, $user->uid);
+    }
+    elseif ($obo_file) {
+      $args = array($obo_name, $obo_file);
+      tripal_add_job("Load OBO $obo_name", 'tripal_cv',
+        "tripal_cv_load_obo_v1_2_file", $args, $user->uid);
+    }
+  }
+}
+
+/**
+ * Form for re-doing the cvterm path
+ *
+ * @ingroup tripal_cv
+ */
+function tripal_cv_cvtermpath_form() {
+
+  // get a list of db from chado for user to choose
+  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
+  $results = chado_query($sql);
+
+  $cvs = array();
+  $cvs[] = '';
+  while ($cv = db_fetch_object($results)) {
+    $cvs[$cv->cv_id] = $cv->name;
+  }
+
+  $form['cvid'] = array(
+    '#title' => t('Controlled Vocabulary/Ontology Name'),
+    '#type' => 'select',
+    '#options' => $cvs,
+    '#description' => t('The Chado cvtermpath is a database table that provides lineage for ontology terms 
+      and is useful for quickly finding any ancestor parent of a term.  This table must be populated for each
+      ontology.  Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
+  );
+
+  $form['description'] = array(
+    '#type' => 'item',
+    '#value' => t("Submit a job to update chado cvtermpath table."),
+    '#weight' => 1,
+  );
+
+  $form['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Update cvtermpath'),
+    '#weight' => 2,
+  );
+
+  return $form;
+}
 /**
  * @file
  * Tripal Ontology Loader
@@ -48,15 +214,18 @@ function tripal_cv_load_obo_v1_2_id($obo_id, $jobid = NULL) {
  */
 function tripal_cv_load_obo_v1_2_file($obo_name, $file, $jobid = NULL, $is_new = TRUE) {
   $newcvs = array();
-
+  
+  // TODO: need better error detection
+  
   tripal_cv_load_obo_v1_2($file, $jobid, $newcvs);
   if ($is_new) {
     tripal_cv_load_obo_add_ref($obo_name, $file);
   }
+  print "Ontology Sucessfully loaded!\n";
 
   // update the cvtermpath table
   tripal_cv_load_update_cvtermpath($newcvs, $jobid);
-  print "Ontology Sucessfully loaded!\n";
+  
 
 }
 

+ 1 - 254
tripal_cv/includes/tripal_cv_admin.inc

@@ -641,181 +641,6 @@ function tripal_cv_cvterm_callback() {
   );
 }
 
-///////////////////////////////
-// Ontology Loader
-//////////////////////////////
-
-/**
- * Purpose: Provides the form to load an already existing controlled
- *  Vocabulary into chado
- *
-  * @ingroup tripal_obo_loader
- */
-function tripal_cv_obo_form(&$form_state = NULL) {
-
-  // get a list of db from chado for user to choose
-  $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY obo_id";
-  $results = db_query($sql);
-
-  $obos = array();
-  $obos[] = '';
-  while ($obo = db_fetch_object($results)) {
-    $obos[$obo->obo_id] = "$obo->name  | $obo->path";
-  }
-
-  $form['obo_existing'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Use a Saved Ontology OBO Reference')
-  );
-
-  $form['obo_new'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Use a New Ontology OBO Reference')
-  );
-
-  $form['obo_existing']['existing_instructions']= array(
-    '#value' => t('The Ontology OBO files listed in the drop down below have been automatically added upon
-                   installation of the Tripal CV module or were added from a previous upload.  Select
-                   an OBO, then click the submit button to load the vocabulary into the database.  If the
-                   vocabularies already exist then the ontology will be updated.'),
-    '#weight'        => -1
-  );
-
-  $form['obo_existing']['obo_id'] = array(
-    '#title' => t('Ontology OBO File Reference'),
-    '#type' => 'select',
-    '#options' => $obos,
-    '#weight'        => 0
-  );
-
-  $form['obo_new']['path_instructions']= array(
-    '#value' => t('Provide the name and path for the OBO file.  If the vocabulary OBO file
-                   is stored local to the server provide a file name. If the vocabulry is stored remotely,
-                   provide a URL.  Only provide a URL or a local file, not both.'),
-    '#weight'        => 0
-  );
-
-  $form['obo_new']['obo_name']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('New Vocabulary Name'),
-    '#description'   => t('Please provide a name for this vocabulary.  After upload, this name will appear in the drop down
-                           list above for use again later.'),
-    '#weight'        => 1
-  );
-
-  $form['obo_new']['obo_url']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('Remote URL'),
-    '#description'   => t('Please enter a URL for the online OBO file.  The file will be downloaded and parsed.
-                           (e.g. http://www.obofoundry.org/ro/ro.obo'),
-    '#default_value' => $default_desc,
-    '#weight'        => 2
-  );
-
-  $form['obo_new']['obo_file']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('Local File'),
-    '#description'   => t('Please enter the full system path for an OBO definition file, or a path within the Drupal
-                           installation (e.g. /sites/default/files/xyz.obo).  The path must be accessible to the
-                           server on which this Drupal instance is running.'),
-    '#default_value' => $default_desc,
-    '#weight'        => 3
-  );
-
-  $form['submit'] = array(
-    '#type'         => 'submit',
-    '#value'        => t('Submit'),
-    '#weight'       => 5,
-    '#executes_submit_callback' => TRUE,
-  );
-
-  $form['#redirect'] = 'admin/tripal/tripal_cv/obo_loader';
-
-  return $form;
-}
-
-/**
- * Purpose: The submit function for the load ontology form. It registers a
- *   tripal job to run the obo_loader.php script
- *
-   * @ingroup tripal_obo_loader
- */
-function tripal_cv_obo_form_submit($form, &$form_state) {
-  global $user;
-
-  $obo_id =  $form_state['values']['obo_id'];
-  $obo_name =  $form_state['values']['obo_name'];
-  $obo_url =  $form_state['values']['obo_url'];
-  $obo_file  = $form_state['values']['obo_file'];
-
-  $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = %d";
-  $obo = db_fetch_object(db_query($sql, $obo_id));
-
-  if ($obo_id) {
-    $args = array($obo_id);
-    tripal_add_job("Load OBO $obo->name", 'tripal_cv',
-       "tripal_cv_load_obo_v1_2_id", $args, $user->uid);
-  }
-  else {
-    if ($obo_url) {
-      $args = array($obo_name, $obo_url);
-      tripal_add_job("Load OBO $obo_name", 'tripal_cv',
-        "tripal_cv_load_obo_v1_2_url", $args, $user->uid);
-    }
-    elseif ($obo_file) {
-      $args = array($obo_name, $obo_file);
-      tripal_add_job("Load OBO $obo_name", 'tripal_cv',
-        "tripal_cv_load_obo_v1_2_file", $args, $user->uid);
-    }
-  }
-
-}
-
-////////////////////////////////////
-// cvterm path management
-///////////////////////////////////
-
-/**
- * Form for re-doing the cvterm path
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_cvtermpath_form() {
-
-  // get a list of db from chado for user to choose
-  $sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
-  $results = chado_query($sql);
-
-  $cvs = array();
-  $cvs[] = '';
-  while ($cv = db_fetch_object($results)) {
-    $cvs[$cv->cv_id] = $cv->name;
-  }
-
-  $form['cvid'] = array(
-    '#title' => t('Controlled Vocabulary/Ontology Name'),
-    '#type' => 'select',
-    '#options' => $cvs,
-    '#description' => t('The Chado cvtermpath is a database table that provides lineage for ontology terms 
-      and is useful for quickly finding any ancestor parent of a term.  This table must be populated for each
-      ontology.  Select a controlled vocabulary for which you would like to upate the cvtermpath.'),
-  );
-
-  $form['description'] = array(
-    '#type' => 'item',
-    '#value' => t("Submit a job to update chado cvtermpath table."),
-    '#weight' => 1,
-  );
-
-  $form['button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Update cvtermpath'),
-    '#weight' => 2,
-  );
-
-  return $form;
-}
-
 /**
  * Cvterm path form submit
  *
@@ -835,82 +660,4 @@ function tripal_cv_cvtermpath_form_submit($form, &$form_state) {
     tripal_add_job("Update cvtermpath: $cv->name", 'tripal_cv',
        'tripal_cv_update_cvtermpath', $job_args, $user->uid);
   }
-}
-
-/**
- * Update the cvtermpath table
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_update_cvtermpath($cvid = NULL, $job_id = NULL) {
-
-  // first get the controlled vocabulary name:
-  $cv = db_fetch_object(chado_query("SELECT * FROM {cv} WHERE cv_id = %d", $cvid));
-  print "\nUpdating cvtermpath for $cv->name...\n";
-
-  // now fill the cvtermpath table
-  // @coder-ignore: using a function rather then tablename therefore table prefixing doesn't apply
-  $sql = "SELECT * FROM fill_cvtermpath('%s')";
-  chado_query($sql, $cv->name);
-
-  return;
-}
-
-//////////////////////////////////////
-// @section Miscellaneous
-// @todo check to see if these functions are still needed and/or if they
-//   should be moved to the api file
-//////////////////////////////////////
-
-/**
- * Get the CV ID based on name
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_get_cv_id($cv_name) {
-
-  $sql = "
-    SELECT cv_id FROM {cv} WHERE name = '%s'
-  ";
-  $cv = db_fetch_object(chado_query($sql, $cv_name));
-
-  return $cv->cv_id;
-}
-
-/**
- * Returns a themed cvterm definition table
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_cvterm_edit($cvterm_id) {
-
-  $sql = "
-    SELECT CVT.name as cvtermname, CVT.definition, CV.name as cvname
-    FROM {CVTerm} CVT
-      INNER JOIN CV on CVT.cv_id = CV.cv_id
-    WHERE CVT.cvterm_id = %d
-  ";
-  $cvterm = db_fetch_object(chado_query($sql, $cvterm_id));
-
-  return theme('tripal_cv_cvterm_edit', $cvterm);
-}
-
-/**
- * Does the actual themeing of the cvterm definition table
- *
- * @ingroup tripal_cv
- */
-function theme_tripal_cv_cvterm_edit(&$cvterm) {
-
-  $output = "
-    <div id=\"cvterm\">
-    <table>
-      <tr><th>Term</th><td>$cvterm->cvtermname</td></tr>
-      <tr><th>Vocabulary</th><td>$cvterm->cvname</td></tr>
-      <tr><th>Definition</th><td>$cvterm->definition</td></tr>
-    </table>
-    </div>
-  ";
-
-  return $output;
-}
+}

+ 13 - 71
tripal_cv/tripal_cv.module

@@ -46,7 +46,7 @@ function tripal_cv_menu() {
     'description' => 'Basic Description of Tripal CV Module Functionality',
     'page callback' => 'tripal_cv_module_description_page',
     'page arguments' => array(''),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
   
@@ -54,7 +54,7 @@ function tripal_cv_menu() {
     'title' => 'Load Ontology',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_cv_obo_form'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
   
@@ -63,7 +63,7 @@ function tripal_cv_menu() {
     'description' => 'The Chado cvtermpath table provides lineage for terms and is useful for quickly finding any ancestor parent of a term.  However, this table must be populated.  This page allows for populating of this table one vocabulary at a time',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_cv_cvtermpath_form'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -74,7 +74,7 @@ function tripal_cv_menu() {
     'title' => 'Add a Vocabulary',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_cv_add_form'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
   
@@ -82,13 +82,13 @@ function tripal_cv_menu() {
     'title' => 'Edit a Vocabulary',
     'description' => 'Edit a controlled vocabularies/ontolgoies in Chado ',
     'page callback' => 'tripal_cv_edit_page',
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
       
   $items['admin/tripal/tripal_cv/cv/edit/js'] = array(
     'page callback' => 'tripal_ajax_cv_edit',
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_CALLBACK,
   );
 
@@ -100,19 +100,19 @@ function tripal_cv_menu() {
     'description' => 'Manage controlled vocabulary/ontology terms in Chado ',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_cv_cvterm_form', 'add'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
   
   $items['admin/tripal/tripal_cv/cvterm/ahah'] = array(
     'page callback' => 'tripal_cv_cvterm_callback',
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_CALLBACK,
   );
   $items['admin/tripal/tripal_cv/cvterm/auto_name/%/%'] = array(
     'page callback' => 'tripal_cv_cvterm_name_autocomplete',
     'page arguments' => array(5, 6),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_CALLBACK,
   );
   
@@ -121,10 +121,10 @@ function tripal_cv_menu() {
     'description' => 'Edit an existing controlled vocabulary/ontology terms in Chado ',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_cv_cvterm_form', 'edit'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer controlled vocabularies'),
     'type' => MENU_NORMAL_ITEM,
   );
-
+  
   /*
    * Charts
    */  
@@ -142,7 +142,7 @@ function tripal_cv_menu() {
    */  
   $items['cv_browser'] = array(
     'page callback' => 'tripal_cv_show_browser',
-    'access arguments' => array('access chado_cv content'),
+    'access arguments' => array('access content'),
     'type' => MENU_CALLBACK
   );
     
@@ -181,49 +181,9 @@ function tripal_cv_menu() {
     'type' => MENU_CALLBACK
   );
 
-  $items['tripal_cv_cvterm_edit'] = array(
-    'path' => 'tripal_cv_edit',
-    'title' => 'CV Term Editor',
-    'page callback' => 'tripal_cv_cvterm_edit',
-    'page arguments' => array(1),
-    'access arguments' => array('edit chado_cv content'),
-    'type' => MENU_CALLBACK
-  );
-
   return $items;
 }
 
-/**
- *  The following function proves access control for users trying to
- *  perform actions on data managed by this module
- *
- * @ingroup tripal_cv
- */
-function chado_cv_access($op, $node, $account) {
-  if ($op == 'create') {
-    if (!user_access('create chado_cv content', $account)) {
-      return FALSE;
-    }
-  }
-  if ($op == 'update') {
-    if (!user_access('edit chado_cv content', $account)) {
-      return FALSE;
-    }
-  }
-  if ($op == 'delete') {
-    if (!user_access('delete chado_cv content', $account)) {
-      return FALSE;
-    }
-  }
-  if ($op == 'view') {
-    if (!user_access('access chado_cv content', $account)) {
-      return FALSE;
-    }
-  }
-
-  return NULL;
-}
-
 /**
  *  Set the permission types that the chado module uses.  Essentially we
  *  want permissionis that protect creation, editing and deleting of chado
@@ -233,10 +193,7 @@ function chado_cv_access($op, $node, $account) {
  */
 function tripal_cv_perm() {
   return array(
-    'access chado_cv content',
-    'create chado_cv content',
-    'delete chado_cv content',
-    'edit chado_cv content',
+    'administer controlled vocabularies',
   );
 }
 
@@ -252,21 +209,6 @@ function tripal_cv_views_api() {
   return array('api' => 2.0);
 }
 
-/**
- *  We need to let drupal know about our theme functions and their arguments.
- *  We create theme functions to allow users of the module to customize the
- *  look and feel of the output generated in this module
- *
- * @ingroup tripal_cv
- */
-function tripal_cv_theme() {
-  return array(
-    'tripal_cv_cvterm_edit' => array(
-      'arguments' => array('cvterm'),
-    ),
-  );
-}
-
 /**
  * Implements hook_coder_ignore().
  * Defines the path to the file (tripal_cv.coder_ignores.txt) where ignore rules for coder are stored

+ 0 - 0
tripal_db/tripal_db.api.inc → tripal_db/api/tripal_db.api.inc


+ 322 - 0
tripal_db/includes/tripal_db.admin.inc

@@ -0,0 +1,322 @@
+<?php
+/**
+ * Purpose: Provide Guidance to new Tripal Admin
+ *
+ * @return HTML Formatted text
+ *
+ * @ingroup tripal_db
+ */
+function tripal_db_module_description_page() {
+  $text = '';
+
+  $text = '<h3>Tripal External Database Administrative Tools Quick Links</h3>';
+  $text .= '<ul>';
+  $text .= '<li>' . l('Add an external database for cross-refernces.', 'admin/tripal/tripal_db/add_db') . '</li>';
+  $text .= '<li>' . l('Update or delete an external database.', 'admin/tripal/tripal_db/edit_db') . '</li>';
+  $text .= '</ul><br>';
+
+  $text .= '<h3>Module Description:</h3>';
+  $text .= '<p>The Tripal DB Module provides the ability to add database cross reference to the 
+    data in your Tripal Website.  Typically an external database (such as NCBI Genbank, Gene Ontology (GO),
+    stocks database) contains a collection of objects (genomic sequences, vocabulary terms, stocks) that are 
+    uniquely identified using an accession number (or identifier).  Data loaded into Tripal can be a
+    associated with these objects in remote databases, and links can appear on pages allowing site visitors
+    to view the associated objects on the remote database\'s website </p>';
+
+  $text .= '<h3>Setup Instructions:</h3>';
+  $text .= '<ol>';
+  $text .= '<li><b>Set Permissions</b>: This module supports the Drupal user permissions interface for
+               controlling administrative access for creating, editing and deleting database cross-reference resources. 
+               The default is that only the site administrator has these
+               permissions.  Best practice is to create <a href="' . url('admin/user/roles') . '">a new role</a> 
+               for administrative tasks, (such as a webmaster role),
+               and then <a href="' . url('admin/user/user') . '">assign users to the role</a>. Finally,
+               <a href="' . url('admin/user/permissions') . '">assign the permission</a> titled "administer db cross-reference".
+               to the new role.</li>';
+  $text .= '<li><b>Adding or Editing an External Databases</b>. Many resources such as NCBI nr or ExPASy SwissProt (to name a few) 
+            come pre-loaded with Chado.  However, you can add new entries or edit existing entries. Also, when loading 
+            ontologies (controlled vocabularies) using the Tripal CV module new databases are added automaticaly for 
+            each ontology.  To enable linking of accession on a page to the page for that accession on the external
+            database, simply add the URL and the URL prefix when adding or editing a database.</li>';
+  
+  $text .= '<li><b>Associate Data with Accessions</b>.  The Tripal loaders (e.g. GFF, OBO) can associate accessions from
+            remote data to genomic features and controlled vocabularies automatically.  Use the loaders to load genomic
+            features and controlled vocabularies respectively.  Additionally, the bulk loader can be used to create
+            loading templates for associating external database accessions.  
+            </li>';
+  $text .= '</ol>';
+
+
+  $text .= '<br><h3>Features of this Module:</h3>';
+  $text .= '<ul>';
+  $text .= '<li><b>Add an External Databases</b>:';
+  $text .= 'By entering the name and any additional details into the <a href="tripal_db/add_db">add database form</a> you register an external database with your website. This allows you to specify that a sequence feature or other data is also stored in an external database. This is escpecially useful if the external database may contain additional details not stored in yours. If the external database is online you can even provide a URL prefix which will automatically link any data in your website to theirs via a web link.</li>';
+
+  $text .= '<li><b>Update or Delete and External Databases</b>';
+  $text .= 'To edit the details of an external database record or to delete an already existing external database, go to the <a href="tripal_db/edit_db">Update/Delete DBs form</a>. This will allow you to change details or enter new details.</li>';
+
+  $text .= '</ul>';
+
+  return $text;
+}
+/**
+ *
+ *
+ * @ingroup tripal_db
+ */
+function tripal_db_admin_page() {
+  $add_url = url("admin/tripal/tripal_db/add_db");
+  $output = "<a href=\"$add_url\">Add a new external database</a>";
+  $output .= drupal_get_form('tripal_db_select_form');
+  $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
+  return $output;
+}
+/**
+ *
+ *
+ * @ingroup tripal_db
+ */
+function tripal_db_select_form() {
+
+
+
+  return $form;
+}
+/**
+ *
+ * @ingroup tripal_db
+ */
+function tripal_ajax_db_edit() {
+  $status = TRUE;
+
+  // prepare and render the form
+  $form = tripal_core_ahah_prepare_form();   
+  $data = drupal_render($form);  
+
+  // bind javascript events to the new objects that will be returned 
+  // so that AHAH enabled elements will work.
+  $settings = tripal_core_ahah_bind_events();
+
+  // return the updated JSON
+  drupal_json(
+    array(
+      'status'   => $status, 
+      'data'     => $data,
+      'settings' => $settings,
+    )  
+  );
+}
+
+/**
+ *
+ * @ingroup tripal_db
+ */
+function tripal_db_form(&$form_state = NULL, $action = 'Update') {
+  
+  $dbid = $form_state['values']['dbid'];
+  
+  
+  if (strcmp($action,'Update')==0) {
+    // get a list of db from chado for user to choose
+    $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
+    $results = chado_query($sql);
+  
+    $dbs = array();
+    $dbs[] = '';
+    while ($db = db_fetch_object($results)) {
+      $dbs[$db->db_id] = $db->name;
+    }
+  
+    $form['dbid'] = array(
+      '#title' => t('External Database Name'),
+      '#type' => 'select',
+      '#options' => $dbs,
+      '#ahah' => array(
+        'path' => 'admin/tripal/tripal_db/edit/js',
+        'wrapper' => 'db-edit-div',
+        'effect' => 'fade',
+        'event' => 'change',
+        'method' => 'replace',
+      ),
+      '#prefix' => '<div id="db-edit-div">',
+      '#suffix' => '</div>',
+      '#default_value' => $dbid,
+      '#description' => t('Please select a database to edit'),
+    );
+  }   
+  else {
+    $default_db = $form_state['values']['name'];
+    $default_desc = $form_state['values']['description'];
+    $default_url = $form_state['values']['url'];
+    $default_urlprefix = $form_state['values']['urlprefix']; 
+  }
+  
+  // get this requested database
+  if ($dbid) {
+    $values = array('db_id' => $dbid);
+    $result = tripal_core_chado_select('db', array('*'), $values);
+    $db = $result[0];
+    $prev_dbid = $form_state['values']['prev_dbid'];
+    // if the database has changed then repopulate the fields with the databaes values
+    if ($prev_dbid != $dbid) {
+      $default_db        = $db->name;
+      $default_desc      = $db->description;
+      $default_url       = $db->url;
+      $default_urlprefix = $db->urlprefix;
+    }
+    // if the database did not change then keep the values in the form values
+    else {
+      $default_db = $form_state['values']['name'];
+      $default_desc = $form_state['values']['description'];
+      $default_url = $form_state['values']['url'];
+      $default_urlprefix = $form_state['values']['urlprefix'];      
+    }
+  }
+  
+  $form['form_action'] = array(
+    '#type' => 'hidden',
+    '#value' => $action, 
+  );  
+
+  // we need to distinguish between edits in a field that may have failed
+  // and when the user selects a different database from the list.  
+  $form['prev_dbid'] = array(
+    '#type' => 'hidden',
+    '#value' => $dbid, 
+  );  
+  
+  // if we want to update a database but the user has not
+  // yet selected a database then return so we don't show the other fields
+  // the rest of the fields will be added with the AHAH callback. 
+  if (strcmp($action,'Update')==0 and !$dbid) {
+    return $form;
+  }
+
+  $form['name']= array(
+    '#type'          => 'textfield',
+    '#title'         => t("Database Name"),
+    '#description'   => t('Please enter the name for this external database.'),
+    '#required'      => TRUE,
+    '#default_value' => $default_db,
+    '#weight'        => 1
+  );
+
+  $form['description']= array(
+    '#type'          => 'textarea',
+    '#title'         => t('Description'),
+    '#description'   => t('Please enter a description for this database'),
+    '#default_value' => $default_desc,
+    '#weight'        => 2
+  );
+  $form['url']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('URL'),
+    '#description'   => t('Please enter the web address for this database.'),
+    '#default_value' => $default_url,
+    '#weight'        => 3
+  );
+  $form['urlprefix']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('URL prefix'),
+    '#description'   => t('Tripal can provide links to external databases when accession numbers or unique identifiers are known.  Typically, a database will provide a unique web address for each accession and the accession usually is the last component of the page address.  Please enter the web address, minus the accession number for this database.  When an accession number is present, Tripal will combine this web address with the accession and provide a link to the external site.'),
+    '#default_value' => $default_urlprefix,
+    '#weight'        => 4
+  );
+
+
+  if (strcmp($action, 'Update')==0) {
+    $form['update'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Update'),
+      '#weight'       => 5,
+      '#executes_submit_callback' => TRUE,
+    );
+    $form['delete'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Delete'),
+      '#weight'       => 6,
+      '#executes_submit_callback' => TRUE,
+    );
+  }
+  else {
+    $form['add'] = array(
+      '#type'         => 'submit',
+      '#value'        => t('Add'),
+      '#weight'       => 5,
+      '#executes_submit_callback' => TRUE,
+    );
+  }
+
+  return $form;
+}
+/**
+ *
+ * @ingroup tripal_db
+ */
+function tripal_db_form_validate($form, &$form_state) {
+  $name =  trim($form_state['values']['name']);
+  $desc =  trim($form_state['values']['description']);
+  $url  =  trim($form_state['values']['url']);
+  $urlp =  trim($form_state['values']['urlprefix']);
+  $dbid =  trim($form_state['values']['dbid']);
+  $op   =  trim($form_state['values']['op']);
+  $action =  $form_state['values']['form_action'];
+ 
+  // make sure the database name is unique
+  $values = array('name' => $name);
+  $results = tripal_core_chado_select('db', array('db_id'), $values);   
+  if (count($results) > 0 and $results[0]->db_id != $dbid) {
+    form_set_error('name', 'The database name must be unique');
+  }
+}
+/**
+ *
+ * @ingroup tripal_db
+ */
+function tripal_db_form_submit($form, &$form_state) {
+
+  $name =  trim($form_state['values']['name']);
+  $desc =  trim($form_state['values']['description']);
+  $url  =  trim($form_state['values']['url']);
+  $urlp =  trim($form_state['values']['urlprefix']);
+  $dbid =  trim($form_state['values']['dbid']);
+  $op   =  trim($form_state['values']['op']);
+
+  $values = array(
+    'name' => $name,
+    'description' => $desc,
+    'url' => $url,
+    'urlprefix' => $urlp,
+  );
+  if ($dbid) {
+    if (strcmp($op, 'Update')==0) {      
+      $match = array('db_id' => $dbid);
+      $success = tripal_core_chado_update('db', $match, $values);
+      if ($success) {
+        drupal_set_message(t("External database updated"));
+      }
+      else {
+        drupal_set_message(t("Failed to update external database."));
+      }
+    }
+    if (strcmp($op, 'Delete')==0) {
+      $match = array('db_id' => $dbid);
+      $success = tripal_core_chado_delete('db', $match);
+      if ($success) {
+        drupal_set_message(t("External database deleted"));
+      }
+      else {
+        drupal_set_message(t("Failed to delete external database."));
+      }
+    }
+  }
+  else {
+    $success = tripal_core_chado_insert('db', $values);
+    if ($success) {
+      drupal_set_message(t("External database added"));
+    }
+    else {
+      drupal_set_message(t("Failed to add external database."));
+    }
+  }
+}

+ 25 - 279
tripal_db/tripal_db.module

@@ -1,6 +1,7 @@
 <?php
 
-require_once "tripal_db.api.inc";
+require_once "api/tripal_db.api.inc";
+require_once "includes/tripal_db.admin.inc";
 
 /**
  * @defgroup tripal_db DB Module
@@ -28,28 +29,32 @@ function tripal_db_menu() {
     'title' => 'Databases',
     'description' => 'Basic Description of Tripal DB Module Functionality',
     'page callback' => 'tripal_db_module_description_page',
-    'access arguments' => array('administer site configuration'),
+    'access callback' => 'user_access',
+    'access arguments' => array('administer db cross-references'),
     'type' => MENU_NORMAL_ITEM,
   );
 
   $items['admin/tripal/tripal_db/edit_db'] = array(
-    'title' => 'Update/Delete Database References',
+    'title' => 'Edit a Database',
     'description' => 'Manage Databases ',
-    'page callback' => 'tripal_db_admin_page',
-    'access arguments' => array('administer site configuration'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_db_form', 'Update'),
+    'access callback' => 'user_access',
+    'access arguments' => array('administer db cross-references'),
     'type' => MENU_NORMAL_ITEM,
   );
 
   $items['admin/tripal/tripal_db/add_db'] = array(
     'title' => 'Add a Database',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_db_form'),
-    'access arguments' => array('access administration pages'),
+    'page arguments' => array('tripal_db_form', 'Add'),
+    'access callback' => 'user_access',
+    'access arguments' => array('administer db cross-references'),
     'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/tripal/tripal_db/edit/js'] = array(
-    'title' => 'Edit Databases',
     'page callback' => 'tripal_ajax_db_edit',
+    'access callback' => 'user_access',
     'access arguments' => array('access administration pages'),
     'type' => MENU_CALLBACK,
   );
@@ -66,10 +71,7 @@ function tripal_db_menu() {
  */
 function tripal_db_perm() {
   return array(
-    'access chado_db content',
-    'create chado_db content',
-    'delete chado_db content',
-    'edit chado_db content',
+    'administer db cross-references',
   );
 }
 
@@ -85,272 +87,16 @@ function tripal_db_views_api() {
   return array('api' => 2.0);
 }
 
-/**
- * Purpose: Provide Guidance to new Tripal Admin
- *
- * @return HTML Formatted text
- *
- * @ingroup tripal_db
- */
-function tripal_db_module_description_page() {
-  $text = '';
-
-  $text = '<h3>Tripal External Database Administrative Tools Quick Links</h3>';
-    $text .= '<ul>';
-      $text .= '<li>' . l('Add External DB', 'admin/tripal/tripal_db/add_db') . '</li>';
-      $text .= '<li>' . l('Update/Delete External DBs', 'admin/tripal/tripal_db/edit_db') . '</li>';
-      $text .= '<li>' . l('Database References Listing', 'admin/tripal/tripal_db/list_dbxrefs') . '</li>';
-    $text .= '</ul>';
-
-  $text .= '<h3>Module Description:</h3>';
-  $text .= '<p>The Tripal DB Module provides functionality for linking the data in your Tripal Website with other biological websites out there. Essentially you register an enternal database with your website and then associate any of your data (usually sequence features) with that external database by providing the accession for your data in the other database. If the other database is online and you provided a URL prefix when you registered the external database with your site then there will be a link on the details page for your data that takes the user to the same record in the external database.</p>';
-
-  $text .= '<h3>Setup Instructions:</h3>';
-  $text .= '<ol>';
-  $text .= '<li><p><b>Set Permissions</b>: The feature module supports the Drupal user permissions interface for
-               controlling access to feature content and functions. These permissions include viewing,
-               creating, editing or administering of
-               feature content. The default is that only the original site administrator has these
-               permissions.  You can <a href="' . url('admin/user/roles') . '">add roles</a> for classifying users,
-               <a href="' . url('admin/user/user') . '">assign users to roles</a> and
-               <a href="' . url('admin/user/permissions') . '">assign permissions</a> for the feature content to
-               those roles.  For a simple setup, allow anonymous users access to view organism content and
-               allow the site administrator all other permissions.</p></li>';
-  $text .= '<li><b>Register any external databases</b> with data pertinent to your site.</li>';
-  $text .= '<li><b>Create Database References</b>: Then as you load in your data, create database references linking your data to the external database.</li>';
-  $text .= '</ol>';
-
-
-  $text .= '<h3>Features of this Module:</h3>';
-  $text .= '<ul>';
-  $text .= '<li><b>Add/Register External Databases</b>:';
-  $text .= 'By entering the name and any additional details into the <a href="tripal_db/add_db">add database form</a> you register an external database with your website. This allows you to specify that a sequence feature or other data is also stored in an external database. This is escpecially useful if the external database may contain additional details not stored in yours. If the external database is online you can even provide a URL prefix which will automatically link any data in your website to theirs via a web link.</li>';
-
-  $text .= '<li><b>Update/Delete External Databases</b>';
-  $text .= 'To edit the details of an external database record or to delete an already existing external database, go to the <a href="tripal_db/edit_db">Update/Delete DBs form</a>. This will allow you to change details or enter new details.</li>';
-
-  $text .= '<li><b>List all External Database References</b>';
-  $text .= 'If you have views installed, there will be a link to a default listing of all database references currently in your database. This listing can be accessed <a href="tripal_db/list_dbxrefs">here</a>. It requires the Drupal Module Views version 2 to be installed (<a href="http://drupal.org/project/views">Drupal Views</a>)</li>';
-  $text .= '</ul>';
-
-  return $text;
-}
-
-/**
- *
- *
- * @ingroup tripal_db
- */
-function tripal_db_admin_page() {
-  $add_url = url("admin/tripal/tripal_db/add_db");
-  $output = "<a href=\"$add_url\">Add a new external database</a>";
-  $output .= drupal_get_form('tripal_db_select_form');
-  $output .= '<div id="db-edit-div">Please select a database above to view or edit</div>';
-  return $output;
-}
-/**
- *
- *
- * @ingroup tripal_db
- */
-function tripal_db_select_form() {
-
-  // get a list of db from chado for user to choose
-  $sql = "SELECT * FROM {db} WHERE NOT name = 'tripal' ORDER BY name ";
-  $results = chado_query($sql);
-
-  $dbs = array();
-  $dbs[] = '';
-  while ($db = db_fetch_object($results)) {
-    $dbs[$db->db_id] = $db->name;
-  }
-
-  $form['dbid'] = array(
-    '#title' => t('External Database Name'),
-    '#type' => 'select',
-    '#options' => $dbs,
-    '#ahah' => array(
-      'path' => 'admin/tripal/tripal_db/edit/js',
-      'wrapper' => 'db-edit-div',
-      'effect' => 'fade',
-      'event' => 'change',
-      'method' => 'replace',
-    ),
-  );
-
-  return $form;
-}
-/**
- *
- * @ingroup tripal_db
- */
-function tripal_ajax_db_edit() {
-  // get the database id, build the form and then return the JSON object
-  $dbid = filter_xss($_POST['dbid']);
-  $form = drupal_get_form('tripal_db_form', $dbid);
-  drupal_json(array('status' => TRUE, 'data' => $form));
-}
-/**
- *
- * @ingroup tripal_db
- */
-function tripal_db_form(&$form_state = NULL, $dbid = NULL) {
-
-  // get this requested database
-  if ($dbid) {
-    $sql = "SELECT * FROM {db} WHERE db_id = %d ";
-    $db = db_fetch_object(chado_query($sql, $dbid));
-
-
-    // set the default values.  If there is a value set in the
-    // form_state then let's use that, otherwise, we'll pull
-    // the values from the database
-    $default_db = $form_state['values']['name'];
-    $default_desc = $form_state['values']['description'];
-    $default_url = $form_state['values']['url'];
-    $default_urlprefix = $form_state['values']['urlprefix'];
-    if (!$default_db) {
-      $default_db = $db->name;
-    }
-    if (!$default_desc) {
-      $default_desc = $db->description;
-    }
-    if (!$default_url) {
-      $default_url = $db->url;
-    }
-    if (!$default_urlprefix) {
-      $default_urlprefix = $db->urlprefix;
-    }
-    $action = 'Update';
-  }
-  else {
-    $action = 'Add';
-  }
-
-  $form['dbid'] = array(
-    '#type' => 'hidden',
-    '#value' => $dbid
-  );
-
-  $form['name']= array(
-    '#type'          => 'textfield',
-    '#title'         => t("Database Name"),
-    '#description'   => t('Please enter the name for this external database.'),
-    '#required'      => TRUE,
-    '#default_value' => $default_db,
-    '#weight'        => 1
-  );
-
-  $form['description']= array(
-    '#type'          => 'textarea',
-    '#title'         => t('Description'),
-    '#description'   => t('Please enter a description for this database'),
-    '#default_value' => $default_desc,
-    '#weight'        => 2
-  );
-  $form['url']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('URL'),
-    '#description'   => t('Please enter the web address for this database.'),
-    '#default_value' => $default_url,
-    '#weight'        => 3
-  );
-  $form['urlprefix']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('URL prefix'),
-    '#description'   => t('Tripal can provide links to external databases when accession numbers or unique identifiers are known.  Typically, a database will provide a unique web address for each accession and the accession usually is the last component of the page address.  Please enter the web address, minus the accession number for this database.  When an accession number is present, Tripal will combine this web address with the accession and provide a link to the external site.'),
-    '#default_value' => $default_urlprefix,
-    '#weight'        => 4
-  );
-
-
-  if (strcmp($action, 'Update')==0) {
-    $form['update'] = array(
-      '#type'         => 'submit',
-      '#value'        => t('Update'),
-      '#weight'       => 5,
-      '#executes_submit_callback' => TRUE,
-    );
-    $form['delete'] = array(
-      '#type'         => 'submit',
-      '#value'        => t('Delete'),
-      '#weight'       => 6,
-      '#executes_submit_callback' => TRUE,
-    );
-  }
-  else {
-    $form['add'] = array(
-      '#type'         => 'submit',
-      '#value'        => t('Add'),
-      '#weight'       => 5,
-      '#executes_submit_callback' => TRUE,
-    );
-  }
-  $form['#redirect'] = 'admin/tripal/tripal_db';
-
-
-  return $form;
-}
-/**
- *
- * @ingroup tripal_db
- */
-function tripal_db_form_submit($form, &$form_state) {
-
-  $name =  $form_state['values']['name'];
-  $desc =  $form_state['values']['description'];
-  $url  =  $form_state['values']['url'];
-  $urlp =  $form_state['values']['urlprefix'];
-  $dbid =  $form_state['values']['dbid'];
-  $op   =  $form_state['values']['op'];
-
-  if ($dbid) {
-    if (strcmp($op, 'Update')==0) {
-      $sql = "
-        UPDATE {db} SET
-          name = '%s',
-          description = '%s',
-          url = '%s',
-          urlprefix = '%s'
-        WHERE db_id = %d
-      ";
-      $db = chado_query($sql, $name, $desc, $url, $urlp, $dbid);
-      if ($db) {
-        drupal_set_message(t("External database updated"));
-      }
-      else {
-        drupal_set_message(t("Failed to update external database."));
-      }
+function tripal_db_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == "tripal_db_form") {    
+    // updating the form through the ahah callback sets the action of
+    // the form to the ahah callback URL. We need to set it back
+    // to the normal form URL 
+    if ($form_state['values']['form_action'] == 'Update') {
+      $form['#action'] = url("admin/tripal/tripal_db/edit_db");
     }
-    if (strcmp($op, 'Delete')==0) {
-      $sql = "
-        DELETE FROM {db}
-        WHERE db_id = %d
-        ";
-      $db = chado_query($sql, $dbid);
-      if ($db) {
-        drupal_set_message(t("External database deleted"));
-      }
-      else {
-        drupal_set_message(t("Failed to delete external database."));
-      }
+    if ($form_state['values']['form_action'] == 'Add') {
+      $form['#action'] = url("admin/tripal/tripal_db/add_db");
     }
-  }
-  else {
-    $sql = "
-          INSERT INTO {db}
-          (name,description,url,urlprefix)
-          VALUES
-          ('%s','%s','%s','%s')
-          ";
-    $db = chado_query($sql, $name, $desc, $url, $urlp);
-    if ($db) {
-      drupal_set_message(t("External database added"));
-    }
-    else {
-      drupal_set_message(t("Failed to add external database."));
-    }
-  }
-
-  return '';
-}
+  } 
+}

+ 139 - 1
tripal_feature/api/tripal_feature.api.inc

@@ -888,4 +888,142 @@ function tripal_feature_get_custom_tables($table = NULL) {
     );
   }
   return $schema;
-}
+}
+
+/**
+ * Using the tripal_core_expand_chado_vars function to retrieve a set
+ * of relationships can be very slow, especialy if there are many relationships
+ * This function is intended to help speed up the retrieval of relationships
+ * by only retrieving the base information for the relationship and returning
+ * an array with  
+ *
+ * @param $feature
+ *   The feature object 
+ 
+ * @return
+ *   An array with two objects
+ *
+ * @ingroup tripal_feature_api
+ */
+function tripal_feature_get_feature_relationships($feature) {
+  // expand the feature object to include the feature relationships.
+  $options = array(
+    'return_array' => 1,
+    'order_by'     => array('rank' => 'ASC'),
+  );
+  $feature = tripal_core_expand_chado_vars($feature, 'table',
+    'feature_relationship', $options);
+
+  // get the subject relationships
+  $srelationships = $feature->feature_relationship->subject_id;
+  $orelationships = $feature->feature_relationship->object_id;
+  
+  // get alignment as child. The $feature->featureloc element
+  // is already populated from the alignment preprocess function
+  $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc');
+  $cfeaturelocs = $feature->featureloc->feature_id;
+  if (!$cfeaturelocs) {
+     $cfeaturelocs = array();
+  } 
+  elseif (!is_array($cfeaturelocs)) { 
+     $cfeaturelocs = array($cfeaturelocs); 
+  }
+  
+  // prepare the SQL statement to get the featureloc for the 
+  // feature in the relationships. 
+  $connection = tripal_db_persistent_chado();
+  $psql = " 
+    PREPARE sel_featureloc_preprocess_relationships (int, int) AS 
+    SELECT 
+      FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
+      FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
+    FROM featureloc FL 
+      INNER JOIN feature F ON F.feature_id = FL.srcfeature_id
+    WHERE FL.feature_id = $1 and FL.srcfeature_id = $2
+  ";
+  tripal_core_chado_prepare('sel_featureloc_preprocess_relationships', $psql, array('int', 'int'));
+
+  
+  // combine both object and subject relationshisp into a single array
+  $relationships = array();
+  $relationships['object'] = array();
+  $relationships['subject'] = array();
+  
+  // iterate through the object relationships
+  if ($orelationships) {
+    foreach ($orelationships as $relationship) {
+       $rel = new stdClass(); 
+       // get locations where the child feature and this feature overlap with the
+       // same landmark feature.
+       $rel->child_featurelocs = array();     
+       foreach ($cfeaturelocs as $featureloc) {
+          $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)", 
+            $relationship->subject_id->feature_id, 
+            $featureloc->srcfeature_id->feature_id);        
+          while ($loc = db_fetch_object($res)) {
+             // add in the node id of the src feature if it exists and save this location
+             $loc->nid = $featureloc->srcfeature_id->nid;
+             $rel->child_featurelocs[] = $loc;
+          }
+       }
+       $rel->record = $relationship;    
+       
+       // get the relationship and child types
+       $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
+       $child_type = $relationship->subject_id->type_id->name;
+       
+       // get the node id of the subject
+       $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
+       $n = db_fetch_object(db_query($sql, $relationship->subject_id->feature_id));
+       if ($n) {
+          $rel->record->nid = $n->nid;
+       }
+  
+       if (!array_key_exists($rel_type, $relationships['object'])) {
+         $relationships['object'][$rel_type] = array();   
+       }
+       if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
+         $relationships['object'][$rel_type][$child_type] = array();   
+       }
+       $relationships['object'][$rel_type][$child_type][] = $rel;     
+    }
+  }
+  
+  // now add in the subject relationships
+  if ($srelationships) {
+    foreach ($srelationships as $relationship) {
+       $rel = new stdClass(); 
+       // get locations where this feature overlaps with the parent
+       $rel->parent_featurelocs = array();     
+       foreach ($cfeaturelocs as $featureloc) {
+          $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)", 
+            $relationship->object_id->feature_id, 
+            $featureloc->srcfeature_id->feature_id);
+          while ($loc = db_fetch_object($res)) {
+             // add in the node id of the src feature if it exists and save this location
+             $loc->nid = $featureloc->srcfeature_id->nid;
+             $rel->parent_featurelocs[] = $loc;
+          }
+       }
+       $rel->record = $relationship;
+       $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
+       $parent_type = $relationship->object_id->type_id->name;
+       
+       // get the node id of the subject
+       $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
+       $n = db_fetch_object(db_query($sql, $relationship->object_id->feature_id));
+       if ($n) {
+          $rel->record->nid = $n->nid;
+       }
+       
+       if (!array_key_exists($rel_type, $relationships['subject'])) {
+         $relationships['subject'][$rel_type] = array();   
+       }
+       if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
+         $relationships['subject'][$rel_type][$parent_type] = array();   
+       }
+       $relationships['subject'][$rel_type][$parent_type][] = $rel;
+    }
+  }
+  return $relationships;
+} 

+ 211 - 203
tripal_feature/includes/gff_loader.inc

@@ -422,7 +422,11 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
     // check to see if we have FASTA section, if so then set the variable
     // to start parsing
     if (preg_match('/^##FASTA/i', $line)) {
-      tripal_feature_load_gff_fasta($fh, $interval, $num_read, $intv_read, $line_num);
+      if($remove) {
+        // we're done because this is a delete operation so break out of the loop.
+        break;         
+      }
+      tripal_feature_load_gff3_fasta($fh, $interval, $num_read, $intv_read, $line_num);
       continue;
     }
     // if the ##sequence-region line is present then we want to add a new feature
@@ -604,17 +608,16 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
     // all types. If not we'll error out.  This test is only necessary if
     // if the landmark and the uniquename are different.  If they are the same
     // then this is the information for the landmark
-    if (strcmp($landmark, $attr_uniquename) != 0 ) {
+    if (!$remove and strcmp($landmark, $attr_uniquename) != 0 ) {
       $select = array(
          'organism_id' => $organism_id,
          'uniquename'  => $landmark,
       );
       $columns = array('count(*) as num_landmarks');
       $options = array('statement_name' => 'sel_feature_numland');      
-      $count = tripal_core_chado_select('feature', $columns, $select, $options);
-     
-      if (!$count or $count[0]->num_landmarks == 0) {
-        watchdog('T_gff3_loader', "The landmark '%landmark' cannot be found for this organism. ".
+      $count = tripal_core_chado_select('feature', $columns, $select, $options);   
+      if (!$count or count($count) == 0 or $count[0]->num_landmarks == 0) {
+        watchdog('T_gff3_loader', "The landmark '%landmark' cannot be found for this organism (" . $organism->genus . " " . $organism->species . ") " .
               "Please add the landmark and then retry the import of this GFF3 ".
               "file", array('%landmark' => $landmark), WATCHDOG_ERROR);
         return '';
@@ -699,127 +702,10 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
         // add parent relationships
         if (array_key_exists('Parent', $tags)) {
           tripal_feature_load_gff3_parents($feature, $cvterm, $tags['Parent'], $organism_id, $fmin);
-        }     
-          
+        }               
         // add target relationships
         if (array_key_exists('Target', $tags)) {
-          // format is: "target_id start end [strand]", where strand is optional and may be "+" or "-"
-          $matched = preg_match('/^(.*?)\s+(\d+)\s+(\d+)(\s+[\+|\-])*$/', trim($tags['Target'][0]), $matches);
-          
-          // the organism and type of the target may also be specified as an attribute. If so, then get that
-          // information
-          $gff_target_organism = array_key_exists('target_organism', $tags) ? $tags['target_organism'][0] : '';
-          $gff_target_type = array_key_exists('target_type', $tags) ? $tags['target_type'][0] : '';
-          
-          // if we have matches and the Target is in the correct format then load the alignment 
-          if ($matched) {
-            $target_feature = $matches[1]; 
-            $start = $matches[2]; 
-            $end = $matches[3]; 
-            // if we have an optional strand, convert it to a numeric value. 
-            if ($matches[4]) {
-              if (preg_match('/^\+$/', trim($matches[4]))) {
-                $target_strand = 1;
-              }
-              elseif (preg_match('/^\-$/', trim($matches[4]))) {
-                $target_strand = -1;
-              }
-              else {
-                $target_strand = 0;
-              }
-            }
-            else {
-               $target_strand = 0;
-            }
-            
-            $target_fmin = $start - 1;
-            $target_fmax = $end;
-            if ($end < $start) {
-              $target_fmin = $end - 1;
-              $target_fmax = $start;
-            }
-            
-            // default the target organism to be the value passed into the function, but if the GFF
-            // file species the target organism then use that instead.
-            $t_organism_id = $target_organism_id;
-            if ($gff_target_organism) {
-              // get the genus and species
-              $success = preg_match('/^(.*?):(.*?)$/', $gff_target_organism, $matches);
-              if ($success) {
-                $values = array(
-                  'genus' => $matches[1],
-                  'species' => $matches[2],
-                );
-                $options = array('statement_name' => 'sel_organism_gesp');
-                $torganism = tripal_core_chado_select('organism', array('organism_id'), $values, $options);
-                if (count($torganism) == 1) {
-                  $t_organism_id = $torganism[0]->organism_id;
-                }
-                else {
-                  watchdog('T_gff3_loader', "Cannot find organism for target %target.", 
-                    array('%target' => $gff_target_organism), WATCHDOG_WARNING);
-                  $t_organism_id = '';                                   
-                }
-              }
-              else {
-                watchdog('T_gff3_loader', "The target_organism attribute is improperly formatted: %target. 
-                  It should be target_organism=genus:species.", 
-                  array('%target' => $gff_target_organism), WATCHDOG_WARNING);
-                $t_organism_id = '';                
-              }
-            }  
-
-            // default the target type to be the value passed into the function, but if the GFF file
-            // species the target type then use that instead
-            $t_type_id = '';
-            if ($target_type) {
-              $values = array(
-                'name' => $target_type,
-                'cv_id' => array(
-                   'name' => 'sequence',
-                )
-              );
-              $options = array('statement_name' => 'sel_cvterm_nacv');
-              $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
-              if (count($type) == 1) {
-                $t_type_id = $type[0]->cvterm_id;
-              }
-              else {
-                watchdog('T_gff3_loader', "The target type does not exist in the sequence ontology: %type. ", 
-                  array('%type' => $target_type), WATCHDOG_ERROR);
-                exit;  
-              }
-            }
-            if ($gff_target_type) {
-              $values = array(
-                'name' => $gff_target_type,
-                'cv_id' => array(
-                   'name' => 'sequence',
-                )
-              );
-              $options = array('statement_name' => 'sel_cvterm_nacv');
-              $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
-              if (count($type) == 1) {
-                $t_type_id = $type[0]->cvterm_id;
-              }
-              else {
-                watchdog('T_gff3_loader', "The target_type attribute does not exist in the sequence ontology: %type. ", 
-                  array('%type' => $gff_target_type), WATCHDOG_WARNING);
-                $t_type_id = '';
-              }
-            }                       
-            
-            // we want to add a featureloc record that uses the target feature as the srcfeature (landmark)
-            // and the landmark as the feature.
-            tripal_feature_load_gff3_featureloc($feature, $organism, $target_feature, $target_fmin, 
-              $target_fmax, $target_strand, $phase, $attr_fmin_partial, $attr_fmax_partial, $attr_residue_info, 
-              $attr_locgroup, $t_type_id, $t_organism_id, $create_target, TRUE); 
-          }
-          // the target attribute is not correctly formatted
-          else {
-            watchdog('T_gff3_loader', "Could not add 'Target' alignment as it is improperly formatted:  '%target'",
-              array('%target' => $tags['Target'][0]), WATCHDOG_ERROR);            
-          }
+          tripal_feature_load_gff3_target($feature, $tags, $target_organism_id, $target_type, $create_target, $attr_locgroup);          
         }
         // add gap information.  This goes in simply as a property
         if (array_key_exists('Gap', $tags)) {
@@ -833,16 +719,13 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
               tripal_feature_load_gff3_property($feature, 'Note', $value);
           }
         }
-
         // add the Derives_from relationship (e.g. polycistronic genes).
         if (array_key_exists('Derives_from', $tags)) {
           tripal_feature_load_gff3_derives_from($feature, $tags['Derives_from'][0], $organism);
         }
-
         // add in the GFF3_source dbxref so that GBrowse can find the feature using the source column
         $source_ref = array('GFF_source:' . $source);
         tripal_feature_load_gff3_dbxref($feature, $source_ref);
-
         // add any additional attributes
         if ($attr_others) {
           foreach ($attr_others as $tag_name => $values) {
@@ -856,88 +739,90 @@ function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
     }
   }
 
-  print "\nSetting ranks of children...\n";
-  
-  // get features in a relationship that are also children of an alignment
-  $sql = "SELECT DISTINCT F.feature_id, F.organism_id, F.type_id, 
-            F.uniquename, FL.strand 
-          FROM tripal_gff_temp TGT 
-            INNER JOIN feature F                ON TGT.feature_id = F.feature_id
-            INNER JOIN feature_relationship FR  ON FR.object_id = TGT.feature_id
-            INNER JOIN cvterm CVT               ON CVT.cvterm_id = FR.type_id  
-            INNER JOIN featureloc FL            ON FL.feature_id = F.feature_id    
-          WHERE CVT.name = 'part_of'";
-  $parents = chado_query($sql);
-  
-  // build and prepare the SQL for selecting the children relationship
-  $sql = "SELECT DISTINCT FR.feature_relationship_id, FL.fmin, FR.rank
-          FROM feature_relationship FR              
-            INNER JOIN featureloc FL on FL.feature_id = FR.subject_id";
-  if (!$connection) {
-    $sql .= "WHERE FR.object_id = %d ".
-            "ORDER BY FL.fmin ASC ";
-  }
-  else {
-    $sql = "PREPARE sel_gffchildren (int) AS " . $sql . " WHERE FR.object_id = \$1 ORDER BY FL.fmin ASC";            
-  }
-  if (!tripal_core_is_sql_prepared('sel_gffchildren')) {
-    $success = tripal_core_chado_prepare('sel_gffchildren', $sql, array('int'));
-    if (!$success) {
-      watchdog("T_gff3_loader", "Cannot prepare statement 'sel_gffchildren' and cannot set children ranks.", 
-         array(), WATCHDOG_WARNING);
-      return 0;  
-    }
-  }
-  
-  // now set the rank of any parent/child relationships.  The order is based
-  // on the fmin.  The start rank is 1.  This allows features with other
-  // relationships to be '0' (the default), and doesn't interfer with the
-  // ordering defined here.        
-  while ($parent = db_fetch_object($parents)) {
+  if (!$remove) {
+    print "\nSetting ranks of children...\n";
     
-    // get the children
-    if ($connection) {      
-      $result = chado_query('EXECUTE sel_gffchildren (%d)', $parent->feature_id);
+    // get features in a relationship that are also children of an alignment
+    $sql = "SELECT DISTINCT F.feature_id, F.organism_id, F.type_id, 
+              F.uniquename, FL.strand 
+            FROM tripal_gff_temp TGT 
+              INNER JOIN feature F                ON TGT.feature_id = F.feature_id
+              INNER JOIN feature_relationship FR  ON FR.object_id = TGT.feature_id
+              INNER JOIN cvterm CVT               ON CVT.cvterm_id = FR.type_id  
+              INNER JOIN featureloc FL            ON FL.feature_id = F.feature_id    
+            WHERE CVT.name = 'part_of'";
+    $parents = chado_query($sql);
+    
+    // build and prepare the SQL for selecting the children relationship
+    $sql = "SELECT DISTINCT FR.feature_relationship_id, FL.fmin, FR.rank
+            FROM feature_relationship FR              
+              INNER JOIN featureloc FL on FL.feature_id = FR.subject_id";
+    if (!$connection) {
+      $sql .= "WHERE FR.object_id = %d ".
+              "ORDER BY FL.fmin ASC ";
     }
     else {
-      $result = chado_query($sql, $parent->feature_id);
+      $sql = "PREPARE sel_gffchildren (int) AS " . $sql . " WHERE FR.object_id = \$1 ORDER BY FL.fmin ASC";            
     }
-    
-    // build an array of the children
-    $children = array();
-    while ($child = db_fetch_object($result)) {
-       $children[] = $child;  
+    if (!tripal_core_is_sql_prepared('sel_gffchildren')) {
+      $success = tripal_core_chado_prepare('sel_gffchildren', $sql, array('int'));
+      if (!$success) {
+        watchdog("T_gff3_loader", "Cannot prepare statement 'sel_gffchildren' and cannot set children ranks.", 
+           array(), WATCHDOG_WARNING);
+        return 0;  
+      }
     }
     
-    // the children list comes sorted in ascending fmin
-    // but if the parent is on the reverse strand we need to 
-    // reverse the order of the children.
-    if ($parent->strand == -1) {
-      arsort($children);
-    }    
-
-    // first set the ranks to a negative number so that we don't
-    // get a duplicate error message when we try to change any of them    
-    $rank = -1;
-    foreach ($children as $child) {
-      $match = array('feature_relationship_id' => $child->feature_relationship_id);
-      $options = array('statement_name' => 'upd_featurerelationship_rank');      
-      $values = array('rank' => $rank);      
-      tripal_core_chado_update('feature_relationship', $match, $values, $options);
-      $rank--;
-    }
-    // now set the rank correctly. The rank should start at 0.
-    $rank = 0;
-    foreach ($children as $child) {
-      $match = array('feature_relationship_id' => $child->feature_relationship_id);
-      $options = array('statement_name' => 'upd_featurerelationship_rank');      
-      $values = array('rank' => $rank); 
-      //print "Was: " . $child->rank . " now $rank ($parent->strand)\n"     ;
-      tripal_core_chado_update('feature_relationship', $match, $values, $options);
-      $rank++;
+    // now set the rank of any parent/child relationships.  The order is based
+    // on the fmin.  The start rank is 1.  This allows features with other
+    // relationships to be '0' (the default), and doesn't interfer with the
+    // ordering defined here.        
+    while ($parent = db_fetch_object($parents)) {
+      
+      // get the children
+      if ($connection) {      
+        $result = chado_query('EXECUTE sel_gffchildren (%d)', $parent->feature_id);
+      }
+      else {
+        $result = chado_query($sql, $parent->feature_id);
+      }
+      
+      // build an array of the children
+      $children = array();
+      while ($child = db_fetch_object($result)) {
+         $children[] = $child;  
+      }
+      
+      // the children list comes sorted in ascending fmin
+      // but if the parent is on the reverse strand we need to 
+      // reverse the order of the children.
+      if ($parent->strand == -1) {
+        arsort($children);
+      }    
+  
+      // first set the ranks to a negative number so that we don't
+      // get a duplicate error message when we try to change any of them    
+      $rank = -1;
+      foreach ($children as $child) {
+        $match = array('feature_relationship_id' => $child->feature_relationship_id);
+        $options = array('statement_name' => 'upd_featurerelationship_rank');      
+        $values = array('rank' => $rank);      
+        tripal_core_chado_update('feature_relationship', $match, $values, $options);
+        $rank--;
+      }
+      // now set the rank correctly. The rank should start at 0.
+      $rank = 0;
+      foreach ($children as $child) {
+        $match = array('feature_relationship_id' => $child->feature_relationship_id);
+        $options = array('statement_name' => 'upd_featurerelationship_rank');      
+        $values = array('rank' => $rank); 
+        //print "Was: " . $child->rank . " now $rank ($parent->strand)\n"     ;
+        tripal_core_chado_update('feature_relationship', $match, $values, $options);
+        $rank++;
+      }
     }
   }
-
+  
   // commit the transaction
   if ($use_transaction) {
     tripal_db_commit_transaction();
@@ -1830,7 +1715,7 @@ function tripal_feature_load_gff3_property($feature, $property, $value) {
 /*
  * 
  */
-function tripal_feature_load_gff_fasta($fh, $interval, &$num_read, &$intv_read, &$line_num) {
+function tripal_feature_load_gff3_fasta($fh, $interval, &$num_read, &$intv_read, &$line_num) {
   print "Loading FASTA sequences\n";
   $residues = '';
   $sql = " 
@@ -1909,4 +1794,127 @@ function tripal_feature_load_gff_fasta($fh, $interval, &$num_read, &$intv_read,
     $options = array('statement_name' => 'upd_feature_re');
     tripal_core_chado_update('feature', $match, $values, $options);
   } 
+}
+
+/*
+ * 
+ */
+function tripal_feature_load_gff3_target($feature, $tags, $target_organism_id, $target_type, $create_target, $attr_locgroup) {
+  // format is: "target_id start end [strand]", where strand is optional and may be "+" or "-"
+  $matched = preg_match('/^(.*?)\s+(\d+)\s+(\d+)(\s+[\+|\-])*$/', trim($tags['Target'][0]), $matches);
+  
+  // the organism and type of the target may also be specified as an attribute. If so, then get that
+  // information
+  $gff_target_organism = array_key_exists('target_organism', $tags) ? $tags['target_organism'][0] : '';
+  $gff_target_type = array_key_exists('target_type', $tags) ? $tags['target_type'][0] : '';
+  
+  // if we have matches and the Target is in the correct format then load the alignment 
+  if ($matched) {
+    $target_feature = $matches[1]; 
+    $start = $matches[2]; 
+    $end = $matches[3]; 
+    // if we have an optional strand, convert it to a numeric value. 
+    if ($matches[4]) {
+      if (preg_match('/^\+$/', trim($matches[4]))) {
+        $target_strand = 1;
+      }
+      elseif (preg_match('/^\-$/', trim($matches[4]))) {
+        $target_strand = -1;
+      }
+      else {
+        $target_strand = 0;
+      }
+    }
+    else {
+       $target_strand = 0;
+    }
+    
+    $target_fmin = $start - 1;
+    $target_fmax = $end;
+    if ($end < $start) {
+      $target_fmin = $end - 1;
+      $target_fmax = $start;
+    }
+    
+    // default the target organism to be the value passed into the function, but if the GFF
+    // file species the target organism then use that instead.
+    $t_organism_id = $target_organism_id;
+    if ($gff_target_organism) {
+      // get the genus and species
+      $success = preg_match('/^(.*?):(.*?)$/', $gff_target_organism, $matches);
+      if ($success) {
+        $values = array(
+          'genus' => $matches[1],
+          'species' => $matches[2],
+        );
+        $options = array('statement_name' => 'sel_organism_gesp');
+        $torganism = tripal_core_chado_select('organism', array('organism_id'), $values, $options);
+        if (count($torganism) == 1) {
+          $t_organism_id = $torganism[0]->organism_id;
+        }
+        else {
+          watchdog('T_gff3_loader', "Cannot find organism for target %target.", 
+            array('%target' => $gff_target_organism), WATCHDOG_WARNING);
+          $t_organism_id = '';                                   
+        }
+      }
+      else {
+        watchdog('T_gff3_loader', "The target_organism attribute is improperly formatted: %target. 
+          It should be target_organism=genus:species.", 
+          array('%target' => $gff_target_organism), WATCHDOG_WARNING);
+        $t_organism_id = '';                
+      }
+    }  
+  
+    // default the target type to be the value passed into the function, but if the GFF file
+    // species the target type then use that instead
+    $t_type_id = '';
+    if ($target_type) {
+      $values = array(
+        'name' => $target_type,
+        'cv_id' => array(
+           'name' => 'sequence',
+        )
+      );
+      $options = array('statement_name' => 'sel_cvterm_nacv');
+      $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
+      if (count($type) == 1) {
+        $t_type_id = $type[0]->cvterm_id;
+      }
+      else {
+        watchdog('T_gff3_loader', "The target type does not exist in the sequence ontology: %type. ", 
+          array('%type' => $target_type), WATCHDOG_ERROR);
+        exit;  
+      }
+    }
+    if ($gff_target_type) {
+      $values = array(
+        'name' => $gff_target_type,
+        'cv_id' => array(
+           'name' => 'sequence',
+        )
+      );
+      $options = array('statement_name' => 'sel_cvterm_nacv');
+      $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values, $options);
+      if (count($type) == 1) {
+        $t_type_id = $type[0]->cvterm_id;
+      }
+      else {
+        watchdog('T_gff3_loader', "The target_type attribute does not exist in the sequence ontology: %type. ", 
+          array('%type' => $gff_target_type), WATCHDOG_WARNING);
+        $t_type_id = '';
+      }
+    }                       
+    
+    // we want to add a featureloc record that uses the target feature as the srcfeature (landmark)
+    // and the landmark as the feature.
+    tripal_feature_load_gff3_featureloc($feature, $organism, $target_feature, $target_fmin, 
+      $target_fmax, $target_strand, $phase, $attr_fmin_partial, $attr_fmax_partial, $attr_residue_info, 
+      $attr_locgroup, $t_type_id, $t_organism_id, $create_target, TRUE); 
+  }
+  // the target attribute is not correctly formatted
+  else {
+    watchdog('T_gff3_loader', "Could not add 'Target' alignment as it is improperly formatted:  '%target'",
+      array('%target' => $tags['Target'][0]), WATCHDOG_ERROR);            
+  }
 }

+ 12 - 129
tripal_feature/tripal_feature.module

@@ -111,7 +111,7 @@ function tripal_feature_perm() {
     'create chado_feature content',
     'delete chado_feature content',
     'edit chado_feature content',
-    'manage chado_feature aggregator',
+    'administer tripal features',
   );
 }
 
@@ -159,7 +159,7 @@ function tripal_feature_menu() {
     'title' => 'Features',
     'description' => 'Basic Description of Tripal Organism Module Functionality',
     'page callback' => 'tripal_feature_module_description_page',
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer tripal features'),
     'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/tripal/tripal_feature/configuration'] = array(
@@ -167,7 +167,7 @@ function tripal_feature_menu() {
     'description' => 'Settings for Chado Features',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_feature_admin'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer tripal features'),
     'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/tripal/tripal_feature/fasta_loader'] = array(
@@ -175,7 +175,7 @@ function tripal_feature_menu() {
     'description' => 'Load sequences from a multi-FASTA file into Chado',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_feature_fasta_load_form'),
-    'access arguments' => array('administer site configuration'),
+    'access arguments' => array('administer tripal features'),
     'type' => MENU_NORMAL_ITEM,
   );
   $items['admin/tripal/tripal_feature/gff3_load'] = array(
@@ -183,7 +183,7 @@ function tripal_feature_menu() {
     'description' => 'Import a GFF3 file into Chado',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_feature_gff3_load_form'),
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal features'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -192,7 +192,7 @@ function tripal_feature_menu() {
     'description' => 'Delete multiple features from Chado',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_feature_delete_form'),
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal features'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -201,7 +201,7 @@ function tripal_feature_menu() {
     'description' => 'Sync features from Chado with Drupal',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_feature_sync_form'),
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal features'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -1925,127 +1925,9 @@ function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
   // we want to provide a new variable that contains the matched features.
   $feature = $variables['node']->feature;
    
-  // expand the feature object to include the feature relationships.
-  $options = array(
-    'return_array' => 1,
-    'order_by'     => array('rank' => 'ASC'),
-  );
-  $feature = tripal_core_expand_chado_vars($feature, 'table',
-    'feature_relationship', $options);
-
-  // get the subject relationships
-  $srelationships = $feature->feature_relationship->subject_id;
-  $orelationships = $feature->feature_relationship->object_id;
-  
-  // get alignment as child. The $feature->featureloc element
-  // is already populated from the alignment preprocess function
-  $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc');
-  $cfeaturelocs = $feature->featureloc->feature_id;
-  if (!$cfeaturelocs) {
-     $cfeaturelocs = array();
+  if (!$feature->all_relationships) {
+    $feature->all_relationships = tripal_feature_get_feature_relationships($feature);  
   } 
-  elseif (!is_array($cfeaturelocs)) { 
-     $cfeaturelocs = array($cfeaturelocs); 
-  }
-  
-  // prepare the SQL statement to get the featureloc for the 
-  // feature in the relationships. 
-  $connection = tripal_db_persistent_chado();
-  $psql = " 
-    PREPARE sel_featureloc_preprocess_relationships (int, int) AS 
-    SELECT 
-      FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id,
-      FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase
-    FROM featureloc FL 
-      INNER JOIN feature F ON F.feature_id = FL.srcfeature_id
-    WHERE FL.feature_id = $1 and FL.srcfeature_id = $2
-  ";
-  tripal_core_chado_prepare('sel_featureloc_preprocess_relationships', $psql, array('int', 'int'));
-
-  
-  // combine both object and subject relationshisp into a single array
-  $relationships = array();
-  $relationships['object'] = array();
-  $relationships['subject'] = array();
-  
-  // iterate through the object relationships
-  if ($orelationships) {
-    foreach ($orelationships as $relationship) {
-       $rel = new stdClass(); 
-       // get locations where the child feature and this feature overlap with the
-       // same landmark feature.
-       $rel->child_featurelocs = array();     
-       foreach ($cfeaturelocs as $featureloc) {
-          $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)", 
-            $relationship->subject_id->feature_id, 
-            $featureloc->srcfeature_id->feature_id);        
-          while ($loc = db_fetch_object($res)) {
-             // add in the node id of the src feature if it exists and save this location
-             $loc->nid = $featureloc->srcfeature_id->nid;
-             $rel->child_featurelocs[] = $loc;
-          }
-       }
-       $rel->record = $relationship;    
-       
-       // get the relationship and child types
-       $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
-       $child_type = $relationship->subject_id->type_id->name;
-       
-       // get the node id of the subject
-       $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
-       $n = db_fetch_object(db_query($sql, $relationship->subject_id->feature_id));
-       if ($n) {
-          $rel->record->nid = $n->nid;
-       }
-  
-       if (!array_key_exists($rel_type, $relationships['object'])) {
-         $relationships['object'][$rel_type] = array();   
-       }
-       if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
-         $relationships['object'][$rel_type][$child_type] = array();   
-       }
-       $relationships['object'][$rel_type][$child_type][] = $rel;     
-    }
-  }
-  
-  // now add in the subject relationships
-  if ($srelationships) {
-    foreach ($srelationships as $relationship) {
-       $rel = new stdClass(); 
-       // get locations where this feature overlaps with the parent
-       $rel->parent_featurelocs = array();     
-       foreach ($cfeaturelocs as $featureloc) {
-          $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)", 
-            $relationship->object_id->feature_id, 
-            $featureloc->srcfeature_id->feature_id);
-          while ($loc = db_fetch_object($res)) {
-             // add in the node id of the src feature if it exists and save this location
-             $loc->nid = $featureloc->srcfeature_id->nid;
-             $rel->parent_featurelocs[] = $loc;
-          }
-       }
-       $rel->record = $relationship;
-       $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
-       $parent_type = $relationship->object_id->type_id->name;
-       
-       // get the node id of the subject
-       $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
-       $n = db_fetch_object(db_query($sql, $relationship->object_id->feature_id));
-       if ($n) {
-          $rel->record->nid = $n->nid;
-       }
-       
-       if (!array_key_exists($rel_type, $relationships['subject'])) {
-         $relationships['subject'][$rel_type] = array();   
-       }
-       if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
-         $relationships['subject'][$rel_type][$parent_type] = array();   
-       }
-       $relationships['subject'][$rel_type][$parent_type][] = $rel;
-    }
-  }
-  $feature->all_relationships = $relationships;
-
 }
 /**
  *
@@ -2053,6 +1935,7 @@ function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
  * @ingroup tripal_feature
  */
 function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
+  
   // we want to provide a new variable that contains the matched features.
   $feature = $variables['node']->feature;
   $feature = tripal_core_expand_chado_vars($feature, 'table', 'featureloc');
@@ -2083,7 +1966,7 @@ function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
   $alignments = array();
   foreach ($pfeaturelocs as $featureloc) {
      // if type is a 'match' then ignore it. We will handle those below
-     if (preg_match('/(^match$|^.*?_match)$/', $featureloc->feature_id->type_id->name)) {
+     if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
         continue;
      }
      $alignment = new stdClass();
@@ -2099,7 +1982,7 @@ function tripal_feature_preprocess_tripal_feature_alignments(&$variables) {
   }
   foreach ($cfeaturelocs as $featureloc) {
      // if type is a 'match' then ignore it. We will handle those below
-     if (preg_match('/(^match$|^.*?_match)$/', $featureloc->feature_id->type_id->name)) {
+     if (preg_match('/(^match$|^.*?_match|match_part)$/', $featureloc->feature_id->type_id->name)) {
         continue;
      }
      $alignment = new stdClass();

+ 380 - 0
tripal_featuremap/includes/tripal_featuremap.admin.inc

@@ -0,0 +1,380 @@
+<?php
+/**
+ * Administrative settings form
+ *
+ * @ingroup tripal_featuremap
+ */
+function tripal_featuremap_admin() {
+  $form = array();
+
+  // before proceeding check to see if we have any
+  // currently processing jobs. If so, we don't want
+  // to give the opportunity to sync maps
+  $active_jobs = FALSE;
+  if (tripal_get_module_active_jobs('tripal_featuremap')) {
+    $active_jobs = TRUE;
+  }
+
+  // add the field set for syncing maps
+  if (!$active_jobs) {
+    get_tripal_featuremap_admin_form_sync_set($form);
+    get_tripal_featuremap_admin_form_cleanup_set($form);
+// TODO: complete coding of indexing and taxonomy assignment to features.    
+//    get_tripal_featuremap_admin_form_reindex_set($form);
+//    get_tripal_featuremap_admin_form_taxonomy_set($form);
+  }
+  else {
+    $form['notice'] = array(
+     '#type' => 'fieldset',
+     '#title' => t('Feature Map Management Temporarily Unavailable')
+    );
+    $form['notice']['message'] = array(
+        '#value' => t('Currently, feature map management jobs are waiting or are running. Managemment features have been hidden until these jobs complete.  Please check back later once these jobs have finished.  You can view the status of pending jobs in the Tripal jobs page.'),
+    );
+  }
+
+  return system_settings_form($form);
+}
+/**
+ *
+ *
+ * @ingroup tripal_featuremap
+ */
+function get_tripal_featuremap_admin_form_cleanup_set(&$form) {
+  $form['cleanup'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Clean Up')
+  );
+  $form['cleanup']['description'] = array(
+     '#type' => 'item',
+     '#value' => t("With Drupal and chado residing in different databases ".
+        "it is possible that nodes in Drupal and maps in Chado become ".
+        "\"orphaned\".  This can occur if an map node in Drupal is ".
+        "deleted but the corresponding chado map is not and/or vice ".
+        "versa. Click the button below to resolve these discrepancies."),
+     '#weight' => 1,
+  );
+  $form['cleanup']['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Clean up orphaned maps'),
+    '#weight' => 2,
+  );
+}
+
+/**
+ *
+ *
+ * @ingroup tripal_featuremap
+ */
+function get_tripal_featuremap_admin_form_taxonomy_set(&$form) {
+  $form['taxonify'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Assign Drupal Taxonomy to Map Features')
+  );
+
+  // get the list of maps
+  $sql = "SELECT * FROM {featuremap} ORDER BY name";
+  $lib_rset = chado_query($sql);
+
+  // iterate through all of the maps
+  $lib_boxes = array();
+  while ($featuremap = db_fetch_object($lib_rset)) {
+    $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
+  }
+
+  $form['taxonify']['description'] = array(
+     '#type' => 'item',
+     '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
+                   "nodes. These terms allow for advanced filtering during searching. This option allows ".
+                   "for setting taxonomy only for features that belong to the selected maps below.  All other features will be unaffected.  To set taxonomy for all features in the site see the Feature Administration page."),
+   '#weight' => 1,
+  );
+
+  $form['taxonify']['tx-maps'] = array(
+   '#title'       => t('Maps'),
+   '#type'        => t('checkboxes'),
+   '#description' => t("Check the maps whose features you want to reset taxonomy.  Note: this list contains all maps, even those that may not be synced."),
+   '#required'    => FALSE,
+   '#prefix'      => '<div id="lib_boxes">',
+   '#suffix'      => '</div>',
+   '#options'     => $lib_boxes,
+   '#weight'      => 2
+  );
+  $form['taxonify']['tx-button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Set Feature Taxonomy'),
+    '#weight'      => 3
+  );
+}
+/**
+ *
+ * @ingroup tripal_featuremap
+ */
+function get_tripal_featuremap_admin_form_reindex_set(&$form) {
+   // define the fieldsets
+  $form['reindex'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Reindex Map Features')
+  );
+
+  // get the list of maps
+  $sql = "SELECT * FROM {featuremap} ORDER BY name";
+  $lib_rset = chado_query($sql);
+
+  // iterate through all of the maps
+  $lib_boxes = array();
+  while ($featuremap = db_fetch_object($lib_rset)) {
+    $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
+  }
+  $form['reindex']['description'] = array(
+     '#type' => 'item',
+     '#value' => t("This option allows for reindexing of only those features that belong to the selected maps below. All other features will be unaffected.  To reindex all features in the site see the Feature Administration page."),
+   '#weight' => 1,
+  );
+
+  $form['reindex']['re-maps'] = array(
+   '#title'       => t('Maps'),
+   '#type'        => t('checkboxes'),
+   '#description' => t("Check the maps whoee features you want to reindex. Note: this list contains all maps, even those that may not be synced."),
+   '#required'    => FALSE,
+   '#prefix'      => '<div id="lib_boxes">',
+   '#suffix'      => '</div>',
+   '#options'     => $lib_boxes,
+   '#weight' => 2,
+  );
+  $form['reindex']['re-button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Reindex Features'),
+    '#weight' => 3,
+  );
+}
+/**
+ *
+ * @ingroup tripal_featuremap
+ */
+function get_tripal_featuremap_admin_form_sync_set(&$form) {
+   // define the fieldsets
+  $form['sync'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Sync Maps')
+  );
+
+
+  // get the list of maps
+  $sql = "SELECT * FROM {featuremap} ORDER BY name";
+  $lib_rset = chado_query($sql);
+
+  // if we've added any maps to the list that can be synced
+  // then we want to build the form components to allow the user
+  // to select one or all of them.  Otherwise, just present
+  // a message stating that all maps are currently synced.
+  $lib_boxes = array();
+  $added = 0;
+  while ($featuremap = db_fetch_object($lib_rset)) {
+    // check to see if the map is already present as a node in drupal.
+    // if so, then skip it.
+    $sql = "SELECT * FROM {chado_featuremap} WHERE featuremap_id = %d";
+    if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
+      $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
+      $added++;
+    }
+  }
+
+  // if we have maps we need to add to the checkbox then
+  // build that form element
+  if ($added > 0) {
+    $lib_boxes['all'] = "All Maps";
+
+    $form['reindex']['description'] = array(
+     '#type' => 'item',
+     '#value' => t("This option allows for the creation of Drupal content for maps in chado. Only the selected maps will be synced."),
+   '#weight' => 1,
+    );
+
+
+    $form['sync']['featuremaps'] = array(
+      '#title'       => t('Available Maps'),
+      '#type'        => t('checkboxes'),
+      '#description' => t("Check the maps you want to sync.  Drupal content will be created for each of the maps listed above.  Select 'All Maps' to sync all of them."),
+      '#required'    => FALSE,
+      '#prefix'      => '<div id="lib_boxes">',
+      '#suffix'      => '</div>',
+      '#options'     => $lib_boxes,
+    '#weight' => 2,
+    );
+    $form['sync']['button'] = array(
+       '#type' => 'submit',
+       '#value' => t('Sync Maps'),
+     '#weight' => 3,
+    );
+  }
+   // we don't have any maps to select from
+  else {
+    $form['sync']['value'] = array(
+       '#value' => t('All maps in Chado are currently synced with Drupal.')
+    );
+  }
+}
+/**
+ *
+ * @ingroup tripal_featuremap
+ */
+function tripal_featuremap_admin_validate($form, &$form_state) {
+  global $user;  // we need access to the user info
+  $job_args = array();
+
+  // Submit the Sync Job if selected
+  if ($form_state['values']['op'] == t('Sync Maps')) {
+
+    // check to see if the user wants to sync chado and drupal.  If
+    // so then we need to register a job to do so with tripal
+    $featuremaps = $form_state['values']['featuremaps'];
+    $do_all = FALSE;
+    $to_sync = array();
+
+  foreach ($featuremaps as $featuremap_id) {
+    if (preg_match("/^all$/i", $featuremap_id)) {
+      $do_all = TRUE;
+    }
+    if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
+      // get the map info
+      $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
+      $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
+      $to_sync[$featuremap_id] = $featuremap->name;
+    }
+  }
+
+  // submit the job to the tripal job manager
+  if ($do_all) {
+    tripal_add_job('Sync all maps', 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
+  }
+  else{
+    foreach ($to_sync as $featuremap_id => $name) {
+      $job_args[0] = $featuremap_id;
+      tripal_add_job("Sync map: $name", 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
+      }
+    }
+  }
+
+  // -------------------------------------
+  // Submit the Reindex Job if selected
+  if ($form_state['values']['op'] == t('Reindex Features')) {
+    $featuremaps = $form_state['values']['re-maps'];
+    foreach ($featuremaps as $featuremap_id) {
+      if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
+        // get the map info
+        $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
+        $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
+        $job_args[0] = $featuremap_id;
+        tripal_add_job("Reindex features for map: $featuremap->name", 'tripal_featuremap',
+         'tripal_featuremap_reindex_features', $job_args, $user->uid);
+      }
+    }
+  }
+
+  // -------------------------------------
+  // Submit the Taxonomy Job if selected
+  if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
+    $featuremaps = $form_state['values']['tx-maps'];
+    foreach ($featuremaps as $featuremap_id) {
+      if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
+        // get the map info
+        $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
+        $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
+        $job_args[0] = $featuremap_id;
+        tripal_add_job("Set taxonomy for features in map: $featuremap->name", 'tripal_featuremap',
+         'tripal_featuremap_taxonify_features', $job_args, $user->uid);
+      }
+    }
+  }
+    // -------------------------------------
+    // Submit the Cleanup Job if selected
+    if ($form_state['values']['op'] == t('Clean up orphaned maps')) {
+      tripal_add_job('Cleanup orphaned maps', 'tripal_featuremap',
+         'tripal_featuremap_cleanup', $job_args, $user->uid);
+    }
+}
+
+/**
+ *
+ *
+ * @ingroup tripal_featuremap
+ */
+function tripal_featuremap_sync_featuremaps($featuremap_id = NULL, $job_id = NULL) {
+
+  global $user;
+  $page_content = '';
+
+  // get the list of featuremaps and create new nodes
+  if (!$featuremap_id) {
+    $sql = "SELECT * FROM {featuremap} L";
+    $results = chado_query($sql);
+  }
+  else {
+    $sql = "SELECT * FROM {featuremap} L WHERE featuremap_id = %d";
+    $results = chado_query($sql, $featuremap_id);
+  }
+
+  // We'll use the following SQL statement for checking if the map
+  // already exists as a drupal node.
+  $sql = "SELECT * FROM {chado_featuremap} ".
+        "WHERE featuremap_id = %d";
+
+  while ($featuremap = db_fetch_object($results)) {
+
+    // check if this map already exists in the drupal database. if it
+    // does then skip this map and go to the next one.
+    if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
+
+    $new_node = new stdClass();
+    $new_node->type = 'chado_featuremap';
+    $new_node->uid = $user->uid;
+    $new_node->title = "$featuremap->name";
+    $new_node->featuremap_id = $featuremap->featuremap_id;
+
+    node_validate($new_node);
+    $errors = form_get_errors();
+    if (!$errors) {
+      $node = node_submit($new_node);
+      node_save($node);
+      if ($node->nid) {
+        print "Added " . $featuremap->name . "\n";
+      }
+      else {
+        print "ERROR: Unable to create " . $featuremap->name . "\n";
+      }
+    }
+    else {
+      print "ERROR: Unable to create " . $featuremap->name . "\n" . print_r($errors, TRUE) . "\n";
+    }
+    }
+    else {
+      print "Skipped " . $featuremap->name . "\n";
+    }
+  }
+  return $page_content;
+}
+
+/**
+ * Remove orphaned drupal nodes
+ *
+ * @param $dummy
+ *   Not Used -kept for backwards compatibility
+ * @param $job_id
+ *   The id of the tripal job executing this function
+ *
+ * @ingroup tripal_featuremap
+ */
+function tripal_featuremap_cleanup($dummy = NULL, $job_id = NULL) {
+
+  return tripal_core_clean_orphaned_nodes('featuremap', $job_id);
+  
+}
+/**
+ * Add the map as a taxonomy term for associating with map_features
+ *
+ * @ingroup tripal_featuremap
+ */
+function tripal_featuremap_add_taxonomy($node, $featuremap_id) {
+
+}

+ 51 - 0
tripal_featuremap/theme/tripal_featuremap_admin.tpl.php

@@ -0,0 +1,51 @@
+<h3>Tripal Feature Map Administrative Tools Quick Links:</h3>
+<ul>
+  <li><a href="<?php print url('admin/tripal/tripal_featuremap/configuration') ?>">Map Configuration</a></li>
+</ul>
+
+<h3>Module Description:</h3>
+<p>The Tripal Map module is an interface for the Chado Map module which groups features (sequences) into 
+   maps (typically genetic maps).
+   This module provides support for visualization of "map" pages, editing and updating.
+</p>
+
+<h3>Setup Instructions:</h3>
+<ol>
+  <li><p>
+    <b>Set Permissions</b>: The map module supports the Drupal user permissions interface for
+    controlling access to map content and functions. These permissions include viewing,
+    creating, editing or administering of
+    map content. The default is that only the  site administrator has these
+    permissions.  You can <a href="<?php print url('admin/user/roles') ?>">add roles</a> for classifying users,
+    <a href="<?php print url('admin/user/user') ?>">assign users to roles</a> and
+    <a href="<?php print url('admin/user/permissions') ?>">assign permissions</a> for the map content to
+    those roles.  For a simple setup, allow anonymous users access to view map content and
+    allow the site administrator all other permissions.
+  </p></li>  
+</ol>
+
+
+<h3>Features of this Module:</h3>
+<ul>
+  <li><p><b>Add a map</b>: Maps can be manually created <?php  print l('here', 'node/add/chado-featuremap') ?><p></li>
+  <li><p><b>Edit or Delete Maps</b>: Maps can be manually edited or deleted by navigating to the map page and clicking the "Edit" button.<p></li>  
+  <li><p><b>Sync any Existing Maps</b>: Before Maps can be viewed on the website, they must first be <?php print l('created manually', 'node/add/chado-featuremap')  ?> 
+            or if they already exist in the Chado database they must be synced. Near the top of the <?php print l('Map Configuration page', 'admin/tripal/tripal_featuremap/configuration') ?>
+            there is
+            a Sync Maps section which provides list of maps currently in chado which can be sync\'d.
+            Simply select the maps you would like to create Drupal/Tripal pages for and click Sync Maps. Once synced, 
+            pages will appear on the site for maps.</p></li>
+  <li><p><b>Integration with Drupal Views</b>: <a href="http://drupal.org/project/views">Drupal Views</a> is
+            a powerful tool that allows the site administrator to create lists or basic search forms of Chado content.
+            It provides a graphical interface within Drupal to allow the site admin to directly query the Chado database
+            and create custom lists without PHP programming or customization of Tripal source code.  Views can also
+            be created to filter out records that has not yet been synced with Drupal in order to protect access to non
+            published data (only works if Chado was installed using Tripal).  You can see a list of available pre-existing
+            Views <a href="<?php print url('admin/build/views/') ?>">here</a>, as well as create your own. </p></li>
+</ul>
+
+<h3>Page Customizations</h3>
+<p>There are several ways to customize the look-and-feel for the way Chado data is presented through Tripal.
+  Below is a description of several methods.  These methods may be used in conjunction with one another to
+  provide fine-grained control.  See the <a href="http://www.gmod.org/wiki/Tripal_Developer's_Handbook">Developers Handbook</a> for further infromation to customize Map pages.
+</p>

+ 21 - 0
tripal_featuremap/tripal_featuremap.install

@@ -16,6 +16,9 @@ function tripal_featuremap_install() {
   // create the tables that correlate drupal nodes with chado
   // features, maps, etc....
   drupal_install_schema('tripal_featuremap');
+  
+  // Add cvterms
+  tripal_featuremap_add_cvterms();
 
 }
 
@@ -105,4 +108,22 @@ function tripal_featuremap_requirements($phase) {
     }
   }
   return $requirements;
+}
+
+/*
+ * 
+ */
+function tripal_featuremap_add_cvterms() {
+  
+   // add cvterms for the map unit types
+   tripal_cv_add_cvterm(array('name' => 'cM','def' => 'Centimorgan units'), 
+     'tripal_featuremap', 0, 1, 'tripal');
+   tripal_cv_add_cvterm(array('name' => 'bp','def' => 'Base pairs units'), 
+     'tripal_featuremap', 0, 1, 'tripal');
+   tripal_cv_add_cvterm(array('name' => 'bin_unit','def' => 'The bin unit'), 
+     'tripal_featuremap', 0, 1, 'tripal');   
+   tripal_cv_add_cvterm(array('name' => 'marker_order','def' => 'Units simply to define marker order.'), 
+     'tripal_featuremap', 0, 1, 'tripal');
+   tripal_cv_add_cvterm(array('name' => 'undefined','def' => 'A catch-all for an undefined unit type'), 
+     'tripal_featuremap', 0, 1, 'tripal');
 }

+ 90 - 570
tripal_featuremap/tripal_featuremap.module

@@ -9,6 +9,7 @@
  */
 
 require('api/tripal_featuremap.api.inc');
+require('includes/tripal_featuremap.admin.inc');
 
 /**
  * Display help and module information
@@ -65,6 +66,7 @@ function tripal_featuremap_perm() {
     'create chado_featuremap content',
     'delete chado_featuremap content',
     'edit chado_featuremap content',
+    'administer tripal featuremap',
   );
 }
 /**
@@ -104,12 +106,14 @@ function chado_featuremap_access($op, $node, $account) {
  */
 function tripal_featuremap_menu() {
   $items = array();
+  
   // The administative settings menu
   $items['admin/tripal/tripal_featuremap'] = array(
     'title' => 'Maps',
     'description' => 'Basic Description of Tripal Map Module Functionality',
-    'page callback' => 'tripal_featuremap_module_description_page',
-    'access arguments' => array('access administration pages'),
+    'page callback' => 'theme',
+    'page arguments' => array('tripal_featuremap_admin'),
+    'access arguments' => array('administer tripal featuremap'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -118,7 +122,7 @@ function tripal_featuremap_menu() {
     'description' => 'Manage integration of Chado maps including associated features.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_featuremap_admin'),
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal featuremap'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -126,19 +130,10 @@ function tripal_featuremap_menu() {
   $items['chado_sync_featuremaps'] = array(
     'title' => 'Sync Data',
     'page callback' => 'tripal_featuremap_sync_featuremaps',
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal featuremap'),
     'type' => MENU_CALLBACK
   );
   
-  // Displaying maps
-  $items['featuremaps'] = array(
-    'menu_name' => ('primary-links'), //Enable the 'Map' primary link
-    'title' => 'Feature Maps',
-    'page callback' => 'tripal_featuremap_show_maps',
-    'access arguments' => array('access chado_featuremap content'),
-    'type' => MENU_NORMAL_ITEM
-  );
-
   return $items;
 }
 
@@ -156,149 +151,7 @@ function tripal_featuremap_views_api() {
   );
 }
 
-/**
- * Purpose: Provide Guidance to new Tripal Admin
- *
- * @return HTML Formatted text
- *
- * @ingroup tripal_featuremap
- */
-function tripal_featuremap_module_description_page() {
-  $text = '';
-  $text .= '<h3>Tripal Feature Map Administrative Tools Quick Links:</h3>';
-  $text .= "<ul>
-             <li><a href=\"" . url("admin/tripal/tripal_featuremap/configuration") . "\">Map Configuration</a></li>
-           </ul>";
-  $text .= '<h3>Module Description:</h3>';
-  $text .= '<p>The Tripal Map module is an interface for the Chado Map module which groups features (sequences) into genetic maps.
-    This module provides support for visualization of "map" pages, editing and updating.</p>';
-
-  $text .= '<h3>Setup Instructions:</h3>';
-  $text .= '<ol>';
-  $text .= '<li><p><b>Set Permissions</b>: The map module supports the Drupal user permissions interface for
-               controlling access to map content and functions. These permissions include viewing,
-               creating, editing or administering of
-               map content. The default is that only the original site administrator has these
-               permissions.  You can <a href="' . url('admin/user/roles') . '">add roles</a> for classifying users,
-               <a href="' . url('admin/user/user') . '">assign users to roles</a> and
-               <a href="' . url('admin/user/permissions') . '">assign permissions</a> for the map content to
-               those roles.  For a simple setup, allow anonymous users access to view organism content and
-               allow the site administrator all other permissions.</p></li>';
-  $text .= '<li><p><b>Sync any Existing Maps</b>: Near the top of the ' . l('Map Configuration page', 'admin/tripal/tripal_featuremap/configuration') . ' there is
-              a Sync Maps section which provides list of maps currently in chado which can be sync\'d.
-              Simply select the maps you would like to create Drupal/Tripal pages for and click Sync Maps.</p></li>';
-  $text .= '</ol>';
-
-
-  $text .= '<h3>Features of this Module:</h3>';
-  $text .= '<ul>';
-    $text .= '<li><b>Add/Edit/Delete Maps</b>: Maps with no associated features can be created ' . l('here', 'node/add/chado-map') . ' but it is
-              recommended to create the map using the feature loader. For example, when you load FASTA files using the Tripal loader you are
-              given the option of specifying a map for all created features. Existing Maps (regardless of the method used to create them) can be
-              edited or deleted by clicking the Edit tab at the top of the Map Page.</li>';
-    $text .= '<li><p><b>Integration with Drupal Views</b>: <a href="http://drupal.org/project/views">Drupal Views</a> is
-              a powerful tool that allows the site administrator to create lists or basic searching forms of Chado content.
-              It provides a graphical interface within Drupal to allow the site admin to directly query the Chado database
-              and create custom lists without PHP programming or customization of Tripal source code.  Views can also
-              be created to filter content that has not yet been synced with Druapl in order to protect access to non
-              published data (only works if Chado was installed using Tripal).  You can see a list of available pre-existing
-              Views <a href="' . url('admin/build/views/') . '">here</a>, as well as create your own. </p></li>';
-    $text .= '<li><b>Basic Listing</b>: This module provides a basic <a href="' . url('maps') . '">map display
-              tool</a> for finding or listing maps in Chado. It does not require indexing for Drupal searching but relies
-              on Drupal Views.  <a href="http://drupal.org/project/views">Drupal Views</a> must be installed.</li>';
-  $text .= '</ul>';
-
-  $text .= '<h3>Page Customizations</h3>';
-  $text .= '<p>There are several ways to customize the look-and-feel for the way Chado data is presented through Tripal.
-             Below is a description of several methods.  These methods may be used in conjunction with one another to
-             provide fine-grained control.
-             <ul>
-
-             <li><p><b>Integration with Drupal Panels</b>:  <a href="http://drupal.org/project/views">Drupal Panels</a>
-              allows for customization of a page layout if you don\'t want to do PHP/Javascript/CSS programming.  Tripal comes with pre-set layouts for map pages.  However,
-              Panels become useful if you prefer a layout that is different from the pre-set layouts.  Chado content
-              is provided to Panels in the form of Drupal "blocks" which you can then place anywhere on a page using the
-              Panel\'s GUI.</p></li>
-
-             <li><p><b>Drupal\'s Content Construction Kit (CCK)</b>: the
-             <a href="http://drupal.org/project/cck">Content Construction Kit (CCK) </a> is a powerful way to add non-Chado content
-             to any page without need to edit template files or knowing PHP.  You must first download and install CCK.
-             With CCK, the site administartor can create a new field to appear on the page.  For example, currently,
-             the Chado publication module is not yet supported by Tripal.  Therefore, the site administrator can add a text
-             field to the map pages.  This content is not stored in Chado, but will appear on the map page.  A field
-             added by CCK will also appear in the form when editing a map to allow users to manually enter the appropriate
-             text.  If the default pre-set layout and themeing for Tripal is used, it is better to create the CCK element,
-             indicate that it is not to be shown (using the CCK interface), then manually add the new content type
-             where desired by editing the templates (as described below).  If using Panels, the CCK field can be added to the
-             location desired using the Panels interface.</p></li>
-
-             <li><p><b>Drupal Node Templates</b>:  The Tripal packages comes with a "theme_tripal" directory that contains the
-             themeing for Chado content.    The map module has a template file for map "nodes" (Tripal map pages).  This file
-             is named "node-chado_featuremap.tpl.php", and provides javascript, HTML and PHP code for display of the map
-             pages.  You can edit this file to control which types of information (or which map "blocks") are displayed for maps. Be sure to
-             copy these template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
-             future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
-             for instructions on how to access variables and other Chado content within the template file.</p></li>
-
-             <li><p><b>Map "Block" Templates</b>:  In the "theme_tripal" directory is a subdirectory named "tripal_featuremap".
-             Inside this directory is a set of templates that control distinct types of information for maps.  For example,
-             there is a "base" template for displaying of data directly from the Chado map table.  These templates are used both by Drupal blocks
-             for use in Drupal Panels (as described above) or for use in the default pre-set layout that the node template
-             provides (also desribed above).  You can customize this template as you desire.  Be sure to copy the
-             template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
-             future Tripal updates may overwrite your customizations.  See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
-             for instructions on how to access variables and other Chado content within the template files.</p></li>
-             </li>
-
-             <li><p><b>Adding Links to the "Resources" Sidebar</b>: If you use the pre-set default Tripal layout for theming, you
-             will see a "Resources" sidebar on each page.  The links that appear on the sidebar are automatically generated
-             using Javascript for all of the map "Blocks" that appear on the page. If you want to add additional links
-             (e.g. a link to a views table showing all features of the current map) and you want that link to appear in the
-             "Resources" sidebar, simply edit the Drupal Node Template (as described above) and add the link to the
-             section at the bottom of the template file where the resources section is found.</p></li>
-
-             </ul>
-             </p>';
-
-  return $text;
-}
 
-/**
- * Administrative settings form
- *
- * @ingroup tripal_featuremap
- */
-function tripal_featuremap_admin() {
-  $form = array();
-
-  // before proceeding check to see if we have any
-  // currently processing jobs. If so, we don't want
-  // to give the opportunity to sync maps
-  $active_jobs = FALSE;
-  if (tripal_get_module_active_jobs('tripal_featuremap')) {
-    $active_jobs = TRUE;
-  }
-
-  // add the field set for syncing maps
-  if (!$active_jobs) {
-    get_tripal_featuremap_admin_form_sync_set($form);
-    get_tripal_featuremap_admin_form_cleanup_set($form);
-    get_tripal_featuremap_admin_form_reindex_set($form);
-    get_tripal_featuremap_admin_form_taxonomy_set($form);
-    get_tripal_featuremap_admin_form_cleanup_set($form);
-  }
-  else {
-    $form['notice'] = array(
-     '#type' => 'fieldset',
-     '#title' => t('Feature Map Management Temporarily Unavailable')
-    );
-    $form['notice']['message'] = array(
-        '#value' => t('Currently, feature map management jobs are waiting or are running. Managemment features have been hidden until these jobs complete.  Please check back later once these jobs have finished.  You can view the status of pending jobs in the Tripal jobs page.'),
-    );
-  }
-
-  return system_settings_form($form);
-}
 
 /**
  * Implementation of hook_nodeapi().
@@ -350,6 +203,11 @@ function tripal_featuremap_theme() {
       'arguments' => array('node' => NULL),
       'template' => 'tripal_featuremap_properties',
     ),
+    'tripal_featuremap_admin' => array(
+      'template' => 'tripal_featuremap_admin',  
+      'arguments' =>  array(NULL),  
+      'path' => drupal_get_path('module', 'tripal_featuremap') . '/theme' 
+    ),
   );
 }
 
@@ -363,267 +221,6 @@ function tripal_featuremap_theme() {
 function theme_tripal_featuremap_search_index($node) {
 }
 
-
-/**
- *
- *
- * @ingroup tripal_featuremap
- */
-function get_tripal_featuremap_admin_form_cleanup_set(&$form) {
-  $form['cleanup'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Clean Up')
-  );
-  $form['cleanup']['description'] = array(
-     '#type' => 'item',
-     '#value' => t("With Drupal and chado residing in different databases ".
-        "it is possible that nodes in Drupal and maps in Chado become ".
-        "\"orphaned\".  This can occur if an map node in Drupal is ".
-        "deleted but the corresponding chado map is not and/or vice ".
-        "versa. Click the button below to resolve these discrepancies."),
-     '#weight' => 1,
-  );
-  $form['cleanup']['button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Clean up orphaned maps'),
-    '#weight' => 2,
-  );
-}
-
-/**
- *
- *
- * @ingroup tripal_featuremap
- */
-function get_tripal_featuremap_admin_form_taxonomy_set(&$form) {
-  $form['taxonify'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Assign Drupal Taxonomy to Map Features')
-  );
-
-  // get the list of maps
-  $sql = "SELECT * FROM {featuremap} ORDER BY name";
-  $lib_rset = chado_query($sql);
-
-  // iterate through all of the maps
-  $lib_boxes = array();
-  while ($featuremap = db_fetch_object($lib_rset)) {
-    $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
-  }
-
-  $form['taxonify']['description'] = array(
-     '#type' => 'item',
-     '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
-                   "nodes. These terms allow for advanced filtering during searching. This option allows ".
-                   "for setting taxonomy only for features that belong to the selected maps below.  All other features will be unaffected.  To set taxonomy for all features in the site see the Feature Administration page."),
-   '#weight' => 1,
-  );
-
-  $form['taxonify']['tx-maps'] = array(
-   '#title'       => t('Maps'),
-   '#type'        => t('checkboxes'),
-   '#description' => t("Check the maps whose features you want to reset taxonomy.  Note: this list contains all maps, even those that may not be synced."),
-   '#required'    => FALSE,
-   '#prefix'      => '<div id="lib_boxes">',
-   '#suffix'      => '</div>',
-   '#options'     => $lib_boxes,
-   '#weight'      => 2
-  );
-  $form['taxonify']['tx-button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Set Feature Taxonomy'),
-    '#weight'      => 3
-  );
-}
-/**
- *
- * @ingroup tripal_featuremap
- */
-function get_tripal_featuremap_admin_form_reindex_set(&$form) {
-   // define the fieldsets
-  $form['reindex'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Reindex Map Features')
-  );
-
-  // get the list of maps
-  $sql = "SELECT * FROM {featuremap} ORDER BY name";
-  $lib_rset = chado_query($sql);
-
-  // iterate through all of the maps
-  $lib_boxes = array();
-  while ($featuremap = db_fetch_object($lib_rset)) {
-    $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
-  }
-  $form['reindex']['description'] = array(
-     '#type' => 'item',
-     '#value' => t("This option allows for reindexing of only those features that belong to the selected maps below. All other features will be unaffected.  To reindex all features in the site see the Feature Administration page."),
-   '#weight' => 1,
-  );
-
-  $form['reindex']['re-maps'] = array(
-   '#title'       => t('Maps'),
-   '#type'        => t('checkboxes'),
-   '#description' => t("Check the maps whoee features you want to reindex. Note: this list contains all maps, even those that may not be synced."),
-   '#required'    => FALSE,
-   '#prefix'      => '<div id="lib_boxes">',
-   '#suffix'      => '</div>',
-   '#options'     => $lib_boxes,
-   '#weight' => 2,
-  );
-  $form['reindex']['re-button'] = array(
-    '#type' => 'submit',
-    '#value' => t('Reindex Features'),
-    '#weight' => 3,
-  );
-}
-/**
- *
- * @ingroup tripal_featuremap
- */
-function get_tripal_featuremap_admin_form_sync_set(&$form) {
-   // define the fieldsets
-  $form['sync'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Sync Maps')
-  );
-
-
-  // get the list of maps
-  $sql = "SELECT * FROM {featuremap} ORDER BY name";
-  $lib_rset = chado_query($sql);
-
-  // if we've added any maps to the list that can be synced
-  // then we want to build the form components to allow the user
-  // to select one or all of them.  Otherwise, just present
-  // a message stating that all maps are currently synced.
-  $lib_boxes = array();
-  $added = 0;
-  while ($featuremap = db_fetch_object($lib_rset)) {
-    // check to see if the map is already present as a node in drupal.
-    // if so, then skip it.
-    $sql = "SELECT * FROM {chado_featuremap} WHERE featuremap_id = %d";
-    if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
-      $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
-      $added++;
-    }
-  }
-
-  // if we have maps we need to add to the checkbox then
-  // build that form element
-  if ($added > 0) {
-    $lib_boxes['all'] = "All Maps";
-
-    $form['reindex']['description'] = array(
-     '#type' => 'item',
-     '#value' => t("This option allows for the creation of Drupal content for maps in chado. Only the selected maps will be synced."),
-   '#weight' => 1,
-    );
-
-
-    $form['sync']['featuremaps'] = array(
-      '#title'       => t('Available Maps'),
-      '#type'        => t('checkboxes'),
-      '#description' => t("Check the maps you want to sync.  Drupal content will be created for each of the maps listed above.  Select 'All Maps' to sync all of them."),
-      '#required'    => FALSE,
-      '#prefix'      => '<div id="lib_boxes">',
-      '#suffix'      => '</div>',
-      '#options'     => $lib_boxes,
-    '#weight' => 2,
-    );
-    $form['sync']['button'] = array(
-       '#type' => 'submit',
-       '#value' => t('Sync Maps'),
-     '#weight' => 3,
-    );
-  }
-   // we don't have any maps to select from
-  else {
-    $form['sync']['value'] = array(
-       '#value' => t('All maps in Chado are currently synced with Drupal.')
-    );
-  }
-}
-/**
- *
- * @ingroup tripal_featuremap
- */
-function tripal_featuremap_admin_validate($form, &$form_state) {
-  global $user;  // we need access to the user info
-  $job_args = array();
-
-  // Submit the Sync Job if selected
-  if ($form_state['values']['op'] == t('Sync Maps')) {
-
-    // check to see if the user wants to sync chado and drupal.  If
-    // so then we need to register a job to do so with tripal
-    $featuremaps = $form_state['values']['featuremaps'];
-    $do_all = FALSE;
-    $to_sync = array();
-
-  foreach ($featuremaps as $featuremap_id) {
-    if (preg_match("/^all$/i", $featuremap_id)) {
-      $do_all = TRUE;
-    }
-    if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
-      // get the map info
-      $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
-      $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
-      $to_sync[$featuremap_id] = $featuremap->name;
-    }
-  }
-
-  // submit the job to the tripal job manager
-  if ($do_all) {
-    tripal_add_job('Sync all maps', 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
-  }
-  else{
-    foreach ($to_sync as $featuremap_id => $name) {
-      $job_args[0] = $featuremap_id;
-      tripal_add_job("Sync map: $name", 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
-      }
-    }
-  }
-
-  // -------------------------------------
-  // Submit the Reindex Job if selected
-  if ($form_state['values']['op'] == t('Reindex Features')) {
-    $featuremaps = $form_state['values']['re-maps'];
-    foreach ($featuremaps as $featuremap_id) {
-      if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
-        // get the map info
-        $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
-        $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
-        $job_args[0] = $featuremap_id;
-        tripal_add_job("Reindex features for map: $featuremap->name", 'tripal_featuremap',
-         'tripal_featuremap_reindex_features', $job_args, $user->uid);
-      }
-    }
-  }
-
-  // -------------------------------------
-  // Submit the Taxonomy Job if selected
-  if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
-    $featuremaps = $form_state['values']['tx-maps'];
-    foreach ($featuremaps as $featuremap_id) {
-      if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
-        // get the map info
-        $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
-        $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
-        $job_args[0] = $featuremap_id;
-        tripal_add_job("Set taxonomy for features in map: $featuremap->name", 'tripal_featuremap',
-         'tripal_featuremap_taxonify_features', $job_args, $user->uid);
-      }
-    }
-  }
-    // -------------------------------------
-    // Submit the Cleanup Job if selected
-    if ($form_state['values']['op'] == t('Clean up orphaned maps')) {
-      tripal_add_job('Cleanup orphaned maps', 'tripal_featuremap',
-         'tripal_featuremap_cleanup', $job_args, $user->uid);
-    }
-}
-
 /**
  *
  * @ingroup tripal_featuremap
@@ -632,9 +229,6 @@ function tripal_featuremap_cron() {
 
 }
 /**
- *
- *                  CHADO_LIBRARY NODE FUNCTIONS
- *
  *  The following function proves access control for users trying to
  *  perform actions on data managed by this module
  *
@@ -670,31 +264,95 @@ function tripal_featuremap_map_access($op, $node, $account) {
   }
   return NULL;
 }
+
+/**
+ *  When editing or creating a new node of type 'chado_featuremap' we need
+ *  a form.  This function creates the form that will be used for this.
+ *
+ * @ingroup tripal_featuremap
+ */
+function chado_featuremap_form($node) {
+  $type = node_get_types('type', $node);  
+  $form = array();
+
+  $featuremap = $node->featuremap;
+
+  // keep track of the map id if we have.  If we do have one then
+  // this is an update as opposed to an insert.
+  $form['featuremap_id'] = array(
+    '#type' => 'value',
+    '#value' => $featuremap->featuremap_id,
+  );
+
+  $form['title']= array(
+    '#type'          => 'textfield',
+    '#title'         => t('Map Name'),
+    '#description'   => t('Please enter a name for this map'),
+    '#required'      => TRUE,
+    '#default_value' => $featuremap->name,
+  );
+
+  $form['description']= array(
+    '#type'          => 'textarea',
+    '#title'         => t('Map Description'),
+    '#description'   => t('A description of the map.'),
+    '#required'      => TRUE,
+    '#default_value' => $featuremap->description,
+  );
+  
+  // get the list of unit types
+  $values = array(
+    'cv_id' => array(
+      'name' => 'tripal_featuremap',
+    )
+  );
+  $columns = array('cvterm_id','name');
+  $options = array('order_by' => array('name' => 'ASC'));
+  $featuremap_units = tripal_core_chado_select('cvterm', $columns, $values, $options);
+  $units = array();
+  $units[''] = '';
+  foreach($featuremap_units as $unit) {
+    $units[$unit->cvterm_id] = $unit->name;
+  }
+
+  $form['unittype_id'] = array(
+    '#title'       => t('Map Units'),
+    '#type'        => t('select'),
+    '#description' => t("Chose the units for this map"),
+    '#required'    => TRUE,
+    '#default_value' => $featuremap->unittype_id->cvterm_id,
+    '#options'     => $units,
+  );
+
+  return $form;
+}
 /**
  *  validates submission of form when adding or updating a map node
  *
  * @ingroup tripal_featuremap
  */
 function chado_featuremap_validate($node) {
-  $lib = 0;
+  $map = 0;
   // check to make sure the unique name on the map is unique
-  // before we try to insert into chado.
+  // before we try to insert into chado. If this is an update then we will
+  // have a featuremap_id, therefore we want to look for another map with this 
+  // name but with a different featuremap_id. If this is an insert, just look
+  // for a case where the name already exists.
   if ($node->featuremap_id) {
     $sql = "SELECT * FROM ".
            "{featuremap} WHERE ".
            "name = '%s' ".
            "AND NOT featuremap_id = %d";
-    $lib = db_fetch_object(chado_query($sql, $node->name, $node->featuremap_id));
+    $map = db_fetch_object(chado_query($sql, $node->title, $node->featuremap_id));
   }
   else {
     $sql = "SELECT * FROM ".
            "{featuremap} ".
            "WHERE name = '%s'";
-    $lib = db_fetch_object(chado_query($sql, $node->name));
+    $map = db_fetch_object(chado_query($sql, $node->title));
   }
-  if ($lib) {
-    form_set_error('name', t('The unique map name already exists. '.
-                                  'Please choose another'));
+  if ($map) {
+    form_set_error('name', t('The unique map name already exists. Please choose another'));
   }
 }
 
@@ -714,6 +372,7 @@ function chado_featuremap_insert($node) {
     $values = array(
       'name' => $node->title,
       'description' => $node->description,
+      'unittype_id' => $node->unittype_id
     );
     $featuremap = tripal_core_chado_insert('featuremap', $values);
   }
@@ -752,138 +411,14 @@ function chado_featuremap_update($node) {
 
   // update the map record
   $match = array(
-     'featuremap_id' => $featuremap_id,
+    'featuremap_id' => $featuremap_id,
   );
   $values = array(
-     'name' => $node->title,
+    'name' => $node->title,
+    'unittype_id' => $node->unittype_id
   );
   $status = tripal_core_chado_update('featuremap', $match, $values);  
 }
-
-
-/**
- * Add the map as a taxonomy term for associating with map_features
- *
- * @ingroup tripal_featuremap
- */
-function tripal_featuremap_add_taxonomy($node, $featuremap_id) {
-
-}
-
-/**
- *  When editing or creating a new node of type 'chado_featuremap' we need
- *  a form.  This function creates the form that will be used for this.
- *
- * @ingroup tripal_featuremap
- */
-function chado_featuremap_form($node) {
-  $form = array();
-
-  $featuremap = $node->featuremap;
-
-  // get the default values
-  $name = $node->name;
-  if (!$name) {
-    $name = $featuremap->name;
-  }
-
-  // keep track of the map id if we have.  If we do have one then
-  // this is an update as opposed to an insert.
-  $form['featuremap_id'] = array(
-    '#type' => 'value',
-    '#value' => $featuremap->featuremap_id,
-  );
-
-  $form['title']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('Map Title'),
-    '#description'   => t('Please enter the title for this map. '.
-                          'This appears at the top of the map page.'),
-    '#required'      => TRUE,
-    '#default_value' => $node->title,
-    '#weight'        => 1
-  );
-
-  $form['name']= array(
-    '#type'          => 'textfield',
-    '#title'         => t('Map Name'),
-    '#description'   => t('Please enter a name for this map'),
-    '#required'      => TRUE,
-    '#default_value' => $name,
-    '#weight'        => 2
-  );
-
-  $form['description']= array(
-    '#type'          => 'textarea',
-    '#title'         => t('Map Description'),
-    '#description'   => t('A brief description of the map'),
-    '#required'      => TRUE,
-    '#default_value' => $description,
-    '#weight'        => 5
-  );
-
-  return $form;
-}
-/**
- *
- *
- * @ingroup tripal_featuremap
- */
-function tripal_featuremap_sync_featuremaps($featuremap_id = NULL, $job_id = NULL) {
-
-  global $user;
-  $page_content = '';
-
-  // get the list of featuremaps and create new nodes
-  if (!$featuremap_id) {
-    $sql = "SELECT * FROM {featuremap} L";
-    $results = chado_query($sql);
-  }
-  else {
-    $sql = "SELECT * FROM {featuremap} L WHERE featuremap_id = %d";
-    $results = chado_query($sql, $featuremap_id);
-  }
-
-  // We'll use the following SQL statement for checking if the map
-  // already exists as a drupal node.
-  $sql = "SELECT * FROM {chado_featuremap} ".
-        "WHERE featuremap_id = %d";
-
-  while ($featuremap = db_fetch_object($results)) {
-
-    // check if this map already exists in the drupal database. if it
-    // does then skip this map and go to the next one.
-    if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
-
-    $new_node = new stdClass();
-    $new_node->type = 'chado_featuremap';
-    $new_node->uid = $user->uid;
-    $new_node->title = "$featuremap->name";
-    $new_node->featuremap_id = $featuremap->featuremap_id;
-
-    node_validate($new_node);
-    $errors = form_get_errors();
-    if (!$errors) {
-      $node = node_submit($new_node);
-      node_save($node);
-      if ($node->nid) {
-        print "Added " . $featuremap->name . "\n";
-      }
-      else {
-        print "ERROR: Unable to create " . $featuremap->name . "\n";
-      }
-    }
-    else {
-      print "ERROR: Unable to create " . $featuremap->name . "\n" . print_r($errors, TRUE) . "\n";
-    }
-    }
-    else {
-      print "Skipped " . $featuremap->name . "\n";
-    }
-    }
-  return $page_content;
-}
-
 /**
  *  When a node is requested by the user this function is called to allow us
  *  to add auxiliary data to the node object.
@@ -951,23 +486,8 @@ function chado_featuremap_delete(&$node) {
   chado_query("DELETE FROM {featuremapprop} WHERE featuremap_id = %d", $featuremap_id);
 }
 
-/**
- * Remove orphaned drupal nodes
- *
- * @param $dummy
- *   Not Used -kept for backwards compatibility
- * @param $job_id
- *   The id of the tripal job executing this function
- *
- * @ingroup tripal_featuremap
- */
-function tripal_featuremap_cleanup($dummy = NULL, $job_id = NULL) {
-
-  return tripal_core_clean_orphaned_nodes('featuremap', $job_id);
-  
-}
-
-/************************************************************************
+/*
+ * 
  */
 function theme_tripal_featuremap_search_result($node) {
 

+ 4 - 32
tripal_library/tripal_library.module

@@ -65,6 +65,7 @@ function tripal_library_perm() {
     'create chado_library content',
     'delete chado_library content',
     'edit chado_library content',
+    'administer tripal library',
   );
 }
 /**
@@ -109,7 +110,7 @@ function tripal_library_menu() {
     'title' => 'Libraries',
     'description' => 'Basic Description of Tripal Library Module Functionality',
     'page callback' => 'tripal_library_module_description_page',
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal library'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -118,7 +119,7 @@ function tripal_library_menu() {
     'description' => 'Manage integration of Chado libraries including associated features.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('tripal_library_admin'),
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal library'),
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -126,18 +127,9 @@ function tripal_library_menu() {
   $items['chado_sync_libraries'] = array(
     'title' => 'Sync Data',
     'page callback' => 'tripal_library_sync_libraries',
-    'access arguments' => array('access administration pages'),
+    'access arguments' => array('administer tripal library'),
     'type' => MENU_CALLBACK
   );
-  // Displaying libraries
-  $items['libraries'] = array(
-    'menu_name' => ('primary-links'), //Enable the 'Library' primary link
-    'title' => 'DNA Libraries',
-    'page callback' => 'tripal_library_show_libraries',
-    'access arguments' => array('access chado_library content'),
-    'type' => MENU_NORMAL_ITEM
-  );
-
   return $items;
 }
 
@@ -751,26 +743,6 @@ function tripal_library_admin_validate($form, &$form_state) {
          'tripal_library_cleanup', $job_args, $user->uid);
     }
 }
-/**
- *
- * @ingroup tripal_library
- */
-function tripal_library_show_libraries() {
-  // Show libraries stored in Drupal's {chado_library} table
-  $sql = "SELECT COUNT(library_id) FROM {chado_library}";
-  $no_libs = db_result(db_query($sql));
-  if ($no_libs != 0) {
-    $libraries = get_chado_libraries();
-    if ($no_libs != count($libraries)) {
-      drupal_set_message(t("Synchronization needed."));
-    }
-      return theme('tripal_library_library_table', $libraries);
-  }
-  else {
-    return t("No library exists. Please contact administrators to ".
-             "synchronize libraries.");
-  }
-}
 
 /**
  *

+ 3 - 2
tripal_organism/tripal_organism.module

@@ -101,7 +101,7 @@ function tripal_organism_menu() {
    'title' => 'Organisms',
    'description' => 'Basic Description of Tripal Organism Module Functionality',
    'page callback' => 'tripal_organism_module_description_page',
-   'access arguments' => array('administer site configuration'),
+   'access arguments' => array('adminster tripal organism'),
    'type' => MENU_NORMAL_ITEM,
   );
 
@@ -110,7 +110,7 @@ function tripal_organism_menu() {
    'description' => 'Manage integration of Chado organisms including associated features',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tripal_organism_admin'),
-   'access arguments' => array('administer site configuration'),
+   'access arguments' => array('adminster tripal organism'),
    'type' => MENU_NORMAL_ITEM,
   );
 
@@ -160,6 +160,7 @@ function tripal_organism_perm() {
     'create chado_organism content',
     'delete chado_organism content',
     'edit chado_organism content',
+    'adminster tripal organism',
   );
 }
 

+ 6 - 1
tripal_stock/includes/tripal_stock-administration.inc

@@ -184,7 +184,12 @@ function tripal_stock_admin() {
           ." between stocks. Only cvs already loaded into chado can be selected here.")
     );
 
-    $cv_options = tripal_cv_get_cv_options();
+    // get the list of CVs for the next form element
+    $results = tripal_core_chado_select('cv', array('cv_id', 'name'), array());
+    $cv_options = array();
+    foreach ($results as $r) {
+      $cv_options[$r->cv_id] = $r->name;
+    }
 
     $form['set_cv']['stock_types_cv'] = array(
      '#type' => 'select',