Skip to content

Commit

Permalink
Add web service for adding organisms to sessions
Browse files Browse the repository at this point in the history
Refs #1447
  • Loading branch information
kimrutherford committed May 7, 2018
1 parent 85689ef commit 5b2e7cb
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/Canto/Controller/Curs.pm
Expand Up @@ -2554,6 +2554,22 @@ sub ws_add_gene : Chained('top') PathPart('ws/gene/add')
$c->forward('View::JSON');
}

sub ws_add_organism : Chained('top') PathPart('ws/organism/add')
{
my ($self, $c, $taxonid) = @_;

my $st = $c->stash();
my $schema = $st->{schema};


my $service_utils = Canto::Curs::ServiceUtils->new(curs_schema => $schema,
config => $c->config());

$st->{json_data} = $service_utils->add_organism_by_taxonid($taxonid);

$c->forward('View::JSON');
}

sub ws_settings_get_all : Chained('top') PathPart('ws/settings/get_all')
{
my ($self, $c) = @_;
Expand Down
71 changes: 71 additions & 0 deletions lib/Canto/Curs/OrganismManager.pm
@@ -0,0 +1,71 @@
package Canto::Curs::OrganismManager;

=head1 NAME
Canto::Curs::OrganismManager - Manage adding and removing session organism
=head1 SYNOPSIS
=head1 AUTHOR
Kim Rutherford C<< <kmr44@cam.ac.uk> >>
=head1 BUGS
Please report any bugs or feature requests to C<kmr44@cam.ac.uk>.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Canto::Curs::OrganismManager
=over 4
=back
=head1 COPYRIGHT & LICENSE
Copyright 2013 Kim Rutherford, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 FUNCTIONS
=cut

use Carp;
use Moose;

use Canto::Track;
use Canto::CursDB::Organism;

has curs_schema => (is => 'rw', isa => 'Canto::CursDB', required => 1);
has organism_lookup => (is => 'ro', init_arg => undef, lazy_build => 1);

with 'Canto::Role::Configurable';

sub _build_organism_lookup
{
my $self = shift;

return Canto::Track::get_adaptor($self->config(), 'organism');
}

sub add_organism_by_taxonid
{
my $self = shift;

my $taxonid = shift;

my $organism_details = $self->organism_lookup->lookup_by_taxonid($taxonid);

if ($organism_details) {
return Canto::CursDB::Organism::get_organism($self->curs_schema(), $taxonid);
} else {
return undef;
}
}

1;
54 changes: 54 additions & 0 deletions lib/Canto/Curs/ServiceUtils.pm
Expand Up @@ -51,13 +51,15 @@ use Canto::Curs::GeneProxy;
use Canto::Curs::Utils;
use Canto::Curs::ConditionUtil;
use Canto::Curs::MetadataStorer;
use Canto::Curs::OrganismManager;

has curs_schema => (is => 'ro', isa => 'Canto::CursDB', required => 1);

has ontology_lookup => (is => 'ro', init_arg => undef, lazy_build => 1);
has allele_lookup => (is => 'ro', init_arg => undef, lazy_build => 1);
has genotype_lookup => (is => 'ro', init_arg => undef, lazy_build => 1);
has organism_lookup => (is => 'ro', init_arg => undef, lazy_build => 1);
has organism_manager => (is => 'ro', init_arg => undef, lazy_build => 1);

has state => (is => 'rw', init_arg => undef,
isa => 'Canto::Curs::State', lazy_build => 1);
Expand Down Expand Up @@ -112,6 +114,14 @@ sub _build_organism_lookup
return Canto::Track::get_adaptor($self->config(), 'organism');
}

sub _build_organism_manager
{
my $self = shift;

return Canto::Curs::OrganismManager->new(config => $self->config(),
curs_schema => $self->curs_schema());
}

sub _build_curator_manager
{
my $self = shift;
Expand Down Expand Up @@ -1306,4 +1316,48 @@ sub add_gene_by_identifier
}
}


=head2 add_organism_by_taxonid
Usage : $service_utils->add_organism_by_taxonid($taxonid);
Function: Add the given organism to the session
Args : $taxonid
Return : a hash, with keys:
status - "success" or "error"
message - on error, the error message
=cut

sub add_organism_by_taxonid
{
my $self = shift;
my $taxonid = shift;

my $curs_schema = $self->curs_schema();

my $organism_manager = $self->organism_manager();

try {
$curs_schema->txn_begin();

my $organism = $organism_manager->add_organism_by_taxonid($taxonid);

if ($organism) {
$curs_schema->txn_commit();
return {
status => 'success',
};
} else {
return {
status => 'error',
message => "organism with taxonid $taxonid not found",
};
}
} catch {
$curs_schema->txn_rollback();
chomp $_;
return _make_error($_);
}
}

1;
14 changes: 13 additions & 1 deletion t/80_curs_service_utils_organism.t
@@ -1,6 +1,6 @@
use strict;
use warnings;
use Test::More tests => 3;
use Test::More tests => 8;
use Test::Deep;
use JSON;

Expand All @@ -26,3 +26,15 @@ my $res = $service_utils->list_for_service('organism');
is (@$res, 1);
is ($res->[0]->{full_name}, "Schizosaccharomyces pombe");
is ($res->[0]->{gene_count}, 4);


# add an organism
$service_utils->add_organism_by_taxonid(4932);

$res = $service_utils->list_for_service('organism');

is (@$res, 2);
is ($res->[0]->{full_name}, "Schizosaccharomyces pombe");
is ($res->[0]->{gene_count}, 4);
is ($res->[1]->{full_name}, "Saccharomyces cerevisiae");
is ($res->[1]->{gene_count}, 0);

0 comments on commit 5b2e7cb

Please sign in to comment.