tripal_core.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. require_once "jobs.php";
  3. require_once "mviews.php";
  4. require_once "cvterms.php";
  5. require_once "chado_install.php";
  6. require_once "bulk_loader.php";
  7. require_once "tripal_core.api.inc";
  8. /**
  9. * @defgroup tripal_core Core Tripal Module
  10. * @{
  11. * Provides basic functions for interacting with chado
  12. * @}
  13. */
  14. /**
  15. *
  16. *
  17. * @ingroup tripal_core
  18. */
  19. function tripal_core_init(){
  20. // the two lines below are necessary to ensure that the search_path
  21. // variable is always set. In the case where a view needs to query the
  22. // chado schema when it is local to the Drupal database. Otherwise the
  23. // search_path isn't set. When tripal_db_set_active is called it
  24. // automatically sets the serach path if chado is local to the
  25. // Drupal database
  26. $previous = tripal_db_set_active('chado');
  27. tripal_db_set_active($previous);
  28. // create the 'tripal' controlled volcabulary in chado but only if it doesn't already exist, and
  29. // only if the chado database is present.
  30. if(tripal_core_is_chado_installed()){
  31. $previous_db = tripal_db_set_active('chado'); // use chado database
  32. if(!db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = 'tripal'"))){
  33. $results = db_query("INSERT INTO {cv} (name,definition) ".
  34. "VALUES ('tripal','Terms used by Tripal for modules to manage data such as that stored in property tables like featureprop, analysisprop, etc')");
  35. }
  36. if(!db_fetch_object(db_query("SELECT * FROM {db} WHERE name = 'tripal'"))){
  37. $results = db_query("INSERT INTO {db} (name,description) ".
  38. "VALUES ('tripal','Used as a database placeholder for tripal defined objects such as tripal cvterms')");
  39. }
  40. tripal_db_set_active($previous_db); // now use drupal database
  41. }
  42. // the tripal themeing uses rounded corners. We could use CSS but IE8 doesn't supprt
  43. // rouneded corners so we will use the JQuery method.
  44. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jquery-corners-0.3/jquery.corners.min.js');
  45. }
  46. /**
  47. *
  48. *
  49. * @ingroup tripal_core
  50. */
  51. function tripal_create_moddir($module_name){
  52. // make the data directory for this module
  53. $data_dir = file_directory_path() . "/tripal/$module_name";
  54. if(!file_check_directory($data_dir,FILE_CREATE_DIRECTORY|FILE_MODIFY_PERMISSIONS)){
  55. $message = "Cannot create directory $data_dir. This module may not ".
  56. "behave correctly without this directory. Please create ".
  57. "the directory manually or fix the problem and reinstall.";
  58. drupal_set_message($message,'error');
  59. watchdog('tripal_core',$message,array(),WATCHDOG_ERROR);
  60. }
  61. }
  62. /**
  63. *
  64. *
  65. * @ingroup tripal_core
  66. */
  67. function tripal_get_moddir($module_name){
  68. $data_dir = file_directory_path() . "/tripal/$module_name";
  69. return $data_dir;
  70. }
  71. /**
  72. *
  73. *
  74. * @ingroup tripal_core
  75. */
  76. function tripal_core_menu() {
  77. $items = array();
  78. // Triapl setting groups
  79. $items['admin/tripal'] = array(
  80. 'title' => 'Tripal Management',
  81. 'description' => "Manage the behavior or Tripal and its various modules.",
  82. 'position' => 'right',
  83. 'weight' => -5,
  84. 'page callback' => 'system_admin_menu_block_page',
  85. 'access arguments' => array('administer site configuration'),
  86. 'file' => 'system.admin.inc',
  87. 'file path' => drupal_get_path('module', 'system'),
  88. );
  89. // the administative settings menu
  90. /* $items['admin/tripal/tripal_core'] = array(
  91. 'title' => 'Tripal core settings',
  92. 'description' => 'Tripal Settings',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('tripal_core_admin'),
  95. 'access arguments' => array('access administration pages'),
  96. 'type' => MENU_NORMAL_ITEM,
  97. );
  98. */
  99. $items['admin/tripal/tripal_jobs'] = array(
  100. 'title' => 'Jobs',
  101. 'description' => 'Jobs managed by Tripal',
  102. 'page callback' => 'tripal_jobs_report',
  103. 'access arguments' => array('access administration pages'),
  104. 'type' => MENU_NORMAL_ITEM,
  105. );
  106. $items['admin/tripal/tripal_jobs/cancel/%'] = array(
  107. 'title' => 'Jobs',
  108. 'description' => 'Cancel a pending job',
  109. 'page callback' => 'tripal_jobs_cancel',
  110. 'page arguments' => array(4),
  111. 'access arguments' => array('access administration pages'),
  112. 'type' => MENU_CALLBACK,
  113. );
  114. $items['admin/tripal/tripal_jobs/rerun/%'] = array(
  115. 'title' => 'Jobs',
  116. 'description' => 'Re-run an existing job.',
  117. 'page callback' => 'tripal_jobs_rerun',
  118. 'page arguments' => array(4),
  119. 'access arguments' => array('access administration pages'),
  120. 'type' => MENU_CALLBACK,
  121. );
  122. $items['admin/tripal/tripal_jobs/view/%'] = array(
  123. 'title' => 'Jobs Details',
  124. 'description' => 'View job details.',
  125. 'page callback' => 'tripal_jobs_view',
  126. 'page arguments' => array(4),
  127. 'access arguments' => array('access administration pages'),
  128. 'type' => MENU_CALLBACK,
  129. );
  130. $items['admin/tripal/tripal_mview/%'] = array(
  131. 'title' => 'Materialized View',
  132. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  133. 'page callback' => 'tripal_mview_report',
  134. 'page arguments' => array(3),
  135. 'access arguments' => array('access administration pages'),
  136. 'type' => MENU_NORMAL_ITEM,
  137. );
  138. $items['admin/tripal/tripal_mviews'] = array(
  139. 'title' => 'Materialized Views',
  140. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  141. 'page callback' => 'tripal_mviews_report',
  142. 'access arguments' => array('access administration pages'),
  143. 'type' => MENU_NORMAL_ITEM,
  144. );
  145. $items['admin/tripal/tripal_mviews/new'] = array(
  146. 'title' => 'Create View',
  147. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  148. 'page callback' => 'drupal_get_form',
  149. 'page arguments' => array('tripal_mviews_form'),
  150. 'access arguments' => array('access administration pages'),
  151. 'type' => MENU_NORMAL_ITEM,
  152. );
  153. $items['admin/tripal/tripal_mviews/edit/%'] = array(
  154. 'title' => 'Edit View',
  155. 'page callback' => 'drupal_get_form',
  156. 'page arguments' => array('tripal_mviews_form',4),
  157. 'access arguments' => array('access administration pages'),
  158. 'type' => MENU_NORMAL_ITEM,
  159. );
  160. $items['admin/tripal/tripal_mviews/action/%/%'] = array(
  161. 'title' => 'Create View',
  162. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  163. 'page callback' => 'tripal_mviews_action',
  164. 'page arguments' => array(4,5),
  165. 'access arguments' => array('access administration pages'),
  166. 'type' => MENU_CALLBACK,
  167. );
  168. $items['tripal_toggle_box_menu/%/%/%'] = array(
  169. 'title' => t('Libraries'),
  170. 'page callback' => 'tripal_toggle_box_menu',
  171. 'page arguments' => array(1,2,3),
  172. 'access arguments' => array('access administration pages'),
  173. 'type' => MENU_CALLBACK | MENU_LINKS_TO_PARENT
  174. );
  175. $items['admin/tripal/chado_1_11_install'] = array(
  176. 'title' => 'Install Chado v1.11',
  177. 'description' => 'Installs Chado version 1.11 inside the current Drupal database',
  178. 'page callback' => 'drupal_get_form',
  179. 'page arguments' => array('tripal_core_chado_v1_11_load_form'),
  180. 'access arguments' => array('access administration pages'),
  181. 'type' => MENU_NORMAL_ITEM,
  182. );
  183. $items['admin/tripal/bulk_load/create'] = array(
  184. 'title' => 'Create Bulk Loader',
  185. 'description' => 'Create a bulk loader template for loading data into Chado',
  186. 'page callback' => 'tripal_core_bulk_loader_create',
  187. 'access arguments' => array('access administration pages'),
  188. 'type' => MENU_NORMAL_ITEM,
  189. );
  190. $items['admin/tripal/bulk_load/step2_get_type/%'] = array(
  191. 'title' => 'Create Bulk Loader',
  192. 'description' => 'Create a bulk loader template for loading data into Chado',
  193. 'page callback' => 'tripal_core_bulk_loader_ahah_step2_feature_type',
  194. 'page arguments' => array(4),
  195. 'access arguments' => array('access administration pages'),
  196. 'type' => MENU_CALLBACK,
  197. );
  198. $items['test2'] = array(
  199. 'title' => 'test',
  200. 'description' => 'test',
  201. 'page callback' => 'tripal_core_chado_insert_test',
  202. 'access arguments' => array('access administration pages'),
  203. 'type' => MENU_CALLBACK,
  204. );
  205. return $items;
  206. }
  207. /**
  208. *
  209. *
  210. * @ingroup tripal_core
  211. */
  212. function tripal_core_admin () {
  213. $form['chado_feature_data_url'] = array (
  214. '#title' => t('URL for data files'),
  215. '#type' => t('textfield'),
  216. '#description' => t("This is the base URL location (without a leading forward slash) for where files (e.g. blast .xml files) related to each feature are stored. All files available for download or parsing that a feature needs for display should be located in this base directory."),
  217. '#required' => TRUE,
  218. '#default_value' => variable_get('chado_feature_data_url','sites/default/files/data'),
  219. );
  220. return system_settings_form($form);
  221. }
  222. /**
  223. * Set the Tripal Database
  224. *
  225. * The tripal_db_set_active function is used to prevent namespace collisions
  226. * when chado and drupal are installed in the same database but in different
  227. * schemas. It is also used for backwards compatibility with older versions
  228. * of tripal or in cases where chado is located outside of the Drupal database.
  229. *
  230. * @ingroup tripal_core
  231. */
  232. function tripal_db_set_active($dbname){
  233. global $db_url, $db_type;
  234. $chado_exists = 0;
  235. // only postgres can support search paths. So if this is MysQL then
  236. // just run the normal tripal_db_set_active function.
  237. if(strcmp($db_type,'pgsql')==0){
  238. // if the 'chado' database is in the $db_url variable then chado is
  239. // not in the same Drupal database
  240. if(is_array($db_url)){
  241. if(isset($db_url[$dbname])){
  242. return db_set_active($dbname);
  243. }
  244. }
  245. // check to make sure the chado schema exists
  246. $sql = "select nspname from pg_catalog.pg_namespace where nspname = 'chado'";
  247. if(db_fetch_object(db_query($sql))){
  248. $chado_exists = 1;
  249. }
  250. // here we make the assumption that the default database schema is
  251. // 'public'. This will most likely always be the case but if not,
  252. // then this code will break
  253. if($chado_exists && strcmp($dbname,'chado')==0){
  254. db_query("set search_path to %s",'chado,public');
  255. return 'public,chado';
  256. }
  257. elseif($chado_exists) {
  258. db_query("set search_path to %s",'public,chado');
  259. return 'chado,public';
  260. }
  261. else {
  262. return db_set_active($dbname);
  263. }
  264. }
  265. else return db_set_active($dbname);
  266. }
  267. /**
  268. *
  269. *
  270. * @ingroup tripal_core
  271. */
  272. function tripal_core_is_chado_installed(){
  273. global $db_url, $db_type;
  274. // first check if chado is in the db_url of the
  275. // settings.php file
  276. if(is_array($db_url)){
  277. if(isset($db_url['chado'])){
  278. return true;
  279. }
  280. }
  281. // check to make sure the chado schema exists
  282. $sql = "select nspname from pg_catalog.pg_namespace where nspname = 'chado'";
  283. if(db_fetch_object(db_query($sql))){
  284. return true;
  285. }
  286. return false;
  287. }
  288. /**
  289. * Implements hook_views_api()
  290. *
  291. * Purpose: Essentially this hook tells drupal that there is views support for
  292. * for this module which then includes tripal_core.views.inc where all the
  293. * views integration code is
  294. *
  295. * @ingroup tripal_core
  296. */
  297. function tripal_core_views_api() {
  298. return array(
  299. 'api' => 2.0,
  300. );
  301. }
  302. /**
  303. * Purpose: Get max rank for a given set of criteria
  304. * This function was developed with the many property tables in chado in mind
  305. *
  306. * @param $tablename
  307. * The name of the chado table you want to select the max rank from this table must contain a
  308. * rank column of type integer
  309. * @param $where_options
  310. * where options should include the id and type for that table to correctly
  311. * group a set of records together where the only difference are the value and rank
  312. * @code
  313. * array(
  314. * <column_name> => array(
  315. * 'type' => <type of column: INT/STRING>,
  316. * 'value' => <the value you want to filter on>,
  317. * 'exact' => <if TRUE use =; if FALSE use ~>,
  318. * )
  319. * )
  320. * @endcode
  321. * @return the maximum rank
  322. *
  323. * @ingroup tripal_core
  324. */
  325. function tripal_get_max_chado_rank ($tablename, $where_options) {
  326. $where= array();
  327. //generate the where clause from supplied options
  328. // the key is the column name
  329. foreach ($where_options as $key => $val_array) {
  330. if (preg_match('/INT/', $val_array['type'])) {
  331. $where[] = $key."=".$val_array['value'];
  332. } else {
  333. if ($val_array['exact']) { $operator='='; }
  334. else { $operator='~'; }
  335. $where[] = $key.$operator."'".$val_array['value']."'";
  336. }
  337. }
  338. $previous_db = tripal_db_set_active('chado');
  339. $result = db_fetch_object(db_query(
  340. "SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
  341. $tablename,
  342. implode(' AND ',$where)
  343. ));
  344. tripal_db_set_active($previous_db);
  345. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  346. if ($result->count > 0) {
  347. return $result->max_rank;
  348. } else {
  349. return -1;
  350. }
  351. }
  352. function tripal_core_theme () {
  353. return array(
  354. 'tripal_core_job_view' => array (
  355. 'arguments' => array('job_id'=> null),
  356. 'template' => 'tripal_core_job_view',
  357. ),
  358. );
  359. }