tripal_entities.install 13 KB

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