tripal_entities.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. // Unfortunately, some Chado base tables do not have a type_id, so we must
  11. // take special action for those tables. These include: organism and
  12. // analysis. Until we can find an appropriate controlled vocabulary
  13. // that is well supported by the community with types for these tables we
  14. // will have to use in-house terms.
  15. // Add a term to be used for an inherent 'type_id' for the organism table.
  16. tripal_insert_cvterm(array(
  17. 'id' => 'local:organism',
  18. 'name' => 'organism',
  19. 'definition' => 'An individual form of life, such as a bacterium, protist, ' .
  20. 'fungus, plant, or animal, composed of a single cell or a complex of cells ' .
  21. 'in which organelles or organs work together to carry out the various ' .
  22. 'processes of life. (American Heritage® Dictionary of the English ' .
  23. 'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
  24. 'Harcourt Publishing Company).',
  25. 'cv_name' => 'local',
  26. ));
  27. // Add a term to be used for an inherent 'type_id' for the organism table.
  28. tripal_insert_cvterm(array(
  29. 'id' => 'local:analysis',
  30. 'name' => 'analysis',
  31. 'definition' => 'A process as a method of studying the nature of something ' .
  32. 'or of determining its essential features and their relations. ' .
  33. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  34. 'Dictionaries Ltd).',
  35. 'cv_name' => 'local',
  36. ));
  37. tripal_insert_cvterm(array(
  38. 'id' => 'local:project',
  39. 'name' => 'project',
  40. 'definition' => 'A plan or proposal for accomplishing something. ' .
  41. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  42. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  43. 'cv_name' => 'local',
  44. ));
  45. // We want to provide a set of commonly used entity types by default. This
  46. // way when a user first installs Tripal there are some commonly used
  47. // formats.
  48. module_load_include('inc', 'tripal_entities', 'api/tripal_entities.api');
  49. module_load_include('inc', 'tripal_entities', 'includes/tripal_entities.admin');
  50. // Create the 'Organism' entity type. This uses the local:organism term.
  51. $error = '';
  52. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  53. $cvterm = chado_generate_var('cvterm', $term);
  54. if (!tripal_create_entity_type($cvterm, $error)) {
  55. throw new Exception($error);
  56. }
  57. // Create the 'Analysis' entity type. This uses the local:analysis term.
  58. $error = '';
  59. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  60. $cvterm = chado_generate_var('cvterm', $term);
  61. if (!tripal_create_entity_type($cvterm, $error)) {
  62. throw new Exception($error);
  63. }
  64. // Create the 'Project' entity type. This uses the local:project term.
  65. $error = '';
  66. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  67. $cvterm = chado_generate_var('cvterm', $term);
  68. if (!tripal_create_entity_type($cvterm, $error)) {
  69. throw new Exception($error);
  70. }
  71. }
  72. /**
  73. * Implements hook_schema().
  74. */
  75. function tripal_entities_schema() {
  76. // Adds a table for managing TripalEntity entities.
  77. $schema['tripal_entity'] = tripal_entities_tripal_entity_schema();
  78. // Adds a table for managing the TripalEntity entity types (bundles).
  79. $schema['tripal_bundle'] = tripal_entities_tripal_bundle_schema();
  80. // Adds a table for additional information related to bundles.
  81. $schema['tripal_bundle_variables'] = tripal_entities_tripal_bundle_variables_schema();
  82. // Links TripalEntity entities to the chado record.
  83. $schema['chado_entity'] = tripal_entities_chado_entity_schema();
  84. return $schema;
  85. }
  86. /**
  87. * Implements hook_uninstall().
  88. *
  89. * At uninstall time we'll notify field.module that the entity was deleted
  90. * so that attached fields can be cleaned up.
  91. */
  92. function tripal_entities_uninstall() {
  93. // $terms = chado_generate_var('tripal_term', array('publish' => 1), array('return_array' => 1));
  94. // foreach ($terms as $term) {
  95. // $bundle_id = 'bio-data_' . $term->cvterm_id;
  96. // field_attach_delete_bundle('TripalEntity', $bundle_id);
  97. // }
  98. }
  99. /**
  100. * @section
  101. * Schema Definitions.
  102. */
  103. /**
  104. * The base table for Biological Data Entities.
  105. *
  106. * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
  107. * this table will have 15 records and include both genes and mRNA's.
  108. */
  109. function tripal_entities_tripal_entity_schema() {
  110. $schema = array(
  111. 'description' => 'The base table for Tripal Vocabulary-based entities.',
  112. 'fields' => array(
  113. 'id' => array(
  114. 'description' => 'The primary identifier for a vocabulary entity.',
  115. 'type' => 'serial',
  116. 'unsigned' => TRUE,
  117. 'not null' => TRUE,
  118. ),
  119. 'type' => array(
  120. 'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
  121. 'type' => 'varchar',
  122. 'length' => 64,
  123. 'not null' => TRUE,
  124. 'default' => '',
  125. ),
  126. 'bundle' => array(
  127. '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.',
  128. 'type' => 'varchar',
  129. 'length' => 1024,
  130. 'not null' => TRUE,
  131. 'default' => '',
  132. ),
  133. 'cvterm_id' => array(
  134. 'description' => 'The cvterm_id for the type of entity. This cvterm_id should match a record in the Chado cvterm table.',
  135. 'type' => 'int',
  136. 'not null' => TRUE,
  137. ),
  138. 'title' => array(
  139. 'description' => 'The title of this node, always treated as non-markup plain text.',
  140. 'type' => 'text',
  141. 'not null' => TRUE,
  142. 'default' => '',
  143. ),
  144. 'uid' => array(
  145. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  146. 'type' => 'int',
  147. 'not null' => TRUE,
  148. 'default' => 0,
  149. ),
  150. 'status' => array(
  151. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  152. 'type' => 'int',
  153. 'not null' => TRUE,
  154. 'default' => 1,
  155. ),
  156. 'created' => array(
  157. 'description' => 'The Unix timestamp when the node was created.',
  158. 'type' => 'int',
  159. 'not null' => TRUE,
  160. 'default' => 0,
  161. ),
  162. 'changed' => array(
  163. 'description' => 'The Unix timestamp when the node was most recently saved.',
  164. 'type' => 'int',
  165. 'not null' => TRUE,
  166. 'default' => 0,
  167. ),
  168. ),
  169. 'indexes' => array(
  170. 'cvterm_id' => array('cvterm_id'),
  171. 'entity_changed' => array('changed'),
  172. 'entity_created' => array('created'),
  173. 'type' => array('type'),
  174. 'uid' => array('uid'),
  175. ),
  176. 'unique keys' => array(
  177. ),
  178. 'primary key' => array('id'),
  179. );
  180. return $schema;
  181. }
  182. /**
  183. * The base table for Biological Data Type Entites.
  184. * This table contains a list of Biological Data Types.
  185. * For the example above (5 genes and 10 mRNAs), there would only be two records in
  186. * this table one for "gene" and another for "mRNA".
  187. */
  188. function tripal_entities_tripal_bundle_schema() {
  189. $schema = array(
  190. 'description' => 'Stores information about defined tripal data types.',
  191. 'fields' => array(
  192. 'id' => array(
  193. 'type' => 'serial',
  194. 'not null' => TRUE,
  195. 'description' => 'Primary Key: Unique Chado data type identifier.',
  196. ),
  197. 'type' => array(
  198. 'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
  199. 'type' => 'varchar',
  200. 'length' => 64,
  201. 'not null' => TRUE,
  202. 'default' => '',
  203. ),
  204. 'bundle' => array(
  205. '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.',
  206. 'type' => 'varchar',
  207. 'length' => 1024,
  208. 'not null' => TRUE,
  209. 'default' => '',
  210. ),
  211. 'label' => array(
  212. 'description' => 'The human-readable name of this bundle.',
  213. 'type' => 'varchar',
  214. 'length' => 255,
  215. 'not null' => TRUE,
  216. 'default' => '',
  217. ),
  218. 'weight' => array(
  219. 'type' => 'int',
  220. 'not null' => TRUE,
  221. 'default' => 0,
  222. 'size' => 'tiny',
  223. 'description' => 'The weight of this tripal data type in relation to others.',
  224. ),
  225. ) + entity_exportable_schema_fields(),
  226. 'primary key' => array('id'),
  227. 'unique keys' => array(
  228. 'bundle' => array('bundle'),
  229. ),
  230. );
  231. return $schema;
  232. }
  233. /**
  234. * Additional Tripal Bundle Information.
  235. *
  236. * This table is used for storing any additonal information describing
  237. * a tripal bundle. For example, this is a good place to store title/url formats.
  238. */
  239. function tripal_entities_tripal_bundle_variables_schema() {
  240. $schema = array(
  241. 'description' => 'This table is used for storing any additonal information describing
  242. a tripal bundle. For example, this is a good place to store title/url formats.',
  243. 'fields' => array (
  244. 'bundle_variable_id' => array (
  245. 'type' => 'serial',
  246. 'not null' => TRUE,
  247. ),
  248. 'bundle_id' => array (
  249. 'type' => 'int',
  250. 'not null' => TRUE,
  251. ),
  252. 'variable_id' => array (
  253. 'type' => 'int',
  254. 'not null' => TRUE,
  255. ),
  256. 'value' => array (
  257. 'type' => 'text',
  258. 'not null' => FALSE,
  259. ),
  260. 'rank' => array (
  261. 'type' => 'int',
  262. 'not null' => TRUE,
  263. 'default' => 0,
  264. ),
  265. ),
  266. 'primary key' => array (
  267. 0 => 'bundle_variable_id',
  268. ),
  269. 'unique keys' => array (
  270. 'tripal_bundle_variables_c1' => array (
  271. 0 => 'bundle_id',
  272. 1 => 'variable_id',
  273. 2 => 'rank',
  274. ),
  275. ),
  276. 'indexes' => array (
  277. 'tripal_bundle_variables_idx1' => array (
  278. 0 => 'variable_id',
  279. ),
  280. ),
  281. 'foreign keys' => array (
  282. 'tripal_variables' => array (
  283. 'table' => 'tripal_variables',
  284. 'columns' => array (
  285. 'variable_id' => 'variable_id',
  286. ),
  287. ),
  288. ),
  289. );
  290. return $schema;
  291. }
  292. /**
  293. * Links Biological Data Entities to the chado "base" table the data is stored in.
  294. * This is where we would specify that a particular gene maps to the record in the
  295. * chado.feature table with a feature_id=2432;
  296. */
  297. function tripal_entities_chado_entity_schema() {
  298. $schema = array(
  299. 'description' => 'The linker table that associates an enitity from the public.tripal_entity table with a "base" record in Chado',
  300. 'fields' => array(
  301. 'chado_entity_id' => array(
  302. 'description' => 'The primary identifier for this table.',
  303. 'type' => 'serial',
  304. 'unsigned' => TRUE,
  305. 'not null' => TRUE,
  306. ),
  307. 'entity_id' => array(
  308. 'description' => 'The unique entity id.',
  309. 'type' => 'int',
  310. 'not null' => TRUE,
  311. ),
  312. 'record_id' => array(
  313. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  314. 'type' => 'int',
  315. 'not null' => TRUE,
  316. ),
  317. 'data_table' => array(
  318. 'description' => 'Indicates the table in Chado that this term services (e.g. feature, stock, library, etc.)',
  319. 'type' => 'varchar',
  320. 'length' => 128,
  321. 'not null' => TRUE,
  322. 'default' => '',
  323. ),
  324. 'type_table' => array(
  325. '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.',
  326. 'type' => 'varchar',
  327. 'length' => 128,
  328. 'not null' => TRUE,
  329. 'default' => '',
  330. ),
  331. 'field' => array(
  332. 'description' => 'The name of the field in the typetable that contains the cvterm record.',
  333. 'type' => 'varchar',
  334. 'length' => 128,
  335. 'not null' => FALSE,
  336. 'default' => ''
  337. ),
  338. ),
  339. 'indexes' => array(
  340. 'record_id' => array('record_id'),
  341. 'entity_id' => array('entity_id'),
  342. 'data_table' => array('data_table'),
  343. ),
  344. 'unique keys' => array(
  345. 'record' => array('data_table', 'record_id'),
  346. 'entity_id' => array('entity_id'),
  347. ),
  348. 'primary key' => array('chado_entity_id'),
  349. );
  350. return $schema;
  351. }