tripal_entities.install 17 KB

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