<commit_before>package EnsEMBL::Web::Component::Gene::GeneExport;

use EnsEMBL::Web::Component;
use EnsEMBL::Web::Form;
use EnsEMBL::Web::Document::SpreadSheet;
use EnsEMBL::Web::SeqDumper;

use strict;
use warnings;
no warnings "uninitialized";

use base qw( EnsEMBL::Web::Component::Gene );

sub _init {
  my $self = shift;
  $self->cacheable(1);
  $self->ajaxable(0);
}

# TODO: UNHACK!
# This package controls exporting for transcripts and locations as well as genes. These functions should be moved elsewhere.

sub content_transcript {
  my $self = shift;
  
  return $self->content('Transcript');
}

sub content_location {
  my $self = shift;
  
  return $self->content('Location');
}

sub content {
  my $self = shift;
  my $type = shift || 'Gene';
  my $object = $self->object;
  
  my ($translation, $three, $five);
  my $html;
  
  if ($type eq 'Location') {
    my $links = [
      [ 'fasta', 'FASTA' ],
      [ 'csv', 'CSV (Comma separated values)' ],
      [ 'gff', 'GFF Format' ],
      [ 'tab', 'Tab separated values' ],
      [ 'embl', 'EMBL' ],
      [ 'genbank', 'GenBank' ]
    ];
    $html .= '<ul>'; 
    $html .= qq{<li><a href="/@{[$object->species]}/$type/Export/$_->[0]?$ENV{'QUERY_STRING'};_format=Text" target="_blank">$_->[1]</a></li>} for (@$links);
    $html .= '</ul>'; 
  } else {
    if ($type eq 'Transcript') {
      $translation = 1 if $object->Obj->translation;
      $three = 1 if $object->Obj->three_prime_utr;
      $five = 1 if $object->Obj->five_prime_utr;
    } else {
      # Gene
      for (@{$object->get_all_transcripts}) {
        $translation = 1 if $_->Obj->translation;
        $three = 1 if $_->Obj->three_prime_utr;
        $five = 1 if $_->Obj->five_prime_utr;
        
        last if $translation && $three && $five;
      }
    }
    
    my $links = [
      [ 'cdna', 'cDNA', 1 ],
      [ 'coding', 'Coding sequence', $translation ],
      [ 'peptide', 'Peptide sequence', $translation ],
      [ 'utr5', "5' UTR", $five ],
      [ 'utr3', "3' UTR", $three ]
    ];
    
    for (@$links) {
      $html .= '<ul>'; 
      $html .= qq{<p><a href="/@{[$object->species]}/$type/Export/fasta?$ENV{'QUERY_STRING'};st=$_->[0];_format=Text" target="_blank">$_->[1]</a></p>} if $_->[2];
      $html .= '</ul>'; 
    }
  }
  
  return $html;
}

sub content_transcript_fasta {
  my $self = shift;

  return '<pre>' . $self->fasta_trans($self->object, $self->object->param('st')) . '</pre>';
}

sub content_location_fasta {
  my $self   = shift;
  my $object = $self->object;
  my $slice  = $object->slice->seq;
  
  $slice =~ s/(.{60})/$1\n/g;
  
  return "<pre>>@{[$object->seq_region_name]} dna:@{[$object->seq_region_type]} @{[$object->slice->name]}\n$slice\n</pre>";
}

sub content_gene_fasta {
  my $self   = shift;
  my $object = $self->object;
  
  my $html;
  $html .= $self->fasta_trans($_, $object->param('st'), $object->stable_id) for (@{$object->get_all_transcripts||[]});
  
  return "<pre>$html</pre>";
}

sub fasta_trans {
  my $self = shift;
  my ($trans_obj, $type, $obj_id) = @_;
  
  my $transcript = $trans_obj->Obj;
  my $id_type = $transcript->isa('Bio::EnsEMBL::PredictionTranscript') ? $transcript->analysis->logic_name : $transcript->status . '_' . $transcript->biotype;
  my $id = $obj_id ? "$obj_id:" : "" . $transcript->stable_id;
  
  my $output = {
    'cdna'    => [ "$id cdna:$id_type", $transcript->spliced_seq ],
    'coding'  => eval { [ "$id cds:$id_type", $transcript->translateable_seq ] },
    'peptide' => eval { [ "$id peptide:@{[$transcript->translation->stable_id]} pep:$id_type", $transcript->translate->seq ] },
    'utr3'    => eval { [ "$id utr3:$id_type", $transcript->three_prime_utr->seq ] },
    'utr5'    => eval { [ "$id utr5:$id_type", $transcript->five_prime_utr->seq ] }
  };
  
  $output->{$type}->[1] =~ s/(.{60})/$1\n/g;
  
  return ">$output->{$type}->[0]\n$output->{$type}->[1]\n";
}

sub content_location_csv {
  my $self = shift;
  
  return $self->content_location_feature('csv');
}

sub content_location_gff {
  my $self = shift;
  
  return $self->content_location_feature('gff');
}

sub content_location_tab {
  my $self = shift;
  
  return $self->content_location_feature('tab');
}
<commit_msg>correct formatting, fix error when output type is wrong (why is it returning incorrect data types? grrrr), get transcript id into gene export<commit_after>package EnsEMBL::Web::Component::Gene::GeneExport;

use EnsEMBL::Web::Component;
use EnsEMBL::Web::Form;
use EnsEMBL::Web::Document::SpreadSheet;
use EnsEMBL::Web::SeqDumper;

use strict;
use warnings;
no warnings "uninitialized";

use base qw( EnsEMBL::Web::Component::Gene );

sub _init {
  my $self = shift;
  $self->cacheable(1);
  $self->ajaxable(0);
}

# TODO: UNHACK!
# This package controls exporting for transcripts and locations as well as genes. These functions should be moved elsewhere.

sub content_transcript {
  my $self = shift;
  
  return $self->content('Transcript');
}

sub content_location {
  my $self = shift;
  
  return $self->content('Location');
}

sub content {
  my $self = shift;
  my $type = shift || 'Gene';
  my $object = $self->object;
  
  my ($translation, $three, $five);
  my $html;
  
  if ($type eq 'Location') {
    my $links = [
      [ 'fasta', 'FASTA' ],
      [ 'csv', 'CSV (Comma separated values)' ],
      [ 'gff', 'GFF Format' ],
      [ 'tab', 'Tab separated values' ],
      [ 'embl', 'EMBL' ],
      [ 'genbank', 'GenBank' ]
    ];
    $html .= '<ul>'; 
    $html .= qq{<li><a href="/@{[$object->species]}/$type/Export/$_->[0]?$ENV{'QUERY_STRING'};_format=Text" target="_blank">$_->[1]</a></li>} for (@$links);
    $html .= '</ul>'; 
  } else {
    if ($type eq 'Transcript') {
      $translation = 1 if $object->Obj->translation;
      $three = 1 if $object->Obj->three_prime_utr;
      $five = 1 if $object->Obj->five_prime_utr;
    } else {
      # Gene
      for (@{$object->get_all_transcripts}) {
        $translation = 1 if $_->Obj->translation;
        $three = 1 if $_->Obj->three_prime_utr;
        $five = 1 if $_->Obj->five_prime_utr;
        
        last if $translation && $three && $five;
      }
    }
    
    my $links = [
      [ 'cdna', 'cDNA', 1 ],
      [ 'coding', 'Coding sequence', $translation ],
      [ 'peptide', 'Peptide sequence', $translation ],
      [ 'utr5', "5' UTR", $five ],
      [ 'utr3', "3' UTR", $three ]
    ];
    
    for (@$links) {
      $html .= '<ul>'; 
      $html .= qq{<li><a href="/@{[$object->species]}/$type/Export/fasta?$ENV{'QUERY_STRING'};st=$_->[0];_format=Text" target="_blank">$_->[1]</a></li>} if $_->[2];
      $html .= '</ul>'; 
    }
  }
  
  return $html;
}

sub content_transcript_fasta {
  my $self = shift;

  return '<pre>' . $self->fasta_trans($self->object, $self->object->param('st')) . '</pre>';
}

sub content_location_fasta {
  my $self   = shift;
  my $object = $self->object;
  my $slice  = $object->slice->seq;
  
  $slice =~ s/(.{60})/$1\n/g;
  
  return "<pre>>@{[$object->seq_region_name]} dna:@{[$object->seq_region_type]} @{[$object->slice->name]}\n$slice\n</pre>";
}

sub content_gene_fasta {
  my $self   = shift;
  my $object = $self->object;
  
  my $html;
  $html .= $self->fasta_trans($_, $object->param('st'), $object->stable_id) for (@{$object->get_all_transcripts||[]});
  
  return "<pre>$html</pre>";
}

sub fasta_trans {
  my $self = shift;
  my ($trans_obj, $type, $obj_id) = @_;
  
  my $transcript = $trans_obj->Obj;
  my $id_type = $transcript->isa('Bio::EnsEMBL::PredictionTranscript') ? $transcript->analysis->logic_name : $transcript->status . '_' . $transcript->biotype;
  my $id = ($obj_id ? "$obj_id:" : "") . $transcript->stable_id;
  
  my $output = {
    'cdna'    => [ "$id cdna:$id_type", $transcript->spliced_seq ],
    'coding'  => eval { [ "$id cds:$id_type", $transcript->translateable_seq ] },
    'peptide' => eval { [ "$id peptide:@{[$transcript->translation->stable_id]} pep:$id_type", $transcript->translate->seq ] },
    'utr3'    => eval { [ "$id utr3:$id_type", $transcript->three_prime_utr->seq ] },
    'utr5'    => eval { [ "$id utr5:$id_type", $transcript->five_prime_utr->seq ] }
  };
  
  return unless ref $output->{$type} eq 'ARRAY';
  
  $output->{$type}->[1] =~ s/(.{60})/$1\n/g;
  
  return ">$output->{$type}->[0]\n$output->{$type}->[1]\n";
}

sub content_location_csv {
  my $self = shift;
  
  return $self->content_location_feature('csv');
}

sub content_location_gff {
  my $self = shift;
  
  return $self->content_location_feature('gff');
}

sub content_location_tab {
  my $self = shift;
  
  return $self->content_location_feature('tab');
}

{'fraction_non_alphanumeric': 0.21245932124593211, 'fraction_numerical': 0.007205950720595072, 'mean_word_length': 3.361378611251901, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 188, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1684363', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3163, 'n_words': 642}