tripal_core.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_mview/%'] = array(
  120. 'title' => 'Materialized View',
  121. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  122. 'page callback' => 'tripal_mview_report',
  123. 'page arguments' => array(3),
  124. 'access arguments' => array('access administration pages'),
  125. 'type' => MENU_NORMAL_ITEM,
  126. );
  127. $items['admin/tripal/tripal_mviews'] = array(
  128. 'title' => 'Materialized Views',
  129. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  130. 'page callback' => 'tripal_mviews_report',
  131. 'access arguments' => array('access administration pages'),
  132. 'type' => MENU_NORMAL_ITEM,
  133. );
  134. $items['admin/tripal/tripal_mviews/new'] = array(
  135. 'title' => 'Create View',
  136. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  137. 'page callback' => 'drupal_get_form',
  138. 'page arguments' => array('tripal_mviews_form'),
  139. 'access arguments' => array('access administration pages'),
  140. 'type' => MENU_NORMAL_ITEM,
  141. );
  142. $items['admin/tripal/tripal_mviews/edit/%'] = array(
  143. 'title' => 'Edit View',
  144. 'page callback' => 'drupal_get_form',
  145. 'page arguments' => array('tripal_mviews_form',4),
  146. 'access arguments' => array('access administration pages'),
  147. 'type' => MENU_NORMAL_ITEM,
  148. );
  149. $items['admin/tripal/tripal_mviews/action/%/%'] = array(
  150. 'title' => 'Create View',
  151. 'description' => 'Materialized views are used to improve speed of large or complex queries.',
  152. 'page callback' => 'tripal_mviews_action',
  153. 'page arguments' => array(4,5),
  154. 'access arguments' => array('access administration pages'),
  155. 'type' => MENU_CALLBACK,
  156. );
  157. $items['tripal_toggle_box_menu/%/%/%'] = array(
  158. 'title' => t('Libraries'),
  159. 'page callback' => 'tripal_toggle_box_menu',
  160. 'page arguments' => array(1,2,3),
  161. 'access arguments' => array('access administration pages'),
  162. 'type' => MENU_CALLBACK | MENU_LINKS_TO_PARENT
  163. );
  164. $items['admin/tripal/chado_1_11_install'] = array(
  165. 'title' => 'Install Chado v1.11',
  166. 'description' => 'Installs Chado version 1.11 inside the current Drupal database',
  167. 'page callback' => 'drupal_get_form',
  168. 'page arguments' => array('tripal_core_chado_v1_11_load_form'),
  169. 'access arguments' => array('access administration pages'),
  170. 'type' => MENU_NORMAL_ITEM,
  171. );
  172. $items['admin/tripal/bulk_load/create'] = array(
  173. 'title' => 'Create Bulk Loader',
  174. 'description' => 'Create a bulk loader template for loading data into Chado',
  175. 'page callback' => 'tripal_core_bulk_loader_create',
  176. 'access arguments' => array('access administration pages'),
  177. 'type' => MENU_NORMAL_ITEM,
  178. );
  179. $items['admin/tripal/bulk_load/step2_get_type/%'] = array(
  180. 'title' => 'Create Bulk Loader',
  181. 'description' => 'Create a bulk loader template for loading data into Chado',
  182. 'page callback' => 'tripal_core_bulk_loader_ahah_step2_feature_type',
  183. 'page arguments' => array(4),
  184. 'access arguments' => array('access administration pages'),
  185. 'type' => MENU_CALLBACK,
  186. );
  187. $items['test2'] = array(
  188. 'title' => 'test',
  189. 'description' => 'test',
  190. 'page callback' => 'tripal_core_chado_insert_test',
  191. 'access arguments' => array('access administration pages'),
  192. 'type' => MENU_CALLBACK,
  193. );
  194. return $items;
  195. }
  196. /**
  197. * Provides the tripal box menu functionality
  198. *
  199. * The typical display for information on feature, organism, library, etc
  200. * pages is to use the Tripal expandable boxes. However, some sites may
  201. * prefer to use a menu system to keep the pages less cluttered. This
  202. * function provides a common interface for setting a Drupal variable
  203. * that indicates whether or not the content is displayed in a box or as
  204. * a menu item. This function just reverses the setting each time it is
  205. * called
  206. *
  207. * @ingroup tripal_core
  208. */
  209. function tripal_toggle_box_menu($module,$box_name,$nid){
  210. // if the content is not in a menu then we wnat to turn on the
  211. // menu. If the content is in a menu item then we want to turn
  212. // on the box.
  213. if(strcmp(variable_get("$module-box-$box_name","menu_off"),"menu_off")==0){
  214. variable_set("$module-box-$box_name","menu_on");
  215. } else {
  216. variable_set("$module-box-$box_name","menu_off");
  217. }
  218. drupal_goto("node/$nid");
  219. }
  220. /**
  221. *
  222. *
  223. * @ingroup tripal_core
  224. */
  225. function tripal_core_admin () {
  226. $form['chado_feature_data_url'] = array (
  227. '#title' => t('URL for data files'),
  228. '#type' => t('textfield'),
  229. '#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."),
  230. '#required' => TRUE,
  231. '#default_value' => variable_get('chado_feature_data_url','sites/default/files/data'),
  232. );
  233. return system_settings_form($form);
  234. }
  235. /**
  236. * Set the Tripal Database
  237. *
  238. * The tripal_db_set_active function is used to prevent namespace collisions
  239. * when chado and drupal are installed in the same database but in different
  240. * schemas. It is also used for backwards compatibility with older versions
  241. * of tripal or in cases where chado is located outside of the Drupal database.
  242. *
  243. * @ingroup tripal_core
  244. */
  245. function tripal_db_set_active($dbname){
  246. global $db_url, $db_type;
  247. $chado_exists = 0;
  248. // only postgres can support search paths. So if this is MysQL then
  249. // just run the normal tripal_db_set_active function.
  250. if(strcmp($db_type,'pgsql')==0){
  251. // if the 'chado' database is in the $db_url variable then chado is
  252. // not in the same Drupal database
  253. if(is_array($db_url)){
  254. if(isset($db_url[$dbname])){
  255. return db_set_active($dbname);
  256. }
  257. }
  258. // check to make sure the chado schema exists
  259. $sql = "select nspname from pg_catalog.pg_namespace where nspname = 'chado'";
  260. if(db_fetch_object(db_query($sql))){
  261. $chado_exists = 1;
  262. }
  263. // here we make the assumption that the default database schema is
  264. // 'public'. This will most likely always be the case but if not,
  265. // then this code will break
  266. if($chado_exists && strcmp($dbname,'chado')==0){
  267. db_query("set search_path to %s",'chado,public');
  268. return 'public,chado';
  269. }
  270. elseif($chado_exists) {
  271. db_query("set search_path to %s",'public,chado');
  272. return 'chado,public';
  273. }
  274. else {
  275. return db_set_active($dbname);
  276. }
  277. }
  278. else return db_set_active($dbname);
  279. }
  280. /**
  281. *
  282. *
  283. * @ingroup tripal_core
  284. */
  285. function tripal_core_is_chado_installed(){
  286. global $db_url, $db_type;
  287. // first check if chado is in the db_url of the
  288. // settings.php file
  289. if(is_array($db_url)){
  290. if(isset($db_url['chado'])){
  291. return true;
  292. }
  293. }
  294. // check to make sure the chado schema exists
  295. $sql = "select nspname from pg_catalog.pg_namespace where nspname = 'chado'";
  296. if(db_fetch_object(db_query($sql))){
  297. return true;
  298. }
  299. return false;
  300. }
  301. /**
  302. * Implements hook_views_api()
  303. *
  304. * Purpose: Essentially this hook tells drupal that there is views support for
  305. * for this module which then includes tripal_core.views.inc where all the
  306. * views integration code is
  307. *
  308. * @ingroup tripal_core
  309. */
  310. function tripal_core_views_api() {
  311. return array(
  312. 'api' => 2.0,
  313. );
  314. }
  315. /**
  316. * Purpose: Get max rank for a given set of criteria
  317. * This function was developed with the many property tables in chado in mind
  318. *
  319. * @param $tablename
  320. * The name of the chado table you want to select the max rank from this table must contain a
  321. * rank column of type integer
  322. * @param $where_options
  323. * where options should include the id and type for that table to correctly
  324. * group a set of records together where the only difference are the value and rank
  325. * @code
  326. * array(
  327. * <column_name> => array(
  328. * 'type' => <type of column: INT/STRING>,
  329. * 'value' => <the value you want to filter on>,
  330. * 'exact' => <if TRUE use =; if FALSE use ~>,
  331. * )
  332. * )
  333. * @endcode
  334. * @return the maximum rank
  335. *
  336. * @ingroup tripal_core
  337. */
  338. function tripal_get_max_chado_rank ($tablename, $where_options) {
  339. $where= array();
  340. //generate the where clause from supplied options
  341. // the key is the column name
  342. foreach ($where_options as $key => $val_array) {
  343. if (preg_match('/INT/', $val_array['type'])) {
  344. $where[] = $key."=".$val_array['value'];
  345. } else {
  346. if ($val_array['exact']) { $operator='='; }
  347. else { $operator='~'; }
  348. $where[] = $key.$operator."'".$val_array['value']."'";
  349. }
  350. }
  351. $previous_db = tripal_db_set_active('chado');
  352. $result = db_fetch_object(db_query(
  353. "SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
  354. $tablename,
  355. implode(' AND ',$where)
  356. ));
  357. tripal_db_set_active($previous_db);
  358. //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
  359. if ($result->count > 0) {
  360. return $result->max_rank;
  361. } else {
  362. return -1;
  363. }
  364. }