tripal_chado.setup.inc 31 KB

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