private function TaxonomyImporter::importRecord

3.x TaxonomyImporter.inc private TaxonomyImporter::importRecord($taxid, $organism = NULL)

Imports an organism from the NCBI taxonomy DB by its taxonomy ID

Parameters

$taxid: The NCBI Taxonomy ID.

$organism: The organism object to which this taxonomy belongs. If the organism is NULL then it will be created.

2 calls to TaxonomyImporter::importRecord()
TaxonomyImporter::run in tripal_chado/includes/TripalImporter/TaxonomyImporter.inc
Performs the import.
TaxonomyImporter::updateExisting in tripal_chado/includes/TripalImporter/TaxonomyImporter.inc
Imports details from NCBI Taxonomy for organisms that alrady exist.

File

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

Class

TaxonomyImporter

Code

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 = chado_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;
    //$this->logMessage(' - Importing @sci_name', array('@sci_name' => $sci_name));

    // 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)));
        }
      }
    }

    // Associate the Dbxref with the organism.
    $this->addDbxref($organism->organism_id, $taxid);

    // 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.
    chado_assign_phylogeny_tree_indices($this->tree);
  }
}