Browse Source

Merge branch '7.x-3.x' into 7.x-3.x-GO

Shawna Spoor 7 years ago
parent
commit
0c8fa89fb7

+ 0 - 10
legacy/tripal_phylogeny/tripal_phylogeny.module

@@ -17,7 +17,6 @@ require_once 'theme/tripal_phylogeny.theme.inc';
 require_once 'includes/tripal_phylogeny.admin.inc';
 require_once 'includes/tripal_phylogeny.chado_node.inc';
 require_once 'includes/tripal_phylogeny.import_tree.inc';
-require_once 'includes/tripal_phylogeny.taxonomy.inc';
 
 /**
  * Implements hook_permission().
@@ -115,15 +114,6 @@ function tripal_phylogeny_menu() {
     'type' => MENU_CALLBACK,
   );
 
-   $items['taxonomy_view'] = array(
-     'title' => 'Taxonomy',
-     'description' => 'Taxonomic view of the species available on this site.',
-     'page callback' => 'tripal_phylogeny_taxonomy_view',
-     'access arguments' => array('access taxonomy content'),
-     'file' => '/includes/tripal_phylogeny.taxonomy.inc',
-     'type' => MENU_NORMAL_ITEM,
-   );
-
    // create a route for viewing json of all phylonodes having this phylotree_id
    $items['ajax/chado_phylotree/%/json'] = array(
      'page callback' => 'tripal_phylogeny_ajax_get_tree_json',

+ 2 - 1
tripal/includes/TripalImporter.inc

@@ -566,7 +566,8 @@ class TripalImporter {
   }
   /**
    * Adds to the count of the total number of items that have been handled.
-   * @param unknown $num_handled
+   *
+   * @param $num_handled
    */
   protected function addItemsHandled($num_handled) {
     $items_handled = $this->num_handled = $this->num_handled + $num_handled;

+ 55 - 4
tripal/includes/tripal.jobs.inc

@@ -239,6 +239,14 @@ function tripal_jobs_report() {
  */
 function tripal_jobs_view($job_id) {
 
+  // set the breadcrumb
+  $breadcrumb = array();
+  $breadcrumb[] = l('Home', '<front>');
+  $breadcrumb[] = l('Administration', 'admin');
+  $breadcrumb[] = l('Tripal', 'admin/tripal');
+  $breadcrumb[] = l('Jobs', 'admin/tripal/tripal_jobs');
+  drupal_set_breadcrumb($breadcrumb);
+
   // get the job record
   $sql =
     "SELECT TJ.job_id,TJ.uid,TJ.job_name,TJ.modulename,TJ.progress,
@@ -313,7 +321,6 @@ function tripal_jobs_view($job_id) {
   $rows[] = array('Submit Date', $job->submit_date);
   $rows[] = array('Start time', $job->start_time);
   $rows[] = array('End time', $job->end_time);
-  $rows[] = array('Error Message', $job->error_msg);
   $rows[] = array('Priority', $job->priority);
   $rows[] = array('Submitting User', $job->username);
 
@@ -327,9 +334,53 @@ function tripal_jobs_view($job_id) {
     'empty' => '',
   );
 
-  $output = '<p>' . substr($links, 0, -2) . '</p>'; // remove trailing |
-  $output .= theme_table($table);
-  return $output;
+  $content['links'] = array(
+    '#type' => 'markup',
+    '#markup' => '<p>' . substr($links, 0, -2) . '</p>',
+  );
+  $content['job_title'] = array(
+    '#type' => 'item',
+    '#title' => t('Job Title'),
+    '#markup' => $job->job_name,
+  );
+  $content['job_status'] = array(
+    '#type' => 'item',
+    '#title' => t('Status'),
+    '#markup' => $job->job_status,
+  );
+  $content['details_fset'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Job Details'),
+    '#collapsed' => TRUE,
+    '#collapsible' => TRUE,
+    '#attributes' => array(
+      'class' => array('collapsible', 'collapsed'),
+    ),
+    '#attached' => array(
+      'js' => array('misc/collapse.js', 'misc/form.js')
+    ),
+  );
+  $content['details_fset']['job_details'] = array(
+    '#type' => 'markup',
+    '#markup' => theme_table($table),
+  );
+  $content['log_fset'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Job Logs'),
+    '#collapsed' => TRUE,
+    '#collapsible' => TRUE,
+    '#attributes' => array(
+      'class' => array('collapsible', 'collapsed'),
+    ),
+    '#attached' => array(
+      'js' => array('misc/collapse.js', 'misc/form.js')
+    ),
+  );
+  $content['log_fset']['job_logs'] = array(
+    '#type' => 'markup',
+    '#markup' => '<pre>' . $job->error_msg . '</pre>',
+  );
+  return $content;
 }
 
 /**

+ 41 - 0
tripal_chado/api/modules/tripal_chado.organism.api.inc

@@ -265,4 +265,45 @@ function tripal_autocomplete_organism($text) {
     $items["$name [id: $organism->organism_id]"] = $name;
   }
   drupal_json_output($items);
+}
+
+/**
+ * A handy function to abbreviate the infraspecific rank.
+ *
+ * @param $rank
+ *   The rank below species.
+ * @return
+ *   The proper abbreviation for the rank.
+ */
+function tripal_abbreviate_infraspeicifc_rank($rank) {
+  $abb = '';
+  switch($rank) {
+    case 'no_rank':
+      $abb = '';
+      break;
+    case 'subspecies':
+      $abb = 'subsp.';
+      break;
+    case 'varietas':
+      $abb = 'var.';
+      break;
+    case 'variety':
+      $abb = 'var.';
+      break;
+    case 'subvarietas':
+      $abb = 'subvar.';
+      break;
+    case 'subvariety':
+      $abb = 'subvar.';
+      break;
+    case 'forma':
+      $abb = 'f.';
+      break;
+    case 'subforma':
+      $abb = 'subf.';
+      break;
+    default:
+      $abb = $rank;
+  }
+  return $abb;
 }

+ 2 - 0
tripal_chado/api/modules/tripal_chado.phylotree.api.inc

@@ -582,6 +582,8 @@ function tripal_assign_phylogeny_tree_indices(&$tree, &$index = 1) {
  *
  * @param $tree
  *   The tree array.
+ * @param $phylotree.
+ *   The phylotree object (from Chado).
  * @param $options
  *   The options provide some direction for how the tree is imported.  The
  *   following keys can be used:

+ 1 - 1
tripal_chado/includes/TripalFields/local__source_data/local__source_data_widget.inc

@@ -70,7 +70,7 @@ class local__source_data_widget extends ChadoFieldWidget {
       '#title' => 'Data Source Name',
       '#description' => 'The name of the source where data was obtained for this analysis.',
       '#default_value' => $sourcename,
-      '#required' => TRUE,
+      '#required' => $element['#required'],
     );
     $widget['source_data']['sourceversion'] = array(
       '#type' => 'textfield',

+ 909 - 0
tripal_chado/includes/TripalImporter/TaxonomyImporter.inc

@@ -0,0 +1,909 @@
+<?php
+
+class TaxonomyImporter extends TripalImporter {
+  /**
+   * The name of this loader.  This name will be presented to the site
+   * user.
+   */
+  public static $name = 'Chado NCBI Taxonomy Loader';
+
+  /**
+   * The machine name for this loader. This name will be used to construct
+   * the URL for the loader.
+   */
+  public static $machine_name = 'chado_taxonomy';
+
+  /**
+   * A brief description for this loader.  This description will be
+   * presented to the site user.
+   */
+  public static $description = 'Imports new organisms from NCBI using taxonomy IDs, or loads taxonomic details about existing organisms.';
+
+  /**
+   * An array containing the extensions of allowed file types.
+   */
+  public static $file_types = array();
+
+
+  /**
+   * Provides information to the user about the file upload.  Typically this
+   * may include a description of the file types allowed.
+   */
+  public static $upload_description = '';
+
+  /**
+   * The title that should appear above the upload button.
+   */
+  public static $upload_title = 'File Upload';
+
+  /**
+   * If the loader should require an analysis record.  To maintain provenance
+   * we should always indiate where the data we are uploading comes from.
+   * The method that Tripal attempts to use for this by associating upload files
+   * with an analysis record.  The analysis record provides the details for
+   * how the file was created or obtained. Set this to FALSE if the loader
+   * should not require an analysis when loading. if $use_analysis is set to
+   * true then the form values will have an 'analysis_id' key in the $form_state
+   * array on submitted forms.
+   */
+  public static $use_analysis = FALSE;
+
+  /**
+   * If the $use_analysis value is set above then this value indicates if the
+   * analysis should be required.
+   */
+  public static $require_analysis = FALSE;
+
+  /**
+   * Text that should appear on the button at the bottom of the importer
+   * form.
+   */
+  public static $button_text = 'Import from NCBI Taxonomy';
+
+  /**
+   * Indicates the methods that the file uploader will support.
+   */
+  public static $methods = array(
+    // Allow the user to upload a file to the server.
+    'file_upload' => FALSE,
+    // Allow the user to provide the path on the Tripal server for the file.
+    'file_local' => FALSE,
+    // Allow the user to provide a remote URL for the file.
+    'file_remote' => FALSE,
+  );
+
+  /**
+   * Indicates if the file must be provided.  An example when it may not be
+   * necessary to require that the user provide a file for uploading if the
+   * loader keeps track of previous files and makes those available for
+   * selection.
+   */
+  public static $file_required = FALSE;
+
+
+  /**
+   * The array of arguments used for this loader.  Each argument should
+   * be a separate array containing a machine_name, name, and description
+   * keys.  This information is used to build the help text for the loader.
+   */
+  public static $argument_list = array();
+
+
+  /**
+   * Indicates how many files are allowed to be uploaded.  By default this is
+   * set to allow only one file.  Change to any positive number. A value of
+   * zero indicates an unlimited number of uploaded files are allowed.
+   */
+  public static $cardinality = 0;
+
+  /**
+   * Holds the list of all orgainsms currently in Chado. This list
+   * is needed when checking to see if an organism has already been
+   * loaded.
+   */
+  private  $all_orgs = array();
+
+  /**
+   * The record from the Chado phylotree table that refers to this
+   * Taxonomic tree.
+   */
+  private $phylotree = NULL;
+
+  /**
+   * The temporary tree array used by the Tripal Phylotree API for
+   * importing a new tree.
+   */
+  private $tree = NULL;
+
+  /**
+   * @see TripalImporter::form()
+   */
+  public function form($form, &$form_state) {
+
+    $form['instructions'] = array(
+      '#type' => 'fieldset',
+      '#title' => 'instructions',
+      '#description' => t('This form is used to import species from the NCBI
+        Taxonomy database into this site. Alternatively, it can import details
+        about organisms from the NCBI Taxonomy database for organisms that
+        already exist on this site.  This loader will also construct
+        the taxonomic tree for the species loaded.'),
+    );
+    $form['taxonomy_ids'] = array(
+      '#type' => 'textarea',
+      '#title' => 'Taxonomy IDa',
+      '#description' => t('Please provide a list of NCBI taxonomy IDs separated
+        by spaces, tabs or new lines.
+        The information about these organisms will be downloaded and new organism
+        records will be added to this site.')
+    );
+
+    $form['import_existing'] = array(
+      '#type' => 'checkbox',
+      '#title' => 'Import details for existing species.',
+      '#description' =>  t('The NCBI Taxonmic Importer examines the organisms
+        currently present in the database and queries NCBI for the
+        taxonomic details.  If the importer is able to match the
+        genus and species with NCBI the species details will be imported,
+        and a page containing the taxonomic tree will be created.'),
+      '#default value' => 1,
+    );
+    return $form;
+  }
+
+  /**
+   * @see TripalImporter::formValidate()
+   */
+  public function formValidate($form, &$form_state) {
+    global $user;
+
+    $import_existing = $form_state['values']['import_existing'];
+    $taxonomy_ids = $form_state['values']['taxonomy_ids'];
+
+    // make sure that we have numeric values, one per line.
+    if ($taxonomy_ids) {
+      $tax_ids = preg_split("/[\s\n\t\r]+/", $taxonomy_ids);
+      $bad_ids = array();
+      foreach ($tax_ids as $tax_id) {
+        $tax_id = trim($tax_id);
+        if (!preg_match('/^\d+$/', $tax_id)) {
+          $bad_ids[] = $tax_id;
+        }
+      }
+      if (count($bad_ids) > 0) {
+        form_set_error('taxonomy_ids',
+            t('Taxonomy IDs must be numeric. The following are not valid: "@ids".',
+                array('@ids' => implode('", "', $bad_ids))));
+      }
+    }
+  }
+
+  /**
+   * Performs the import.
+   */
+  public function run() {
+    global $site_name;
+
+
+    $arguments = $this->arguments['run_args'];
+    $taxonomy_ids = $arguments['taxonomy_ids'];
+    $import_existing = $arguments['import_existing'];
+
+
+    // Get the list of all organisms as we'll need this to lookup existing
+    // organisms.
+    $sql = "
+      SELECT O.*, CVT.name as type
+      FROM {organism} O
+       LEFT JOIN {cvterm} CVT ON CVT.cvterm_id = O.type_id
+    ";
+    $results = chado_query($sql);
+    while ($item = $results->fetchObject()) {
+      $this->all_orgs[] = $item;
+    }
+
+    // Get the phylotree object.
+    $this->logMessage('Initializing Tree...');
+    $this->phylotree = $this->initTree();
+    $this->logMessage('Rebuilding Tree...');
+    $this->tree = $this->rebuildTree();
+
+    // Clean out the phnylondes for this tree in the event this is a reload
+    chado_delete_record('phylonode', array('phylotree_id' => $this->phylotree->phylotree_id));
+
+    // Get the taxonomy IDs provided by the user (if any).
+    $tax_ids = array();
+    if ($taxonomy_ids) {
+      $tax_ids = preg_split("/[\s\n\t\r]+/", $taxonomy_ids);
+    }
+
+    // Set the number of items to handle.
+    if ($taxonomy_ids and $import_existing) {
+      $this->setTotalItems(count($this->all_orgs) + count($tax_ids));
+    }
+    if ($taxonomy_ids and !$import_existing) {
+      $this->setTotalItems(count($tax_ids));
+    }
+    if (!$taxonomy_ids and $import_existing) {
+      $this->setTotalItems(count($this->all_orgs));
+    }
+    $this->setItemsHandled($num_handled);
+
+    // If the user wants to import new taxonomy IDs then do that.
+    if ($taxonomy_ids){
+      $this->logMessage('Importing Taxonomy IDs...');
+      foreach ($tax_ids as $tax_id) {
+        $tax_id = trim($tax_id);
+        $this->importRecord($tax_id);
+        $this->addItemsHandled(1);
+      }
+    }
+
+    // If the user wants to update existing records then do that.
+    if ($import_existing) {
+      $this->logMessage('Updating Existing...');
+      $this->updateExisting();
+    }
+
+    // Now import the tree.
+    $options = array('taxonomy' => 1);
+    tripal_phylogeny_import_tree($this->tree, $this->phylotree, $options);
+  }
+
+
+  /**
+   * Create the taxonomic tree in Chado.
+   *
+   * If the tree already exists it will not be recreated.
+   *
+   * @throws Exception
+   * @return
+   *   Returns the phylotree object.
+   */
+  private function initTree() {
+    // Add the taxonomy tree record into the phylotree table. If the tree
+    // already exists then don't insert it again.
+    $tree_name = $site_name . 'Taxonomy Tree';
+    $phylotree = chado_select_record('phylotree', array('*'), array('name' => $tree_name));
+    if (count($phylotree) == 0) {
+      // Add the taxonomic tree.
+      $phylotree = array(
+        'name' =>  $site_name . 'Taxonomy Tree',
+        'description' => 'The taxonomic tree of species present on this site. Click a species name for more details.',
+        'leaf_type' => 'taxonomy',
+        'analysis_id' => $analysis->analysis_id,
+        'tree_file' => '/dev/null',
+        'format' => 'taxonomy',
+        'no_load' => TRUE,
+      );
+      $errors = array();
+      $warnings = array();
+      $success = tripal_insert_phylotree($phylotree, $errors, $warnings);
+      if (!$success) {
+        throw new Exception("Cannot add the Taxonomy Tree record.");
+      }
+      $phylotree = (object) $phylotree;
+    }
+    else {
+      $phylotree = $phylotree[0];
+    }
+    return $phylotree;
+  }
+
+
+
+  /**
+   * Iterates through all existing organisms and rebuilds the taxonomy tree.
+   *
+   * The phloytree API doesn't support adding nodes to existing trees only
+   * importing whole trees. So, we must rebuild the tree using the current
+   * organisms and then we can add to it.
+   *
+   */
+  private function rebuildTree() {
+    $lineage_nodes[] = array();
+
+    // Get the "rank" cvterm. It requires that the TAXRANK vocabulary is loaded.
+    $rank_cvterm = tripal_get_cvterm(array(
+      'name' => 'rank',
+      'cv_id' => array('name' => 'local')
+    ));
+
+    // The taxonomic tree must have a root, so create that first.
+    $tree = array(
+      'name' => 'root',
+      'depth' => 0,
+      'is_root' => 1,
+      'is_leaf' => 0,
+      'is_internal' => 0,
+      'left_index' => 0,
+      'right_index' => 0,
+      'branch_set' => array(),
+    );
+
+    foreach ($this->all_orgs as $organism) {
+      // First get the phylonode record for this organism.
+      $sql = "
+        SELECT P.*
+        FROM {phylonode} P
+         INNER JOIN {phylonode_organism} PO on PO.phylonode_id = P.phylonode_id
+        WHERE P.phylotree_id = :phylotree_id AND PO.organism_id = :organism_id
+      ";
+      $args = array(
+        ':phylotree_id' => $this->phylotree->phylotree_id,
+        ':organism_id' => $organism->organism_id,
+      );
+      $result = chado_query($sql, $args);
+      if (!$result) {
+        continue;
+      }
+      $phylonode = $result->fetchObject();
+
+      // Next get the lineage for this organism.
+      $lineage = $this->getProperty($organism->organism_id, 'lineage');
+      if (!$lineage) {
+        continue;
+      }
+      $lineage_depth = preg_split('/;\s*/', $lineage->value);
+
+      // Now rebuild the tree by first creating the nodes for the full
+      // lineage and then adding the organism as a leaf node.
+      $parent = $tree;
+      $i = 1;
+      $lineage_good = TRUE;
+      foreach ($lineage_depth as $child) {
+        // We need to find the node in the phylotree for this level of the
+        // lineage, but there's a lot of repeats and we don't want to keep
+        // doing the same queries over and over, so we store the nodes
+        // we've already seen in the $lineage_nodes array for fast lookup.
+        if (array_key_exists($child, $lineage_nodes)) {
+          $phylonode = $lineage_nodes[$child];
+          if (!$phylonode) {
+            $lineage_good = FALSE;
+            continue;
+          }
+        }
+        else {
+          $values = array(
+            'phylotree_id' => $this->phylotree->phylotree_id,
+            'label' => $child,
+          );
+          $options = array(
+            'include_fk' => array(
+              'type_id' => array(
+                'cv_id' => 1,
+                'dbxref_id' => 1,
+              ),
+            )
+          );
+          $phylonode = chado_generate_var('phylonode', $values, $options);
+          $lineage_nodes[$child] = $phylonode;
+          if (!$phylonode) {
+            $lineage_good = FALSE;
+            continue;
+          }
+          $options = array('return_array' => TRUE);
+          $phylonode = chado_expand_var($phylonode, 'table', 'phylonodeprop', $options);
+        }
+        $name = $child;
+        $node_rank = (string) $child->Rank;
+        $node = array(
+          'name' => $name,
+          'depth' => $i,
+          'is_root' => 0,
+          'is_leaf' => 0,
+          'is_internal' => 1,
+          'left_index' => 0,
+          'right_index' => 0,
+          'parent' => $parent,
+          'branch_set' => array(),
+          'parent' => $parent['name'],
+          'properties' => array(
+            $phylonode->phylonodeprop[0]->type_id->cvterm_id => $phylonode->phylonodeprop[0]->value,
+          ),
+        );
+        $parent = $node;
+        $this->addTaxonomyNode($tree, $node, $lineage_depth);
+        $i++;
+      } // end foreach ($lineage_depth as $child) { ...
+
+      // If $stop is set then we had problems setting the lineage so
+      // skip adding the leaf node below.
+      if (!$lineage_good) {
+        continue;
+      }
+
+      // Now add in the leaf node
+      $sci_name = $organism->genus . ' ' . $organism->species;
+      if ($organism->type_id) {
+        if ($organism->type and $organism->type == 'no_rank') {
+          $sci_name .= ' ' . $organism->infraspecific_name;
+        }
+        else {
+          $type = tripal_abbreviate_infraspeicifc_rank($organism->type);
+          $sci_name .= ' ' . $type . ' ' . $organism->infraspecific_name;
+        }
+      }
+
+      $node = array(
+        'name' => $sci_name,
+        'depth' => $i,
+        'is_root' => 0,
+        'is_leaf' => 1,
+        'is_internal' => 0,
+        'left_index' => 0,
+        'right_index' => 0,
+        'parent' => $parent['name'],
+        'organism_id' => $organism->organism_id,
+        'properties' => array(
+          $rank_cvterm->cvterm_id => $organism->type,
+        ),
+      );
+      $this->addTaxonomyNode($tree, $node, $lineage_depth);
+
+      // Set the indecies for the tree.
+      tripal_assign_phylogeny_tree_indices($tree);
+    }
+
+    return $tree;
+  }
+
+  /**
+   * Imports details from NCBI Taxonomy for organisms that alrady exist.
+   */
+  private function updateExisting() {
+
+    foreach ($this->all_orgs as $organism) {
+      // If the organism record is marked as new then let's skip it because
+      // it was newly added and should have the updated information already.
+      if ($organism->is_new) {
+        continue;
+      }
+
+      // TODO: we should check if the organism already has a taxonomy ID.
+      // if so we should use that instead of the scientific name.
+
+      // Build the query string to get the information about this species.
+      $sci_name = $organism->genus . ' ' . $organism->species;
+      if ($organism->type_id) {
+        $type = tripal_abbreviate_infraspeicifc_rank($organism->type);
+        if ($type) {
+          $sci_name .= ' ' . $type . ' ' . $infraspecific_name;
+        }
+        else {
+          $sci_name .= ' ' . $infraspecific_name;
+        }
+      }
+      $sci_name = urlencode($sci_name);
+      $search_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?".
+          "db=taxonomy" .
+          "&term=$sci_name";
+
+      // Get the search response from NCBI.
+      $rfh = fopen($search_url, "r");
+      $xml_text = '';
+      while (!feof($rfh)) {
+        $xml_text .= fread($rfh, 255);
+      }
+      fclose($rfh);
+
+      // Parse the XML to get the taxonomy ID
+      $xml = new SimpleXMLElement($xml_text);
+      if ($xml) {
+        $taxid = (string) $xml->IdList->Id;
+        if ($taxid) {
+          $this->importRecord($taxid, $organism);
+        }
+      }
+      $this->addItemsHandled(1);
+    }
+  }
+
+  /**
+   * Checks the Chado database to see if the organism already exists.
+   *
+   * @param $taxid
+   *   The taxonomic ID for the organism.
+   * @param $sci_name
+   *   The scientific name for the organism as returned by NCBI
+   */
+  private function findOrganism($taxid, $sci_name) {
+    $organism = NULL;
+
+    // First check the taxid to see if it's present and assocaited with an
+    // organism already.
+    $values = array(
+      'db_id' => array(
+        'name' => NCBITaxon
+      ),
+      'accession' => $taxid,
+    );
+    $columns = array('dbxref_id');
+    $dbxref = chado_select_record('dbxref', $columns, $values);
+    if (count($dbxref) > 0) {
+      $columns = array('organism_id');
+      $values = array('dbxref_id' => $dbxref[0]->dbxref_id);
+      $organism_dbxref = chado_select_record('organism_dbxref', $columns, $values);
+      if (count($organism_dbxref) >0) {
+        $organism_id = $organism_dbxref[0]->organism_id;
+        $columns = array('*');
+        $values = array('organism_id' => $organism_id);
+        $organism = chado_select_record('organism', $columns, $values);
+        if (count($organism) > 0) {
+          $organism = $organism[0];
+        }
+      }
+    }
+
+    // If the caller did not provide an organism then we want to try and
+    // add one. But, it only makes sense to add one if this record
+    // is of rank species.
+    // First check if the full name (including the infrasepcific name)
+    // are all present in the genus and species name.  This would have
+    // been the Chado v1.2 (or less) of storing species.
+    if (!$organism) {
+      $sql = "
+          SELECT organism_id
+          FROM {organism}
+          WHERE concat(genus, ' ', species) = :sci_name
+        ";
+      $results = chado_query($sql, array(':sci_name' => $sci_name));
+      if ($results) {
+        $item = $results->fetchObject();
+        $columns = array('*');
+        $values = array('organism_id' => $item->organism_id);
+        $organism = chado_select_record('organism', $columns, $values);
+        if (count($organism) > 0) {
+          $organism = $organism[0];
+        }
+      }
+    }
+    // Second, check if the full name includes the infraspecific name.
+    if (!$organism) {
+      foreach ($this->all_orgs as $item) {
+        if (!$item->type) {
+          if ($sci_name == trim($item->genus) . ' ' . trim($item->species)) {
+            $organism = $item;
+          }
+        }
+        else {
+          if ($item->type == 'no_rank') {
+            if ($sci_name == trim($item->genus) . ' ' . trim($item->species) . ' ' . trim($item->infraspecific_name)) {
+              $organism = $item;
+            }
+          }
+          else {
+            $valid_terms = array(
+              'subspecies' => 'subsp.',
+              'varietas' => 'var.',
+              'subvariety' => 'subvar.',
+              'forma' => 'forma',
+              'subforma' => 'subforma',
+              'strain' => 'strain',
+            );
+            if (array_key_exists($rank, $valid_terms)) {
+              if ($sci_name == trim($item->genus) . ' ' . trim($item->species) . ' ' . $valid_terms[$rank] . ' ' . trim($item->infraspecific_name)) {
+                $organism = $item;
+              }
+            }
+          }
+        }
+      }
+    }
+
+    return $organism;
+  }
+  /**
+   * Adds a new organism record to Chado.
+   *
+   * @param sci_name
+   *   The scientific name as provied by NCBI Taxonomy.
+   * @param $rank
+   *   The rank of the organism as provied by NCBI Taxonomy.
+   */
+  private function addOrganism($sci_name, $rank) {
+
+    $organism = NULL;
+    $matches = array();
+    $genus = '';
+    $species = '';
+    $infra = '';
+    $values = array();
+
+    // Check if the scientific name has an infraspecific part or is just
+    // a species name.
+    if (preg_match('/^(.+?)\s+(.+?)\s+(.+)$/', $sci_name, $matches)) {
+      $genus = $matches[1];
+      $species = $matches[2];
+      $type = tripal_get_cvterm(array(
+        'name' => preg_replace('/ /','_', $rank),
+        'cv_id' => array('name' => 'taxonomic_rank')
+      ));
+      $infra = $matches[3];
+      $values = array(
+        'genus' => $genus,
+        'species' => $species,
+        'abbreviation' => $genus[0] . '. ' . $species,
+        'type_id' => $type->cvterm_id,
+        'infraspecific_name' => $infra,
+      );
+      $organism = chado_insert_record('organism', $values);
+      $organism = (object) $organism;
+      $organism->type = $rank;
+    }
+    else if (preg_match('/^(.+?)\s+(.+?)$/', $sci_name, $matches)) {
+      $genus = $matches[1];
+      $species = $matches[2];
+      $infra = '';
+      $values = array(
+        'genus' => $genus,
+        'species' => $species,
+        'abbreviation' => $genus[0] . '. ' . $species,
+      );
+      $organism = chado_insert_record('organism', $values);
+      $organism = (object) $organism;
+    }
+    if ($organism) {
+      $organism->is_new = TRUE;
+      $this->all_orgs[] = $organism;
+    }
+
+    return $organism;
+  }
+
+  /**
+   * Imports an organism from the NCBI taxonomy DB by its taxonomy ID
+   *
+   * @param $taxid
+   *   The NCBI Taxonomy ID.
+   * @param $organism
+   *   The organism object to which this taxonomy belongs.  If the organism
+   *   is NULL then it will be created.
+   */
+  private function importRecord($taxid, $organism = NULL) {
+    $adds_organism = $organism ? FALSE : TRUE;
+
+    // Get the "rank" cvterm. It requires that the TAXRANK vocabulary is loaded.
+    $rank_cvterm = tripal_get_cvterm(array(
+      'name' => 'rank',
+      'cv_id' => array('name' => 'local')
+    ));
+
+    // Get the details for this taxonomy.
+    $fetch_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?".
+        "db=taxonomy" .
+        "&id=$taxid";
+
+    // Get the search response from NCBI.
+    $rfh = fopen($fetch_url, "r");
+    $xml_text = '';
+    while (!feof($rfh)) {
+      $xml_text .= fread($rfh, 255);
+    }
+    fclose($rfh);
+
+    $xml = new SimpleXMLElement($xml_text);
+    if ($xml) {
+      $taxon = $xml->Taxon;
+
+      // Get the genus and species from the xml.
+      $parent = (string) $taxon->ParentTaxId;
+      $rank = (string) $taxon->Rank;
+      $sci_name = (string) $taxon->ScientificName;
+
+      // If we don't have an organism record provided then see if there
+      // is one provided by Chado, if not, the try to add one.
+      if (!$organism) {
+        $organism = $this->findOrganism($taxid, $sci_name);
+        if (!$organism) {
+          $organism = $this->addOrganism($sci_name, $rank);
+          if (!$organism) {
+            throw new Exception(t('Cannot add organism: @sci_name', array('@sci_name' => $sci_name)));
+          }
+        }
+      }
+
+      // Get properties for this organism.
+      $lineage = (string) $taxon->Lineage;
+      $genetic_code = (string) $taxon->GeneticCode->GCId;
+      $genetic_code_name = (string) $taxon->GeneticCode->GCName;
+      $mito_genetic_code = (string) $taxon->MitoGeneticCode->MGCId;
+      $mito_genetic_code_name = (string) $taxon->MitoGeneticCode->MGCName;
+      $division = (string) $taxon->Division;
+
+      // Add in the organism properties.
+      $this->addProperty($organism->organism_id, 'division', $division);
+      $this->addProperty($organism->organism_id, 'mitochondrial_genetic_code_name', $mito_genetic_code_name);
+      $this->addProperty($organism->organism_id, 'mitochondrial_genetic_code', $mito_genetic_code);
+      $this->addProperty($organism->organism_id, 'genetic_code_name', $genetic_code_name);
+      $this->addProperty($organism->organism_id, 'lineage', $lineage);
+      $this->addProperty($organism->organism_id, 'genetic_code', $genetic_code);
+
+      $name_ranks = array();
+      if ($taxon->OtherNames->children) {
+        foreach ($taxon->OtherNames->children() as $child) {
+          $type = $child->getName();
+          $name = (string) $child;
+          if (!array_key_exists($type, $name_ranks)) {
+            $name_ranks[$type] = 0;
+          }
+          switch ($type) {
+            case 'GenbankCommonName':
+              $this->addProperty($organism->organism_id, 'genbank_common_name', $name, $name_ranks[$type]);
+              break;
+            case 'Synonym':
+            case 'GenbankSynonym':
+              $this->addProperty($organism->organism_id, 'synonym', $name, $name_ranks[$type]);
+              break;
+            case 'CommonName':
+              // If we had to add the organism then include the commone name too.
+              if ($adds_organism) {
+                $organism->common_name = $name;
+                $values = array('organism_id' => $organism->id);
+                chado_update_record('organism', $values, $organism);
+              }
+            case 'Includes':
+              $this->addProperty($organism->organism_id, 'other_name', $name, $name_ranks[$type]);
+              break;
+            case 'EquivalentName':
+              $this->addProperty($organism->organism_id, 'equivalent_name', $name, $name_ranks[$type]);
+              break;
+            case 'Anamorph':
+              $this->addProperty($organism->organism_id, 'anamorph', $name, $name_ranks[$type]);
+              break;
+            case 'Name':
+              // skip the Name stanza
+              break;
+            default:
+              print "NOTICE: Skipping unrecognzed name type: $type\n";
+              // do nothing for unrecognized types
+          }
+          $name_ranks[$type]++;
+        }
+      }
+
+      // Generate a nested array structure that can be used for importing the tree.
+      $lineage_depth = preg_split('/;\s*/', $lineage);
+      $parent = $this->tree;
+      $i = 1;
+      foreach ($taxon->LineageEx->children() as $child) {
+        $tid = (string) $child->TaxID;
+        $name = (string) $child->ScientificName;
+        $node_rank = (string) $child->Rank;
+        $node = array(
+          'name' => $name,
+          'depth' => $i,
+          'is_root' => 0,
+          'is_leaf' => 0,
+          'is_internal' => 1,
+          'left_index' => 0,
+          'right_index' => 0,
+          'parent' => $parent,
+          'branch_set' => array(),
+          'parent' => $parent['name'],
+          'properties' => array(
+            $rank_cvterm->cvterm_id => $node_rank,
+          ),
+        );
+        $parent = $node;
+        $this->addTaxonomyNode($this->tree, $node, $lineage_depth);
+        $i++;
+      }
+      // Now add in the leaf node
+      $node = array(
+        'name' => $sci_name,
+        'depth' => $i,
+        'is_root' => 0,
+        'is_leaf' => 1,
+        'is_internal' => 0,
+        'left_index' => 0,
+        'right_index' => 0,
+        'parent' => $parent['name'],
+        'organism_id' => $organism->organism_id,
+        'properties' => array(
+          $rank_cvterm->cvterm_id => $rank,
+        ),
+      );
+      $this->addTaxonomyNode($this->tree, $node, $lineage_depth);
+
+      // Set the indecies for the tree.
+      tripal_assign_phylogeny_tree_indices($this->tree);
+    }
+  }
+  /**
+   *
+   */
+  private function addTaxonomyNode(&$tree, $node, $lineage_depth) {
+
+    // Get the branch set for the tree root.
+    $branch_set = &$tree['branch_set'];
+
+    // Iterate through the tree up until the depth where this node will
+    // be placed.
+    $node_depth = $node['depth'];
+    for ($i = 1; $i <= $node_depth; $i++) {
+      // Iterate through any existing nodes in the branch set to see if
+      // the node name matches the correct name for the lineage at this
+      // depth. If it matches then it is inside of this branch set that
+      // we will place the node.
+      for ($j = 0; $j < count($branch_set); $j++) {
+        // If this node already exists in the tree then return.
+        if ($branch_set[$j]['name'] == $node['name'] and
+            $branch_set[$j]['depth'] = $node['depth']) {
+          return;
+        }
+        // Otherwise, set the branch to be the current branch and continue.
+        if ($branch_set[$j]['name'] == $lineage_depth[$i-1]) {
+          $branch_set = &$branch_set[$j]['branch_set'];
+          break;
+        }
+      }
+    }
+    // Add the node to the last branch set.  This should be where this node goes.
+    $branch_set[] = $node;
+  }
+
+  /**
+   * Retrieves a property for a given organism.
+   *
+   * @param $organism_id
+   *   The organism ID to which the property is added.
+   * @param $term_name
+   *   The name of the organism property term.  This term must be
+   *   present in the 'organism_property' cv.
+   * @param $rank
+   *   The order for this property. The first instance of this term for
+   *   this organism should be zero. Defaults to zero.
+   * @return
+   *   The property object.
+   */
+  private function getProperty($organism_id, $term_name, $rank = 0) {
+    $record = array(
+      'table' => 'organism',
+      'id' => $organism_id
+    );
+    $property = array(
+      'type_name' => $term_name,
+      'cv_name' => 'organism_property',
+      'value' => $value,
+      'rank' => $rank
+    );
+
+    return chado_get_property($record, $property);
+  }
+
+  /**
+   * Adds a property to an organism node.
+   *
+   * @param $organism_id
+   *   The organism ID to which the property is added.
+   * @param $term_name
+   *   The name of the organism property term.  This term must be
+   *   present in the 'organism_property' cv.
+   * @param $value
+   *   The value of the property.
+   * @param $rank
+   *   The order for this property. The first instance of this term for
+   *   this organism should be zero. Defaults to zero.
+   */
+  private function addProperty($organism_id, $term_name, $value, $rank = 0) {
+    if (!$value) {
+      return;
+    }
+
+    $record = array(
+      'table' => 'organism',
+      'id' => $organism_id
+    );
+    $property = array(
+      'type_name' => $term_name,
+      'cv_name' => 'organism_property',
+      'value' => $value
+    );
+    // Delete all properties of this type if the rank is zero.
+    if ($rank == 0) {
+      chado_delete_property($record, $property);
+    }
+    chado_insert_property($record, $property);
+  }
+}

+ 0 - 368
tripal_chado/includes/loaders/tripal_chado.taxonomy_importer.inc

@@ -1,368 +0,0 @@
-<?php
-
-/**
- *
- */
-function tripal_chado_taxonomy_load_form($form, &$form_state) {
-  $form['instructions'] = array(
-    '#type' => 'item',
-    '#markup' => '',
-  );
-
-  $form['import_existing'] = array(
-    '#type' => 'checkbox',
-    '#title' => 'Import taxonomy for existing species.',
-    '#description' =>  t('The NCBI Taxonmic Importer examines the organisms
-      currently present in the database and queries NCBI for the
-      taxonomic details.  If the importer is able to match the
-      genus and species with NCBI the species details will be imported,
-      and a page containing the taxonomic tree will be created.'),
-  );
-
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#name' => 'import',
-    '#value' => 'Submit',
-  );
-  return $form;
-}
-
-/**
- *
- * @param unknown $form
- * @param unknown $form_state
- */
-function tripal_chado_taxonomy_load_form_validate($form, &$form_state) {
-  global $user;
-
-  if (!$form_state['values']['import_existing']) {
-    form_set_error('import_exists', 'Please confirm the import by clicking the checkbox.');
-  }
-}
-
-/**
- *
- * @param unknown $form
- * @param unknown $form_state
- */
-function tripal_chado_taxonomy_load_form_submit($form, &$form_state) {
-  global $user;
-
-  if ($form_state['values']['import_existing']) {
-    $args = array();
-    $includes = array();
-    $includes[] = module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.taxonomy_importer');
-    tripal_add_job("Import NCBI Taxonomy", 'tripal_chado',
-      'tripal_chado_ncbi_taxonomy_import', $args, $user->uid, 10, $includes);
-  }
-}
-
-/**
- *
- * @param unknown $job_id
- */
-function tripal_chado_ncbi_taxonomy_import($job_id) {
-
-  print "\nNOTE: Importing of NCBI taxonomy data is performed using a database transaction. \n" .
-    "If the load fails or is terminated prematurely then the entire set of \n" .
-    "insertions/updates is rolled back and will not be found in the database\n\n";
-
-  $transaction = db_transaction();
-  try {
-    // TDDO: there should be an API function named tripal_insert_analysis().
-    // But until then we have to insert the analysis manually.
-    // Get the version of this module for the analysis record:
-    $info = system_get_info('module', 'tripal_chado');
-    $version = $info['version'];
-    $analysis_name = 'NCBI Taxonomy Tree Import';
-
-    // If the analysis record already exists then don't add it again.
-    $analysis = chado_select_record('analysis', array('*'), array('name' => $analysis_name));
-    if (count($analysis) == 0) {
-      $values = array(
-        'name' => 'NCBI Taxonomy Tree Import',
-        'description' => 'Used to import NCBI taxonomy details for organisms in this database.',
-        'program' => 'Tripal Phylogeny Module NCBI Taxonomy Importer',
-        'programversion' => $version,
-        'sourcename' => 'NCBI Taxonomy',
-        'sourceuri' => 'http://www.ncbi.nlm.nih.gov/taxonomy',
-      );
-      $analysis = chado_insert_record('analysis', $values);
-      if (!$analysis) {
-        throw new Exception("Cannot add NCBI Taxonomy Tree Import Analysis.");
-      }
-    }
-    else {
-      $analysis = $analysis[0];
-    }
-
-    // If the tree already exists then don't insert it again.
-    global $site_name;
-    $tree_name = $site_name . 'Taxonomy Tree';
-    $phylotree = chado_select_record('phylotree', array('*'), array('name' => $tree_name));
-    if (count($phylotree) == 0) {
-      // Add the taxonomic tree.
-      $options = array(
-        'name' =>  $site_name . 'Taxonomy Tree',
-        'description' => 'The taxonomic tree of species present on this site. Click a species name for more details.',
-        'leaf_type' => 'taxonomy',
-        'analysis_id' => $analysis->analysis_id,
-        'tree_file' => '/dev/null',
-        'format' => 'taxonomy',
-        'no_load' => TRUE,
-      );
-      $errors = array();
-      $warnings = array();
-      $success = tripal_insert_phylotree($options, $errors, $warnings);
-      if (!$success) {
-        throw new Exception("Cannot add the Taxonomy Tree record.");
-      }
-      $phylotree = (object) $options;
-    }
-    else {
-      $phylotree = $phylotree[0];
-    }
-
-    // Clean out the phylotree in the event this is a reload
-    chado_delete_record('phylonode', array('phylotree_id' => $phylotree->phylotree_id));
-
-    // The taxonomic tree must have a root, so create that first.
-    $tree = array(
-      'name' => 'root',
-      'depth' => 0,
-      'is_root' => 1,
-      'is_leaf' => 0,
-      'is_internal' => 0,
-      'left_index' => 0,
-      'right_index' => 0,
-      'branch_set' => array(),
-    );
-
-    // Get the "rank" cvterm. It requires that the TAXRANK vocabulary is loaded.
-    $rank_cvterm = tripal_get_cvterm(array(
-      'name' => 'rank',
-      'cv_id' => array('name' => 'local')
-    ));
-
-    // Get the list of organisms
-    $sql = "SELECT O.* FROM {organism} O";
-    $organisms = chado_query($sql);
-    while ($organism = $organisms->fetchObject()) {
-      // Build the query string to get the information about this species.
-      $term = $organism->genus . ' ' . $organism->species;
-      $term = urlencode($term);
-      $search_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?".
-        "db=taxonomy" .
-        "&term=$term";
-
-      // Get the search response from NCBI.
-      $rfh = fopen($search_url, "r");
-      $xml_text = '';
-      while (!feof($rfh)) {
-        $xml_text .= fread($rfh, 255);
-      }
-      fclose($rfh);
-
-      // Parse the XML to get the taxonomy ID
-      $xml = new SimpleXMLElement($xml_text);
-      if ($xml) {
-        $taxid = (string) $xml->IdList->Id;
-        if ($taxid) {
-          print "$taxid\t$organism->genus $organism->species\n";
-          // If we have a taxonomy ID we can now get the details.
-          $fetch_url = "http://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?".
-            "db=taxonomy" .
-            "&id=$taxid";
-          // Get the search response from NCBI.
-          $rfh = fopen($fetch_url, "r");
-          $xml_text = '';
-          while (!feof($rfh)) {
-            $xml_text .= fread($rfh, 255);
-          }
-          fclose($rfh);
-
-          $xml = new SimpleXMLElement($xml_text);
-          if ($xml) {
-            $taxon = $xml->Taxon;
-
-            // Add in the organism properties
-            $lineage = (string) $taxon->Lineage;
-            tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'lineage', $lineage);
-
-            $genetic_code = (string) $taxon->GeneticCode->GCId;
-            tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'genetic_code', $genetic_code);
-
-            $genetic_code_name = (string) $taxon->GeneticCode->GCName;
-            tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'genetic_code_name', $genetic_code_name);
-
-            $mito_genetic_code = (string) $taxon->MitoGeneticCode->MGCId;
-            tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'mitochondrial_genetic_code', $mito_genetic_code);
-
-            $mito_genetic_code_name = (string) $taxon->MitoGeneticCode->MGCName;
-            tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'mitochondrial_genetic_code_name', $mito_genetic_code_name);
-
-            $division = (string) $taxon->Division;
-            tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'division', $division);
-
-            $name_ranks = array();
-            foreach ($taxon->OtherNames->children() as $child) {
-              $type = $child->getName();
-              $name = (string) $child;
-              if (!array_key_exists($type, $name_ranks)) {
-                $name_ranks[$type] = 0;
-              }
-              switch ($type) {
-                case 'GenbankCommonName':
-                  tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'genbank_common_name', $name, $name_ranks[$type]);
-                  break;
-                case 'Synonym':
-                case 'GenbankSynonym':
-                  tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'synonym', $name, $name_ranks[$type]);
-                  break;
-                case 'CommonName':
-                case 'Includes':
-                  tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'other_name', $name, $name_ranks[$type]);
-                  break;
-                case 'EquivalentName':
-                  tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'equivalent_name', $name, $name_ranks[$type]);
-                  break;
-                case 'Anamorph':
-                  tripal_chado_taxonomy_add_organism_property($organism->organism_id, 'anamorph', $name, $name_ranks[$type]);
-                  break;
-                case 'Name':
-                  // skip the Name stanza
-                  break;
-                default:
-                  print "NOTICE: Skipping unrecognzed name type: $type\n";
-                  // do nothing for unrecognized types
-              }
-              $name_ranks[$type]++;
-            }
-
-            // Generate a nested array structure that can be used for importing the tree.
-            $parent = (string) $taxon->ParentTaxId;
-            $rank = (string) $taxon->Rank;
-            $sci_name = (string) $taxon->ScientificName;
-            $lineage_depth = preg_split('/;\s*/', $lineage);
-            $parent = $tree;
-            $i = 1;
-            foreach ($taxon->LineageEx->children() as $child) {
-              $tid = (string) $child->TaxID;
-              $name = (string) $child->ScientificName;
-              $node_rank = (string) $child->Rank;
-              $node = array(
-                'name' => $name,
-                'depth' => $i,
-                'is_root' => 0,
-                'is_leaf' => 0,
-                'is_internal' => 1,
-                'left_index' => 0,
-                'right_index' => 0,
-                'parent' => $parent,
-                'branch_set' => array(),
-                'parent' => $parent['name'],
-                'properties' => array(
-                  $rank_cvterm->cvterm_id => $node_rank,
-                ),
-              );
-              $parent = $node;
-              tripal_chado_taxonomy_import_add_node($tree, $node, $lineage_depth);
-              $i++;
-            }
-            // Now add in the leaf node
-            $node = array(
-              'name' => $sci_name,
-              'depth' => $i,
-              'is_root' => 0,
-              'is_leaf' => 1,
-              'is_internal' => 0,
-              'left_index' => 0,
-              'right_index' => 0,
-              'parent' => $parent['name'],
-              'organism_id' => $organism->organism_id,
-              'properties' => array(
-                $rank_cvterm->cvterm_id => $rank,
-              ),
-            );
-            tripal_chado_taxonomy_import_add_node($tree, $node, $lineage_depth);
-
-            // Set the indecies for the tree.
-            tripal_assign_phylogeny_tree_indices($tree);
-          } // end: if ($xml) { ...
-        } // end: if ($taxid) { ...
-      } // end: if ($xml) { ...
-    } // end: while ($organism = $organisms->fetchObject()) { ...
-    // print json_encode(($tree));
-
-    // Now add the tree
-    $options = array('taxonomy' => 1);
-    tripal_phylogeny_import_tree($tree, $phylotree, $options);
-  }
-  catch (Exception $e) {
-    $transaction->rollback();
-    print "\n"; // make sure we start errors on new line
-    watchdog_exception('tripal_chado', $e);
-    print "FAILED: Rolling back database changes...\n";
-  }
-}
-
-/**
- *
- * @param unknown $node
- */
-function tripal_chado_taxonomy_import_add_node(&$tree, $node, $lineage_depth) {
-
-   // Get the branch set for the tree root.
-   $branch_set = &$tree['branch_set'];
-
-   // Iterate through the tree up until the depth where this node will
-   // be placed.
-   $node_depth = $node['depth'];
-   for ($i = 1; $i <= $node_depth; $i++) {
-     // Iterate through any existing nodes in the branch set to see if
-     // the node name matches the correct name for the lineage at this
-     // depth. If it matches then it is inside of this branch set that
-     // we will place the node.
-     for ($j = 0; $j < count($branch_set); $j++) {
-       // If this node already exists in the tree then return.
-       if ($branch_set[$j]['name'] == $node['name'] and
-           $branch_set[$j]['depth'] = $node['depth']) {
-         return;
-       }
-       // Otherwise, set the branch to be the current branch and continue.
-       if ($branch_set[$j]['name'] == $lineage_depth[$i-1]) {
-         $branch_set = &$branch_set[$j]['branch_set'];
-         break;
-       }
-     }
-   }
-   // Add the node to the last branch set.  This should be where this node goes.
-   $branch_set[] = $node;
-}
-
-/**
- *
- * @param unknown $organism_id
- * @param unknown $term_name
- * @param unknown $value
- */
-function tripal_chado_taxonomy_add_organism_property($organism_id, $term_name, $value, $rank = 0) {
-  if (!$value) {
-    return;
-  }
-
-  $record = array(
-    'table' => 'organism',
-    'id' => $organism_id
-  );
-  $property = array(
-    'type_name' => $term_name,
-    'cv_name' => 'organism_property',
-    'value' => $value
-  );
-  // Delete all properties of this type if the rank is zero.
-  if ($rank == 0) {
-    chado_delete_property($record, $property);
-  }
-  chado_insert_property($record, $property);
-}

+ 0 - 13
tripal_chado/includes/tripal_chado.semweb.inc

@@ -1466,19 +1466,6 @@ function tripal_chado_populate_vocab_SWO() {
   ));
   tripal_associate_chado_semweb_term('analysis', 'program', $term);
 
-  $term = tripal_insert_cvterm(array(
-    'id' => 'SWO:0000001',
-    'name' => 'software',
-    'cv_name' => 'swo',
-    'definition' => 'Computer software, or generally just software, is any ' .
-    'set of machine-readable instructions (most often in the form of a ' .
-    'computer program) that conform to a given syntax (sometimes ' .
-    'referred to as a language) that is interpretable by a given ' .
-    'processor and that directs a computer\'s processor to perform ' .
-    'specific operations.',
-  ));
-  tripal_associate_chado_semweb_term('analysis', 'program', $term);
-
 }
 
 /**

+ 5 - 3
legacy/tripal_phylogeny/includes/tripal_phylogeny.taxonomy.inc → tripal_chado/includes/tripal_chado.taxonomy.inc

@@ -20,7 +20,7 @@ function tripal_phylogeny_taxonomy_view() {
         NCBI taxonomy loader to update the view</p>',
       array(
         '@menu' => url('admin/structure/menu/manage/navigation'),
-        '@taxloader' => url('admin/tripal/loaders/ncbi_taxonomy_loader'
+        '@taxloader' => url('admin/tripal/loaders/chado_taxonomy'
       ))
   );
   $admin_message = tripal_set_message($message, TRIPAL_INFO, array('return_html' => TRUE));
@@ -30,14 +30,16 @@ function tripal_phylogeny_taxonomy_view() {
     $node = new stdClass();
     $node->phylotree = $phylotree;
 
-    $html =  theme('tripal_phylogeny_taxonomic_tree', array('node' => $node)) .
+    $html = theme('tripal_phylogeny_taxonomic_tree', array('node' => $node)) .
       $admin_message;
     return $html;
   }
 
-  return array(
+  $content['admin_message'] = array(
     '#type' => 'markup',
     '#markup' => t('This site has not yet prepared the taxonomy for viewing.') . $admin_message,
   );
+
+  return $content;
 }
 

+ 0 - 1
tripal_chado/tripal_chado.install

@@ -1330,4 +1330,3 @@ function tripal_chado_update_7319() {
     throw new DrupalUpdateException('Could not perform update: '. $error);
   }
 }
-

+ 16 - 10
tripal_chado/tripal_chado.module

@@ -370,16 +370,6 @@ function tripal_chado_menu() {
     'type' => MENU_NORMAL_ITEM,
     'weight' => 6
   );
-  $items['admin/tripal/loaders/ncbi_taxonomy_loader'] = array(
-    'title' => 'Chado NCBI Taxonomy Loader',
-    'description' => 'Loads taxonomic details about installed organisms.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_chado_taxonomy_load_form'),
-    'access arguments' => array('administer tripal'),
-    'file' => 'includes/loaders/tripal_chado.taxonomy_importer.inc',
-    'file path' => drupal_get_path('module', 'tripal_chado'),
-    'type' => MENU_NORMAL_ITEM,
-  );
 
   // Data Loaders
   $items['admin/tripal/loaders/newic_phylotree_loader'] = array(
@@ -751,6 +741,20 @@ function tripal_chado_menu() {
     'access arguments' => array('administer tripal'),
   );
 
+  //////////////////////////////////////////////////////////////////////////////
+  //                           Phylogeny
+  //////////////////////////////////////////////////////////////////////////////
+  $items['taxonomy'] = array(
+    'title' => 'Taxonomy',
+    'description' => 'Taxonomic view of the species available on this site.',
+    'page callback' => 'tripal_phylogeny_taxonomy_view',
+    'access arguments' => array('access taxonomy content'),
+    'file' => '/includes/tripal_phylogeny.taxonomy.inc',
+    'type' => MENU_NORMAL_ITEM,
+    'file' =>  'includes/tripal_chado.taxonomy.inc',
+    'file path' => drupal_get_path('module', 'tripal_chado'),
+  );
+
   return $items;
 }
 
@@ -807,6 +811,8 @@ function tripal_chado_theme($existing, $type, $theme, $path) {
       'render element' => 'form',
       'file' => 'includes/tripal_chado.pub_search.inc',
     ),
+
+
   );
 
   // Override the theme for each entity to use the legacy modules