tripal_library.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. 'size' => 'big',
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. ),
  115. 'name' => array(
  116. 'type' => 'varchar',
  117. 'length' => 255,
  118. 'not null' => TRUE,
  119. ),
  120. 'num_features' => array(
  121. 'type' => 'int',
  122. 'not null' => TRUE,
  123. ),
  124. 'feature_type' => array(
  125. 'type' => 'varchar',
  126. 'length' => 255,
  127. 'not null' => TRUE,
  128. ),
  129. ),
  130. 'indexes' => array(
  131. 'library_feature_count_idx1' => array('library_id'),
  132. ),
  133. );
  134. $sql = "
  135. SELECT
  136. L.library_id, L.name,
  137. count(F.feature_id) as num_features,
  138. CVT.name as feature_type
  139. FROM library L
  140. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  141. INNER JOIN feature F ON LF.feature_id = F.feature_id
  142. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  143. GROUP BY L.library_id, L.name, CVT.name
  144. ";
  145. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  146. }
  147. /**
  148. * Adds new CV's used by this module
  149. *
  150. * @ingroup tripal_library
  151. */
  152. function tripal_library_add_cvs(){
  153. tripal_insert_cv(
  154. 'library_property',
  155. 'Contains properties for libraries.'
  156. );
  157. tripal_insert_cv(
  158. 'library_type',
  159. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  160. );
  161. }
  162. /**
  163. * Adds cvterms needed for the library module
  164. *
  165. * @ingroup tripal_library
  166. */
  167. function tripal_library_add_cvterms() {
  168. // Insert cvterm 'library_description' into cvterm table of chado
  169. // database. This CV term is used to keep track of the library
  170. // description in the libraryprop table.
  171. tripal_insert_cvterm(
  172. array(
  173. 'name' => 'Library Description',
  174. 'definition' => 'Description of a library',
  175. 'cv_name' => 'library_property',
  176. 'is_relationship' => 0,
  177. 'db_name' => 'tripal'
  178. ),
  179. array('update_existing' => TRUE)
  180. );
  181. // add cvterms for the map unit types
  182. tripal_insert_cvterm(
  183. array(
  184. 'name' => 'cdna_library',
  185. 'definition' => 'cDNA library',
  186. 'cv_name' => 'library_type',
  187. 'is_relationship' => 0,
  188. 'db_name' => 'tripal'
  189. ),
  190. array('update_existing' => TRUE)
  191. );
  192. tripal_insert_cvterm(
  193. array(
  194. 'name' => 'bac_library',
  195. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  196. 'cv_name' => 'library_type',
  197. 'is_relationship' => 0,
  198. 'db_name' => 'tripal'
  199. ),
  200. array('update_existing' => TRUE)
  201. );
  202. tripal_insert_cvterm(
  203. array(
  204. 'name' => 'fosmid_library',
  205. 'definition' => 'Fosmid library',
  206. 'cv_name' => 'library_type',
  207. 'is_relationship' => 0,
  208. 'db_name' => 'tripal'
  209. ),
  210. array('update_existing' => TRUE)
  211. );
  212. tripal_insert_cvterm(
  213. array(
  214. 'name' => 'cosmid_library',
  215. 'definition' => 'Cosmid library',
  216. 'cv_name' => 'library_type',
  217. 'is_relationship' => 0,
  218. 'db_name' => 'tripal'
  219. ),
  220. array('update_existing' => TRUE)
  221. );
  222. tripal_insert_cvterm(
  223. array(
  224. 'name' => 'yac_library',
  225. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  226. 'cv_name' => 'library_type',
  227. 'is_relationship' => 0,
  228. 'db_name' => 'tripal'
  229. ),
  230. array('update_existing' => TRUE)
  231. );
  232. tripal_insert_cvterm(
  233. array(
  234. 'name' => 'genomic_library',
  235. 'definition' => 'Genomic Library',
  236. 'cv_name' => 'library_type',
  237. 'is_relationship' => 0,
  238. 'db_name' => 'tripal'
  239. ),
  240. array('update_existing' => TRUE)
  241. );
  242. }
  243. /**
  244. * This is the required update for tripal_library when upgrading from Drupal core API 6.x.
  245. *
  246. */
  247. function tripal_library_update_7200() {
  248. // Make sure we have the full API loaded this will help during a
  249. // site upgrade when the tripal_core module is disabled.
  250. module_load_include('module', 'tripal_core', 'tripal_core');
  251. tripal_core_import_api();
  252. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  253. // rename that to just be 'library_type'. We cannot use the Tripal API calls
  254. // because during upgrade the tripal_core should also be disabled
  255. try {
  256. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  257. if (!$check->cv_id) {
  258. $sql = "UPDATE chado.cv SET name = 'library_type' WHERE name = 'tripal_library_types'";
  259. db_query($sql);
  260. }
  261. }
  262. catch (\PDOException $e) {
  263. $error = $e->getMessage();
  264. throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: '. $error);
  265. }
  266. // add the library_property CV
  267. try {
  268. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchObject();
  269. if (!$check->cv_id) {
  270. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  271. 'library_property',
  272. 'Contains properties for libraries.')
  273. ";
  274. db_query($sql);
  275. }
  276. }
  277. catch (\PDOException $e) {
  278. $error = $e->getMessage();
  279. throw new DrupalUpdateException('Failed to add library_property vocabulary: '. $error);
  280. }
  281. // add the library_type CV
  282. try {
  283. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  284. if (!$check->cv_id) {
  285. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  286. 'library_type',
  287. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).')
  288. ";
  289. db_query($sql);
  290. }
  291. }
  292. catch (\PDOException $e) {
  293. $error = $e->getMessage();
  294. throw new DrupalUpdateException('Failed to add library_type vocabulary: '. $error);
  295. }
  296. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  297. // 'tripal' CV. It should be stored in the new library_property CV that
  298. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  299. // reset the CV ID for that term and rename the term to 'Library Description'
  300. // We cannot use the Tripal API calls in the 7000 update
  301. // because during upgrade the tripal_core should also be disabled
  302. $sql = "
  303. UPDATE chado.cvterm
  304. SET
  305. name = 'Library Description',
  306. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'library_property')
  307. WHERE
  308. name = 'library_description' AND
  309. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  310. ";
  311. try {
  312. db_query($sql);
  313. }
  314. catch (\PDOException $e) {
  315. $error = $e->getMessage();
  316. throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: '. $error);
  317. }
  318. // During the upgrade from D6 to D7 the vocabulary terms assigned to libraries were
  319. // copied to the field_data_taxonomyextra table rather than to the correct
  320. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  321. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Library'")->fetchField();
  322. if ($vid) {
  323. try {
  324. if (db_table_exists('field_data_taxonomyextra')) {
  325. // first move from the field_data_taxonomyextra table
  326. $sql = "
  327. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  328. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  329. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  330. FROM field_data_taxonomyextra
  331. WHERE bundle = 'chado_feature')
  332. ";
  333. db_query($sql);
  334. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_library'";
  335. db_query($sql);
  336. // next move from the field_revision_taxonomyextra table
  337. $sql = "
  338. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  339. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  340. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  341. FROM field_revision_taxonomyextra
  342. WHERE bundle = 'chado_feature')
  343. ";
  344. db_query($sql);
  345. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_library'";
  346. db_query($sql);
  347. }
  348. }
  349. catch (\PDOException $e) {
  350. $error = $e->getMessage();
  351. throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
  352. }
  353. }
  354. // set the default vocabularies
  355. // library_type
  356. try {
  357. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchField();
  358. $cdi = db_select('tripal_cv_defaults', 't')
  359. ->fields('t', array('cv_default_id'))
  360. ->condition('table_name', 'library')
  361. ->condition('field_name', 'type_id')
  362. ->execute()
  363. ->fetchField();
  364. if (!$cdi) {
  365. db_insert('tripal_cv_defaults')
  366. ->fields(array(
  367. 'table_name' => 'library',
  368. 'field_name' => 'type_id',
  369. 'cv_id' => $cv_id
  370. ))
  371. ->execute();
  372. }
  373. }
  374. catch (\PDOException $e) {
  375. $error = $e->getMessage();
  376. throw new DrupalUpdateException('Failed to set library_type vocabulary as default: '. $error);
  377. }
  378. // library_property
  379. try {
  380. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchField();
  381. $cdi = db_select('tripal_cv_defaults', 't')
  382. ->fields('t', array('cv_default_id'))
  383. ->condition('table_name', 'libraryprop')
  384. ->condition('field_name', 'type_id')
  385. ->execute()
  386. ->fetchField();
  387. if (!$cdi) {
  388. db_insert('tripal_cv_defaults')
  389. ->fields(array(
  390. 'table_name' => 'libraryprop',
  391. 'field_name' => 'type_id',
  392. 'cv_id' => $cv_id
  393. ))
  394. ->execute();
  395. }
  396. }
  397. catch (\PDOException $e) {
  398. $error = $e->getMessage();
  399. throw new DrupalUpdateException('Failed to set the library_property vocabulary as default: '. $error);
  400. }
  401. }
  402. /**
  403. * Implementation of hook_update_dependencies().
  404. *
  405. * It specifies a list of other modules whose updates must be run prior to
  406. * this one. It also ensures the the Tripal API is in scope for site
  407. * upgrades when tripal_core is disabled.
  408. */
  409. function tripal_library_update_dependencies() {
  410. $dependencies = array();
  411. // the tripal_cv update 7200 must run prior to update 7200 of this module
  412. $dependencies['tripal_library'][7200] = array(
  413. 'tripal_cv' => 7200
  414. );
  415. return $dependencies;
  416. }
  417. /**
  418. * Fixes an error with the materialized view installation
  419. *
  420. */
  421. function tripal_library_update_7201() {
  422. // Make sure we have the full API loaded this will help during a
  423. // site upgrade when the tripal_core module is disabled.
  424. module_load_include('module', 'tripal_core', 'tripal_core');
  425. tripal_core_import_api();
  426. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  427. // materialized view schema to the mviews table.
  428. // get the schema for the materialized view from the custom_tables table
  429. // as there is a copy there, but only if the schema is missing from the
  430. // materialized view table
  431. $view_name = 'library_feature_count';
  432. $schema = db_select('tripal_mviews', 'tm')
  433. ->fields('tm', array('mv_schema'))
  434. ->condition('name', $view_name)
  435. ->execute()
  436. ->fetchField();
  437. if (!$schema or $schema == 'Array') {
  438. $schema = db_select('tripal_custom_tables', 'tct')
  439. ->fields('tct', array('schema'))
  440. ->condition('table_name', $view_name)
  441. ->execute()
  442. ->fetchField();
  443. $schema_str = var_export(unserialize($schema), TRUE);
  444. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  445. db_update('tripal_mviews')
  446. ->fields(array(
  447. 'mv_schema' => $schema_str
  448. ))
  449. ->condition('name', $view_name)
  450. ->execute();
  451. }
  452. }