tripal_library.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. // the library types were formerly in a vocabulary named 'tripal_library_types'.
  248. // rename that to just be 'library_type'. We cannot use the Tripal API calls
  249. // because during upgrade the tripal_core should also be disabled
  250. try {
  251. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  252. if (!$check->cv_id) {
  253. $sql = "UPDATE chado.cv SET name = 'library_type' WHERE name = 'tripal_library_types'";
  254. db_query($sql);
  255. }
  256. }
  257. catch (\PDOException $e) {
  258. $error = $e->getMessage();
  259. throw new DrupalUpdateException('Failed to change the vocabulary from tripal_library_types to library_type: '. $error);
  260. }
  261. // add the library_property CV
  262. try {
  263. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchObject();
  264. if (!$check->cv_id) {
  265. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  266. 'library_property',
  267. 'Contains properties for libraries.')
  268. ";
  269. db_query($sql);
  270. }
  271. }
  272. catch (\PDOException $e) {
  273. $error = $e->getMessage();
  274. throw new DrupalUpdateException('Failed to add library_property vocabulary: '. $error);
  275. }
  276. // add the library_type CV
  277. try {
  278. $check = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchObject();
  279. if (!$check->cv_id) {
  280. $sql = "INSERT INTO chado.cv (name, definition) VALUES (
  281. 'library_type',
  282. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).')
  283. ";
  284. db_query($sql);
  285. }
  286. }
  287. catch (\PDOException $e) {
  288. $error = $e->getMessage();
  289. throw new DrupalUpdateException('Failed to add library_type vocabulary: '. $error);
  290. }
  291. // For Tripal in Drupal 6 the library_description cvterm was stored in the
  292. // 'tripal' CV. It should be stored in the new library_property CV that
  293. // is added by this module for Tripal 2.0 and Drupal 7. So, we need to
  294. // reset the CV ID for that term and rename the term to 'Library Description'
  295. // We cannot use the Tripal API calls in the 7000 update
  296. // because during upgrade the tripal_core should also be disabled
  297. $sql = "
  298. UPDATE chado.cvterm
  299. SET
  300. name = 'Library Description',
  301. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'library_property')
  302. WHERE
  303. name = 'library_description' AND
  304. cv_id = (SELECT cv_id FROM chado.cv WHERE name = 'tripal')
  305. ";
  306. try {
  307. db_query($sql);
  308. }
  309. catch (\PDOException $e) {
  310. $error = $e->getMessage();
  311. throw new DrupalUpdateException('Failed to change library_description property type to the library_property CV and update the name: '. $error);
  312. }
  313. // During the upgrade from D6 to D7 the vocabulary terms assigned to libraries were
  314. // copied to the field_data_taxonomyextra table rather than to the correct
  315. // field_data_taxonomy_vocabulary_[vid] table. We'll move them.
  316. $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Library'")->fetchField();
  317. if ($vid) {
  318. try {
  319. // first move from the field_data_taxonomyextra table
  320. $sql = "
  321. INSERT INTO {field_data_taxonomy_vocabulary_$vid}
  322. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  323. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  324. FROM field_data_taxonomyextra
  325. WHERE bundle = 'chado_feature')
  326. ";
  327. db_query($sql);
  328. $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_library'";
  329. db_query($sql);
  330. // next move from the field_revision_taxonomyextra table
  331. $sql = "
  332. INSERT INTO {field_revision_taxonomy_vocabulary_$vid}
  333. (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)
  334. (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid
  335. FROM field_revision_taxonomyextra
  336. WHERE bundle = 'chado_feature')
  337. ";
  338. db_query($sql);
  339. $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_library'";
  340. db_query($sql);
  341. }
  342. catch (\PDOException $e) {
  343. $error = $e->getMessage();
  344. throw new DrupalUpdateException('Could not move library taxonomy terms: '. $error);
  345. }
  346. }
  347. // set the default vocabularies
  348. // library_type
  349. try {
  350. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_type'")->fetchField();
  351. db_insert('tripal_cv_defaults')
  352. ->fields(array(
  353. 'table_name' => 'library',
  354. 'field_name' => 'type_id',
  355. 'cv_id' => $cv_id
  356. ))
  357. ->execute();
  358. }
  359. catch (\PDOException $e) {
  360. $error = $e->getMessage();
  361. throw new DrupalUpdateException('Failed to set library_type vocabulary as default: '. $error);
  362. }
  363. // library_property
  364. try {
  365. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'library_property'")->fetchField();
  366. db_insert('tripal_cv_defaults')
  367. ->fields(array(
  368. 'table_name' => 'libraryprop',
  369. 'field_name' => 'type_id',
  370. 'cv_id' => $cv_id
  371. ))
  372. ->execute();
  373. }
  374. catch (\PDOException $e) {
  375. $error = $e->getMessage();
  376. throw new DrupalUpdateException('Failed to set the library_property vocabulary as default: '. $error);
  377. }
  378. }
  379. /**
  380. * Implementation of hook_update_dependencies(). It specifies a list of
  381. * other modules whose updates must be run prior to this one.
  382. */
  383. function tripal_library_update_dependencies() {
  384. $dependencies = array();
  385. // the tripal_cv update 7200 must run prior to update 7200 of this module
  386. $dependencies['tripal_library'][7200] = array(
  387. 'tripal_cv' => 7200
  388. );
  389. return $dependencies;
  390. }
  391. /**
  392. * Fixes an error with the materialized view installation
  393. *
  394. */
  395. function tripal_library_update_7201() {
  396. // there is a bug in the Tripal v2.0-alpha release that didn't add the
  397. // materialized view schema to the mviews table.
  398. // get the schema for the materialized view from the custom_tables table
  399. // as there is a copy there, but only if the schema is missing from the
  400. // materialized view table
  401. $view_name = 'library_feature_count';
  402. $schema = db_select('tripal_mviews', 'tm')
  403. ->fields('tm', array('mv_schema'))
  404. ->condition('name', $view_name)
  405. ->execute()
  406. ->fetchField();
  407. if (!$schema or $schema == 'Array') {
  408. $schema = db_select('tripal_custom_tables', 'tct')
  409. ->fields('tct', array('schema'))
  410. ->condition('table_name', $view_name)
  411. ->execute()
  412. ->fetchField();
  413. $schema_str = var_export(unserialize($schema), TRUE);
  414. $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);
  415. db_update('tripal_mviews')
  416. ->fields(array(
  417. 'mv_schema' => $schema_str
  418. ))
  419. ->condition('name', $view_name)
  420. ->execute();
  421. }
  422. }