<commit_before>package CoGe::Services::JBrowse::Configuration;
use base 'CGI::Application';

use CoGeX;
use CoGe::Accessory::Web;
use JSON;
use Data::Dumper;
use Sort::Versions;
use Cwd 'abs_path';
use Time::HiRes qw ( time );

my $coge_conf;
my %expTypeToName = (
    1 => 'quant',
    2 => 'snp'
);

sub setup {
    my $self = shift;

    #FIXME - move this into service.pl
    $coge_conf = abs_path($0);
    $coge_conf =~ s/services\/JBrowse\/service\.pl/coge\.conf/;

    $self->run_modes(
        'refseq_config' => 'refseq_config',
        'track_config'  => 'track_config',
    );
    $self->start_mode('track_config');
    $self->mode_param('rm');
}

sub refseq_config {
    my $self           = shift;
    my $gid            = $self->query->param('gid');
    my $SEQ_CHUNK_SIZE = 20000;
    print STDERR "Configuration::refseq_config gid=$gid\n";

    # Load config file
    my $P      = CoGe::Accessory::Web::get_defaults($coge_conf);
    my $DBNAME = $P->{DBNAME};
    my $DBHOST = $P->{DBHOST};
    my $DBPORT = $P->{DBPORT};
    my $DBUSER = $P->{DBUSER};
    my $DBPASS = $P->{DBPASS};

    # Connect to the database
    my $connstr = "dbi:mysql:dbname=$DBNAME;host=$DBHOST;port=$DBPORT";
    my $coge = CoGeX->connect( $connstr, $DBUSER, $DBPASS );

    #$coge->storage->debugobj(new DBIxProfiler());
    #$coge->storage->debug(1);

    # Get genome
    my $genome = $coge->resultset('Genome')->find($gid);
    return unless $genome;

    my @chromosomes;
    foreach my $chr ( sort { $b->sequence_length <=> $a->sequence_length }
        $genome->genomic_sequences )
    {
        push @chromosomes,
          {
            name         => $chr->chromosome,
            length       => $chr->sequence_length,
            seqChunkSize => $SEQ_CHUNK_SIZE,
            start        => 0,
            end          => $chr->sequence_length - 1
          };
    }

    return encode_json( \@chromosomes );
}

sub track_config {
    my $self       = shift;
    my $gid        = $self->query->param('gid');
    my $cas_ticket = $self->query->param('ticket');
    print STDERR "Configuration::track_config gid=$gid\n";

    # Get config
    my $P      = CoGe::Accessory::Web::get_defaults($coge_conf);
    my $DBNAME = $P->{DBNAME};
    my $DBHOST = $P->{DBHOST};
    my $DBPORT = $P->{DBPORT};
    my $DBUSER = $P->{DBUSER};
    my $DBPASS = $P->{DBPASS};

    # Connect to the database
    my $connstr = "dbi:mysql:dbname=$DBNAME;host=$DBHOST;port=$DBPORT";
    my $coge = CoGeX->connect( $connstr, $DBUSER, $DBPASS );

    #$coge->storage->debugobj(new DBIxProfiler());
    #$coge->storage->debug(1);

    # Get user
    my $COOKIE_NAME = $P->{COOKIE_NAME};
    my $USER        = undef;
    ($USER) = CoGe::Accessory::Web->login_cas(
        ticket   => $cas_ticket,
        coge     => $coge,
        this_url => $FORM->url()
    ) if ($cas_ticket);
    ($USER) = CoGe::Accessory::LogUser->get_user(
        cookie_name => $COOKIE_NAME,
        coge        => $coge
    ) unless $USER;

    #	my $start_time = time;

    # Get genome
    my $genome = $coge->resultset('Genome')->find($gid);
    return unless $genome;

    my @tracks;

    # Add reference sequence track
    push @tracks, {
        chunkSize     => 20000,
        baseUrl       => "services/JBrowse/service.pl/sequence/$gid/",
        type          => "SequenceTrack",
        storeClass    => "JBrowse/Store/SeqFeature/REST",
        label         => "sequence",
        key           => "Sequence",
        formatVersion => 1,

        # CoGe-specific stuff
        coge => {
            id   => $gid,
            type => 'sequence'
        }
    };

    # Add GC content track
    push @tracks, {

        #baseUrl => "http://geco.iplantcollaborative.org/rchasman/gc/$gid/",
        baseUrl    => "services/JBrowse/track/gc/$gid/",
        type       => "CoGe/View/Track/GC_Content",
        storeClass => "JBrowse/Store/SeqFeature/REST",
        track      => "gc_content",
        label      => "gc_content",
        key        => "GC Content",
        style      => {
            height    => 50,
            pos_color => 'rgb(0, 135, 0)',
            neg_color => '#f00',
            bg_color  => 'rgba(232, 255, 220, 0.4)'
        },

        # CoGe-specific stuff
        coge => {
            id   => $gid,
            type => 'gc_content'
        }
    };

    #	print STDERR 'time1: ' . (time - $start_time) . "\n";

    # Add feature tracks
    my @feat_type_names =
      grep( !/chromosome/, $genome->distinct_feature_type_names );
    if (@feat_type_names) {

        # Add main "Features" track
        push @tracks, {
            baseUrl      => "services/JBrowse/track/annotation/$gid/",
            autocomplete => "all",
            track        => "features",
            label        => "features",
            key          => "Features",
            type         => "CoGe/View/Track/CoGeFeatures",
            description  => "note, description",
            storeClass   => "JBrowse/Store/SeqFeature/REST",
            onClick =>
              "FeatAnno.pl?dsg=$gid;chr={chr};start={start};stop={end}",
            maxFeatureScreenDensity => 50,<commit_msg>Remove main feature track max density for better performance<commit_after>package CoGe::Services::JBrowse::Configuration;
use base 'CGI::Application';

use CoGeX;
use CoGe::Accessory::Web;
use JSON;
use Data::Dumper;
use Sort::Versions;
use Cwd 'abs_path';
use Time::HiRes qw ( time );

my $coge_conf;
my %expTypeToName = (
    1 => 'quant',
    2 => 'snp'
);

sub setup {
    my $self = shift;

    #FIXME - move this into service.pl
    $coge_conf = abs_path($0);
    $coge_conf =~ s/services\/JBrowse\/service\.pl/coge\.conf/;

    $self->run_modes(
        'refseq_config' => 'refseq_config',
        'track_config'  => 'track_config',
    );
    $self->start_mode('track_config');
    $self->mode_param('rm');
}

sub refseq_config {
    my $self           = shift;
    my $gid            = $self->query->param('gid');
    my $SEQ_CHUNK_SIZE = 20000;
    print STDERR "Configuration::refseq_config gid=$gid\n";

    # Load config file
    my $P      = CoGe::Accessory::Web::get_defaults($coge_conf);
    my $DBNAME = $P->{DBNAME};
    my $DBHOST = $P->{DBHOST};
    my $DBPORT = $P->{DBPORT};
    my $DBUSER = $P->{DBUSER};
    my $DBPASS = $P->{DBPASS};

    # Connect to the database
    my $connstr = "dbi:mysql:dbname=$DBNAME;host=$DBHOST;port=$DBPORT";
    my $coge = CoGeX->connect( $connstr, $DBUSER, $DBPASS );

    #$coge->storage->debugobj(new DBIxProfiler());
    #$coge->storage->debug(1);

    # Get genome
    my $genome = $coge->resultset('Genome')->find($gid);
    return unless $genome;

    my @chromosomes;
    foreach my $chr ( sort { $b->sequence_length <=> $a->sequence_length }
        $genome->genomic_sequences )
    {
        push @chromosomes,
          {
            name         => $chr->chromosome,
            length       => $chr->sequence_length,
            seqChunkSize => $SEQ_CHUNK_SIZE,
            start        => 0,
            end          => $chr->sequence_length - 1
          };
    }

    return encode_json( \@chromosomes );
}

sub track_config {
    my $self       = shift;
    my $gid        = $self->query->param('gid');
    my $cas_ticket = $self->query->param('ticket');
    print STDERR "Configuration::track_config gid=$gid\n";

    # Get config
    my $P      = CoGe::Accessory::Web::get_defaults($coge_conf);
    my $DBNAME = $P->{DBNAME};
    my $DBHOST = $P->{DBHOST};
    my $DBPORT = $P->{DBPORT};
    my $DBUSER = $P->{DBUSER};
    my $DBPASS = $P->{DBPASS};

    # Connect to the database
    my $connstr = "dbi:mysql:dbname=$DBNAME;host=$DBHOST;port=$DBPORT";
    my $coge = CoGeX->connect( $connstr, $DBUSER, $DBPASS );

    #$coge->storage->debugobj(new DBIxProfiler());
    #$coge->storage->debug(1);

    # Get user
    my $COOKIE_NAME = $P->{COOKIE_NAME};
    my $USER        = undef;
    ($USER) = CoGe::Accessory::Web->login_cas(
        ticket   => $cas_ticket,
        coge     => $coge,
        this_url => $FORM->url()
    ) if ($cas_ticket);
    ($USER) = CoGe::Accessory::LogUser->get_user(
        cookie_name => $COOKIE_NAME,
        coge        => $coge
    ) unless $USER;

    #	my $start_time = time;

    # Get genome
    my $genome = $coge->resultset('Genome')->find($gid);
    return unless $genome;

    my @tracks;

    # Add reference sequence track
    push @tracks, {
        chunkSize     => 20000,
        baseUrl       => "services/JBrowse/service.pl/sequence/$gid/",
        type          => "SequenceTrack",
        storeClass    => "JBrowse/Store/SeqFeature/REST",
        label         => "sequence",
        key           => "Sequence",
        formatVersion => 1,

        # CoGe-specific stuff
        coge => {
            id   => $gid,
            type => 'sequence'
        }
    };

    # Add GC content track
    push @tracks, {

        #baseUrl => "http://geco.iplantcollaborative.org/rchasman/gc/$gid/",
        baseUrl    => "services/JBrowse/track/gc/$gid/",
        type       => "CoGe/View/Track/GC_Content",
        storeClass => "JBrowse/Store/SeqFeature/REST",
        track      => "gc_content",
        label      => "gc_content",
        key        => "GC Content",
        style      => {
            height    => 50,
            pos_color => 'rgb(0, 135, 0)',
            neg_color => '#f00',
            bg_color  => 'rgba(232, 255, 220, 0.4)'
        },

        # CoGe-specific stuff
        coge => {
            id   => $gid,
            type => 'gc_content'
        }
    };

    #	print STDERR 'time1: ' . (time - $start_time) . "\n";

    # Add feature tracks
    my @feat_type_names =
      grep( !/chromosome/, $genome->distinct_feature_type_names );
    if (@feat_type_names) {

        # Add main "Features" track
        push @tracks, {
            baseUrl      => "services/JBrowse/track/annotation/$gid/",
            autocomplete => "all",
            track        => "features",
            label        => "features",
            key          => "Features",
            type         => "CoGe/View/Track/CoGeFeatures",
            description  => "note, description",
            storeClass   => "JBrowse/Store/SeqFeature/REST",
            onClick =>
              "FeatAnno.pl?dsg=$gid;chr={chr};start={start};stop={end}",
            maxFeatureScreenDensity => 10,
{'fraction_non_alphanumeric': 0.1603163885405614, 'fraction_numerical': 0.00790971351403492, 'mean_word_length': 2.046723479282986, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 191, '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': '7148490', 'n_tokens_mistral': 3653, 'n_tokens_neox': 3462, 'n_words': 683}