tripal_core.module 12 KB

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