tripal_chado.setup.inc 29 KB

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