tripal_phylogeny.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the phylotree module
  5. */
  6. /**
  7. * Implements hook_install().
  8. *
  9. * @ingroup tripal_phylogeny
  10. */
  11. function tripal_phylogeny_install() {
  12. // Add the vocabularies used by the feature module.
  13. tripal_phylogeny_add_cvterms();
  14. // Set the default vocabularies.
  15. tripal_set_default_cv('phylonode', 'type_id', 'tripal_phylogeny');
  16. tripal_set_default_cv('phylotree', 'type_id', 'sequence');
  17. // Add the materializedviews.
  18. tripal_phylogeny_add_mview();
  19. // We want to integrate the materialized views so that they
  20. // are available for Drupal Views, upon which our search forms are built.
  21. tripal_phylogeny_integrate_view();
  22. $mview_id = tripal_get_mview_id('phylotree_count');
  23. // SPF: commented out automatic populate of MView. If the query fails
  24. // for some reason the install will not properly complete.
  25. // tripal_populate_mview($mview_id);
  26. // Add the custom tables.
  27. tripal_phylogeny_add_custom_tables();
  28. // Add an index on the phylonode table.
  29. $exists = chado_index_exists('phylonode', 'parent_phylonode_id');
  30. if (!$exists) {
  31. chado_add_index('phylonode', 'parent_phylonode_id', array('parent_phylonode_id'));
  32. }
  33. // Add in the variables that this module will use to store properties for
  34. // loading of the tree files.
  35. tripal_insert_variable('phylotree_name_re', 'The regular expression for matching a name in a string.');
  36. tripal_insert_variable('phylotree_use_uniquename', 'Set to 1 if features should be matched using the unqiuename rather than the name.');
  37. tripal_insert_variable('phylotree_tree_file', 'Holds the Drupal file ID for the uploaded tree file.');
  38. }
  39. /**
  40. * Implements hook_disable().
  41. *
  42. * Disable default views when module is disabled
  43. *
  44. * @ingroup tripal_phylogeny
  45. */
  46. function tripal_phylogeny_disable() {
  47. // Disable all default views provided by this module
  48. require_once("tripal_phylogeny.views_default.inc");
  49. $views = tripal_phylogeny_views_default_views();
  50. foreach (array_keys($views) as $view_name) {
  51. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  52. }
  53. }
  54. /**
  55. * Implementation of hook_requirements().
  56. *
  57. * @ingroup tripal_phylogeny
  58. */
  59. function tripal_phylogeny_requirements($phase) {
  60. $requirements = array();
  61. if ($phase == 'install') {
  62. // Make sure chado is installed.
  63. if (!$GLOBALS["chado_is_installed"]) {
  64. $requirements ['tripal_phylogeny'] = array(
  65. 'title' => "tripal_phylogeny",
  66. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  67. 'severity' => REQUIREMENT_ERROR,
  68. );
  69. }
  70. }
  71. return $requirements;
  72. }
  73. /**
  74. * Implementation of hook_schema().
  75. * Standard tripal linker table between a node and a phylotree record.
  76. * @ingroup tripal_phylogeny
  77. */
  78. function tripal_phylogeny_schema() {
  79. $schema['chado_phylotree'] = array(
  80. 'fields' => array(
  81. 'vid' => array(
  82. 'type' => 'int',
  83. 'unsigned' => TRUE,
  84. 'not null' => TRUE,
  85. 'default' => 0
  86. ),
  87. 'nid' => array(
  88. 'type' => 'int',
  89. 'unsigned' => TRUE,
  90. 'not null' => TRUE,
  91. 'default' => 0
  92. ),
  93. 'phylotree_id' => array(
  94. 'type' => 'int',
  95. 'not null' => TRUE,
  96. 'default' => 0
  97. )
  98. ),
  99. 'indexes' => array(
  100. 'chado_phylotree_idx1' => array('phylotree_id')
  101. ),
  102. 'unique keys' => array(
  103. 'chado_phylotree_uq1' => array('nid', 'vid'),
  104. 'chado_phylotree_uq2' => array('vid')
  105. ),
  106. 'primary key' => array('nid'),
  107. );
  108. return $schema;
  109. }
  110. /**
  111. * Adds controlled vocabulary terms needed by this module.
  112. *
  113. * @ingroup tripal_phylogeny
  114. */
  115. function tripal_phylogeny_add_cvterms() {
  116. tripal_insert_cv(
  117. 'tripal_phylogeny',
  118. 'Terms used by the Tripal phylotree module for phylogenetic and taxonomic trees.'
  119. );
  120. // Add the terms used to identify nodes in the tree.
  121. tripal_insert_cvterm(
  122. array(
  123. 'name' => 'phylo_leaf',
  124. 'definition' => 'A leaf node in a phylogenetic tree.',
  125. 'cv_name' => 'tripal_phylogeny',
  126. 'is_relationship' => 0,
  127. 'db_name' => 'tripal'
  128. ),
  129. array('update_existing' => TRUE)
  130. );
  131. // Add the terms used to identify nodes in the tree.
  132. tripal_insert_cvterm(
  133. array(
  134. 'name' => 'phylo_root',
  135. 'definition' => 'The root node of a phylogenetic tree.',
  136. 'cv_name' => 'tripal_phylogeny',
  137. 'is_relationship' => 0,
  138. 'db_name' => 'tripal'
  139. ),
  140. array('update_existing' => TRUE)
  141. );
  142. // Add the terms used to identify nodes in the tree.
  143. tripal_insert_cvterm(
  144. array(
  145. 'name' => 'phylo_interior',
  146. 'definition' => 'An interior node in a phylogenetic tree.',
  147. 'cv_name' => 'tripal_phylogeny',
  148. 'is_relationship' => 0,
  149. 'db_name' => 'tripal'
  150. ),
  151. array('update_existing' => TRUE)
  152. );
  153. // Add the terms used to identify nodes in the tree.
  154. tripal_insert_cvterm(
  155. array(
  156. 'name' => 'taxonomy',
  157. 'definition' => 'A term used to indicate if a phylotree is a taxonomic tree',
  158. 'cv_name' => 'tripal_phylogeny',
  159. 'is_relationship' => 0,
  160. 'db_name' => 'tripal'
  161. ),
  162. array('update_existing' => TRUE)
  163. );
  164. tripal_insert_cvterm(
  165. array(
  166. 'name' => 'rank',
  167. 'definition' => 'A taxonmic rank',
  168. 'cv_name' => 'tripal_phylogeny',
  169. 'is_relationship' => 0,
  170. 'db_name' => 'tripal'
  171. ),
  172. array('update_existing' => TRUE)
  173. );
  174. // // ----------------------------------------------
  175. // // Add the terms for importing NCBI taxonomy data
  176. // $values = array(
  177. // 'name' => 'taxonomy',
  178. // 'description' => 'The NCBI Taxonomy Database is a curated classification and nomenclature for all of the organisms in the public sequence databases. This currently represents about 10% of the described species of life on the planet.',
  179. // 'url' => 'http://www.ncbi.nlm.nih.gov/taxonomy',
  180. // 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?id=',
  181. // );
  182. // tripal_insert_db($values);
  183. // tripal_insert_cv('taxonomy', 'A taxonomy for NCBI taxonomy ranks');
  184. // $terms = array('rank', 'taxid', 'class', 'cohort', 'family', 'forma', 'genus', 'infraclass',
  185. // 'infraorder', 'kingdom', 'no rank', 'order', 'parvorder', 'phylum',
  186. // 'species', 'species group', 'species subgroup', 'subclass', 'subcohort',
  187. // 'subfamily', 'subgenus', 'subkingdom', 'suborder', 'subphylum',
  188. // 'subspecies', 'subtribe', 'superclass', 'superfamily', 'superkingdom',
  189. // 'superorder', 'superphylum', 'tribe', 'varietas');
  190. // $options = array('update_existing' => TRUE);
  191. // foreach ($terms as $term) {
  192. // $value = array(
  193. // 'name' => $term,
  194. // 'definition' => '',
  195. // 'cv_name' => 'taxonomy',
  196. // 'is_relationship' => 0,
  197. // 'db_name' => 'taxonomy'
  198. // );
  199. // tripal_insert_cvterm($value, $options);
  200. // }
  201. $terms = array(
  202. 'lineage',
  203. 'genetic_code',
  204. 'genetic_code_name',
  205. 'mitochondrial_genetic_code',
  206. 'mitochondrial_genetic_code_name',
  207. 'division',
  208. 'genbank_common_name',
  209. 'synonym',
  210. 'other_name',
  211. 'equivalent_name',
  212. 'anamorph'
  213. );
  214. $options = array('update_existing' => TRUE);
  215. foreach ($terms as $term) {
  216. $value = array(
  217. 'name' => $term,
  218. 'definition' => '',
  219. 'cv_name' => 'organism_property',
  220. 'is_relationship' => 0,
  221. 'db_name' => 'tripal'
  222. );
  223. tripal_insert_cvterm($value, $options);
  224. }
  225. }
  226. /**
  227. * Implementation of hook_uninstall().
  228. */
  229. function tripal_phylogeny_uninstall() {
  230. // Drop the MView table if it exists
  231. $mview_id = tripal_get_mview_id('phylotree_count');
  232. if ($mview_id) {
  233. tripal_delete_mview($mview_id);
  234. }
  235. // Remove views integration.
  236. // Note: tripal_remove_views_intergration accepts table_name and priority in a key value form.
  237. $delete_view=array(
  238. 'table_name' => 'phylotree_count',
  239. 'priority' => '-1',
  240. );
  241. tripal_remove_views_integration($delete_view);
  242. }
  243. function tripal_phylogeny_add_mview(){
  244. // Materialized view addition.
  245. $sql_query="
  246. WITH count_genes as
  247. (SELECT count(*) count, t.phylotree_id
  248. FROM phylotree t, phylonode n
  249. WHERE n.phylotree_id = t.phylotree_id AND n.label is NOT NULL
  250. GROUP BY t.phylotree_id)
  251. SELECT
  252. phylotree.phylotree_id AS phylotree_phylotree_id,
  253. phylotree.name AS phylotree_name,
  254. phylotree.comment AS phylotree_comment,
  255. count_genes.count AS total_count
  256. FROM chado.phylotree phylotree
  257. LEFT JOIN chado_phylotree chado_phylotree ON phylotree.phylotree_id = chado_phylotree.phylotree_id
  258. LEFT JOIN count_genes count_genes ON phylotree.phylotree_id = count_genes.phylotree_id
  259. ";
  260. // Table Phylotree User Search description.
  261. $schema = array (
  262. 'table' => 'phylotree_count',
  263. 'fields' => array(
  264. 'phylotree_phylotree_id' => array(
  265. 'type' => 'int',
  266. 'not null' => FALSE,
  267. ),
  268. 'phylotree_name' => array(
  269. 'type' => 'text',
  270. 'not null' => FALSE,
  271. ),
  272. 'phylotree_comment' => array(
  273. 'type' => 'text',
  274. 'not null' => FALSE,
  275. ),
  276. 'total_count' => array(
  277. 'type' => 'int',
  278. 'not null' => TRUE,
  279. ),
  280. ),
  281. 'primary key' => array('phylotree_phylotree_id'),
  282. );
  283. // Add a comment to make sure this view makes sense to the site administator.
  284. $comment = t('This view is used to provide a table for Phylotree User Search with total count.');
  285. tripal_add_mview('phylotree_count', 'tripal_phylogeny', $schema, $sql_query, $comment);
  286. }
  287. /**
  288. * Integrate the qtl_search materialized view for use by Drupal Views and
  289. * our search form
  290. */
  291. function tripal_phylogeny_integrate_view(){
  292. $integration = array (
  293. 'table' => 'phylotree_count',
  294. 'name' => 'phylotree_count',
  295. 'type' => 'chado',
  296. 'description' => '',
  297. 'priority' => '-1',
  298. 'base_table' => '1',
  299. 'fields' => array (
  300. 'phylotree_phylotree_id' => array (
  301. 'name' => 'phylotree_phylotree_id',
  302. 'title' => 'Phylotree ID',
  303. 'description' => 'Phylotree ID',
  304. 'type' => 'int',
  305. 'handlers' => array (
  306. 'filter' => array (
  307. 'name' => 'views_handler_filter_numeric'
  308. ),
  309. 'field' => array (
  310. 'name' => 'views_handler_field_numeric'
  311. ),
  312. 'sort' => array (
  313. 'name' => 'views_handler_sort'
  314. ),
  315. 'argument' => array (
  316. 'name' => 'views_handler_argument_numeric'
  317. ),
  318. 'relationship' => array (
  319. 'name' => 'views_handler_relationship'
  320. )
  321. ),
  322. 'joins' => array ()
  323. ),
  324. 'phylotree_name' => array (
  325. 'name' => 'phylotree_name',
  326. 'title' => 'Family ID',
  327. 'description' => 'Family ID',
  328. 'type' => 'text',
  329. 'handlers' => array (
  330. 'filter' => array (
  331. 'name' => 'tripal_views_handler_filter_select_string'
  332. ),
  333. 'field' => array (
  334. 'name' => 'views_handler_field'
  335. ),
  336. 'sort' => array (
  337. 'name' => 'views_handler_sort'
  338. ),
  339. 'argument' => array (
  340. 'name' => 'views_handler_argument_string'
  341. ),
  342. 'relationship' => array (
  343. 'name' => 'views_handler_relationship'
  344. )
  345. ),
  346. 'joins' => array ()
  347. ),
  348. 'phylotree_comment' => array (
  349. 'name' => 'phylotree_comment',
  350. 'title' => 'Description',
  351. 'description' => 'Description',
  352. 'type' => 'text',
  353. 'handlers' => array (
  354. 'filter' => array (
  355. 'name' => 'tripal_views_handler_filter_select_string'
  356. ),
  357. 'field' => array (
  358. 'name' => 'views_handler_field'
  359. ),
  360. 'sort' => array (
  361. 'name' => 'views_handler_sort'
  362. ),
  363. 'argument' => array (
  364. 'name' => 'views_handler_argument_string'
  365. ),
  366. 'relationship' => array (
  367. 'name' => 'views_handler_relationship'
  368. )
  369. ),
  370. 'joins' => array ()
  371. ),
  372. 'total_count' => array (
  373. 'name' => 'total_count',
  374. 'title' => 'Total count',
  375. 'description' => 'Total count',
  376. 'type' => 'int',
  377. 'handlers' => array (
  378. 'filter' => array (
  379. 'name' => 'views_handler_filter_numeric'
  380. ),
  381. 'field' => array (
  382. 'name' => 'views_handler_field'
  383. ),
  384. 'sort' => array (
  385. 'name' => 'views_handler_sort'
  386. ),
  387. 'argument' => array (
  388. 'name' => 'views_handler_argument_numeric'
  389. ),
  390. 'relationship' => array (
  391. 'name' => 'views_handler_relationship'
  392. )
  393. ),
  394. 'joins' => array ()
  395. )
  396. )
  397. );
  398. // Add the array above that will integrate our qtl_search materialized view
  399. // for use with Drupal Views.
  400. tripal_add_views_integration($integration);
  401. }
  402. /**
  403. * Add any custom tables needed by this module.
  404. * - phylotreeprop: keep track of properties of phylotree
  405. *
  406. * @ingroup tripal_phylogeny
  407. */
  408. function tripal_phylogeny_add_custom_tables() {
  409. $schema = array (
  410. 'table' => 'phylotreeprop',
  411. 'fields' => array (
  412. 'phylotreeprop_id' => array (
  413. 'type' => 'serial',
  414. 'not null' => true,
  415. ),
  416. 'phylotree_id' => array (
  417. 'type' => 'int',
  418. 'not null' => true,
  419. ),
  420. 'type_id' => array (
  421. 'type' => 'int',
  422. 'not null' => true,
  423. ),
  424. 'value' => array (
  425. 'type' => 'text',
  426. 'not null' => false,
  427. ),
  428. 'rank' => array (
  429. 'type' => 'int',
  430. 'not null' => true,
  431. 'default' => 0,
  432. ),
  433. ),
  434. 'primary key' => array (
  435. 0 => 'phylotreeprop_id',
  436. ),
  437. 'unique keys' => array (
  438. 'phylotreeprop_c1' => array (
  439. 0 => 'phylotree_id',
  440. 1 => 'type_id',
  441. 2 => 'rank',
  442. ),
  443. ),
  444. 'indexes' => array (
  445. 'phylotreeprop_idx1' => array (
  446. 0 => 'phylotree_id',
  447. ),
  448. 'phylotreeprop_idx2' => array (
  449. 0 => 'type_id',
  450. ),
  451. ),
  452. 'foreign keys' => array (
  453. 'cvterm' => array (
  454. 'table' => 'cvterm',
  455. 'columns' => array (
  456. 'type_id' => 'cvterm_id',
  457. ),
  458. ),
  459. 'phylotree' => array (
  460. 'table' => 'phylotree',
  461. 'columns' => array (
  462. 'phylotree_id' => 'phylotree_id',
  463. ),
  464. ),
  465. ),
  466. );
  467. chado_create_custom_table('phylotreeprop', $schema, TRUE);
  468. }
  469. /**
  470. * Adds the vocabulary needed for storing taxonmies.
  471. */
  472. function tripal_phylogeny_update_7200() {
  473. try {
  474. tripal_phylogeny_add_cvterms();
  475. }
  476. catch (\PDOException $e) {
  477. $error = $e->getMessage();
  478. throw new DrupalUpdateException('Failed to complete update' . $error);
  479. }
  480. }