tripal_chado.setup.inc 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Prepares Chado for Tripal use
  8. */
  9. function tripal_chado_prepare_form($form, $form_state) {
  10. $form = array();
  11. if (variable_get('tripal_chado_is_prepared') == TRUE) {
  12. drupal_set_message('Your site is prepared.');
  13. }
  14. $form['instructions'] = array(
  15. '#type' => 'item',
  16. '#title' => 'Prepare Drupal for Chado.',
  17. '#description' => t("Before a Drupal site can use Chado (via Tripal), both
  18. Chado and Drupal must be prepared a bit more. Tripal will add some new
  19. materialized views, custom tables and controlled vocabularies to Chado.
  20. It will also add some management tables to Drupal. You only are
  21. required to prepare your Drupal site if this is a brand-new Drupal
  22. installation or if Chado was installed outside of Tripal. If you
  23. installed Chado using Tripal then you do not need to run this step.
  24. If you are upgrading from a previous version of Tripal, you do not
  25. need to prepare your site, and you can click the 'Skip' button."),
  26. );
  27. $form['prepare-button'] = array(
  28. '#type' => 'submit',
  29. '#value' => t('Prepare this site'),
  30. '#name' => 'prepare-chado',
  31. );
  32. $form['skip-button'] = array(
  33. '#type' => 'submit',
  34. '#value' => t('Skip'),
  35. '#name' => 'prepare-skip',
  36. );
  37. return $form;
  38. }
  39. /**
  40. * Submit function for the tripal_chado_prepare_form().
  41. *
  42. * @param $form
  43. * @param $form_state
  44. */
  45. function tripal_chado_prepare_form_submit($form, $form_state) {
  46. if ($form_state['clicked_button']['#name'] == "prepare-chado") {
  47. global $user;
  48. $args = array();
  49. $includes = array(
  50. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup'),
  51. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader'),
  52. );
  53. tripal_add_job('Prepare Chado', 'tripal_chado',
  54. 'tripal_chado_prepare_chado', $args,
  55. $user->uid, 10, $includes);
  56. }
  57. if ($form_state['clicked_button']['#name'] == "prepare-skip") {
  58. variable_set('tripal_chado_is_prepared', TRUE);
  59. }
  60. }
  61. /**
  62. *
  63. */
  64. function tripal_chado_load_ontologies() {
  65. // Insert commonly used ontologies into the tables.
  66. $ontologies = array(
  67. array(
  68. 'name' => 'Relationship Ontology (legacy)',
  69. 'path' => '{tripal_chado}/files/legacy_ro.obo',
  70. 'auto_load' => FALSE,
  71. 'cv_name' => 'ro'
  72. ),
  73. array(
  74. 'name' => 'Gene Ontology',
  75. 'path' => 'http://purl.obolibrary.org/obo/go.obo',
  76. 'auto_load' => FALSE,
  77. 'cv_name' => 'cellualar_component'
  78. ),
  79. array(
  80. 'name' => 'Taxonomic Rank',
  81. 'path' => 'http://purl.obolibrary.org/obo/taxrank.obo',
  82. 'auto_load' => TRUE,
  83. 'cv_name' => 'taxrank'
  84. ),
  85. array(
  86. 'name' => 'Tripal Contact',
  87. 'path' => '{tripal_chado}/files/tcontact.obo',
  88. 'auto_load' => TRUE,
  89. 'cv_name' => 'tripal_contact'
  90. ),
  91. array(
  92. 'name' => 'Tripal Publication',
  93. 'path' => '{tripal_chado}/files/tpub.obo',
  94. 'auto_load' => TRUE,
  95. 'cv_name' => 'tripal_pub'
  96. ),
  97. array(
  98. 'name' => 'Sequence Ontology',
  99. 'path' => 'http://purl.obolibrary.org/obo/so.obo',
  100. 'auto_load' => TRUE,
  101. 'cv_name' => 'sequence'
  102. ),
  103. );
  104. for ($i = 0; $i < count($ontologies); $i++) {
  105. $obo_id = tripal_insert_obo($ontologies[$i]['name'], $ontologies[$i]['path']);
  106. if ($ontologies[$i]['auto_load'] == TRUE) {
  107. // Only load ontolgogies that are not already in the cv table.
  108. $cv = tripal_get_cv(array('name' => $ontologies[$i]['cv_name']));
  109. if (!$cv) {
  110. tripal_chado_load_obo_v1_2_id($obo_id);
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Prepares Chado for use by Tripal.
  117. */
  118. function tripal_chado_prepare_chado() {
  119. try {
  120. // Get the effective version. Pass true as second argument
  121. // to warn the user if the current version is not compatible.
  122. $version = chado_get_version(FALSE, FALSE);
  123. // We want to force the version of Chado to be set properly.
  124. $real_version = chado_get_version(TRUE);
  125. // Import commonly used ontologies if needed.
  126. tripal_chado_load_ontologies();
  127. // Populate the semantic web associations for Chado tables/fields.
  128. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.semweb');
  129. tripal_chado_populate_chado_semweb_table();
  130. // Initialize the population of the chado_cvterm_mapping table. This will
  131. // map existing data types already in Chado so that when users want to
  132. // add new content types it simplifies the form for them.
  133. tripal_chado_map_cvterms();
  134. // Add the cv_root_mview.
  135. tripal_chado_add_cv_root_mview_mview();
  136. // Create the temp table we will use for loading OBO files.
  137. tripal_chado_add_tripal_obo_temp_table();
  138. // Support for Analyses
  139. // we may need the analysisfeatureprop table if it doesn't already exist
  140. tripal_chado_add_analysisfeatureprop_table();
  141. // add materialized views
  142. tripal_chado_add_analysis_organism_mview();
  143. // Support for Contacts
  144. // Add the contactprop table to Chado.
  145. tripal_chado_add_contactprop_table();
  146. // Support for Features
  147. // Add the materialized view.
  148. tripal_chado_add_organism_feature_count_mview();
  149. // Add the custom tables for the GFF loader.
  150. tripal_chado_add_tripal_gff_temp_table();
  151. tripal_chado_add_tripal_gffcds_temp_table();
  152. tripal_chado_add_tripal_gffprotein_temp_table();
  153. // Support for FeatureMaps.
  154. // add the featuremapprop table to Chado
  155. tripal_chado_add_featuremap_dbxref_table();
  156. tripal_chado_add_featuremapprop_table();
  157. //tripal_chado_add_featuremappos_table();
  158. // Support for Libraries.
  159. // add the materialized view
  160. tripal_chado_add_library_feature_count_mview();
  161. // Support for Pubs.
  162. // add the custom tables
  163. tripal_chado_add_pubauthor_contact_table();
  164. // Add the supported loaders
  165. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  166. // Support for Stocks.
  167. // add the materialized view
  168. tripal_chado_add_organism_stock_count_mview();
  169. // We want to provide a set of commonly used entity types by default. This
  170. // way when a user first installs Tripal there are some commonly used
  171. // formats.
  172. module_load_include('inc', 'tripal', 'api/tripal.api');
  173. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  174. // Create the 'Organism' entity type. This uses the obi:organism term.
  175. $error = '';
  176. $args = array(
  177. 'vocabulary' => 'OBI',
  178. 'accession' => '0100026',
  179. 'term_name' => 'organism',
  180. 'storage_args' => array(
  181. 'data_table' => 'organism',
  182. )
  183. );
  184. if (!tripal_create_bundle($args, $error)) {
  185. throw new Exception($error['!message']);
  186. }
  187. // Create the 'Analysis' entity type. This uses the local:analysis term.
  188. $error = '';
  189. $args = array(
  190. 'vocabulary' => 'local',
  191. 'accession' => 'analysis',
  192. 'term_name' => 'analysis',
  193. 'storage_args' => array(
  194. 'data_table' => 'analysis',
  195. )
  196. );
  197. if (!tripal_create_bundle($args, $error)) {
  198. throw new Exception($error['!message']);
  199. }
  200. // Create the 'Project' entity type. This uses the local:project term.
  201. $error = '';
  202. $args = array(
  203. 'vocabulary' => 'local',
  204. 'accession' => 'project',
  205. 'term_name' => 'project',
  206. 'storage_args' => array(
  207. 'data_table' => 'project',
  208. )
  209. );
  210. if (!tripal_create_bundle($args, $error)) {
  211. throw new Exception($error['!message']);
  212. }
  213. // Create the 'Map' entity type. This uses the local:project term.
  214. $error = '';
  215. $args = array(
  216. 'vocabulary' => 'data',
  217. 'accession' => '1274',
  218. 'term_name' => 'Map',
  219. 'storage_args' => array(
  220. 'data_table' => 'featuremap',
  221. )
  222. );
  223. if (!tripal_create_bundle($args, $error)) {
  224. throw new Exception($error['!message']);
  225. }
  226. // Create the 'Publication' entity type.
  227. $error = '';
  228. $args = array(
  229. 'vocabulary' => 'TPUB',
  230. 'accession' => '0000002',
  231. 'term_name' => 'Publication',
  232. 'storage_args' => array(
  233. 'data_table' => 'pub',
  234. )
  235. );
  236. if (!tripal_create_bundle($args, $error)) {
  237. throw new Exception($error['!message']);
  238. }
  239. // Create the 'Gene' entity type.
  240. $error = '';
  241. $args = array(
  242. 'vocabulary' => 'SO',
  243. 'accession' => '0000704',
  244. 'term_name' => 'gene',
  245. 'storage_args' => array(
  246. 'data_table' => 'feature',
  247. 'type_column' => 'type_id',
  248. )
  249. );
  250. if (!tripal_create_bundle($args, $error)) {
  251. throw new Exception($error['!message']);
  252. }
  253. // Create the 'mRNA' entity type.
  254. $error = '';
  255. $args = array(
  256. 'vocabulary' => 'SO',
  257. 'accession' => '0000234',
  258. 'term_name' => 'mRNA',
  259. 'storage_args' => array(
  260. 'data_table' => 'feature',
  261. 'type_column' => 'type_id',
  262. )
  263. );
  264. if (!tripal_create_bundle($args, $error)) {
  265. throw new Exception($error['!message']);
  266. }
  267. // Set a variable to indicate the site is prepared.
  268. variable_set('tripal_chado_is_prepared', TRUE);
  269. }
  270. catch (Exception $e) {
  271. throw new Exception($e);
  272. }
  273. }
  274. /**
  275. * Creates a materialized view that stores the type & number of stocks per organism
  276. *
  277. * @ingroup tripal_stock
  278. */
  279. function tripal_chado_add_organism_stock_count_mview() {
  280. $view_name = 'organism_stock_count';
  281. $comment = 'Stores the type and number of stocks per organism';
  282. $schema = array(
  283. 'description' => $comment,
  284. 'table' => $view_name,
  285. 'fields' => array(
  286. 'organism_id' => array(
  287. 'size' => 'big',
  288. 'type' => 'int',
  289. 'not null' => TRUE,
  290. ),
  291. 'genus' => array(
  292. 'type' => 'varchar',
  293. 'length' => '255',
  294. 'not null' => TRUE,
  295. ),
  296. 'species' => array(
  297. 'type' => 'varchar',
  298. 'length' => '255',
  299. 'not null' => TRUE,
  300. ),
  301. 'common_name' => array(
  302. 'type' => 'varchar',
  303. 'length' => '255',
  304. 'not null' => FALSE,
  305. ),
  306. 'num_stocks' => array(
  307. 'type' => 'int',
  308. 'not null' => TRUE,
  309. ),
  310. 'cvterm_id' => array(
  311. 'size' => 'big',
  312. 'type' => 'int',
  313. 'not null' => TRUE,
  314. ),
  315. 'stock_type' => array(
  316. 'type' => 'varchar',
  317. 'length' => '255',
  318. 'not null' => TRUE,
  319. ),
  320. ),
  321. 'indexes' => array(
  322. 'organism_stock_count_idx1' => array('organism_id'),
  323. 'organism_stock_count_idx2' => array('cvterm_id'),
  324. 'organism_stock_count_idx3' => array('stock_type'),
  325. ),
  326. );
  327. $sql = "
  328. SELECT
  329. O.organism_id, O.genus, O.species, O.common_name,
  330. count(S.stock_id) as num_stocks,
  331. CVT.cvterm_id, CVT.name as stock_type
  332. FROM organism O
  333. INNER JOIN stock S ON O.Organism_id = S.organism_id
  334. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  335. GROUP BY
  336. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  337. ";
  338. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  339. }
  340. /**
  341. * Add custom table related to publications
  342. * - pubauthor_contact
  343. *
  344. * @ingroup tripal_pub
  345. */
  346. function tripal_chado_add_pubauthor_contact_table() {
  347. $schema = array (
  348. 'table' => 'pubauthor_contact',
  349. 'fields' => array (
  350. 'pubauthor_contact_id' => array (
  351. 'type' => 'serial',
  352. 'not null' => true,
  353. ),
  354. 'contact_id' => array (
  355. 'type' => 'int',
  356. 'not null' => true,
  357. ),
  358. 'pubauthor_id' => array (
  359. 'type' => 'int',
  360. 'not null' => true,
  361. ),
  362. ),
  363. 'primary key' => array (
  364. 0 => 'pubauthor_contact_id',
  365. ),
  366. 'unique keys' => array (
  367. 'pubauthor_contact_c1' => array (
  368. 0 => 'contact_id',
  369. 1 => 'pubauthor_id',
  370. ),
  371. ),
  372. 'foreign keys' => array (
  373. 'contact' => array (
  374. 'table' => 'contact',
  375. 'columns' => array (
  376. 'contact_id' => 'contact_id',
  377. ),
  378. ),
  379. 'pubauthor' => array (
  380. 'table' => 'pubauthor',
  381. 'columns' => array (
  382. 'pubauthor_id' => 'pubauthor_id',
  383. ),
  384. ),
  385. ),
  386. );
  387. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  388. }
  389. /**
  390. * Adds a materialized view keeping track of the type of features associated with each library
  391. *
  392. * @ingroup tripal_library
  393. */
  394. function tripal_chado_add_library_feature_count_mview(){
  395. $view_name = 'library_feature_count';
  396. $comment = 'Provides count of feature by type that are associated with all libraries';
  397. $schema = array(
  398. 'table' => $view_name,
  399. 'description' => $comment,
  400. 'fields' => array(
  401. 'library_id' => array(
  402. 'size' => 'big',
  403. 'type' => 'int',
  404. 'not null' => TRUE,
  405. ),
  406. 'name' => array(
  407. 'type' => 'varchar',
  408. 'length' => 255,
  409. 'not null' => TRUE,
  410. ),
  411. 'num_features' => array(
  412. 'type' => 'int',
  413. 'not null' => TRUE,
  414. ),
  415. 'feature_type' => array(
  416. 'type' => 'varchar',
  417. 'length' => 255,
  418. 'not null' => TRUE,
  419. ),
  420. ),
  421. 'indexes' => array(
  422. 'library_feature_count_idx1' => array('library_id'),
  423. ),
  424. );
  425. $sql = "
  426. SELECT
  427. L.library_id, L.name,
  428. count(F.feature_id) as num_features,
  429. CVT.name as feature_type
  430. FROM library L
  431. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  432. INNER JOIN feature F ON LF.feature_id = F.feature_id
  433. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  434. GROUP BY L.library_id, L.name, CVT.name
  435. ";
  436. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  437. }
  438. /**
  439. * Add custom tables needed by the feature map module
  440. * - featuremapprop
  441. * - featuremap_dbxref
  442. * - featureposprop
  443. *
  444. * @ingroup tripal_featuremap
  445. */
  446. function tripal_chado_add_featuremapprop_table(){
  447. // add the featuremaprop table to Chado
  448. $schema = array (
  449. 'table' => 'featuremapprop',
  450. 'fields' => array (
  451. 'featuremapprop_id' => array (
  452. 'type' => 'serial',
  453. 'not null' => true,
  454. ),
  455. 'featuremap_id' => array (
  456. 'type' => 'int',
  457. 'not null' => true,
  458. ),
  459. 'type_id' => array (
  460. 'type' => 'int',
  461. 'not null' => true,
  462. ),
  463. 'value' => array (
  464. 'type' => 'text',
  465. 'not null' => false,
  466. ),
  467. 'rank' => array (
  468. 'type' => 'int',
  469. 'not null' => true,
  470. 'default' => 0,
  471. ),
  472. ),
  473. 'primary key' => array (
  474. 0 => 'featuremapprop_id',
  475. ),
  476. 'unique keys' => array (
  477. 'featuremapprop_c1' => array (
  478. 0 => 'featuremap_id',
  479. 1 => 'type_id',
  480. 2 => 'rank',
  481. ),
  482. ),
  483. 'indexes' => array (
  484. 'featuremapprop_idx1' => array (
  485. 0 => 'featuremap_id',
  486. ),
  487. 'featuremapprop_idx2' => array (
  488. 0 => 'type_id',
  489. ),
  490. ),
  491. 'foreign keys' => array (
  492. 'cvterm' => array (
  493. 'table' => 'cvterm',
  494. 'columns' => array (
  495. 'type_id' => 'cvterm_id',
  496. ),
  497. ),
  498. 'featuremap' => array (
  499. 'table' => 'featuremap',
  500. 'columns' => array (
  501. 'featuremap_id' => 'featuremap_id',
  502. ),
  503. ),
  504. ),
  505. );
  506. chado_create_custom_table('featuremapprop', $schema, TRUE);
  507. }
  508. /**
  509. *
  510. */
  511. function tripal_chado_add_featuremap_dbxref_table(){
  512. // add the featuremap_dbxref table to Chado
  513. $schema = array (
  514. 'table' => 'featuremap_dbxref',
  515. 'fields' => array (
  516. 'featuremap_dbxref_id' => array (
  517. 'type' => 'serial',
  518. 'not null' => true,
  519. ),
  520. 'featuremap_id' => array (
  521. 'type' => 'int',
  522. 'not null' => true,
  523. ),
  524. 'dbxref_id' => array (
  525. 'type' => 'int',
  526. 'not null' => true,
  527. ),
  528. ),
  529. 'primary key' => array (
  530. 0 => 'featuremap_dbxref_id',
  531. ),
  532. 'unique keys' => array (
  533. 'featuremap_dbxref_c1' => array (
  534. 0 => 'featuremap_id',
  535. 1 => 'dbxref_id',
  536. ),
  537. ),
  538. 'indexes' => array (
  539. 'featuremap_dbxref_idx1' => array (
  540. 0 => 'featuremap_dbxref_id',
  541. ),
  542. 'featuremap_dbxref_idx2' => array (
  543. 0 => 'dbxref_id',
  544. ),
  545. ),
  546. 'foreign keys' => array (
  547. 'dbxref' => array (
  548. 'table' => 'dbxref',
  549. 'columns' => array (
  550. 'dbxref_id' => 'dbxref_id',
  551. ),
  552. ),
  553. 'featuremap' => array (
  554. 'table' => 'featuremap',
  555. 'columns' => array (
  556. 'featuremap_id' => 'featuremap_id',
  557. ),
  558. ),
  559. ),
  560. 'referring_tables' => NULL,
  561. );
  562. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  563. }
  564. /**
  565. *
  566. */
  567. function tripal_chado_add_featureposprop_table(){
  568. $schema = array (
  569. 'table' => 'featureposprop',
  570. 'fields' => array (
  571. 'featureposprop_id' => array (
  572. 'type' => 'serial',
  573. 'not null' => true,
  574. ),
  575. 'featurepos_id' => array (
  576. 'type' => 'int',
  577. 'not null' => true,
  578. ),
  579. 'type_id' => array (
  580. 'type' => 'int',
  581. 'not null' => true,
  582. ),
  583. 'value' => array (
  584. 'type' => 'text',
  585. 'not null' => false,
  586. ),
  587. 'rank' => array (
  588. 'type' => 'int',
  589. 'not null' => true,
  590. 'default' => 0,
  591. ),
  592. ),
  593. 'primary key' => array (
  594. 0 => 'featureposprop_id',
  595. ),
  596. 'unique keys' => array (
  597. 'featureposprop_id' => array (
  598. 0 => 'featurepos_id',
  599. 1 => 'type_id',
  600. 2 => 'rank',
  601. ),
  602. ),
  603. 'indexes' => array (
  604. 'featureposprop_c1' => array (
  605. 0 => 'featurepos_id',
  606. ),
  607. 'featureposprop_idx2' => array (
  608. 0 => 'type_id',
  609. ),
  610. ),
  611. 'foreign keys' => array (
  612. 'cvterm' => array (
  613. 'table' => 'cvterm',
  614. 'columns' => array (
  615. 'type_id' => 'cvterm_id',
  616. ),
  617. ),
  618. 'featurepos' => array (
  619. 'table' => 'featurepos',
  620. 'columns' => array (
  621. 'featurepos_id' => 'featurepos_id',
  622. ),
  623. ),
  624. ),
  625. );
  626. chado_create_custom_table('featureposprop', $schema, TRUE);
  627. }
  628. /**
  629. *
  630. */
  631. function tripal_chado_add_tripal_gff_temp_table() {
  632. $schema = array(
  633. 'table' => 'tripal_gff_temp',
  634. 'fields' => array(
  635. 'feature_id' => array(
  636. 'type' => 'int',
  637. 'not null' => TRUE,
  638. ),
  639. 'organism_id' => array(
  640. 'type' => 'int',
  641. 'not null' => TRUE,
  642. ),
  643. 'uniquename' => array(
  644. 'type' => 'text',
  645. 'not null' => TRUE,
  646. ),
  647. 'type_name' => array(
  648. 'type' => 'varchar',
  649. 'length' => '1024',
  650. 'not null' => TRUE,
  651. ),
  652. ),
  653. 'indexes' => array(
  654. 'tripal_gff_temp_idx0' => array('feature_id'),
  655. 'tripal_gff_temp_idx0' => array('organism_id'),
  656. 'tripal_gff_temp_idx1' => array('uniquename'),
  657. ),
  658. 'unique keys' => array(
  659. 'tripal_gff_temp_uq0' => array('feature_id'),
  660. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  661. ),
  662. );
  663. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  664. }
  665. /**
  666. *
  667. */
  668. function tripal_chado_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  669. $schema = array(
  670. 'table' => 'tripal_gffcds_temp',
  671. 'fields' => array(
  672. 'feature_id' => array(
  673. 'type' => 'int',
  674. 'not null' => TRUE,
  675. ),
  676. 'parent_id' => array(
  677. 'type' => 'int',
  678. 'not null' => TRUE,
  679. ),
  680. 'phase' => array(
  681. 'type' => 'int',
  682. 'not null' => TRUE,
  683. ),
  684. 'strand' => array(
  685. 'type' => 'int',
  686. 'not null' => TRUE,
  687. ),
  688. 'fmin' => array(
  689. 'type' => 'int',
  690. 'not null' => TRUE,
  691. ),
  692. 'fmax' => array(
  693. 'type' => 'int',
  694. 'not null' => TRUE,
  695. ),
  696. ),
  697. 'indexes' => array(
  698. 'tripal_gff_temp_idx0' => array('feature_id'),
  699. 'tripal_gff_temp_idx0' => array('parent_id'),
  700. ),
  701. );
  702. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  703. }
  704. /**
  705. *
  706. */
  707. function tripal_chado_add_tripal_gffprotein_temp_table() {
  708. $schema = array(
  709. 'table' => 'tripal_gffprotein_temp',
  710. 'fields' => array(
  711. 'feature_id' => array(
  712. 'type' => 'int',
  713. 'not null' => TRUE,
  714. ),
  715. 'parent_id' => array(
  716. 'type' => 'int',
  717. 'not null' => TRUE,
  718. ),
  719. 'fmin' => array(
  720. 'type' => 'int',
  721. 'not null' => TRUE,
  722. ),
  723. 'fmax' => array(
  724. 'type' => 'int',
  725. 'not null' => TRUE,
  726. ),
  727. ),
  728. 'indexes' => array(
  729. 'tripal_gff_temp_idx0' => array('feature_id'),
  730. 'tripal_gff_temp_idx0' => array('parent_id'),
  731. ),
  732. 'unique keys' => array(
  733. 'tripal_gff_temp_uq0' => array('feature_id'),
  734. ),
  735. );
  736. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  737. }
  738. /**
  739. * Creates a materialized view that stores the type & number of features per organism
  740. *
  741. * @ingroup tripal_feature
  742. */
  743. function tripal_chado_add_organism_feature_count_mview() {
  744. $view_name = 'organism_feature_count';
  745. $comment = 'Stores the type and number of features per organism';
  746. $schema = array(
  747. 'description' => $comment,
  748. 'table' => $view_name,
  749. 'fields' => array(
  750. 'organism_id' => array(
  751. 'size' => 'big',
  752. 'type' => 'int',
  753. 'not null' => TRUE,
  754. ),
  755. 'genus' => array(
  756. 'type' => 'varchar',
  757. 'length' => '255',
  758. 'not null' => TRUE,
  759. ),
  760. 'species' => array(
  761. 'type' => 'varchar',
  762. 'length' => '255',
  763. 'not null' => TRUE,
  764. ),
  765. 'common_name' => array(
  766. 'type' => 'varchar',
  767. 'length' => '255',
  768. 'not null' => FALSE,
  769. ),
  770. 'num_features' => array(
  771. 'type' => 'int',
  772. 'not null' => TRUE,
  773. ),
  774. 'cvterm_id' => array(
  775. 'size' => 'big',
  776. 'type' => 'int',
  777. 'not null' => TRUE,
  778. ),
  779. 'feature_type' => array(
  780. 'type' => 'varchar',
  781. 'length' => '255',
  782. 'not null' => TRUE,
  783. ),
  784. ),
  785. 'indexes' => array(
  786. 'organism_feature_count_idx1' => array('organism_id'),
  787. 'organism_feature_count_idx2' => array('cvterm_id'),
  788. 'organism_feature_count_idx3' => array('feature_type'),
  789. ),
  790. );
  791. $sql = "
  792. SELECT
  793. O.organism_id, O.genus, O.species, O.common_name,
  794. count(F.feature_id) as num_features,
  795. CVT.cvterm_id, CVT.name as feature_type
  796. FROM organism O
  797. INNER JOIN feature F ON O.Organism_id = F.organism_id
  798. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  799. GROUP BY
  800. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  801. ";
  802. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  803. }
  804. /**
  805. * Add any custom tables needed by this module.
  806. * - Contactprop: keep track of properties of contact
  807. *
  808. * @ingroup tripal_contact
  809. */
  810. function tripal_chado_add_contactprop_table(){
  811. $schema = array (
  812. 'table' => 'contactprop',
  813. 'fields' => array (
  814. 'contactprop_id' => array (
  815. 'type' => 'serial',
  816. 'not null' => true,
  817. ),
  818. 'contact_id' => array (
  819. 'type' => 'int',
  820. 'not null' => true,
  821. ),
  822. 'type_id' => array (
  823. 'type' => 'int',
  824. 'not null' => true,
  825. ),
  826. 'value' => array (
  827. 'type' => 'text',
  828. 'not null' => false,
  829. ),
  830. 'rank' => array (
  831. 'type' => 'int',
  832. 'not null' => true,
  833. 'default' => 0,
  834. ),
  835. ),
  836. 'primary key' => array (
  837. 0 => 'contactprop_id',
  838. ),
  839. 'unique keys' => array (
  840. 'contactprop_c1' => array (
  841. 0 => 'contact_id',
  842. 1 => 'type_id',
  843. 2 => 'rank',
  844. ),
  845. ),
  846. 'indexes' => array (
  847. 'contactprop_idx1' => array (
  848. 0 => 'contact_id',
  849. ),
  850. 'contactprop_idx2' => array (
  851. 0 => 'type_id',
  852. ),
  853. ),
  854. 'foreign keys' => array (
  855. 'cvterm' => array (
  856. 'table' => 'cvterm',
  857. 'columns' => array (
  858. 'type_id' => 'cvterm_id',
  859. ),
  860. ),
  861. 'contact' => array (
  862. 'table' => 'contact',
  863. 'columns' => array (
  864. 'contact_id' => 'contact_id',
  865. ),
  866. ),
  867. ),
  868. );
  869. chado_create_custom_table('contactprop', $schema, TRUE);
  870. }
  871. /**
  872. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  873. * analysisfeature links.
  874. *
  875. * @ingroup tripal_analysis
  876. */
  877. function tripal_chado_add_analysisfeatureprop_table() {
  878. // Create analysisfeatureprop table in chado. This is needed for Chado
  879. // version 1.11, the table exists in Chado 1.2.
  880. if (!db_table_exists('chado.analysisfeatureprop')) {
  881. $sql = "
  882. CREATE TABLE {analysisfeatureprop} (
  883. analysisfeatureprop_id SERIAL PRIMARY KEY,
  884. analysisfeature_id INTEGER NOT NULL,
  885. type_id INTEGER NOT NULL,
  886. value TEXT,
  887. rank INTEGER NOT NULL,
  888. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  889. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  890. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  891. )
  892. ";
  893. chado_query($sql);
  894. }
  895. }
  896. /**
  897. * Creates a view showing the link between an organism & it's analysis through associated features.
  898. *
  899. * @ingroup tripal_analysis
  900. */
  901. function tripal_chado_add_analysis_organism_mview() {
  902. $view_name = 'analysis_organism';
  903. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  904. // this is the SQL used to identify the organism to which an analsysis
  905. // has been used. This is obtained though the analysisfeature -> feature -> organism
  906. // joins
  907. $sql = "
  908. SELECT DISTINCT A.analysis_id, O.organism_id
  909. FROM analysis A
  910. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  911. INNER JOIN feature F ON AF.feature_id = F.feature_id
  912. INNER JOIN organism O ON O.organism_id = F.organism_id
  913. ";
  914. // the schema array for describing this view
  915. $schema = array(
  916. 'table' => $view_name,
  917. 'description' => $comment,
  918. 'fields' => array(
  919. 'analysis_id' => array(
  920. 'size' => 'big',
  921. 'type' => 'int',
  922. 'not null' => TRUE,
  923. ),
  924. 'organism_id' => array(
  925. 'size' => 'big',
  926. 'type' => 'int',
  927. 'not null' => TRUE,
  928. ),
  929. ),
  930. 'indexes' => array(
  931. 'networkmod_qtl_indx0' => array('analysis_id'),
  932. 'networkmod_qtl_indx1' => array('organism_id'),
  933. ),
  934. 'foreign keys' => array(
  935. 'analysis' => array(
  936. 'table' => 'analysis',
  937. 'columns' => array(
  938. 'analysis_id' => 'analysis_id',
  939. ),
  940. ),
  941. 'organism' => array(
  942. 'table' => 'organism',
  943. 'columns' => array(
  944. 'organism_id' => 'organism_id',
  945. ),
  946. ),
  947. ),
  948. );
  949. // add the view
  950. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  951. }
  952. /**
  953. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  954. *
  955. * @ingroup tripal_cv
  956. */
  957. function tripal_chado_add_cv_root_mview_mview() {
  958. $mv_name = 'cv_root_mview';
  959. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  960. $schema = array(
  961. 'table' => $mv_name,
  962. 'description' => $comment,
  963. 'fields' => array(
  964. 'name' => array(
  965. 'type' => 'varchar',
  966. 'length' => 255,
  967. 'not null' => TRUE,
  968. ),
  969. 'cvterm_id' => array(
  970. 'size' => 'big',
  971. 'type' => 'int',
  972. 'not null' => TRUE,
  973. ),
  974. 'cv_id' => array(
  975. 'size' => 'big',
  976. 'type' => 'int',
  977. 'not null' => TRUE,
  978. ),
  979. 'cv_name' => array(
  980. 'type' => 'varchar',
  981. 'length' => 255,
  982. 'not null' => TRUE,
  983. ),
  984. ),
  985. 'indexes' => array(
  986. 'cv_root_mview_indx1' => array('cvterm_id'),
  987. 'cv_root_mview_indx2' => array('cv_id'),
  988. ),
  989. );
  990. $sql = "
  991. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  992. FROM cvterm_relationship CVTR
  993. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  994. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  995. WHERE CVTR.object_id not in
  996. (SELECT subject_id FROM cvterm_relationship)
  997. ";
  998. // Create the MView
  999. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1000. }
  1001. /**
  1002. * Creates a temporary table to store obo details while loading an obo file
  1003. *
  1004. * @ingroup tripal_cv
  1005. */
  1006. function tripal_chado_add_tripal_obo_temp_table() {
  1007. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1008. // we create it here using plain SQL because we want it to be in the chado schema but we
  1009. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1010. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1011. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1012. if (!db_table_exists('chado.tripal_obo_temp')) {
  1013. $sql = "
  1014. CREATE TABLE {tripal_obo_temp} (
  1015. id character varying(255) NOT NULL,
  1016. stanza text NOT NULL,
  1017. type character varying(50) NOT NULL,
  1018. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1019. );
  1020. ";
  1021. chado_query($sql);
  1022. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1023. chado_query($sql);
  1024. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1025. chado_query($sql);
  1026. }
  1027. }