tripal_gbrowse.module 101 KB

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