tripal_phylogeny.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * @file
  4. * Integrates the Chado Phylotree module with Drupal Nodes & Views
  5. */
  6. /**
  7. * @defgroup tripal_phylogeny Phylotree Module
  8. * @ingroup tripal_modules
  9. * @{
  10. * Integrates the Chado Phylotree module with Drupal Nodes
  11. * @}
  12. */
  13. require_once 'api/tripal_phylogeny.api.inc';
  14. require_once 'theme/tripal_phylogeny.theme.inc';
  15. require_once 'includes/tripal_phylogeny.admin.inc';
  16. require_once 'includes/tripal_phylogeny.chado_node.inc';
  17. require_once 'includes/tripal_phylogeny.import_tree.inc';
  18. /**
  19. * Implements hook_permission().
  20. *
  21. * Set the permission types that the chado module uses. Essentially we
  22. * want permissionis that protect creation, editing and deleting of chado
  23. * data objects
  24. *
  25. * @ingroup tripal_phylogeny
  26. */
  27. function tripal_phylogeny_permission() {
  28. return array(
  29. 'access chado_phylotree content' => array(
  30. 'title' => t('View Phylotrees'),
  31. 'description' => t('Allow users to view phylotree pages.'),
  32. ),
  33. 'administer tripal phylotree' => array(
  34. 'title' => t('Administer Phylotrees'),
  35. 'description' => t('Allow users to administer all phylotrees.'),
  36. ),
  37. );
  38. }
  39. /**
  40. * Implements hook_menu().
  41. *
  42. * Menu items are automatically added for the new node types created
  43. * by this module to the 'Create Content' Navigation menu item. This function
  44. * adds more menu items needed for this module.
  45. *
  46. * @ingroup tripal_phylogeny
  47. */
  48. function tripal_phylogeny_menu() {
  49. $items = array();
  50. // administration landing page. currently has no content but is
  51. // apparently required for the Sync and Help links to work.
  52. $items['admin/tripal/legacy/tripal_phylogeny'] = array(
  53. 'title' => 'Phylogeny and Taxonomy',
  54. 'description' => 'Phylogenetic and taxonomic trees.',
  55. 'page callback' => 'tripal_phylogeny_admin_phylotrees_listing',
  56. 'access arguments' => array('administer tripal phylotree'),
  57. 'type' => MENU_NORMAL_ITEM,
  58. );
  59. // help menu
  60. $items['admin/tripal/legacy/tripal_phylogeny/help'] = array(
  61. 'title' => 'Help',
  62. 'description' => 'Basic Description of Tripal Phylotree Module Functionality',
  63. 'page callback' => 'theme',
  64. 'page arguments' => array('tripal_phylogeny_help'),
  65. 'access arguments' => array('administer tripal phylotree'),
  66. 'type' => MENU_LOCAL_TASK,
  67. 'weight' => 10
  68. );
  69. // configuration menu item
  70. $items['admin/tripal/legacy/tripal_phylogeny/configuration'] = array(
  71. 'title' => 'Settings',
  72. 'description' => 'Configure the Tripal Phylotree module',
  73. 'page callback' => 'drupal_get_form',
  74. 'page arguments' => array('tripal_phylogeny_admin'),
  75. 'access arguments' => array('administer tripal phylotree'),
  76. 'type' => MENU_LOCAL_TASK,
  77. 'weight' => 1
  78. );
  79. $items['admin/tripal/legacy/tripal_phylogeny/plots'] = array(
  80. 'title' => 'Plot Defaults',
  81. 'description' => 'Set defaults for the trees',
  82. 'page callback' => 'drupal_get_form',
  83. 'page arguments' => array('tripal_phylogeny_default_plots_form'),
  84. 'access arguments' => array('administer tripal phylotree'),
  85. 'type' => MENU_LOCAL_TASK,
  86. 'weight' => 2
  87. );
  88. // sync menu item (will be rendered as a tab by tripal)
  89. $items['admin/tripal/legacy/tripal_phylogeny/sync'] = array(
  90. 'title' => ' Sync',
  91. 'description' => 'Create pages on this site for phylotrees stored in Chado',
  92. 'page callback' => 'drupal_get_form',
  93. 'page arguments' => array('chado_node_sync_form', 'tripal_phylogeny', 'chado_phylotree'),
  94. 'access arguments' => array('administer tripal phylotree'),
  95. 'type' => MENU_LOCAL_TASK,
  96. 'weight' => 3
  97. );
  98. // Enable admin view
  99. $items['admin/tripal/legacy/tripal_phylogeny/views/phylotree/enable'] = array(
  100. 'title' => 'Enable Phylotree Administrative View',
  101. 'page callback' => 'tripal_enable_view',
  102. 'page arguments' => array('tripal_phylogeny_admin_phylotree', 'admin/tripal/legacy/tripal_phylogeny'),
  103. 'access arguments' => array('administer tripal phylotree'),
  104. 'type' => MENU_CALLBACK,
  105. );
  106. // create a route for viewing json of all phylonodes having this phylotree_id
  107. $items['ajax/chado_phylotree/%/json'] = array(
  108. 'page callback' => 'tripal_phylogeny_ajax_get_tree_json',
  109. 'page arguments' => array(2),
  110. // allow all anonymous http clients
  111. 'access callback' => TRUE
  112. );
  113. return $items;
  114. }
  115. /**
  116. * Implements hook_search_biological_data_views().
  117. *
  118. * Adds the described views to the "Search Data" Page created by Tripal Views
  119. */
  120. function tripal_phylogeny_search_biological_data_views() {
  121. return array(
  122. 'tripal_phylogeny_user_phylotree' => array(
  123. 'machine_name' => 'tripal_phylogeny_user_phylotree',
  124. 'human_name' => 'Phylogenetic Trees',
  125. 'description' => 'Gene trees, species trees, etc.',
  126. 'link' => 'chado/phylotree'
  127. ),
  128. );
  129. }
  130. /**
  131. * Implements hook_views_api().
  132. *
  133. * Essentially this hook tells drupal that there is views support for
  134. * for this module which then includes tripal_db.views.inc where all the
  135. * views integration code is
  136. *
  137. * @ingroup tripal_phylogeny
  138. */
  139. function tripal_phylogeny_views_api() {
  140. return array(
  141. 'api' => 3.0,
  142. );
  143. }
  144. /**
  145. * Implements hook_theme().
  146. *
  147. * We need to let drupal know about our theme functions and their arguments.
  148. * We create theme functions to allow users of the module to customize the
  149. * look and feel of the output generated in this module
  150. *
  151. * @ingroup tripal_phylogeny
  152. */
  153. function tripal_phylogeny_theme($existing, $type, $theme, $path) {
  154. $core_path = drupal_get_path('module', 'tripal_core');
  155. $items = array(
  156. // built-in theme
  157. 'node__chado_phylotree' => array(
  158. 'template' => 'node--chado-generic',
  159. 'render element' => 'node',
  160. 'base hook' => 'node',
  161. 'path' => "$core_path/theme/templates",
  162. ),
  163. // base template for this page (default tab) includes the phylogram
  164. 'tripal_phylogeny_base' => array(
  165. 'variables' => array('node' => NULL),
  166. 'template' => 'tripal_phylogeny_base',
  167. 'path' => "$path/theme/templates",
  168. ),
  169. // Template for the phylogram.
  170. 'tripal_phylogeny_phylogram' => array(
  171. 'variables' => array('node' => NULL),
  172. 'template' => 'tripal_phylogeny_phylogram',
  173. 'path' => "$path/theme/templates",
  174. ),
  175. // Template for the taxonomic tree.
  176. 'tripal_phylogeny_taxonomic_tree' => array(
  177. 'variables' => array('node' => NULL),
  178. 'template' => 'tripal_phylogeny_taxonomic_tree',
  179. 'path' => "$path/theme/templates",
  180. ),
  181. // partial for organisms block
  182. 'tripal_phylogeny_organisms' => array(
  183. 'variables' => array('node' => NULL),
  184. 'template' => 'tripal_phylogeny_organisms',
  185. 'path' => "$path/theme/templates",
  186. ),
  187. // partial for cross references block
  188. 'tripal_phylogeny_references' => array(
  189. 'variables' => array('node' => NULL),
  190. 'template' => 'tripal_phylogeny_references',
  191. 'path' => "$path/theme/templates",
  192. ),
  193. // partial for cross references block
  194. 'tripal_phylogeny_analysis' => array(
  195. 'variables' => array('node' => NULL),
  196. 'template' => 'tripal_phylogeny_analysis',
  197. 'path' => "$path/theme/templates",
  198. ),
  199. // partial for teaser view
  200. 'tripal_phylogeny_teaser' => array(
  201. 'variables' => array('node' => NULL),
  202. 'template' => 'tripal_phylogeny_teaser',
  203. 'path' => "$path/theme/templates",
  204. ),
  205. // FORM THEMES
  206. // Theme function for the project table in admin projects form
  207. 'tripal_phylogeny_admin_org_color_tables' => array(
  208. 'render element' => 'element',
  209. )
  210. );
  211. return $items;
  212. }
  213. /**
  214. * Implements hook_help().
  215. * Adds a help page to the module list
  216. *
  217. * @ingroup tripal_phylogeny
  218. */
  219. function tripal_phylogeny_help ($path, $arg) {
  220. if ($path == 'admin/help#tripal_phylogeny') {
  221. return theme('tripal_phylogeny_help', array());
  222. }
  223. }
  224. /**
  225. * Get json representation of a phylotree id.
  226. *
  227. * This function is meant to be called via AJAX.
  228. *
  229. * @param int $phylotree_id
  230. * the ID of the phylotree node.
  231. *
  232. * @return string json
  233. *
  234. * @ingroup tripal_phylogeny
  235. */
  236. function tripal_phylogeny_ajax_get_tree_json($phylotree_id) {
  237. $phylotree = chado_generate_var('phylotree', array('phylotree_id' => $phylotree_id));
  238. // This SQL gets all of the phylonodes for a given tree as well as the
  239. // features and organisms with which it is assocaited. Each phylonode
  240. // can be associated with an organism in one of two ways: 1) via a
  241. // feature linked by the phylonode.feature_id field or 2) via a
  242. // a record in the phylonde_organsim table. Therefore both types of
  243. // organism records are returned in the query below, but those
  244. // retrieved via a FK link on features are prefixed with 'fo_'.
  245. $sql = "
  246. SELECT
  247. n.phylonode_id, n.parent_phylonode_id, n.label AS name, n.distance AS length,
  248. f.feature_id, f.name AS feature_name,
  249. cvt.name AS cvterm_name,
  250. o.organism_id, o.common_name, o.abbreviation, o.genus, o.species,
  251. fo.organism_id AS fo_organism_id, fo.common_name AS fo_common_name,
  252. fo.abbreviation AS fo_abbreviation, fo.genus as fo_genus, fo.species AS fo_species,
  253. cf.nid AS feature_node_id,
  254. fco.nid AS fo_organism_node_id,
  255. co.nid AS organism_node_id
  256. FROM {phylonode} n
  257. LEFT OUTER JOIN {cvterm} cvt ON n.type_id = cvt.cvterm_id
  258. LEFT OUTER JOIN {feature} f ON n.feature_id = f.feature_id
  259. LEFT OUTER JOIN [chado_feature] cf ON cf.feature_id = f.feature_id
  260. LEFT OUTER JOIN {organism} fo ON f.organism_id = fo.organism_id
  261. LEFT OUTER JOIN [chado_organism] fco ON fco.organism_id = fo.organism_id
  262. LEFT OUTER JOIN {phylonode_organism} po ON po.phylonode_id = n.phylonode_id
  263. LEFT OUTER JOIN {organism} o ON PO.organism_id = o.organism_id
  264. LEFT OUTER JOIN [chado_organism] co ON co.organism_id = o.organism_id
  265. WHERE n.phylotree_id = :phylotree_id
  266. ";
  267. $args = array(':phylotree_id' => $phylotree_id);
  268. $result = chado_query($sql, $args);
  269. // Fetch all the phylonodes into an assoc array indexed by phylonode_id.
  270. // Convert from resultset record to array, fixing datatypes. chado_query
  271. // returns numeric as string and fun stuff like that.
  272. $phylonodes = array();
  273. $root_phylonode_ref = null;
  274. foreach ($result as $r) {
  275. $phylonode_id = (int) $r->phylonode_id;
  276. // expect all nodes to have these properties
  277. $node = array(
  278. 'phylonode_id' => $phylonode_id,
  279. 'parent_phylonode_id' => (int) $r->parent_phylonode_id,
  280. 'length' => (double) $r->length,
  281. 'cvterm_name' => $r->cvterm_name
  282. );
  283. // If the nodes are taxonomic then set an equal distance
  284. if ($phylotree->type_id->name == 'taxonomy') {
  285. $node['length'] = 0.001;
  286. }
  287. // Other props may exist only for leaf nodes
  288. if ($r->name) {
  289. $node['name'] = $r->name;
  290. }
  291. // If this node is associated with a feature then add in the details
  292. if ($r->feature_id) {
  293. $node['feature_id'] = (int) $r->feature_id;
  294. $node['feature_name'] = $r->feature_name;
  295. $node['feature_node_id'] = (int) $r->feature_node_id;
  296. }
  297. // Add in the organism fields when they are available via the
  298. // phylonode_organism table.
  299. if ($r->organism_id) {
  300. $node['organism_id'] = (int) $r->organism_id;
  301. $node['common_name'] = $r->common_name;
  302. $node['abbreviation'] = $r->abbreviation;
  303. $node['genus'] = $r->genus;
  304. $node['species'] = $r->species;
  305. $node['organism_node_id'] = (int) $r->organism_node_id;
  306. // If the node does not have a name but is linked to an organism
  307. // then set the name to be that of the genus and species.
  308. if (!$r->name) {
  309. $node['name'] = $r->genus . ' ' . $r->species;
  310. }
  311. }
  312. // Add in the organism fields when they are available via the
  313. // the phylonode.feature_id FK relationship.
  314. if ($r->fo_organism_id) {
  315. $node['fo_organism_id'] = (int) $r->fo_organism_id;
  316. $node['fo_common_name'] = $r->fo_common_name;
  317. $node['fo_abbreviation'] = $r->fo_abbreviation;
  318. $node['fo_genus'] = $r->fo_genus;
  319. $node['fo_species'] = $r->fo_species;
  320. $node['fo_organism_node_id'] = (int) $r->fo_organism_node_id;
  321. }
  322. // Add this node to the list, organized by ID.
  323. $phylonodes[$phylonode_id] = $node;
  324. }
  325. // Populate the children[] arrays for each node.
  326. foreach ($phylonodes as $key => &$node) {
  327. if ($node['parent_phylonode_id'] !== 0) {
  328. $parent_ref = &$phylonodes[ $node['parent_phylonode_id']];
  329. // Append node refernce to children.
  330. $parent_ref['children'][] = &$node;
  331. }
  332. else {
  333. $root_phylonode_ref = &$node;
  334. }
  335. }
  336. // dump datastructure as json to browser. drupal sets the mime-type correctly.
  337. drupal_json_output($root_phylonode_ref);
  338. }