tripal_chado.setup.inc 67 KB

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