tripal_entities.install 19 KB

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