tripal_entities.install 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 external databases used by the file module.
  8. // tripal_entities_add_dbs();
  9. // add any controlled vocabularies used by the file module.
  10. // tripal_entities_add_cvs();
  11. // add any controlled vocabulary terms
  12. // tripal_entities_add_cvterms();
  13. // add any custom tables. For this case we will add an 'file' table to the
  14. // chado schema
  15. tripal_entities_add_custom_tables();
  16. }
  17. /**
  18. *
  19. */
  20. function tripal_entities_add_custom_tables() {
  21. tripal_entities_add_tripal_vocabulary_table();
  22. tripal_entities_add_tripal_vocabulary_usage_table();
  23. tripal_entities_add_tripal_term_table();
  24. tripal_entities_add_tripal_term_usage_table();
  25. tripal_entities_add_tripal_term_relationship_table();
  26. }
  27. /**The current Entity Example (Genes) requires the Sequence Ontology. [warning]
  28. *
  29. */
  30. function tripal_entities_add_tripal_vocabulary_table() {
  31. $schema = array (
  32. 'table' => 'tripal_vocabulary',
  33. 'fields' => array (
  34. 'vocabulary_id' => array(
  35. 'type' => 'serial',
  36. 'not null' => TRUE
  37. ),
  38. 'cv_id' => array (
  39. 'type' => 'int',
  40. 'not null' => TRUE
  41. ),
  42. 'db_id' => array (
  43. 'type' => 'int',
  44. 'not null' => TRUE
  45. ),
  46. 'publish' => array (
  47. 'type' => 'int',
  48. 'not null' => TRUE,
  49. 'default' => 0
  50. ),
  51. ),
  52. 'primary key' => array (
  53. 0 => 'vocabulary_id'
  54. ),
  55. 'foreign keys' => array (
  56. 'cv' => array (
  57. 'table' => 'cv',
  58. 'columns' => array (
  59. 'cv_id' => 'cv_id'
  60. )
  61. ),
  62. 'db' => array (
  63. 'table' => 'db',
  64. 'columns' => array (
  65. 'db_id' => 'db_id'
  66. )
  67. ),
  68. ),
  69. 'unique keys' => array (
  70. 'tripal_vocabulary_cvdb' => array (
  71. 'cv_id', 'db_id'
  72. ),
  73. ),
  74. 'indexes' => array(
  75. 'tripal_vocabulary_cv_id' => array('cv_id'),
  76. 'tripal_vocabulary_db_id' => array('db_id'),
  77. )
  78. );
  79. chado_create_custom_table('tripal_vocabulary', $schema, TRUE);
  80. }
  81. /**
  82. *
  83. */
  84. function tripal_entities_add_tripal_term_table() {
  85. $schema = array (
  86. 'table' => 'tripal_term',
  87. 'fields' => array (
  88. 'term_id' => array(
  89. 'type' => 'serial',
  90. 'not null' => TRUE
  91. ),
  92. 'vocabulary_id' => array (
  93. 'type' => 'int',
  94. 'not null' => TRUE
  95. ),
  96. 'cvterm_id' => array (
  97. 'type' => 'int',
  98. 'not null' => TRUE
  99. ),
  100. 'publish' => array (
  101. 'type' => 'int',
  102. 'not null' => TRUE,
  103. 'default' => 0
  104. ),
  105. ),
  106. 'primary key' => array (
  107. 0 => 'term_id'
  108. ),
  109. 'foreign keys' => array (
  110. 'cvterm' => array (
  111. 'table' => 'cvterm',
  112. 'columns' => array (
  113. 'cvterm_id' => 'cvterm_id'
  114. )
  115. ),
  116. 'tripal_vocabulary' => array (
  117. 'table' => 'tripal_vocabulary',
  118. 'columns' => array (
  119. 'vocabulary_id' => 'vocabulary_id'
  120. )
  121. ),
  122. ),
  123. 'unique keys' => array (
  124. 'tripal_term_unq' => array (
  125. 'vocabulary_id', 'cvterm_id'
  126. ),
  127. ),
  128. 'indexes' => array(
  129. 'tripal_term_vocabulary_id' => array('vocabulary_id'),
  130. 'tripal_term_cvterm_id' => array('cvterm_id'),
  131. ),
  132. );
  133. chado_create_custom_table('tripal_term', $schema, TRUE);
  134. }
  135. /**
  136. *
  137. */
  138. function tripal_entities_add_tripal_vocabulary_usage_table(){
  139. $schema = array (
  140. 'table' => 'tripal_vocabulary_usage',
  141. 'fields' => array (
  142. 'vocabulary_usage_id' => array(
  143. 'type' => 'serial',
  144. 'not null' => TRUE
  145. ),
  146. 'vocabulary_id' => array (
  147. 'type' => 'int',
  148. 'not null' => TRUE
  149. ),
  150. 'data_table' => array (
  151. 'type' => 'varchar',
  152. 'length' => 128,
  153. 'not null' => TRUE
  154. ),
  155. 'type_table' => array (
  156. 'type' => 'varchar',
  157. 'length' => 128,
  158. 'not null' => TRUE
  159. ),
  160. 'field' => array (
  161. 'type' => 'varchar',
  162. 'length' => 128,
  163. 'not null' => TRUE
  164. ),
  165. ),
  166. 'primary key' => array (
  167. 0 => 'vocabulary_usage_id'
  168. ),
  169. 'foreign keys' => array (
  170. 'tripal_vocabulary' => array (
  171. 'table' => 'tripal_vocabulary',
  172. 'columns' => array (
  173. 'vocabulary_id' => 'vocabulary_id'
  174. ),
  175. ),
  176. ),
  177. 'unique keys' => array (
  178. 'tripal_vocabulary_ridbase' => array (
  179. 'vocabulary_id', 'data_table'
  180. ),
  181. ),
  182. 'indexes' => array(
  183. 'tripal_vocabulary_vocabulary_id' => array('vocabulary_id'),
  184. 'tripal_vocabulary_data_table' => array('data_table'),
  185. 'tripal_vocabulary_type_table' => array('type_table'),
  186. ),
  187. );
  188. chado_create_custom_table('tripal_vocabulary_usage', $schema, TRUE);
  189. }
  190. /**
  191. *
  192. */
  193. function tripal_entities_add_tripal_term_usage_table(){
  194. $schema = array (
  195. 'table' => 'tripal_term_usage',
  196. 'fields' => array (
  197. 'term_usage_id' => array(
  198. 'type' => 'serial',
  199. 'not null' => TRUE
  200. ),
  201. 'term_id' => array (
  202. 'type' => 'int',
  203. 'not null' => TRUE
  204. ),
  205. 'data_table' => array (
  206. 'type' => 'varchar',
  207. 'length' => 128,
  208. 'not null' => TRUE
  209. ),
  210. 'type_table' => array (
  211. 'type' => 'varchar',
  212. 'length' => 128,
  213. 'not null' => TRUE
  214. ),
  215. 'field' => array (
  216. 'type' => 'varchar',
  217. 'length' => 128,
  218. 'not null' => TRUE
  219. ),
  220. ),
  221. 'primary key' => array (
  222. 0 => 'term_usage_id'
  223. ),
  224. 'foreign keys' => array (
  225. 'tripal_term' => array (
  226. 'table' => 'tripal_term',
  227. 'columns' => array (
  228. 'term_id' => 'term_id'
  229. ),
  230. ),
  231. ),
  232. 'unique keys' => array (
  233. 'tripal_term_usage_ridbase' => array (
  234. 'term_id', 'type_table', 'field'
  235. ),
  236. ),
  237. 'indexes' => array(
  238. 'tripal_term_usage_term_id' => array('term_id'),
  239. ),
  240. );
  241. chado_create_custom_table('tripal_term_usage', $schema, TRUE);
  242. }
  243. /**
  244. *
  245. */
  246. function tripal_entities_add_tripal_term_relationship_table(){
  247. $schema = array (
  248. 'table' => 'tripal_term_relationship',
  249. 'fields' => array (
  250. 'relationship_id' => array(
  251. 'type' => 'serial',
  252. 'not null' => TRUE
  253. ),
  254. 'subject_id' => array (
  255. 'type' => 'int',
  256. 'not null' => TRUE
  257. ),
  258. 'type_id' => array (
  259. 'type' => 'int',
  260. 'not null' => TRUE
  261. ),
  262. 'object_id' => array (
  263. 'type' => 'int',
  264. 'not null' => FALSE
  265. ),
  266. 'fieldname' => array(
  267. 'type' => 'varchar',
  268. 'length' => 128,
  269. 'not null' => FALSE,
  270. )
  271. ),
  272. 'primary key' => array (
  273. 0 => 'relationship_id'
  274. ),
  275. 'foreign keys' => array (
  276. 'tripal_term' => array (
  277. 'table' => 'tripal_term',
  278. 'columns' => array (
  279. 'subject_id' => 'term_id',
  280. 'object_id' => 'term_id',
  281. ),
  282. ),
  283. ),
  284. 'unique keys' => array (
  285. 'tripal_term_relationship_unq' => array (
  286. 'subject_id', 'type_id', 'object_id'
  287. ),
  288. ),
  289. 'indexes' => array(
  290. 'tripal_term_relationship_subject_id' => array('subject_id'),
  291. 'tripal_term_relationship_object_id' => array('object_id'),
  292. 'tripal_term_relationship_type_id' => array('type_id'),
  293. ),
  294. );
  295. chado_create_custom_table('tripal_term_relationship', $schema, TRUE);
  296. }
  297. /**
  298. * Implements hook_schema().
  299. *
  300. * @ingroup entity_example
  301. */
  302. function tripal_entities_schema() {
  303. $schema['tripal_entity'] = array(
  304. 'description' => 'The base table for Tripal Vocabulary-based entities.',
  305. 'fields' => array(
  306. 'id' => array(
  307. 'description' => 'The primary identifier for a vocabulary entity.',
  308. 'type' => 'serial',
  309. 'unsigned' => TRUE,
  310. 'not null' => TRUE,
  311. ),
  312. 'type' => array(
  313. 'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
  314. 'type' => 'varchar',
  315. 'length' => 64,
  316. 'not null' => TRUE,
  317. 'default' => '',
  318. ),
  319. 'bundle' => array(
  320. '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.',
  321. 'type' => 'varchar',
  322. 'length' => 1024,
  323. 'not null' => TRUE,
  324. 'default' => '',
  325. ),
  326. 'title' => array(
  327. 'description' => 'The title of this node, always treated as non-markup plain text.',
  328. 'type' => 'text',
  329. 'not null' => TRUE,
  330. 'default' => '',
  331. ),
  332. 'uid' => array(
  333. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  334. 'type' => 'int',
  335. 'not null' => TRUE,
  336. 'default' => 0,
  337. ),
  338. 'status' => array(
  339. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  340. 'type' => 'int',
  341. 'not null' => TRUE,
  342. 'default' => 1,
  343. ),
  344. 'created' => array(
  345. 'description' => 'The Unix timestamp when the node was created.',
  346. 'type' => 'int',
  347. 'not null' => TRUE,
  348. 'default' => 0,
  349. ),
  350. 'changed' => array(
  351. 'description' => 'The Unix timestamp when the node was most recently saved.',
  352. 'type' => 'int',
  353. 'not null' => TRUE,
  354. 'default' => 0,
  355. ),
  356. ),
  357. 'indexes' => array(
  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. return $schema;
  469. }
  470. /**
  471. * Implements hook_uninstall().
  472. *
  473. * At uninstall time we'll notify field.module that the entity was deleted
  474. * so that attached fields can be cleaned up.
  475. */
  476. function tripal_entities_uninstall() {
  477. // TODO: make this dynamic (not hardcoded bundle).
  478. $terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  479. foreach ($terms as $term) {
  480. $entity_type = $term->vocab_id->db_id->name;
  481. $bundle_id = $term->cvterm_id->dbxref_id->db_id->name . '_' . $term->cvterm_id->dbxref_id->accession;
  482. print "$entity_type : $bundle_id\n";
  483. field_attach_delete_bundle($entity_type, $bundle_id);
  484. }
  485. }