function tripal_phylogeny_ajax_get_tree_json
2.x tripal_phylogeny.module | tripal_phylogeny_ajax_get_tree_json($phylotree_id) |
3.x tripal_chado.phylotree.inc | tripal_phylogeny_ajax_get_tree_json($phylotree_id) |
Get json representation of a phylotree id.
This function is meant to be called via AJAX.
Parameters
int $phylotree_id: the ID of the phylotree node.
Return value
string json
Related topics
1 string reference to 'tripal_phylogeny_ajax_get_tree_json'
- tripal_phylogeny_menu in tripal_phylogeny/
tripal_phylogeny.module - Implements hook_menu().
File
- tripal_phylogeny/
tripal_phylogeny.module, line 283 - Integrates the Chado Phylotree module with Drupal Nodes & Views
Code
function tripal_phylogeny_ajax_get_tree_json($phylotree_id) {
$phylotree = chado_generate_var('phylotree', array('phylotree_id' => $phylotree_id));
// This SQL gets all of the phylonodes for a given tree as well as the
// features and organisms with which it is assocaited. Each phylonode
// can be associated with an orgnaism in one of two ways: 1) via a
// feature linked by the phylonode.feature_id field or 2) via a
// a record in the phylonde_organsim table. Therefore both types of
// organism records are returned in the query below, but those
// retrieved via a FK link on features are prefixed with 'fo_'.
$sql = "
SELECT
n.phylonode_id, n.parent_phylonode_id, n.label AS name, n.distance AS length,
f.feature_id, f.name AS feature_name,
cvt.name AS cvterm_name,
o.organism_id, o.common_name, o.abbreviation, o.genus, o.species,
fo.organism_id AS fo_organism_id, fo.common_name AS fo_common_name,
fo.abbreviation AS fo_abbreviation, fo.genus as fo_genus, fo.species AS fo_species,
cf.nid AS feature_node_id,
fco.nid AS fo_organism_node_id,
co.nid AS organism_node_id
FROM {phylonode} n
LEFT OUTER JOIN {cvterm} cvt ON n.type_id = cvt.cvterm_id
LEFT OUTER JOIN {feature} f ON n.feature_id = f.feature_id
LEFT OUTER JOIN [chado_feature] cf ON cf.feature_id = f.feature_id
LEFT OUTER JOIN {organism} fo ON f.organism_id = fo.organism_id
LEFT OUTER JOIN [chado_organism] fco ON fco.organism_id = fo.organism_id
LEFT OUTER JOIN {phylonode_organism} po ON po.phylonode_id = n.phylonode_id
LEFT OUTER JOIN {organism} o ON PO.organism_id = o.organism_id
LEFT OUTER JOIN [chado_organism] co ON co.organism_id = o.organism_id
WHERE n.phylotree_id = :phylotree_id
";
$args = array(':phylotree_id' => $phylotree_id);
$result = chado_query($sql, $args);
// Fetch all the phylonodes into an assoc array indexed by phylonode_id.
// Convert from resultset record to array, fixing datatypes. chado_query
// returns numeric as string and fun stuff like that.
$phylonodes = array();
$root_phylonode_ref = null;
foreach ($result as $r) {
$phylonode_id = (int) $r->phylonode_id;
// expect all nodes to have these properties
$node = array(
'phylonode_id' => $phylonode_id,
'parent_phylonode_id' => (int) $r->parent_phylonode_id,
'length' => (double) $r->length,
'cvterm_name' => $r->cvterm_name
);
// If the nodes are taxonomic then set an equal distance
if ($phylotree->type_id->name == 'taxonomy') {
$node['length'] = 0.001;
}
// Other props may exist only for leaf nodes
if ($r->name) {
$node['name'] = $r->name;
}
// If this node is associated with a feature then add in the details
if ($r->feature_id) {
$node['feature_id'] = (int) $r->feature_id;
$node['feature_name'] = $r->feature_name;
$node['feature_node_id'] = (int) $r->feature_node_id;
}
// Add in the organism fields when they are available via the
// phylonode_organism table.
if ($r->organism_id) {
$node['organism_id'] = (int) $r->organism_id;
$node['common_name'] = $r->common_name;
$node['abbreviation'] = $r->abbreviation;
$node['genus'] = $r->genus;
$node['species'] = $r->species;
$node['organism_node_id'] = (int) $r->organism_node_id;
// If the node does not have a name but is linked to an organism
// then set the name to be that of the genus and species.
if (!$r->name) {
$node['name'] = $r->genus . ' ' . $r->species;
}
}
// Add in the organism fields when they are available via the
// the phylonode.feature_id FK relationship.
if ($r->fo_organism_id) {
$node['fo_organism_id'] = (int) $r->fo_organism_id;
$node['fo_common_name'] = $r->fo_common_name;
$node['fo_abbreviation'] = $r->fo_abbreviation;
$node['fo_genus'] = $r->fo_genus;
$node['fo_species'] = $r->fo_species;
$node['fo_organism_node_id'] = (int) $r->fo_organism_node_id;
}
// Add this node to the list, organized by ID.
$phylonodes[$phylonode_id] = $node;
}
// Populate the children[] arrays for each node.
foreach ($phylonodes as $key => &$node) {
if ($node['parent_phylonode_id'] !== 0) {
$parent_ref = &$phylonodes[$node['parent_phylonode_id']];
// Append node refernce to children.
$parent_ref['children'][] = &$node;
}
else {
$root_phylonode_ref = &$node;
}
}
// dump datastructure as json to browser. drupal sets the mime-type correctly.
drupal_json_output($root_phylonode_ref);
}