GBrowse

 view release on metacpan or  search on metacpan

lib/Bio/Graphics/Browser2/DataLoader/useq.pm  view on Meta::CPAN

    return $self->{default_track_name}++;
}

sub find_paths {
	my $self = shift;
	
	# looking for a non-executable jar file that may not be in env path
	# so hard code likely paths, taking into account known Virtual Machines
	my @paths = qw(/usr /usr/local /opt /opt/gbrowse /data /data/opt /Applications);
    push @paths, split ':', $ENV{PATH};
    
	my ($USeq2UCSCBig, $bigPath, $java);
	foreach my $p (@paths) {
		unless ($USeq2UCSCBig) {
			my $path = File::Spec->catdir($p, 'USeq*');
			foreach my $candidate (reverse glob($path)) {
				# we reverse the glob results to ensure we find the latest 
				# version if multiple installed, eg USeq_8.4.4 before USeq_8.0.9
				my $app = File::Spec->catdir($candidate, 'Apps', 'USeq2UCSCBig');
				if (-e $app) {
					$USeq2UCSCBig = $app;
				}
			}
		}
		
		unless ($bigPath) {
			# we need the bin path to both converter utilities
			my $w2bw = File::Spec->catdir($p, 'wigToBigWig');
			my $b2bb = File::Spec->catdir($p, 'bedToBigBed');
			if ( (-e $w2bw && -x _ ) and
				 (-e $b2bb && -x _ ) ) {
				$bigPath = $p;
			}
		}
		
		unless ($java) {
			my $path = File::Spec->catdir($p, 'java');
			$java = $path if (-e $path && -x _ );
		}
		
		last if $USeq2UCSCBig && $bigPath && $java;
	}
	
	die "Please install the USeq package from http://useq.sourceforge.net" 
		unless $USeq2UCSCBig;
	die "Please install wigToBigWig and bedToBigBed in your path" 
		unless $bigPath;
	die "Please install Java 1.6+" unless $java;
	
	$self->{USeq2UCSCBig} = $USeq2UCSCBig;
	$self->{bigPath}      = $bigPath;
	$self->{java}         = $java;
	return 1;
}


sub load {
    my $self                = shift;
    my ($initial_lines,$fh) = @_;
    
    $self->flag_busy(1);
    eval {
	$self->open_conf;
	$self->set_status('starting load');
	
	mkdir $self->sources_path or die $!;
	$self->{useq} = File::Spec->catfile($self->sources_path,$self->track_name);
	my $source_file = IO::File->new($self->{useq},'>');

	warn "sourcefile=$self->{useq}";

	$self->start_load;

	$self->set_status('load data');
	my $bytes_loaded = 0;
	foreach (@$initial_lines) {
	    $source_file->print($_);
	    $bytes_loaded += length $_;
	}

	my $buffer;
	while ((my $bytes = read($fh,$buffer,8192) > 0)) {
	    $source_file->print($buffer);
	    $bytes_loaded += length $ buffer;
	    $self->set_status("loaded $bytes_loaded bytes") if $bytes++ % 10000;
	}
	$source_file->close();
    
	$self->finish_load;
	$self->close_conf;
	$self->set_processing_complete;
    };

    $self->flag_busy(0);
    die $@ if $@;
    return $self->tracks;
}

sub finish_load {
    my $self = shift;
	
    $self->convert_useq;
    
	my @bw;
	my @bb;
	my $path = $self->sources_path . "/*";
	foreach my $f (glob($path)) {
		push @bw, $f if $f =~ /\.bw$/;
		push @bb, $f if $f =~ /\.bb$/;
	}
	
	if (scalar @bb == 1) {
		$self->finish_bigbed_load(@bb);
	}
	elsif (scalar @bw == 1) {
		$self->finish_bigwig_load(@bw);
	}
	elsif (scalar @bw == 2) {
		$self->finish_stranded_bigwig_load(@bw);
	}
}

sub convert_useq {
	my $self = shift;
	
    $self->set_status('Converting with USeq2UCSCBig');
	my $java    = $self->{java};
	my $app     = $self->{USeq2UCSCBig};
	my $bigPath = $self->{bigPath};
	my $useq    = $self->{useq};
	local $SIG{CHLD} = 'DEFAULT';
	my $fh;
	open $fh, "($java -jar '$app' -d '$bigPath' -u '$useq' && echo 'success') 2>&1 |";
	my @lines = <$fh>;
	close $fh;
	unless ($lines[-1] =~ /success/) {
	    die "USEQ CONVERSION ERROR: @lines";
	}
	unlink $useq; # no longer need the original file
	unlink "$useq.chromLengths" if -e "$useq.chromLengths";
	unlink "$useq.wig" if -e "$useq.wig";
}

sub finish_bigbed_load {
    my $self = shift;
    my $bigbed     = shift;
	my @COLORS = qw(blue red orange brown mauve peach 
					green cyan yellow coral);
    
    my $loadid     = $self->loadid;
    $self->set_status('writing configuration for bigBed');
    my $conf       = $self->conf_fh;
    my $dbid       = $self->new_track_label;
    print $conf <<END;



( run in 0.477 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )