tripal_chado.setup.inc 66 KB

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