tripal_gbrowse.module 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320
  1. <?php
  2. // $Id$
  3. /**
  4. * @file
  5. * This file contains all the functions which provide functionality to this module.
  6. * This module was developed by Chad N.A. Krilow and Lacey-Anne Sanderson,
  7. * University of Saskatchewan.
  8. *
  9. * The GBrowse manamgenet module allows you to sync data in a chado/Tripal instance with
  10. * multiple GBrowse/mysql instances as well as manage and create such GBrowse instances
  11. */
  12. //-----------------------------------------------------------------------------
  13. // SECTION: Main Outline Menu for GBrowse Management
  14. //-----------------------------------------------------------------------------
  15. /**
  16. * Tripal-GBrowse-Menu
  17. *
  18. * Implemets hook_menu(): Adds menu items for the tripal_gbrowse module menu. This section
  19. * gives the outline for the main menu of the Tripal-GBrowse module
  20. *
  21. * @return
  22. * An array of menu items that is visible within the Drupal Menu, returned as soon
  23. * as the program is ran
  24. */
  25. function tripal_gbrowse_menu() {
  26. $items = array();
  27. $items[ 'admin/tripal/tripal_gbrowse' ]= array(
  28. 'title' => t('GBrowse Management'),
  29. 'description' => ('GBrowse management allows a user to create a database, register
  30. a GBrowse instance and to load/delete feature libraries'),
  31. 'page callback' => 'tripal_gbrowse_administration_description_page',
  32. 'access arguments' => array('administer site configuration'),
  33. 'type' => MENU_NORMAL_ITEM
  34. );
  35. $items['admin/tripal/tripal_gbrowse/configuration'] = array(
  36. 'title' => t('Configuration'),
  37. 'description' => t('Configuration for this module'),
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('tripal_gbrowse_administration_form'),
  40. 'access arguments' => array('administer site configuration'),
  41. 'type' => MENU_NORMAL_ITEM
  42. );
  43. $items['admin/tripal/tripal_gbrowse/load_features'] = array(
  44. 'title' => t('Load Features'),
  45. 'description' => t('Load features associated with a specified library into a GBrowse instance.'),
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('tripal_gbrowse_load_features_form'),
  48. 'access arguments' => array('administer site configuration'),
  49. 'type' => MENU_NORMAL_ITEM
  50. );
  51. $items['admin/tripal/tripal_gbrowse/delete_features']=array(
  52. 'title' => t('Delete Features'),
  53. 'description' => t('Interface to delete features of a given library from a GBrowse Instance'),
  54. 'page callback' => 'drupal_get_form',
  55. 'page arguments' => array('tripal_gbrowse_delete_library_features_from_gbrowse_form'),
  56. 'access arguments' => array('administer site configuration'),
  57. 'type' => MENU_NORMAL_ITEM
  58. );
  59. $items['admin/tripal/tripal_gbrowse/add_tracks']=array(
  60. 'title' => t('Add Tracks'),
  61. 'description' => t('Interface for Adding tracks to a GBrowse Instance'),
  62. 'page callback' => 'drupal_get_form',
  63. 'page arguments' => array('tripal_gbrowse_add_tracks_form'),
  64. 'access arguments' => array('administer site configuration'),
  65. 'type' => MENU_NORMAL_ITEM
  66. );
  67. $items['admin/tripal/tripal_gbrowse/delete_tracks']=array(
  68. 'title' => t('Delete Tracks'),
  69. 'description' => t('Interface for Deleting tracks from a GBrowse Instance'),
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('tripal_gbrowse_delete_tracks_form'),
  72. 'access arguments' => array('administer site configuration'),
  73. 'type' => MENU_NORMAL_ITEM
  74. );
  75. return $items;
  76. }
  77. /**
  78. * Implements hook_theme(): Register themeing functions for this module
  79. *
  80. * Contains the seperate tripal_gbrowse_X.tpl.php files, so as they can be included in the
  81. * theme for the designed module
  82. *
  83. * @return
  84. * An array of themeing functions to register
  85. *
  86. */
  87. function tripal_gbrowse_theme() {
  88. return array(
  89. // Block Templates------------------------------
  90. 'tripal_gbrowse_database_details' => array (
  91. 'arguments' => array('node'=> null),
  92. 'template' => 'tripal_gbrowse_database_details',
  93. ),
  94. 'tripal_gbrowse_details' => array (
  95. 'arguments' => array('node'=> null),
  96. 'template' => 'tripal_gbrowse_details',
  97. ),
  98. 'tripal_gbrowse_gbrowse_instance' => array (
  99. 'arguments' => array('node'=> null),
  100. 'template' => 'tripal_gbrowse_gbrowse_instance',
  101. ),
  102. 'tripal_gbrowse_teaser' => array (
  103. 'arguments' => array('node'=> null),
  104. 'template' => 'tripal_gbrowse_teaser',
  105. ),
  106. 'tripal_gbrowse_loaded_libraries' => array (
  107. 'arguments' => array('node'=> null),
  108. 'template' => 'tripal_gbrowse_loaded_libraries',
  109. ),
  110. );
  111. }
  112. /**
  113. * Purpose: Implement Blocks relating to Tripal GBrowse content
  114. *
  115. * @param $op
  116. * What kind of information to retrieve about the block or blocks.
  117. * Possible values include list, configure, save, view.
  118. * @param $delta
  119. * Which block to return (not applicable if $op is 'list').
  120. * @param $edit
  121. * If $op is 'save', the submitted form data from the configuration form.
  122. *
  123. * @return
  124. * One of the following depending on $op: An array of block descriptions (list), the configuration
  125. * form (configure), nothing (save), an array defining subject and content for the block indexed
  126. * by $delta (view)
  127. *
  128. */
  129. function tripal_gbrowse_block ($op = 'list', $delta = 0, $edit=array()) {
  130. switch($op) {
  131. case 'list':
  132. $blocks['database_details']['info'] = t('Tripal GBrowse Database Details');
  133. $blocks['database_details']['cache'] = BLOCK_NO_CACHE;
  134. $blocks['details']['info'] = t('Tripal GBrowse Details');
  135. $blocks['details']['cache'] = BLOCK_NO_CACHE;
  136. $blocks['gbrowse_instance']['info'] = t('Tripal GBrowse Instance');
  137. $blocks['gbrowse_instance']['cache'] = BLOCK_NO_CACHE;
  138. $blocks['teaser']['info'] = t('Tripal GBrowse Teaser');
  139. $blocks['teaser']['cache'] = BLOCK_NO_CACHE;
  140. // $blocks['teaser']['info'] = t('Tripal GBrowse Libraries');
  141. // $blocks['teaser']['cache'] = BLOCK_NO_CACHE;
  142. return $blocks;
  143. case 'view':
  144. if(user_access('access tripal_gbrowse content') and arg(0) == 'node' and is_numeric(arg(1))) {
  145. $nid = arg(1);
  146. $node = node_load($nid);
  147. $block = array();
  148. switch($delta){
  149. case 'database_details':
  150. $block['subject'] = t('Tripal GBrowse Database Details');
  151. $block['content'] = theme('tripal_gbrowse_database_details',$node);
  152. break;
  153. case 'details':
  154. $block['subject'] = t('Tripal GBrowse Details');
  155. $block['content'] = theme('tripal_gbrowse_details',$node);
  156. break;
  157. case 'gbrowse_instance':
  158. $block['subject'] = t('Tripal GBrowse Instance');
  159. $block['content'] = theme('tripal_gbrowse_gbrowse_instance',$node);
  160. break;
  161. case 'teaser':
  162. $block['subject'] = t('Tripal GBrowse Teaser');
  163. $block['content'] = theme('tripal_gbrowse_teaser',$node);
  164. break;
  165. // case 'teaser':
  166. // $block['subject'] = t('Tripal GBrowse Libraries');
  167. // $block['content'] = theme('tripal_gbrowse_loaded_libraries',$node);
  168. // break;
  169. }
  170. return $block;
  171. }
  172. }
  173. }
  174. /**
  175. * Implements hook_views_api()
  176. * Purpose: Essentially this hook tells drupal that there is views support
  177. * for this module which then includes tripal_db.views.inc where all the
  178. * views integration code is
  179. */
  180. function tripal_gbrowse_views_api() {
  181. return array(
  182. 'api' => 2.0,
  183. );
  184. }
  185. /**
  186. * This section uses HTML to output the descriptions of the module, through the Tripal Management
  187. * Administation window.Any installation instructions are given as well as included features
  188. * as well as a overview of the purpose Module.
  189. *
  190. * @return
  191. * Returns '$text'The HTML description of the Module, its instructions, features, and any other
  192. * important aspects. This is returned when the Administration page is selected.
  193. */
  194. function tripal_gbrowse_administration_description_page() {
  195. $text = ' ';
  196. $text .= '</p><h3>GBrowse Feature Administrative Tools Quick Links:</h3>';
  197. $text .= "<ul>";
  198. $text .= "<li>".l("Load Features", "admin/tripal/tripal_gbrowse/load_features"). "</li>";
  199. $text .= "<li>".l("Configuration", "admin/tripal/tripal_gbrowse/configuration"). "</li>";
  200. $text .= "<li>".l("Add Tracks","admin/tripal/tripal_gbrowse/job_sumit"). "</li>";
  201. $text .= "<li>".l("Delete Tracks","admin/tripal/tripal_gbrowse/delete_features"). "</li>";
  202. $text .= "</ul>";
  203. $text .= '<h3>Module Description:</h3>';
  204. $text .= '<p>This module provides an interface to register already existing GBrowse instances with Drupal/Tripal and/or create new '
  205. .'GBrowse Instances. Furthermore, it allows basic management of these GBrowse instances including un-registering and/or '
  206. .'deleting and loading of features in Tripal/Chado into a MySQL GBrowse instance. As such, it provides interoperability '
  207. .'and syncronization between a Tripal/Chado installation and GBrowse</p>';
  208. $text .= '<h3>Setup Instructions:</h3>';
  209. $text .= '<ol>';
  210. $text .= '<li><b>Ensure the PHP-MySQL extension is installed:</b> Installation instructions are dependant upon your operating system.</li>';
  211. $text .= '<li><b>Move Template Configuration File:</b> Move the template.conf file included with this module into the GBrowse configuration file directory.</li>';
  212. $text .= '<li><b>Register Existing GBrowse Instances</b>: Register any existing GBrowse instances through the form provided at '
  213. .'<a href="tripal_gbrowse/register_gbrowse">Administration -> Tripal Management -> GBrowse Management -> Register GBrowse Instance</a>.</li>';
  214. $text .= '<li><b>Load desired Feature Libraries into GBrowse Instances</b>: Additional feature libraries should be loaded through the form provided at '.l('Administration -> Tripal Management -> GBrowse Management -> Load Features','admin/tripal/tripal_gbrowse/load_features').'</li>';
  215. $text .= '<ol>';
  216. $text .= '<h3>Features of this Module:</h3>';
  217. $text .= '<ul>';
  218. $text .= '<li><b>Register GBrowse Instance:</b> In order to load features into a GBrowse instance you need to first register it with Tripal/Drupal. Any existing '
  219. .'GBrowse instance that utilizes a MySQL database can be registered with the module. Furthermore, you can create new GBrowse Instances '
  220. .'through this module by selecting that option in the register GBrowse Instance form. This'
  221. .'newly created GBrowse instance uses a MySQL database with is created and '
  222. .'a user name and password is setup and a default configuration file is created. Finally, the database is initialized and the first sequence '
  223. .'set is loaded into the GBrowse instance.</li>';
  224. $text .= '<li><b>Un-Register/Delete GBrowse Instance:</b> Un-registering a GBrowse instance alone will remove the GBrowse instance from the Drupal site.'
  225. .'When removing the entire instance, the database and the configuration file will be deleted. This cannot be undone!</li>';
  226. $text .='<li><b>Intergrated GBrowse Node/Page:</b> Allows users to easily access GBrowse instances that have been created.'
  227. .'This module provides details for a registered GBrowse Instance, including an I-Frame, providing instance access to the GBrowse display,'
  228. .'as well,the module provides a link to a GBrowse instance in a new window.</li>';
  229. $text .= '<li><b>Load Feature (Libraries) Libraries:</b> Simply by selecting a Tripal Library and a GBrowse Instance, '
  230. .'the features associated with the selected Library are loaded into the selected GBrowse Instance. Libraries can also be updated by selecting that option. This removes all of the pre-existing'
  231. .'features associated with that library, then reloades them. Both loading and updating utilize the Perl script "bp_seqfeature_load.pl" which should be installed when you install GBrowse.</li>';
  232. $text .= '<li><b>Delete Feature (Libraries) Libraries:</b> A user can select a desired Tripal Library and a specific GBrowse instance and all of the features associated with that library in the selected isntance are removed.'
  233. .' This section uses the Perl "script bp_seqfeature_delete.pl" to delete the specified features.</li>';
  234. $text .= '<li><b>Add Tracks:</b> Allows a user to add tracks to a given GBrowse instance. This allows them to define exactly how they want their features to be displayed, as well as, to separate their features into their own GBrowse track.</li>';
  235. $text .= '<li><b>Delete Tracks:</b> Allows a user to delete default tracks or user created tracks for a given GBrowse instance.</li>';
  236. $text .= '</ul>';
  237. return $text;
  238. }
  239. /**
  240. * Implements hook_job_describe_args()
  241. *
  242. * This function modifies the Job Arguments page and represents the job arguments with
  243. * meaningfull descriptive names, sends a link to the library and shows if the database
  244. * was cleared or not.
  245. *
  246. * @param $call_back
  247. * The name of the function being called by tripal_jobs
  248. *
  249. * @param $args
  250. * The arguments that are passed, given if the selected proccess is
  251. * 1-'tripal_gbrowse_delete_features_from_gbrowse'
  252. * 2-'tripal_gbrowse_load_features_into_gbrowse'
  253. * If 1- then the arguments are based on deleting a feature from GBrowse.
  254. * If 2- then the arguments are based on loading features into GBrowse.
  255. *
  256. * @return
  257. * Job arguments:Database name, user Name, Password, Library ID, If GBrowse database was cleared,
  258. * to be displayed in Job Details page.
  259. */
  260. function tripal_gbrowse_job_describe_args($call_back, $args){
  261. switch ($call_back){
  262. case 'tripal_gbrowse_delete_features_from_gbrowse':
  263. $new_args['GBrowse Database Name']=$args[0];
  264. $new_args['User Name']=$args[1];
  265. $new_args['Password']=$args[2];
  266. $result = db_fetch_object(db_query("SELECT nid FROM chado_library WHERE library_id=%d",$args[3]));
  267. $node = node_load($result->nid);
  268. $new_args['Library']= l($node->title, 'node/'.$result->nid);
  269. if(empty($args[4])){
  270. $new_args['Clear GBrowse Database']= 'No';
  271. }else{
  272. $new_args['Clear GBrowse Database']= 'Yes';
  273. }
  274. break;
  275. case 'tripal_gbrowse_load_features_into_gbrowse':
  276. $new_args['GBrowse Database Name']=$args[0];
  277. $new_args['User Name']=$args[1];
  278. $new_args['Password']=$args[2];
  279. $result = db_fetch_object(db_query("SELECT nid FROM chado_library WHERE library_id=%d",$args[3]));
  280. $node = node_load($result->nid);
  281. $new_args['Library']= l($node->title, 'node/'.$result->nid);
  282. if(empty($args[4])){
  283. $new_args['Clear GBrowse Database']= 'No';
  284. }else{
  285. $new_args['Clear GBrowse Database']= 'Yes';
  286. }
  287. break;
  288. }
  289. return $new_args;
  290. }
  291. //-----------------------------------------------------------------------------
  292. // SECTION: Database API
  293. //-----------------------------------------------------------------------------
  294. /**
  295. * The purpose of this function is to retrieve the previousley loaded libraries from database.
  296. *
  297. * @param $gbrowse
  298. * A GBrowse instance that contains the information of the current instance
  299. *
  300. * @return
  301. * An array of libraries that have been loaded into the database
  302. */
  303. function tripal_gbrowse_getloaded_libraries($gbrowse){
  304. $loaded_libraries = array();
  305. $link = mysql_connect('localhost', $gbrowse->database_user, $gbrowse->user_password);
  306. if (!$link) {
  307. drupal_set_message('Could not connect: ' . mysql_error(), 'error');
  308. }
  309. //Sending query to the database
  310. $resource = mysql_query('use ' .$gbrowse->database_name);
  311. if (empty($resource)) {
  312. drupal_set_message ('Database is empty or non-existent','error');
  313. }
  314. $resource = mysql_query('select * from typelist');
  315. if (empty($resource)) {
  316. drupal_set_message ('Missing table typelist in database','error');
  317. }
  318. $library_array = array();
  319. while($record = mysql_fetch_object($resource)){
  320. preg_match('/^[^:]+:(.+)/', $record->tag, $matches);
  321. $library_array[$matches[1]]= $matches[1];
  322. }
  323. mysql_close($link);
  324. return $library_array;
  325. }
  326. /**
  327. * The purpose of this function is to firstly create a file for backup purposes. Secondly, this
  328. * function uses a 'mysqldump' the contents of the selected database to a backup file. It then
  329. * access the database, drops the old database, creates a new one with the modified infromation,
  330. * then uploads the previously backed up infromation into the newly created database.
  331. *
  332. *
  333. * @param $node
  334. * A node object containing the data and infromation of the GBrowse instance
  335. *
  336. * @param $old_database
  337. * The name of the originally created database, which will be replaced if the user
  338. * changes the name of the database that they have created
  339. *
  340. */
  341. function tripal_gbrowse_database_modification($node,$old_database){
  342. global $user;
  343. $user_name = $node->database_user;
  344. $user_password = $node->user_password;
  345. //creating a file in the /tmp directory for back-up purposes
  346. $back_upfile = '/tmp/' .$old_database .'_'.time().'.sql';
  347. //Using the 'mysqldump' command to dump the database for backup purposses
  348. $command = 'mysqldump -u' .$user_name .' -p' .$user_password .' ' .$old_database .' > ' .$back_upfile;
  349. exec($command);
  350. //To create a database use the mysql_query() function to execute an SQL query
  351. $link = mysql_connect('localhost', $user_name,$user_password);
  352. //Link operation verification
  353. if (!$link) {
  354. drupal_set_message('Could not connect: ' . mysql_error(), 'error');
  355. return false;
  356. }
  357. $sql = 'DROP DATABASE ' . $old_database;
  358. if(mysql_query($sql, $link)){
  359. drupal_set_message( "Database Dropped successfully\n");
  360. }else{
  361. drupal_set_message( 'Error dropping database: ' . mysql_error(), 'error');
  362. return false;
  363. }
  364. $sql = 'CREATE DATABASE ' . $node->database_name;
  365. if (mysql_query($sql, $link)) {
  366. drupal_set_message( "Database Created Successfully\n");
  367. }
  368. else {
  369. drupal_set_message( 'Error Creating Database: ' . mysql_error() . "\n");
  370. return false;
  371. }
  372. mysql_query( "GRANT SELECT ON " .$node->database_name.".* TO 'www-data'@'localhost'",$link);
  373. $command = 'mysql -u' .$user_name .' -p' .$user_password .' ' .$node->database_name .' < ' .$back_upfile;
  374. exec($command);
  375. mysql_close($link);
  376. }
  377. //-----------------------------------------------------------------------------
  378. // SECTION: Node Capability
  379. //-----------------------------------------------------------------------------
  380. /**
  381. * Implementation of hook_tripal_gbrowse_node_info()
  382. *
  383. * This node_info, is a simple node that describes the functionallity of the module. It specifies
  384. * that the title(gbrowse Name) and body(Description) set to true so that they information can be
  385. * entered
  386. *
  387. */
  388. function tripal_gbrowse_node_info() {
  389. return array(
  390. 'tripal_gbrowse' => array(
  391. 'name' => t('GBrowse Instance'),
  392. 'module' => 'tripal_gbrowse',
  393. 'description' => t('A module for interfacing the GMOD database with Drupal, providing:
  394. Registration,Alteration and Deletion of GBrowse Instances,Configuration Files and Databases'),
  395. 'has_title' => TRUE,
  396. 'title_label' =>t('gbrowse Name'),
  397. 'had_body' => FALSE,
  398. )
  399. );
  400. }
  401. /**
  402. * Implementation of tripal_gbrowse_form().
  403. *
  404. * This form takes the gbrowse Title infromation and the gbrowse description from the user. It
  405. * then puts the infromation into the Chado_gbrowse database table.
  406. *
  407. * @parm &$node
  408. * The node that is created when the database is initialized
  409. *
  410. * @parm $form_state
  411. * The state of the form, that has the user entered information that is neccessary for, setting
  412. * up the database of the gbrowse
  413. *
  414. * @return $form
  415. * The information that was enterd allong with
  416. *
  417. */
  418. function tripal_gbrowse_form(&$node, $form_state) {
  419. $type = node_get_types('type', $node);
  420. //Creating Fieldset for multiple fields in form
  421. $form['register_gbrowse'] = array(
  422. '#type' => 'fieldset',
  423. '#title' => t('Data Base Specific Fields'),
  424. );
  425. //field for the name of the database
  426. $form['register_gbrowse']['database_name'] = array(
  427. '#type' => 'textfield',
  428. '#title' => t('GBrowse Database Name'),
  429. '#size' => 30,
  430. '#maxlength' => 64,
  431. '#description' => t('Desired Database Name Field'),
  432. '#default_value' => isset($node->gbrowse->database_name) ? $node->gbrowse->database_name : '',
  433. '#required' => TRUE,
  434. );
  435. //User of database
  436. $form['register_gbrowse']['database_user'] = array(
  437. '#type' => 'textfield',
  438. '#title' => t('GBrowse User'),
  439. '#size' => 30,
  440. '#maxlength' => 64,
  441. '#description' => t('GBrowse MySQL User Field'),
  442. '#default_value' => isset($node->gbrowse->database_user) ? $node->gbrowse->database_user : '',
  443. '#required' => TRUE,
  444. );
  445. //Specified password for access to database
  446. $form['register_gbrowse']['user_password'] = array(
  447. '#type' => 'password',
  448. '#title' => t('Password'),
  449. '#size' => 30,
  450. '#maxlength' => 64,
  451. '#description' => t('User Password Field'),
  452. '#default_value' => isset($node->gbrowse->user_password) ? $node->gbrowse->user_password : '',
  453. '#required' => TRUE,
  454. );
  455. //Creating Fieldset for multiple fields in form
  456. $form['gbrowse_details'] = array(
  457. '#type' => 'fieldset',
  458. '#title'=>'GBrowse Specifications' ,
  459. );
  460. //Human-readable name for the gbrowse instance
  461. $form['gbrowse_details']['title'] = array(
  462. '#type' => 'textfield',
  463. '#title' => t('GBrowse Name'),
  464. '#size' => 30,
  465. '#maxlength' => 64,
  466. '#description' => t('GBrowse Instance'),
  467. '#default_value' => isset($node->gbrowse->gbrowse_name) ? $node->gbrowse->gbrowse_name : '',
  468. '#required' => TRUE,
  469. );
  470. //$_SERVER: takes the current url and sets to variable
  471. $default_value = variable_get('tripal_gbrowse_gbrowse_address_stub', '');
  472. //Link to the gbrowse instance
  473. $form['gbrowse_details']['gbrowse_link'] = array(
  474. '#type' => 'textfield',
  475. '#title' => t('GBrowse Link'),
  476. '#size' => 30,
  477. '#maxlength' => 64,
  478. '#description'=>t('Enter Full HTML address Including Host
  479. (ex:http://knowpulse2.usask.ca/) of the new GBrowse instance. By default this is '.$default_value.'[configuration file name] where the configuration file name is the same as that entered below except without .conf'),
  480. '#default_value' => isset($node->gbrowse->gbrowse_link) ? $node->gbrowse->gbrowse_link : $default_value,
  481. '#required' => TRUE,
  482. );
  483. //Fully qualified location to the gbrowse configuration file
  484. $form['gbrowse_details']['config_file'] = array(
  485. '#type' => 'textfield',
  486. '#title' => t('Configuration File'),
  487. '#size' => 30,
  488. '#maxlength' => 64,
  489. '#description' => t('Enter Fully Qualified File Name including Path (ex: file.conf) '),
  490. '#default_value' => isset($node->gbrowse->config_file) ? $node->gbrowse->config_file : variable_get('tripal_gbrowse_configuration_file_path', ''),
  491. '#required' => TRUE,
  492. );
  493. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  494. foreach ($result as $value) {
  495. $newArray[$value->library_id]=$value->name; //options for the select list
  496. }
  497. if (!$node->gbrowse->gbrowse_id) {
  498. // Creating Fieldset for multiple fields in form
  499. $form['create_instance'] = array(
  500. '#type' => 'fieldset',
  501. '#title' => t('Create New GBrowse Instance'),
  502. );
  503. if(empty($newArray)){
  504. drupal_set_message('There are currently no libraries to load.','error');
  505. $form['create_instance']['check_box'] = array(
  506. '#type' => 'item',
  507. '#value'=>'A GBrowse instance can not be created without a valid loadable backbone.
  508. To make a valid loadable backbone available, please load features grouped in a library using
  509. any of the feature loaders available.'
  510. );
  511. }else {
  512. //A box if checked creates GBrowse Instances if they do not exist
  513. $form['create_instance']['check_box'] = array(
  514. '#type' => 'checkbox',
  515. '#title' => t('Create Instance'),
  516. '#description' => t('Create GBrowse Instane Unless Previous Existance'),
  517. );
  518. //Library ID
  519. $form['create_instance']['library_id'] = array(
  520. '#type' => 'select',
  521. '#title' => t('Sequence BackBone'),
  522. '#options' => $newArray,
  523. '#description' => t('Sequence Features Locations'),
  524. );
  525. }
  526. }
  527. return $form;
  528. }
  529. /**
  530. * Implements tripal_gbrowse_help()
  531. *
  532. * This function simply states, in HTML tags, the creator of the the module and the contact
  533. * for the programmer
  534. *
  535. * @parm $path
  536. * The absolute path of the module and help information
  537. *
  538. * @parm $arg
  539. * The argument
  540. */
  541. function tripal_gbrowse_help($path, $arg) {
  542. switch ($path) {
  543. case 'admin/help#tripal_gbrowse':
  544. return '<p>'. t('Module created by:Chad Krilow (e-mail:cnk046@mail.usask.ca)') .'</p>';
  545. break;
  546. }
  547. }
  548. /**
  549. * Implements hook_perm()
  550. *
  551. * This function sets the permission for the user to access the information in the database.
  552. * This includes creating, inserting, deleting and updating of information in the database
  553. *
  554. */
  555. function tripal_gbrowse_perm(){
  556. return array('create tripal_gbrowse', 'edit own tripal_gbrowse', 'access database-related details' ,'access tripal_gbrowse content');
  557. }
  558. /**
  559. * Implements tripal_gbrowse_access()
  560. *
  561. * This function sets the access permission for operations on the database.
  562. *
  563. *
  564. * @parm $op
  565. * The operation that is to be performed
  566. *
  567. * @parm $node
  568. * The specific node that is to have the operation performed
  569. *
  570. * @parm $account
  571. * The account of the user that is performing the operations
  572. *
  573. * @return
  574. * True if a operation was performed
  575. *
  576. */
  577. function tripal_gbrowse_access($op, $node, $account) {
  578. if($op == 'create') {
  579. // Only users with permission to do so may create this node type.
  580. return user_access('create tripal_gbrowse', $account);
  581. }
  582. // Users who create a node may edit or delete it later, assuming they have the
  583. // necessary permissions.
  584. if($op == 'update' || $op == 'delete') {
  585. if(user_access('edit own tripal_gbrowse',$account) && ($account->uid == $node->uid)) {
  586. return TRUE;
  587. }
  588. }
  589. }
  590. /**
  591. * Implementation of tripal_gbrowse_insert()
  592. *
  593. * This function inserts user entered information pertaining to the GBrowse instance into the
  594. * 'tripal_gbrowse_instances' talble of the database, requiring that a 'Check Box' has been selected.
  595. *
  596. * @parm $node
  597. * Then node which contains the information stored within the node-ID
  598. *
  599. *
  600. *
  601. */
  602. function tripal_gbrowse_insert($node) {
  603. $values = array(
  604. 'database_name'=> $node->database_name,
  605. 'database_user'=> $node->database_user,
  606. 'user_password'=> $node->user_password,
  607. 'gbrowse_name'=> $node->title,
  608. 'gbrowse_link'=> $node->gbrowse_link,
  609. 'config_file'=> $node->config_file,
  610. 'library_id'=>$node->library_id,
  611. 'nid'=>$node->nid,
  612. 'vid'=>$node->vid,
  613. );
  614. //used to write to a database table described by Drupal Schema (not workable for chado)
  615. drupal_write_record('tripal_gbrowse_instances', $values);
  616. if($node->check_box==1){
  617. tripal_gbrowse_create_gbrowse_instance($values);
  618. }else{
  619. drupal_set_message("Create instance check box not selected, can not initialize instance");
  620. }
  621. }
  622. /**
  623. * Implementation of tripal_gbrowse_delete().
  624. *
  625. * This function takes a node and if the variable 'tripal_gbrowse_delete_all' has been defined,
  626. * the GBrowse instance pertaining to the node passed into the function is deleted. Following,
  627. * given the node-ID, the instance will be deleted from the 'tripal_gbrowse_instances' table.
  628. *
  629. *
  630. * @parm $node
  631. * Then node which contains the information stored within the node-ID
  632. *
  633. */
  634. function tripal_gbrowse_delete($node){
  635. if(variable_get('tripal_gbrowse_delete_all', '')){
  636. tripal_gbrowse_delete_gbrowse_instance($node->gbrowse);
  637. }
  638. //deleteing in drupal chado_gbrowse table
  639. db_query('DELETE FROM {tripal_gbrowse_instances} WHERE nid = %d', $node->nid);
  640. }
  641. /*
  642. *
  643. * Implements hook_update
  644. *
  645. * The purpose of the function is to allow the module to take action when an edited node is being
  646. * updated in the database. It also updates any name changes to the configuration file that was
  647. * created upon registering a GBrowse instance. This is done by moving the contents of the original
  648. * configuration file to the newly named file.As well, the database will be changed, if the name
  649. * is altered by a registered user.If this is the case, the function 'tripal_gbrowse_database_modification'
  650. * is called and the node and the old database infromation is passed, so as it may be altered and updated
  651. * accordingley.
  652. *
  653. * @param $node
  654. * The node being updated
  655. *
  656. */
  657. function tripal_gbrowse_update($node){
  658. //select from table before doing this, cause i wont be able to see the old details afterwards
  659. $old_data = db_fetch_object(
  660. db_query('SELECT database_name,config_file FROM {tripal_gbrowse_instances} WHERE nid=%d AND vid=%d',
  661. $node->nid, $node->vid));
  662. $old_config_file=$old_data->config_file;
  663. $old_database_name=$old_data->database_name;
  664. $match= array(
  665. 'nid',
  666. 'vid',
  667. );
  668. $values = array(
  669. 'database_name' => $node->database_name,
  670. 'database_user' => $node->database_user,
  671. 'user_password' => $node->user_password,
  672. 'gbrowse_name' => $node->gbrowse_name,
  673. 'gbrowse_link' => $node->gbrowse_link,
  674. 'config_file' => $node->config_file,
  675. 'library_id'=>$node->library_id,
  676. 'nid' => $node->nid,
  677. 'vid' => $node->vid,
  678. );
  679. //Pulling out the configuration file
  680. $config_file = $node->config_file;
  681. if(strcmp($old_config_file, $config_file)!=0){
  682. //Removing configuration file from the directory
  683. $command = "mv " .$old_config_file ." ".$config_file;
  684. exec($command);
  685. }
  686. if($old_database_name != $node->database_name){
  687. tripal_gbrowse_database_modification($node,$old_database_name);
  688. }
  689. drupal_write_record('tripal_gbrowse_instances', $values, $match);
  690. }
  691. /**
  692. * Implementation of tripal_gbrowse_load().
  693. *
  694. *
  695. * @param $node
  696. * The node that is to be accessed from the database
  697. *
  698. * @return $node
  699. * The node that is to be loaded into the database
  700. *
  701. */
  702. function tripal_gbrowse_load($node) {
  703. $result = db_fetch_object(db_query('SELECT gbrowse_id,database_name,database_user,user_password,
  704. gbrowse_name,gbrowse_link,config_file FROM {tripal_gbrowse_instances} WHERE nid=%d AND vid=%d',$node->nid, $node->vid));
  705. $node->gbrowse = $result;
  706. return $node;
  707. }
  708. //-----------------------------------------------------------------------------
  709. // SECTION: Configuration form
  710. //-----------------------------------------------------------------------------
  711. /**
  712. *
  713. * Implemets hook_menu(): Adds menu items for the tripal_gbrowse module
  714. *
  715. * This form creates a specific Configuration File directory and sets a specific
  716. * GBrowse web address for later access
  717. *
  718. * @return
  719. * An array of menu items '$form'
  720. */
  721. function tripal_gbrowse_administration_form () {
  722. //Creating Fieldset for multiple fields in form
  723. $form['configuration_features'] = array(
  724. '#type' => 'fieldset',
  725. '#title' => t('GBrowse Instance-Related Fields'),
  726. );
  727. //Configuration File Directory
  728. $form['configuration_features']['configuration_file'] = array(
  729. '#type' => 'textfield',
  730. '#title' => t('Set Default Configuration File Directory'),
  731. '#size' => 30,
  732. '#maxlength' => 64,
  733. '#description' => t('Specific Configuration File Directory'),
  734. '#default_value' => variable_get('tripal_gbrowse_configuration_file_path', ''),
  735. );
  736. //GBrowse Web Address
  737. $form['configuration_features']['gbrowse_address'] = array(
  738. '#type' => 'textfield',
  739. '#title' => t('Set Default GBrowse Web Address'),
  740. '#size' => 30,
  741. '#maxlength' => 64,
  742. '#description' => t('Desired GBrowse web address'),
  743. '#default_value' => variable_get('tripal_gbrowse_gbrowse_address_stub', ''),
  744. );
  745. //A box if checked creates GBrowse Instances(.conf file and database)
  746. $form['configuration_features']['delete_check_box'] = array(
  747. '#type' => 'checkbox',
  748. '#title' => t('Delete Instance'),
  749. '#description' => t('Delete GBrowse Configuration file and Database '),
  750. '#default_value'=>variable_get('tripal_gbrowse_delete_all', ''),
  751. );
  752. $form['configuration_features']['submit'] = array(
  753. '#type' => 'submit',
  754. '#weight' => 10,
  755. '#value' => t('Submit')
  756. );
  757. return $form;
  758. }
  759. /**
  760. * Validate User-submitted data for Register GBrowse Instances Form
  761. *
  762. * This function validates the user input for each form element in the Register GBrowse
  763. * instances form that is a text field. Validations checked include:
  764. * -parse_url-checks that the url can be parsed
  765. *
  766. * @param $form
  767. * The form that the user submitted and whose input is being validated
  768. * @param $form_state
  769. * An array containing the state of the form being validated including user-input
  770. */
  771. function tripal_gbrowse_administration_form_validate ($form, $form_state) {
  772. if ( !valid_url($form_state['values']['gbrowse_address'])) {
  773. form_set_error('gbrowse_address', 'Not a vaild url');
  774. }
  775. }
  776. /**
  777. *
  778. * Configuration-Form
  779. *
  780. * @param $form
  781. * -The submitted form containing the user entered infromation
  782. * @param $form_state
  783. * -Is the state of the form: i.e what button was pressed, what infromation was entered,etc.
  784. * The key is the 'values'
  785. */
  786. function tripal_gbrowse_administration_form_submit ($form, $form_state) {
  787. if(!preg_match('/\/$/', $form_state['values']['configuration_file'])) {
  788. $form_state['values']['configuration_file'] = $form_state['values']['configuration_file'] . "/";
  789. }
  790. variable_set('tripal_gbrowse_configuration_file_path',$form_state['values']['configuration_file'] );
  791. drupal_set_message('Configuration File Path set to: '.variable_get('tripal_gbrowse_configuration_file_path', ''));
  792. variable_set('tripal_gbrowse_gbrowse_address_stub',$form_state['values']['gbrowse_address'] );
  793. drupal_set_message('GBrowse Default Address set to: '.variable_get('tripal_gbrowse_gbrowse_address_stub', ''));
  794. if($form_state['values']['delete_check_box']==1){
  795. variable_set('tripal_gbrowse_delete_all',$form_state['values']['delete_check_box'] );
  796. drupal_set_message('Selected GBrowse instance will be deleted: '.variable_get('tripal_gbrowse_delete_all', 'NOT SET'));
  797. }
  798. }
  799. //-----------------------------------------------------------------------------
  800. // SECTION: Register-GBrowse-Instance
  801. //-----------------------------------------------------------------------------
  802. /**
  803. *
  804. * Register & Create GBrowse instance
  805. *
  806. * This form submit takes it's fields from the form for registering a GBrowse instance. The 'sed'command
  807. * is used to select certain lines of a template 'conf' file and replaces them with the user specified GBrowse instance
  808. * and data base name. It then removes the temporary file that was created. This function utilizes the MySQL database
  809. * and uses the commands for MySQL to create a database for the registered GBrowse instance, appropriate warnings
  810. * are issued if there are problems connecting/registering/accessing the database.
  811. *
  812. * @param $form
  813. * -The submitted form containing the user entered infromation
  814. * @param $form_state
  815. * This is the state of the form: i.e what button was pressed.The key is
  816. * the name of the fields in database
  817. *
  818. */
  819. function tripal_gbrowse_create_gbrowse_instance($values) {
  820. global $user; //make current user details available so access of user id is available
  821. //checking for existance of configuration file, if allready created a warning is issued
  822. if ( file_exists($values['config_file']) ){
  823. drupal_set_message('Configuration File Already Exists', 'warning');
  824. }else{
  825. $config_path = variable_get('tripal_gbrowse_configuration_file_path', '');
  826. if(empty($config_path)){
  827. drupal_set_message( "Invalid or Non-Existent Configuration Path", 'error');
  828. }
  829. $temp_file = '/tmp/temporary_gbrowse_'.time().'.conf';
  830. // Using 'sed' to select the second line of the template.config substitute <#gbrowse_name>
  831. //for .$values['gbrowse_name']
  832. $command = "sed '2 s/<#gbrowse_name#>/".$values['gbrowse_name']."/' ".$config_path."template.conf > ".$temp_file;
  833. exec($command);
  834. // Using 'sed' to select the fifth line of the template.config substitute <#database_name>
  835. //for .$values['database_name']
  836. $command = "sed '5 s/<#database_name#>/".$values['database_name']."/' ".$temp_file." > ".$values['config_file'];
  837. exec($command);
  838. // Check configuration file is there
  839. if (file_exists($values['config_file'])) {
  840. drupal_set_message('Configuration File created successfully');
  841. } else {
  842. drupal_set_message('Unable to create configuration file','error');
  843. }
  844. //To create a database use the mysql_query() function to execute an SQL query
  845. $link = mysql_connect('localhost', $values['database_user'],$values['user_password']);
  846. if (!$link) {
  847. drupal_set_message('Could not connect: ' . mysql_error());
  848. return false;
  849. }
  850. $sql = 'CREATE DATABASE ' . $values['database_name'];
  851. if (mysql_query($sql, $link)) {
  852. drupal_set_message( "Database created successfully\n");
  853. }
  854. else {
  855. drupal_set_message( 'Error creating database: ' . mysql_error() . "\n");
  856. return false;
  857. }
  858. mysql_query( "GRANT SELECT ON " .$values['database_name'].".* TO 'www-data'@'localhost'",$link);
  859. mysql_close($link);
  860. $job_args=array($values['database_name'],$values['database_user'],$values['user_password'],$values['library_id'],TRUE);
  861. //registering a job with the tripal system, this job calls the function when executed
  862. $job_id = tripal_add_job('Loading Features Into GBrowse', 'tripal_gbrowse','tripal_gbrowse_load_features_into_gbrowse', $job_args, $user->uid);
  863. }
  864. }
  865. //-----------------------------------------------------------------------------
  866. // SECTION: Un-Register Previously Created GBrowse Instance
  867. //-----------------------------------------------------------------------------
  868. /**
  869. * Un-Register GBrowse Instance Form Submit
  870. *
  871. * This is the Form-Submit section of the Un-Register GBrowse instance function. Depending on
  872. * which instance was selected, it will be removed from the database. A check box is provided
  873. * which, if checked is true, will delete all data/files that was associated with the selected
  874. * GBrowse instance.SQL commands are used in order to remove the selected instances from the
  875. * SQL database.
  876. *
  877. * @param $form
  878. * the submitted form containing the user entered infromation
  879. * @param $form_state
  880. * The state of the form: i.e if check-box was checked and what gbrowse_instance was selected to be deleted
  881. * The key is the 'values'
  882. *
  883. * @return
  884. * true or false, depending wheater the check box had been selected, if true, the
  885. * selected GBrowse instance will be deleted
  886. */
  887. function tripal_gbrowse_delete_gbrowse_instance($gbrowse) {
  888. // Retrieve current gbrowse instance details
  889. $gbrowse_instance = db_fetch_object(
  890. db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $gbrowse->gbrowse_id )
  891. );
  892. //Pulling out the configuration file
  893. $config_file = $gbrowse->config_file;
  894. //Removing configuration file from the directory
  895. $command = "rm ".$config_file;
  896. exec($command);
  897. if (file_exists($config_file)) {
  898. drupal_set_message('Configuration File ('.$config_file.') not removed!', 'error');
  899. } else {
  900. drupal_set_message('Configuration File successfully removed');
  901. }
  902. $user_name = $gbrowse->database_user;
  903. $user_password = $gbrowse->user_password;
  904. $link = mysql_connect('localhost', $user_name,$user_password);
  905. if (!$link) {
  906. drupal_set_message('Could not connect: ' . mysql_error(), 'error');
  907. return false;
  908. }
  909. $sql = 'DROP DATABASE ' . $gbrowse->database_name;
  910. if(mysql_query($sql, $link)){
  911. drupal_set_message( "Database removed successfully\n");
  912. }else{
  913. drupal_set_message( 'Error removing database: ' . mysql_error(), 'error');
  914. return false;
  915. }
  916. mysql_close($link);
  917. }
  918. //-----------------------------------------------------------------------------
  919. // SECTION: Load features into GBrowse Form and Form Submit
  920. //-----------------------------------------------------------------------------
  921. /**
  922. * Implements hook_form
  923. *
  924. * This function creates a form for the database name, user name for database operation and a
  925. * password for access.The module uses these features to access information regarding the user's
  926. * operations and needs.
  927. *
  928. * @return
  929. * An array describing the form to be rendered
  930. */
  931. function tripal_gbrowse_load_features_form() {
  932. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  933. foreach ($result as $value) {
  934. $newArray[$value->library_id]=$value->name; //options for the select list
  935. }
  936. //Creating Fieldset for multiple fields in form
  937. $form['load_features'] = array(
  938. '#type' => 'fieldset',
  939. '#title' => t('Data Base Specific Fields'),
  940. );
  941. //Library ID
  942. $form['load_features']['library_id'] = array(
  943. '#type' => 'select',
  944. '#title' => t('Libraries'),
  945. '#options' => $newArray,
  946. '#description' => t('Library of Genetic Sequences.'),
  947. );
  948. //Sending query to the database
  949. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  950. $items = array();
  951. while($record = db_fetch_object($resource)){
  952. $items[$record->gbrowse_id]= $record->gbrowse_name;
  953. }
  954. //GBrowse Instances
  955. $form['load_features']['gbrowse_id'] = array(
  956. '#type' => 'select',
  957. '#title' => t('GBrowse Instances'),
  958. '#options' => $items,
  959. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  960. );
  961. //A checkbox to update features for the gbrowse
  962. $form['load_features']['check_box'] = array(
  963. '#type' => 'checkbox',
  964. '#title' => t('Update Features'),
  965. '#description' => t('Existing Same Name Features Updated Accordingly.'),
  966. );
  967. $form['load_features']['submit'] = array(
  968. '#type' => 'submit',
  969. '#weight' => 10,
  970. '#value' => t('Submit Job')
  971. );
  972. return $form;
  973. }
  974. /**
  975. *
  976. * Load-Features-Form
  977. *
  978. * @param $form
  979. * -The submitted form containing the user entered infromation
  980. * @param $form_state
  981. * -Is the state of the form: i.e what button was pressed, what infromation was entered,etc.
  982. * The key is the 'values'
  983. */
  984. function tripal_gbrowse_load_features_form_submit($form, $form_state) {
  985. global $user; //needed to make the current users details available so access of user id is available
  986. $record = db_fetch_object(mysql_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_id']));
  987. $name = $record->database_name;
  988. $user_name = $record->database_user;
  989. $password= $record->user_password;
  990. $job_args = array($name,$user_name,$password,$form_state['values']['library_id'], FALSE);
  991. //Checking state of check box, if checked delete features from GBrowse
  992. if($form_state['values']['check_box']==1){
  993. //registering a delete job with the tripal system, this job calls the function when executed
  994. $job_id = tripal_add_job('Deleting Features From GBrowse', 'tripal_gbrowse',
  995. 'tripal_gbrowse_delete_features_from_gbrowse', $job_args, $user->uid);
  996. }
  997. //registering a job with the tripal system, this job calls the function when executed
  998. $job_id = tripal_add_job('Loading Features Into GBrowse', 'tripal_gbrowse',
  999. 'tripal_gbrowse_load_features_into_gbrowse', $job_args, $user->uid);
  1000. }
  1001. //-----------------------------------------------------------------------------
  1002. // SECTION: Delete library features from database
  1003. //-----------------------------------------------------------------------------
  1004. /**
  1005. *
  1006. * This form's purpose is to delete library featues from a selected GBrowse insatance. By selecting
  1007. * a Library of genetic sequences and a GBrowse instance, the instance will be removed as will all the
  1008. * associated libraries. This form allows access to a specified database and will delete any
  1009. * selected Library features. It will return the form that contains all of the neccessary
  1010. * information to send to the 'form_submit' function.
  1011. *
  1012. * @return $form
  1013. * An array of menu items allowing deletion of features from the library
  1014. */
  1015. function tripal_gbrowse_delete_library_features_from_gbrowse_form() {
  1016. $form = array();
  1017. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  1018. foreach ($result as $value){
  1019. $newArray[$value->library_id]=$value->name; //options for the select list
  1020. }
  1021. //Creating Fieldset for multiple fields in form
  1022. $form['delete_gbrowse'] = array(
  1023. '#type' => 'fieldset',
  1024. '#title' => t(''),
  1025. );
  1026. //Library ID
  1027. $form['delete_gbrowse']['library_id'] = array(
  1028. '#type' => 'select',
  1029. '#title' => t('Libraries'),
  1030. '#options' => $newArray,
  1031. '#description' => t('Genetic Sequences.'),
  1032. );
  1033. //Sending query to the database
  1034. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1035. $items = array();
  1036. while($record = db_fetch_object($resource)){
  1037. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1038. }
  1039. $form['delete_gbrowse']['gbrowse_id'] = array(
  1040. '#type' => 'select',
  1041. '#title' => t('GBrowse Instances'),
  1042. '#options' => $items,
  1043. '#description' => t('Selected Instances to be Removed.'),
  1044. );
  1045. //Delete button to remove Library
  1046. $form['delete_gbrowse']['submit'] = array(
  1047. '#type' => 'submit',
  1048. '#weight' => 10,
  1049. '#value' => t('Delete')
  1050. );
  1051. return $form;
  1052. }
  1053. /**
  1054. *
  1055. * This 'form_submit' function uses MySQL commands to select the GBrowse instance from a selected database.
  1056. * Then the selected features are deleted from the library.
  1057. *
  1058. *
  1059. * @param $form
  1060. * The submitted form that
  1061. * @param $form_state
  1062. * The state of the form, includes the selected library and the GBrowse instance to be removed
  1063. * The key is the 'values'
  1064. */
  1065. function tripal_gbrowse_delete_library_features_from_gbrowse_form_submit($form, $form_state) {
  1066. global $user; //needed to make the current users details available so access of user id is available
  1067. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_id']));
  1068. $name = $record->database_name;
  1069. $user_name = $record->database_user;
  1070. $password= $record->user_password;
  1071. $job_args = array($name,$user_name,$password,$form_state['values']['library_id']);
  1072. //Checking state of submit button, if selected delete features from library
  1073. if($form_state['values']['op'] == 'Delete'){
  1074. //registering a delete job with the tripal system, this job calls the function when executed
  1075. $job_id = tripal_add_job('Deleting Features From GBrowse', 'tripal_gbrowse', 'tripal_gbrowse_delete_features_from_gbrowse', $job_args, $user->uid);
  1076. }
  1077. }
  1078. //-----------------------------------------------------------------------------
  1079. // SECTION:Load Desired Features into GBrowse
  1080. //-----------------------------------------------------------------------------
  1081. /**
  1082. * Load Library features into database
  1083. *
  1084. * This loading function firstly creates a output GFF3 format file for verification purposes. Then, a 'Chado' table
  1085. * is selected,depending which table the user is interested in, from this table specific infromation
  1086. * is taken out and placed in the GFF3 file.The Featurelocation(fmin) & featurelocation (fmax) are used
  1087. * to set the featureloc.feature_id = feature.feature_id, the fmin = start & fmax = end of the desired gene.
  1088. * The testing if the phase is empty is done, if so a '.' is inserted into $phase variable. If the parent
  1089. * feature is indicated in feature location for the child feature will be set.The Attributes section
  1090. * sets the, ID, Name, Alias, Dbrexf to variables to be put into a CONF file.The strand is either a
  1091. * positive, negative, or zero value and is represented by +/./- accordingley.
  1092. *
  1093. * @param $name
  1094. * name of GBrowse database
  1095. * @param $user_name
  1096. * GBrowse MySQL user name
  1097. * @param $password
  1098. * user password for GBrowse
  1099. * @param $library_id
  1100. * GBrowse MySQL user name
  1101. * @param $clear_database
  1102. * Used to clear the database of its backbone structure
  1103. */
  1104. function tripal_gbrowse_load_features_into_gbrowse($db1_name, $user_name, $password, $library_id,$clear_database){
  1105. //A test file created to verify output
  1106. $myFile = "/tmp/temporaryGBrowse_".time().".gff3";
  1107. //Test opening the file, will print warning if file is not able to be opened
  1108. $fh = fopen($myFile, 'w') or die("WARNING: Can not open file");
  1109. print "Creating GFF3 file of features for library...\n";
  1110. if (!preg_match('/^\d+$/', $library_id)) {
  1111. print "ERROR: Invalid Library ID supplied (Library ID= ".$library_id.")\n";
  1112. return false;
  1113. }
  1114. // Selecting all features
  1115. $values = array('library_id' => $library_id);
  1116. $prime_result = tripal_core_chado_select ( 'library_feature', array('feature_id'), $values );
  1117. // Select Table:library; column:uniquename
  1118. $result = tripal_core_chado_select ( 'library', array('uniquename'), $values );
  1119. $source = $result[0]->uniquename ;
  1120. // Check source: ERROR
  1121. //For each feature, type id form feature table, specific record the feature id containing $feature_id
  1122. foreach ($prime_result as $value) {
  1123. $feature_id = $value->feature_id;
  1124. if (!preg_match('/^\d+$/', $feature_id)) {
  1125. print "ERROR: Invalid feature ID (".$feature_id.") associated with Library (Library ID = ".$library_id.")\n";
  1126. }
  1127. $result = tripal_core_chado_select('feature', array('type_id'), array('feature_id'=>$feature_id));
  1128. $type_id = $result[0]->type_id;
  1129. if (empty($type_id)) {
  1130. print "ERROR: No Type associated with Feature (Feature ID = ".$feature_id.")\n";
  1131. }
  1132. //Taking the cvterm name from the cvterm table given the type-id($type_id)
  1133. $result = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id'=>$type_id));
  1134. //The extracted cvterm name
  1135. $type = $result[0]->name;
  1136. if (empty($type)) {
  1137. print "ERROR: Type associated with Feature (".$type_id.") doesn't exist in cvterm table (Feature ID = ".$feature_id.")\n";
  1138. }
  1139. //Featureloc.fmin & featureloc.fmax ( where featureloc.feature_id = feature.feature_id )
  1140. //fmin = start & fmax = end
  1141. $result = tripal_core_chado_select('featureloc', array('fmin','fmax', 'strand', 'phase', 'srcfeature_id'), array('feature_id'=>$feature_id));
  1142. $start = $result[0]->fmin;
  1143. if (!preg_match('/^\d+$/', $start)) {
  1144. print "ERROR: Invalid Start (".$start.") of Feature (Feature ID= ".$feature_id.")\n";
  1145. }
  1146. $end = $result[0]->fmax;
  1147. if (!preg_match('/^\d+$/', $end)) {
  1148. print "ERROR: Invalid End (".$end.") of Feature (Feature ID= ".$feature_id.")\n";
  1149. }
  1150. $score = '.';
  1151. //Converting from +1/0/-1 to +/./- as there are only three possible values
  1152. if ($result[0]->strand < 0) {
  1153. $strand= '-';
  1154. } elseif ($result[0]->strand == 0) {
  1155. $strand = '.';
  1156. } elseif($result[0]->strand > 0) {
  1157. $strand = '+';
  1158. } else{
  1159. $strand = '.';
  1160. }
  1161. //Testing if phase is empty, if so a '.' is inserted into $phase variable
  1162. $phase = $result[0]->phase;
  1163. if(empty($phase)){
  1164. $phase = '.';
  1165. }
  1166. //This is the parent feature as indicated in feature location for the child feature and
  1167. //the current line described the child feature
  1168. $srcfeature_id = $result[0]->srcfeature_id;
  1169. if (empty($srcfeature_id)) {
  1170. print "ERROR: No Parent associated with Feature Location (Feature ID = ".$feature_id.")\n";
  1171. }
  1172. $result = tripal_core_chado_select('feature', array('name'), array('feature_id'=>$srcfeature_id));
  1173. $seqid = $result[0]->name;
  1174. if (empty($seqid)) {
  1175. print "ERROR: Parent (Parent Feature ID = ".$seqid.") associated with Feature doesn't exist in feature table (Feature ID =".$feature_id."\n";
  1176. }
  1177. //Attributes section, ID, Name, Alias, Dbrexf
  1178. $result = tripal_core_chado_select('feature', array('uniquename', 'name'), array('feature_id'=>$feature_id));
  1179. $id = $result[0]->uniquename;
  1180. if (empty($id)) {
  1181. print "ERROR: Missing Unique Name for Feature (Feature ID = ".$feature_id.")\n";
  1182. }
  1183. $name = $result[0]->name;
  1184. if (empty($name)) {
  1185. print "WARNING: Missing Name for Feature (Feature ID = ".$feature_id.")\n";
  1186. }
  1187. //Synonym
  1188. $result = tripal_core_chado_select('feature_synonym', array('synonym_id'), array('feature_id'=>$feature_id));
  1189. $synonyms = array();
  1190. foreach ($result as $obj){
  1191. $synonym_id = $obj->synonym_id;
  1192. $sub_result = tripal_core_chado_select('synonym', array('name'), array('synonym_id'=>$synonym_id));
  1193. // check that $sub_result[0]->name isn't empty
  1194. if (empty($sub_result[0]->name)) {
  1195. print "ERROR: Synonym (".$synonym_id.") associated with feature not found in synonym table (Feature ID = ".$feature_id.")\n";
  1196. } else {
  1197. $synonyms[] = $sub_result[0]->name;
  1198. }
  1199. }
  1200. // Additional Dbxref's
  1201. $result = tripal_core_chado_select('feature_dbxref', array('dbxref_id'), array('feature_id'=>$feature_id));
  1202. $dbxref = array();
  1203. foreach ($result as $obj){
  1204. $dbxref_id = $obj->dbxref_id;
  1205. if (!empty($dbxref_id)) {
  1206. $sub1_result = tripal_core_chado_select('dbxref', array('db_id', 'accession'), array('dbxref_id'=>$dbxref_id));
  1207. $dbxref_accession = $sub1_result[0]->accession;
  1208. $db_id = $sub1_result[0]->db_id;
  1209. if (!empty($db_id)) {
  1210. $sub_sub_result =tripal_core_chado_select('db', array('name'), array('db_id'=>$db_id));
  1211. $db_name = $sub_sub_result[0]->name;
  1212. if (empty($db_name)) {
  1213. print "ERROR: Database (".$db_id.") associated with database reference not in db table (Database Reference ID = ".$dbxref_id.")\n";
  1214. } elseif (empty($dbxref_accession)) {
  1215. print "WARNING: Missing Accession for Database Reference (Database Reference ID = ".$dbxref_id.")\n";
  1216. } else {
  1217. $dbxref[] = $db_name . ':' . $dbxref_accession;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. //Main Dbxref
  1223. $result = tripal_core_chado_select('feature', array('dbxref_id'), array('feature_id'=>$feature_id));
  1224. $dbxref_id = $result[0]->dbxref_id;
  1225. if (empty($dbxref_id)) {
  1226. print "WARNING: No Database Reference associated directly with Feature (Feature ID = ".$feature_id.")\n";
  1227. }
  1228. $result = tripal_core_chado_select('dbxref', array('accession','db_id'), array('dbxref_id'=>$dbxref_id));
  1229. $dbxref_accession = $result[0]->accession;
  1230. //checking $dbrexf_accession for emptyness
  1231. if(!empty($dbxref_accession)){
  1232. $db_id = $result[0]->db_id;
  1233. $sub_sub_result =tripal_core_chado_select('db', array('name'), array('db_id'=>$db_id));
  1234. $db_name = $sub_sub_result[0]->name;
  1235. $dbxref[] = $db_name . ':' . $dbxref_accession;
  1236. } else {
  1237. print "WARNING: Missing Accession for Database Reference (Database Reference ID = ".$dbxref_id.")\n";
  1238. }
  1239. //attributes array is assigned given elements based on previous initializations
  1240. $attributes = array();
  1241. $attributes[] = 'ID='.$id;
  1242. if (!empty($name)) { $attributes[] = 'Name='.$name; }
  1243. if (!empty($synonyms)) { $attributes[] = 'Alias='.implode(',',$synonyms); }
  1244. if (!empty($dbxref)) { $attributes[] = 'Dbxref=' .implode(',', $dbxref); }
  1245. //write to string for printing
  1246. $stringData = implode("\t", array($seqid,$source,$type,$start,$end,$score,$strand,$phase,
  1247. implode(';',$attributes)))."\n";
  1248. //Write concatinated data ($stringData) to file
  1249. fwrite($fh, $stringData);
  1250. }
  1251. //Closing file
  1252. fclose($fh);
  1253. if($clear_database){
  1254. //The loading script: bp_seqfeature_load.pl, allows loading of data to specific file
  1255. $command= "bp_seqfeature_load.pl -c -u '" .$user_name. "' -p '" .$password. "' -d " .$db1_name. " " . $myFile;
  1256. }else{
  1257. //The loading script: bp_seqfeature_load.pl, allows loading of data to specific file
  1258. $command= "bp_seqfeature_load.pl -u '" .$user_name. "' -p '" .$password. "' -d " .$db1_name. " " . $myFile;
  1259. }
  1260. //optional 'print' statement to verify that the above comman is being called and executed properly
  1261. print 'Executing Command:'.$command."\n";
  1262. exec($command, $out_put, $return_var);
  1263. }
  1264. //-----------------------------------------------------------------------------
  1265. // SECTION: Delete Features from GBrowse
  1266. //-----------------------------------------------------------------------------
  1267. /**
  1268. * Delete-GBrowse-Feature
  1269. *
  1270. * This form Removes features from library that the user has specified. The form allows the user
  1271. * to select a desired instance. Once the library and instance are selected and deleted, the selected
  1272. * libraries
  1273. *
  1274. *
  1275. *
  1276. * @param $db1_name
  1277. * name of GBrowse database
  1278. * @param $user_name
  1279. * GBrowse MySQL user name
  1280. * @param $password
  1281. * user password for GBrowse
  1282. * @param $library_id
  1283. * The id of the library that is being selected from
  1284. *
  1285. */
  1286. function tripal_gbrowse_delete_features_from_gbrowse($db1_name, $user_name, $password, $library_id){
  1287. $result = tripal_core_chado_select ( 'library', array('name'), array('library_id' => $library_id)
  1288. );
  1289. $library_name = $result[0]->name;
  1290. print "Removing Features of all types related to the ".$library_name." library...\n";
  1291. $sql = 'SELECT cvterm.name FROM feature JOIN cvterm cvterm ON cvterm.cvterm_id=feature.type_id
  1292. JOIN library_feature library_feature ON library_feature.feature_id=feature.feature_id JOIN library
  1293. library ON library.library_id=library_feature.library_id WHERE library.library_id=%d GROUP BY
  1294. cvterm.name';
  1295. $previous_db = tripal_db_set_active('chado');
  1296. $resource = db_query($sql, $library_id);
  1297. tripal_db_set_active($previous_db);
  1298. while($record = db_fetch_object($resource)){
  1299. $type = $record->name;
  1300. //The loading script: bp_seqfeature_delete.pl, allows removal of data to specific file
  1301. $command= "bp_seqfeature_delete.pl -u '".$user_name."' -p '".$password."' -d '" .$db1_name. "' -t '" .$type. ":".$library_name."'";
  1302. print "Executing Command: ".$command."\n";
  1303. //exec() allows for execution of a external program: given the input
  1304. exec($command);
  1305. }
  1306. }
  1307. //-----------------------------------------------------------------------------
  1308. // SECTION: Add Tracks
  1309. //-----------------------------------------------------------------------------
  1310. /**
  1311. * Add-GBrowse-Tracks
  1312. * This Menu item is implemented to allow users to define and add tracks including their name,
  1313. * glyph, colour (fore and back ground), size and link. By adding tracks, the user is able to
  1314. * visually inspect the features of their genomic data. The Glyph library has been hardcoded
  1315. * into the software and represents the most used Glyph representations. The "Glyph" option
  1316. * determines the shape of each of the displayed genomic features associated with a specific
  1317. * GBrowse track. It presents a configurable set of parameters specific to its type allowing
  1318. * further control over the display features such as height, color (foreground & backround) etc.
  1319. *
  1320. *
  1321. * @return
  1322. * An '$form' that is an array of menu items that allows the user to Register a GBrowse instance
  1323. */
  1324. function tripal_gbrowse_add_tracks_form() {
  1325. //Creating Fieldset for multiple fields in form
  1326. $form['add_tracks_field_set'] = array(
  1327. '#type' => 'fieldset',
  1328. '#title' => t('Content-Related Fields'),
  1329. );
  1330. //Sending query to the database for GBrowse Instances
  1331. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1332. $items = array();
  1333. while($record = db_fetch_object($resource)){
  1334. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1335. }
  1336. //GBrowse Instances
  1337. $form['add_tracks_field_set']['gbrowse_instances'] = array(
  1338. '#type' => 'select',
  1339. '#title' => t('Registered GBrowse Instances'),
  1340. '#options' => $items,
  1341. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1342. '#required' => TRUE,
  1343. );
  1344. //Feature Type
  1345. $sql = 'SELECT name as type, cvterm_id '
  1346. .'FROM cvterm '
  1347. .'WHERE cvterm_id IN (SELECT type_id FROM feature)';
  1348. $previous_db = tripal_db_set_active('chado');
  1349. $resource = db_query($sql);
  1350. tripal_db_set_active($previous_db);
  1351. $items = array();
  1352. while($record = db_fetch_object($resource)){
  1353. $items[$record->type] = $record->type;
  1354. }
  1355. $form['add_tracks_field_set']['type_name'] = array(
  1356. '#type' => 'select',
  1357. '#title' => t('Feature Type'),
  1358. '#options' => $items,
  1359. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1360. '#required' => TRUE,
  1361. );
  1362. //Populating Select list for Library
  1363. $newArray['none'] = 'None'; //No option for select list
  1364. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  1365. foreach ($result as $value) {
  1366. $newArray[$value->name]=$value->name; //options for the select list
  1367. }
  1368. //Library
  1369. $form['add_tracks_field_set']['library_id'] = array(
  1370. '#type' => 'select',
  1371. '#title' => t('Library'),
  1372. '#options' => $newArray,
  1373. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1374. );
  1375. //Creating Fieldset for multiple fields in form
  1376. $form['track_text'] = array(
  1377. '#type' => 'fieldset',
  1378. '#title' => 'Track-Specific Fields'
  1379. );
  1380. //Specify Track Identifier
  1381. $form['track_text']['identifier'] = array(
  1382. '#type' => 'textfield',
  1383. '#title' => t('Track Identifier'),
  1384. '#size' => 30,
  1385. '#maxlength' => 64,
  1386. '#description' => t('Specify Track Identifier (Must be Unique and Specific)'),
  1387. '#required' => TRUE,
  1388. );
  1389. //Specify Track Name
  1390. $form['track_text']['track_name'] = array(
  1391. '#type' => 'textfield',
  1392. '#title' => t('Track Name'),
  1393. '#size' => 30,
  1394. '#maxlength' => 64,
  1395. '#description' => t('Specify Track Name (Must be Unique and Specific)'),
  1396. '#required' => TRUE,
  1397. );
  1398. //Populating Array of Glyph List
  1399. $item = array();
  1400. $item[''] = 'Default Glyph';
  1401. $item['generic'] = 'Generic';
  1402. $item['allele_tower '] = 'Allele Tower';
  1403. $item['anchored_arrow'] = 'Anchored Arrow';
  1404. $item['arrow'] = 'Arrow';
  1405. $item['box'] = 'Box';
  1406. $item['cds'] = 'CDS';
  1407. $item['crossbox'] = 'Cross Box';
  1408. $item['diamond'] = 'Diamond';
  1409. $item['dna'] = 'DNA';
  1410. $item['dot'] = 'Dot';
  1411. $item['ellipse'] = 'Ellipse';
  1412. $item['extending_arrow'] = 'Extending Arrow';
  1413. $item['generic'] = 'Generic';
  1414. $item['graded_segments'] = 'Graded Segments';
  1415. $item['heterogeneous_segments '] = 'Heterogeneous Segments';
  1416. $item['image'] = 'Image';
  1417. $item['line'] = 'Line';
  1418. $item['primers'] = 'Primers';
  1419. $item['processed_transcript'] = 'Processed Transcript';
  1420. $item['rndrect'] = 'Rndrect';
  1421. $item['ruler_arrow'] = 'Ruler Arrow';
  1422. $item['segments'] = 'Segments';
  1423. $item['span'] = 'Span';
  1424. $item['toomany'] = 'Too Many';
  1425. $item['transcript'] = 'Transcript';
  1426. $item['transcript2'] = 'Transcript2';
  1427. $item['translation'] = 'Translation';
  1428. $item['triangle'] = 'Triangle';
  1429. $item['wiggle_densit'] = 'Wiggle Densit';
  1430. $item['wiggle_xyplot'] = 'Wiggle XYplot';
  1431. $item['xyplot'] = 'XYplot';
  1432. //Glyphs
  1433. $form['track_text']['glyph'] = array(
  1434. '#type' => 'select',
  1435. '#title' => t('Glyph'),
  1436. '#options' => $item,
  1437. '#description' => t('Shape of Genomic Feature Track '),
  1438. );
  1439. //Modify The foreground colour of track
  1440. $form['track_text']['foreground_colour'] = array(
  1441. '#type' => 'textfield',
  1442. '#title' => t('Foreground Colour'),
  1443. '#size' => 30,
  1444. '#maxlength' => 64,
  1445. '#description' => t('Enter name/Hex-code for identifying foreground colour of glyph.
  1446. List of available colours here:'.l("GBrowse Colour Palette.pdf",
  1447. "http://gmod.org/mediawiki/images/a/ae/Gbrowse_Color_Palette.pdf")),
  1448. );
  1449. //Modify The Backround colour of track
  1450. $form['track_text']['backround_colour'] = array(
  1451. '#type' => 'textfield',
  1452. '#title' => t('Background Colour'),
  1453. '#size' => 30,
  1454. '#maxlength' => 20,
  1455. '#description' => t('Enter name/Hex-code for identifying background colour of glyph.
  1456. List of available colours here:'.l("GBrowse Colour Palette.pdf",
  1457. "http://gmod.org/mediawiki/images/a/ae/Gbrowse_Color_Palette.pdf")),
  1458. );
  1459. //Modify The Height of track
  1460. $form['track_text']['track_height'] = array(
  1461. '#type' => 'textfield',
  1462. '#title' => t('Height'),
  1463. '#size' => 30,
  1464. '#maxlength' => 20,
  1465. '#description' => t('Modify Track Height'),
  1466. );
  1467. //Specify Link
  1468. $form['track_text']['link'] = array(
  1469. '#type' => 'textfield',
  1470. '#title' => t('Link'),
  1471. '#size' => 30,
  1472. '#maxlength' => 64,
  1473. '#description' => t('Specify to Further details (HTML address)'),
  1474. );
  1475. //Register job submit button
  1476. $form['create_instance']['submit'] = array(
  1477. '#type' => 'submit',
  1478. '#value' => t('Add Track')
  1479. );
  1480. return $form;
  1481. }
  1482. /**
  1483. * Validate User-submitted data for Register GBrowse Instances
  1484. *
  1485. * This function validates the user input for each form element in the Register GBrowse
  1486. * instances form that is a text field. Validations checked include:
  1487. * - database_name: contains only word characters not including punctuation
  1488. * or white-space ([A-Z][a-z][0-1]_)
  1489. *
  1490. * @param $form
  1491. * The form that the user submitted and whose input is being validated
  1492. *
  1493. * @param $form_state
  1494. * An array containing the state of the form being validated including user-input
  1495. */
  1496. function tripal_gbrowse_add_tracks_form_validate ($form, $form_state) {
  1497. if (!empty($form_state['values']['foreground_colour'])) {
  1498. // foreground colour is present and a valid colour
  1499. if ( !preg_match('/^[\w#]+$/', $form_state['values']['foreground_colour'])) {
  1500. form_set_error('foreground_colour',
  1501. 'The Foreground Colour should be either the name of a colour listed in the linked to pdf or a valid hex colour');
  1502. }
  1503. }
  1504. if (!empty($form_state['values']['backround_colour'])) {
  1505. if(!preg_match('/^[\w#]+$/', $form_state['values']['backround_colour'])){
  1506. form_set_error('backround_colour','The Background Colour should be either the name of a colour listed in the linked to pdf or a valid hex colour');
  1507. }
  1508. }
  1509. if (!empty($form_state['values']['track_height'])){
  1510. if(!preg_match('/^\d*$/', $form_state['values']['track_height'])){
  1511. form_set_error('track_height','The Track Height must be a positive integer');
  1512. }
  1513. }
  1514. if(!preg_match('/^\w+$/', $form_state['values']['identifier'])){
  1515. form_set_error('identifier','The Identifier must be composed of the following characters only: [A-Z][a-z][0-9] and _');
  1516. }
  1517. if (!empty($form_state['values']['link'])){
  1518. if(!preg_match('/^http:/', $form_state['values']['link'])){
  1519. form_set_error('link','The Link must be a conventional web site name, beginging with http: ');
  1520. }
  1521. }
  1522. }
  1523. /**
  1524. *Add-GBrowse-Tracks Form_Submit
  1525. *
  1526. * @param $form
  1527. * The form for the information that the user has entered
  1528. *
  1529. * @param $form_state
  1530. * This is the state of the form: i.e what button was pressed.The key is
  1531. * the name of the fields in database
  1532. *
  1533. * @return
  1534. * An '$form' that is an array of menu items that allows the user to Register a GBrowse instance
  1535. */
  1536. function tripal_gbrowse_add_tracks_form_submit($form, $form_state) {
  1537. $string = "\n[".$form_state['values']['identifier']."]\n"
  1538. ."key = ".$form_state['values']['track_name']."\n";
  1539. if($form_state['values']['library_id'] == 'none'){
  1540. $string .= "feature = ".$form_state['values']['type_name']."\n";
  1541. } else {
  1542. $string .= "feature = ".$form_state['values']['type_name'].":" .$form_state['values']['library_id']."\n";
  1543. }
  1544. if (!empty($form_state['values']['glyph'])) {
  1545. $string .= "glyph = ".$form_state['values']['glyph']."\n";
  1546. }
  1547. if (!empty($form_state['values']['track_height'])) {
  1548. $string .= "height = ".$form_state['values']['track_height']."\n";
  1549. }
  1550. if (!empty($form_state['values']['foreground_colour'])) {
  1551. $string .= "bgcolor = ".$form_state['values']['foreground_colour']."\n";
  1552. }
  1553. if (!empty($form_state['values']['backround_colour'])) {
  1554. $string .= "fgcolor = ".$form_state['values']['backround_colour']."\n";
  1555. }
  1556. if (!empty($form_state['values']['link'])) {
  1557. $string .= "link = ".$form_state['values']['link']."\n";
  1558. }
  1559. $string .= "\n";
  1560. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_instances']));
  1561. $config_file = $record->config_file;
  1562. $command = "echo '".$string."' >> ".$config_file;
  1563. exec($command);
  1564. }
  1565. //-----------------------------------------------------------------------------
  1566. // SECTION: Delete Tracks
  1567. //-----------------------------------------------------------------------------
  1568. /**
  1569. *Delete-GBrowse-Tracks
  1570. *
  1571. * This Form allows selection for a specific GBrowse Instance to be removed in a multi-submit form outline.
  1572. * Once the GBrowse instance is selected,the corresponding track list that corresponds to
  1573. * the selected GBrowse instance is deleted. This can be done one track at a time. As
  1574. * there is no limit to the number of tracks that can be added to a particular GBrowse instance and
  1575. * thus all the tracks that have bee added can be deleted, one track at a time. The track that is to
  1576. * be deleted is to be selected from the list. When selected and the delete button is pushed,
  1577. * the track that was selected is removed from the 'configuration' file.
  1578. *
  1579. * @param $form_state
  1580. * The state of the form as it is setting the default value of $form_state is NULL
  1581. *
  1582. * @return
  1583. * An '$form' that is an array of menu items that allows the user to Register a GBrowse instance
  1584. */
  1585. function tripal_gbrowse_delete_tracks_form(&$form_state=NULL) {
  1586. //Creating Fieldset for multiple fields in form
  1587. $form['delete_tracks_field_set'] = array(
  1588. '#type' => ($form_state['storage']['gbrowse_id']) ? 'hidden' : 'fieldset',
  1589. '#title' => t('GBrowse Specific Fields'),
  1590. );
  1591. //Sending query to the database for GBrowse Instances
  1592. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1593. $items = array();
  1594. while($record = db_fetch_object($resource)){
  1595. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1596. }
  1597. //GBrowse Instance Select List
  1598. $form['delete_tracks_field_set']['gbrowse_instances'] = array(
  1599. '#type' => 'select',
  1600. '#title' => t('Registered GBrowse Instances'),
  1601. '#options' => $items,
  1602. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1603. '#default_value' => $form_state['values']['gbrowse_instances'],
  1604. );
  1605. //Set Instance Submit button
  1606. $form['delete_tracks_field_set']['submit_instance'] = array(
  1607. '#type' => 'submit',
  1608. '#value' => t('Set Instance')
  1609. );
  1610. //Creating Fieldset for multiple fields in form
  1611. $form['delete_tracks'] = array(
  1612. '#type' => ($form_state['storage']['gbrowse_id']) ? 'fieldset' : 'hidden',
  1613. '#title' => t('Track Specific Fields'),
  1614. );
  1615. $gbrowse_id = $form_state['storage']['gbrowse_id'];
  1616. if ($gbrowse_id){
  1617. $items = track_name_options($gbrowse_id);
  1618. }else {
  1619. $items = array();
  1620. }
  1621. //Track Name Select List
  1622. $form['delete_tracks']['track_line_number'] = array(
  1623. '#type' => 'select',
  1624. '#title' => t('Track Name'),
  1625. '#options' => $items,
  1626. '#description' => t('Name of Track to Remove '),
  1627. '#default_value' => $form_state['values']['gbrowse_id'],
  1628. );
  1629. //Remove Track button
  1630. $form['delete_tracks']['remove_track'] = array(
  1631. '#type' => 'submit',
  1632. '#value' => t('Remove Track')
  1633. );
  1634. return $form;
  1635. }
  1636. /**
  1637. *Delete-GBrowse-Tracks
  1638. *
  1639. *@param $form
  1640. * The form that is completed in the above section
  1641. *
  1642. * @param $form_state
  1643. * The state of the form, as it is completed by the user and will contain all of the user's
  1644. * selected infromation that is to be removed from the configuration file.
  1645. *
  1646. */
  1647. function tripal_gbrowse_delete_tracks_form_submit($form, &$form_state) {
  1648. $form_state['storage']['gbrowse_id'] = $form_state['values']['gbrowse_instances'];
  1649. //Checking that the "Remove Track" button was selected
  1650. if($form_state['clicked_button']['#value']=='Remove Track'){
  1651. //selecting GBrowse-ID from the database
  1652. $resource= db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['storage']['gbrowse_id']));
  1653. //putting contents of the configuration file into an variable
  1654. $config_file = (string) $resource->config_file;
  1655. $read_file_handle = fopen($config_file,'r') or drupal_set_message("WARNING: Can not open file:" .$config_file, 'error');
  1656. $track_number = $form_state['values']['track_line_number'];
  1657. $line_num = 0;
  1658. //create temp file
  1659. $new_conf_file = '/tmp/temp_config_file.temp';
  1660. //making '.temp' file in the /tmp directory
  1661. $command = 'touch /tmp/temp_config_file.temp';
  1662. exec($command);
  1663. //Checking that the newly created file is writable
  1664. if (is_writable($new_conf_file)) {
  1665. //opening file for writing to
  1666. $write_file_handle = fopen($new_conf_file, 'w') or drupal_set_message("WARNING: Can not open file:" .$new_conf_file, 'error');
  1667. }else{
  1668. drupal_set_message("WARNING: Attempting to create a temporary file -insufficient permissions: ".$new_conf_file, 'error');
  1669. }
  1670. while(!feof($read_file_handle)){
  1671. $file_line = fgets($read_file_handle);
  1672. $line_num++;
  1673. if($line_num == $track_number){
  1674. $remove_track = TRUE;
  1675. }
  1676. if(preg_match('/^\s*$/',$file_line)){
  1677. $remove_track=FALSE;
  1678. }
  1679. if($remove_track==FALSE){
  1680. fwrite($write_file_handle, $file_line);
  1681. }
  1682. }
  1683. $command = 'cp '.$new_conf_file.' '.$config_file ;
  1684. exec($command);
  1685. $command = 'rm '.$new_conf_file ;
  1686. exec($command);
  1687. }
  1688. }
  1689. /**
  1690. * Populate Track-Name List
  1691. *
  1692. * This section takes a GBrowse ID so the coresponding configuration file can be accessed. Once it has been
  1693. * obtained from the database, by the db_query() command. The configuration file is is searched for lines
  1694. * that start with "[ ]". This corresponds to the framework of the conf file, as the Track Names are
  1695. * enclosed within "[ ]". The file is then scanned from the spots that contain the "[ ]".
  1696. *
  1697. *
  1698. * @param gbrowse_id
  1699. * The Selected GBrowse instance's identification
  1700. *
  1701. * @return
  1702. * $items() and array with the tracks that are in the selected GBrowse instance.
  1703. */
  1704. function track_name_options($gbrowse_id) {
  1705. $items = array();
  1706. // Use GBrowse ID to get configuration filename
  1707. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $gbrowse_id));
  1708. //putting contents of the configuration file into an variable
  1709. $config_file = (string) $record->config_file;
  1710. $config_array = array();
  1711. //command to select the lines that begin with '[' and have characters within the '[ ]'
  1712. $command = 'grep -n "^\[.*\]" ' .$config_file;
  1713. //execution of the command
  1714. exec($command , $config_array);
  1715. $config_array_size = count($config_array);
  1716. //while the end-of-file has not been reached
  1717. for($i = 0 ; $i < $config_array_size; $i++){
  1718. $file_line = $config_array[$i];
  1719. if ($is_track_definition){
  1720. if(preg_match('/^(\d+):\[(.+)\]$/', $file_line, $matches)){
  1721. $items[$matches[1]] = $matches[2];
  1722. }
  1723. }
  1724. if(preg_match('/\[TRACK DEFAULTS\]/', $file_line)){
  1725. $is_track_definition = TRUE;
  1726. }
  1727. }
  1728. return $items;
  1729. }
  1730. //-----------------------------------------------------------------------------
  1731. // END OF SOFTWARE
  1732. //-----------------------------------------------------------------------------