tripal_gbrowse.module 98 KB

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