tripal_chado.setup.inc 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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. // Add defaults to the tables that correlate OBO files/references with
  88. // a chado CV.
  89. tripal_cv_add_obo_defaults();
  90. // Create the temp table we will use for loading OBO files.
  91. tripal_cv_create_tripal_obo_temp();
  92. // Add the synonym_type vocabulary.
  93. tripal_insert_cv(
  94. 'synonym_type',
  95. 'A vocabulary used for synonym types (e.g. exact, broad, narrow, related).'
  96. );
  97. // Add the Chado ontology CV.
  98. //$obo_path = '{tripal_chado}/files/cv_property.obo';
  99. //$obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  100. //tripal_chado_load_obo_v1_2_id($obo_id);
  101. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  102. ///////////////////////////////////////////////////////////////////////////
  103. // Chado Misc
  104. ///////////////////////////////////////////////////////////////////////////
  105. tripal_insert_cv(
  106. 'synonym_type',
  107. 'A vocabulary for storing synonym types.'
  108. );
  109. // set the default vocabularies
  110. tripal_set_default_cv('synonym', 'type_id', 'synonym_type');
  111. // Add cvterms.
  112. tripal_insert_misc_cvterms();
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Chado Organism Module
  115. /////////////////////////////////////////////////////////////////////////////
  116. tripal_insert_cv(
  117. 'organism_property',
  118. 'Contains properties for organisms'
  119. );
  120. // Set the default vocabularies.
  121. tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
  122. // Add taxonomic terms.
  123. $obo_id = tripal_insert_obo('Taxonomic Rank', '{tripal_chado}/files/taxrank.obo');
  124. $cv = chado_select_record('cv', array('name'), array('name' => 'taxonomic_rank'));
  125. if (count($cv) == 0 and $obo_id) {
  126. tripal_chado_load_obo_v1_2_id($obo_id);
  127. }
  128. /////////////////////////////////////////////////////////////////////////////
  129. // Chado CompAnalysis Module
  130. /////////////////////////////////////////////////////////////////////////////
  131. // we may need the analysisfeatureprop table if it doesn't already exist
  132. tripal_analysis_create_analysisfeatureprop();
  133. // add cvterms
  134. tripal_analysis_add_cvterms();
  135. // add materialized views
  136. tripal_analysis_add_mview_analysis_organism();
  137. // set the default vocabularies
  138. tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  139. /////////////////////////////////////////////////////////////////////////////
  140. // Chado Contact Module
  141. /////////////////////////////////////////////////////////////////////////////
  142. // Add the contactprop table to Chado.
  143. tripal_contact_add_custom_tables();
  144. tripal_insert_db(array(
  145. 'name' => 'TContact',
  146. 'description' => 'The Tripal Contact controlled vocabulary.',
  147. ));
  148. tripal_insert_db(array(
  149. 'name' => 'TPUB',
  150. 'description' => 'The Tripal Publication controlled vocabulary.',
  151. ));
  152. // Add loading of the the tripal contact ontology to the job queue.
  153. $obo_path = '{tripal_chado}/files/tcontact.obo';
  154. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  155. $cv = chado_select_record('cv', array('name'), array('name' => 'tripal_contact'));
  156. if (count($cv) == 0 and $obo) {
  157. tripal_chado_load_obo_v1_2_id($obo_id);
  158. }
  159. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  160. // Add cvterms for relationship types.
  161. tripal_contact_add_cvs();
  162. // Set the default vocabularies.
  163. tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
  164. tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  165. tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  166. /////////////////////////////////////////////////////////////////////////////
  167. // Chado Feature Module
  168. /////////////////////////////////////////////////////////////////////////////
  169. // Note: the feature_property OBO that came with Chado v1.2 should not
  170. // be automatically installed. Some of the terms are duplicates of
  171. // others in better maintained vocabularies. New Tripal sites should
  172. // use those.
  173. // $obo_path = '{tripal_feature}/files/feature_property.obo';
  174. // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  175. // tripal_chado_load_obo_v1_2_id($obo_id);
  176. //// tripal_submit_obo_job(array('obo_id' => $obo_id));
  177. // Add the vocabularies used by the feature module.
  178. tripal_feature_add_cvs();
  179. // Add the materialized view.
  180. tripal_feature_add_organism_count_mview();
  181. // Add the custom tables.
  182. tripal_feature_add_tripal_gff_temp_table();
  183. tripal_feature_add_tripal_gffcds_temp_table();
  184. tripal_feature_add_tripal_gffprotein_temp_table();
  185. // Load the sequence ontology if it isn't already loaded
  186. $cv = chado_select_record('cv', array('name'), array('name' => 'sequence'));
  187. $obo = tripal_get_obo(array('obo_name' => 'Sequence Ontology'));
  188. if (count($cv) == 0 and $obo) {
  189. print "Installing the Sequence Ontology... This may take a while...\n";
  190. tripal_chado_load_obo_v1_2_id($obo->obo_id);
  191. }
  192. // Set the default vocabularies.
  193. tripal_set_default_cv('feature', 'type_id', 'sequence');
  194. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  195. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  196. /////////////////////////////////////////////////////////////////////////////
  197. // Chado Map Module
  198. /////////////////////////////////////////////////////////////////////////////
  199. // add the featuremapprop table to Chado
  200. tripal_featuremap_add_custom_tables();
  201. // Add cvterms
  202. tripal_featuremap_add_cvs();
  203. tripal_featuremap_add_cvterms();
  204. // set the default vocabularies
  205. tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
  206. tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
  207. tripal_set_default_cv('featuremap', 'unittype_id', 'featuremap_units');
  208. /////////////////////////////////////////////////////////////////////////////
  209. // Chado Library Module
  210. /////////////////////////////////////////////////////////////////////////////
  211. // add the materialized view
  212. tripal_library_add_mview_library_feature_count();
  213. // add cvterms
  214. tripal_library_add_cvs();
  215. tripal_library_add_cvterms();
  216. // set the default vocabularies
  217. tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  218. tripal_set_default_cv('library', 'type_id', 'library_type');
  219. /////////////////////////////////////////////////////////////////////////////
  220. // Chado NatDiv Module
  221. /////////////////////////////////////////////////////////////////////////////
  222. // add cvterms
  223. tripal_natural_diversity_add_cvterms();
  224. /////////////////////////////////////////////////////////////////////////////
  225. // Chado Project Module
  226. /////////////////////////////////////////////////////////////////////////////
  227. tripal_project_add_cvs();
  228. tripal_project_add_cvterms();
  229. // set the default vocabularies
  230. tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  231. tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  232. /////////////////////////////////////////////////////////////////////////////
  233. // Chado Pub Module
  234. /////////////////////////////////////////////////////////////////////////////
  235. global $base_path;
  236. // add loading of the the tripal pub ontology to the job queue
  237. $obo_path = '{tripal_chado}/files/tpub.obo';
  238. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  239. $cv = chado_select_record('cv', array('name'), array('name' => 'tripal_pub'));
  240. if (count($cv) == 0 and $obo) {
  241. tripal_chado_load_obo_v1_2_id($obo_id);
  242. }
  243. tripal_pub_add_cvs();
  244. tripal_pub_add_dbs();
  245. // add the custom tables
  246. tripal_pub_add_custom_tables();
  247. // set the default vocabularies
  248. tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  249. tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  250. tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  251. // Add the supported loaders
  252. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  253. /////////////////////////////////////////////////////////////////////////////
  254. // Chado Stock Module
  255. /////////////////////////////////////////////////////////////////////////////
  256. // add some controlled vocabularies
  257. tripal_stock_add_cvs();
  258. // set the default vocabularies
  259. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  260. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  261. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  262. // add the materialized view
  263. tripal_stock_add_organism_count_mview();
  264. /////////////////////////////////////////////////////////////////////////////
  265. // Entity Bundles
  266. /////////////////////////////////////////////////////////////////////////////
  267. // First, populate the semantic web associations for Chado tables/fields.
  268. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.semweb');
  269. tripal_chado_populate_chado_semweb_table();
  270. // Unfortunately, some Chado base tables do not have a type_id, so we must
  271. // take special action for those tables. These include: organism and
  272. // analysis. Until we can find an appropriate controlled vocabulary
  273. // that is well supported by the community with types for these tables we
  274. // will have to use in-house terms.
  275. // Add a term to be used for an inherent 'type_id' for the organism table.
  276. tripal_insert_cvterm(array(
  277. 'id' => 'local:analysis',
  278. 'name' => 'analysis',
  279. 'definition' => 'A process as a method of studying the nature of something ' .
  280. 'or of determining its essential features and their relations. ' .
  281. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  282. 'Dictionaries Ltd).',
  283. 'cv_name' => 'local',
  284. ));
  285. // TODO: change this to foaf:Project
  286. tripal_insert_cvterm(array(
  287. 'id' => 'local:project',
  288. 'name' => 'project',
  289. 'definition' => 'A plan or proposal for accomplishing something. ' .
  290. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  291. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  292. 'cv_name' => 'local',
  293. ));
  294. tripal_insert_cvterm(array(
  295. 'id' => 'local:contact',
  296. 'name' => 'contact',
  297. 'definition' => 'An entity (e.g. individual or organization) through ' .
  298. 'whom a person can gain access to information, favors, ' .
  299. 'influential people, and the like.',
  300. 'cv_name' => 'local',
  301. ));
  302. tripal_insert_cvterm(array(
  303. 'id' => 'local:relationship',
  304. 'name' => 'relationship',
  305. 'definition' => 'The way in which two things are connected.',
  306. 'cv_name' => 'local',
  307. ));
  308. // For the TripalBundle entities we will want to associate the cvterm_id,
  309. // and the chado table and field that it maps to. We will use a few
  310. // variables to do this:
  311. tripal_insert_variable(
  312. 'chado_cv_id',
  313. 'The ID of the controlled vocabulary in the chado.cv table..'
  314. );
  315. tripal_insert_variable(
  316. 'chado_cvterm_id',
  317. 'The ID of the controlled vocabulary term in the chado.cvterm table.'
  318. );
  319. tripal_insert_variable(
  320. 'chado_table',
  321. 'The name of the table to which a TripalBundle maps.'
  322. );
  323. tripal_insert_variable(
  324. 'chado_type_table',
  325. 'The table that houses the foreign key the cvterm table.'
  326. );
  327. tripal_insert_variable(
  328. 'chado_type_column',
  329. 'The name of the column within the type table that houses the cvterm.'
  330. );
  331. // We want to provide a set of commonly used entity types by default. This
  332. // way when a user first installs Tripal there are some commonly used
  333. // formats.
  334. module_load_include('inc', 'tripal', 'api/tripal.api');
  335. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  336. // Create the 'Organism' entity type. This uses the obi:organism term.
  337. $error = '';
  338. if (!tripal_create_bundle('OBI', '0100026', 'organism', $error)) {
  339. throw new Exception($error);
  340. }
  341. // Create the 'Analysis' entity type. This uses the local:analysis term.
  342. $error = '';
  343. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  344. throw new Exception($error);
  345. }
  346. // Create the 'Project' entity type. This uses the local:project term.
  347. $error = '';
  348. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  349. throw new Exception($error);
  350. }
  351. // Set a variable to indicate the site is prepared.
  352. variable_set('tripal_chado_is_prepared', TRUE);
  353. }
  354. catch (Exception $e) {
  355. throw new Exception($e);
  356. }
  357. }
  358. /**
  359. * Creates a materialized view that stores the type & number of stocks per organism
  360. *
  361. * @ingroup tripal_stock
  362. */
  363. function tripal_stock_add_organism_count_mview() {
  364. $view_name = 'organism_stock_count';
  365. $comment = 'Stores the type and number of stocks per organism';
  366. $schema = array(
  367. 'description' => $comment,
  368. 'table' => $view_name,
  369. 'fields' => array(
  370. 'organism_id' => array(
  371. 'size' => 'big',
  372. 'type' => 'int',
  373. 'not null' => TRUE,
  374. ),
  375. 'genus' => array(
  376. 'type' => 'varchar',
  377. 'length' => '255',
  378. 'not null' => TRUE,
  379. ),
  380. 'species' => array(
  381. 'type' => 'varchar',
  382. 'length' => '255',
  383. 'not null' => TRUE,
  384. ),
  385. 'common_name' => array(
  386. 'type' => 'varchar',
  387. 'length' => '255',
  388. 'not null' => FALSE,
  389. ),
  390. 'num_stocks' => array(
  391. 'type' => 'int',
  392. 'not null' => TRUE,
  393. ),
  394. 'cvterm_id' => array(
  395. 'size' => 'big',
  396. 'type' => 'int',
  397. 'not null' => TRUE,
  398. ),
  399. 'stock_type' => array(
  400. 'type' => 'varchar',
  401. 'length' => '255',
  402. 'not null' => TRUE,
  403. ),
  404. ),
  405. 'indexes' => array(
  406. 'organism_stock_count_idx1' => array('organism_id'),
  407. 'organism_stock_count_idx2' => array('cvterm_id'),
  408. 'organism_stock_count_idx3' => array('stock_type'),
  409. ),
  410. );
  411. $sql = "
  412. SELECT
  413. O.organism_id, O.genus, O.species, O.common_name,
  414. count(S.stock_id) as num_stocks,
  415. CVT.cvterm_id, CVT.name as stock_type
  416. FROM organism O
  417. INNER JOIN stock S ON O.Organism_id = S.organism_id
  418. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  419. GROUP BY
  420. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  421. ";
  422. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  423. }
  424. /**
  425. * Add cvs related to publications
  426. *
  427. * @ingroup tripal_pub
  428. */
  429. function tripal_stock_add_cvs() {
  430. // Add cv for relationship types
  431. tripal_insert_cv(
  432. 'stock_relationship',
  433. 'Contains types of relationships between stocks.'
  434. );
  435. tripal_insert_cv(
  436. 'stock_property',
  437. 'Contains properties for stocks.'
  438. );
  439. tripal_insert_cv(
  440. 'stock_type',
  441. 'Contains a list of types for stocks.'
  442. );
  443. }
  444. /**
  445. * Add custom table related to publications
  446. * - pubauthor_contact
  447. *
  448. * @ingroup tripal_pub
  449. */
  450. function tripal_pub_add_custom_tables() {
  451. $schema = array (
  452. 'table' => 'pubauthor_contact',
  453. 'fields' => array (
  454. 'pubauthor_contact_id' => array (
  455. 'type' => 'serial',
  456. 'not null' => true,
  457. ),
  458. 'contact_id' => array (
  459. 'type' => 'int',
  460. 'not null' => true,
  461. ),
  462. 'pubauthor_id' => array (
  463. 'type' => 'int',
  464. 'not null' => true,
  465. ),
  466. ),
  467. 'primary key' => array (
  468. 0 => 'pubauthor_contact_id',
  469. ),
  470. 'unique keys' => array (
  471. 'pubauthor_contact_c1' => array (
  472. 0 => 'contact_id',
  473. 1 => 'pubauthor_id',
  474. ),
  475. ),
  476. 'foreign keys' => array (
  477. 'contact' => array (
  478. 'table' => 'contact',
  479. 'columns' => array (
  480. 'contact_id' => 'contact_id',
  481. ),
  482. ),
  483. 'pubauthor' => array (
  484. 'table' => 'pubauthor',
  485. 'columns' => array (
  486. 'pubauthor_id' => 'pubauthor_id',
  487. ),
  488. ),
  489. ),
  490. );
  491. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  492. }
  493. /**
  494. * Adds dbs related to publications
  495. *
  496. * @ingroup tripal_pub
  497. */
  498. function tripal_pub_add_dbs() {
  499. // make sure we have our supported databases
  500. tripal_insert_db(
  501. array(
  502. 'name' => 'PMID',
  503. 'description' => 'PubMed',
  504. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  505. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  506. ),
  507. array('update_existing' => TRUE)
  508. );
  509. tripal_insert_db(
  510. array(
  511. 'name' => 'AGL',
  512. 'description' => 'USDA National Agricultural Library',
  513. 'url' => 'http://agricola.nal.usda.gov/'
  514. ),
  515. array('update_existing' => TRUE)
  516. );
  517. }
  518. /**
  519. * Add cvs related to publications
  520. *
  521. * @ingroup tripal_pub
  522. */
  523. function tripal_pub_add_cvs() {
  524. // Add the cv for pub properties
  525. tripal_insert_cv(
  526. 'tripal_pub',
  527. 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  528. );
  529. // Add the cv for pub types
  530. tripal_insert_cv(
  531. 'pub_type',
  532. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  533. );
  534. // Add the cv for pub properties
  535. tripal_insert_cv(
  536. 'pub_property',
  537. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  538. );
  539. // Add cv for relationship types
  540. tripal_insert_cv(
  541. 'pub_relationship',
  542. 'Contains types of relationships between publications.'
  543. );
  544. }
  545. /**
  546. * Add cvs pertaining to projects
  547. *
  548. * @ingroup tripal_project
  549. */
  550. function tripal_project_add_cvs() {
  551. // Add the cv for project properties
  552. tripal_insert_cv(
  553. 'project_property',
  554. 'Contains properties for projects'
  555. );
  556. // Add cv for relationship types
  557. tripal_insert_cv(
  558. 'project_relationship',
  559. 'Contains Types of relationships between projects.'
  560. );
  561. }
  562. /**
  563. * Add cvterms pertaining to projects
  564. *
  565. * @ingroup tripal_project
  566. */
  567. function tripal_project_add_cvterms() {
  568. // Insert cvterm 'Project Description' into cvterm table of chado
  569. // database. This CV term is used to keep track of the project
  570. // description in the projectprop table.
  571. tripal_insert_cvterm(
  572. array(
  573. 'name' => 'Project Description',
  574. 'definition' => 'Description of a project',
  575. 'cv_name' => 'project_property',
  576. 'is_relationship' => 0,
  577. 'db_name' => 'local'
  578. ),
  579. array('update_existing' => TRUE)
  580. );
  581. }
  582. /**
  583. * Add cvterms related to natural diversity
  584. *
  585. * @ingroup tripal_natural_diversity
  586. */
  587. function tripal_natural_diversity_add_cvterms(){
  588. // add cvterms for the nd_experiment_types
  589. tripal_insert_cvterm(
  590. array(
  591. 'name' => 'Genotyping',
  592. 'definition' => 'An experiment where genotypes of individuals are identified.',
  593. 'cv_name' => 'nd_experiment_types',
  594. 'is_relationship' => 0,
  595. 'db_name' => 'local'
  596. ),
  597. array('update_existing' => TRUE)
  598. );
  599. tripal_insert_cvterm(
  600. array(
  601. 'name' => 'Phenotyping',
  602. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  603. 'cv_name' => 'nd_experiment_types',
  604. 'is_relationship' => 0,
  605. 'db_name' => 'local'
  606. ),
  607. array('update_existing' => TRUE)
  608. );
  609. tripal_insert_cvterm(
  610. array(
  611. 'name' => 'Location',
  612. 'definition' => 'The name of the location.',
  613. 'cv_name' => 'nd_geolocation_property',
  614. 'is_relationship' => 0,
  615. 'db_name' => 'local'
  616. ),
  617. array('update_existing' => TRUE)
  618. );
  619. }
  620. /**
  621. * Adds a materialized view keeping track of the type of features associated with each library
  622. *
  623. * @ingroup tripal_library
  624. */
  625. function tripal_library_add_mview_library_feature_count(){
  626. $view_name = 'library_feature_count';
  627. $comment = 'Provides count of feature by type that are associated with all libraries';
  628. $schema = array(
  629. 'table' => $view_name,
  630. 'description' => $comment,
  631. 'fields' => array(
  632. 'library_id' => array(
  633. 'size' => 'big',
  634. 'type' => 'int',
  635. 'not null' => TRUE,
  636. ),
  637. 'name' => array(
  638. 'type' => 'varchar',
  639. 'length' => 255,
  640. 'not null' => TRUE,
  641. ),
  642. 'num_features' => array(
  643. 'type' => 'int',
  644. 'not null' => TRUE,
  645. ),
  646. 'feature_type' => array(
  647. 'type' => 'varchar',
  648. 'length' => 255,
  649. 'not null' => TRUE,
  650. ),
  651. ),
  652. 'indexes' => array(
  653. 'library_feature_count_idx1' => array('library_id'),
  654. ),
  655. );
  656. $sql = "
  657. SELECT
  658. L.library_id, L.name,
  659. count(F.feature_id) as num_features,
  660. CVT.name as feature_type
  661. FROM library L
  662. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  663. INNER JOIN feature F ON LF.feature_id = F.feature_id
  664. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  665. GROUP BY L.library_id, L.name, CVT.name
  666. ";
  667. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  668. }
  669. /**
  670. * Adds cvterms needed for the library module
  671. *
  672. * @ingroup tripal_library
  673. */
  674. function tripal_library_add_cvterms() {
  675. // Insert cvterm 'library_description' into cvterm table of chado
  676. // database. This CV term is used to keep track of the library
  677. // description in the libraryprop table.
  678. tripal_insert_cvterm(
  679. array(
  680. 'id' => 'local:library_description',
  681. 'name' => 'Library Description',
  682. 'definition' => 'Description of a library',
  683. 'cv_name' => 'library_property',
  684. 'is_relationship' => 0,
  685. 'db_name' => 'local'
  686. ),
  687. array('update_existing' => TRUE)
  688. );
  689. // add cvterms for the map unit types
  690. tripal_insert_cvterm(
  691. array(
  692. 'id' => 'local:cdna_library',
  693. 'name' => 'cdna_library',
  694. 'definition' => 'cDNA library',
  695. 'cv_name' => 'library_type',
  696. 'is_relationship' => 0,
  697. 'db_name' => 'local'
  698. ),
  699. array('update_existing' => TRUE)
  700. );
  701. tripal_insert_cvterm(
  702. array(
  703. 'id' => 'local:bac_library',
  704. 'name' => 'bac_library',
  705. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  706. 'cv_name' => 'library_type',
  707. 'is_relationship' => 0,
  708. 'db_name' => 'local'
  709. ),
  710. array('update_existing' => TRUE)
  711. );
  712. tripal_insert_cvterm(
  713. array(
  714. 'id' => 'local:fosmid_library',
  715. 'name' => 'fosmid_library',
  716. 'definition' => 'Fosmid library',
  717. 'cv_name' => 'library_type',
  718. 'is_relationship' => 0,
  719. 'db_name' => 'local'
  720. ),
  721. array('update_existing' => TRUE)
  722. );
  723. tripal_insert_cvterm(
  724. array(
  725. 'id' => 'local:cosmid_library',
  726. 'name' => 'cosmid_library',
  727. 'definition' => 'Cosmid library',
  728. 'cv_name' => 'library_type',
  729. 'is_relationship' => 0,
  730. 'db_name' => 'local'
  731. ),
  732. array('update_existing' => TRUE)
  733. );
  734. tripal_insert_cvterm(
  735. array(
  736. 'id' => 'local:yac_library',
  737. 'name' => 'yac_library',
  738. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  739. 'cv_name' => 'library_type',
  740. 'is_relationship' => 0,
  741. 'db_name' => 'local'
  742. ),
  743. array('update_existing' => TRUE)
  744. );
  745. tripal_insert_cvterm(
  746. array(
  747. 'id' => 'local:genomic_library',
  748. 'name' => 'genomic_library',
  749. 'definition' => 'Genomic Library',
  750. 'cv_name' => 'library_type',
  751. 'is_relationship' => 0,
  752. 'db_name' => 'local'
  753. ),
  754. array('update_existing' => TRUE)
  755. );
  756. }
  757. /**
  758. * Adds new CV's used by this module
  759. *
  760. * @ingroup tripal_library
  761. */
  762. function tripal_library_add_cvs(){
  763. tripal_insert_cv(
  764. 'library_property',
  765. 'Contains properties for libraries.'
  766. );
  767. tripal_insert_cv(
  768. 'library_type',
  769. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  770. );
  771. }
  772. /**
  773. * Add custom tables needed by the feature map module
  774. * - featuremapprop
  775. * - featuremap_dbxref
  776. * - featureposprop
  777. *
  778. * @ingroup tripal_featuremap
  779. */
  780. function tripal_featuremap_add_custom_tables(){
  781. // add the featuremaprop table to Chado
  782. $schema = array (
  783. 'table' => 'featuremapprop',
  784. 'fields' => array (
  785. 'featuremapprop_id' => array (
  786. 'type' => 'serial',
  787. 'not null' => true,
  788. ),
  789. 'featuremap_id' => array (
  790. 'type' => 'int',
  791. 'not null' => true,
  792. ),
  793. 'type_id' => array (
  794. 'type' => 'int',
  795. 'not null' => true,
  796. ),
  797. 'value' => array (
  798. 'type' => 'text',
  799. 'not null' => false,
  800. ),
  801. 'rank' => array (
  802. 'type' => 'int',
  803. 'not null' => true,
  804. 'default' => 0,
  805. ),
  806. ),
  807. 'primary key' => array (
  808. 0 => 'featuremapprop_id',
  809. ),
  810. 'unique keys' => array (
  811. 'featuremapprop_c1' => array (
  812. 0 => 'featuremap_id',
  813. 1 => 'type_id',
  814. 2 => 'rank',
  815. ),
  816. ),
  817. 'indexes' => array (
  818. 'featuremapprop_idx1' => array (
  819. 0 => 'featuremap_id',
  820. ),
  821. 'featuremapprop_idx2' => array (
  822. 0 => 'type_id',
  823. ),
  824. ),
  825. 'foreign keys' => array (
  826. 'cvterm' => array (
  827. 'table' => 'cvterm',
  828. 'columns' => array (
  829. 'type_id' => 'cvterm_id',
  830. ),
  831. ),
  832. 'featuremap' => array (
  833. 'table' => 'featuremap',
  834. 'columns' => array (
  835. 'featuremap_id' => 'featuremap_id',
  836. ),
  837. ),
  838. ),
  839. );
  840. chado_create_custom_table('featuremapprop', $schema, TRUE);
  841. // add the featuremap_dbxref table to Chado
  842. $schema = array (
  843. 'table' => 'featuremap_dbxref',
  844. 'fields' => array (
  845. 'featuremap_dbxref_id' => array (
  846. 'type' => 'serial',
  847. 'not null' => true,
  848. ),
  849. 'featuremap_id' => array (
  850. 'type' => 'int',
  851. 'not null' => true,
  852. ),
  853. 'dbxref_id' => array (
  854. 'type' => 'int',
  855. 'not null' => true,
  856. ),
  857. ),
  858. 'primary key' => array (
  859. 0 => 'featuremap_dbxref_id',
  860. ),
  861. 'unique keys' => array (
  862. 'featuremap_dbxref_c1' => array (
  863. 0 => 'featuremap_id',
  864. 1 => 'dbxref_id',
  865. ),
  866. ),
  867. 'indexes' => array (
  868. 'featuremap_dbxref_idx1' => array (
  869. 0 => 'featuremap_dbxref_id',
  870. ),
  871. 'featuremap_dbxref_idx2' => array (
  872. 0 => 'dbxref_id',
  873. ),
  874. ),
  875. 'foreign keys' => array (
  876. 'dbxref' => array (
  877. 'table' => 'dbxref',
  878. 'columns' => array (
  879. 'dbxref_id' => 'dbxref_id',
  880. ),
  881. ),
  882. 'featuremap' => array (
  883. 'table' => 'featuremap',
  884. 'columns' => array (
  885. 'featuremap_id' => 'featuremap_id',
  886. ),
  887. ),
  888. ),
  889. 'referring_tables' => NULL,
  890. );
  891. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  892. $schema = array (
  893. 'table' => 'featureposprop',
  894. 'fields' => array (
  895. 'featureposprop_id' => array (
  896. 'type' => 'serial',
  897. 'not null' => true,
  898. ),
  899. 'featurepos_id' => array (
  900. 'type' => 'int',
  901. 'not null' => true,
  902. ),
  903. 'type_id' => array (
  904. 'type' => 'int',
  905. 'not null' => true,
  906. ),
  907. 'value' => array (
  908. 'type' => 'text',
  909. 'not null' => false,
  910. ),
  911. 'rank' => array (
  912. 'type' => 'int',
  913. 'not null' => true,
  914. 'default' => 0,
  915. ),
  916. ),
  917. 'primary key' => array (
  918. 0 => 'featureposprop_id',
  919. ),
  920. 'unique keys' => array (
  921. 'featureposprop_id' => array (
  922. 0 => 'featurepos_id',
  923. 1 => 'type_id',
  924. 2 => 'rank',
  925. ),
  926. ),
  927. 'indexes' => array (
  928. 'featureposprop_c1' => array (
  929. 0 => 'featurepos_id',
  930. ),
  931. 'featureposprop_idx2' => array (
  932. 0 => 'type_id',
  933. ),
  934. ),
  935. 'foreign keys' => array (
  936. 'cvterm' => array (
  937. 'table' => 'cvterm',
  938. 'columns' => array (
  939. 'type_id' => 'cvterm_id',
  940. ),
  941. ),
  942. 'featurepos' => array (
  943. 'table' => 'featurepos',
  944. 'columns' => array (
  945. 'featurepos_id' => 'featurepos_id',
  946. ),
  947. ),
  948. ),
  949. );
  950. chado_create_custom_table('featureposprop', $schema, TRUE);
  951. }
  952. /**
  953. * Add cv terms needed by the featuremap module
  954. *
  955. * @ingroup tripal_featuremap
  956. */
  957. function tripal_featuremap_add_cvterms() {
  958. // add cvterms for the map unit types
  959. tripal_insert_cvterm(
  960. array(
  961. 'name' => 'cM',
  962. 'definition' => 'Centimorgan units',
  963. 'cv_name' => 'featuremap_units',
  964. 'is_relationship' => 0,
  965. 'db_name' => 'local'
  966. ),
  967. array('update_existing' => TRUE)
  968. );
  969. tripal_insert_cvterm(
  970. array(
  971. 'name' => 'bp',
  972. 'definition' => 'Base pairs units',
  973. 'cv_name' => 'featuremap_units',
  974. 'is_relationship' => 0,
  975. 'db_name' => 'local'
  976. ),
  977. array('update_existing' => TRUE)
  978. );
  979. tripal_insert_cvterm(
  980. array(
  981. 'name' => 'bin_unit',
  982. 'definition' => 'The bin unit',
  983. 'cv_name' => 'featuremap_units',
  984. 'is_relationship' => 0,
  985. 'db_name' => 'local'
  986. ),
  987. array('update_existing' => TRUE)
  988. );
  989. tripal_insert_cvterm(
  990. array(
  991. 'name' => 'marker_order',
  992. 'definition' => 'Units simply to define marker order.',
  993. 'cv_name' => 'featuremap_units',
  994. 'is_relationship' => 0,
  995. 'db_name' => 'local'
  996. ),
  997. array('update_existing' => TRUE)
  998. );
  999. tripal_insert_cvterm(
  1000. array(
  1001. 'name' => 'undefined',
  1002. 'definition' => 'A catch-all for an undefined unit type',
  1003. 'cv_name' => 'featuremap_units',
  1004. 'is_relationship' => 0,
  1005. 'db_name' => 'local'
  1006. ),
  1007. array('update_existing' => TRUE)
  1008. );
  1009. // featurepos properties
  1010. tripal_insert_cvterm(
  1011. array(
  1012. 'name' => 'start',
  1013. 'definition' => 'The start coordinate for a map feature.',
  1014. 'cv_name' => 'featurepos_property',
  1015. 'is_relationship' => 0,
  1016. 'db_name' => 'local'
  1017. ),
  1018. array('update_existing' => TRUE)
  1019. );
  1020. tripal_insert_cvterm(
  1021. array(
  1022. 'name' => 'stop',
  1023. 'definition' => 'The end coordinate for a map feature',
  1024. 'cv_name' => 'featurepos_property',
  1025. 'is_relationship' => 0,
  1026. 'db_name' => 'local'
  1027. ),
  1028. array('update_existing' => TRUE)
  1029. );
  1030. // add cvterms for map properties
  1031. tripal_insert_cvterm(
  1032. array(
  1033. 'name' => 'Map Dbxref',
  1034. 'definition' => 'A unique identifer for the map in a remote database. The '
  1035. . 'format is a database abbreviation and a unique accession separated '
  1036. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1037. 'cv_name' => 'featuremap_property',
  1038. 'is_relationship' => 0,
  1039. 'db_name' => 'local'
  1040. ),
  1041. array('update_existing' => TRUE)
  1042. );
  1043. tripal_insert_cvterm(
  1044. array(
  1045. 'name' => 'Map Type',
  1046. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1047. 'cv_name' => 'featuremap_property',
  1048. 'is_relationship' => 0,
  1049. 'db_name' => 'local'
  1050. ),
  1051. array('update_existing' => TRUE)
  1052. );
  1053. tripal_insert_cvterm(
  1054. array(
  1055. 'name' => 'Genome Group',
  1056. 'definition' => '',
  1057. 'cv_name' => 'featuremap_property',
  1058. 'is_relationship' => 0,
  1059. 'db_name' => 'local'
  1060. ),
  1061. array('update_existing' => TRUE)
  1062. );
  1063. tripal_insert_cvterm(
  1064. array(
  1065. 'name' => 'URL',
  1066. 'definition' => 'A univeral resource locator (URL) reference where the '
  1067. . 'publication can be found. For maps found online, this would be '
  1068. . 'the web address for the map.',
  1069. 'cv_name' => 'featuremap_property',
  1070. 'is_relationship' => 0,
  1071. 'db_name' => 'local'
  1072. ),
  1073. array('update_existing' => TRUE)
  1074. );
  1075. tripal_insert_cvterm(
  1076. array(
  1077. 'name' => 'Population Type',
  1078. 'definition' => 'A brief description of the population type used to generate '
  1079. . 'the map (e.g. RIL, F2, BC1, etc).',
  1080. 'cv_name' => 'featuremap_property',
  1081. 'is_relationship' => 0,
  1082. 'db_name' => 'local'
  1083. ),
  1084. array('update_existing' => TRUE)
  1085. );
  1086. tripal_insert_cvterm(
  1087. array(
  1088. 'name' => 'Population Size',
  1089. 'definition' => 'The size of the population used to construct the map.',
  1090. 'cv_name' => 'featuremap_property',
  1091. 'is_relationship' => 0,
  1092. 'db_name' => 'local'
  1093. ),
  1094. array('update_existing' => TRUE)
  1095. );
  1096. tripal_insert_cvterm(
  1097. array(
  1098. 'name' => 'Methods',
  1099. 'definition' => 'A brief description of the methods used to construct the map.',
  1100. 'cv_name' => 'featuremap_property',
  1101. 'is_relationship' => 0,
  1102. 'db_name' => 'local'
  1103. ),
  1104. array('update_existing' => TRUE)
  1105. );
  1106. tripal_insert_cvterm(
  1107. array(
  1108. 'name' => 'Software',
  1109. 'definition' => 'The software used to construct the map.',
  1110. 'cv_name' => 'featuremap_property',
  1111. 'is_relationship' => 0,
  1112. 'db_name' => 'local'
  1113. ),
  1114. array('update_existing' => TRUE)
  1115. );
  1116. }
  1117. /**
  1118. * Add cvs needed by the featuremap module
  1119. *
  1120. * @ingroup tripal_featuremap
  1121. */
  1122. function tripal_featuremap_add_cvs() {
  1123. tripal_insert_cv(
  1124. 'featuremap_units',
  1125. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1126. );
  1127. tripal_insert_cv(
  1128. 'featurepos_property',
  1129. 'Contains terms map properties.'
  1130. );
  1131. tripal_insert_cv(
  1132. 'featuremap_property',
  1133. 'Contains positional types for the feature positions'
  1134. );
  1135. }
  1136. /**
  1137. * Add cvs related to features
  1138. *
  1139. * @ingroup tripal_pub
  1140. */
  1141. function tripal_feature_add_cvs() {
  1142. // Add cv for relationship types
  1143. tripal_insert_cv(
  1144. 'feature_relationship',
  1145. 'Contains types of relationships between features.'
  1146. );
  1147. // The feature_property CV may already exists. It comes with Chado, but
  1148. // we need to add it just in case it doesn't get added before the feature
  1149. // module is installed. But as of Tripal v3.0 the Chado version of this
  1150. // vocabulary is no longer loaded by default.
  1151. tripal_insert_cv(
  1152. 'feature_property',
  1153. 'Stores properties about features'
  1154. );
  1155. // the feature type vocabulary should be the sequence ontology, and even though
  1156. // this ontology should get loaded we will create it here just so that we can
  1157. // set the default vocabulary for the feature.type_id field
  1158. tripal_insert_cv(
  1159. 'sequence',
  1160. 'The Sequence Ontology'
  1161. );
  1162. }
  1163. /**
  1164. *
  1165. */
  1166. function tripal_feature_add_tripal_gff_temp_table() {
  1167. $schema = array(
  1168. 'table' => 'tripal_gff_temp',
  1169. 'fields' => array(
  1170. 'feature_id' => array(
  1171. 'type' => 'int',
  1172. 'not null' => TRUE,
  1173. ),
  1174. 'organism_id' => array(
  1175. 'type' => 'int',
  1176. 'not null' => TRUE,
  1177. ),
  1178. 'uniquename' => array(
  1179. 'type' => 'text',
  1180. 'not null' => TRUE,
  1181. ),
  1182. 'type_name' => array(
  1183. 'type' => 'varchar',
  1184. 'length' => '1024',
  1185. 'not null' => TRUE,
  1186. ),
  1187. ),
  1188. 'indexes' => array(
  1189. 'tripal_gff_temp_idx0' => array('feature_id'),
  1190. 'tripal_gff_temp_idx0' => array('organism_id'),
  1191. 'tripal_gff_temp_idx1' => array('uniquename'),
  1192. ),
  1193. 'unique keys' => array(
  1194. 'tripal_gff_temp_uq0' => array('feature_id'),
  1195. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1196. ),
  1197. );
  1198. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1199. }
  1200. /**
  1201. *
  1202. */
  1203. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1204. $schema = array(
  1205. 'table' => 'tripal_gffcds_temp',
  1206. 'fields' => array(
  1207. 'feature_id' => array(
  1208. 'type' => 'int',
  1209. 'not null' => TRUE,
  1210. ),
  1211. 'parent_id' => array(
  1212. 'type' => 'int',
  1213. 'not null' => TRUE,
  1214. ),
  1215. 'phase' => array(
  1216. 'type' => 'int',
  1217. 'not null' => TRUE,
  1218. ),
  1219. 'strand' => array(
  1220. 'type' => 'int',
  1221. 'not null' => TRUE,
  1222. ),
  1223. 'fmin' => array(
  1224. 'type' => 'int',
  1225. 'not null' => TRUE,
  1226. ),
  1227. 'fmax' => array(
  1228. 'type' => 'int',
  1229. 'not null' => TRUE,
  1230. ),
  1231. ),
  1232. 'indexes' => array(
  1233. 'tripal_gff_temp_idx0' => array('feature_id'),
  1234. 'tripal_gff_temp_idx0' => array('parent_id'),
  1235. ),
  1236. );
  1237. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1238. }
  1239. /**
  1240. *
  1241. */
  1242. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1243. $schema = array(
  1244. 'table' => 'tripal_gffprotein_temp',
  1245. 'fields' => array(
  1246. 'feature_id' => array(
  1247. 'type' => 'int',
  1248. 'not null' => TRUE,
  1249. ),
  1250. 'parent_id' => array(
  1251. 'type' => 'int',
  1252. 'not null' => TRUE,
  1253. ),
  1254. 'fmin' => array(
  1255. 'type' => 'int',
  1256. 'not null' => TRUE,
  1257. ),
  1258. 'fmax' => array(
  1259. 'type' => 'int',
  1260. 'not null' => TRUE,
  1261. ),
  1262. ),
  1263. 'indexes' => array(
  1264. 'tripal_gff_temp_idx0' => array('feature_id'),
  1265. 'tripal_gff_temp_idx0' => array('parent_id'),
  1266. ),
  1267. 'unique keys' => array(
  1268. 'tripal_gff_temp_uq0' => array('feature_id'),
  1269. ),
  1270. );
  1271. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1272. }
  1273. /**
  1274. * Creates a materialized view that stores the type & number of features per organism
  1275. *
  1276. * @ingroup tripal_feature
  1277. */
  1278. function tripal_feature_add_organism_count_mview() {
  1279. $view_name = 'organism_feature_count';
  1280. $comment = 'Stores the type and number of features per organism';
  1281. $schema = array(
  1282. 'description' => $comment,
  1283. 'table' => $view_name,
  1284. 'fields' => array(
  1285. 'organism_id' => array(
  1286. 'size' => 'big',
  1287. 'type' => 'int',
  1288. 'not null' => TRUE,
  1289. ),
  1290. 'genus' => array(
  1291. 'type' => 'varchar',
  1292. 'length' => '255',
  1293. 'not null' => TRUE,
  1294. ),
  1295. 'species' => array(
  1296. 'type' => 'varchar',
  1297. 'length' => '255',
  1298. 'not null' => TRUE,
  1299. ),
  1300. 'common_name' => array(
  1301. 'type' => 'varchar',
  1302. 'length' => '255',
  1303. 'not null' => FALSE,
  1304. ),
  1305. 'num_features' => array(
  1306. 'type' => 'int',
  1307. 'not null' => TRUE,
  1308. ),
  1309. 'cvterm_id' => array(
  1310. 'size' => 'big',
  1311. 'type' => 'int',
  1312. 'not null' => TRUE,
  1313. ),
  1314. 'feature_type' => array(
  1315. 'type' => 'varchar',
  1316. 'length' => '255',
  1317. 'not null' => TRUE,
  1318. ),
  1319. ),
  1320. 'indexes' => array(
  1321. 'organism_feature_count_idx1' => array('organism_id'),
  1322. 'organism_feature_count_idx2' => array('cvterm_id'),
  1323. 'organism_feature_count_idx3' => array('feature_type'),
  1324. ),
  1325. );
  1326. $sql = "
  1327. SELECT
  1328. O.organism_id, O.genus, O.species, O.common_name,
  1329. count(F.feature_id) as num_features,
  1330. CVT.cvterm_id, CVT.name as feature_type
  1331. FROM organism O
  1332. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1333. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1334. GROUP BY
  1335. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1336. ";
  1337. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1338. }
  1339. /**
  1340. * Adds any cvs needed by this module.
  1341. *
  1342. * @ingroup tripal_contact
  1343. */
  1344. function tripal_contact_add_cvs() {
  1345. // Add the cv for contact properties. This is a default vocabulary in the event
  1346. // that a user does not want to use the tripal_contact vocabulary
  1347. tripal_insert_cv(
  1348. 'contact_property',
  1349. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1350. );
  1351. // add the cv for the contact type. This is a default vocabulary in the event
  1352. // that a user does not want to use the tripal_contact vocabulary
  1353. tripal_insert_cv(
  1354. 'contact_type',
  1355. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1356. );
  1357. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1358. tripal_insert_cv(
  1359. 'tripal_contact',
  1360. 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
  1361. );
  1362. // add the cv for contact relationships
  1363. tripal_insert_cv(
  1364. 'contact_relationship',
  1365. 'Contains types of relationships between contacts.'
  1366. );
  1367. }
  1368. /**
  1369. * Add any custom tables needed by this module.
  1370. * - Contactprop: keep track of properties of contact
  1371. *
  1372. * @ingroup tripal_contact
  1373. */
  1374. function tripal_contact_add_custom_tables(){
  1375. $schema = array (
  1376. 'table' => 'contactprop',
  1377. 'fields' => array (
  1378. 'contactprop_id' => array (
  1379. 'type' => 'serial',
  1380. 'not null' => true,
  1381. ),
  1382. 'contact_id' => array (
  1383. 'type' => 'int',
  1384. 'not null' => true,
  1385. ),
  1386. 'type_id' => array (
  1387. 'type' => 'int',
  1388. 'not null' => true,
  1389. ),
  1390. 'value' => array (
  1391. 'type' => 'text',
  1392. 'not null' => false,
  1393. ),
  1394. 'rank' => array (
  1395. 'type' => 'int',
  1396. 'not null' => true,
  1397. 'default' => 0,
  1398. ),
  1399. ),
  1400. 'primary key' => array (
  1401. 0 => 'contactprop_id',
  1402. ),
  1403. 'unique keys' => array (
  1404. 'contactprop_c1' => array (
  1405. 0 => 'contact_id',
  1406. 1 => 'type_id',
  1407. 2 => 'rank',
  1408. ),
  1409. ),
  1410. 'indexes' => array (
  1411. 'contactprop_idx1' => array (
  1412. 0 => 'contact_id',
  1413. ),
  1414. 'contactprop_idx2' => array (
  1415. 0 => 'type_id',
  1416. ),
  1417. ),
  1418. 'foreign keys' => array (
  1419. 'cvterm' => array (
  1420. 'table' => 'cvterm',
  1421. 'columns' => array (
  1422. 'type_id' => 'cvterm_id',
  1423. ),
  1424. ),
  1425. 'contact' => array (
  1426. 'table' => 'contact',
  1427. 'columns' => array (
  1428. 'contact_id' => 'contact_id',
  1429. ),
  1430. ),
  1431. ),
  1432. );
  1433. chado_create_custom_table('contactprop', $schema, TRUE);
  1434. }
  1435. /**
  1436. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1437. * analysisfeature links.
  1438. *
  1439. * @ingroup tripal_analysis
  1440. */
  1441. function tripal_analysis_create_analysisfeatureprop() {
  1442. // Create analysisfeatureprop table in chado. This is needed for Chado
  1443. // version 1.11, the table exists in Chado 1.2.
  1444. if (!db_table_exists('chado.analysisfeatureprop')) {
  1445. $sql = "
  1446. CREATE TABLE {analysisfeatureprop} (
  1447. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1448. analysisfeature_id INTEGER NOT NULL,
  1449. type_id INTEGER NOT NULL,
  1450. value TEXT,
  1451. rank INTEGER NOT NULL,
  1452. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1453. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1454. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1455. )
  1456. ";
  1457. chado_query($sql);
  1458. }
  1459. }
  1460. /**
  1461. * Creates a view showing the link between an organism & it's analysis through associated features.
  1462. *
  1463. * @ingroup tripal_analysis
  1464. */
  1465. function tripal_analysis_add_mview_analysis_organism() {
  1466. $view_name = 'analysis_organism';
  1467. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1468. // this is the SQL used to identify the organism to which an analsysis
  1469. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1470. // joins
  1471. $sql = "
  1472. SELECT DISTINCT A.analysis_id, O.organism_id
  1473. FROM analysis A
  1474. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1475. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1476. INNER JOIN organism O ON O.organism_id = F.organism_id
  1477. ";
  1478. // the schema array for describing this view
  1479. $schema = array(
  1480. 'table' => $view_name,
  1481. 'description' => $comment,
  1482. 'fields' => array(
  1483. 'analysis_id' => array(
  1484. 'size' => 'big',
  1485. 'type' => 'int',
  1486. 'not null' => TRUE,
  1487. ),
  1488. 'organism_id' => array(
  1489. 'size' => 'big',
  1490. 'type' => 'int',
  1491. 'not null' => TRUE,
  1492. ),
  1493. ),
  1494. 'indexes' => array(
  1495. 'networkmod_qtl_indx0' => array('analysis_id'),
  1496. 'networkmod_qtl_indx1' => array('organism_id'),
  1497. ),
  1498. 'foreign keys' => array(
  1499. 'analysis' => array(
  1500. 'table' => 'analysis',
  1501. 'columns' => array(
  1502. 'analysis_id' => 'analysis_id',
  1503. ),
  1504. ),
  1505. 'organism' => array(
  1506. 'table' => 'organism',
  1507. 'columns' => array(
  1508. 'organism_id' => 'organism_id',
  1509. ),
  1510. ),
  1511. ),
  1512. );
  1513. // add the view
  1514. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1515. }
  1516. /**
  1517. * Adds controlled vocabulary terms needed by this module.
  1518. *
  1519. * @ingroup tripal_analysis
  1520. */
  1521. function tripal_analysis_add_cvterms() {
  1522. tripal_insert_cv(
  1523. 'tripal_analysis',
  1524. 'Terms used for managing analyses in Tripal'
  1525. );
  1526. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1527. // get rid of it in case it is used, so just keep it in the Tripal CV
  1528. tripal_insert_cvterm(
  1529. array(
  1530. 'name' => 'analysis_date',
  1531. 'definition' => 'The date that an analysis was performed.',
  1532. 'cv_name' => 'tripal_analysis',
  1533. 'is_relationship' => 0,
  1534. 'db_name' => 'local'
  1535. ),
  1536. array('update_existing' => TRUE)
  1537. );
  1538. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1539. // get rid of it in case it is used, so just keep it in the Tripal CV
  1540. tripal_insert_cvterm(
  1541. array(
  1542. 'name' => 'analysis_short_name',
  1543. 'definition' => 'A computer legible (no spaces or special characters) '
  1544. . 'abbreviation for the analysis.',
  1545. 'cv_name' => 'tripal_analysis',
  1546. 'is_relationship' => 0,
  1547. 'db_name' => 'local'
  1548. ),
  1549. array('update_existing' => TRUE)
  1550. );
  1551. // the 'analysis_property' vocabulary is for user definable properties wo we
  1552. // will add an 'Analysis Type' to this vocubulary
  1553. tripal_insert_cvterm(
  1554. array(
  1555. 'name' => 'Analysis Type',
  1556. 'definition' => 'The type of analysis that was performed.',
  1557. 'cv_name' => 'analysis_property',
  1558. 'is_relationship' => 0,
  1559. 'db_name' => 'local'
  1560. ),
  1561. array('update_existing' => TRUE)
  1562. );
  1563. }
  1564. /**
  1565. * Add's defaults to the tripal_cv_obo table
  1566. *
  1567. * @ingroup tripal_cv
  1568. */
  1569. function tripal_cv_add_obo_defaults() {
  1570. // Insert commonly used ontologies into the tables.
  1571. $ontologies = array(
  1572. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  1573. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  1574. array('Sequence Ontology', 'https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/so-xp-simple.obo'),
  1575. array('Gene Ontology', 'http://geneontology.org/ontology/go.obo'),
  1576. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  1577. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  1578. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  1579. );
  1580. foreach ($ontologies as $o) {
  1581. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  1582. }
  1583. }
  1584. /**
  1585. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1586. *
  1587. * @ingroup tripal_cv
  1588. */
  1589. function tripal_cv_add_cv_root_mview() {
  1590. $mv_name = 'cv_root_mview';
  1591. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1592. $schema = array(
  1593. 'table' => $mv_name,
  1594. 'description' => $comment,
  1595. 'fields' => array(
  1596. 'name' => array(
  1597. 'type' => 'varchar',
  1598. 'length' => 255,
  1599. 'not null' => TRUE,
  1600. ),
  1601. 'cvterm_id' => array(
  1602. 'size' => 'big',
  1603. 'type' => 'int',
  1604. 'not null' => TRUE,
  1605. ),
  1606. 'cv_id' => array(
  1607. 'size' => 'big',
  1608. 'type' => 'int',
  1609. 'not null' => TRUE,
  1610. ),
  1611. 'cv_name' => array(
  1612. 'type' => 'varchar',
  1613. 'length' => 255,
  1614. 'not null' => TRUE,
  1615. ),
  1616. ),
  1617. 'indexes' => array(
  1618. 'cv_root_mview_indx1' => array('cvterm_id'),
  1619. 'cv_root_mview_indx2' => array('cv_id'),
  1620. ),
  1621. );
  1622. $sql = "
  1623. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1624. FROM cvterm_relationship CVTR
  1625. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1626. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1627. WHERE CVTR.object_id not in
  1628. (SELECT subject_id FROM cvterm_relationship)
  1629. ";
  1630. // Create the MView
  1631. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1632. }
  1633. /**
  1634. * Creates a temporary table to store obo details while loading an obo file
  1635. *
  1636. * @ingroup tripal_cv
  1637. */
  1638. function tripal_cv_create_tripal_obo_temp() {
  1639. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1640. // we create it here using plain SQL because we want it to be in the chado schema but we
  1641. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1642. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1643. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1644. if (!db_table_exists('chado.tripal_obo_temp')) {
  1645. $sql = "
  1646. CREATE TABLE {tripal_obo_temp} (
  1647. id character varying(255) NOT NULL,
  1648. stanza text NOT NULL,
  1649. type character varying(50) NOT NULL,
  1650. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1651. );
  1652. ";
  1653. chado_query($sql);
  1654. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1655. chado_query($sql);
  1656. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1657. chado_query($sql);
  1658. }
  1659. }
  1660. /**
  1661. * Adds generic CVterms.
  1662. */
  1663. function tripal_insert_misc_cvterms() {
  1664. tripal_insert_cvterm(
  1665. array(
  1666. 'name' => 'exact',
  1667. 'id' => "local:exact",
  1668. 'definition' => 'An exact equivalent; interchangeable with the term name.',
  1669. 'is_obsolete' => 0,
  1670. 'cv_name' => 'synonym_type',
  1671. 'is_relationship' => FALSE
  1672. ),
  1673. array('update_existing' => TRUE)
  1674. );
  1675. tripal_insert_cvterm(
  1676. array(
  1677. 'name' => 'broad',
  1678. 'id' => "local:broad",
  1679. 'definition' => 'The synonym is broader than the name.',
  1680. 'is_obsolete' => 0,
  1681. 'cv_name' => 'synonym_type',
  1682. 'is_relationship' => FALSE
  1683. ),
  1684. array('update_existing' => TRUE)
  1685. );
  1686. tripal_insert_cvterm(
  1687. array(
  1688. 'name' => 'narrow',
  1689. 'id' => "local:narrow",
  1690. 'definition' => 'The synonym is narrower or more precise than the name.',
  1691. 'is_obsolete' => 0,
  1692. 'cv_name' => 'synonym_type',
  1693. 'is_relationship' => FALSE
  1694. ),
  1695. array('update_existing' => TRUE)
  1696. );
  1697. tripal_insert_cvterm(
  1698. array(
  1699. 'name' => 'related',
  1700. 'id' => "local:related",
  1701. 'definition' => 'The synonym is related in some way.',
  1702. 'is_obsolete' => 0,
  1703. 'cv_name' => 'synonym_type',
  1704. 'is_relationship' => FALSE
  1705. ),
  1706. array('update_existing' => TRUE)
  1707. );
  1708. }