<commit_before>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2017] 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::Web::File::AttachedFormat::TRACKHUB;

use strict;
use warnings;
no warnings 'uninitialized';

use EnsEMBL::Web::File::Utils::TrackHub;
use EnsEMBL::Web::File::Utils::URL qw(chase_redirects);

use base qw(EnsEMBL::Web::File::AttachedFormat);

sub new {
  my $self = shift->SUPER::new(@_);
  
  $self->{'trackhub'} = EnsEMBL::Web::File::Utils::TrackHub->new('hub' => $self->{'hub'},
                                                                 'url' => $self->{'url'});
  return $self;
}

sub check_data {
### Checks if this hub is a) available and b) usable
### @param assembly_lookup HashRef (optional) - passed to TrackHub
### @return Array
###               error ArrayRef
###               hub_info HashRef
  my ($self, $assembly_lookup) = @_;
  my $error;
 
  ## Validate using hubCheck, if available - but don't bother if it's from the registry
  ## as (in theory) it will have already been checked
  ## However only warn the errors, as most hubs have minor errors and hubCheck
  ## is not yet flexible enough to deal with them
  my $assembly_check = $self->{'registry'} ? 0 : 1;
  my $hubCheck = $self->{'hub'}->species_defs->HUBCHECK_BIN;
  if ($hubCheck && !$self->{'registry'}) {
    my $url = $self->{'url'};
    my $hc_error = `$hubCheck '$url' -checkSettings -noTracks`;	
    if ($hc_error) {
      ## Parse and ignore issues we don't care about
      my @lines = split /\n/, $hc_error;
      my @error_lines;
      my $problematic = 0;
      for my $line (@lines) {
        next if $line =~ m/deprecated|cram|^Found\s[0-9]*\sproblems?:$/;
        push @error_lines, $line;
      }
      if (scalar @error_lines) {
        my $error_list = '<ul><li>'.join('</li><li>', @error_lines).'</li></ul>'; 
        my $warning = qq(<p>The trackhub at $url failed to validate with <a href="https://genome.ucsc.edu/goldenpath/help/hgTrackHubHelp.html#Debug">hubCheck</a>, producing the following errors:</p>$error_list<p>It may therefore not display correctly in Ensembl.</p>);
        $self->{'hub'}->session->set_record_data({
                        type     => 'message',
                        code     => 'AttachURL',
                        message  => $warning,
                        function => '_warning'
                      });
      }
    }
  }
 
  ## Check that we can use it with this website's species
  my $hub_params = {'parse_tracks' => 0};
  $hub_params->{'assembly_lookup'} = $assembly_lookup if $assembly_check;
  my $hub_info = $self->{'trackhub'}->get_hub($hub_params);
  
  if ($hub_info->{'error'}) {
    $error  = sprintf('<p>Unable to attach remote TrackHub: %s</p>', $self->url);
    $error .= "<p>$_.</p>" for ref $hub_info->{'error'} eq 'ARRAY' 
                ? @{$hub_info->{'error'}} : $hub_info->{'error'};
  }

  my @assemblies = keys %{$hub_info->{'genomes'}||{}};
  return ($self->url, $error, { 
                                name        => $hub_info->{'details'}{'shortLabel'}, 
                                description => $hub_info->{'details'}{'longLabel'}, 
                                assemblies  => \@assemblies
                              }
          );  
}

sub style {
  my $self = shift;
  return $self->{'_cache'}{'style'} ||= $self->_calc_style;
}

sub _calc_style {
  my $self      = shift;
  my $tl_score  = 0;
  my $trackline = $self->{'trackline'};
  <commit_msg>Moved a line to where it makes more sense<commit_after>=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2017] 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::Web::File::AttachedFormat::TRACKHUB;

use strict;
use warnings;
no warnings 'uninitialized';

use EnsEMBL::Web::File::Utils::TrackHub;
use EnsEMBL::Web::File::Utils::URL qw(chase_redirects);

use base qw(EnsEMBL::Web::File::AttachedFormat);

sub new {
  my $self = shift->SUPER::new(@_);
  
  $self->{'trackhub'} = EnsEMBL::Web::File::Utils::TrackHub->new('hub' => $self->{'hub'},
                                                                 'url' => $self->{'url'});
  return $self;
}

sub check_data {
### Checks if this hub is a) available and b) usable
### @param assembly_lookup HashRef (optional) - passed to TrackHub
### @return Array
###               error ArrayRef
###               hub_info HashRef
  my ($self, $assembly_lookup) = @_;
  my $error;
 
  ## Validate using hubCheck, if available - but don't bother if it's from the registry
  ## as (in theory) it will have already been checked
  ## However only warn the errors, as most hubs have minor errors and hubCheck
  ## is not yet flexible enough to deal with them
  my $hubCheck = $self->{'hub'}->species_defs->HUBCHECK_BIN;
  if ($hubCheck && !$self->{'registry'}) {
    my $url = $self->{'url'};
    my $hc_error = `$hubCheck '$url' -checkSettings -noTracks`;	
    if ($hc_error) {
      ## Parse and ignore issues we don't care about
      my @lines = split /\n/, $hc_error;
      my @error_lines;
      my $problematic = 0;
      for my $line (@lines) {
        next if $line =~ m/deprecated|cram|^Found\s[0-9]*\sproblems?:$/;
        push @error_lines, $line;
      }
      if (scalar @error_lines) {
        my $error_list = '<ul><li>'.join('</li><li>', @error_lines).'</li></ul>'; 
        my $warning = qq(<p>The trackhub at $url failed to validate with <a href="https://genome.ucsc.edu/goldenpath/help/hgTrackHubHelp.html#Debug">hubCheck</a>, producing the following errors:</p>$error_list<p>It may therefore not display correctly in Ensembl.</p>);
        $self->{'hub'}->session->set_record_data({
                        type     => 'message',
                        code     => 'AttachURL',
                        message  => $warning,
                        function => '_warning'
                      });
      }
    }
  }
 
  ## Check that we can use it with this website's species
  my $hub_params = {'parse_tracks' => 0};
  ## Don't check assembly if the hub came from the registry
  my $assembly_check = $self->{'registry'} ? 0 : 1;
  $hub_params->{'assembly_lookup'} = $assembly_lookup if $assembly_check;
  my $hub_info = $self->{'trackhub'}->get_hub($hub_params);
  
  if ($hub_info->{'error'}) {
    $error  = sprintf('<p>Unable to attach remote TrackHub: %s</p>', $self->url);
    $error .= "<p>$_.</p>" for ref $hub_info->{'error'} eq 'ARRAY' 
                ? @{$hub_info->{'error'}} : $hub_info->{'error'};
  }

  my @assemblies = keys %{$hub_info->{'genomes'}||{}};
  return ($self->url, $error, { 
                                name        => $hub_info->{'details'}{'shortLabel'}, 
                                description => $hub_info->{'details'}{'longLabel'}, 
                                assemblies  => \@assemblies
                              }
          );  
}

sub style {
  my $self = shift;
  return $self->{'_cache'}{'style'} ||= $self->_calc_style;
}

sub _calc_style {
  my $self      = shift;
  my $tl_score  = 0;
  my $trackline = $self->{'trackline'};
  
{'fraction_non_alphanumeric': 0.14893359185651964, 'fraction_numerical': 0.0067862336403296175, 'mean_word_length': 2.700896860986547, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 113, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2489760', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2487, 'n_words': 742}