tripal_core.module 13 KB

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