App-Mimosa

 view release on metacpan or  search on metacpan

lib/App/Mimosa/Controller/JSON.pm  view on Meta::CPAN

package App::Mimosa::Controller::JSON;
use Moose;
use Bio::Chado::Schema;
use File::Spec::Functions;
use Set::Scalar;
use Digest::SHA1 'sha1_hex';
use App::Mimosa::Util qw/slurp/;

use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller::REST' }

__PACKAGE__->config(
    default   => 'application/json',
    stash_key => 'rest',
    'map' => {
        # Work around an ExtJS bug that sends the wrong content-type
        'text/html'        => 'JSON',
    }

);

sub grid_json :Path("/api/grid/json.json") :ActionClass('REST') :Local { }

# Answer GET requests to the above Path
sub grid_json_GET {
    my ( $self, $c ) = @_;

    my $data = _grid_json_data($c);
    # Return a 200 OK, with the data in entity serialized in the body
    $self->status_ok( $c, entity => $data );
}

sub autodetect :Private {
    my ($self, $c) = @_;
    my $bcs        = $c->model('BCS');
    my $config     = $self->_app->config;
    my $seq_dir    = $config->{sequence_data_dir};

    # This isn't madness, this is FASTA!
    # http://en.wikipedia.org/wiki/FASTA_format#File_extension
    my @fasta_extensions = qw/fasta fna ffn faa frn fa seq mpfa/;
    my @globs            = glob(join " ", map { catfile($seq_dir, "*.$_") } @fasta_extensions);
    my @seq_files  = map { $_ =~ s!$seq_dir/(.*)!$1!g; $_ } grep { !-d } @globs;
    my $rs         = $bcs->resultset('Mimosa::SequenceSet');
    my @shortnames = map { $_->shortname } ($rs->all);

    # set difference
    my @new_sets = (Set::Scalar->new(@seq_files) - Set::Scalar->new(@shortnames))->elements;

    # nonzero difference means we have new sequence files, so we grab metadata about them
    if (@new_sets) {
        for my $seq_set (@new_sets) {
            my $fasta      = slurp("$seq_dir/$seq_set");
            my $sha1       = sha1_hex($fasta);
            $c->log->debug("adding $seq_dir/$seq_set ($sha1) to the db");
            # insert data about new sequences
            $rs->create({
                title     => "stuff and thangs",
                sha1      => $sha1,
                shortname => $seq_set,
                # do we have to guess this?
                alphabet  => 'nucleotide',
                info_url  => 'http://localhost',
            });
        }
    }
}



( run in 0.562 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )