public function TaxonomyImporter::run

3.x TaxonomyImporter.inc public TaxonomyImporter::run()

Performs the import.

Overrides TripalImporter::run

File

tripal_chado/includes/TripalImporter/TaxonomyImporter.inc, line 184

Class

TaxonomyImporter

Code

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.
  if (chado_get_version() > 1.2) {
    $sql = "
        SELECT O.*, CVT.name as type
        FROM {organism} O
         LEFT JOIN {cvterm} CVT ON CVT.cvterm_id = O.type_id
        ORDER BY O.genus, O.species
      ";
  }
  else {
    $sql = "
        SELECT O.*, '' as type
        FROM {organism} O
        ORDER BY O.genus, O.species
      ";
  }
  $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);
  chado_phylogeny_import_tree($this->tree, $this->phylotree, $options);
}