tripal_chado.setup.inc 34 KB

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