private function TaxonomyImporter::rebuildTree
3.x TaxonomyImporter.inc | private TaxonomyImporter::rebuildTree() |
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.
1 call to TaxonomyImporter::rebuildTree()
- TaxonomyImporter::run in tripal_chado/
includes/ TripalImporter/ TaxonomyImporter.inc - Performs the import.
File
- tripal_chado/
includes/ TripalImporter/ TaxonomyImporter.inc, line 311
Class
Code
private function rebuildTree() {
$lineage_nodes[] = array();
// Get the "rank" cvterm. It requires that the TAXRANK vocabulary is loaded.
$rank_cvterm = tripal_chado_get_cvtermget_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(),
);
$total = count($this->all_orgs);
$j = 1;
foreach ($this->all_orgs as $organism) {
$sci_name = chado_get_organism_scientific_name($organism);
//$this->logMessage("- " . ($j++) . " of $total. Adding @organism", array('@organism' => $sci_name));
// 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,
);
$columns = array('*');
$phylonode = chado_select_record('phylonode', $columns, $values);
if (count($phylonode) == 0) {
$lineage_nodes[$child] = NULL;
$lineage_good = FALSE;
continue;
}
$phylonode = $phylonode[0];
$lineage_nodes[$child] = $phylonode;
$values = array(
'phylonode_id' => $phylonode->phylonode_id,
'type_id' => $rank_cvterm->cvterm_id,
);
$columns = array('*');
$phylonodeprop = chado_select_record('phylonodeprop', $columns, $values);
}
$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(
$rank_cvterm->cvterm_id => $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;
}
$rank_type = 'species';
if (property_exists($organism, 'type_id') and $organism->type_id) {
$rank_type = $organism->type;
}
// Now add in the leaf node
$sci_name = chado_get_organism_scientific_name($organism);
$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_type,
),
);
$this->addTaxonomyNode($tree, $node, $lineage_depth);
// Set the indecies for the tree.
chado_assign_phylogeny_tree_indices($tree);
}
return $tree;
}