<commit_before>#!/usr/bin/perl

=head1 NAME

  /bulk/display.pl

=head1 DESCRIPTION

  This perl script is used on the bulk download page. It determines the format
  of the data given back to the user (submitted to download.pl). This includes
  the format of the the html pages that display the data as well as determining
  the fasta format and the text format.

=cut

use strict;
use warnings;
use CatalystX::GlobalContext '$c';

bulk_display->new($c)->display_page;

package bulk_display;
use CGI ();
use CXGN::Page;
use CXGN::Page::FormattingHelpers qw/ html_break_string/;

=head2 new

  Desc: sub new
  Args: default
  Ret : $args, $class;

  When implemented creates a new display object that the methods in the display
  package can be called on.

=cut

sub new {
    my $class   = shift;
    my $c       = shift;
    my $args    = {};

    # define some constants
    $args->{pagesize} = 50;
    $args->{content}  = "";
    $args->{tempdir} = $c->path_to( $c->tempfiles_subdir('bulk') );

    # get cgi arguments
    $args->{cgi}         = my $cgi = CGI->new;
    $args->{dumpfile}    = $cgi->param("dumpfile");
    $args->{page_number} = $cgi->param("page_number");
    $args->{outputType}  = $cgi->param("outputType");
    $args->{seq_type}    = $cgi->param("seq_type");
    $args->{idType}      = $cgi->param("idType");
    $args->{summary}     = $cgi->param("summary");
    $args->{download}    = $cgi->param("download");
    $args->{page} = CXGN::Page->new( "Browse Bulk Results", "<NAME>" );

    return bless $args, $class;
}

=head2 display_page

  Desc: sub display_page
  Args: default
  Ret : page

  Calls summary page to be displayed. Determines what page to render depending
  on the outout type the user selects.

=cut

sub display_page() {
    my $self = shift;

    # if summary switch is set, display summary page
    if ( $self->{summary} ) { $self->display_summary_page(); }

    elsif ( $self->{outputType} =~ /html/i ) {
        $self->render_html_table_page();
    }
    elsif ( $self->{outputType} =~ /text/i ) {
        $self->render_text_page();
    }
    elsif ( $self->{outputType} =~ /fasta/i ) {
        $self->render_fasta_page();
    }
    elsif ( $self->{outputType} =~ /notfound/i ) {
        $self->display_ids_notfound();
    }
    else { $self->render_html_table_page(); }
}

=head2 display_summary_page

  Desc: sub display_summary_page
  Args: default
  Ret : n/a

  Opens temp file created in download.pl then opens a filehandle and prints the
  data to that file.

=cut

sub display_summary_page {
    my $self = shift;
    my $file = $self->{tempdir} . "/" . $self->{dumpfile} . ".summary";
    open( F, "<$file" ) || $self->{page}->error_page("can't open $file");
    $self->{page}->header();
    while (<F>) {
        print $_;
    }
    $self->{page}->footer();
}

=head2 render_html_page

  Desc: sub render_html_page
  Args: default
  Ret : n/a

  Creates html page in style specified by object that calls the method.

=cut

sub render_html_page {
    my $self  = shift;
    my $style = shift;
    if ( $style =~ /HTML/i || !defined($style) ) {
        $self->render_html_table_page();
    }
    if ( $style =~ /tree/i ) { $self->render_html_tree_page(); }
}

=head2 render_html_table_page

  Desc: sub render_html_table_page
  Args: default
  Ret : n/a

  Creates the html table that will contain the data on the page. Prints page
  number and links above and below the table. Also determines the format of text
  that will be place in the table (by download.pl). For example, sequences and
  quality values are displayed in a smaller font and appear 60 per line (value
  can be adjusted).

=cut

sub render_html_table_page {
    my $self = shift;

    #
    # open the file
    #
    if ( !exists( $self->{page_number} ) || ( $self->{page_number} == 0 ) ) {
        $self->{page_number} = 1;
    }
    if ( !exists( $self->{page_size} ) ) { $self->{page_size} = 50; }

    $self->{debug} = 0;
    my $line_count =
      $self->getFileLines( $self->{tempdir} . "/" . $self->{dumpfile} );
    $self->{line_count} = $line_count;
    $self->debug("Line Count in the file: $line_count");
    if ( $line_count < 2 ) {
        $self->{content} .=
"No data was retrieved. Please verify your input parameters. Thanks.<br /><br />\n";
    }
    else {
        open( F, "<" . $self->{tempdir} . "/" . $self->{dumpfile} )
          || $self->{page}->error_page("Can't open $self->{dumpfile}");

        #
        # read the column definitions
        #
        my @output_fields;
        my $defs = <F>;
        if ($defs) { chomp($defs); @output_fields = split /\t/, $defs; }
        $self->debug( "column definitions: " . ( join " ", @output_fields ) );

        # define the links
        my %links = (
            clone_name =>
"/search/est.pl?request_type=10&amp;search=Search&amp;request_id=",
            SGN_U        => "/search/unigene.pl?unigene_id=",
            converted_id => "/search/unigene.pl?unigene_id=",
        );
        $self->{links} = \%links;

        #
        # read in the required page
        #
        my $line = 0;
        my @data = ();
        $self->{page}->header("Bulk download results");
        $self->buttons();
        $self->{content} .= "<table summary=\"\" border=\"1\">\n";

        # print table header
        $self->{content} .=
            "<tr><td>line</td><td>"
          . ( join "</td><td>", @output_fields )
          . "</td></tr>";

        while (<F>) {
            chomp;
            $line++;
            if ( $line >=
                ( ( ( $self->{page_number} - 1 ) * $self->{page_size} ) + 1 )
                && (
                    $line <= ( ( $self->{page_number} ) * $self->{page_size} ) )
              )
            {<commit_msg>bulk display should print header earlier<commit_after>#!/usr/bin/perl

=head1 NAME

  /bulk/display.pl

=head1 DESCRIPTION

  This perl script is used on the bulk download page. It determines the format
  of the data given back to the user (submitted to download.pl). This includes
  the format of the the html pages that display the data as well as determining
  the fasta format and the text format.

=cut

use strict;
use warnings;
use CatalystX::GlobalContext '$c';

bulk_display->new($c)->display_page;

package bulk_display;
use CGI ();
use CXGN::Page;
use CXGN::Page::FormattingHelpers qw/ html_break_string/;

=head2 new

  Desc: sub new
  Args: default
  Ret : $args, $class;

  When implemented creates a new display object that the methods in the display
  package can be called on.

=cut

sub new {
    my $class   = shift;
    my $c       = shift;
    my $args    = {};

    # define some constants
    $args->{pagesize} = 50;
    $args->{content}  = "";
    $args->{tempdir} = $c->path_to( $c->tempfiles_subdir('bulk') );

    # get cgi arguments
    $args->{cgi}         = my $cgi = CGI->new;
    $args->{dumpfile}    = $cgi->param("dumpfile");
    $args->{page_number} = $cgi->param("page_number");
    $args->{outputType}  = $cgi->param("outputType");
    $args->{seq_type}    = $cgi->param("seq_type");
    $args->{idType}      = $cgi->param("idType");
    $args->{summary}     = $cgi->param("summary");
    $args->{download}    = $cgi->param("download");
    $args->{page} = CXGN::Page->new( "Browse Bulk Results", "<NAME>" );

    return bless $args, $class;
}

=head2 display_page

  Desc: sub display_page
  Args: default
  Ret : page

  Calls summary page to be displayed. Determines what page to render depending
  on the outout type the user selects.

=cut

sub display_page() {
    my $self = shift;

    # if summary switch is set, display summary page
    if ( $self->{summary} ) { $self->display_summary_page(); }

    elsif ( $self->{outputType} =~ /html/i ) {
        $self->render_html_table_page();
    }
    elsif ( $self->{outputType} =~ /text/i ) {
        $self->render_text_page();
    }
    elsif ( $self->{outputType} =~ /fasta/i ) {
        $self->render_fasta_page();
    }
    elsif ( $self->{outputType} =~ /notfound/i ) {
        $self->display_ids_notfound();
    }
    else { $self->render_html_table_page(); }
}

=head2 display_summary_page

  Desc: sub display_summary_page
  Args: default
  Ret : n/a

  Opens temp file created in download.pl then opens a filehandle and prints the
  data to that file.

=cut

sub display_summary_page {
    my $self = shift;
    my $file = $self->{tempdir} . "/" . $self->{dumpfile} . ".summary";
    open( F, "<$file" ) || $self->{page}->error_page("can't open $file");
    $self->{page}->header();
    while (<F>) {
        print $_;
    }
    $self->{page}->footer();
}

=head2 render_html_page

  Desc: sub render_html_page
  Args: default
  Ret : n/a

  Creates html page in style specified by object that calls the method.

=cut

sub render_html_page {
    my $self  = shift;
    my $style = shift;
    if ( $style =~ /HTML/i || !defined($style) ) {
        $self->render_html_table_page();
    }
    if ( $style =~ /tree/i ) { $self->render_html_tree_page(); }
}

=head2 render_html_table_page

  Desc: sub render_html_table_page
  Args: default
  Ret : n/a

  Creates the html table that will contain the data on the page. Prints page
  number and links above and below the table. Also determines the format of text
  that will be place in the table (by download.pl). For example, sequences and
  quality values are displayed in a smaller font and appear 60 per line (value
  can be adjusted).

=cut

sub render_html_table_page {
    my $self = shift;

    $self->{page}->header("Bulk download results");

    #
    # open the file
    #
    if ( !exists( $self->{page_number} ) || ( $self->{page_number} == 0 ) ) {
        $self->{page_number} = 1;
    }
    if ( !exists( $self->{page_size} ) ) { $self->{page_size} = 50; }

    $self->{debug} = 0;
    my $line_count =
      $self->getFileLines( $self->{tempdir} . "/" . $self->{dumpfile} );
    $self->{line_count} = $line_count;
    $self->debug("Line Count in the file: $line_count");
    if ( $line_count < 2 ) {
        $self->{content} .=
"No data was retrieved. Please verify your input parameters. Thanks.<br /><br />\n";
    }
    else {
        open( F, "<" . $self->{tempdir} . "/" . $self->{dumpfile} )
          || $self->{page}->error_page("Can't open $self->{dumpfile}");

        #
        # read the column definitions
        #
        my @output_fields;
        my $defs = <F>;
        if ($defs) { chomp($defs); @output_fields = split /\t/, $defs; }
        $self->debug( "column definitions: " . ( join " ", @output_fields ) );

        # define the links
        my %links = (
            clone_name =>
"/search/est.pl?request_type=10&amp;search=Search&amp;request_id=",
            SGN_U        => "/search/unigene.pl?unigene_id=",
            converted_id => "/search/unigene.pl?unigene_id=",
        );
        $self->{links} = \%links;

        #
        # read in the required page
        #
        my $line = 0;
        my @data = ();
        $self->buttons();
        $self->{content} .= "<table summary=\"\" border=\"1\">\n";

        # print table header
        $self->{content} .=
            "<tr><td>line</td><td>"
          . ( join "</td><td>", @output_fields )
          . "</td></tr>";

        while (<F>) {
            chomp;
            $line++;
            if ( $line >=
                ( ( ( $self->{page_number} - 1 ) * $self->{page_size} ) + 1 )
                && (
                    $line <= ( ( $self->{page_number} ) * $self->{page_size} ) )
              )
            {
{'fraction_non_alphanumeric': 0.16119506687510857, 'fraction_numerical': 0.003995136355740838, 'mean_word_length': 2.4321907600596124, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 189, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1271029', 'n_tokens_mistral': 3986, 'n_tokens_neox': 3858, 'n_words': 1013}