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

2 string references to 'tripal_phylogeny_ajax_get_tree_json'
tripal_chado_menu in tripal_chado/tripal_chado.module
Implements hook_menu().
tripal_phylogeny_menu in legacy/tripal_phylogeny/tripal_phylogeny.module
Implements hook_menu().

File

tripal_chado/includes/tripal_chado.phylotree.inc, line 92
This file contains the functions used for administration of the module

Code

function tripal_phylogeny_ajax_get_tree_json($phylotree_id) {

  $phylotree = chado_generate_var('phylotree', array('phylotree_id' => $phylotree_id));

  // For backwards compatibility with Tripal v2 and the legacy modules of
  // Tripal v3 we have two different SQL statements.
  if (module_exists('tripal_phylogeny')) {
    // This SQL gets all of the phylonodes for a given tree as well as the
    // features and organisms with which it is associated.  Each phylonode
    // can be associated with an organism 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_nid,
        fco.nid AS fo_organism_nid,
        co.nid AS organism_nid
      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
    ";
  }
  else {
    $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
      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 {organism} fo             ON f.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
      WHERE n.phylotree_id = :phylotree_id
    ";
  }
  $args = array(':phylotree_id' => $phylotree_id);
  $results = 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;

  if ($results) {
    while ($r = $results->fetchObject()) {
      $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' or $phylotree->type_id->name == 'Speces tree') {
        $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;
        if (module_exists('tripal_phylogeny')) {
          $node['feature_nid'] = (int) $r->feature_nid;
        }
        else {
          $entity_id = chado_get_record_entity_by_table('feature', $r->feature_id);
          $node['feature_eid'] = (int) $entity_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;
        if (module_exists('tripal_phylogeny')) {
          $node['organism_nid'] = (int) $r->organism_nid;
        }
        else {
          $entity_id = chado_get_record_entity_by_table('organism', $r->organism_id);
          $node['organism_eid'] = (int) $entity_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;
        if (module_exists('tripal_phylogeny')) {
          $node['fo_organism_nid'] = (int) $r->fo_organism_nid;
        }
        else {
          $entity_id = chado_get_record_entity_by_table('organism', $r->fo_organism_id);
          $node['fo_organism_eid'] = (int) $entity_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);
}