tripal_gbrowse.module 99 KB

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