tripal_phylogeny.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the phylotree module
  5. */
  6. /**
  7. * Implements hook_install().
  8. *
  9. * @ingroup tripal_legacy_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_legacy_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_legacy_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_legacy_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_legacy_phylogeny
  114. */
  115. function tripal_phylogeny_add_cvterms() {
  116. }
  117. /**
  118. * Implementation of hook_uninstall().
  119. */
  120. function tripal_phylogeny_uninstall() {
  121. // Drop the MView table if it exists
  122. $mview_id = tripal_get_mview_id('phylotree_count');
  123. if ($mview_id) {
  124. tripal_delete_mview($mview_id);
  125. }
  126. // Remove views integration.
  127. // Note: tripal_remove_views_intergration accepts table_name and priority in a key value form.
  128. $delete_view=array(
  129. 'table_name' => 'phylotree_count',
  130. 'priority' => '-1',
  131. );
  132. tripal_remove_views_integration($delete_view);
  133. }
  134. function tripal_phylogeny_add_mview(){
  135. // Materialized view addition.
  136. $sql_query="
  137. WITH count_genes as
  138. (SELECT count(*) count, t.phylotree_id
  139. FROM phylotree t, phylonode n
  140. WHERE n.phylotree_id = t.phylotree_id AND n.label is NOT NULL
  141. GROUP BY t.phylotree_id)
  142. SELECT
  143. phylotree.phylotree_id AS phylotree_phylotree_id,
  144. phylotree.name AS phylotree_name,
  145. phylotree.comment AS phylotree_comment,
  146. count_genes.count AS total_count
  147. FROM chado.phylotree phylotree
  148. LEFT JOIN chado_phylotree chado_phylotree ON phylotree.phylotree_id = chado_phylotree.phylotree_id
  149. LEFT JOIN count_genes count_genes ON phylotree.phylotree_id = count_genes.phylotree_id
  150. ";
  151. // Table Phylotree User Search description.
  152. $schema = array (
  153. 'table' => 'phylotree_count',
  154. 'fields' => array(
  155. 'phylotree_phylotree_id' => array(
  156. 'type' => 'int',
  157. 'not null' => FALSE,
  158. ),
  159. 'phylotree_name' => array(
  160. 'type' => 'text',
  161. 'not null' => FALSE,
  162. ),
  163. 'phylotree_comment' => array(
  164. 'type' => 'text',
  165. 'not null' => FALSE,
  166. ),
  167. 'total_count' => array(
  168. 'type' => 'int',
  169. 'not null' => TRUE,
  170. ),
  171. ),
  172. 'primary key' => array('phylotree_phylotree_id'),
  173. );
  174. // Add a comment to make sure this view makes sense to the site administator.
  175. $comment = t('This view is used to provide a table for Phylotree User Search with total count.');
  176. tripal_add_mview('phylotree_count', 'tripal_phylogeny', $schema, $sql_query, $comment);
  177. }
  178. /**
  179. * Integrate the qtl_search materialized view for use by Drupal Views and
  180. * our search form
  181. */
  182. function tripal_phylogeny_integrate_view(){
  183. $integration = array (
  184. 'table' => 'phylotree_count',
  185. 'name' => 'phylotree_count',
  186. 'type' => 'chado',
  187. 'description' => '',
  188. 'priority' => '-1',
  189. 'base_table' => '1',
  190. 'fields' => array (
  191. 'phylotree_phylotree_id' => array (
  192. 'name' => 'phylotree_phylotree_id',
  193. 'title' => 'Phylotree ID',
  194. 'description' => 'Phylotree ID',
  195. 'type' => 'int',
  196. 'handlers' => array (
  197. 'filter' => array (
  198. 'name' => 'views_handler_filter_numeric'
  199. ),
  200. 'field' => array (
  201. 'name' => 'views_handler_field_numeric'
  202. ),
  203. 'sort' => array (
  204. 'name' => 'views_handler_sort'
  205. ),
  206. 'argument' => array (
  207. 'name' => 'views_handler_argument_numeric'
  208. ),
  209. 'relationship' => array (
  210. 'name' => 'views_handler_relationship'
  211. )
  212. ),
  213. 'joins' => array ()
  214. ),
  215. 'phylotree_name' => array (
  216. 'name' => 'phylotree_name',
  217. 'title' => 'Family ID',
  218. 'description' => 'Family ID',
  219. 'type' => 'text',
  220. 'handlers' => array (
  221. 'filter' => array (
  222. 'name' => 'tripal_views_handler_filter_select_string'
  223. ),
  224. 'field' => array (
  225. 'name' => 'views_handler_field'
  226. ),
  227. 'sort' => array (
  228. 'name' => 'views_handler_sort'
  229. ),
  230. 'argument' => array (
  231. 'name' => 'views_handler_argument_string'
  232. ),
  233. 'relationship' => array (
  234. 'name' => 'views_handler_relationship'
  235. )
  236. ),
  237. 'joins' => array ()
  238. ),
  239. 'phylotree_comment' => array (
  240. 'name' => 'phylotree_comment',
  241. 'title' => 'Description',
  242. 'description' => 'Description',
  243. 'type' => 'text',
  244. 'handlers' => array (
  245. 'filter' => array (
  246. 'name' => 'tripal_views_handler_filter_select_string'
  247. ),
  248. 'field' => array (
  249. 'name' => 'views_handler_field'
  250. ),
  251. 'sort' => array (
  252. 'name' => 'views_handler_sort'
  253. ),
  254. 'argument' => array (
  255. 'name' => 'views_handler_argument_string'
  256. ),
  257. 'relationship' => array (
  258. 'name' => 'views_handler_relationship'
  259. )
  260. ),
  261. 'joins' => array ()
  262. ),
  263. 'total_count' => array (
  264. 'name' => 'total_count',
  265. 'title' => 'Total count',
  266. 'description' => 'Total count',
  267. 'type' => 'int',
  268. 'handlers' => array (
  269. 'filter' => array (
  270. 'name' => 'views_handler_filter_numeric'
  271. ),
  272. 'field' => array (
  273. 'name' => 'views_handler_field'
  274. ),
  275. 'sort' => array (
  276. 'name' => 'views_handler_sort'
  277. ),
  278. 'argument' => array (
  279. 'name' => 'views_handler_argument_numeric'
  280. ),
  281. 'relationship' => array (
  282. 'name' => 'views_handler_relationship'
  283. )
  284. ),
  285. 'joins' => array ()
  286. )
  287. )
  288. );
  289. // Add the array above that will integrate our qtl_search materialized view
  290. // for use with Drupal Views.
  291. tripal_add_views_integration($integration);
  292. }
  293. /**
  294. * Add any custom tables needed by this module.
  295. * - phylotreeprop: keep track of properties of phylotree
  296. *
  297. * @ingroup tripal_legacy_phylogeny
  298. */
  299. function tripal_phylogeny_add_custom_tables() {
  300. $schema = array (
  301. 'table' => 'phylotreeprop',
  302. 'fields' => array (
  303. 'phylotreeprop_id' => array (
  304. 'type' => 'serial',
  305. 'not null' => true,
  306. ),
  307. 'phylotree_id' => array (
  308. 'type' => 'int',
  309. 'not null' => true,
  310. ),
  311. 'type_id' => array (
  312. 'type' => 'int',
  313. 'not null' => true,
  314. ),
  315. 'value' => array (
  316. 'type' => 'text',
  317. 'not null' => false,
  318. ),
  319. 'rank' => array (
  320. 'type' => 'int',
  321. 'not null' => true,
  322. 'default' => 0,
  323. ),
  324. ),
  325. 'primary key' => array (
  326. 0 => 'phylotreeprop_id',
  327. ),
  328. 'unique keys' => array (
  329. 'phylotreeprop_c1' => array (
  330. 0 => 'phylotree_id',
  331. 1 => 'type_id',
  332. 2 => 'rank',
  333. ),
  334. ),
  335. 'indexes' => array (
  336. 'phylotreeprop_idx1' => array (
  337. 0 => 'phylotree_id',
  338. ),
  339. 'phylotreeprop_idx2' => array (
  340. 0 => 'type_id',
  341. ),
  342. ),
  343. 'foreign keys' => array (
  344. 'cvterm' => array (
  345. 'table' => 'cvterm',
  346. 'columns' => array (
  347. 'type_id' => 'cvterm_id',
  348. ),
  349. ),
  350. 'phylotree' => array (
  351. 'table' => 'phylotree',
  352. 'columns' => array (
  353. 'phylotree_id' => 'phylotree_id',
  354. ),
  355. ),
  356. ),
  357. );
  358. chado_create_custom_table('phylotreeprop', $schema, TRUE);
  359. }
  360. /**
  361. * Adds the vocabulary needed for storing taxonmies.
  362. */
  363. function tripal_phylogeny_update_7200() {
  364. try {
  365. tripal_phylogeny_add_cvterms();
  366. }
  367. catch (\PDOException $e) {
  368. $error = $e->getMessage();
  369. throw new DrupalUpdateException('Failed to complete update' . $error);
  370. }
  371. }