| 1 | package Bio::Graphics::Browser2::Util; | ||
| 2 | |||
| 3 | # a package of useful internal routines for GBrowse | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Bio::Graphics::Browser2::Util -- Exported utilities | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Bio::Graphics::Browser2::Util; | ||
| 12 | |||
| 13 | my $r = modperl_request(); | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | This package provides functions that support the Generic Genome | ||
| 18 | Browser. It is not currently designed for external use. | ||
| 19 | |||
| 20 | =head2 FUNCTIONS | ||
| 21 | |||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use base 'Exporter'; | ||
| 27 | use Text::ParseWords qw(); | ||
| 28 | use Carp 'carp','cluck'; | ||
| 29 | use Digest::MD5; | ||
| 30 | |||
| 31 | our @EXPORT = qw(modperl_request error citation shellwords url_label); | ||
| 32 | our @EXPORT_OK = qw(modperl_request error citation shellwords url_label segment_str); | ||
| 33 | |||
| 34 | my $CURRENT_LANGUAGE; | ||
| 35 | |||
| 36 | use constant DEBUG => 0; | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item my $r = modperl_request() | ||
| 41 | |||
| 42 | Return an Apache2::Request or an Apache::Request object, depending on | ||
| 43 | whichever version of Apache is running. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub modperl_request { | ||
| 48 | return unless $ENV{MOD_PERL}; | ||
| 49 | (exists $ENV{MOD_PERL_API_VERSION} && | ||
| 50 | $ENV{MOD_PERL_API_VERSION} >= 2 ) ? Apache2::RequestUtil->request | ||
| 51 | : Apache->request; | ||
| 52 | } | ||
| 53 | |||
| 54 | =item error('message') | ||
| 55 | |||
| 56 | Prints an error message | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub error { | ||
| 61 | my ($msg,$details) = @_; | ||
| 62 | warn "@_";# if DEBUG; | ||
| 63 | $msg =~ s/'/\\'/g; | ||
| 64 | $details =~ s/'/\\'/g; | ||
| 65 | $msg =~ s/\n/<br>/g; | ||
| 66 | $details =~ s/\n/<br>/g; | ||
| 67 | print CGI::script({-type=>'text/javascript'}, | ||
| 68 | qq[Controller.show_error('$msg','$details')]); | ||
| 69 | # print CGI::h2({-class=>'error'},@msg); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | =item url_label($yucky_url) | ||
| 74 | |||
| 75 | Creates a label.alias for URL strings starting with 'http' or 'ftp'. | ||
| 76 | The last word (following a '/') in the url is used for the label. | ||
| 77 | Returns a string "url:label". | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub url_label { | ||
| 82 | my $label = shift; | ||
| 83 | my $key; | ||
| 84 | if ($label =~ m!^(?:http|ftp)://([^/]+)!) { | ||
| 85 | my $l = $label; | ||
| 86 | my $host = $1; | ||
| 87 | $l =~ s!^\W+//!!; | ||
| 88 | my (undef,$type) = $l =~ /\S+t(ype)?=([^;\&]+)/; | ||
| 89 | $l =~ s/\?.+//; | ||
| 90 | ($key) = grep /$_/, reverse split('/',$l); | ||
| 91 | $key = "$host/$key" if $key; | ||
| 92 | $key .= ":$type" if $type; | ||
| 93 | } | ||
| 94 | return $key || $label; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | =item citation(DataSource, 'label, [Language]) | ||
| 99 | |||
| 100 | Returns a track citation | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub citation { | ||
| 105 | my $data_source = shift; | ||
| 106 | my $label = shift; | ||
| 107 | my $language = shift; | ||
| 108 | if ($label =~ /^plugin\:/) { | ||
| 109 | my $label_fix = $'; | ||
| 110 | $label_fix =~ s/\:detail$//; | ||
| 111 | $label = join(":",($label_fix,'plugin')); | ||
| 112 | } | ||
| 113 | my $c; | ||
| 114 | if ($language) { | ||
| 115 | for my $l ($language->language) { | ||
| 116 | $c ||= $data_source->setting($label=>"citation:$l"); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | $c ||= $data_source->setting($label=>'citation'); | ||
| 120 | |||
| 121 | my $keywords = $data_source->code_setting($label=>'keywords'); | ||
| 122 | |||
| 123 | $c .= "<br><i>$keywords</i>" if $keywords; | ||
| 124 | return $c; | ||
| 125 | } | ||
| 126 | |||
| 127 | # work around an annoying uninit variable warning from Text::Parsewords | ||
| 128 | sub shellwords { | ||
| 129 | my @args = @_; | ||
| 130 | return unless @args; | ||
| 131 | foreach(@args) { | ||
| 132 | s/^\s+//; | ||
| 133 | s/\s+$//; | ||
| 134 | $_ = '' unless defined $_; | ||
| 135 | } | ||
| 136 | my @result = Text::ParseWords::shellwords(@args); | ||
| 137 | return @result; | ||
| 138 | } | ||
| 139 | |||
| 140 | =item $id = generate_id | ||
| 141 | |||
| 142 | Generate a new md5 hash of a random value for use in various IDs. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub generate_id { | ||
| 147 | my $md5 = new Digest::MD5(); | ||
| 148 | $md5->add($$ , time() , rand(time) ); | ||
| 149 | return $md5->hexdigest(); | ||
| 150 | } | ||
| 151 | |||
| 152 | =item $string = segment_str($segment) | ||
| 153 | |||
| 154 | Returns a nicely formatted string in the format chr:start..stop with commas | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub segment_str { | ||
| 159 | my $segment = shift; | ||
| 160 | my $s = 'Bio::Graphics::Browser2::DataSource'; | ||
| 161 | return $segment->seq_id . ':' . | ||
| 162 | $s->commas($segment->start) . '..' . | ||
| 163 | $s->commas($segment->end); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub set_language { | ||
| 167 | my $self = shift; | ||
| 168 | $CURRENT_LANGUAGE = shift; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub translate { | ||
| 172 | my $self = shift; | ||
| 173 | return 'untranslatable' unless $CURRENT_LANGUAGE; | ||
| 174 | return $CURRENT_LANGUAGE->translate(@_); | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; | ||
| 178 | |||
| 179 | =back | ||
| 180 | |||
| 181 | =head1 SEE ALSO | ||
| 182 | |||
| 183 | L<Bio::Graphics::Browser>, | ||
| 184 | L<Bio::Graphics::Panel>, | ||
| 185 | L<Bio::Graphics::Glyph>, | ||
| 186 | L<Bio::Graphics::Feature>, | ||
| 187 | L<Bio::Graphics::FeatureFile> | ||
| 188 | |||
| 189 | =head1 AUTHOR | ||
| 190 | |||
| 191 | Lincoln Stein E<lt>lstein@cshl.orgE<gt>. | ||
| 192 | |||
| 193 | Copyright (c) 2003 Cold Spring Harbor Laboratory | ||
| 194 | |||
| 195 | This package and its accompanying libraries is free software; you can | ||
| 196 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 197 | version 1, or at your option, any later version) or the Artistic | ||
| 198 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 199 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 200 | |||
| 201 | =cut |