tripal_library.install 13 KB

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