tripal_entities.install 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /**
  3. * @file
  4. * Install for a tripal data entity - creates the base table for our entity.
  5. */
  6. function tripal_entities_install() {
  7. // add any custom tables. For this case we will add an 'file' table to the
  8. // chado schema
  9. tripal_entities_add_custom_tables();
  10. }
  11. /**
  12. *
  13. */
  14. function tripal_entities_add_custom_tables() {
  15. tripal_entities_add_tripal_vocabulary_table();
  16. tripal_entities_add_tripal_vocabulary_usage_table();
  17. tripal_entities_add_tripal_term_table();
  18. tripal_entities_add_tripal_term_usage_table();
  19. tripal_entities_add_tripal_term_relationship_table();
  20. }
  21. /**The current Entity Example (Genes) requires the Sequence Ontology. [warning]
  22. *
  23. */
  24. function tripal_entities_add_tripal_vocabulary_table() {
  25. $schema = array (
  26. 'table' => 'tripal_vocabulary',
  27. 'fields' => array (
  28. 'vocabulary_id' => array(
  29. 'type' => 'serial',
  30. 'not null' => TRUE
  31. ),
  32. 'cv_id' => array (
  33. 'type' => 'int',
  34. 'not null' => TRUE
  35. ),
  36. 'db_id' => array (
  37. 'type' => 'int',
  38. 'not null' => TRUE
  39. ),
  40. 'publish' => array (
  41. 'type' => 'int',
  42. 'not null' => TRUE,
  43. 'default' => 0
  44. ),
  45. ),
  46. 'primary key' => array (
  47. 0 => 'vocabulary_id'
  48. ),
  49. 'foreign keys' => array (
  50. 'cv' => array (
  51. 'table' => 'cv',
  52. 'columns' => array (
  53. 'cv_id' => 'cv_id'
  54. )
  55. ),
  56. 'db' => array (
  57. 'table' => 'db',
  58. 'columns' => array (
  59. 'db_id' => 'db_id'
  60. )
  61. ),
  62. ),
  63. 'unique keys' => array (
  64. 'tripal_vocabulary_cvdb' => array (
  65. 'cv_id', 'db_id'
  66. ),
  67. ),
  68. 'indexes' => array(
  69. 'tripal_vocabulary_cv_id' => array('cv_id'),
  70. 'tripal_vocabulary_db_id' => array('db_id'),
  71. )
  72. );
  73. chado_create_custom_table('tripal_vocabulary', $schema, TRUE);
  74. }
  75. /**
  76. *
  77. */
  78. function tripal_entities_add_tripal_term_table() {
  79. $schema = array (
  80. 'table' => 'tripal_term',
  81. 'fields' => array (
  82. 'term_id' => array(
  83. 'type' => 'serial',
  84. 'not null' => TRUE
  85. ),
  86. 'vocabulary_id' => array (
  87. 'type' => 'int',
  88. 'not null' => TRUE
  89. ),
  90. 'cvterm_id' => array (
  91. 'type' => 'int',
  92. 'not null' => TRUE
  93. ),
  94. 'publish' => array (
  95. 'type' => 'int',
  96. 'not null' => TRUE,
  97. 'default' => 0
  98. ),
  99. ),
  100. 'primary key' => array (
  101. 0 => 'term_id'
  102. ),
  103. 'foreign keys' => array (
  104. 'cvterm' => array (
  105. 'table' => 'cvterm',
  106. 'columns' => array (
  107. 'cvterm_id' => 'cvterm_id'
  108. )
  109. ),
  110. 'tripal_vocabulary' => array (
  111. 'table' => 'tripal_vocabulary',
  112. 'columns' => array (
  113. 'vocabulary_id' => 'vocabulary_id'
  114. )
  115. ),
  116. ),
  117. 'unique keys' => array (
  118. 'tripal_term_unq' => array (
  119. 'vocabulary_id', 'cvterm_id'
  120. ),
  121. ),
  122. 'indexes' => array(
  123. 'tripal_term_vocabulary_id' => array('vocabulary_id'),
  124. 'tripal_term_cvterm_id' => array('cvterm_id'),
  125. ),
  126. );
  127. chado_create_custom_table('tripal_term', $schema, TRUE);
  128. }
  129. /**
  130. *
  131. */
  132. function tripal_entities_add_tripal_vocabulary_usage_table(){
  133. $schema = array (
  134. 'table' => 'tripal_vocabulary_usage',
  135. 'fields' => array (
  136. 'vocabulary_usage_id' => array(
  137. 'type' => 'serial',
  138. 'not null' => TRUE
  139. ),
  140. 'vocabulary_id' => array (
  141. 'type' => 'int',
  142. 'not null' => TRUE
  143. ),
  144. 'data_table' => array (
  145. 'type' => 'varchar',
  146. 'length' => 128,
  147. 'not null' => TRUE
  148. ),
  149. 'type_table' => array (
  150. 'type' => 'varchar',
  151. 'length' => 128,
  152. 'not null' => TRUE
  153. ),
  154. 'field' => array (
  155. 'type' => 'varchar',
  156. 'length' => 128,
  157. 'not null' => TRUE
  158. ),
  159. ),
  160. 'primary key' => array (
  161. 0 => 'vocabulary_usage_id'
  162. ),
  163. 'foreign keys' => array (
  164. 'tripal_vocabulary' => array (
  165. 'table' => 'tripal_vocabulary',
  166. 'columns' => array (
  167. 'vocabulary_id' => 'vocabulary_id'
  168. ),
  169. ),
  170. ),
  171. 'unique keys' => array (
  172. 'tripal_vocabulary_ridbase' => array (
  173. 'vocabulary_id', 'data_table'
  174. ),
  175. ),
  176. 'indexes' => array(
  177. 'tripal_vocabulary_vocabulary_id' => array('vocabulary_id'),
  178. 'tripal_vocabulary_data_table' => array('data_table'),
  179. 'tripal_vocabulary_type_table' => array('type_table'),
  180. ),
  181. );
  182. chado_create_custom_table('tripal_vocabulary_usage', $schema, TRUE);
  183. }
  184. /**
  185. *
  186. */
  187. function tripal_entities_add_tripal_term_usage_table(){
  188. $schema = array (
  189. 'table' => 'tripal_term_usage',
  190. 'fields' => array (
  191. 'term_usage_id' => array(
  192. 'type' => 'serial',
  193. 'not null' => TRUE
  194. ),
  195. 'term_id' => array (
  196. 'type' => 'int',
  197. 'not null' => TRUE
  198. ),
  199. 'data_table' => array (
  200. 'type' => 'varchar',
  201. 'length' => 128,
  202. 'not null' => TRUE
  203. ),
  204. 'type_table' => array (
  205. 'type' => 'varchar',
  206. 'length' => 128,
  207. 'not null' => TRUE
  208. ),
  209. 'field' => array (
  210. 'type' => 'varchar',
  211. 'length' => 128,
  212. 'not null' => TRUE
  213. ),
  214. ),
  215. 'primary key' => array (
  216. 0 => 'term_usage_id'
  217. ),
  218. 'foreign keys' => array (
  219. 'tripal_term' => array (
  220. 'table' => 'tripal_term',
  221. 'columns' => array (
  222. 'term_id' => 'term_id'
  223. ),
  224. ),
  225. ),
  226. 'unique keys' => array (
  227. 'tripal_term_usage_ridbase' => array (
  228. 'term_id', 'type_table', 'field'
  229. ),
  230. ),
  231. 'indexes' => array(
  232. 'tripal_term_usage_term_id' => array('term_id'),
  233. ),
  234. );
  235. chado_create_custom_table('tripal_term_usage', $schema, TRUE);
  236. }
  237. /**
  238. *
  239. */
  240. function tripal_entities_add_tripal_term_relationship_table(){
  241. $schema = array (
  242. 'table' => 'tripal_term_relationship',
  243. 'fields' => array (
  244. 'relationship_id' => array(
  245. 'type' => 'serial',
  246. 'not null' => TRUE
  247. ),
  248. 'subject_id' => array (
  249. 'type' => 'int',
  250. 'not null' => TRUE
  251. ),
  252. 'type_id' => array (
  253. 'type' => 'int',
  254. 'not null' => TRUE
  255. ),
  256. 'object_id' => array (
  257. 'type' => 'int',
  258. 'not null' => FALSE
  259. ),
  260. 'fieldname' => array(
  261. 'type' => 'varchar',
  262. 'length' => 128,
  263. 'not null' => FALSE,
  264. )
  265. ),
  266. 'primary key' => array (
  267. 0 => 'relationship_id'
  268. ),
  269. 'foreign keys' => array (
  270. 'tripal_term' => array (
  271. 'table' => 'tripal_term',
  272. 'columns' => array (
  273. 'subject_id' => 'term_id',
  274. 'object_id' => 'term_id',
  275. ),
  276. ),
  277. ),
  278. 'unique keys' => array (
  279. 'tripal_term_relationship_unq' => array (
  280. 'subject_id', 'type_id', 'object_id'
  281. ),
  282. ),
  283. 'indexes' => array(
  284. 'tripal_term_relationship_subject_id' => array('subject_id'),
  285. 'tripal_term_relationship_object_id' => array('object_id'),
  286. 'tripal_term_relationship_type_id' => array('type_id'),
  287. ),
  288. );
  289. chado_create_custom_table('tripal_term_relationship', $schema, TRUE);
  290. }
  291. /**
  292. * Implements hook_schema().
  293. *
  294. * @ingroup entity_example
  295. */
  296. function tripal_entities_schema() {
  297. $schema['tripal_entity'] = array(
  298. 'description' => 'The base table for Tripal Vocabulary-based entities.',
  299. 'fields' => array(
  300. 'id' => array(
  301. 'description' => 'The primary identifier for a vocabulary entity.',
  302. 'type' => 'serial',
  303. 'unsigned' => TRUE,
  304. 'not null' => TRUE,
  305. ),
  306. 'type' => array(
  307. 'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
  308. 'type' => 'varchar',
  309. 'length' => 64,
  310. 'not null' => TRUE,
  311. 'default' => '',
  312. ),
  313. 'bundle' => array(
  314. 'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
  315. 'type' => 'varchar',
  316. 'length' => 1024,
  317. 'not null' => TRUE,
  318. 'default' => '',
  319. ),
  320. 'cvterm_id' => array(
  321. 'description' => 'The cvterm_id for the type of entity. This cvterm_id should match a record in the Chado cvterm table.',
  322. 'type' => 'int',
  323. 'not null' => TRUE,
  324. ),
  325. 'title' => array(
  326. 'description' => 'The title of this node, always treated as non-markup plain text.',
  327. 'type' => 'text',
  328. 'not null' => TRUE,
  329. 'default' => '',
  330. ),
  331. 'uid' => array(
  332. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  333. 'type' => 'int',
  334. 'not null' => TRUE,
  335. 'default' => 0,
  336. ),
  337. 'status' => array(
  338. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  339. 'type' => 'int',
  340. 'not null' => TRUE,
  341. 'default' => 1,
  342. ),
  343. 'created' => array(
  344. 'description' => 'The Unix timestamp when the node was created.',
  345. 'type' => 'int',
  346. 'not null' => TRUE,
  347. 'default' => 0,
  348. ),
  349. 'changed' => array(
  350. 'description' => 'The Unix timestamp when the node was most recently saved.',
  351. 'type' => 'int',
  352. 'not null' => TRUE,
  353. 'default' => 0,
  354. ),
  355. ),
  356. 'indexes' => array(
  357. 'cvterm_id' => array('cvterm_id'),
  358. 'entity_changed' => array('changed'),
  359. 'entity_created' => array('created'),
  360. 'type' => array('type'),
  361. 'uid' => array('uid'),
  362. ),
  363. 'unique keys' => array(
  364. ),
  365. 'primary key' => array('id'),
  366. );
  367. $schema['tripal_bundle'] = array(
  368. 'description' => 'Stores information about defined tripal data types.',
  369. 'fields' => array(
  370. 'id' => array(
  371. 'type' => 'serial',
  372. 'not null' => TRUE,
  373. 'description' => 'Primary Key: Unique Chado data type identifier.',
  374. ),
  375. 'type' => array(
  376. 'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
  377. 'type' => 'varchar',
  378. 'length' => 64,
  379. 'not null' => TRUE,
  380. 'default' => '',
  381. ),
  382. 'bundle' => array(
  383. 'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
  384. 'type' => 'varchar',
  385. 'length' => 1024,
  386. 'not null' => TRUE,
  387. 'default' => '',
  388. ),
  389. 'label' => array(
  390. 'description' => 'The human-readable name of this bundle.',
  391. 'type' => 'varchar',
  392. 'length' => 255,
  393. 'not null' => TRUE,
  394. 'default' => '',
  395. ),
  396. 'weight' => array(
  397. 'type' => 'int',
  398. 'not null' => TRUE,
  399. 'default' => 0,
  400. 'size' => 'tiny',
  401. 'description' => 'The weight of this tripal data type in relation to others.',
  402. ),
  403. 'data' => array(
  404. 'type' => 'text',
  405. 'not null' => FALSE,
  406. 'size' => 'big',
  407. 'serialize' => TRUE,
  408. 'description' => 'A serialized array of additional data related to this tripal data type.',
  409. ),
  410. ) + entity_exportable_schema_fields(),
  411. 'primary key' => array('id'),
  412. 'unique keys' => array(
  413. 'bundle' => array('bundle'),
  414. ),
  415. );
  416. $schema['chado_entity'] = array(
  417. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  418. 'fields' => array(
  419. 'chado_entity_id' => array(
  420. 'description' => 'The primary identifier for this table.',
  421. 'type' => 'serial',
  422. 'unsigned' => TRUE,
  423. 'not null' => TRUE,
  424. ),
  425. 'entity_id' => array(
  426. 'description' => 'The unique entity id.',
  427. 'type' => 'int',
  428. 'not null' => TRUE,
  429. ),
  430. 'record_id' => array(
  431. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  432. 'type' => 'int',
  433. 'not null' => TRUE,
  434. ),
  435. 'data_table' => array(
  436. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  437. 'type' => 'varchar',
  438. 'length' => 128,
  439. 'not null' => TRUE,
  440. 'default' => '',
  441. ),
  442. 'type_table' => array(
  443. 'description' => 'Sometimes the record in the data table doesn’t have a field that specifies the record type. For example, an analysis type is stored in the analysisprop table. If the data_table does have a type field then this value will be the same as the data_table.',
  444. 'type' => 'varchar',
  445. 'length' => 128,
  446. 'not null' => TRUE,
  447. 'default' => '',
  448. ),
  449. 'field' => array(
  450. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  451. 'type' => 'varchar',
  452. 'length' => 128,
  453. 'not null' => FALSE,
  454. 'default' => ''
  455. ),
  456. ),
  457. 'indexes' => array(
  458. 'record_id' => array('record_id'),
  459. 'entity_id' => array('entity_id'),
  460. 'data_table' => array('data_table'),
  461. ),
  462. 'unique keys' => array(
  463. 'record' => array('data_table', 'record_id'),
  464. 'entity_id' => array('entity_id'),
  465. ),
  466. 'primary key' => array('chado_entity_id'),
  467. );
  468. $schema['tripal_panels'] = array(
  469. 'description' => 'The list of panels into which fields can be placed.',
  470. 'fields' => array(
  471. 'panel_id' => array(
  472. 'description' => 'The primary identifier for this table.',
  473. 'type' => 'serial',
  474. 'unsigned' => TRUE,
  475. 'not null' => TRUE,
  476. ),
  477. 'bundle_id' => array(
  478. 'description' => 'A bundle ID from the tripal_bundle table.',
  479. 'type' => 'int',
  480. 'not null' => TRUE,
  481. ),
  482. 'name' => array(
  483. 'description' => 'The computer readable name for the panel. This name must only have alphanumerical characters and underscores and must not begin with a number. ',
  484. 'type' => 'varchar',
  485. 'length' => 128,
  486. 'not null' => TRUE,
  487. 'default' => '',
  488. ),
  489. 'label' => array(
  490. 'description' => 'A human readable name for panel. This name will be shown to users in the sidebar menu.',
  491. 'type' => 'varchar',
  492. 'length' => 128,
  493. 'not null' => TRUE,
  494. 'default' => '',
  495. ),
  496. 'settings' => array(
  497. 'description' => 'Contains a serialized array tripal_fields_layoutof settings for the panel.',
  498. 'type' => 'text',
  499. 'not null' => FALSE,
  500. ),
  501. 'weight' => array(
  502. 'type' => 'int',
  503. 'not null' => FALSE,
  504. 'default' => 0
  505. ),
  506. ),
  507. 'indexes' => array(
  508. 'bundle_id' => array('bundle_id'),
  509. ),
  510. 'unique keys' => array(
  511. 'bundle_panel' => array('bundle_id', 'name'),
  512. ),
  513. 'foreign keys' => array(
  514. 'tripal_bundle' => array(
  515. 'table' => 'tripal_bundle',
  516. 'columns' => array(
  517. 'bundle_id' => 'id',
  518. ),
  519. ),
  520. ),
  521. 'primary key' => array('panel_id'),
  522. );
  523. $schema['tripal_panel_fields'] = array(
  524. 'description' => 'The list of panels into which fields can be placed.',
  525. 'fields' => array(
  526. 'panel_field_id' => array(
  527. 'description' => 'The primary identifier for this table.',
  528. 'type' => 'serial',
  529. 'unsigned' => TRUE,
  530. 'not null' => TRUE,
  531. ),
  532. 'panel_id' => array(
  533. 'description' => 'The primary identifier for this table.',
  534. 'type' => 'int',
  535. 'not null' => TRUE,
  536. ),
  537. 'field_id' => array(
  538. 'description' => 'A bundle ID from the tripal_bundle table.',
  539. 'type' => 'int',
  540. 'not null' => TRUE,
  541. ),
  542. ),
  543. 'indexes' => array(
  544. 'panel_id' => array('panel_id'),
  545. 'field_id' => array('field_id'),
  546. ),
  547. 'unique keys' => array(
  548. 'panel_field' => array('panel_id', 'field_id'),
  549. ),
  550. 'foreign keys' => array(
  551. 'tripal_panel' => array(
  552. 'table' => 'tripal_panel',
  553. 'columns' => array(
  554. 'panel_id' => 'panel_id',
  555. ),
  556. ),
  557. 'field_config' => array(
  558. 'table' => 'field_config',
  559. 'columns' => array(
  560. 'field_id' => 'id',
  561. ),
  562. ),
  563. ),
  564. 'primary key' => array('panel_field_id'),
  565. );
  566. return $schema;
  567. }
  568. /**
  569. * Implements hook_uninstall().
  570. *
  571. * At uninstall time we'll notify field.module that the entity was deleted
  572. * so that attached fields can be cleaned up.
  573. */
  574. function tripal_entities_uninstall() {
  575. $terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  576. foreach ($terms as $term) {
  577. $bundle_id = $term->cvterm_id->dbxref_id->db_id->name . '_' . $term->cvterm_id->dbxref_id->accession;
  578. field_attach_delete_bundle('BioData', $bundle_id);
  579. }
  580. }