tripal_gbrowse.module 100 KB

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