tripal_chado.setup.inc 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  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. // we want to force the version of Chado to be set properly
  12. $real_version = chado_get_version(TRUE);
  13. $form['current_version'] = array(
  14. '#type' => 'item',
  15. '#title' => t("Current installed version of Chado:"),
  16. '#description' => $real_version,
  17. );
  18. $form['instructions'] = array(
  19. '#type' => 'item',
  20. '#description' => t("Click the button below to prepare a Chado installation for
  21. use by Tripal. If you used Tripal to install Chado then this step
  22. is not necessary. If Chado was installed outside of Tripal then
  23. it should be prepared. Preparing Chado involves adding of
  24. specific controlled vocabularies, custom tables and materialized views."),
  25. );
  26. $form['button'] = array(
  27. '#type' => 'submit',
  28. '#value' => t('Prepare Chado'),
  29. '#name' => 'prepare-chado',
  30. );
  31. return $form;
  32. }
  33. /**
  34. * Submit function for the tripal_chado_prepare_form().
  35. *
  36. * @param $form
  37. * @param $form_state
  38. */
  39. function tripal_chado_prepare_form_submit($form, $form_state) {
  40. if ($form_state['clicked_button']['#name'] == "prepare-chado") {
  41. global $user;
  42. $args = array();
  43. tripal_add_job('Prepare Chado', 'tripal_chado', 'tripal_chado_prepare_chado', $args, $user->uid);
  44. }
  45. }
  46. /**
  47. * Load Chado Schema Form
  48. *
  49. * @ingroup tripal_chado
  50. */
  51. function tripal_chado_load_form($form, $form_state) {
  52. // we want to force the version of Chado to be set properly
  53. $real_version = chado_get_version(TRUE);
  54. // get the effective version. Pass true as second argument
  55. // to warn the user if the current version is not compatible
  56. $version = chado_get_version(FALSE, TRUE);
  57. $form['current_version'] = array(
  58. '#type' => 'item',
  59. '#title' => t("Current installed version of Chado:"),
  60. '#description' => $real_version,
  61. );
  62. $form['action_to_do'] = array(
  63. '#type' => 'radios',
  64. '#title' => 'Installation/Upgrade Action',
  65. '#options' => array(
  66. 'Install Chado v1.2' => t('New Install of Chado v1.2 (erases all existing Chado data if Chado already exists)'),
  67. 'Upgrade Chado v1.11 to v1.2' => t('Upgrade existing Chado v1.11 to v1.2 (no data is lost)'),
  68. 'Install Chado v1.11' => t('New Install of Chado v1.11 (erases all existing Chado data if Chado already exists)')
  69. ),
  70. '#description' => t('Select an action to perform. If you want to install Chado all other Tripal modules must not be installed.'),
  71. '#required' => TRUE,
  72. );
  73. $form['warning'] = array(
  74. '#markup' => "<div><font color=\"red\">WARNING:</font>" . t('A new install of
  75. Chado will remove and recreate the Chado database if it already exists.') . '</div>',
  76. );
  77. $form['button'] = array(
  78. '#type' => 'submit',
  79. '#value' => t('Install/Upgrade Chado'),
  80. );
  81. return $form;
  82. }
  83. function tripal_chado_chado_load_form_validate($form, &$form_state) {
  84. // We do not want to allow re-installation of Chado if other
  85. // Tripal modules are installed. This is because the install files
  86. // of those modules may add content to Chado and reinstalling Chado
  87. // removes that content which may break the modules.
  88. if ($form_state['values']['action_to_do'] == "Install Chado v1.2" or
  89. $form_state['values']['action_to_do'] == "Install Chado v1.11") {
  90. $modules = system_get_info('module');
  91. $list = array();
  92. foreach ($modules as $mname => $module) {
  93. if (array_key_exists('dependencies', $module) and in_array('tripal_chado', $module['dependencies'])) {
  94. $list[] = $module['name'] . " ($mname)";
  95. }
  96. }
  97. if (count($list) > 0) {
  98. form_set_error("action_to_do", "Chado cannot be installed while other Tripal modules
  99. are enabled. You must fully uninstall the following modules if you
  100. would like to install or re-install chado.<br>" .
  101. implode("<br>", $list));
  102. }
  103. }
  104. if ($form_state['values']['action_to_do'] == "Upgrade Chado v1.11 to v1.2") {
  105. // Make sure we are already not at v1.2
  106. $real_version = chado_get_version(TRUE);
  107. if ($real_version == "1.2") {
  108. form_set_error("action_to_do", "You are already at v1.2. There is no need to upgrade.");
  109. }
  110. }
  111. }
  112. /**
  113. * Submit Load Chado Schema Form
  114. *
  115. * @ingroup tripal_chado
  116. */
  117. function tripal_chado_chado_load_form_submit($form, &$form_state) {
  118. global $user;
  119. $action_to_do = trim($form_state['values']['action_to_do']);
  120. $args = array($action_to_do);
  121. tripal_add_job($action_to_do, 'tripal_chado', 'tripal_chado_install_chado', $args, $user->uid);
  122. }
  123. /**
  124. * Install Chado Schema
  125. *
  126. * @ingroup tripal_chado
  127. */
  128. function tripal_chado_install_chado($action) {
  129. $vsql = "
  130. INSERT INTO {chadoprop} (type_id, value)
  131. VALUES (
  132. (SELECT cvterm_id
  133. FROM {cvterm} CVT
  134. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  135. WHERE CV.name = 'chado_properties' AND CVT.name = 'version'),
  136. :version)
  137. ";
  138. if ($action == 'Install Chado v1.2') {
  139. $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.2.sql';
  140. $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/initialize-1.2.sql';
  141. if (tripal_chado_reset_chado_schema()) {
  142. $success = tripal_chado_install_sql($schema_file);
  143. if ($success) {
  144. print "Install of Chado v1.2 (Step 1 of 2) Successful!\n";
  145. }
  146. else {
  147. print "Installation (Step 1 of 2) Problems! Please check output above for errors.\n";
  148. exit;
  149. }
  150. $success = tripal_chado_install_sql($init_file);
  151. if ($success) {
  152. print "Install of Chado v1.2 (Step 2 of 2) Successful.\nInstallation Complete\n";
  153. }
  154. else {
  155. print "Installation (Step 2 of 2) Problems! Please check output above for errors.\n";
  156. exit;
  157. }
  158. chado_query($vsql, array(':version' => '1.2')); # set the version
  159. }
  160. else {
  161. print "ERROR: cannot install chado. Please check database permissions\n";
  162. exit;
  163. }
  164. }
  165. elseif ($action == 'Upgrade Chado v1.11 to v1.2') {
  166. $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.11-1.2-diff.sql';
  167. $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/upgrade-1.11-1.2.sql';
  168. $success = tripal_chado_install_sql($schema_file);
  169. if ($success) {
  170. print "Upgrade from v1.11 to v1.2 (Step 1 of 2) Successful!\n";
  171. }
  172. else {
  173. print "Upgrade (Step 1 of 2) problems! Please check output above for errors.\n";
  174. exit;
  175. }
  176. $success = tripal_chado_install_sql($init_file);
  177. if ($success) {
  178. print "Upgrade from v1.11 to v1.2 (Step 2 of 2) Successful.\nUpgrade Complete!\n";
  179. }
  180. else {
  181. print "Upgrade (Step 2 of 2) problems! Please check output above for errors.\n";
  182. exit;
  183. }
  184. chado_query($vsql, array(':version' => '1.2')); # set the version
  185. }
  186. elseif ($action == 'Install Chado v1.11') {
  187. $schema_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/default_schema-1.11.sql';
  188. $init_file = drupal_get_path('module', 'tripal_chado') . '/chado_schema/initialize-1.11.sql';
  189. if (tripal_chado_reset_chado_schema()) {
  190. $success = tripal_chado_install_sql($schema_file);
  191. if ($success) {
  192. print "Install of Chado v1.11 (Step 1 of 2) Successful!\n";
  193. }
  194. else {
  195. print "Installation (Step 1 of 2) Problems! Please check output above for errors.\n";
  196. exit;
  197. }
  198. $success = tripal_chado_install_sql($init_file);
  199. if ($success) {
  200. print "Install of Chado v1.11 (Step 2 of 2) Successful.\nInstallation Complete!\n";
  201. }
  202. else {
  203. print "Installation (Step 2 of 2) Problems! Please check output above for errors.\n";
  204. exit;
  205. }
  206. }
  207. else {
  208. print "ERROR: cannot install chado. Please check database permissions\n";
  209. exit;
  210. }
  211. }
  212. }
  213. /**
  214. * Prepares Chado for use by Tripal.
  215. */
  216. function tripal_chado_prepare_chado() {
  217. // we want to force the version of Chado to be set properly
  218. $real_version = chado_get_version(TRUE);
  219. // get the effective version. Pass true as second argument
  220. // to warn the user if the current version is not compatible
  221. $version = chado_get_version(FALSE, FALSE);
  222. /////////////////////////////////////////////////////////////////////////////
  223. // Chado CV Module
  224. /////////////////////////////////////////////////////////////////////////////
  225. // Add the cv_root_mview.
  226. tripal_cv_add_cv_root_mview();
  227. // Add defaults to the tables that correlate OBO files/references with
  228. // a chado CV.
  229. tripal_cv_add_obo_defaults();
  230. // Add the Chado ontology CV.
  231. $obo_path = '{tripal_chado}/files/cv_property.obo';
  232. $obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  233. tripal_submit_obo_job(array('obo_id' => $obo_id));
  234. // Create the temp table we will use for loading OBO files.
  235. tripal_cv_create_tripal_obo_temp();
  236. /////////////////////////////////////////////////////////////////////////////
  237. // Chado Organism Module
  238. /////////////////////////////////////////////////////////////////////////////
  239. tripal_insert_cv(
  240. 'organism_property',
  241. 'Contains properties for organisms'
  242. );
  243. // set the default vocabularies
  244. tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
  245. /////////////////////////////////////////////////////////////////////////////
  246. // Chado CompAnalysis Module
  247. /////////////////////////////////////////////////////////////////////////////
  248. // we may need the analysisfeatureprop table if it doesn't already exist
  249. tripal_analysis_create_analysisfeatureprop();
  250. // add cvterms
  251. tripal_analysis_add_cvterms();
  252. // add materialized views
  253. tripal_analysis_add_mview_analysis_organism();
  254. // set the default vocabularies
  255. tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  256. /////////////////////////////////////////////////////////////////////////////
  257. // Chado Contact Module
  258. /////////////////////////////////////////////////////////////////////////////
  259. // Add the contactprop table to Chado.
  260. tripal_contact_add_custom_tables();
  261. // Add loading of the the tripal contact ontology to the job queue.
  262. $obo_path = '{tripal_chado}/files/tcontact.obo';
  263. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  264. tripal_submit_obo_job(array('obo_id' => $obo_id));
  265. // Add cvterms for relationship types.
  266. tripal_contact_add_cvs();
  267. // Set the default vocabularies.
  268. tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
  269. tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  270. tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  271. /////////////////////////////////////////////////////////////////////////////
  272. // Chado Feature Module
  273. /////////////////////////////////////////////////////////////////////////////
  274. // Note: the feature_property OBO that came with Chado v1.2 should not
  275. // be automatically installed. Some of the terms are duplicates of
  276. // others in better maintained vocabularies. New Tripal sites should
  277. // use those.
  278. // $obo_path = '{tripal_feature}/files/feature_property.obo';
  279. // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  280. // tripal_submit_obo_job(array('obo_id' => $obo_id));
  281. // Add the materialized view.
  282. tripal_feature_add_organism_count_mview();
  283. // Add the custom tables.
  284. tripal_feature_add_tripal_gff_temp_table();
  285. tripal_feature_add_tripal_gffcds_temp_table();
  286. tripal_feature_add_tripal_gffprotein_temp_table();
  287. // Add the vocabularies used by the feature module.
  288. tripal_feature_add_cvs();
  289. // Set the default vocabularies.
  290. tripal_set_default_cv('feature', 'type_id', 'sequence');
  291. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  292. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  293. /////////////////////////////////////////////////////////////////////////////
  294. // Chado Map Module
  295. /////////////////////////////////////////////////////////////////////////////
  296. // add the featuremapprop table to Chado
  297. tripal_featuremap_add_custom_tables();
  298. // Add cvterms
  299. tripal_featuremap_add_cvs();
  300. tripal_featuremap_add_cvterms();
  301. // set the default vocabularies
  302. tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
  303. tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
  304. tripal_set_default_cv('featuremap', 'unittype_id', 'featuremap_units');
  305. /////////////////////////////////////////////////////////////////////////////
  306. // Chado Library Module
  307. /////////////////////////////////////////////////////////////////////////////
  308. // add the materialized view
  309. tripal_library_add_mview_library_feature_count();
  310. // add cvterms
  311. tripal_library_add_cvs();
  312. tripal_library_add_cvterms();
  313. // set the default vocabularies
  314. tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  315. tripal_set_default_cv('library', 'type_id', 'library_type');
  316. /////////////////////////////////////////////////////////////////////////////
  317. // Chado NatDiv Module
  318. /////////////////////////////////////////////////////////////////////////////
  319. // add cvterms
  320. tripal_natural_diversity_add_cvterms();
  321. /////////////////////////////////////////////////////////////////////////////
  322. // Chado Project Module
  323. /////////////////////////////////////////////////////////////////////////////
  324. tripal_project_add_cvs();
  325. tripal_project_add_cvterms();
  326. // set the default vocabularies
  327. tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  328. tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  329. /////////////////////////////////////////////////////////////////////////////
  330. // Chado Pub Module
  331. /////////////////////////////////////////////////////////////////////////////
  332. global $base_path;
  333. // add loading of the the tripal pub ontology to the job queue
  334. $obo_path = '{tripal}/files/tpub.obo';
  335. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  336. tripal_submit_obo_job(array('obo_id' => $obo_id));
  337. tripal_pub_add_cvs();
  338. // add the custom tables
  339. tripal_pub_add_custom_tables();
  340. // set the default vocabularies
  341. tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  342. tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  343. tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  344. /////////////////////////////////////////////////////////////////////////////
  345. // Chado Stock Module
  346. /////////////////////////////////////////////////////////////////////////////
  347. // add some controlled vocabularies
  348. tripal_stock_add_cvs();
  349. // set the default vocabularies
  350. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  351. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  352. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  353. // add the materialized view
  354. tripal_stock_add_organism_count_mview();
  355. /////////////////////////////////////////////////////////////////////////////
  356. // Entity Bundles
  357. /////////////////////////////////////////////////////////////////////////////
  358. // Unfortunately, some Chado base tables do not have a type_id, so we must
  359. // take special action for those tables. These include: organism and
  360. // analysis. Until we can find an appropriate controlled vocabulary
  361. // that is well supported by the community with types for these tables we
  362. // will have to use in-house terms.
  363. // Add a term to be used for an inherent 'type_id' for the organism table.
  364. tripal_insert_cvterm(array(
  365. 'id' => 'local:organism',
  366. 'name' => 'organism',
  367. 'definition' => 'An individual form of life, such as a bacterium, protist, ' .
  368. 'fungus, plant, or animal, composed of a single cell or a complex of cells ' .
  369. 'in which organelles or organs work together to carry out the various ' .
  370. 'processes of life. (American Heritage® Dictionary of the English ' .
  371. 'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
  372. 'Harcourt Publishing Company).',
  373. 'cv_name' => 'local',
  374. ));
  375. // Add a term to be used for an inherent 'type_id' for the organism table.
  376. tripal_insert_cvterm(array(
  377. 'id' => 'local:analysis',
  378. 'name' => 'analysis',
  379. 'definition' => 'A process as a method of studying the nature of something ' .
  380. 'or of determining its essential features and their relations. ' .
  381. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  382. 'Dictionaries Ltd).',
  383. 'cv_name' => 'local',
  384. ));
  385. tripal_insert_cvterm(array(
  386. 'id' => 'local:project',
  387. 'name' => 'project',
  388. 'definition' => 'A plan or proposal for accomplishing something. ' .
  389. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  390. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  391. 'cv_name' => 'local',
  392. ));
  393. // For the TripalBundle entities we will want to associate the cvterm_id,
  394. // and the chado table and field that it maps to. We will use a few
  395. // variables to do this:
  396. tripal_insert_variable('chado_cvterm_id', 'The cvterm_id that a TripalBundle maps to.');
  397. tripal_insert_variable('chado_table', 'The name of the table to which a TripalBundle maps.');
  398. tripal_insert_variable('chado_column', 'The name of the column within the table that a TripalBundle maps to.');
  399. // We want to provide a set of commonly used entity types by default. This
  400. // way when a user first installs Tripal there are some commonly used
  401. // formats.
  402. module_load_include('inc', 'tripal', 'api/tripal.api');
  403. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  404. // Create the 'Organism' entity type. This uses the local:organism term.
  405. $error = '';
  406. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  407. $cvterm = chado_generate_var('cvterm', $term);
  408. if (!tripal_create_bundle('local', 'organism', 'organism', $error)) {
  409. throw new Exception($error);
  410. }
  411. // Create the 'Analysis' entity type. This uses the local:analysis term.
  412. $error = '';
  413. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  414. $cvterm = chado_generate_var('cvterm', $term);
  415. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  416. throw new Exception($error);
  417. }
  418. // Create the 'Project' entity type. This uses the local:project term.
  419. $error = '';
  420. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  421. $cvterm = chado_generate_var('cvterm', $term);
  422. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  423. throw new Exception($error);
  424. }
  425. }
  426. /**
  427. * Reset the Chado Schema
  428. * This drops the current chado and chado-related schema and re-creates it
  429. *
  430. * @ingroup tripal_chado
  431. */
  432. function tripal_chado_reset_chado_schema() {
  433. // drop current chado and chado-related schema
  434. if (chado_dbschema_exists('genetic_code')) {
  435. print "Dropping existing 'genetic_code' schema\n";
  436. db_query("drop schema genetic_code cascade");
  437. }
  438. if (chado_dbschema_exists('so')) {
  439. print "Dropping existing 'so' schema\n";
  440. db_query("drop schema so cascade");
  441. }
  442. if (chado_dbschema_exists('frange')) {
  443. print "Dropping existing 'frange' schema\n";
  444. db_query("drop schema frange cascade");
  445. }
  446. if (chado_dbschema_exists('chado')) {
  447. print "Dropping existing 'chado' schema\n";
  448. db_query("drop schema chado cascade");
  449. }
  450. // create the new chado schema
  451. print "Creating 'chado' schema\n";
  452. db_query("create schema chado");
  453. if (chado_dbschema_exists('chado')) {
  454. // before creating the plpgsql language let's check to make sure
  455. // it doesn't already exists
  456. $sql = "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'";
  457. $results = db_query($sql);
  458. $count = $results->fetchObject();
  459. if (!$count or $count->count == 0) {
  460. db_query("create language plpgsql");
  461. }
  462. return TRUE;
  463. }
  464. return FALSE;
  465. }
  466. /**
  467. * Execute the provided SQL
  468. *
  469. * @param $sql_file
  470. * Contains SQL statements to be executed
  471. *
  472. * @ingroup tripal_chado
  473. */
  474. function tripal_chado_install_sql($sql_file) {
  475. $chado_local = chado_dbschema_exists('chado');
  476. if ($chado_local) {
  477. db_query("set search_path to chado");
  478. }
  479. print "Loading $sql_file...\n";
  480. $lines = file($sql_file, FILE_SKIP_EMPTY_LINES);
  481. if (!$lines) {
  482. return 'Cannot open $schema_file';
  483. }
  484. $stack = array();
  485. $in_string = 0;
  486. $query = '';
  487. $i = 0;
  488. $success = 1;
  489. foreach ($lines as $line_num => $line) {
  490. $i++;
  491. $type = '';
  492. // find and remove comments except when inside of strings
  493. if (preg_match('/--/', $line) and !$in_string and !preg_match("/'.*?--.*?'/", $line)) {
  494. $line = preg_replace('/--.*$/', '', $line); // remove comments
  495. }
  496. if (preg_match('/\/\*.*?\*\//', $line)) {
  497. $line = preg_replace('/\/\*.*?\*\//', '', $line); // remove comments
  498. }
  499. // skip empty lines
  500. if (preg_match('/^\s*$/', $line) or strcmp($line, '')==0) {
  501. continue;
  502. }
  503. // Find SQL for new objects
  504. if (preg_match('/^\s*CREATE\s+TABLE/i', $line) and !$in_string) {
  505. $stack[] = 'table';
  506. $line = preg_replace("/public\./", "chado.", $line);
  507. }
  508. if (preg_match('/^\s*ALTER\s+TABLE/i', $line) and !$in_string) {
  509. $stack[] = 'alter table';
  510. $line = preg_replace("/public\./", "chado.", $line);
  511. }
  512. if (preg_match('/^\s*SET/i', $line) and !$in_string) {
  513. $stack[] = 'set';
  514. }
  515. if (preg_match('/^\s*CREATE\s+SCHEMA/i', $line) and !$in_string) {
  516. $stack[] = 'schema';
  517. }
  518. if (preg_match('/^\s*CREATE\s+SEQUENCE/i', $line) and !$in_string) {
  519. $stack[] = 'sequence';
  520. $line = preg_replace("/public\./", "chado.", $line);
  521. }
  522. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i', $line) and !$in_string) {
  523. $stack[] = 'view';
  524. $line = preg_replace("/public\./", "chado.", $line);
  525. }
  526. if (preg_match('/^\s*COMMENT/i', $line) and !$in_string and sizeof($stack)==0) {
  527. $stack[] = 'comment';
  528. $line = preg_replace("/public\./", "chado.", $line);
  529. }
  530. if (preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i', $line) and !$in_string) {
  531. $stack[] = 'function';
  532. $line = preg_replace("/public\./", "chado.", $line);
  533. }
  534. if (preg_match('/^\s*CREATE\s+INDEX/i', $line) and !$in_string) {
  535. $stack[] = 'index';
  536. }
  537. if (preg_match('/^\s*INSERT\s+INTO/i', $line) and !$in_string) {
  538. $stack[] = 'insert';
  539. $line = preg_replace("/public\./", "chado.", $line);
  540. }
  541. if (preg_match('/^\s*CREATE\s+TYPE/i', $line) and !$in_string) {
  542. $stack[] = 'type';
  543. }
  544. if (preg_match('/^\s*GRANT/i', $line) and !$in_string) {
  545. $stack[] = 'grant';
  546. }
  547. if (preg_match('/^\s*CREATE\s+AGGREGATE/i', $line) and !$in_string) {
  548. $stack[] = 'aggregate';
  549. }
  550. // determine if we are in a string that spans a line
  551. $matches = preg_match_all("/[']/i", $line, $temp);
  552. $in_string = $in_string - ($matches % 2);
  553. $in_string = abs($in_string);
  554. // if we've reached the end of an object the pop the stack
  555. if (strcmp($stack[sizeof($stack)-1], 'table') == 0 and preg_match('/\);\s*$/', $line)) {
  556. $type = array_pop($stack);
  557. }
  558. if (strcmp($stack[sizeof($stack)-1], 'alter table') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  559. $type = array_pop($stack);
  560. }
  561. if (strcmp($stack[sizeof($stack)-1], 'set') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  562. $type = array_pop($stack);
  563. }
  564. if (strcmp($stack[sizeof($stack)-1], 'schema') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  565. $type = array_pop($stack);
  566. }
  567. if (strcmp($stack[sizeof($stack)-1], 'sequence') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  568. $type = array_pop($stack);
  569. }
  570. if (strcmp($stack[sizeof($stack)-1], 'view') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  571. $type = array_pop($stack);
  572. }
  573. if (strcmp($stack[sizeof($stack)-1], 'comment') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  574. $type = array_pop($stack);
  575. }
  576. if (strcmp($stack[sizeof($stack)-1], 'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i", $line)) {
  577. $type = array_pop($stack);
  578. }
  579. if (strcmp($stack[sizeof($stack)-1], 'index') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  580. $type = array_pop($stack);
  581. }
  582. if (strcmp($stack[sizeof($stack)-1], 'insert') == 0 and preg_match('/\);\s*$/', $line)) {
  583. $type = array_pop($stack);
  584. }
  585. if (strcmp($stack[sizeof($stack)-1], 'type') == 0 and preg_match('/\);\s*$/', $line)) {
  586. $type = array_pop($stack);
  587. }
  588. if (strcmp($stack[sizeof($stack)-1], 'grant') == 0 and preg_match('/;\s*$/', $line) and !$in_string) {
  589. $type = array_pop($stack);
  590. }
  591. if (strcmp($stack[sizeof($stack)-1], 'aggregate') == 0 and preg_match('/\);\s*$/', $line)) {
  592. $type = array_pop($stack);
  593. }
  594. // if we're in a recognized SQL statement then let's keep track of lines
  595. if ($type or sizeof($stack) > 0) {
  596. $query .= "$line";
  597. }
  598. else {
  599. print "UNHANDLED $i, $in_string: $line";
  600. tripal_chado_chado_install_done();
  601. return FALSE;
  602. }
  603. if (preg_match_all("/\n/", $query, $temp) > 100) {
  604. print "SQL query is too long. Terminating:\n$query\n";
  605. tripal_chado_chado_install_done();
  606. return FALSE;
  607. }
  608. if ($type and sizeof($stack) == 0) {
  609. //print "Adding $type: line $i\n";
  610. // rewrite the set search_path to make 'public' be 'chado', but only if the
  611. // chado schema exists
  612. if (strcmp($type, 'set') == 0 and $chado_local) {
  613. $query = preg_replace("/public/m", "chado", $query);
  614. }
  615. // execute the statement
  616. $result = db_query($query);
  617. if (!$result) {
  618. $error = pg_last_error();
  619. print "FAILED. Line $i, $in_string\n$error:\n$query\n\n";
  620. tripal_chado_chado_install_done();
  621. $success = 0;
  622. return $success;
  623. }
  624. $query = '';
  625. }
  626. }
  627. tripal_chado_chado_install_done();
  628. return $success;
  629. }
  630. /**
  631. * Finish the Chado Schema Installation
  632. *
  633. * @ingroup tripal_chado
  634. */
  635. function tripal_chado_chado_install_done() {
  636. db_query("set search_path to default");
  637. }
  638. /**
  639. * Creates a materialized view that stores the type & number of stocks per organism
  640. *
  641. * @ingroup tripal_stock
  642. */
  643. function tripal_stock_add_organism_count_mview() {
  644. $view_name = 'organism_stock_count';
  645. $comment = 'Stores the type and number of stocks per organism';
  646. $schema = array(
  647. 'description' => $comment,
  648. 'table' => $view_name,
  649. 'fields' => array(
  650. 'organism_id' => array(
  651. 'type' => 'int',
  652. 'not null' => TRUE,
  653. ),
  654. 'genus' => array(
  655. 'type' => 'varchar',
  656. 'length' => '255',
  657. 'not null' => TRUE,
  658. ),
  659. 'species' => array(
  660. 'type' => 'varchar',
  661. 'length' => '255',
  662. 'not null' => TRUE,
  663. ),
  664. 'common_name' => array(
  665. 'type' => 'varchar',
  666. 'length' => '255',
  667. 'not null' => FALSE,
  668. ),
  669. 'num_stocks' => array(
  670. 'type' => 'int',
  671. 'not null' => TRUE,
  672. ),
  673. 'cvterm_id' => array(
  674. 'type' => 'int',
  675. 'not null' => TRUE,
  676. ),
  677. 'stock_type' => array(
  678. 'type' => 'varchar',
  679. 'length' => '255',
  680. 'not null' => TRUE,
  681. ),
  682. ),
  683. 'indexes' => array(
  684. 'organism_stock_count_idx1' => array('organism_id'),
  685. 'organism_stock_count_idx2' => array('cvterm_id'),
  686. 'organism_stock_count_idx3' => array('stock_type'),
  687. ),
  688. );
  689. $sql = "
  690. SELECT
  691. O.organism_id, O.genus, O.species, O.common_name,
  692. count(S.stock_id) as num_stocks,
  693. CVT.cvterm_id, CVT.name as stock_type
  694. FROM organism O
  695. INNER JOIN stock S ON O.Organism_id = S.organism_id
  696. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  697. GROUP BY
  698. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  699. ";
  700. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  701. }
  702. /**
  703. * Add cvs related to publications
  704. *
  705. * @ingroup tripal_pub
  706. */
  707. function tripal_stock_add_cvs() {
  708. // Add cv for relationship types
  709. tripal_insert_cv(
  710. 'stock_relationship',
  711. 'Contains types of relationships between stocks.'
  712. );
  713. tripal_insert_cv(
  714. 'stock_property',
  715. 'Contains properties for stocks.'
  716. );
  717. tripal_insert_cv(
  718. 'stock_type',
  719. 'Contains a list of types for stocks.'
  720. );
  721. }
  722. /**
  723. * Implementation of hook_schema().
  724. *
  725. * @ingroup tripal_pub
  726. */
  727. function tripal_chado_tripal_pub_import_schema() {
  728. return array(
  729. 'fields' => array(
  730. 'pub_import_id' => array(
  731. 'type' => 'serial',
  732. 'not null' => TRUE
  733. ),
  734. 'name' => array(
  735. 'type' => 'varchar',
  736. 'length' => 255,
  737. 'not null' => TRUE
  738. ),
  739. 'criteria' => array(
  740. 'type' => 'text',
  741. 'size' => 'normal',
  742. 'not null' => TRUE,
  743. 'description' => 'Contains a serialized PHP array containing the search criteria'
  744. ),
  745. 'disabled' => array(
  746. 'type' => 'int',
  747. 'unsigned' => TRUE,
  748. 'not NULL' => TRUE,
  749. 'default' => 0
  750. ),
  751. 'do_contact' => array(
  752. 'type' => 'int',
  753. 'unsigned' => TRUE,
  754. 'not NULL' => TRUE,
  755. 'default' => 0
  756. ),
  757. ),
  758. 'primary key' => array('pub_import_id'),
  759. 'indexes' => array(
  760. 'name' => array('name')
  761. ),
  762. );
  763. }
  764. /**
  765. * Add custom table related to publications
  766. * - pubauthor_contact
  767. *
  768. * @ingroup tripal_pub
  769. */
  770. function tripal_pub_add_custom_tables() {
  771. $schema = array (
  772. 'table' => 'pubauthor_contact',
  773. 'fields' => array (
  774. 'pubauthor_contact_id' => array (
  775. 'type' => 'serial',
  776. 'not null' => true,
  777. ),
  778. 'contact_id' => array (
  779. 'type' => 'int',
  780. 'not null' => true,
  781. ),
  782. 'pubauthor_id' => array (
  783. 'type' => 'int',
  784. 'not null' => true,
  785. ),
  786. ),
  787. 'primary key' => array (
  788. 0 => 'pubauthor_contact_id',
  789. ),
  790. 'unique keys' => array (
  791. 'pubauthor_contact_c1' => array (
  792. 0 => 'contact_id',
  793. 1 => 'pubauthor_id',
  794. ),
  795. ),
  796. 'foreign keys' => array (
  797. 'contact' => array (
  798. 'table' => 'contact',
  799. 'columns' => array (
  800. 'contact_id' => 'contact_id',
  801. ),
  802. ),
  803. 'pubauthor' => array (
  804. 'table' => 'pubauthor',
  805. 'columns' => array (
  806. 'pubauthor_id' => 'pubauthor_id',
  807. ),
  808. ),
  809. ),
  810. );
  811. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  812. }
  813. /**
  814. * Add cvs related to publications
  815. *
  816. * @ingroup tripal_pub
  817. */
  818. function tripal_pub_add_cvs() {
  819. // Add the cv for pub properties
  820. tripal_insert_cv(
  821. 'tripal_pub',
  822. '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.'
  823. );
  824. // Add the cv for pub types
  825. tripal_insert_cv(
  826. 'pub_type',
  827. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  828. );
  829. // Add the cv for pub properties
  830. tripal_insert_cv(
  831. 'pub_property',
  832. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  833. );
  834. // Add cv for relationship types
  835. tripal_insert_cv(
  836. 'pub_relationship',
  837. 'Contains types of relationships between publications.'
  838. );
  839. }
  840. /**
  841. * Add cvs pertaining to projects
  842. *
  843. * @ingroup tripal_project
  844. */
  845. function tripal_project_add_cvs() {
  846. // Add the cv for project properties
  847. tripal_insert_cv(
  848. 'project_property',
  849. 'Contains properties for projects'
  850. );
  851. // Add cv for relationship types
  852. tripal_insert_cv(
  853. 'project_relationship',
  854. 'Contains Types of relationships between projects.'
  855. );
  856. }
  857. /**
  858. * Add cvterms pertaining to projects
  859. *
  860. * @ingroup tripal_project
  861. */
  862. function tripal_project_add_cvterms() {
  863. // Insert cvterm 'Project Description' into cvterm table of chado
  864. // database. This CV term is used to keep track of the project
  865. // description in the projectprop table.
  866. tripal_insert_cvterm(
  867. array(
  868. 'name' => 'Project Description',
  869. 'definition' => 'Description of a project',
  870. 'cv_name' => 'project_property',
  871. 'is_relationship' => 0,
  872. 'db_name' => 'tripal'
  873. ),
  874. array('update_existing' => TRUE)
  875. );
  876. }
  877. /**
  878. * Add cvterms related to natural diversity
  879. *
  880. * @ingroup tripal_natural_diversity
  881. */
  882. function tripal_natural_diversity_add_cvterms(){
  883. // add cvterms for the nd_experiment_types
  884. tripal_insert_cvterm(
  885. array(
  886. 'name' => 'Genotyping',
  887. 'definition' => 'An experiment where genotypes of individuals are identified.',
  888. 'cv_name' => 'nd_experiment_types',
  889. 'is_relationship' => 0,
  890. 'db_name' => 'tripal'
  891. ),
  892. array('update_existing' => TRUE)
  893. );
  894. tripal_insert_cvterm(
  895. array(
  896. 'name' => 'Phenotyping',
  897. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  898. 'cv_name' => 'nd_experiment_types',
  899. 'is_relationship' => 0,
  900. 'db_name' => 'tripal'
  901. ),
  902. array('update_existing' => TRUE)
  903. );
  904. tripal_insert_cvterm(
  905. array(
  906. 'name' => 'Location',
  907. 'definition' => 'The name of the location.',
  908. 'cv_name' => 'nd_geolocation_property',
  909. 'is_relationship' => 0,
  910. 'db_name' => 'tripal'
  911. ),
  912. array('update_existing' => TRUE)
  913. );
  914. }
  915. /**
  916. * Adds a materialized view keeping track of the type of features associated with each library
  917. *
  918. * @ingroup tripal_library
  919. */
  920. function tripal_library_add_mview_library_feature_count(){
  921. $view_name = 'library_feature_count';
  922. $comment = 'Provides count of feature by type that are associated with all libraries';
  923. $schema = array(
  924. 'table' => $view_name,
  925. 'description' => $comment,
  926. 'fields' => array(
  927. 'library_id' => array(
  928. 'type' => 'int',
  929. 'not null' => TRUE,
  930. ),
  931. 'name' => array(
  932. 'type' => 'varchar',
  933. 'length' => 255,
  934. 'not null' => TRUE,
  935. ),
  936. 'num_features' => array(
  937. 'type' => 'int',
  938. 'not null' => TRUE,
  939. ),
  940. 'feature_type' => array(
  941. 'type' => 'varchar',
  942. 'length' => 255,
  943. 'not null' => TRUE,
  944. ),
  945. ),
  946. 'indexes' => array(
  947. 'library_feature_count_idx1' => array('library_id'),
  948. ),
  949. );
  950. $sql = "
  951. SELECT
  952. L.library_id, L.name,
  953. count(F.feature_id) as num_features,
  954. CVT.name as feature_type
  955. FROM library L
  956. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  957. INNER JOIN feature F ON LF.feature_id = F.feature_id
  958. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  959. GROUP BY L.library_id, L.name, CVT.name
  960. ";
  961. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  962. }
  963. /**
  964. * Adds cvterms needed for the library module
  965. *
  966. * @ingroup tripal_library
  967. */
  968. function tripal_library_add_cvterms() {
  969. // Insert cvterm 'library_description' into cvterm table of chado
  970. // database. This CV term is used to keep track of the library
  971. // description in the libraryprop table.
  972. tripal_insert_cvterm(
  973. array(
  974. 'name' => 'Library Description',
  975. 'definition' => 'Description of a library',
  976. 'cv_name' => 'library_property',
  977. 'is_relationship' => 0,
  978. 'db_name' => 'tripal'
  979. ),
  980. array('update_existing' => TRUE)
  981. );
  982. // add cvterms for the map unit types
  983. tripal_insert_cvterm(
  984. array(
  985. 'name' => 'cdna_library',
  986. 'definition' => 'cDNA library',
  987. 'cv_name' => 'library_type',
  988. 'is_relationship' => 0,
  989. 'db_name' => 'tripal'
  990. ),
  991. array('update_existing' => TRUE)
  992. );
  993. tripal_insert_cvterm(
  994. array(
  995. 'name' => 'bac_library',
  996. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  997. 'cv_name' => 'library_type',
  998. 'is_relationship' => 0,
  999. 'db_name' => 'tripal'
  1000. ),
  1001. array('update_existing' => TRUE)
  1002. );
  1003. tripal_insert_cvterm(
  1004. array(
  1005. 'name' => 'fosmid_library',
  1006. 'definition' => 'Fosmid library',
  1007. 'cv_name' => 'library_type',
  1008. 'is_relationship' => 0,
  1009. 'db_name' => 'tripal'
  1010. ),
  1011. array('update_existing' => TRUE)
  1012. );
  1013. tripal_insert_cvterm(
  1014. array(
  1015. 'name' => 'cosmid_library',
  1016. 'definition' => 'Cosmid library',
  1017. 'cv_name' => 'library_type',
  1018. 'is_relationship' => 0,
  1019. 'db_name' => 'tripal'
  1020. ),
  1021. array('update_existing' => TRUE)
  1022. );
  1023. tripal_insert_cvterm(
  1024. array(
  1025. 'name' => 'yac_library',
  1026. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  1027. 'cv_name' => 'library_type',
  1028. 'is_relationship' => 0,
  1029. 'db_name' => 'tripal'
  1030. ),
  1031. array('update_existing' => TRUE)
  1032. );
  1033. tripal_insert_cvterm(
  1034. array(
  1035. 'name' => 'genomic_library',
  1036. 'definition' => 'Genomic Library',
  1037. 'cv_name' => 'library_type',
  1038. 'is_relationship' => 0,
  1039. 'db_name' => 'tripal'
  1040. ),
  1041. array('update_existing' => TRUE)
  1042. );
  1043. }
  1044. /**
  1045. * Adds new CV's used by this module
  1046. *
  1047. * @ingroup tripal_library
  1048. */
  1049. function tripal_library_add_cvs(){
  1050. tripal_insert_cv(
  1051. 'library_property',
  1052. 'Contains properties for libraries.'
  1053. );
  1054. tripal_insert_cv(
  1055. 'library_type',
  1056. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  1057. );
  1058. }
  1059. /**
  1060. * Add custom tables needed by the feature map module
  1061. * - featuremapprop
  1062. * - featuremap_dbxref
  1063. * - featureposprop
  1064. *
  1065. * @ingroup tripal_featuremap
  1066. */
  1067. function tripal_featuremap_add_custom_tables(){
  1068. // add the featuremaprop table to Chado
  1069. $schema = array (
  1070. 'table' => 'featuremapprop',
  1071. 'fields' => array (
  1072. 'featuremapprop_id' => array (
  1073. 'type' => 'serial',
  1074. 'not null' => true,
  1075. ),
  1076. 'featuremap_id' => array (
  1077. 'type' => 'int',
  1078. 'not null' => true,
  1079. ),
  1080. 'type_id' => array (
  1081. 'type' => 'int',
  1082. 'not null' => true,
  1083. ),
  1084. 'value' => array (
  1085. 'type' => 'text',
  1086. 'not null' => false,
  1087. ),
  1088. 'rank' => array (
  1089. 'type' => 'int',
  1090. 'not null' => true,
  1091. 'default' => 0,
  1092. ),
  1093. ),
  1094. 'primary key' => array (
  1095. 0 => 'featuremapprop_id',
  1096. ),
  1097. 'unique keys' => array (
  1098. 'featuremapprop_c1' => array (
  1099. 0 => 'featuremap_id',
  1100. 1 => 'type_id',
  1101. 2 => 'rank',
  1102. ),
  1103. ),
  1104. 'indexes' => array (
  1105. 'featuremapprop_idx1' => array (
  1106. 0 => 'featuremap_id',
  1107. ),
  1108. 'featuremapprop_idx2' => array (
  1109. 0 => 'type_id',
  1110. ),
  1111. ),
  1112. 'foreign keys' => array (
  1113. 'cvterm' => array (
  1114. 'table' => 'cvterm',
  1115. 'columns' => array (
  1116. 'type_id' => 'cvterm_id',
  1117. ),
  1118. ),
  1119. 'featuremap' => array (
  1120. 'table' => 'featuremap',
  1121. 'columns' => array (
  1122. 'featuremap_id' => 'featuremap_id',
  1123. ),
  1124. ),
  1125. ),
  1126. );
  1127. chado_create_custom_table('featuremapprop', $schema, TRUE);
  1128. // add the featuremap_dbxref table to Chado
  1129. $schema = array (
  1130. 'table' => 'featuremap_dbxref',
  1131. 'fields' => array (
  1132. 'featuremap_dbxref_id' => array (
  1133. 'type' => 'serial',
  1134. 'not null' => true,
  1135. ),
  1136. 'featuremap_id' => array (
  1137. 'type' => 'int',
  1138. 'not null' => true,
  1139. ),
  1140. 'dbxref_id' => array (
  1141. 'type' => 'int',
  1142. 'not null' => true,
  1143. ),
  1144. ),
  1145. 'primary key' => array (
  1146. 0 => 'featuremap_dbxref_id',
  1147. ),
  1148. 'unique keys' => array (
  1149. 'featuremap_dbxref_c1' => array (
  1150. 0 => 'featuremap_id',
  1151. 1 => 'dbxref_id',
  1152. ),
  1153. ),
  1154. 'indexes' => array (
  1155. 'featuremap_dbxref_idx1' => array (
  1156. 0 => 'featuremap_dbxref_id',
  1157. ),
  1158. 'featuremap_dbxref_idx2' => array (
  1159. 0 => 'dbxref_id',
  1160. ),
  1161. ),
  1162. 'foreign keys' => array (
  1163. 'dbxref' => array (
  1164. 'table' => 'dbxref',
  1165. 'columns' => array (
  1166. 'dbxref_id' => 'dbxref_id',
  1167. ),
  1168. ),
  1169. 'featuremap' => array (
  1170. 'table' => 'featuremap',
  1171. 'columns' => array (
  1172. 'featuremap_id' => 'featuremap_id',
  1173. ),
  1174. ),
  1175. ),
  1176. 'referring_tables' => NULL,
  1177. );
  1178. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  1179. $schema = array (
  1180. 'table' => 'featureposprop',
  1181. 'fields' => array (
  1182. 'featureposprop_id' => array (
  1183. 'type' => 'serial',
  1184. 'not null' => true,
  1185. ),
  1186. 'featurepos_id' => array (
  1187. 'type' => 'int',
  1188. 'not null' => true,
  1189. ),
  1190. 'type_id' => array (
  1191. 'type' => 'int',
  1192. 'not null' => true,
  1193. ),
  1194. 'value' => array (
  1195. 'type' => 'text',
  1196. 'not null' => false,
  1197. ),
  1198. 'rank' => array (
  1199. 'type' => 'int',
  1200. 'not null' => true,
  1201. 'default' => 0,
  1202. ),
  1203. ),
  1204. 'primary key' => array (
  1205. 0 => 'featureposprop_id',
  1206. ),
  1207. 'unique keys' => array (
  1208. 'featureposprop_id' => array (
  1209. 0 => 'featurepos_id',
  1210. 1 => 'type_id',
  1211. 2 => 'rank',
  1212. ),
  1213. ),
  1214. 'indexes' => array (
  1215. 'featureposprop_c1' => array (
  1216. 0 => 'featurepos_id',
  1217. ),
  1218. 'featureposprop_idx2' => array (
  1219. 0 => 'type_id',
  1220. ),
  1221. ),
  1222. 'foreign keys' => array (
  1223. 'cvterm' => array (
  1224. 'table' => 'cvterm',
  1225. 'columns' => array (
  1226. 'type_id' => 'cvterm_id',
  1227. ),
  1228. ),
  1229. 'featurepos' => array (
  1230. 'table' => 'featurepos',
  1231. 'columns' => array (
  1232. 'featurepos_id' => 'featurepos_id',
  1233. ),
  1234. ),
  1235. ),
  1236. );
  1237. chado_create_custom_table('featureposprop', $schema, TRUE);
  1238. }
  1239. /**
  1240. * Add cv terms needed by the featuremap module
  1241. *
  1242. * @ingroup tripal_featuremap
  1243. */
  1244. function tripal_featuremap_add_cvterms() {
  1245. // add cvterms for the map unit types
  1246. tripal_insert_cvterm(
  1247. array(
  1248. 'name' => 'cM',
  1249. 'definition' => 'Centimorgan units',
  1250. 'cv_name' => 'featuremap_units',
  1251. 'is_relationship' => 0,
  1252. 'db_name' => 'tripal'
  1253. ),
  1254. array('update_existing' => TRUE)
  1255. );
  1256. tripal_insert_cvterm(
  1257. array(
  1258. 'name' => 'bp',
  1259. 'definition' => 'Base pairs units',
  1260. 'cv_name' => 'featuremap_units',
  1261. 'is_relationship' => 0,
  1262. 'db_name' => 'tripal'
  1263. ),
  1264. array('update_existing' => TRUE)
  1265. );
  1266. tripal_insert_cvterm(
  1267. array(
  1268. 'name' => 'bin_unit',
  1269. 'definition' => 'The bin unit',
  1270. 'cv_name' => 'featuremap_units',
  1271. 'is_relationship' => 0,
  1272. 'db_name' => 'tripal'
  1273. ),
  1274. array('update_existing' => TRUE)
  1275. );
  1276. tripal_insert_cvterm(
  1277. array(
  1278. 'name' => 'marker_order',
  1279. 'definition' => 'Units simply to define marker order.',
  1280. 'cv_name' => 'featuremap_units',
  1281. 'is_relationship' => 0,
  1282. 'db_name' => 'tripal'
  1283. ),
  1284. array('update_existing' => TRUE)
  1285. );
  1286. tripal_insert_cvterm(
  1287. array(
  1288. 'name' => 'undefined',
  1289. 'definition' => 'A catch-all for an undefined unit type',
  1290. 'cv_name' => 'featuremap_units',
  1291. 'is_relationship' => 0,
  1292. 'db_name' => 'tripal'
  1293. ),
  1294. array('update_existing' => TRUE)
  1295. );
  1296. // featurepos properties
  1297. tripal_insert_cvterm(
  1298. array(
  1299. 'name' => 'start',
  1300. 'definition' => 'The start coordinate for a map feature.',
  1301. 'cv_name' => 'featurepos_property',
  1302. 'is_relationship' => 0,
  1303. 'db_name' => 'tripal'
  1304. ),
  1305. array('update_existing' => TRUE)
  1306. );
  1307. tripal_insert_cvterm(
  1308. array(
  1309. 'name' => 'stop',
  1310. 'definition' => 'The end coordinate for a map feature',
  1311. 'cv_name' => 'featurepos_property',
  1312. 'is_relationship' => 0,
  1313. 'db_name' => 'tripal'
  1314. ),
  1315. array('update_existing' => TRUE)
  1316. );
  1317. // add cvterms for map properties
  1318. tripal_insert_cvterm(
  1319. array(
  1320. 'name' => 'Map Dbxref',
  1321. 'definition' => 'A unique identifer for the map in a remote database. The '
  1322. . 'format is a database abbreviation and a unique accession separated '
  1323. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1324. 'cv_name' => 'featuremap_property',
  1325. 'is_relationship' => 0,
  1326. 'db_name' => 'tripal'
  1327. ),
  1328. array('update_existing' => TRUE)
  1329. );
  1330. tripal_insert_cvterm(
  1331. array(
  1332. 'name' => 'Map Type',
  1333. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1334. 'cv_name' => 'featuremap_property',
  1335. 'is_relationship' => 0,
  1336. 'db_name' => 'tripal'
  1337. ),
  1338. array('update_existing' => TRUE)
  1339. );
  1340. tripal_insert_cvterm(
  1341. array(
  1342. 'name' => 'Genome Group',
  1343. 'definition' => '',
  1344. 'cv_name' => 'featuremap_property',
  1345. 'is_relationship' => 0,
  1346. 'db_name' => 'tripal'
  1347. ),
  1348. array('update_existing' => TRUE)
  1349. );
  1350. tripal_insert_cvterm(
  1351. array(
  1352. 'name' => 'URL',
  1353. 'definition' => 'A univeral resource locator (URL) reference where the '
  1354. . 'publication can be found. For maps found online, this would be '
  1355. . 'the web address for the map.',
  1356. 'cv_name' => 'featuremap_property',
  1357. 'is_relationship' => 0,
  1358. 'db_name' => 'tripal'
  1359. ),
  1360. array('update_existing' => TRUE)
  1361. );
  1362. tripal_insert_cvterm(
  1363. array(
  1364. 'name' => 'Population Type',
  1365. 'definition' => 'A brief description of the population type used to generate '
  1366. . 'the map (e.g. RIL, F2, BC1, etc).',
  1367. 'cv_name' => 'featuremap_property',
  1368. 'is_relationship' => 0,
  1369. 'db_name' => 'tripal'
  1370. ),
  1371. array('update_existing' => TRUE)
  1372. );
  1373. tripal_insert_cvterm(
  1374. array(
  1375. 'name' => 'Population Size',
  1376. 'definition' => 'The size of the population used to construct the map.',
  1377. 'cv_name' => 'featuremap_property',
  1378. 'is_relationship' => 0,
  1379. 'db_name' => 'tripal'
  1380. ),
  1381. array('update_existing' => TRUE)
  1382. );
  1383. tripal_insert_cvterm(
  1384. array(
  1385. 'name' => 'Methods',
  1386. 'definition' => 'A brief description of the methods used to construct the map.',
  1387. 'cv_name' => 'featuremap_property',
  1388. 'is_relationship' => 0,
  1389. 'db_name' => 'tripal'
  1390. ),
  1391. array('update_existing' => TRUE)
  1392. );
  1393. tripal_insert_cvterm(
  1394. array(
  1395. 'name' => 'Software',
  1396. 'definition' => 'The software used to construct the map.',
  1397. 'cv_name' => 'featuremap_property',
  1398. 'is_relationship' => 0,
  1399. 'db_name' => 'tripal'
  1400. ),
  1401. array('update_existing' => TRUE)
  1402. );
  1403. }
  1404. /**
  1405. * Add cvs needed by the featuremap module
  1406. *
  1407. * @ingroup tripal_featuremap
  1408. */
  1409. function tripal_featuremap_add_cvs() {
  1410. tripal_insert_cv(
  1411. 'featuremap_units',
  1412. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1413. );
  1414. tripal_insert_cv(
  1415. 'featurepos_property',
  1416. 'Contains terms map properties.'
  1417. );
  1418. tripal_insert_cv(
  1419. 'featuremap_property',
  1420. 'Contains positional types for the feature positions'
  1421. );
  1422. }
  1423. /**
  1424. * Add cvs related to features
  1425. *
  1426. * @ingroup tripal_pub
  1427. */
  1428. function tripal_feature_add_cvs() {
  1429. // Add cv for relationship types
  1430. tripal_insert_cv(
  1431. 'feature_relationship',
  1432. 'Contains types of relationships between features.'
  1433. );
  1434. // The feature_property CV may already exists. It comes with Chado, but
  1435. // we need to add it just in case it doesn't get added before the feature
  1436. // module is installed. But as of Tripal v3.0 the Chado version of this
  1437. // vocabulary is no longer loaded by default.
  1438. tripal_insert_cv(
  1439. 'feature_property',
  1440. 'Stores properties about features'
  1441. );
  1442. // the feature type vocabulary should be the sequence ontology, and even though
  1443. // this ontology should get loaded we will create it here just so that we can
  1444. // set the default vocabulary for the feature.type_id field
  1445. tripal_insert_cv(
  1446. 'sequence',
  1447. 'The Sequence Ontology'
  1448. );
  1449. }
  1450. function tripal_feature_add_tripal_gff_temp_table() {
  1451. $schema = array(
  1452. 'table' => 'tripal_gff_temp',
  1453. 'fields' => array(
  1454. 'feature_id' => array(
  1455. 'type' => 'int',
  1456. 'not null' => TRUE,
  1457. ),
  1458. 'organism_id' => array(
  1459. 'type' => 'int',
  1460. 'not null' => TRUE,
  1461. ),
  1462. 'uniquename' => array(
  1463. 'type' => 'text',
  1464. 'not null' => TRUE,
  1465. ),
  1466. 'type_name' => array(
  1467. 'type' => 'varchar',
  1468. 'length' => '1024',
  1469. 'not null' => TRUE,
  1470. ),
  1471. ),
  1472. 'indexes' => array(
  1473. 'tripal_gff_temp_idx0' => array('feature_id'),
  1474. 'tripal_gff_temp_idx0' => array('organism_id'),
  1475. 'tripal_gff_temp_idx1' => array('uniquename'),
  1476. ),
  1477. 'unique keys' => array(
  1478. 'tripal_gff_temp_uq0' => array('feature_id'),
  1479. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1480. ),
  1481. );
  1482. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1483. }
  1484. /**
  1485. *
  1486. */
  1487. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1488. $schema = array(
  1489. 'table' => 'tripal_gffcds_temp',
  1490. 'fields' => array(
  1491. 'feature_id' => array(
  1492. 'type' => 'int',
  1493. 'not null' => TRUE,
  1494. ),
  1495. 'parent_id' => array(
  1496. 'type' => 'int',
  1497. 'not null' => TRUE,
  1498. ),
  1499. 'phase' => array(
  1500. 'type' => 'int',
  1501. 'not null' => TRUE,
  1502. ),
  1503. 'strand' => array(
  1504. 'type' => 'int',
  1505. 'not null' => TRUE,
  1506. ),
  1507. 'fmin' => array(
  1508. 'type' => 'int',
  1509. 'not null' => TRUE,
  1510. ),
  1511. 'fmax' => array(
  1512. 'type' => 'int',
  1513. 'not null' => TRUE,
  1514. ),
  1515. ),
  1516. 'indexes' => array(
  1517. 'tripal_gff_temp_idx0' => array('feature_id'),
  1518. 'tripal_gff_temp_idx0' => array('parent_id'),
  1519. ),
  1520. );
  1521. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1522. }
  1523. /**
  1524. *
  1525. */
  1526. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1527. $schema = array(
  1528. 'table' => 'tripal_gffprotein_temp',
  1529. 'fields' => array(
  1530. 'feature_id' => array(
  1531. 'type' => 'int',
  1532. 'not null' => TRUE,
  1533. ),
  1534. 'parent_id' => array(
  1535. 'type' => 'int',
  1536. 'not null' => TRUE,
  1537. ),
  1538. 'fmin' => array(
  1539. 'type' => 'int',
  1540. 'not null' => TRUE,
  1541. ),
  1542. 'fmax' => array(
  1543. 'type' => 'int',
  1544. 'not null' => TRUE,
  1545. ),
  1546. ),
  1547. 'indexes' => array(
  1548. 'tripal_gff_temp_idx0' => array('feature_id'),
  1549. 'tripal_gff_temp_idx0' => array('parent_id'),
  1550. ),
  1551. 'unique keys' => array(
  1552. 'tripal_gff_temp_uq0' => array('feature_id'),
  1553. ),
  1554. );
  1555. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1556. }
  1557. /**
  1558. * Creates a materialized view that stores the type & number of features per organism
  1559. *
  1560. * @ingroup tripal_feature
  1561. */
  1562. function tripal_feature_add_organism_count_mview() {
  1563. $view_name = 'organism_feature_count';
  1564. $comment = 'Stores the type and number of features per organism';
  1565. $schema = array(
  1566. 'description' => $comment,
  1567. 'table' => $view_name,
  1568. 'fields' => array(
  1569. 'organism_id' => array(
  1570. 'type' => 'int',
  1571. 'not null' => TRUE,
  1572. ),
  1573. 'genus' => array(
  1574. 'type' => 'varchar',
  1575. 'length' => '255',
  1576. 'not null' => TRUE,
  1577. ),
  1578. 'species' => array(
  1579. 'type' => 'varchar',
  1580. 'length' => '255',
  1581. 'not null' => TRUE,
  1582. ),
  1583. 'common_name' => array(
  1584. 'type' => 'varchar',
  1585. 'length' => '255',
  1586. 'not null' => FALSE,
  1587. ),
  1588. 'num_features' => array(
  1589. 'type' => 'int',
  1590. 'not null' => TRUE,
  1591. ),
  1592. 'cvterm_id' => array(
  1593. 'type' => 'int',
  1594. 'not null' => TRUE,
  1595. ),
  1596. 'feature_type' => array(
  1597. 'type' => 'varchar',
  1598. 'length' => '255',
  1599. 'not null' => TRUE,
  1600. ),
  1601. ),
  1602. 'indexes' => array(
  1603. 'organism_feature_count_idx1' => array('organism_id'),
  1604. 'organism_feature_count_idx2' => array('cvterm_id'),
  1605. 'organism_feature_count_idx3' => array('feature_type'),
  1606. ),
  1607. );
  1608. $sql = "
  1609. SELECT
  1610. O.organism_id, O.genus, O.species, O.common_name,
  1611. count(F.feature_id) as num_features,
  1612. CVT.cvterm_id, CVT.name as feature_type
  1613. FROM organism O
  1614. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1615. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1616. GROUP BY
  1617. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1618. ";
  1619. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1620. }
  1621. /**
  1622. * Adds any cvs needed by this module.
  1623. *
  1624. * @ingroup tripal_contact
  1625. */
  1626. function tripal_contact_add_cvs() {
  1627. // Add the cv for contact properties. This is a default vocabulary in the event
  1628. // that a user does not want to use the tripal_contact vocabulary
  1629. tripal_insert_cv(
  1630. 'contact_property',
  1631. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1632. );
  1633. // add the cv for the contact type. This is a default vocabulary in the event
  1634. // that a user does not want to use the tripal_contact vocabulary
  1635. tripal_insert_cv(
  1636. 'contact_type',
  1637. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1638. );
  1639. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1640. tripal_insert_cv(
  1641. 'tripal_contact',
  1642. '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.'
  1643. );
  1644. // add the cv for contact relationships
  1645. tripal_insert_cv(
  1646. 'contact_relationship',
  1647. 'Contains types of relationships between contacts.'
  1648. );
  1649. }
  1650. /**
  1651. * Add any custom tables needed by this module.
  1652. * - Contactprop: keep track of properties of contact
  1653. *
  1654. * @ingroup tripal_contact
  1655. */
  1656. function tripal_contact_add_custom_tables(){
  1657. $schema = array (
  1658. 'table' => 'contactprop',
  1659. 'fields' => array (
  1660. 'contactprop_id' => array (
  1661. 'type' => 'serial',
  1662. 'not null' => true,
  1663. ),
  1664. 'contact_id' => array (
  1665. 'type' => 'int',
  1666. 'not null' => true,
  1667. ),
  1668. 'type_id' => array (
  1669. 'type' => 'int',
  1670. 'not null' => true,
  1671. ),
  1672. 'value' => array (
  1673. 'type' => 'text',
  1674. 'not null' => false,
  1675. ),
  1676. 'rank' => array (
  1677. 'type' => 'int',
  1678. 'not null' => true,
  1679. 'default' => 0,
  1680. ),
  1681. ),
  1682. 'primary key' => array (
  1683. 0 => 'contactprop_id',
  1684. ),
  1685. 'unique keys' => array (
  1686. 'contactprop_c1' => array (
  1687. 0 => 'contact_id',
  1688. 1 => 'type_id',
  1689. 2 => 'rank',
  1690. ),
  1691. ),
  1692. 'indexes' => array (
  1693. 'contactprop_idx1' => array (
  1694. 0 => 'contact_id',
  1695. ),
  1696. 'contactprop_idx2' => array (
  1697. 0 => 'type_id',
  1698. ),
  1699. ),
  1700. 'foreign keys' => array (
  1701. 'cvterm' => array (
  1702. 'table' => 'cvterm',
  1703. 'columns' => array (
  1704. 'type_id' => 'cvterm_id',
  1705. ),
  1706. ),
  1707. 'contact' => array (
  1708. 'table' => 'contact',
  1709. 'columns' => array (
  1710. 'contact_id' => 'contact_id',
  1711. ),
  1712. ),
  1713. ),
  1714. );
  1715. chado_create_custom_table('contactprop', $schema, TRUE);
  1716. }
  1717. /**
  1718. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1719. * analysisfeature links.
  1720. *
  1721. * @ingroup tripal_analysis
  1722. */
  1723. function tripal_analysis_create_analysisfeatureprop() {
  1724. // Create analysisfeatureprop table in chado. This is needed for Chado
  1725. // version 1.11, the table exists in Chado 1.2.
  1726. if (!db_table_exists('chado.analysisfeatureprop')) {
  1727. $sql = "
  1728. CREATE TABLE {analysisfeatureprop} (
  1729. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1730. analysisfeature_id INTEGER NOT NULL,
  1731. type_id INTEGER NOT NULL,
  1732. value TEXT,
  1733. rank INTEGER NOT NULL,
  1734. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1735. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1736. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1737. )
  1738. ";
  1739. chado_query($sql);
  1740. }
  1741. }
  1742. /**
  1743. * Creates a view showing the link between an organism & it's analysis through associated features.
  1744. *
  1745. * @ingroup tripal_analysis
  1746. */
  1747. function tripal_analysis_add_mview_analysis_organism() {
  1748. $view_name = 'analysis_organism';
  1749. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1750. // this is the SQL used to identify the organism to which an analsysis
  1751. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1752. // joins
  1753. $sql = "
  1754. SELECT DISTINCT A.analysis_id, O.organism_id
  1755. FROM analysis A
  1756. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1757. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1758. INNER JOIN organism O ON O.organism_id = F.organism_id
  1759. ";
  1760. // the schema array for describing this view
  1761. $schema = array(
  1762. 'table' => $view_name,
  1763. 'description' => $comment,
  1764. 'fields' => array(
  1765. 'analysis_id' => array(
  1766. 'type' => 'int',
  1767. 'not null' => TRUE,
  1768. ),
  1769. 'organism_id' => array(
  1770. 'type' => 'int',
  1771. 'not null' => TRUE,
  1772. ),
  1773. ),
  1774. 'indexes' => array(
  1775. 'networkmod_qtl_indx0' => array('analysis_id'),
  1776. 'networkmod_qtl_indx1' => array('organism_id'),
  1777. ),
  1778. 'foreign keys' => array(
  1779. 'analysis' => array(
  1780. 'table' => 'analysis',
  1781. 'columns' => array(
  1782. 'analysis_id' => 'analysis_id',
  1783. ),
  1784. ),
  1785. 'organism' => array(
  1786. 'table' => 'organism',
  1787. 'columns' => array(
  1788. 'organism_id' => 'organism_id',
  1789. ),
  1790. ),
  1791. ),
  1792. );
  1793. // add the view
  1794. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1795. }
  1796. /**
  1797. * Adds controlled vocabulary terms needed by this module.
  1798. *
  1799. * @ingroup tripal_analysis
  1800. */
  1801. function tripal_analysis_add_cvterms() {
  1802. tripal_insert_cv(
  1803. 'tripal_analysis',
  1804. 'Terms used for managing analyses in Tripal'
  1805. );
  1806. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1807. // get rid of it in case it is used, so just keep it in the Tripal CV
  1808. tripal_insert_cvterm(
  1809. array(
  1810. 'name' => 'analysis_date',
  1811. 'definition' => 'The date that an analysis was performed.',
  1812. 'cv_name' => 'tripal',
  1813. 'is_relationship' => 0,
  1814. 'db_name' => 'tripal'
  1815. ),
  1816. array('update_existing' => TRUE)
  1817. );
  1818. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1819. // get rid of it in case it is used, so just keep it in the Tripal CV
  1820. tripal_insert_cvterm(
  1821. array(
  1822. 'name' => 'analysis_short_name',
  1823. 'definition' => 'A computer legible (no spaces or special characters) '
  1824. . 'abbreviation for the analysis.',
  1825. 'cv_name' => 'tripal',
  1826. 'is_relationship' => 0,
  1827. 'db_name' => 'tripal'
  1828. ),
  1829. array('update_existing' => TRUE)
  1830. );
  1831. // the 'analysis_property' vocabulary is for user definable properties wo we
  1832. // will add an 'Analysis Type' to this vocubulary
  1833. tripal_insert_cvterm(
  1834. array(
  1835. 'name' => 'Analysis Type',
  1836. 'definition' => 'The type of analysis that was performed.',
  1837. 'cv_name' => 'analysis_property',
  1838. 'is_relationship' => 0,
  1839. 'db_name' => 'tripal'
  1840. ),
  1841. array('update_existing' => TRUE)
  1842. );
  1843. }
  1844. /**
  1845. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1846. *
  1847. * @ingroup tripal_cv
  1848. */
  1849. function tripal_cv_add_cv_root_mview() {
  1850. $mv_name = 'cv_root_mview';
  1851. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1852. $schema = array(
  1853. 'table' => $mv_name,
  1854. 'description' => $comment,
  1855. 'fields' => array(
  1856. 'name' => array(
  1857. 'type' => 'varchar',
  1858. 'length' => 255,
  1859. 'not null' => TRUE,
  1860. ),
  1861. 'cvterm_id' => array(
  1862. 'type' => 'int',
  1863. 'not null' => TRUE,
  1864. ),
  1865. 'cv_id' => array(
  1866. 'type' => 'int',
  1867. 'not null' => TRUE,
  1868. ),
  1869. 'cv_name' => array(
  1870. 'type' => 'varchar',
  1871. 'length' => 255,
  1872. 'not null' => TRUE,
  1873. ),
  1874. ),
  1875. 'indexes' => array(
  1876. 'cv_root_mview_indx1' => array('cvterm_id'),
  1877. 'cv_root_mview_indx2' => array('cv_id'),
  1878. ),
  1879. );
  1880. $sql = "
  1881. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1882. FROM cvterm_relationship CVTR
  1883. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1884. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1885. WHERE CVTR.object_id not in
  1886. (SELECT subject_id FROM cvterm_relationship)
  1887. ";
  1888. // Create the MView
  1889. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1890. }
  1891. /**
  1892. * Creates a temporary table to store obo details while loading an obo file
  1893. *
  1894. * @ingroup tripal_cv
  1895. */
  1896. function tripal_cv_create_tripal_obo_temp() {
  1897. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1898. // we create it here using plain SQL because we want it to be in the chado schema but we
  1899. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1900. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1901. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1902. if (!db_table_exists('chado.tripal_obo_temp')) {
  1903. $sql = "
  1904. CREATE TABLE {tripal_obo_temp} (
  1905. id character varying(255) NOT NULL,
  1906. stanza text NOT NULL,
  1907. type character varying(50) NOT NULL,
  1908. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1909. );
  1910. ";
  1911. chado_query($sql);
  1912. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1913. chado_query($sql);
  1914. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1915. chado_query($sql);
  1916. }
  1917. }