tripal_chado.setup.inc 64 KB

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