tripal_chado.setup.inc 64 KB

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