tripal_core.module 13 KB

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