tripal_chado.setup.inc 67 KB

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