tripal_library.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the library module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_library
  11. */
  12. function tripal_library_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_library.views_default.inc");
  15. $views = tripal_library_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_library
  24. */
  25. function tripal_library_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_library'] = array(
  31. 'title' => "tripal_library",
  32. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_library
  43. */
  44. function tripal_library_install() {
  45. // add the materialized view
  46. tripal_library_add_mview_library_feature_count();
  47. // add cvterms
  48. tripal_library_add_cvs();
  49. tripal_library_add_cvterms();
  50. // set the default vocabularies
  51. tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  52. tripal_set_default_cv('library', 'type_id', 'library_type');
  53. }
  54. /**
  55. * Implementation of hook_uninstall().
  56. *
  57. * @ingroup tripal_library
  58. */
  59. function tripal_library_uninstall() {
  60. }
  61. /**
  62. * Implementation of hook_schema().
  63. *
  64. * @ingroup tripal_library
  65. */
  66. function tripal_library_schema() {
  67. $schema['chado_library'] = array(
  68. 'fields' => array(
  69. 'vid' => array(
  70. 'type' => 'int',
  71. 'unsigned' => TRUE,
  72. 'not null' => TRUE,
  73. 'default' => 0
  74. ),
  75. 'nid' => array(
  76. 'type' => 'int',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. 'default' => 0
  80. ),
  81. 'library_id' => array(
  82. 'type' => 'int',
  83. 'not null' => TRUE,
  84. 'default' => 0
  85. )
  86. ),
  87. 'indexes' => array(
  88. 'chado_library_idx1' => array('library_id')
  89. ),
  90. 'unique keys' => array(
  91. 'chado_library_uq1' => array('nid', 'vid'),
  92. 'chado_library_uq2' => array('vid')
  93. ),
  94. 'primary key' => array('nid'),
  95. );
  96. return $schema;
  97. }
  98. /**
  99. * Adds a materialized view keeping track of the type of features associated with each library
  100. *
  101. * @ingroup tripal_library
  102. */
  103. function tripal_library_add_mview_library_feature_count(){
  104. $view_name = 'library_feature_count';
  105. $comment = 'Provides count of feature by type that are associated with all libraries';
  106. $schema = array(
  107. 'table' => $view_name,
  108. 'description' => $comment,
  109. 'fields' => array(
  110. 'library_id' => array(
  111. 'type' => 'int',
  112. 'not null' => TRUE,
  113. ),
  114. 'name' => array(
  115. 'type' => 'varchar',
  116. 'length' => 255,
  117. 'not null' => TRUE,
  118. ),
  119. 'num_features' => array(
  120. 'type' => 'int',
  121. 'not null' => TRUE,
  122. ),
  123. 'feature_type' => array(
  124. 'type' => 'varchar',
  125. 'length' => 255,
  126. 'not null' => TRUE,
  127. ),
  128. ),
  129. 'indexes' => array(
  130. 'library_feature_count_idx1' => array('library_id'),
  131. ),
  132. );
  133. $sql = "
  134. SELECT
  135. L.library_id, L.name,
  136. count(F.feature_id) as num_features,
  137. CVT.name as feature_type
  138. FROM library L
  139. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  140. INNER JOIN feature F ON LF.feature_id = F.feature_id
  141. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  142. GROUP BY L.library_id, L.name, CVT.name
  143. ";
  144. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  145. }
  146. /**
  147. * Adds new CV's used by this module
  148. *
  149. * @ingroup tripal_library
  150. */
  151. function tripal_library_add_cvs(){
  152. tripal_insert_cv(
  153. 'library_property',
  154. 'Contains properties for libraries.'
  155. );
  156. tripal_insert_cv(
  157. 'library_type',
  158. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  159. );
  160. }
  161. /**
  162. * Adds cvterms needed for the library module
  163. *
  164. * @ingroup tripal_library
  165. */
  166. function tripal_library_add_cvterms() {
  167. // Insert cvterm 'library_description' into cvterm table of chado
  168. // database. This CV term is used to keep track of the library
  169. // description in the libraryprop table.
  170. tripal_insert_cvterm(
  171. array(
  172. 'name' => 'Library Description',
  173. 'definition' => 'Description of a library',
  174. 'cv_name' => 'library_property',
  175. 'is_relationship' => 0,
  176. 'db_name' => 'tripal'
  177. ),
  178. array('update_existing' => TRUE)
  179. );
  180. // add cvterms for the map unit types
  181. tripal_insert_cvterm(
  182. array(
  183. 'name' => 'cdna_library',
  184. 'definition' => 'cDNA library',
  185. 'cv_name' => 'library_type',
  186. 'is_relationship' => 0,
  187. 'db_name' => 'tripal'
  188. ),
  189. array('update_existing' => TRUE)
  190. );
  191. tripal_insert_cvterm(
  192. array(
  193. 'name' => 'bac_library',
  194. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  195. 'cv_name' => 'library_type',
  196. 'is_relationship' => 0,
  197. 'db_name' => 'tripal'
  198. ),
  199. array('update_existing' => TRUE)
  200. );
  201. tripal_insert_cvterm(
  202. array(
  203. 'name' => 'fosmid_library',
  204. 'definition' => 'Fosmid library',
  205. 'cv_name' => 'library_type',
  206. 'is_relationship' => 0,
  207. 'db_name' => 'tripal'
  208. ),
  209. array('update_existing' => TRUE)
  210. );
  211. tripal_insert_cvterm(
  212. array(
  213. 'name' => 'cosmid_library',
  214. 'definition' => 'Cosmid library',
  215. 'cv_name' => 'library_type',
  216. 'is_relationship' => 0,
  217. 'db_name' => 'tripal'
  218. ),
  219. array('update_existing' => TRUE)
  220. );
  221. tripal_insert_cvterm(
  222. array(
  223. 'name' => 'yac_library',
  224. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  225. 'cv_name' => 'library_type',
  226. 'is_relationship' => 0,
  227. 'db_name' => 'tripal'
  228. ),
  229. array('update_existing' => TRUE)
  230. );
  231. tripal_insert_cvterm(
  232. array(
  233. 'name' => 'genomic_library',
  234. 'definition' => 'Genomic Library',
  235. 'cv_name' => 'library_type',
  236. 'is_relationship' => 0,
  237. 'db_name' => 'tripal'
  238. ),
  239. array('update_existing' => TRUE)
  240. );
  241. }
  242. /**
  243. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  244. *
  245. */
  246. function tripal_library_update_7200() {
  247. // Make sure we have the full API loaded this will help during a
  248. // site upgrade when the tripal_core module is disabled.
  249. module_load_include('module', 'tripal_core', 'tripal_core');
  250. tripal_core_import_api();
  251. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  252. // rename that to just be 'library_type'. We cannot use the Tripal API calls
  253. // because during upgrade the tripal_core should also be disabled
  254. try {
  255. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  256. if (!$check->cv_id) {
  257. $sql = "UPDATE chado.cv SET name = 'library_type' WHERE name = 'tripal_library_types'";
  258. db_query($sql);
  259. }
  260. }
  261. catch (\PDOException $e) {
  262. $error = $e->getMessage();
  263. throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: '. $error);
  264. }
  265. // add the library_property CV
  266. try {
  267. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchObject();
  268. if (!$check->cv_id) {
  269. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  270. 'library_property',
  271. 'Contains properties for libraries.')
  272. ";
  273. db_query($sql);
  274. }
  275. }
  276. catch (\PDOException $e) {
  277. $error = $e->getMessage();
  278. throw new DrupalUpdateException('Failed to add library_property vocabulary: '. $error);
  279. }
  280. // add the library_type CV
  281. try {
  282. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  283. if (!$check->cv_id) {
  284. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  285. 'library_type',
  286. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).')
  287. ";
  288. db_query($sql);
  289. }
  290. }
  291. catch (\PDOException $e) {
  292. $error = $e->getMessage();
  293. throw new DrupalUpdateException('Failed to add library_type vocabulary: '. $error);
  294. }
  295. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  296. // 'tripal' CV. It should be stored in the new library_property CV that
  297. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  298. // reset the CV ID for that term and rename the term to 'Library Description'
  299. // We cannot use the Tripal API calls in the 7000 update
  300. // because during upgrade the tripal_core should also be disabled
  301. $sql = "
  302. UPDATE chado.cvterm
  303. SET
  304. name = 'Library Description',
  305. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'library_property')
  306. WHERE
  307. name = 'library_description' AND
  308. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  309. ";
  310. try {
  311. db_query($sql);
  312. }
  313. catch (\PDOException $e) {
  314. $error = $e->getMessage();
  315. throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: '. $error);
  316. }
  317. // During the upgrade from D6 to D7 the vocabulary terms assigned to libraries were
  318. // copied to the field_data_taxonomyextra table rather than to the correct
  319. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  320. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Library'")->fetchField();
  321. if ($vid) {
  322. try {
  323. if (db_table_exists('field_data_taxonomyextra')) {
  324. // first move from the field_data_taxonomyextra table
  325. $sql = "
  326. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  327. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  328. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  329. FROM field_data_taxonomyextra
  330. WHERE bundle = 'chado_feature')
  331. ";
  332. db_query($sql);
  333. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_library'";
  334. db_query($sql);
  335. // next move from the field_revision_taxonomyextra table
  336. $sql = "
  337. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  338. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  339. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  340. FROM field_revision_taxonomyextra
  341. WHERE bundle = 'chado_feature')
  342. ";
  343. db_query($sql);
  344. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_library'";
  345. db_query($sql);
  346. }
  347. }
  348. catch (\PDOException $e) {
  349. $error = $e->getMessage();
  350. throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
  351. }
  352. }
  353. // set the default vocabularies
  354. // library_type
  355. try {
  356. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchField();
  357. $cdi = db_select('tripal_cv_defaults', 't')
  358. ->fields('t', array('cv_default_id'))
  359. ->condition('table_name', 'library')
  360. ->condition('field_name', 'type_id')
  361. ->execute()
  362. ->fetchField();
  363. if (!$cdi) {
  364. db_insert('tripal_cv_defaults')
  365. ->fields(array(
  366. 'table_name' => 'library',
  367. 'field_name' => 'type_id',
  368. 'cv_id' => $cv_id
  369. ))
  370. ->execute();
  371. }
  372. }
  373. catch (\PDOException $e) {
  374. $error = $e->getMessage();
  375. throw new DrupalUpdateException('Failed to set library_type vocabulary as default: '. $error);
  376. }
  377. // library_property
  378. try {
  379. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchField();
  380. $cdi = db_select('tripal_cv_defaults', 't')
  381. ->fields('t', array('cv_default_id'))
  382. ->condition('table_name', 'libraryprop')
  383. ->condition('field_name', 'type_id')
  384. ->execute()
  385. ->fetchField();
  386. if (!$cdi) {
  387. db_insert('tripal_cv_defaults')
  388. ->fields(array(
  389. 'table_name' => 'libraryprop',
  390. 'field_name' => 'type_id',
  391. 'cv_id' => $cv_id
  392. ))
  393. ->execute();
  394. }
  395. }
  396. catch (\PDOException $e) {
  397. $error = $e->getMessage();
  398. throw new DrupalUpdateException('Failed to set the library_property vocabulary as default: '. $error);
  399. }
  400. }
  401. /**
  402. * Implementation of hook_update_dependencies().
  403. *
  404. * It specifies a list of other modules whose updates must be run prior to
  405. * this one. It also ensures the the Tripal API is in scope for site
  406. * upgrades when tripal_core is disabled.
  407. */
  408. function tripal_library_update_dependencies() {
  409. $dependencies = array();
  410. // the tripal_cv update 7200 must run prior to update 7200 of this module
  411. $dependencies['tripal_library'][7200] = array(
  412. 'tripal_cv' => 7200
  413. );
  414. return $dependencies;
  415. }
  416. /**
  417. * Fixes an error with the materialized view installation
  418. *
  419. */
  420. function tripal_library_update_7201() {
  421. // Make sure we have the full API loaded this will help during a
  422. // site upgrade when the tripal_core module is disabled.
  423. module_load_include('module', 'tripal_core', 'tripal_core');
  424. tripal_core_import_api();
  425. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  426. // materialized view schema to the mviews table.
  427. // get the schema for the materialized view from the custom_tables table
  428. // as there is a copy there, but only if the schema is missing from the
  429. // materialized view table
  430. $view_name = 'library_feature_count';
  431. $schema = db_select('tripal_mviews', 'tm')
  432. ->fields('tm', array('mv_schema'))
  433. ->condition('name', $view_name)
  434. ->execute()
  435. ->fetchField();
  436. if (!$schema or $schema == 'Array') {
  437. $schema = db_select('tripal_custom_tables', 'tct')
  438. ->fields('tct', array('schema'))
  439. ->condition('table_name', $view_name)
  440. ->execute()
  441. ->fetchField();
  442. $schema_str = var_export(unserialize($schema), TRUE);
  443. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  444. db_update('tripal_mviews')
  445. ->fields(array(
  446. 'mv_schema' => $schema_str
  447. ))
  448. ->condition('name', $view_name)
  449. ->execute();
  450. }
  451. }