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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
|
package Bio::Graphics::Browser2::TrackDumper;
###################################################################
#
# This package holds the majority of code from the gbgff script.
# It is used to output GFF3 to allow for track sharing.
#
###################################################################
# $Id$
# Simple track dumper, suitable for a lightweight replacement to DAS.
# Call this way:
# http://my.host/cgi-bin/gb2/gbrowse/volvox?gbgff=1;q=ctgA:1..2000;l=Genes%5EExampleFeatures
#
# From within the "Add Remote Annotations" section, you can say:
# http://my.host/cgi-bin/gb2/gbrowse/volvox?gbgff=1;q=$segment;l=Genes%5EExampleFeatures
#
# Note that the track name delimiter has changed to %5E, and the option is "l".
# The old method of separating with + signs had to abandoned because of spaces in the
# track label names.
#
# To share uploads, do this:
# http://my.host/cgi-bin/gb2/gbrowse/volvox?gbgff=1;q=ctgA:1..2000;\
# l=Genes%5EExampleFeatures%5Efile:my_upload.txt;id=session_id
use strict;
use CGI 'param', 'path_info', 'header';
use Bio::Graphics::Browser2;
use Bio::Graphics::FeatureFile;
use Bio::Graphics::Browser2::RegionSearch;
use Bio::Graphics::Browser2::Shellwords;
use Bio::Graphics::Browser2::Util 'modperl_request';
use Bio::Graphics::Browser2::TrackDumper::RichSeqMaker;
use Bio::SeqIO;
sub new {
my $class = shift;
my %options = @_;
my $self = bless {
data_source => $options{-data_source},
stylesheet => $options{-stylesheet},
'dump' => $options{'-dump'}, # in quotes because "dump" is a perl keyword
labels => $options{-labels},
mimetype => $options{-mimetype},
format => $options{-format},
requested_segment => $options{-segment},
},
ref $class || $class;
$self->check_source() or return;
$self->get_segment($options{-segment}) or return
if $options{-segment};
return $self;
}
sub forced_format { shift->{format} }
sub add_prefix {
my $self = shift;
return $self->{'.prefix'} if exists $self->{'.prefix'};
my $datasource = $self->data_source;
return $self->{'.prefix'} = $datasource->seqid_prefix;
}
sub print_stylesheet {
my $self = shift;
my ($segment,$labels) = @_;
$segment||= $self->get_segment;
$labels ||= $self->get_labels;
my $types = $labels ? $self->labels_to_types($labels,eval{$segment->length}) : undef;
my $files = $labels ? $self->labels_to_files($labels,eval{$segment->length}) : undef;
$self->print_configuration( $self->data_source(), $labels );
$self->print_configuration( $_, [ $_->labels ] ) for @$files;
}
sub dump_track {
my $self = shift;
my ($label,$segment) = @_;
my $source = $self->data_source;
my $key = $source->setting($label=>'key');
my $db = $source->open_database($label);
my @types = shellwords($source->setting($label=>'feature'));
my $dump_method = $self->guess_dump_method($db,$label);
local $SIG{PIPE} = sub {die 'aborted track dump due to sigPIPE'};
$self->$dump_method($db,$segment,\@types,$label);
}
sub dump_gff3_autowig {
shift->_dump_gff3(@_,1);
}
sub dump_gff3 {
shift->_dump_gff3(@_);
}
sub dump_vista_peaks {
shift->_dump_gff3(@_,1,1); # magic number==1 means print peaks only
}
sub dump_vista_wiggle {
shift->_dump_gff3(@_,1,2); # magic number==2 means print signal data only
}
sub dump_vista {
shift->_dump_gff3(@_,1,3); # magic number==3 means print peaks + signal data
}
sub dump_fasta {
my $self = shift;
my ($db,$segment,$types,$label) = @_;
my $out = new Bio::SeqIO(-format=>'fasta',-fh=>\*STDOUT);
for my $seg ($self->get_segs($db,$segment)) {
my $seq = $seg->seq;
$out->write_seq($seq);
}
}
sub dump_genbank {
my $self = shift;
my ($db,$segment,$types,$label) = @_;
my $out = new Bio::SeqIO(-format=>'genbank',-fh=>\*STDOUT);
for my $seg ($self->get_segs($db,$segment)) {
my $seq = $self->get_rich_seq($db,$seg,$types);
$out->write_seq($seq);
}
}
sub get_segs {
my $self = shift;
my ($db,$segment) = @_;
return $segment if $segment;
my @segs = eval {
map {$db->segment($_)} $db->seq_ids;
};
return @segs if @segs;
my $default = $self->data_source->open_database(); # try default
return eval {
map {$default->segment($_)} $default->seq_ids;
};
}
sub get_rich_seq {
my $self = shift;
my ($db,$segment,$types) = @_;
my @args = $segment ? (-seq_id=> $segment->seq_id,
-start => $segment->start,
-end => $segment->end)
: ();
my $iterator = $db->get_seq_stream(@args,-type=>$types);
return Bio::Graphics::Browser2::TrackDumper::RichSeqMaker->stream_to_rich_seq($segment,$iterator);
}
sub _dump_gff3 {
my $self = shift;
my ($db,$segment,$types,$label,$autowig,$vista) = @_;
my @args = $segment ? (-seq_id=> $segment->seq_id,
-start => $segment->start,
-end => $segment->end)
: ();
my $iterator = $db->get_seq_stream(@args,-type=>$types);
# The logic here is as follows:
# 1) if the features contain the 'wigfile' attribute, then we
# defer printing to the end and produce a facsimile of
# a UCSC wigfile upload (using bedgraph format).
# 2) otherwise we print out a valid gff3 file
my ($gff3_header,$bed_header,%peaks,%wigs,%bigwigs);
while (my $f = $iterator->next_seq) {
if (($autowig || $vista) && (my ($wig) = $f->get_tag_values('wigfile'))) {
my ($start,$end) = ($f->start,$f->end);
$start = $segment->start if $segment && $start < $segment->start;
$end = $segment->end if $segment && $end > $segment->end;
if (my $base = $self->data_source->setting($label=>'basedir')) {
$wig = File::Spec->rel2abs($wig,$base);
}
my $w = $wig =~ /\.bw$/ ? \%bigwigs : \%wigs;
my $trackname = $f->display_name || $f->type;
$trackname .= " (Signal)" if $vista;
$w->{$trackname}{$f->seq_id}{$wig} = [$start,$end];
} else {
$self->print_gff3_header($segment) unless $gff3_header++;
$self->print_feature($f);
}
if ($vista && (my ($peaks) = $f->get_tag_values('peak_type'))) {
$peaks{$f->display_name || $f->type}{$peaks} = [$segment->seq_id,$segment->start,$segment->end];
}
}
$vista ||= 3;
$self->print_peaks($db,\%peaks,$label) if %peaks && $vista==1 or $vista == 3;
$self->print_bio_graphics_wigs(\%wigs,$label) if %wigs && $vista==2 or $vista == 3;
$self->print_bigwigs(\%bigwigs,$label) if %bigwigs && $vista==2 or $vista == 3;
}
sub dump_sam {
my $self = shift;
my ($db,$segment,$types,$label) = @_;
my @args = $segment ? (-seq_id => $segment->seq_id,
-start => $segment->start,
-end => $segment->end)
: ();
my $key = $self->data_source->setting($label=>'key');
$key ||= $label;
my $header = $db->header;
my $prefix = $self->add_prefix;
print $header->text;
print "\@CO\t$key\n";
print "\@CO\tGenerated by GBrowse from ",$self->origin($label),"\n";
if ($segment) {
my $seq_id = $segment->seq_id;
$seq_id = "$prefix$seq_id" if $prefix;
print "\@CO\tSequence-region ",$seq_id,':',$segment->start,'..',$segment->end,"\n" if $segment;
}
# there are problems with the filehandle-based conversion of
# BAM to TAM because the low-level functions write to STDOUT directly
# which are munged in modperl and fastcgi environments.
# in the case of modperl, we revert to doing the conversion at the perl level
# which may be slower
if (modperl_request()) {
if (Bio::DB::Bam::AlignWrapper->can('tam_line')) {
my $iterator = $db->features(@args,-iterator=>1);
while ($_ = $iterator->next_seq) {
my $tam_line = $_->tam_line;
$tam_line =~ s/^([^\t]+)\t([^\t}+)\t([^\t+])/$1\t$2\t$prefix$3/ if $prefix;
print $tam_line,"\n";
}
} else {
print "## Bio::DB::SamTools 1.19 or higher required for printing SAM data when running under mod_perl\n";
}
return;
}
# With FastCGI, we can handle this by piping the result to ourselves.
if (Bio::Graphics::Browser2::Render->fcgi_request()) {
eval "use IO::Pipe" unless IO::Pipe->can('new');
my $pipe = IO::Pipe->new;
my $child = Bio::Graphics::Browser2::Render->fork();
if ($child) {
$pipe->reader();
while (<$pipe>) {
s/^([^\t]+)\t([^\t]+)\t([^\t]+)/$1\t$2\t$prefix$3/ if $prefix;
print $_;
}
} else {
$pipe->writer();
my $fh = $db->features(@args,-fh => 1);
while (<$fh>) {
print $pipe $_ ;
}
exit 0;
}
return;
}
# CGI scripts simply do it directly.
my $fh = $db->features(@args,-fh => 1);
while (<$fh>) {
s/^([^\t]+)\t([^\t]+)\t([^\t]+)/$1\t$2\t$prefix$3/ if $prefix;
print $_;
}
}
sub dump_bigwig {
my $self = shift;
my ($db,$segment,$types,$label) = @_;
my @types = shellwords($self->data_source->setting($label=>'feature'));
my $key = $self->data_source->setting($label=>'key');
$key ||= $label;
my @location_args = $segment ? (-seq_id => $segment->seq_id,
-start => $segment->start,
-end => $segment->end)
: ();
my @type_args = @types ? (-type => \@types)
: ();
my $iterator = $db->get_seq_stream(@location_args,@type_args);
my $origin = $self->origin($label);
my $last_type = '';
my $last_name = '';
while (my $f = $iterator->next_seq) {
my $type = $f->type;
my $name = $f->display_name;
if ($type ne $last_type or $name ne $last_name) {
print "\n" if $last_type or $last_name;
$name ||= $type;
print qq(track type=bedGraph name="$key ($name)" description="Generated by GBrowse from $origin"\n);
$last_type = $type;
}
if ($f->can('get_seq_stream')) { # a summary type; need to do recursive iteration
my $i = $f->get_seq_stream(@location_args);
while (my $g = $i->next_seq) { $self->print_bed_score($g) }
}
else {
$self->print_bed_score($f);
}
}
}
sub print_peaks {
my $self = shift;
my ($db,$peaks,$label) = @_;
for my $trackname (keys %$peaks) {
$self->print_bed_header("$trackname (Peaks)");
for my $type (keys %{$peaks->{$trackname}}) {
my ($seqid,$start,$end) = @{$peaks->{$trackname}{$type}};
my $iterator = $db->get_seq_stream(-seq_id=> $seqid,
-start => $start,
-end => $end,
-type => $type,
);
while (my $f = $iterator->next_seq) {
$self->print_bed_score($f);
}
}
print "\n";
}
}
sub print_bed_header {
my $self = shift;
my $label = shift;
print "track name=\"$label\"\n";
}
sub print_bed_score {
my $self = shift;
my $f = shift;
my $score = defined $f->score ? sprintf("%.4f",$f->score) : '';
$score =~ s/\.?0+$//;
my $seq_id = $f->seq_id;
if (my $prefix = $self->add_prefix) {
$seq_id = "$prefix$seq_id";
}
print join ("\t",$seq_id,$f->start,$f->end,$score),"\n";
}
sub available_formats {
my $class = shift;
my ($source,$label) = @_;
my $db = $source->open_database($label);
my $glyph = $source->code_setting($label => 'glyph');
my @formats = qw(fasta);
push @formats,qw(gff3 genbank) unless $db->isa('Bio::DB::Bam')
or $db->isa('Bio::DB::Sam')
or $db->isa('Bio::DB::BigWig')
or $glyph =~ /wiggle|xyplot|density/;
push @formats,'vista','vista_wiggle','vista_peaks' if $glyph =~ /vista/i;
push @formats,'sam' if $db->isa('Bio::DB::Bam') or $db->isa('Bio::DB::Sam');
push @formats,'bed' if $db->isa('Bio::DB::BigWig') or $db->isa('Bio::DB::BigWigSet');
push @formats, qw(gff3 bed) if $db->isa('Bio::DB::BigBed');
push @formats,'bed' if $glyph =~ /wiggle|xyplot|density/;
my %seenit;
return grep {!$seenit{$_}++} @formats;
}
sub guess_dump_method {
my $self = shift;
my $db = shift;
my $label= shift;
return 'dump_gff3' if $self->forced_format eq 'gff3';
return 'dump_fasta' if $self->forced_format eq 'fasta';
return 'dump_genbank' if $self->forced_format eq 'genbank';
return 'dump_sam' if $self->forced_format eq 'sam';
return 'dump_bigwig' if $self->forced_format eq 'bed';
return 'dump_vista' if $self->forced_format eq 'vista';
return 'dump_vista_wiggle' if $self->forced_format eq 'vista_wiggle';
return 'dump_vista_peaks' if $self->forced_format eq 'vista_peaks';
return 'dump_vista' if $self->data_source->code_setting($label => 'glyph') =~ /vista/i;
return 'dump_gff3_autowig' if $db->isa('Bio::DB::SeqFeature::Store');
return 'dump_gff3_autowig' if $db->isa('Bio::DB::GFF');
return 'dump_gff3_autowig' if $db->isa('Bio::DB::Das::Chado');
return 'dump_gff3_autowig' if $db->isa('Bio::DB::DasI');
return 'dump_gff3_autowig' if $db->isa('Bio::DB::BigBed');
my $type = $self->guess_file_type();
return 'dump_sam' if $type eq 'sam';
return 'dump_bigwig' if $type eq 'bed';
return 'dump_gff3_autowig'
}
sub print_gff3_header {
my $self = shift;
my $segment = shift;
my $date = localtime;
print "##gff-version 3\n";
print "##date $date\n";
print "##source gbrowse gbgff gff3 dumper\n";
if ($segment) {
my $prefix = $self->add_prefix;
my $seq_id = $segment->seq_id;
$seq_id = "$prefix$seq_id" if $prefix;
print "##sequence-region ",$seq_id,':',$segment->start,'..',$segment->end,"\n";
}
}
sub origin {
my $self = shift;
my $label = shift;
my $source = $self->data_source;
my $origin = CGI::url(-full=>1);
$origin .= "/".$source->name;
$origin .= "?l=".CGI::escape($label) if $label;
return $origin;
}
sub print_bio_graphics_wigs {
my $self = shift;
my ($wigs,$label) = @_;
my $source = $self->data_source;
# may need the relative pathname to get the wigs (sometimes)
my $basedir = $source->code_setting($label=>'basedir');
$basedir = $basedir->() if $basedir && ref($basedir) eq 'CODE'; # a bit awkward here
eval "use Bio::Graphics::Wiggle"
unless Bio::Graphics::Wiggle->can('new');
for my $track_name (keys %$wigs) {
my $origin = $self->origin($label);
print qq(track type=bedGraph name="$track_name" description="Generated by GBrowse from $origin; precision limited to 255 levels"\n);
for my $seqid (keys %{$wigs->{$track_name}}) {
for my $wigfile (keys %{$wigs->{$track_name}{$seqid}}) {
my ($start,$end) = @{$wigs->{$track_name}{$seqid}{$wigfile}};
my $path = File::Spec->rel2abs($wigfile,$basedir);
my $wig = Bio::Graphics::Wiggle->new($path) or next;
$wig->export_to_bedgraph($start,$end,\*STDOUT);
}
}
print "\n";
}
}
sub print_bigwigs {
my $self = shift;
my ($wigs,$label) = @_;
my $source = $self->data_source;
# may need the relative pathname to get the wigs (sometimes)
my $basedir = $source->code_setting($label=>'basedir');
$basedir = $basedir->() if $basedir && ref($basedir) eq 'CODE'; # a bit awkward here
eval "use Bio::DB::BigWig"
unless Bio::DB::BigWig->can('new');
for my $track_name (keys %$wigs) {
for my $seqid (keys %{$wigs->{$track_name}}) {
for my $wigfile (keys %{$wigs->{$track_name}{$seqid}}) {
my ($start,$end) = @{$wigs->{$track_name}{$seqid}{$wigfile}};
my $path = File::Spec->rel2abs($wigfile,$basedir);
my $wig = Bio::DB::BigWig->new($path) or next;
my $segment = Bio::Graphics::Feature->new(-seq_id=>$seqid,-start=>$start,-end=>$end);
$self->dump_bigwig($wig,$segment,[],$track_name);
print "\n";
}
}
}
}
sub print_datafile {
my $self = shift;
my $labels = $self->get_labels;
my $segment= $self->get_segment;
$self->print_stylesheet($segment,$labels)
if $self->get_do_stylesheet;
if (@$labels == 1) {
$self->dump_track($labels->[0],$segment);
return;
}
# We don't handle heterogeneous downloads (multiple tracks with different database backends)
# well, so we default to a uniform GFF3 dump. In the future, this should be replaced with
# TAR file generation.
my $types = $labels ? $self->labels_to_types($labels,eval{$segment->length}) : undef;
my $files = $labels ? $self->labels_to_files($labels,eval{$segment->length}) : undef;
my %filters
= map { $_ => $self->data_source->setting( $_ => 'filter' ) || undef }
@$labels;
$self->print_gff3_header($segment);
$self->print_gff3_data($_, $types, \%filters ) for $self->db;
$self->print_gff3_data($_) for @$files;
}
sub print_fasta {
my $self = shift;
my $segment = $self->get_segment;
my $seq = $segment->seq;
eval "require Bio::SeqIO" unless Bio::SeqIO->can('new');
my $out = Bio::SeqIO->new(-format => 'Fasta');
$out->write_seq($seq);
}
sub data_source { shift->{data_source} }
sub db {
my $self = shift;
return @{$self->{db}} if $self->{db};
my $source = $self->data_source;
my $tracks = $self->get_labels;
$tracks = $self->all_databases unless $tracks;
my %seenit;
my @dbs = grep {defined($_) && !$seenit{$_}++}
map {$source->open_database($_)} ('general',@$tracks);
$self->{db} = \@dbs;
return @dbs;
}
sub segment {
my $self = shift;
my $d = $self->{segment};
$self->{segment} = shift if @_;
$d;
}
sub check_source {
my $self = shift;
my $data_source = $self->{data_source};
my $source_name = $data_source->name();
$source_name =~ s!^/+!!; # get rid of leading / from path_info()
$source_name =~ s!/+$!!; # get rid of trailing / from path_info() !
if ($source_name) {
unless ( $data_source->globals->valid_source($source_name) ) {
print header('text/plain'), "# Invalid source $source_name; "
. "you may not have permission to access this data source.\n";
return;
}
}
return 1;
}
sub get_segment {
my $self = shift;
return $self->{segment} unless @_;
my $segment = shift;
# check whether someone called us directly by pasting into location box
if ($segment =~ /^\$segment/) {
print header('text/plain');
print "# To share this track, please paste its URL into the \"Enter Remote Annotation\" box\n",
"# at the bottom of a GBrowse window and not directly into your browser's Location area.\n";
return;
}
my ( $seqid, $start, $end )
= $segment =~ /^([^:]+)(?::([\d-]+)(?:\.\.|,)([\d-]+))?/;
unless ( defined $seqid ) {
print header('text/plain');
print "# Please provide the segment argument.\n";
return;
}
my $datasource = $self->data_source;
my $tracks = $self->get_labels;
$tracks = $self->all_databases unless $tracks;
my $search = Bio::Graphics::Browser2::RegionSearch->new(
{
source => $datasource,
state => $self->state || {},
}
);
$search->init_databases();
my ($f) = $search->search_features({-search_term=>$segment});
unless ($f) {
print header('text/plain'), "# The landmark named $segment was not found.\n";
return;
}
$self->segment($f);
return $f;
}
sub state {
my $self = shift;
my $d = $self->{state};
$self->{state} = shift if @_;
$d;
}
sub all_databases {
my $self = shift;
my $source = $self->data_source;
return [map {"$_:database"} $source->databases];
}
sub get_labels {
my $self = shift;
my @labels = @{ $self->{labels} || [] };
return \@labels;
}
sub get_id {
my $self = shift;
return $self->{id};
}
sub get_featurefiles {
my $self = shift;
my @ff = grep {/^file:/} @{ $self->{labels} || [] };
return unless @ff;
return shellwords(@ff);
}
sub get_do_stylesheet {
my $self = shift;
my $doit = $self->{stylesheet};
return 0 if defined $doit && $doit =~ /^(no|off|0)$/i;
return 1;
}
sub guess_file_type {
my $self = shift;
my $labels = $self->get_labels;
my $forced = $self->forced_format;
return $forced if defined $forced;
# Currently we can't deal with heterogeneous downloads,
# so if multiple tracks are selected, we default to the
# common gff3 format. In the future, we should create a
# TAR file for download.
if (@$labels > 1) {
return 'gff3';
}
my $label = $labels->[0];
my $source = $self->data_source;
my $glyph = $source->setting( $label => 'glyph');
my $db = $source->open_database($label);
if ($glyph && $glyph =~ /vista/i) {
return 'bed';
}
if ($glyph && $glyph =~ /wiggle/) {
return 'bed';
}
if ($db->isa('Bio::DB::Bam') or $db->isa('Bio::DB::Sam')) {
return 'sam';
}
if ($db->isa('Bio::DB::BigWig') or $db->isa('Bio::DB::BigWigSet')) {
return 'bed';
}
return 'gff3';
}
sub get_file_mime_type {
my $self = shift;
return $self->{mimetype} if defined $self->{mimetype};
my $dump = $self->{'dump'} || '';
my $type = $self->guess_file_type;
my $x = "application/x-$type";
return $type if $dump eq 'edit';
return 'application/octet-stream' if $dump eq 'save';
return 'text/plain';
}
sub get_file_extension {
my $self = shift;
my $type = $self->guess_file_type;
return $type;
}
sub labels_to_types {
my $self = shift;
my $labels = shift;
my $length = shift;
my $data_source = $self->data_source;
# remove dynamic labels, such as uploads
my @labels = grep { /:(overview|region|detail)$/ # keep overview/region sections
||
!/^\w+:/x # discard over dynamic sections
} @$labels;
my @types;
for my $l (@labels) {
my @f = shellwords( $data_source->semantic_setting( $l => 'feature', $length ) || '' );
next unless @f;
push @types, @f;
}
return \@types;
}
sub labels_to_files {
my $self = shift;
my $labels = shift;
@$labels or return [];
# get the feature files, if appropriate
my $id = $self->get_id or return [];
# first get the main source
my $data_source = $self->data_source;
my $segment = $self->segment;
my @files;
my $search = Bio::Graphics::Browser2::RegionSearch->new(
{
source => $data_source,
state => $self->state || {},
}
);
$search->init_databases();
my $mapper = $search->coordinate_mapper( $segment, 1 );
for my $filename (@$labels) {
$filename =~ s/:(detail|overview|region).*$//;
my $path = Bio::Graphics::Browser2::UserData->file2path($data_source,$id,$filename);
my $featurefile = eval {
Bio::Graphics::FeatureFile->new(
-file => $path,
-smart_features => 1,
-map_coords => $mapper,
);
};
warn "Error while loading remote feature file $filename: $@" if $@;
push @files, $featurefile if $featurefile;
}
return \@files;
}
sub print_feature {
my $self = shift;
my $f = shift;
eval { $f->version(3) };
my $s = $f->gff_string(1); # the flag is for GFF3 subfeature recursion
chomp $s;
if (my $prefix = $self->add_prefix) {
$s =~ s/^/$prefix/gm unless $s =~ /^$prefix/m;
}
$s =~ s/\t\t/\t\.\t/g if $s; # no empty columns
$self->do_wigfile_substitution( \$s );
print $s, "\n";
}
sub print_configuration {
my $self = shift;
my $config = shift;
my $labels = shift;
my @labels = $labels ? @$labels : $config->labels;
for my $l (@labels) {
# a special config setting - don't want it to leak through
next if $l =~ m/^\w+:/ && $l !~ m/:(overview|region|details?)$/;
next if $l =~ m/^_scale/;
print "[$l]\n";
my @s = $config->_setting($l);
for my $s (@s) {
my $value = $config->setting( $l => $s );
if ( ref $value eq 'CODE' ) {
$value
= $config->config->can('get_callback_source')
? $config->config->get_callback_source( $l => $s )
: $config->setting( 'TRACK DEFAULTS' => $s );
defined $value or next;
chomp ($value);
}
next if $s =~ /^balloon/; # doesn't work right
print "$s = $value\n";
}
print "\n";
}
}
sub print_gff3_data {
my $self = shift;
my $db = shift;
my $types = shift;
my $filters = shift;
my $s = $self->get_segment;
my $data_source = $self->data_source;
my $len = $s->length;
my @args = (
-seq_id => $s->seq_id,
-start => $s->start,
-end => $s->end
);
push @args, ( -type => $types ) if $types;
my $iterator = $db->get_seq_stream(@args);
FEATURE:
while ( my $f = $iterator->next_seq ) {
for my $l ( $data_source->feature2label( $f, $len ) ) {
next FEATURE if $filters->{$l} && !$filters->{$l}->($f);
}
$self->print_feature($f);
}
}
# Because we can't sling whole wigfiles around, we serialize a section
# of the wigfile and send just the data and header information in "wif" format.
sub do_wigfile_substitution {
my $self = shift;
my $gff_line_ref = shift;
$$gff_line_ref =~ /wigfile=/ or return;
my $segment = $self->segment;
my ( $start, $end ) = ( $segment->start, $segment->end );
eval { # trap all errors, which can be plentiful!
eval "use Bio::Graphics::Wiggle"
unless Bio::Graphics::Wiggle->can('new');
eval "use MIME::Base64" unless MIME::Base64->can('encode');
$$gff_line_ref =~ s{wigfile=([^;&\n]+)}
{
my $wig = Bio::Graphics::Wiggle->new(CGI::unescape($1));
my $wif = $wig->export_to_wif($start,$end);
'wigdata='.MIME::Base64::encode_base64($wif,'');
}exg;
};
warn $@ if $@;
}
# Experimental feature:
# list all the labels that are marked "discoverable"
sub print_scan {
my $self = shift;
print $self->get_scan;
}
sub get_scan {
my $self = shift;
my $config = $self->data_source;
my @labels = $config->labels;
my $result = '';
$result .= "# Discoverable tracks from ".CGI->self_url."\n";
for my $l (@labels) {
next if $l =~ /^_/;
next if $l =~ /:\w+/;
next unless $config->fallback_setting($l => 'discoverable');
next if $config->code_setting($l=>'global feature');
my $key = $config->code_setting($l => 'key') || $l;
my $citation = $config->code_setting($l => 'citation');
my $data_source = $config->code_setting($l => 'data source');
my $track_source = $config->code_setting($l => 'track source');
my $subtracks = $config->subtrack_scan_list($l);
$result .= <<END;
[$l]
key = $key
END
;
$result .= "select = @$subtracks\n" if $subtracks;
$result .= "citation = $citation\n" if $citation;
$result .= "data source = $data_source\n" if $data_source;
$result .= "track source = $track_source\n" if $track_source;
$result .= "\n";
}
return $result;
}
1;
|