File: Util.pm

package info (click to toggle)
gbrowse 2.56%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,112 kB
  • ctags: 4,436
  • sloc: perl: 50,765; sh: 249; sql: 62; makefile: 45; ansic: 27
file content (201 lines) | stat: -rw-r--r-- 4,376 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package Bio::Graphics::Browser2::Util;

# a package of useful internal routines for GBrowse

=head1 NAME

Bio::Graphics::Browser2::Util -- Exported utilities

=head1 SYNOPSIS

  use Bio::Graphics::Browser2::Util;

  my $r = modperl_request();

=head1 DESCRIPTION

This package provides functions that support the Generic Genome
Browser.  It is not currently designed for external use.

=head2 FUNCTIONS


=cut

use strict;
use base 'Exporter';
use Text::ParseWords qw();
use Carp 'carp','cluck';
use Digest::MD5;

our @EXPORT    = qw(modperl_request error citation shellwords url_label);
our @EXPORT_OK = qw(modperl_request error citation shellwords url_label segment_str);

my $CURRENT_LANGUAGE;

use constant DEBUG => 0;

=over 4

=item my $r = modperl_request()

Return an Apache2::Request or an Apache::Request object, depending on
whichever version of Apache is running.

=cut

sub modperl_request {
  return unless $ENV{MOD_PERL};
  (exists $ENV{MOD_PERL_API_VERSION} &&
   $ENV{MOD_PERL_API_VERSION} >= 2 ) ? Apache2::RequestUtil->request
                                     : Apache->request;
}

=item error('message')

Prints an error message

=cut

sub error {
  my ($msg,$details) = @_;
  warn "@_";# if DEBUG;
  $msg     =~ s/'/\\'/g;
  $details =~ s/'/\\'/g;
  $msg     =~ s/\n/<br>/g;
  $details =~ s/\n/<br>/g;
  print CGI::script({-type=>'text/javascript'},
	       qq[Controller.show_error('$msg','$details')]);
#  print CGI::h2({-class=>'error'},@msg);
}


=item url_label($yucky_url)

Creates a label.alias for URL strings starting with 'http' or 'ftp'.
The last word (following a '/') in the url is used for the label.
Returns a string "url:label".

=cut

sub url_label {
  my $label = shift;
  my $key;
  if ($label =~ m!^(?:http|ftp)://([^/]+)!) {
    my $l    = $label;
    my $host = $1;
    $l =~ s!^\W+//!!;
    my (undef,$type) = $l =~ /\S+t(ype)?=([^;\&]+)/;
    $l =~ s/\?.+//;
    ($key) = grep /$_/, reverse split('/',$l);
    $key = "$host/$key" if $key;
    $key .= ":$type"  if $type;
  }
  return $key || $label;
}


=item citation(DataSource, 'label, [Language])

Returns a track citation

=cut

sub citation {
  my $data_source = shift;
  my $label       = shift;
  my $language    = shift;
  if ($label =~ /^plugin\:/) { 
    my $label_fix = $'; 
    $label_fix =~ s/\:detail$//;
    $label = join(":",($label_fix,'plugin'));
  }
  my $c;
  if ($language) {
    for my $l ($language->language) {
      $c ||= $data_source->setting($label=>"citation:$l");
    }
  }
  $c ||= $data_source->setting($label=>'citation');

  my $keywords                 = $data_source->code_setting($label=>'keywords');

  $c          .= "<br><i>$keywords</i>"         if $keywords;
  return $c;
}

# work around an annoying uninit variable warning from Text::Parsewords
sub shellwords {
    my @args = @_;
    return unless @args;
    foreach(@args) {
	s/^\s+//;
	s/\s+$//;
	$_ = '' unless defined $_;
    }
    my @result = Text::ParseWords::shellwords(@args);
    return @result;
}

=item $id = generate_id

Generate a new md5 hash of a random value for use in various IDs.

=cut

sub generate_id {
    my $md5 = new Digest::MD5();
    $md5->add($$ , time() , rand(time) );
    return $md5->hexdigest();
}

=item $string = segment_str($segment)

Returns a nicely formatted string in the format chr:start..stop with commas

=cut

sub segment_str {
    my $segment = shift;
    my $s  = 'Bio::Graphics::Browser2::DataSource';
    return $segment->seq_id . ':' .
	   $s->commas($segment->start) . '..' .
	   $s->commas($segment->end);
}

sub set_language {
    my $self = shift;
    $CURRENT_LANGUAGE = shift;
}

sub translate {
    my $self = shift;
    return 'untranslatable' unless $CURRENT_LANGUAGE;
    return $CURRENT_LANGUAGE->translate(@_);
}

1;

=back

=head1 SEE ALSO

L<Bio::Graphics::Browser>,
L<Bio::Graphics::Panel>,
L<Bio::Graphics::Glyph>,
L<Bio::Graphics::Feature>,
L<Bio::Graphics::FeatureFile>

=head1 AUTHOR

Lincoln Stein E<lt>lstein@cshl.orgE<gt>.

Copyright (c) 2003 Cold Spring Harbor Laboratory

This package and its accompanying libraries is free software; you can
redistribute it and/or modify it under the terms of the GPL (either
version 1, or at your option, any later version) or the Artistic
License 2.0.  Refer to LICENSE for the full license text. In addition,
please see DISCLAIMER.txt for disclaimers of warranty.

=cut