blib/lib/Bio/Graphics/Browser2/Region.pm untested

filter: Statementstmt 0.0% 0 / 310 Branchbran 0.0% 0 / 146 Conditioncond 0.0% 0 / 138 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 644 CC 74 SCAR 53.0
CC
74
Cov
0%
lookup_features
650.0
_feature_get
182.0
parse_searchopts
72.0
CRAP 201.1
1 package Bio::Graphics::Browser2::Region;
2  
3 # provide method for fetching and manipulating the current
4 # region or regions.
5  
6 use strict;
7 use warnings;
8 use Bio::Graphics::Browser2::Shellwords;
9 use constant DEBUG=>0;
10  
11 sub new {
12 my $self = shift;
13 my $args = shift;
14 my ($source,$db,$state,$searchopts)
15 = @{$args}{'source','db','state','searchopts'};
16  
17 $searchopts ||= 'default';
18  
19 return bless {
20 source => $source,
21 db => $db,
22 state => $state,
23 searchopts => $self->parse_searchopts($searchopts),
24 },ref($self) || $self;
25 }
26  
27 sub source { shift->{source} }
28 sub state { shift->{state} }
29 sub db { shift->{db} }
30 sub searchopts { shift->{searchopts} }
31  
32 sub parse_searchopts {
33 my $self = shift;
34 my $optstring = shift;
35  
36 my @default = qw(exact wildcard stem fulltext heuristic);
37 my %all = map {$_=>1} qw(exact wildcard stem fulltext heuristic autocomplete);
38  
39 my %opts;
40 my @tokens = split /[\s,]+/,lc $optstring;
41 @tokens = ('default') unless @tokens;
42  
43 for my $t (@tokens) {
44 my ($sign,$token) = $t =~ /([+-]?)(\w+)/;
45  
46 if ($token eq 'all') {
47 $opts{$_}++ foreach keys %all;
48 next;
49 }
50
51 if ($token eq 'none') {
52 %opts = ();
53 next;
54 }
55  
56 if ($token eq 'default') {
57 $opts{$_}++ foreach @default;
58 next;
59 }
60
61 next unless $all{$token};
62 $opts{$token}++ if $sign eq '+' or !$sign;
63  
64 delete $opts{$token} if $sign eq '-';
65 }
66  
67 return \%opts;
68 }
69  
70 sub feature_count {
71 my $self = shift;
72 my $features = $self->features;
73 return 0 unless $features;
74 return scalar @$features;
75 }
76  
77 # get/set list of features that we are working on
78 sub features {
79 my $self = shift;
80 my $d = $self->{features};
81 $self->{features} = shift if @_;
82 return $d;
83 }
84  
85 # lazy retrieval of current segment(s) -- may transform features if needed
86 sub segments {
87 my $self = shift;
88 $self->{segments} ||=
89 $self->features2segments($self->features) if $self->features;
90 return $self->{segments} || [];
91 }
92  
93 # lazy retrieval of first segment
94 sub seg {
95 my $self = shift;
96 unless (exists $self->{segment}) {
97 my $segments = $self->segments;
98 $self->{segment} = $segments->[0] if @$segments;
99 if (my $seg = $self->{segment}) {
100 my $state = $self->state;
101 $state->{ref} = $seg->seq_id;
102 $state->{start} = $seg->start;
103 $state->{stop} = $seg->end;
104 }
105 }
106 return $self->{segment};
107 }
108  
109 # lazy retrieval of first whole segment
110 sub whole_seg {
111 my $self = shift;
112 unless (exists $self->{whole_segment}) {
113 my $whole_seg = $self->get_whole_segment($self->seg);
114 $self->{whole_segment} = $whole_seg;
115 my $state = $self->state;
116 $state->{seg_min} = $whole_seg->start;
117 $state->{seg_max} = $whole_seg->end;
118 }
119 return $self->{whole_segment};
120 }
121  
122 sub set_features_by_region {
123 my $self = shift;
124 my ($ref,$start,$stop) = @_;
125 my $divider = $self->source->unit_divider;
126 my $features = $self->lookup_features($ref,$start/$divider,$stop/$divider);
127 $self->features($features);
128 return $features;
129 }
130  
131 # For backward compatibility, you can call search_features() either
132 # with a scalar string, in which case it will be treated as a search
133 # string to be parsed and search (for example "chrI:1000.2000") or
134 # call with a hashref containing the arguments to be passed to
135 # the db adaptor's features() method.
136 sub search_features {
137 my $self = shift;
138 my $args = shift;
139  
140 my $db = $self->db;
141 my $state = $self->state;
142  
143 $args ||= { };
144 unless (%$args) {
145 return unless $state->{name};
146 $args->{-search_term} = $state->{name};
147 }
148  
149 warn "SEARCHING FOR ",join ' ',%$args," in $db" if DEBUG;
150  
151 my $features = $self->search_db($args);
152  
153 warn "FOUND @$features " if $features && DEBUG;
154 $self->features($features);
155 return $features;
156 }
157  
158  
159 sub features2segments {
160 my $self = shift;
161 my $features = shift;
162 my $refclass = $self->source->global_setting('reference class') || '';
163 my $db = $self->db;
164 my %seenit;
165 my @segments =
166 map {
167 my $version = $_->can('version') ? $_->version : undef;
168 $db->segment(-class => $refclass,
169 -seq_id => $_->seq_id,
170 -name => $_->seq_id, # to avoid breakage due to sloppy API
171 -start => $_->start||0,
172 -end => $_->end ||0,
173 -stop => $_->end ||0, # to avoid breakage due to sloppy API
174 -absolute => 1,
175 defined $version ? (-version => $version) : ())
176 } grep {
177 !$seenit{$_->seq_id,$_->start,$_->end}++}
178 @$features;
179 return \@segments;
180 }
181  
182 sub get_whole_segment {
183 my $self = shift;
184 my $segment = shift;
185  
186 my $factory = $self->source->open_database();
187  
188 # the segment class has been deprecated, but we still must support it
189 my $class = eval {$segment->seq_id->class} || eval{$factory->refclass};
190  
191 my ($whole_segment) = $factory->segment(-class=>$class,
192 -name=>$segment->seq_id);
193 $whole_segment ||= $segment; # just paranoia
194 $whole_segment;
195 }
196  
197  
198 sub search_db {
199 my $self = shift;
200 my $args = shift;
201 my ($features);
202 if (my $name = $args->{-search_term}) {
203 $name =~ tr/a-zA-Z0-9|.'"_*?: ;+-\/\#\[\]//cd; # remove rude/naughty characters
204 my ($ref,$start,$stop,$class,$id) = $self->parse_feature_name($name);
205 $features = $self->lookup_features($ref,$start,$stop,$class,$name,$id);
206 }
207 elsif ($args->{-name} && $args->{-name}=~/^id:(.+)/) {
208 $features = $self->lookup_features(undef,undef,undef,undef,undef,$1);
209 }
210 else {
211 my @features = $self->db->features(%$args);
212 $features = \@features;
213 }
214 return wantarray ? @$features : $features;
215 }
216  
217 sub lookup_features {
218 my $self = shift;
219 my ($name,$start,$stop,$class,$literal_name,$id) = @_;
220 my $source = $self->source;
221  
222 my $refclass = $source->global_setting('reference class') || 'Sequence';
223  
224 my $db = $self->db;
225  
226 my $divisor = $source->unit_divider;
227 $start *= $divisor if defined $start;
228 $stop *= $divisor if defined $stop;
229  
230 # automatic classes to try
231 my @classes = $class ? ($class)
232 : (split /\s+/,$source->global_setting('automatic classes')||'');
233  
234 if (defined $id && $db->can('get_feature_by_id')) { # this overrides everything else
235 my $f = $db->get_feature_by_id($id);
236 return $f ? [$f] : [];
237 }
238  
239 my $features = [];
240  
241 my $searchopts = $self->searchopts;
242  
243 SEARCHING:
244 {
245  
246 warn "searchopts = ",join ',',%$searchopts if DEBUG;
247 unless (%$searchopts) {
248 warn "segment(-name => $name,-start=>$start,-end=>$stop)" if DEBUG;
249 my @f = $db->segment(-name => $name,-start=>$start,-end=>$stop);
250 $features = \@f;
251 last SEARCHING;
252 }
253  
254 for my $n ([$name,$start,$stop],
255 [$literal_name,undef,undef]) {
256  
257 my ($name_to_try,$start_to_try,$stop_to_try) = @$n;
258  
259 $name_to_try =~ s/([*?])/\\$1/g
260 unless $searchopts->{wildcard};
261  
262 if ($searchopts->{exact}) {
263  
264 # first try the non-heuristic search
265 for my $class ($class,$refclass,@classes) {
266 $features = $self->_feature_get($db,
267 $name_to_try,$class,
268 $start_to_try,$stop_to_try);
269 last SEARCHING if @$features;
270 }
271 }
272  
273 # heuristic fetch. Try various abbreviations and wildcards
274 my @sloppy_names = ();
275  
276 if ($searchopts->{heuristic}) {
277 my $seqid_prefix = $source->seqid_prefix;
278 if ($name_to_try =~ /^([\dIVXA-F]+)$/) {
279 my %seenit;
280 my $id = $1;
281 foreach (qw(CHROMOSOME_ Chr chr),$seqid_prefix) {
282 next unless $_;
283 next if $seenit{$_}++;
284 my $n = "${_}${id}";
285 push @sloppy_names,$n;
286 }
287 }
288  
289 # try to remove the chr CHROMOSOME_I
290 if ((my $chr = $name_to_try) =~ s/^(chromosome_?|chr)//i) {
291 push @sloppy_names,$chr;
292 }
293  
294 if ($seqid_prefix && (my $chr = $name_to_try) =~ s/^$seqid_prefix//) {
295 push @sloppy_names,$chr;
296 }
297 }
298  
299 if ($searchopts->{stem}) {
300 push @sloppy_names,"$name_to_try*"
301 if length $name_to_try >= 3 and $name_to_try !~ /\*$/;
302 }
303  
304 for my $n (@sloppy_names) {
305 for my $c ('',@classes) {
306 $features = $self->_feature_get($db,$n,$c,$start_to_try,$stop_to_try);
307 last SEARCHING if @$features;
308 }
309 }
310
311 }
312 }
313  
314 if (!@$features && $searchopts->{fulltext}) {
315 warn "try a keyword search for $literal_name" if DEBUG;
316 $features = $self->_feature_keyword_search($literal_name);
317 }
318  
319 if ($class) {
320 my $regex = quotemeta($class);
321 my @f;
322 foreach (@$features) {
323 my $c = eval {$_->class};
324 push @f,$_ if ($c && $c =~ /^$regex/i) or $_->primary_tag =~ /^$regex/i;
325 }
326 $features = \@f;
327 }
328
329 return $features;
330 }
331  
332 sub _feature_get {
333 my $self = shift;
334 my ($db,$name,$class,$start,$stop) = @_;
335  
336 my $refclass = $self->source->global_setting('reference class') || 'Sequence';
337  
338 my @argv = (-name => $name);
339 push @argv,(-class => $class) if defined $class;
340 push @argv,(-start => $start) if defined $start;
341 push @argv,(-end => $stop) if defined $stop;
342  
343 my @features;
344 @features = grep {$_->length} $db->get_feature_by_name(@argv) #misnomer -- should be get_features_by_name!
345 if !defined($start) && !defined($stop);
346  
347 warn "get_feature_by_name(@argv) => @features" if DEBUG;
348  
349 @features = grep {$_->length} $db->get_features_by_alias(@argv)
350 if !@features
351 && !defined($start)
352 && !defined($stop)
353 && $db->can('get_features_by_alias');
354  
355 warn "get_features_by_alias(@argv) => @features" if DEBUG;
356  
357 @features = grep {$_->length} $db->segment(@argv)
358 if !@features && $name !~ /[*?]/;
359 return [] unless @features;
360  
361 warn "segment => @features" if DEBUG;
362  
363 # Deal with multiple hits. Winnow down to just those that
364 # were mentioned in the config file.
365 $class ||= ''; # to get rid of uninit variable warnings
366 my $types = $self->source->_all_types($db);
367 my @filtered = grep {
368 my $type = $_->primary_tag;
369 my $method = eval {$_->method} || '';
370 my $fclass = eval {$_->class} || '';
371 $type eq 'Segment' # ugly stuff accomodates loss of "class" concept in GFF3
372 || $type eq 'region'
373 || $types->{$type}
374 || $types->{$method}
375 || !$fclass
376 || $fclass eq $refclass
377 || $fclass eq $class;
378 } @features;
379  
380 # remove duplicate features -- same name, source, method and position
381 warn "before duplicate removal features = @features" if DEBUG;
382  
383 my %seenit;
384 @features = grep {
385 my $key = eval{$_->gff_string} || join (':',$_->type,$_->seq_id,$_->start,$_->end);
386 !$seenit{$key}++;
387 } @features;
388  
389 warn "after duplicate removal features = @features" if DEBUG;
390  
391 return \@features;
392 }
393  
394 sub _feature_keyword_search {
395 my $self = shift;
396 my $searchterm = shift;
397 my $source = $self->source;
398  
399 # if they wanted something specific, don't give them non-specific results.
400 return if $searchterm =~ /^[\w._-]+:/;
401  
402 # Need to untaint the searchterm. We are very lenient about
403 # what is accepted here because we wil be quote-metaing it later.
404 $searchterm =~ /([\w .,~!@\#$%^&*()-+=<>?:;\/]+)/;
405 $searchterm = $1;
406  
407 my $db = $self->db;
408 my $max_keywords = $source->global_setting('max keyword results');
409 my @matches;
410 if ($db->can('search_attributes')) {
411 my @attribute_names = shellwords ($source->global_setting('search attributes')||'');
412 @attribute_names = ('Note') unless @attribute_names;
413 @matches = $db->search_attributes($searchterm,\@attribute_names,$max_keywords);
414 } elsif ($db->can('search_notes')) {
415 @matches = $db->search_notes($searchterm,$max_keywords);
416 }
417  
418 my @results;
419 for my $r (@matches) {
420 my ($name,$description,$score,$type,$id) = @$r;
421 my ($seg) = $db->segment($name) or next;
422 push @results,Bio::Graphics::Feature->new(-name => $name,
423 -class => eval{$name->class} || undef,
424 -desc => $description,
425 -score => $score,
426 -ref => $seg->abs_ref,
427 -start => $seg->abs_start,
428 -end => $seg->abs_end,
429 -type => $type || 'feature',
430 -primary_id => $id || undef,
431 -factory=> $db);
432  
433 }
434 return \@results;
435 }
436  
437 sub parse_feature_name {
438 my $self = shift;
439 my $name = shift;
440  
441 if ($name =~ /^id:(.+)/) {
442 return (undef,undef,undef,undef,$1);
443 }
444  
445 my ($class,$ref,$start,$stop);
446 if (my @a = $self->is_chromosome_region($name)) {
447 ($ref,$start,$stop) = @a;
448 }
449  
450 elsif ($name =~ /^(\w+):([^:]+)$/) {
451 $class = $1;
452 $ref = $2;
453 }
454  
455 else {
456 $ref = $name;
457 }
458 return ($ref,$start,$stop,$class);
459 }
460  
461 sub is_chromosome_region {
462 my $self = shift;
463 my $name = shift;
464 if ( ($name !~ /\.\./ and $name =~ /([\w._\/-]+):\s*(-?[-e\d.]+)\s*,\s*(-?[-e\d.]+)\s*$/) or
465 $name =~ /([\w._\/-]+):\s*(-?[-e\d,.]+?)\s*(?:-|\.\.)\s*(-?[-e\d,.]+)\s*$/) {
466 my $ref = $1;
467 my $start = $2;
468 my $stop = $3;
469 $start =~ s/,//g; # get rid of commas
470 $stop =~ s/,//g;
471 return ($ref,$start,$stop);
472 }
473 return;
474 }
475  
476 =head2 $whole = $db->whole_segment ($segment,$settings);
477  
478 Given a detail segment, return the whole seq_id that contains it
479  
480 =cut
481  
482 sub whole_segment {
483 my $self = shift;
484 my $segment = shift || $self->seg;
485 return unless $segment;
486 my $db = $segment->factory;
487 my $class = eval {$segment->seq_id->class} || eval{$db->refclass} || 'Sequence';
488 my ($whole) = $db->segment(-class=>$class,
489 -name=>$segment->seq_id);
490 return $whole;
491 }
492  
493 =head2 $region = $db->region_segment ($segment,$settings [,$whole]);
494  
495 Given a detail segment and the current settings, return the region
496 centered on the segment. The whole segment can be passed if desired --
497 this will avoid a redundant lookup.
498  
499 =cut
500  
501 sub region_segment {
502 my $self = shift;
503 my $segment = shift || $self->seg;
504 my $settings = shift;
505 my $whole = shift;
506  
507 $whole ||= $self->whole_segment($segment) or return;
508  
509 my $regionview_length = $settings->{region_size}||0;
510 my $detail_start = $segment->start;
511 my $detail_end = $segment->end;
512 my $whole_start = $whole->start;
513 my $whole_end = $whole->end;
514  
515 # region can't be smaller than detail
516 if ($detail_end - $detail_start + 1 > $regionview_length) {
517 $regionview_length = 3*($detail_end - $detail_start + 1);
518 }
519  
520 my $midpoint = ($detail_end + $detail_start) / 2;
521 my $regionview_start = int($midpoint - $regionview_length/2 + 1);
522 my $regionview_end = int($midpoint + $regionview_length/2);
523  
524 if ($regionview_length > $whole->length) {
525 $regionview_length = $whole->length;
526 }
527  
528 if ($regionview_start < $whole_start) {
529 $regionview_start = 1;
530 $regionview_end = $regionview_length;
531 }
532  
533 if ($regionview_end > $whole_end) {
534 $regionview_start = $whole_end - $regionview_length + 1;
535 $regionview_end = $whole_end;
536 }
537  
538 my $db = eval {$segment->factory};
539 my $class = eval {$segment->seq_id->class} || eval{$db->refclass};
540  
541 my ($region_segment) = $db ? $db->segment(-class => $class,
542 -name => $segment->seq_id,
543 -seq_id=>$segment->seq_id,
544 -start => $regionview_start,
545 -end => $regionview_end)
546 :Bio::Graphics::Feature->new(-name => $segment->seq_id,
547 -seq_id => $segment->seq_id,
548 -start => $regionview_start,
549 -end => $regionview_end);
550 return $region_segment;
551 }
552  
553  
554  
555  
556 1;