tripal_phylogeny.module 13 KB

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