tripal_core.module 13 KB

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