<commit_before>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016] EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::Draw::Role::BigWig;

### Module for getting data in BigWIG format (either user-attached, or
### internally configured via an ini file or database record

use strict;

use Role::Tiny;
use List::Util qw(min max);

use EnsEMBL::Web::IOWrapper::Indexed;

sub render_signal {
  my $self = shift;
  $self->{'my_config'}->set('drawing_style', ['Graph::Histogram']);
  $self->{'my_config'}->set('height', 60);
  $self->_render_aggregate;
}

sub get_data {
  my ($self, $bins, $url) = @_;

  my $data = $self->_fetch_data($bins,$url);

  if ($data) {
    ## Adjust max and min according to track settings
    my $viewLimits = $self->my_config('viewLimits');
    foreach (@$data) {
      my ($min_score, $max_score);

      ## Constrain to configured range, if any
      my $signal_range = $self->my_config('signal_range');
      if (defined $signal_range) {
        $min_score = $signal_range->[0];
        $max_score = $signal_range->[1];
      }

      ## Otherwise constrain to configured view limits 
      unless(defined $min_score) {
        if (defined $viewLimits) {
          $min_score = [ split ':', $viewLimits ]->[0];
        } else {
          $min_score = $_->{'metadata'}{'min_score'};
        }
      }
      unless(defined $max_score) {
        if (defined $viewLimits) {
          $max_score = [ split ':', $viewLimits ]->[1];
        } else {
          $max_score = $_->{'metadata'}{'max_score'};
        }
      }

      ## Finally constrain to gang range if configured 
      my $gang = $self->gang();
      if ($gang and $gang->{'max'}) {
        $max_score = $gang->{'max'};
      }
      if ($gang and $gang->{'min'}) {
        $min_score = $gang->{'min'};
      }

      $_->{'metadata'}{'max_score'} = $max_score;
      $_->{'metadata'}{'min_score'} = $min_score;
    }

  } else {
    warn "!!! $self: ERROR FETCHING DATA FOR FILE ".$self->my_config('caption')." (BIGWIG FORMAT)";
  }
  #$self->{'config'}->add_to_legend($legend);

  return $data;
}

sub _fetch_data {
### Get the data and cache it
  my ($self, $bins, $url) = @_;
  $bins ||= $self->bins;

  #return $self->{'_cache'}{'data'} if $self->{'_cache'}{'data'};
 
  my $hub       = $self->{'config'}->hub;
  $url          ||= $self->my_config('url');

  if (!$url) { ## Internally configured bigwig file?
    my $dba       = $hub->database($self->my_config('type'), $self->species);

    if ($dba) {
      my $dfa = $dba->get_DataFileAdaptor();
      $dfa->global_base_path($hub->species_defs->DATAFILE_BASE_PATH);
      my ($logic_name) = @{$self->my_config('logic_names')||[]};
      my ($df) = @{$dfa->fetch_all_by_logic_name($logic_name)||[]};
      my $paths = $df->get_all_paths;
      $url = $paths->[-1];
    }
  }
  return [] unless $url;

  my $slice     = $self->{'container'};
  my $args      = { 'options' => {
                                  'hub'         => $hub,
                                  'config_type' => $self->{'config'}{'type'},
                                  'track'       => $self->{'my_config'}{'id'},
                                  },
                    'default_strand' => 1,
                    };

  my $iow = EnsEMBL::Web::IOWrapper::Indexed::open($url, 'BigWig', $args);
  my $data = [];

  if ($iow) {
    ## We need to pass 'faux' metadata to the ensembl-io wrapper, because
    ## most files won't have explicit colour settings
    my $colour = $self->my_config('colour') || 'slategray';
    $self->{'my_config'}->set('axis_colour', $colour);
    $bins   ||= $self->bins;
    my $metadata = {
                    'name'            => $self->{'my_config'}->get('name'),
                    'colour'          => $colour,
                    'join_colour'     => $colour,
                    'label_colour'    => $colour,<commit_msg>Check file exists before trying to open it<commit_after>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016] EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::Draw::Role::BigWig;

### Module for getting data in BigWIG format (either user-attached, or
### internally configured via an ini file or database record

use strict;

use Role::Tiny;
use List::Util qw(min max);

use EnsEMBL::Web::File::Utils::IO;
use EnsEMBL::Web::File::Utils::URL;

use EnsEMBL::Web::IOWrapper::Indexed;

sub render_signal {
  my $self = shift;
  $self->{'my_config'}->set('drawing_style', ['Graph::Histogram']);
  $self->{'my_config'}->set('height', 60);
  $self->_render_aggregate;
}

sub get_data {
  my ($self, $bins, $url) = @_;

  my $data = $self->_fetch_data($bins,$url);

  if ($data) {
    ## Adjust max and min according to track settings
    my $viewLimits = $self->my_config('viewLimits');
    foreach (@$data) {
      my ($min_score, $max_score);

      ## Constrain to configured range, if any
      my $signal_range = $self->my_config('signal_range');
      if (defined $signal_range) {
        $min_score = $signal_range->[0];
        $max_score = $signal_range->[1];
      }

      ## Otherwise constrain to configured view limits 
      unless(defined $min_score) {
        if (defined $viewLimits) {
          $min_score = [ split ':', $viewLimits ]->[0];
        } else {
          $min_score = $_->{'metadata'}{'min_score'};
        }
      }
      unless(defined $max_score) {
        if (defined $viewLimits) {
          $max_score = [ split ':', $viewLimits ]->[1];
        } else {
          $max_score = $_->{'metadata'}{'max_score'};
        }
      }

      ## Finally constrain to gang range if configured 
      my $gang = $self->gang();
      if ($gang and $gang->{'max'}) {
        $max_score = $gang->{'max'};
      }
      if ($gang and $gang->{'min'}) {
        $min_score = $gang->{'min'};
      }

      $_->{'metadata'}{'max_score'} = $max_score;
      $_->{'metadata'}{'min_score'} = $min_score;
    }

  } else {
    warn "!!! $self: ERROR FETCHING DATA FOR FILE ".$self->my_config('caption')." (BIGWIG FORMAT)";
  }
  #$self->{'config'}->add_to_legend($legend);

  return $data;
}

sub _fetch_data {
### Get the data and cache it
  my ($self, $bins, $url) = @_;
  $bins ||= $self->bins;

  #return $self->{'_cache'}{'data'} if $self->{'_cache'}{'data'};
 
  my $hub       = $self->{'config'}->hub;
  $url          ||= $self->my_config('url');

  if (!$url) { ## Internally configured bigwig file?
    my $dba       = $hub->database($self->my_config('type'), $self->species);

    if ($dba) {
      my $dfa = $dba->get_DataFileAdaptor();
      $dfa->global_base_path($hub->species_defs->DATAFILE_BASE_PATH);
      my ($logic_name) = @{$self->my_config('logic_names')||[]};
      my ($df) = @{$dfa->fetch_all_by_logic_name($logic_name)||[]};
      my $paths = $df->get_all_paths;
      $url = $paths->[-1];
    }
  }
  return [] unless $url;

  my $check;
  if ($url =~ /^http|ftp/) {
    $check = EnsEMBL::Web::File::Utils::URL::file_exists($url, {'nice' => 1});
  }
  else {
    $check = EnsEMBL::Web::File::Utils::IO::file_exists($url, {'nice' => 1});
  }

  if ($check->{'error'}) {
    $self->no_file($check->{'error'}->[0]);
    return [];
  }

  my $slice     = $self->{'container'};
  my $args      = { 'options' => {
                                  'hub'         => $hub,
                                  'config_type' => $self->{'config'}{'type'},
                                  'track'       => $self->{'my_config'}{'id'},
                                  },
                    'default_strand' => 1,
                    };

  my $iow = EnsEMBL::Web::IOWrapper::Indexed::open($url, 'BigWig', $args);
  my $data = [];

  if ($iow) {
    ## We need to pass 'faux' metadata to the ensembl-io wrapper, because
    ## most files won't have explicit colour settings
    my $colour = $self->my_config('colour') || 'slategray';
    $self->{'my_config'}->set('axis_colour', $colour);
    $bins   ||= $self->bins;
    my $metadata = {
                    'name'            => $self->{'my_config'}->get('name'),
                    'colour'          => $colour,
                    'join_colour'     => $colour,
                    'label_colour'    => $colour,
{'fraction_non_alphanumeric': 0.17313623311709028, 'fraction_numerical': 0.005636498989684143, 'mean_word_length': 2.59892843474933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1669203', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3002, 'n_words': 754}