clean
[sgn.git] / lib / SGN / Controller / AJAX / BreedersToolbox / Population.pm
bloba1af96e397c166d3fc5f786482ef9e15d5543515
2 package SGN::Controller::AJAX::BreedersToolbox::Population;
4 use Moose;
5 use Bio::GeneticRelationships::Population;
6 use List::MoreUtils qw | any |;
7 use Data::Dumper;
9 BEGIN { extends 'Catalyst::Controller::REST' }
11 __PACKAGE__->config(
12 default => 'application/json',
13 stash_key => 'rest',
14 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
17 has 'schema' => (
18 is => 'rw',
19 isa => 'DBIx::Class::Schema',
20 lazy_build => 1,
23 sub create_population :Path('/ajax/population/new') Args(0) {
24 my $self = shift;
25 my $c = shift;
27 my $population_name = $c->req->param('population_name');
28 my @members = $c->req->param('accessions[]');
29 my $schema = $c->dbic_schema("Bio::Chado::Schema");
31 my $population = Bio::GeneticRelationships::Population->new( { name => $population_name});
32 $population->set_members(\@members);
35 my $population_cvterm_id = $c->model("Cvterm")->get_cvterm_row($schema, "population", "stock_type");
36 my $member_of_cvterm_id = $c->model("Cvterm")->get_cvterm_row($schema, "member_of", "stock_relationship");
38 # create population stock entry
40 my $pop_rs = $schema->resultset("Stock::Stock")->create(
42 name => $population_name,
43 uniquename => $population_name,
44 type_id => $population_cvterm_id->cvterm_id(),
45 });
47 # generate population connections to the members
48 foreach my $m (@members) {
49 my $m_row = $schema->resultset("Stock::Stock")->find({ uniquename => $m });
50 my $connection = $schema->resultset("Stock::StockRelationship")->create(
52 subject_id => $m_row->stock_id,
53 object_id => $pop_rs->stock_id,
54 type_id => $member_of_cvterm_id->cvterm_id(),
55 });
57 $c->stash->{rest} = { message => "Success! Population created" };
58 return;