tripal_gbrowse.module 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938
  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. if(!user_access('create tripal_gbrowse', $account)){
  642. return FALSE;
  643. }
  644. }
  645. // Users who create a node may edit or delete it later, assuming they have the
  646. // necessary permissions.
  647. if($op == 'update' || $op == 'delete') {
  648. if(!user_access('edit own tripal_gbrowse',$account)){
  649. return FALSE;
  650. }
  651. if(user_access('edit own tripal_gbrowse',$account) &&
  652. $account->uid != $node->uid){
  653. return FALSE;
  654. }
  655. }
  656. return NULL;
  657. }
  658. /**
  659. * Implementation of tripal_gbrowse_insert()
  660. *
  661. * This function inserts user entered information pertaining to the GBrowse instance into the
  662. * 'tripal_gbrowse_instances' talble of the database, requiring that a 'Check Box' has been selected.
  663. *
  664. * @parm $node
  665. * Then node which contains the information stored within the node-ID
  666. *
  667. *
  668. *
  669. */
  670. function tripal_gbrowse_insert($node) {
  671. $values = array(
  672. 'database_name'=> $node->database_name,
  673. 'database_user'=> $node->database_user,
  674. 'user_password'=> $node->user_password,
  675. 'gbrowse_name'=> $node->title,
  676. 'gbrowse_link'=> $node->gbrowse_link,
  677. 'config_file'=> $node->config_file,
  678. 'library_id'=>$node->library_id,
  679. 'nid'=>$node->nid,
  680. 'vid'=>$node->vid,
  681. );
  682. //used to write to a database table described by Drupal Schema (not workable for chado)
  683. drupal_write_record('tripal_gbrowse_instances', $values);
  684. if($node->check_box==1){
  685. tripal_gbrowse_create_gbrowse_instance($values);
  686. }else{
  687. drupal_set_message("Create instance check box not selected, unable initialize instance");
  688. }
  689. }
  690. /**
  691. * Implementation of tripal_gbrowse_delete().
  692. *
  693. * This function takes a node and if the variable 'tripal_gbrowse_delete_all' has been defined,
  694. * the GBrowse instance pertaining to the node passed into the function is deleted. Following,
  695. * given the node-ID, the instance will be deleted from the 'tripal_gbrowse_instances' table.
  696. *
  697. *
  698. * @parm $node
  699. * Then node which contains the information stored within the node-ID
  700. *
  701. */
  702. function tripal_gbrowse_delete($node){
  703. if(variable_get('tripal_gbrowse_delete_all', '')){
  704. tripal_gbrowse_delete_gbrowse_instance($node->gbrowse);
  705. }
  706. //deleteing in drupal chado_gbrowse table
  707. db_query('DELETE FROM {tripal_gbrowse_instances} WHERE nid = %d', $node->nid);
  708. }
  709. /*
  710. *
  711. * Implements hook_update
  712. *
  713. * The purpose of the function is to allow the module to take action when an edited node is being
  714. * updated in the database. It also updates any name changes to the configuration file that was
  715. * created upon registering a GBrowse instance. This is done by moving the contents of the original
  716. * configuration file to the newly named file.As well, the database will be changed, if the name
  717. * is altered by a registered user.If this is the case, the function 'tripal_gbrowse_database_modification'
  718. * is called and the node and the old database infromation is passed, so as it may be altered and updated
  719. * accordingley.
  720. *
  721. * @param $node
  722. * The node being updated
  723. *
  724. */
  725. function tripal_gbrowse_update($node){
  726. //select from table before doing this, cause i wont be able to see the old details afterwards
  727. $old_data = db_fetch_object(
  728. db_query('SELECT database_name,config_file FROM {tripal_gbrowse_instances} WHERE nid=%d AND vid=%d',
  729. $node->nid, $node->vid));
  730. $old_config_file=$old_data->config_file;
  731. $old_database_name=$old_data->database_name;
  732. $match= array(
  733. 'nid',
  734. 'vid',
  735. );
  736. $values = array(
  737. 'database_name' => $node->database_name,
  738. 'database_user' => $node->database_user,
  739. 'user_password' => $node->user_password,
  740. 'gbrowse_name' => $node->gbrowse_name,
  741. 'gbrowse_link' => $node->gbrowse_link,
  742. 'config_file' => $node->config_file,
  743. 'library_id'=>$node->library_id,
  744. 'nid' => $node->nid,
  745. 'vid' => $node->vid,
  746. );
  747. //Pulling out the configuration file
  748. $config_file = $node->config_file;
  749. if(strcmp($old_config_file, $config_file)!=0){
  750. //Removing configuration file from the directory
  751. $command = "mv " .$old_config_file ." ".$config_file;
  752. exec($command);
  753. }
  754. if($old_database_name != $node->database_name){
  755. tripal_gbrowse_database_modification($node,$old_database_name);
  756. }
  757. drupal_write_record('tripal_gbrowse_instances', $values, $match);
  758. }
  759. /**
  760. * Implementation of tripal_gbrowse_load().
  761. *
  762. *
  763. * @param $node
  764. * The node that is to be accessed from the database
  765. *
  766. * @return $node
  767. * The node with the information to be loaded into the database
  768. *
  769. */
  770. function tripal_gbrowse_load($node) {
  771. $result = db_fetch_object(db_query('SELECT gbrowse_id,database_name,database_user,user_password,
  772. gbrowse_name,gbrowse_link,config_file FROM {tripal_gbrowse_instances} WHERE nid=%d AND vid=%d',$node->nid, $node->vid));
  773. $node->gbrowse = $result;
  774. return $node;
  775. }
  776. //-----------------------------------------------------------------------------
  777. // SECTION: Configuration form
  778. //-----------------------------------------------------------------------------
  779. /**
  780. * Implemets hook_menu(): Adds menu items for the tripal_gbrowse module
  781. *
  782. * This form creates a specific Configuration File directory and sets a specific
  783. * GBrowse web address for later access
  784. *
  785. * @return
  786. * An array of menu items '$form'
  787. */
  788. function tripal_gbrowse_administration_form () {
  789. //Creating Fieldset for multiple fields in form
  790. $form['configuration_features'] = array(
  791. '#type' => 'fieldset',
  792. '#title' => t('Module Configuration Fields'),
  793. );
  794. //Configuration File Directory
  795. $form['configuration_features']['configuration_file'] = array(
  796. '#type' => 'textfield',
  797. '#title' => t('Set Default Configuration File Directory'),
  798. '#size' => 30,
  799. '#maxlength' => 64,
  800. '#description' => t('Specific Configuration File Directory'),
  801. '#default_value' => variable_get('tripal_gbrowse_configuration_file_path', ''),
  802. );
  803. //GBrowse Web Address
  804. $form['configuration_features']['gbrowse_address'] = array(
  805. '#type' => 'textfield',
  806. '#title' => t('Set Default GBrowse Web Address'),
  807. '#size' => 30,
  808. '#maxlength' => 64,
  809. '#description' => t('Desired GBrowse web address'),
  810. '#default_value' => variable_get('tripal_gbrowse_gbrowse_address_stub', ''),
  811. );
  812. //A box if checked creates GBrowse Instances(.conf file and database)
  813. $form['configuration_features']['delete_check_box'] = array(
  814. '#type' => 'checkbox',
  815. '#title' => t('Delete Instance'),
  816. '#description' => t('Delete GBrowse Configuration file and Database '),
  817. '#default_value'=>variable_get('tripal_gbrowse_delete_all', ''),
  818. );
  819. $form['configuration_features']['submit'] = array(
  820. '#type' => 'submit',
  821. '#weight' => 10,
  822. '#value' => t('Submit')
  823. );
  824. return $form;
  825. }
  826. /**
  827. * Validate User-submitted data for Register GBrowse Instances Form
  828. *
  829. * This function validates the user input for each form element in the Register GBrowse
  830. * instances form that is a text field. Validations checked include:
  831. * -parse_url-checks that the url can be parsed
  832. *
  833. * @param $form
  834. * The form that the user submitted and whose input is being validated
  835. * @param $form_state
  836. * An array containing the state of the form being validated including user-input
  837. */
  838. function tripal_gbrowse_administration_form_validate ($form, $form_state) {
  839. if ( !valid_url($form_state['values']['gbrowse_address'])) {
  840. form_set_error('gbrowse_address', 'Invalid URL');
  841. }
  842. }
  843. /**
  844. *
  845. * Configuration-Form
  846. *
  847. * @param $form
  848. * -The submitted form containing the user entered infromation
  849. * @param $form_state
  850. * -Is the state of the form: i.e what button was pressed, what infromation was entered,etc.
  851. * The key is the 'values'
  852. */
  853. function tripal_gbrowse_administration_form_submit ($form, $form_state) {
  854. if(!preg_match('/\/$/', $form_state['values']['configuration_file'])) {
  855. $form_state['values']['configuration_file'] = $form_state['values']['configuration_file'] . "/";
  856. }
  857. variable_set('tripal_gbrowse_configuration_file_path',$form_state['values']['configuration_file'] );
  858. drupal_set_message('Configuration File Path set to: '.variable_get('tripal_gbrowse_configuration_file_path', ''));
  859. variable_set('tripal_gbrowse_gbrowse_address_stub',$form_state['values']['gbrowse_address'] );
  860. drupal_set_message('GBrowse Default Address set to: '.variable_get('tripal_gbrowse_gbrowse_address_stub', ''));
  861. if($form_state['values']['delete_check_box']==1){
  862. variable_set('tripal_gbrowse_delete_all',$form_state['values']['delete_check_box'] );
  863. drupal_set_message('Selected GBrowse instance will be deleted: '.variable_get('tripal_gbrowse_delete_all', 'NOT SET'));
  864. }
  865. }
  866. //-----------------------------------------------------------------------------
  867. // SECTION: Register-GBrowse-Instance
  868. //-----------------------------------------------------------------------------
  869. /**
  870. *
  871. * Register & Create GBrowse instance
  872. *
  873. * This form submit takes it's fields from the form for registering a GBrowse instance. The 'sed'command
  874. * is used to select certain lines of a template 'conf' file and replaces them with the user specified GBrowse instance
  875. * and data base name. It then removes the temporary file that was created. This function utilizes the MySQL database
  876. * and uses the commands for MySQL to create a database for the registered GBrowse instance, appropriate warnings
  877. * are issued if there are problems connecting/registering/accessing the database.
  878. *
  879. * @param $form
  880. * -The submitted form containing the user entered infromation
  881. *
  882. * @param $form_state
  883. * This is the state of the form: i.e what button was pressed.The key is
  884. * the name of the fields in database
  885. *
  886. */
  887. function tripal_gbrowse_create_gbrowse_instance($values) {
  888. global $user; //make current user details available so access of user id is available
  889. //checking for existance of configuration file, if allready created a warning is issued
  890. if ( file_exists($values['config_file']) ){
  891. drupal_set_message('Configuration File Already Exists', 'warning');
  892. }else{
  893. $config_path = variable_get('tripal_gbrowse_configuration_file_path', '');
  894. if(empty($config_path)){
  895. drupal_set_message( "Invalid or Non-Existent Configuration Path (Check Configuration Menu Defaults)", 'error');
  896. }
  897. $temp_file = '/tmp/temporary_gbrowse_'.time().'.conf';
  898. // Using 'sed' to select the second line of the template.config substitute <#gbrowse_name>
  899. //for .$values['gbrowse_name']
  900. $command = "sed '2 s/<#gbrowse_name#>/".$values['gbrowse_name']."/' ".$config_path."template.conf > ".$temp_file;
  901. exec($command);
  902. // Using 'sed' to select the fifth line of the template.config substitute <#database_name>
  903. //for .$values['database_name']
  904. $command = "sed '5 s/<#database_name#>/".$values['database_name']."/' ".$temp_file." > ".$values['config_file'];
  905. exec($command);
  906. // Check configuration file is there
  907. if (file_exists($values['config_file'])) {
  908. drupal_set_message('Configuration File created successfully');
  909. } else {
  910. drupal_set_message('Unable to create configuration file','error');
  911. }
  912. //To create a database use the mysql_query() function to execute an SQL query
  913. $link = mysql_connect('localhost', $values['database_user'],$values['user_password']);
  914. if (!$link) {
  915. drupal_set_message('Could not connect: ' . mysql_error());
  916. return false;
  917. }
  918. $sql = 'CREATE DATABASE ' . $values['database_name'];
  919. if (mysql_query($sql, $link)) {
  920. drupal_set_message( "Database created successfully\n");
  921. } else {
  922. drupal_set_message( 'Error creating database: ' . mysql_error() . "\n");
  923. return false;
  924. }
  925. mysql_query( "GRANT SELECT ON " .$values['database_name'].".* TO 'www-data'@'localhost'",$link);
  926. mysql_close($link);
  927. $job_args=array($values['database_name'],$values['database_user'],$values['user_password'],$values['library_id'],TRUE);
  928. //registering a job with the tripal system, this job calls the function when executed
  929. $job_id = tripal_add_job('Loading Features Into GBrowse', 'tripal_gbrowse','tripal_gbrowse_load_library_features_into_gbrowse', $job_args, $user->uid);
  930. drupal_set_message(' The Backbone needs to be loaded into GBrowse (done by executing Tripal Jobs Loading script),
  931. Before loaded libraries or GBrowse Instance will be displayed. ');
  932. }
  933. }
  934. //-----------------------------------------------------------------------------
  935. // SECTION: Un-Register Existing GBrowse Instances
  936. //-----------------------------------------------------------------------------
  937. /**
  938. * Un-Register GBrowse Instance Form Submit
  939. *
  940. * This is the Form-Submit section of the Un-Register GBrowse instance function. Depending on
  941. * which instance was selected, it will be removed from the database. A check box is provided
  942. * which, if checked is true, will delete all data/files that was associated with the selected
  943. * GBrowse instance.SQL commands are used in order to remove the selected instances from the
  944. * SQL database.
  945. *
  946. * @param $form
  947. * the submitted form containing the user entered infromation
  948. * @param $form_state
  949. * The state of the form: i.e if check-box was checked and what gbrowse_instance was selected to be deleted
  950. * The key is the 'values'
  951. *
  952. * @return
  953. * true or false, depending wheater the check box had been selected, if true, the
  954. * selected GBrowse instance will be deleted
  955. */
  956. function tripal_gbrowse_delete_gbrowse_instance($gbrowse) {
  957. // Retrieve current gbrowse instance details
  958. $gbrowse_instance = db_fetch_object(
  959. db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $gbrowse->gbrowse_id )
  960. );
  961. //Pulling out the configuration file
  962. $config_file = $gbrowse->config_file;
  963. //Removing configuration file from the directory
  964. $command = "rm ".$config_file;
  965. exec($command);
  966. if (file_exists($config_file)) {
  967. drupal_set_message('Configuration File ('.$config_file.') not removed!', 'error');
  968. } else {
  969. drupal_set_message('Configuration File removed successfully');
  970. }
  971. $user_name = $gbrowse->database_user;
  972. $user_password = $gbrowse->user_password;
  973. $link = mysql_connect('localhost', $user_name,$user_password);
  974. if (!$link) {
  975. drupal_set_message('Could not connect: ' . mysql_error(), 'error');
  976. return false;
  977. }
  978. $sql = 'DROP DATABASE ' . $gbrowse->database_name;
  979. if(mysql_query($sql, $link)){
  980. drupal_set_message( "Database removed successfully\n");
  981. }else{
  982. drupal_set_message( 'Error removing database: ' . mysql_error(), 'error');
  983. return false;
  984. }
  985. mysql_close($link);
  986. }
  987. //-----------------------------------------------------------------------------
  988. // SECTION: Load Library features into GBrowse Form & Form Submit
  989. //-----------------------------------------------------------------------------
  990. /**
  991. * Implements hook_form
  992. *
  993. * This function creates a form for the database name, user name for database operation and a
  994. * password for access.The module uses these features to access information regarding the user's
  995. * operations and needs.
  996. *
  997. * @return
  998. * An array describing the form to be rendered
  999. */
  1000. function tripal_gbrowse_load_library_features_form() {
  1001. $new_array=array();
  1002. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  1003. foreach ($result as $value) {
  1004. $new_array[$value->library_id]=$value->name; //options for the select list
  1005. }
  1006. //Creating Fieldset for multiple fields in form
  1007. $form['load_features'] = array(
  1008. '#type' => 'fieldset'
  1009. );
  1010. //Library ID
  1011. $form['load_features']['library_id'] = array(
  1012. '#type' => 'select',
  1013. '#title' => t('Libraries'),
  1014. '#options' => $new_array,
  1015. '#description' => t('Library of Genetic Sequences.'),
  1016. );
  1017. //Sending query to the database
  1018. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1019. $items = array();
  1020. while($record = db_fetch_object($resource)){
  1021. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1022. }
  1023. //GBrowse Instances
  1024. $form['load_features']['gbrowse_id'] = array(
  1025. '#type' => 'select',
  1026. '#title' => t('GBrowse Instances'),
  1027. '#options' => $items,
  1028. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1029. );
  1030. //A checkbox to update features for the gbrowse
  1031. $form['load_features']['check_box'] = array(
  1032. '#type' => 'checkbox',
  1033. '#title' => t('Update Features'),
  1034. '#description' => t('Existing Same Name Features Updated Accordingly.'),
  1035. );
  1036. $form['load_features']['submit'] = array(
  1037. '#type' => 'submit',
  1038. '#weight' => 10,
  1039. '#value' => t('Submit Job')
  1040. );
  1041. return $form;
  1042. }
  1043. /**
  1044. * Load-Features-Form
  1045. *
  1046. * @param $form
  1047. * -The submitted form containing the user entered infromation
  1048. * @param $form_state
  1049. * -Is the state of the form: i.e what button was pressed, what infromation was entered,etc.
  1050. * The key is the 'values'
  1051. */
  1052. function tripal_gbrowse_load_library_features_form_submit($form,$form_state) {
  1053. global $user; //needed to make the current users details available so access of user id is available
  1054. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_id']));
  1055. $name = $record->database_name;
  1056. $user_name = $record->database_user;
  1057. $password= $record->user_password;
  1058. $job_args = array($name,$user_name,$password,$form_state['values']['library_id'], FALSE);
  1059. //Checking state of check box, if checked delete features from GBrowse
  1060. if($form_state['values']['check_box']==1){
  1061. //registering a delete job with the tripal system, this job calls the function when executed
  1062. $job_id = tripal_add_job('Deleting Features From GBrowse', 'tripal_gbrowse',
  1063. 'tripal_gbrowse_delete_library_features_from_gbrowse', $job_args, $user->uid);
  1064. }
  1065. //registering a job with the tripal system, this job calls the function when executed
  1066. $job_id = tripal_add_job('Loading Features Into GBrowse', 'tripal_gbrowse',
  1067. 'tripal_gbrowse_load_library_features_into_gbrowse', $job_args, $user->uid);
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. // SECTION:Load Library Features into GBrowse
  1071. //-----------------------------------------------------------------------------
  1072. /**
  1073. * Load Library features into database
  1074. *
  1075. * This loading function firstly creates a output GFF3 format file for verification purposes. Then, a 'Chado' table
  1076. * is selected,depending which table the user is interested in, from this table specific infromation
  1077. * is taken out and placed in the GFF3 file.The Featurelocation(fmin) & featurelocation (fmax) are used
  1078. * to set the featureloc.feature_id = feature.feature_id, the fmin = start & fmax = end of the desired gene.
  1079. * The testing if the phase is empty is done, if so a '.' is inserted into $phase variable. If the parent
  1080. * feature is indicated in feature location for the child feature will be set.The Attributes section
  1081. * sets the, ID, Name, Alias, Dbrexf to variables to be put into a CONF file.The strand is either a
  1082. * positive, negative, or zero value and is represented by +/./- accordingley.
  1083. *
  1084. * @param $name
  1085. * name of GBrowse database
  1086. *
  1087. * @param $user_name
  1088. * GBrowse MySQL user name
  1089. *
  1090. * @param $password
  1091. * user password for GBrowse
  1092. *
  1093. * @param $library_id
  1094. * GBrowse MySQL user name
  1095. *
  1096. * @param $clear_database
  1097. * Used to clear the database of its backbone structure
  1098. */
  1099. function tripal_gbrowse_load_library_features_into_gbrowse($database_name, $user_name, $password, $library_id,$clear_database){
  1100. //A test file created to verify output
  1101. $myFile = "/tmp/temporaryGBrowse_".time().".gff3";
  1102. //Test opening the file, will print warning if file is not able to be opened
  1103. $fh = fopen($myFile, 'w') or die("WARNING: Can not open file");
  1104. print "Creating GFF3 file of features for library...\n";
  1105. if (!preg_match('/^\d+$/', $library_id)) {
  1106. print "ERROR: Invalid Library ID supplied (Library ID = ".$library_id.")\n";
  1107. return false;
  1108. }
  1109. // Selecting all features
  1110. $values = array('library_id' => $library_id);
  1111. $prime_result = tripal_core_chado_select ( 'library_feature', array('feature_id'), $values );
  1112. // Select Table:library; column:uniquename
  1113. $result = tripal_core_chado_select ( 'library', array('uniquename'), $values );
  1114. $source = $result[0]->uniquename ;
  1115. // Check source: ERROR
  1116. //For each feature, type id form feature table, specific record the feature id containing $feature_id
  1117. foreach ($prime_result as $value) {
  1118. $feature_id = $value->feature_id;
  1119. if (!preg_match('/^\d+$/', $feature_id)) {
  1120. print "ERROR: Invalid feature ID (".$feature_id.") associated with Library (Library ID = ".$library_id.")\n";
  1121. }
  1122. $result = tripal_core_chado_select('feature', array('type_id'), array('feature_id'=>$feature_id));
  1123. $type_id = $result[0]->type_id;
  1124. if (empty($type_id)) {
  1125. print "ERROR: No Type associated with Feature (Feature ID = ".$feature_id.")\n";
  1126. }
  1127. //Taking the cvterm name from the cvterm table given the type-id($type_id)
  1128. $result = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id'=>$type_id));
  1129. //The extracted cvterm name
  1130. $type = $result[0]->name;
  1131. if (empty($type)) {
  1132. print "ERROR: Type associated with Feature (".$type_id.") doesn't exist in cvterm table (Feature ID = ".$feature_id.")\n";
  1133. }
  1134. //Featureloc.fmin & featureloc.fmax ( where featureloc.feature_id = feature.feature_id )
  1135. //fmin = start & fmax = end
  1136. $result = tripal_core_chado_select('featureloc', array('fmin','fmax', 'strand', 'phase', 'srcfeature_id'), array('feature_id'=>$feature_id));
  1137. $start = $result[0]->fmin;
  1138. if (!preg_match('/^\d+$/', $start)) {
  1139. print "ERROR: Invalid Start (".$start.") of Feature (Feature ID= ".$feature_id.")\n";
  1140. }
  1141. $end = $result[0]->fmax;
  1142. if (!preg_match('/^\d+$/', $end)) {
  1143. print "ERROR: Invalid End (".$end.") of Feature (Feature ID= ".$feature_id.")\n";
  1144. }
  1145. $score = '.';
  1146. //Converting from +1/0/-1 to +/./- as there are only three possible values
  1147. if ($result[0]->strand < 0) {
  1148. $strand= '-';
  1149. } elseif ($result[0]->strand == 0) {
  1150. $strand = '.';
  1151. } elseif($result[0]->strand > 0) {
  1152. $strand = '+';
  1153. } else{
  1154. $strand = '.';
  1155. }
  1156. //Testing if phase is empty, if so a '.' is inserted into $phase variable
  1157. $phase = $result[0]->phase;
  1158. if(empty($phase)){
  1159. $phase = '.';
  1160. }
  1161. //This is the parent feature as indicated in feature location for the child feature and
  1162. //the current line described the child feature
  1163. $srcfeature_id = $result[0]->srcfeature_id;
  1164. if (empty($srcfeature_id)) {
  1165. print "ERROR: No Parent associated with Feature Location (Feature ID = ".$feature_id.")\n";
  1166. }
  1167. $result = tripal_core_chado_select('feature', array('name'), array('feature_id'=>$srcfeature_id));
  1168. $seqid = $result[0]->name;
  1169. if (empty($seqid)) {
  1170. print "ERROR: Parent (Parent Feature ID = ".$seqid.") associated with Feature doesn't exist in feature table (Feature ID =".$feature_id."\n";
  1171. }
  1172. //Attributes section, ID, Name, Alias, Dbrexf
  1173. $result = tripal_core_chado_select('feature', array('uniquename', 'name'), array('feature_id'=>$feature_id));
  1174. $id = $result[0]->uniquename;
  1175. if (empty($id)) {
  1176. print "ERROR: Missing Unique Name for Feature (Feature ID = ".$feature_id.")\n";
  1177. }
  1178. $name = $result[0]->name;
  1179. if (empty($name)) {
  1180. print "WARNING: Missing Name for Feature (Feature ID = ".$feature_id.")\n";
  1181. }
  1182. //Synonym
  1183. $result = tripal_core_chado_select('feature_synonym', array('synonym_id'), array('feature_id'=>$feature_id));
  1184. $synonyms = array();
  1185. foreach ($result as $obj){
  1186. $synonym_id = $obj->synonym_id;
  1187. $sub_result = tripal_core_chado_select('synonym', array('name'), array('synonym_id'=>$synonym_id));
  1188. // check that $sub_result[0]->name isn't empty
  1189. if (empty($sub_result[0]->name)) {
  1190. print "ERROR: Synonym (".$synonym_id.") associated with feature not found in synonym table (Feature ID = ".$feature_id.")\n";
  1191. } else {
  1192. $synonyms[] = $sub_result[0]->name;
  1193. }
  1194. }
  1195. // Additional Dbxref's
  1196. $result = tripal_core_chado_select('feature_dbxref', array('dbxref_id'), array('feature_id'=>$feature_id));
  1197. $dbxref = array();
  1198. foreach ($result as $obj){
  1199. $dbxref_id = $obj->dbxref_id;
  1200. if (!empty($dbxref_id)) {
  1201. $sub1_result = tripal_core_chado_select('dbxref', array('db_id', 'accession'), array('dbxref_id'=>$dbxref_id));
  1202. $dbxref_accession = $sub1_result[0]->accession;
  1203. $db_id = $sub1_result[0]->db_id;
  1204. if (!empty($db_id)) {
  1205. $sub_sub_result =tripal_core_chado_select('db', array('name'), array('db_id'=>$db_id));
  1206. $db_name = $sub_sub_result[0]->name;
  1207. if (empty($db_name)) {
  1208. print "ERROR: Database (".$db_id.") associated with database reference not in db table (Database Reference ID = ".$dbxref_id.")\n";
  1209. } elseif (empty($dbxref_accession)) {
  1210. print "WARNING: Missing Accession for Database Reference (Database Reference ID = ".$dbxref_id.")\n";
  1211. } else {
  1212. $dbxref[] = $db_name . ':' . $dbxref_accession;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. //Main Dbxref
  1218. $result = tripal_core_chado_select('feature', array('dbxref_id'), array('feature_id'=>$feature_id));
  1219. $dbxref_id = $result[0]->dbxref_id;
  1220. if (empty($dbxref_id)) {
  1221. print "WARNING: No Database Reference associated directly with Feature (Feature ID = ".$feature_id.")\n";
  1222. }
  1223. $result = tripal_core_chado_select('dbxref', array('accession','db_id'), array('dbxref_id'=>$dbxref_id));
  1224. $dbxref_accession = $result[0]->accession;
  1225. //checking $dbrexf_accession for emptyness
  1226. if(!empty($dbxref_accession)){
  1227. $db_id = $result[0]->db_id;
  1228. $sub_sub_result =tripal_core_chado_select('db', array('name'), array('db_id'=>$db_id));
  1229. $db_name = $sub_sub_result[0]->name;
  1230. $dbxref[] = $db_name . ':' . $dbxref_accession;
  1231. } else {
  1232. print "WARNING: Missing Accession for Database Reference (Database Reference ID = ".$dbxref_id.")\n";
  1233. }
  1234. //attributes array is assigned given elements based on previous initializations
  1235. $attributes = array();
  1236. $attributes[] = 'ID='.$id;
  1237. if (!empty($name)) { $attributes[] = 'Name='.$name; }
  1238. if (!empty($synonyms)) { $attributes[] = 'Alias='.implode(',',$synonyms); }
  1239. if (!empty($dbxref)) { $attributes[] = 'Dbxref=' .implode(',', $dbxref); }
  1240. //write to string for printing
  1241. $stringData = implode("\t", array($seqid,$source,$type,$start,$end,$score,$strand,$phase,
  1242. implode(';',$attributes)))."\n";
  1243. //Write concatinated data ($stringData) to file
  1244. fwrite($fh, $stringData);
  1245. }
  1246. //Closing file
  1247. fclose($fh);
  1248. if($clear_database){
  1249. //The loading script: bp_seqfeature_load.pl, allows loading of data to specific file
  1250. $command= "bp_seqfeature_load.pl -c -u '" .$user_name. "' -p '" .$password. "' -d " .$database_name. " " . $myFile;
  1251. }else{
  1252. //The loading script: bp_seqfeature_load.pl, allows loading of data to specific file
  1253. $command= "bp_seqfeature_load.pl -u '" .$user_name. "' -p '" .$password. "' -d " .$database_name. " " . $myFile;
  1254. }
  1255. //optional 'print' statement to verify that the above comman is being called and executed properly
  1256. print 'Executing Command:'.$command."\n";
  1257. exec($command, $out_put, $return_var);
  1258. }
  1259. //-----------------------------------------------------------------------------
  1260. // SECTION: Delete library features from database Form & Form submit
  1261. //-----------------------------------------------------------------------------
  1262. /**
  1263. *
  1264. * This form's purpose is to delete library featues from a selected GBrowse insatance. By selecting
  1265. * a Library of genetic sequences and a GBrowse instance, the instance will be removed as will all the
  1266. * associated libraries. This form allows access to a specified database and will delete any
  1267. * selected Library features. It will return the form that contains all of the neccessary
  1268. * information to send to the 'form_submit' function.
  1269. *
  1270. * @return $form
  1271. * An array of menu items allowing deletion of features from the library
  1272. */
  1273. function tripal_gbrowse_delete_library_features_from_gbrowse_form() {
  1274. $form = array();
  1275. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  1276. foreach ($result as $value){
  1277. $newArray[$value->library_id]=$value->name; //options for the select list
  1278. }
  1279. //Creating Fieldset for multiple fields in form
  1280. $form['delete_gbrowse'] = array(
  1281. '#type' => 'fieldset',
  1282. '#title' => t(''),
  1283. );
  1284. //Library ID
  1285. $form['delete_gbrowse']['library_id'] = array(
  1286. '#type' => 'select',
  1287. '#title' => t('Libraries'),
  1288. '#options' => $newArray,
  1289. '#description' => t('Genetic Sequences.'),
  1290. );
  1291. //Sending query to the database
  1292. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1293. $items = array();
  1294. while($record = db_fetch_object($resource)){
  1295. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1296. }
  1297. $form['delete_gbrowse']['gbrowse_id'] = array(
  1298. '#type' => 'select',
  1299. '#title' => t('GBrowse Instances'),
  1300. '#options' => $items,
  1301. '#description' => t('Selected Instances to be Removed.'),
  1302. );
  1303. //Delete button to remove Library
  1304. $form['delete_gbrowse']['submit'] = array(
  1305. '#type' => 'submit',
  1306. '#weight' => 10,
  1307. '#value' => t('Delete')
  1308. );
  1309. return $form;
  1310. }
  1311. /**
  1312. * This 'form_submit' function uses MySQL commands to select the GBrowse instance from a selected database.
  1313. * Then the selected features are deleted from the library.
  1314. *
  1315. *
  1316. * @param $form
  1317. * The submitted form that
  1318. *
  1319. * @param $form_state
  1320. * The state of the form, includes the selected library and the GBrowse instance to be removed
  1321. * The key is the 'values'
  1322. */
  1323. function tripal_gbrowse_delete_library_features_from_gbrowse_form_submit($form, $form_state) {
  1324. global $user; //needed to make the current users details available so access of user id is available
  1325. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_id']));
  1326. $name = $record->database_name;
  1327. $user_name = $record->database_user;
  1328. $password= $record->user_password;
  1329. $job_args = array($name,$user_name,$password,$form_state['values']['library_id']);
  1330. //Checking state of submit button, if selected delete features from library
  1331. if($form_state['values']['op'] == 'Delete'){
  1332. //registering a delete job with the tripal system, this job calls the function when executed
  1333. $job_id = tripal_add_job('Deleting Library Features From GBrowse', 'tripal_gbrowse', 'tripal_gbrowse_delete_library_features_from_gbrowse', $job_args, $user->uid);
  1334. }
  1335. }
  1336. //-----------------------------------------------------------------------------
  1337. // SECTION: Delete Library Features from GBrowse
  1338. //-----------------------------------------------------------------------------
  1339. /**
  1340. * Delete-GBrowse-Feature
  1341. *
  1342. * This form Removes features from library that the user has specified. The form allows the user
  1343. * to select a desired instance. Once the library and instance are selected and deleted, the selected
  1344. * libraries
  1345. *
  1346. *
  1347. *
  1348. * @param $database_name
  1349. * name of GBrowse database
  1350. * @param $user_name
  1351. * GBrowse MySQL user name
  1352. * @param $password
  1353. * user password for GBrowse
  1354. * @param $library_id
  1355. * The id of the library that is being selected from
  1356. *
  1357. */
  1358. function tripal_gbrowse_delete_library_features_from_gbrowse($database_name, $user_name, $password, $library_id){
  1359. $result = tripal_core_chado_select ( 'library', array('name'), array('library_id' => $library_id)
  1360. );
  1361. $library_name = $result[0]->name;
  1362. print "Removing Features of all types related to the ".$library_name." library...\n";
  1363. $sql = 'SELECT cvterm.name FROM feature JOIN cvterm cvterm ON cvterm.cvterm_id=feature.type_id
  1364. JOIN library_feature library_feature ON library_feature.feature_id=feature.feature_id JOIN library
  1365. library ON library.library_id=library_feature.library_id WHERE library.library_id=%d GROUP BY
  1366. cvterm.name';
  1367. $previous_db = tripal_db_set_active('chado');
  1368. $resource = db_query($sql, $library_id);
  1369. tripal_db_set_active($previous_db);
  1370. while($record = db_fetch_object($resource)){
  1371. $type = $record->name;
  1372. //The loading script: bp_seqfeature_delete.pl, allows removal of data to specific file
  1373. $command= "bp_seqfeature_delete.pl -u '".$user_name."' -p '".$password."' -d '" .$database_name. "' -t '" .$type. ":".$library_name."'";
  1374. print "Executing Command: ".$command."\n";
  1375. //exec() allows for execution of a external program: given the input
  1376. exec($command);
  1377. }
  1378. }
  1379. //-----------------------------------------------------------------------------
  1380. // SECTION: Load analyses into GBrowse Form and Form Submit
  1381. //-----------------------------------------------------------------------------
  1382. /**
  1383. * Implements hook_form
  1384. *
  1385. * This function creates a form for the database name, user name for database operation and a
  1386. * password for access.The module uses these analyses to access information regarding the user's
  1387. * operations and needs.
  1388. *
  1389. * @return
  1390. * An array describing the form to be rendered
  1391. */
  1392. function tripal_gbrowse_load_analyses_form(){
  1393. $newArray=array();
  1394. $result = tripal_core_chado_select ( 'analysis', array('analysis_id','name'), array() );
  1395. foreach ($result as $value) {
  1396. $newArray[$value->analysis_id]=$value->name; //options for the select list
  1397. }
  1398. //Creating Fieldset for multiple fields in form
  1399. $form['load_analyses'] = array(
  1400. '#type' => 'fieldset',
  1401. );
  1402. //Library ID
  1403. $form['load_analyses']['analysis_id'] = array(
  1404. '#type' => 'select',
  1405. '#title' => t('Analyses'),
  1406. '#options' => $newArray,
  1407. '#description' => t('Analyses.'),
  1408. );
  1409. //Sending query to the database
  1410. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1411. $items = array();
  1412. while($record = db_fetch_object($resource)){
  1413. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1414. }
  1415. //GBrowse Instances
  1416. $form['load_analyses']['gbrowse_id'] = array(
  1417. '#type' => 'select',
  1418. '#title' => t('GBrowse Instances'),
  1419. '#options' => $items,
  1420. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1421. );
  1422. //A checkbox to update features for the gbrowse
  1423. $form['load_analyses']['check_box'] = array(
  1424. '#type' => 'checkbox',
  1425. '#title' => t('Update Analyses'),
  1426. '#description' => t('Existing Same Name Analyses Updated Accordingly.'),
  1427. );
  1428. $form['load_analyses']['submit'] = array(
  1429. '#type' => 'submit',
  1430. '#weight' => 10,
  1431. '#value' => t('Submit Job')
  1432. );
  1433. return $form;
  1434. }
  1435. /**
  1436. * Load-Features-Form
  1437. *
  1438. * @param $form
  1439. * -The submitted form containing the user entered infromation
  1440. *
  1441. * @param $form_state
  1442. * -Is the state of the form: i.e what button was pressed, what infromation was entered,etc.
  1443. * The key is the 'values'
  1444. */
  1445. function tripal_gbrowse_load_analyses_form_submit($form, $form_state) {
  1446. global $user; //needed to make the current users details available so access of user id is available
  1447. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_id']));
  1448. $name = $record->database_name;
  1449. $user_name = $record->database_user;
  1450. $password= $record->user_password;
  1451. $job_args = array($name,$user_name,$password,$form_state['values']['analysis_id'], FALSE);
  1452. //Checking state of check box, if checked delete features from GBrowse
  1453. if($form_state['values']['check_box']==1){
  1454. //registering a delete job with the tripal system, this job calls the function when executed
  1455. $job_id = tripal_add_job('Deleting Features From GBrowse', 'tripal_gbrowse',
  1456. 'tripal_gbrowse_delete_analysis_features_from_gbrowse', $job_args, $user->uid);
  1457. }
  1458. //registering a job with the tripal system, this job calls the function when executed
  1459. $job_id = tripal_add_job('Loading Features Into GBrowse', 'tripal_gbrowse',
  1460. 'tripal_gbrowse_load_analysis_features_into_gbrowse', $job_args, $user->uid);
  1461. }
  1462. //-----------------------------------------------------------------------------
  1463. // SECTION:Load Analysis Features into GBrowse
  1464. //-----------------------------------------------------------------------------
  1465. /**
  1466. * Load Analysis features into database
  1467. *
  1468. * This loading function firstly creates a output GFF3 format file for verification purposes. Then, a 'Chado' table
  1469. * is selected,depending which table the user is interested in, from this table specific infromation
  1470. * is taken out and placed in the GFF3 file.The Featurelocation(fmin) & featurelocation (fmax) are used
  1471. * to set the featureloc.feature_id = feature.feature_id, the fmin = start & fmax = end of the desired gene.
  1472. * The testing if the phase is empty is done, if so a '.' is inserted into $phase variable. If the parent
  1473. * feature is indicated in feature location for the child feature will be set.The Attributes section
  1474. * sets the, ID, Name, Alias, Dbrexf to variables to be put into a CONF file.The strand is either a
  1475. * positive, negative, or zero value and is represented by +/./- accordingley.
  1476. *
  1477. * @param $name
  1478. * name of GBrowse database
  1479. *
  1480. * @param $user_name
  1481. * GBrowse MySQL user name
  1482. *
  1483. * @param $password
  1484. * user password for GBrowse
  1485. *
  1486. * @param $library_id
  1487. * GBrowse MySQL user name
  1488. *
  1489. * @param $clear_database
  1490. * Used to clear the database of its backbone structure
  1491. */
  1492. function tripal_gbrowse_load_analysis_features_into_gbrowse($database_name, $user_name, $password, $analysis_id,$clear_database){
  1493. //print 'Analysis ID: '.$analysis_id.".\n";
  1494. //A test file created to verify output
  1495. $myFile = "/tmp/temporaryGBrowse_".time().".gff3";
  1496. //Test opening the file, will print warning if file is not able to be opened
  1497. $fh = fopen($myFile, 'w') or die("WARNING: Can not open file");
  1498. print "Creating GFF3 file of features for Analysis...\n";
  1499. if (!preg_match('/^\d+$/', $analysis_id)) {
  1500. print "ERROR: Invalid Analysis ID supplied (Analysis ID= ".$analysis_id.")\n";
  1501. return false;
  1502. }
  1503. // Selecting all analyses
  1504. $values = array('analysis_id' => $analysis_id);
  1505. $prime_result = tripal_core_chado_select ( 'analysisfeature', array('feature_id'), $values);
  1506. // Select Table:library; column:uniquename
  1507. $result = tripal_core_chado_select ( 'analysis', array('name'), $values );
  1508. $source = $result[0]->name ;
  1509. // Check source: ERROR
  1510. //For each feature, type id form feature table, specific record the feature id containing $feature_id
  1511. foreach ($prime_result as $value) {
  1512. $feature_id = $value->feature_id;
  1513. if (!preg_match('/^\d+$/', $feature_id)) {
  1514. print "ERROR: Invalid feature ID (".$feature_id.") associated with Library (Library ID = ".$analysis_id.")\n";
  1515. }
  1516. $result = tripal_core_chado_select('feature', array('type_id'), array('feature_id'=>$feature_id));
  1517. $type_id = $result[0]->type_id;
  1518. if (empty($type_id)) {
  1519. print "ERROR: No Type associated with Feature ( Feature ID = ".$feature_id.")\n";
  1520. }
  1521. //Taking the cvterm name from the cvterm table given the type-id($type_id)
  1522. $result = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id'=>$type_id));
  1523. //The extracted cvterm name
  1524. $type = $result[0]->name;
  1525. if (empty($type)) {
  1526. print "ERROR: Type associated with Feature (".$type_id.") doesn't exist in cvterm table (Feature ID = ".$feature_id.")\n";
  1527. }
  1528. //Featureloc.fmin & featureloc.fmax ( where featureloc.feature_id = feature.feature_id )
  1529. //fmin = start & fmax = end
  1530. $result = tripal_core_chado_select('featureloc', array('fmin','fmax', 'strand', 'phase', 'srcfeature_id'), array('feature_id'=>$feature_id));
  1531. $start = $result[0]->fmin;
  1532. if (!preg_match('/^\d+$/', $start)) {
  1533. print "ERROR: Invalid Start (".$start.") of Feature (Feature ID= ".$feature_id.")\n";
  1534. }
  1535. $end = $result[0]->fmax;
  1536. if (!preg_match('/^\d+$/', $end)) {
  1537. print "ERROR: Invalid End (".$end.") of Feature (Feature ID= ".$feature_id.")\n";
  1538. }
  1539. $score = '.';
  1540. //Converting from +1/0/-1 to +/./- as there are only three possible values
  1541. if ($result[0]->strand < 0) {
  1542. $strand= '-';
  1543. } elseif ($result[0]->strand == 0) {
  1544. $strand = '.';
  1545. } elseif($result[0]->strand > 0) {
  1546. $strand = '+';
  1547. } else{
  1548. $strand = '.';
  1549. }
  1550. //Testing if phase is empty, if so a '.' is inserted into $phase variable
  1551. $phase = $result[0]->phase;
  1552. if(empty($phase)){
  1553. $phase = '.';
  1554. }
  1555. //This is the parent feature as indicated in feature location for the child feature and
  1556. //the current line described the child feature
  1557. $srcfeature_id = $result[0]->srcfeature_id;
  1558. if (empty($srcfeature_id)) {
  1559. print "ERROR: No Parent associated with Feature Location (Feature ID = ".$feature_id.")\n";
  1560. }
  1561. $result = tripal_core_chado_select('feature', array('name'), array('feature_id'=>$srcfeature_id));
  1562. $seqid = $result[0]->name;
  1563. if (empty($seqid)) {
  1564. print "ERROR: Parent (Parent Feature ID = ".$seqid.") associated with Feature doesn't exist in feature table (Feature ID =".$feature_id."\n";
  1565. }
  1566. //Attributes section, ID, Name, Alias, Dbrexf
  1567. $result = tripal_core_chado_select('feature', array('uniquename', 'name'), array('feature_id'=>$feature_id));
  1568. $id = $result[0]->uniquename;
  1569. if (empty($id)) {
  1570. print "ERROR: Missing Unique Name for Feature (Feature ID = ".$feature_id.")\n";
  1571. }
  1572. $name = $result[0]->name;
  1573. if (empty($name)) {
  1574. print "WARNING: Missing Name for Feature (Feature ID = ".$feature_id.")\n";
  1575. }
  1576. //Synonym
  1577. $result = tripal_core_chado_select('feature_synonym', array('synonym_id'), array('feature_id'=>$feature_id));
  1578. $synonyms = array();
  1579. foreach ($result as $obj){
  1580. $synonym_id = $obj->synonym_id;
  1581. $sub_result = tripal_core_chado_select('synonym', array('name'), array('synonym_id'=>$synonym_id));
  1582. // check that $sub_result[0]->name isn't empty
  1583. if (empty($sub_result[0]->name)) {
  1584. print "ERROR: Synonym (".$synonym_id.") associated with feature not found in synonym table (Feature ID = ".$feature_id.")\n";
  1585. } else {
  1586. $synonyms[] = $sub_result[0]->name;
  1587. }
  1588. }
  1589. // Additional Dbxref's
  1590. $result = tripal_core_chado_select('feature_dbxref', array('dbxref_id'), array('feature_id'=>$feature_id));
  1591. $dbxref = array();
  1592. foreach ($result as $obj){
  1593. $dbxref_id = $obj->dbxref_id;
  1594. if (!empty($dbxref_id)) {
  1595. $sub1_result = tripal_core_chado_select('dbxref', array('db_id', 'accession'), array('dbxref_id'=>$dbxref_id));
  1596. $dbxref_accession = $sub1_result[0]->accession;
  1597. $db_id = $sub1_result[0]->db_id;
  1598. if (!empty($db_id)) {
  1599. $sub_sub_result =tripal_core_chado_select('db', array('name'), array('db_id'=>$db_id));
  1600. $db_name = $sub_sub_result[0]->name;
  1601. if (empty($db_name)) {
  1602. print "ERROR: Database (".$db_id.") associated with database reference not in db table (Database Reference ID = ".$dbxref_id.")\n";
  1603. } elseif (empty($dbxref_accession)) {
  1604. print "WARNING: Missing Accession for Database Reference (Database Reference ID = ".$dbxref_id.")\n";
  1605. } else {
  1606. $dbxref[] = $db_name . ':' . $dbxref_accession;
  1607. }
  1608. }
  1609. }
  1610. }
  1611. //Main Dbxref
  1612. $result = tripal_core_chado_select('feature', array('dbxref_id'), array('feature_id'=>$feature_id));
  1613. $dbxref_id = $result[0]->dbxref_id;
  1614. if (empty($dbxref_id)) {
  1615. print "WARNING: No Database Reference associated directly with Feature (Feature ID = ".$feature_id.")\n";
  1616. }
  1617. $result = tripal_core_chado_select('dbxref', array('accession','db_id'), array('dbxref_id'=>$dbxref_id));
  1618. $dbxref_accession = $result[0]->accession;
  1619. //checking $dbrexf_accession for emptyness
  1620. if(!empty($dbxref_accession)){
  1621. $db_id = $result[0]->db_id;
  1622. $sub_sub_result =tripal_core_chado_select('db', array('name'), array('db_id'=>$db_id));
  1623. $db_name = $sub_sub_result[0]->name;
  1624. $dbxref[] = $db_name . ':' . $dbxref_accession;
  1625. } else {
  1626. print "WARNING: Missing Accession for Database Reference (Database Reference ID = ".$dbxref_id.")\n";
  1627. }
  1628. //attributes array is assigned given elements based on previous initializations
  1629. $attributes = array();
  1630. $attributes[] = 'ID='.$id;
  1631. if (!empty($name)) {
  1632. $attributes[] = 'Name='.$name;
  1633. }
  1634. if (!empty($synonyms)) {
  1635. $attributes[] = 'Alias='.implode(',',$synonyms);
  1636. }
  1637. if (!empty($dbxref)) {
  1638. $attributes[] = 'Dbxref=' .implode(',', $dbxref);
  1639. }
  1640. //write to string for printing
  1641. $stringData = implode("\t", array($seqid,$source,$type,$start,$end,$score,$strand,$phase,
  1642. implode(';',$attributes)))."\n";
  1643. //Write concatinated data ($stringData) to file
  1644. fwrite($fh, $stringData);
  1645. }
  1646. //Closing file
  1647. fclose($fh);
  1648. if($clear_database){
  1649. //The loading script: bp_seqfeature_load.pl, allows loading of data to specific file
  1650. $command= "bp_seqfeature_load.pl -c -u '" .$user_name. "' -p '" .$password. "' -d " .$database_name. " " . $myFile;
  1651. }else{
  1652. //The loading script: bp_seqfeature_load.pl, allows loading of data to specific file
  1653. $command= "bp_seqfeature_load.pl -u '" .$user_name. "' -p '" .$password. "' -d " .$database_name. " " . $myFile;
  1654. }
  1655. //optional 'print' statement to verify that the above comman is being called and executed properly
  1656. print 'Executing Command:'.$command."\n";
  1657. exec($command, $out_put, $return_var);
  1658. }
  1659. //-----------------------------------------------------------------------------
  1660. // SECTION: Delete analysis features from database Form & Form submit
  1661. //-----------------------------------------------------------------------------
  1662. /**
  1663. * This form's purpose is to delete analysis featues from a selected GBrowse insatance. By selecting
  1664. * a Library of analyses and a GBrowse instance, the instance will be removed as will all the
  1665. * associated analysis library. This form allows access to a specified database and will delete any
  1666. * selected Analysis features. It will return the form that contains all of the neccessary
  1667. * information to send to the 'form_submit' function.
  1668. *
  1669. * @return $form
  1670. * An array of menu items allowing deletion of features from the library
  1671. */
  1672. function tripal_gbrowse_delete_analysis_features_from_gbrowse_form() {
  1673. $form = array();
  1674. $result = tripal_core_chado_select ( 'analysis', array('analysis_id','name'), array() );
  1675. foreach ($result as $value){
  1676. $newArray[$value->analysis_id]=$value->name; //options for the select list
  1677. }
  1678. //Creating Fieldset for multiple fields in form
  1679. $form['delete_analysis'] = array(
  1680. '#type' => 'fieldset',
  1681. '#title' => t(''),
  1682. );
  1683. //Library ID
  1684. $form['delete_analysis']['analysis_id'] = array(
  1685. '#type' => 'select',
  1686. '#title' => t('Analysis Features'),
  1687. '#options' => $newArray,
  1688. '#description' => t('Analysis.'),
  1689. );
  1690. //Sending query to the database
  1691. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1692. $items = array();
  1693. while($record = db_fetch_object($resource)){
  1694. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1695. }
  1696. $form['delete_analysis']['gbrowse_id'] = array(
  1697. '#type' => 'select',
  1698. '#title' => t('GBrowse Instances'),
  1699. '#options' => $items,
  1700. '#description' => t('Selected Instances to be Removed.'),
  1701. );
  1702. //Delete button to remove Library
  1703. $form['delete_analysis']['submit'] = array(
  1704. '#type' => 'submit',
  1705. '#weight' => 10,
  1706. '#value' => t('Delete')
  1707. );
  1708. return $form;
  1709. }
  1710. /**
  1711. * This 'form_submit' function uses MySQL commands to select the GBrowse instance from a selected database.
  1712. * Then the selected features are deleted from the library.
  1713. *
  1714. *
  1715. * @param $form
  1716. * The submitted form that
  1717. *
  1718. * @param $form_state
  1719. * The state of the form, includes the selected library and the GBrowse instance to be removed
  1720. * The key is the 'values'
  1721. */
  1722. function tripal_gbrowse_delete_analysis_features_from_gbrowse_form_submit($form, $form_state) {
  1723. global $user; //needed to make the current users details available so access of user id is available
  1724. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_id']));
  1725. $name = $record->database_name;
  1726. $user_name = $record->database_user;
  1727. $password= $record->user_password;
  1728. $job_args = array($name,$user_name,$password,$form_state['values']['analysis_id']);
  1729. //Checking state of submit button, if selected delete features from library
  1730. if($form_state['values']['op'] == 'Delete'){
  1731. //registering a delete job with the tripal system, this job calls the function when executed
  1732. $job_id = tripal_add_job('Deleting Analysis Features From GBrowse', 'tripal_gbrowse', 'tripal_gbrowse_delete_analysis_features_from_gbrowse', $job_args, $user->uid);
  1733. }
  1734. }
  1735. //-----------------------------------------------------------------------------
  1736. // SECTION: Delete Analysis Features from GBrowse
  1737. //-----------------------------------------------------------------------------
  1738. /**
  1739. * Delete-GBrowse-Feature
  1740. *
  1741. * This form Removes features from Analysis that the user has specified. The form allows the user
  1742. * to select a desired instance. Once the analysis and instance are selected and deleted, the selected
  1743. * analyses will be disassociated with the specified instance.
  1744. *
  1745. * @param $database_name
  1746. * name of GBrowse database
  1747. *
  1748. * @param $user_name
  1749. * GBrowse MySQL user name
  1750. *
  1751. * @param $password
  1752. * user password for GBrowse
  1753. *
  1754. * @param $library_id
  1755. * The id of the library that is being selected from
  1756. *
  1757. */
  1758. function tripal_gbrowse_delete_analysis_features_from_gbrowse($database_name, $user_name, $password, $analysis_id){
  1759. $result = tripal_core_chado_select ( 'analysis', array('name'), array('analysis_id' => $analysis_id)
  1760. );
  1761. $analysis_name = $result[0]->name;
  1762. print "Removing Analysis Features of all types related to the ".$analysis_name." analysis...\n";
  1763. $sql = 'SELECT cvterm.name FROM feature JOIN cvterm cvterm ON cvterm.cvterm_id=feature.type_id
  1764. JOIN analysisfeature analysisfeature ON analysisfeature.feature_id=feature.feature_id JOIN analysis
  1765. analysis ON analysis.analysis_id=analysisfeature.analysis_id WHERE analysis.analysis_id=%d GROUP BY
  1766. cvterm.name';
  1767. $previous_db = tripal_db_set_active('chado');
  1768. $resource = db_query($sql, $analysis_id);
  1769. tripal_db_set_active($previous_db);
  1770. while($record = db_fetch_object($resource)){
  1771. $type = $record->name;
  1772. //The loading script: bp_seqfeature_delete.pl, allows removal of data to specific file
  1773. $command= "bp_seqfeature_delete.pl -u '".$user_name."' -p '".$password."' -d '" .$database_name. "' -t '" .$type. ":".$analysis_name."'";
  1774. print "Executing Command: ".$command."\n";
  1775. //exec() allows for execution of a external program: given the input
  1776. exec($command);
  1777. }
  1778. }
  1779. //-----------------------------------------------------------------------------
  1780. // SECTION: Add Tracks Form & Form submit
  1781. //-----------------------------------------------------------------------------
  1782. /**
  1783. * Add-GBrowse-Tracks
  1784. * This Menu item is implemented to allow users to define and add tracks including their name,
  1785. * glyph, colour (fore and back ground), size and link. By adding tracks, the user is able to
  1786. * visually inspect the features of their genomic data. The Glyph library has been hardcoded
  1787. * into the software and represents the most used Glyph representations. The "Glyph" option
  1788. * determines the shape of each of the displayed genomic features associated with a specific
  1789. * GBrowse track. It presents a configurable set of parameters specific to its type allowing
  1790. * further control over the display features such as height, color (foreground & backround) etc.
  1791. *
  1792. *
  1793. * @return
  1794. * An '$form' that is an array of menu items that allows the user to Register a GBrowse instance
  1795. */
  1796. function tripal_gbrowse_add_tracks_form() {
  1797. //Creating Fieldset for multiple fields in form
  1798. $form['add_tracks_field_set'] = array(
  1799. '#type' => 'fieldset',
  1800. '#title' => t('Content-Related Fields'),
  1801. );
  1802. //Sending query to the database for GBrowse Instances
  1803. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  1804. $items = array();
  1805. while($record = db_fetch_object($resource)){
  1806. $items[$record->gbrowse_id]= $record->gbrowse_name;
  1807. }
  1808. //GBrowse Instances
  1809. $form['add_tracks_field_set']['gbrowse_instances'] = array(
  1810. '#type' => 'select',
  1811. '#title' => t('Registered GBrowse Instances'),
  1812. '#options' => $items,
  1813. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1814. '#required' => TRUE,
  1815. );
  1816. //Feature Type
  1817. $sql = 'SELECT name as type, cvterm_id '
  1818. .'FROM cvterm '
  1819. .'WHERE cvterm_id IN (SELECT type_id FROM feature)';
  1820. $previous_db = tripal_db_set_active('chado');
  1821. $resource = db_query($sql);
  1822. tripal_db_set_active($previous_db);
  1823. $items = array();
  1824. while($record = db_fetch_object($resource)){
  1825. $items[$record->type] = $record->type;
  1826. }
  1827. $form['add_tracks_field_set']['type_name'] = array(
  1828. '#type' => 'select',
  1829. '#title' => t('Feature Type'),
  1830. '#options' => $items,
  1831. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1832. '#required' => TRUE,
  1833. );
  1834. //Populating Select list for Library
  1835. $newArray['none'] = 'None'; //No option for select list
  1836. $result = tripal_core_chado_select ( 'library', array('library_id','name'), array() );
  1837. foreach ($result as $value) {
  1838. $newArray[$value->name]=$value->name; //options for the select list
  1839. }
  1840. //Library
  1841. $form['add_tracks_field_set']['library_id'] = array(
  1842. '#type' => 'select',
  1843. '#title' => t('Library'),
  1844. '#options' => $newArray,
  1845. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  1846. );
  1847. //Creating Fieldset for multiple fields in form
  1848. $form['track_text'] = array(
  1849. '#type' => 'fieldset',
  1850. '#title' => 'Track-Specific Fields'
  1851. );
  1852. //Specify Track Identifier
  1853. $form['track_text']['identifier'] = array(
  1854. '#type' => 'textfield',
  1855. '#title' => t('Track Identifier'),
  1856. '#size' => 30,
  1857. '#maxlength' => 64,
  1858. '#description' => t('Specify Track Identifier (Must be Unique and Specific)'),
  1859. '#required' => TRUE,
  1860. );
  1861. //Specify Track Name
  1862. $form['track_text']['track_name'] = array(
  1863. '#type' => 'textfield',
  1864. '#title' => t('Track Name'),
  1865. '#size' => 30,
  1866. '#maxlength' => 64,
  1867. '#description' => t('Specify Track Name (Must be Unique and Specific)'),
  1868. '#required' => TRUE,
  1869. );
  1870. //Populating Array of Glyph List
  1871. $item = array();
  1872. $item[''] = 'Default Glyph';
  1873. $item['generic'] = 'Generic';
  1874. $item['allele_tower '] = 'Allele Tower';
  1875. $item['anchored_arrow'] = 'Anchored Arrow';
  1876. $item['arrow'] = 'Arrow';
  1877. $item['box'] = 'Box';
  1878. $item['cds'] = 'CDS';
  1879. $item['crossbox'] = 'Cross Box';
  1880. $item['diamond'] = 'Diamond';
  1881. $item['dna'] = 'DNA';
  1882. $item['dot'] = 'Dot';
  1883. $item['ellipse'] = 'Ellipse';
  1884. $item['extending_arrow'] = 'Extending Arrow';
  1885. $item['generic'] = 'Generic';
  1886. $item['graded_segments'] = 'Graded Segments';
  1887. $item['heterogeneous_segments '] = 'Heterogeneous Segments';
  1888. $item['image'] = 'Image';
  1889. $item['line'] = 'Line';
  1890. $item['primers'] = 'Primers';
  1891. $item['processed_transcript'] = 'Processed Transcript';
  1892. $item['rndrect'] = 'Rndrect';
  1893. $item['ruler_arrow'] = 'Ruler Arrow';
  1894. $item['segments'] = 'Segments';
  1895. $item['span'] = 'Span';
  1896. $item['toomany'] = 'Too Many';
  1897. $item['transcript'] = 'Transcript';
  1898. $item['transcript2'] = 'Transcript2';
  1899. $item['translation'] = 'Translation';
  1900. $item['triangle'] = 'Triangle';
  1901. $item['wiggle_densit'] = 'Wiggle Densit';
  1902. $item['wiggle_xyplot'] = 'Wiggle XYplot';
  1903. $item['xyplot'] = 'XYplot';
  1904. //Glyphs
  1905. $form['track_text']['glyph'] = array(
  1906. '#type' => 'select',
  1907. '#title' => t('Glyph'),
  1908. '#options' => $item,
  1909. '#description' => t('Shape of Genomic Feature Track '),
  1910. );
  1911. //Modify The foreground colour of track
  1912. $form['track_text']['foreground_colour'] = array(
  1913. '#type' => 'textfield',
  1914. '#title' => t('Foreground Colour'),
  1915. '#size' => 30,
  1916. '#maxlength' => 64,
  1917. '#description' => t('Enter name/Hex-code for identifying foreground colour of glyph.
  1918. List of available colours here:'.l("GBrowse Colour Palette.pdf",
  1919. "http://gmod.org/mediawiki/images/a/ae/Gbrowse_Color_Palette.pdf")),
  1920. );
  1921. //Modify The Backround colour of track
  1922. $form['track_text']['backround_colour'] = array(
  1923. '#type' => 'textfield',
  1924. '#title' => t('Background Colour'),
  1925. '#size' => 30,
  1926. '#maxlength' => 20,
  1927. '#description' => t('Enter name/Hex-code for identifying background colour of glyph.
  1928. List of available colours here:'.l("GBrowse Colour Palette.pdf",
  1929. "http://gmod.org/mediawiki/images/a/ae/Gbrowse_Color_Palette.pdf")),
  1930. );
  1931. //Modify The Height of track
  1932. $form['track_text']['track_height'] = array(
  1933. '#type' => 'textfield',
  1934. '#title' => t('Height'),
  1935. '#size' => 30,
  1936. '#maxlength' => 20,
  1937. '#description' => t('Modify Track Height'),
  1938. );
  1939. //Specify Link
  1940. $form['track_text']['link'] = array(
  1941. '#type' => 'textfield',
  1942. '#title' => t('Link'),
  1943. '#size' => 30,
  1944. '#maxlength' => 64,
  1945. '#description' => t('Specify to Further details (HTML address)'),
  1946. );
  1947. //Register job submit button
  1948. $form['create_instance']['submit'] = array(
  1949. '#type' => 'submit',
  1950. '#value' => t('Add Track')
  1951. );
  1952. return $form;
  1953. }
  1954. /**
  1955. * Validate User-submitted data for Register GBrowse Instances
  1956. *
  1957. * This function validates the user input for each form element in the Register GBrowse
  1958. * instances form that is a text field. Validations checked include:
  1959. * - database_name: contains only word characters not including punctuation
  1960. * or white-space ([A-Z][a-z][0-1]_)
  1961. *
  1962. * @param $form
  1963. * The form that the user submitted and whose input is being validated
  1964. *
  1965. * @param $form_state
  1966. * An array containing the state of the form being validated including user-input
  1967. */
  1968. function tripal_gbrowse_add_tracks_form_validate ($form, $form_state) {
  1969. if (!empty($form_state['values']['foreground_colour'])) {
  1970. // foreground colour is present and a valid colour
  1971. if ( !preg_match('/^[\w#]+$/', $form_state['values']['foreground_colour'])) {
  1972. form_set_error('foreground_colour',
  1973. 'The Foreground Colour should be either the name of a colour listed in the linked to pdf or a valid hex colour');
  1974. }
  1975. }
  1976. if (!empty($form_state['values']['backround_colour'])) {
  1977. if(!preg_match('/^[\w#]+$/', $form_state['values']['backround_colour'])){
  1978. 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');
  1979. }
  1980. }
  1981. if (!empty($form_state['values']['track_height'])){
  1982. if(!preg_match('/^\d*$/', $form_state['values']['track_height'])){
  1983. form_set_error('track_height','The Track Height must be a positive integer');
  1984. }
  1985. }
  1986. if(!preg_match('/^\w+$/', $form_state['values']['identifier'])){
  1987. form_set_error('identifier','The Identifier must be composed of the following characters only: [A-Z][a-z][0-9] and _');
  1988. }
  1989. if (!empty($form_state['values']['link'])){
  1990. if(!preg_match('/^http:/', $form_state['values']['link'])){
  1991. form_set_error('link','The Link must be a conventional web site name, beginging with http: ');
  1992. }
  1993. }
  1994. }
  1995. /**
  1996. *Add-GBrowse-Tracks Form_Submit
  1997. *
  1998. * @param $form
  1999. * The form for the information that the user has entered
  2000. *
  2001. * @param $form_state
  2002. * This is the state of the form: i.e what button was pressed.The key is
  2003. * the name of the fields in database
  2004. *
  2005. * @return
  2006. * An '$form' that is an array of menu items that allows the user to Register a GBrowse instance
  2007. */
  2008. function tripal_gbrowse_add_tracks_form_submit($form, $form_state) {
  2009. $string = "\n[".$form_state['values']['identifier']."]\n"
  2010. ."key = ".$form_state['values']['track_name']."\n";
  2011. if($form_state['values']['library_id'] == 'none'){
  2012. $string .= "feature = ".$form_state['values']['type_name']."\n";
  2013. } else {
  2014. $string .= "feature = ".$form_state['values']['type_name'].":" .$form_state['values']['library_id']."\n";
  2015. }
  2016. if (!empty($form_state['values']['glyph'])) {
  2017. $string .= "glyph = ".$form_state['values']['glyph']."\n";
  2018. }
  2019. if (!empty($form_state['values']['track_height'])) {
  2020. $string .= "height = ".$form_state['values']['track_height']."\n";
  2021. }
  2022. if (!empty($form_state['values']['foreground_colour'])) {
  2023. $string .= "bgcolor = ".$form_state['values']['foreground_colour']."\n";
  2024. }
  2025. if (!empty($form_state['values']['backround_colour'])) {
  2026. $string .= "fgcolor = ".$form_state['values']['backround_colour']."\n";
  2027. }
  2028. if (!empty($form_state['values']['link'])) {
  2029. $string .= "link = ".$form_state['values']['link']."\n";
  2030. }
  2031. $string .= "\n";
  2032. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['values']['gbrowse_instances']));
  2033. $config_file = $record->config_file;
  2034. $command = "echo '".$string."' >> ".$config_file;
  2035. exec($command);
  2036. }
  2037. //-----------------------------------------------------------------------------
  2038. // SECTION: Delete Tracks Form & Form submit
  2039. //-----------------------------------------------------------------------------
  2040. /**
  2041. *Delete-GBrowse-Tracks
  2042. *
  2043. * This Form allows selection for a specific GBrowse Instance to be removed in a multi-submit form outline.
  2044. * Once the GBrowse instance is selected,the corresponding track list that corresponds to
  2045. * the selected GBrowse instance is deleted. This can be done one track at a time. As
  2046. * there is no limit to the number of tracks that can be added to a particular GBrowse instance and
  2047. * thus all the tracks that have bee added can be deleted, one track at a time. The track that is to
  2048. * be deleted is to be selected from the list. When selected and the delete button is pushed,
  2049. * the track that was selected is removed from the 'configuration' file.
  2050. *
  2051. * @param $form_state
  2052. * The state of the form as it is setting the default value of $form_state is NULL
  2053. *
  2054. * @return
  2055. * An '$form' that is an array of menu items that allows the user to Register a GBrowse instance
  2056. */
  2057. function tripal_gbrowse_delete_tracks_form(&$form_state=NULL) {
  2058. //Creating Fieldset for multiple fields in form
  2059. $form['delete_tracks_field_set'] = array(
  2060. '#type' => ($form_state['storage']['gbrowse_id']) ? 'hidden' : 'fieldset',
  2061. '#title' => t('GBrowse Specific Fields'),
  2062. );
  2063. //Sending query to the database for GBrowse Instances
  2064. $resource = db_query('SELECT * FROM {tripal_gbrowse_instances}');
  2065. $items = array();
  2066. while($record = db_fetch_object($resource)){
  2067. $items[$record->gbrowse_id]= $record->gbrowse_name;
  2068. }
  2069. //GBrowse Instance Select List
  2070. $form['delete_tracks_field_set']['gbrowse_instances'] = array(
  2071. '#type' => 'select',
  2072. '#title' => t('Registered GBrowse Instances'),
  2073. '#options' => $items,
  2074. '#description' => t('Selected GBrowse Instances to be Loaded.'),
  2075. '#default_value' => $form_state['values']['gbrowse_instances'],
  2076. );
  2077. //Set Instance Submit button
  2078. $form['delete_tracks_field_set']['submit_instance'] = array(
  2079. '#type' => 'submit',
  2080. '#value' => t('Set Instance')
  2081. );
  2082. //Creating Fieldset for multiple fields in form
  2083. $form['delete_tracks'] = array(
  2084. '#type' => ($form_state['storage']['gbrowse_id']) ? 'fieldset' : 'hidden',
  2085. '#title' => t('Track Specific Fields'),
  2086. );
  2087. $gbrowse_id = $form_state['storage']['gbrowse_id'];
  2088. if ($gbrowse_id){
  2089. $items = track_name_options($gbrowse_id);
  2090. }else {
  2091. $items = array();
  2092. }
  2093. //Track Name Select List
  2094. $form['delete_tracks']['track_line_number'] = array(
  2095. '#type' => 'select',
  2096. '#title' => t('Track Name'),
  2097. '#options' => $items,
  2098. '#description' => t('Name of Track to Remove '),
  2099. '#default_value' => $form_state['values']['gbrowse_id'],
  2100. );
  2101. //Remove Track button
  2102. $form['delete_tracks']['remove_track'] = array(
  2103. '#type' => 'submit',
  2104. '#value' => t('Remove Track')
  2105. );
  2106. return $form;
  2107. }
  2108. /**
  2109. *Delete-GBrowse-Tracks
  2110. *
  2111. *@param $form
  2112. * The form that is completed in the above section
  2113. *
  2114. * @param $form_state
  2115. * The state of the form, as it is completed by the user and will contain all of the user's
  2116. * selected infromation that is to be removed from the configuration file.
  2117. *
  2118. */
  2119. function tripal_gbrowse_delete_tracks_form_submit($form, &$form_state) {
  2120. $form_state['storage']['gbrowse_id'] = $form_state['values']['gbrowse_instances'];
  2121. //Checking that the "Remove Track" button was selected
  2122. if($form_state['clicked_button']['#value']=='Remove Track'){
  2123. //selecting GBrowse-ID from the database
  2124. $resource= db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $form_state['storage']['gbrowse_id']));
  2125. //putting contents of the configuration file into an variable
  2126. $config_file = (string) $resource->config_file;
  2127. $read_file_handle = fopen($config_file,'r') or drupal_set_message("WARNING: Can not open file:" .$config_file, 'error');
  2128. $track_number = $form_state['values']['track_line_number'];
  2129. $line_num = 0;
  2130. //create temp file
  2131. $new_conf_file = '/tmp/temp_config_file.temp';
  2132. //making '.temp' file in the /tmp directory
  2133. $command = 'touch /tmp/temp_config_file.temp';
  2134. exec($command);
  2135. //Checking that the newly created file is writable
  2136. if (is_writable($new_conf_file)) {
  2137. //opening file for writing to
  2138. $write_file_handle = fopen($new_conf_file, 'w') or drupal_set_message("WARNING: Can not open file:" .$new_conf_file, 'error');
  2139. }else{
  2140. drupal_set_message("WARNING: Attempting to create a temporary file -insufficient permissions: ".$new_conf_file, 'error');
  2141. }
  2142. while(!feof($read_file_handle)){
  2143. $file_line = fgets($read_file_handle);
  2144. $line_num++;
  2145. if($line_num == $track_number){
  2146. $remove_track = TRUE;
  2147. }
  2148. if(preg_match('/^\s*$/',$file_line)){
  2149. $remove_track=FALSE;
  2150. }
  2151. if($remove_track==FALSE){
  2152. fwrite($write_file_handle, $file_line);
  2153. }
  2154. }
  2155. $command = 'cp '.$new_conf_file.' '.$config_file ;
  2156. exec($command);
  2157. $command = 'rm '.$new_conf_file ;
  2158. exec($command);
  2159. }
  2160. }
  2161. /**
  2162. * Populate Track-Name List
  2163. *
  2164. * This section takes a GBrowse ID so the coresponding configuration file can be accessed. Once it has been
  2165. * obtained from the database, by the db_query() command. The configuration file is is searched for lines
  2166. * that start with "[ ]". This corresponds to the framework of the conf file, as the Track Names are
  2167. * enclosed within "[ ]". The file is then scanned from the spots that contain the "[ ]".
  2168. *
  2169. *
  2170. * @param gbrowse_id
  2171. * The Selected GBrowse instance's identification
  2172. *
  2173. * @return
  2174. * $items() and array with the tracks that are in the selected GBrowse instance.
  2175. */
  2176. function track_name_options($gbrowse_id) {
  2177. $items = array();
  2178. // Use GBrowse ID to get configuration filename
  2179. $record = db_fetch_object(db_query('SELECT * FROM {tripal_gbrowse_instances} WHERE gbrowse_id=%d', $gbrowse_id));
  2180. //putting contents of the configuration file into an variable
  2181. $config_file = (string) $record->config_file;
  2182. $config_array = array();
  2183. //command to select the lines that begin with '[' and have characters within the '[ ]'
  2184. $command = 'grep -n "^\[.*\]" ' .$config_file;
  2185. //execution of the command
  2186. exec($command , $config_array);
  2187. $config_array_size = count($config_array);
  2188. //while the end-of-file has not been reached
  2189. for($i = 0 ; $i < $config_array_size; $i++){
  2190. $file_line = $config_array[$i];
  2191. if ($is_track_definition){
  2192. if(preg_match('/^(\d+):\[(.+)\]$/', $file_line, $matches)){
  2193. $items[$matches[1]] = $matches[2];
  2194. }
  2195. }
  2196. if(preg_match('/\[TRACK DEFAULTS\]/', $file_line)){
  2197. $is_track_definition = TRUE;
  2198. }
  2199. }
  2200. return $items;
  2201. }
  2202. //-----------------------------------------------------------------------------
  2203. // END OF SOFTWARE
  2204. //-----------------------------------------------------------------------------