From f6c481397140888c4122e25f2938b263418dbf34 Mon Sep 17 00:00:00 2001 From: nickmorales Date: Wed, 14 Sep 2016 21:12:40 +0000 Subject: [PATCH] add all cross progeny from folder page --- js/CXGN/List.js | 90 +++++++++++++++++++--------- lib/CXGN/Cross.pm | 40 +++++++++++++ lib/CXGN/Pedigree/AddCrosses.pm | 4 +- lib/SGN/Controller/AJAX/Cross.pm | 27 +++------ lib/SGN/Controller/AJAX/List.pm | 37 ++++++++++++ lib/SGN/Controller/Folder.pm | 16 ++++- mason/breeders_toolbox/folder/folder.mas | 18 ++++-- mason/breeders_toolbox/folder/folder_set.mas | 12 ++-- 8 files changed, 184 insertions(+), 60 deletions(-) create mode 100644 lib/CXGN/Cross.pm diff --git a/js/CXGN/List.js b/js/CXGN/List.js index 40552958d..d4f9705b6 100644 --- a/js/CXGN/List.js +++ b/js/CXGN/List.js @@ -572,6 +572,28 @@ CXGN.List.prototype = { return info; }, + + addCrossProgenyToList: function(list_id, text) { + if (! text) { + return; + } + var list = text.split("\n"); + list = list.filter(function(n){ return n != '' }); + console.log(list); + var addeditems; + jQuery.ajax( { + url: '/list/add_cross_progeny', + async: false, + data: { 'list_id':list_id, 'cross_id_list' : JSON.stringify(list) }, + success: function(response) { + console.log(response); + addeditems = response.success.count; + } + }); + //var info = this.addBulk(list_id, list); + + return addeditems; + }, /* listSelect: Creates an html select with lists of requested types. @@ -805,23 +827,28 @@ function addToListMenu(listMenuDiv, dataDiv, options) { var listType; var typeSourceDiv; var type; + var addition_type; if (options) { - if (options.selectText) { - selectText = options.selectText; - } - if (options.typeSourceDiv) { - var sourcetype = getData(options.typeSourceDiv, selectText); - if (sourcetype) { - type = sourcetype.replace(/(\n|\r)+$/, ''); - } - } - if (options.listType) { - type = options.listType; - } + if (options.selectText) { + selectText = options.selectText; + } + if (options.typeSourceDiv) { + var sourcetype = getData(options.typeSourceDiv, selectText); + if (sourcetype) { + type = sourcetype.replace(/(\n|\r)+$/, ''); + } + } + if (options.listType) { + type = options.listType; + } + if (options.additionType) { + addition_type = options.additionType; + } } + html = '
'; - html += '
'; + html += '
'; html += '

'; html += '
'+lo.listSelect(dataDiv, [ type ]); @@ -832,22 +859,27 @@ function addToListMenu(listMenuDiv, dataDiv, options) { var list_id = 0; - jQuery('#'+dataDiv+'_add_to_new_list').click( - function() { - var lo = new CXGN.List(); - var new_name = jQuery('#'+dataDiv+'_new_list_name').val(); - var type = jQuery('#'+dataDiv+'_list_type').val(); - - var data = getData(dataDiv, selectText); - - list_id = lo.newList(new_name); - if (list_id > 0) { - var elementsAdded = lo.addToList(list_id, data); - if (type) { lo.setListType(list_id, type); } - alert("Added "+elementsAdded+" list elements to list "+new_name+" and set type to "+type); - } - } - ); + jQuery('#'+dataDiv+'_add_to_new_list').click( function() { + var lo = new CXGN.List(); + var new_name = jQuery('#'+dataDiv+'_new_list_name').val(); + var type = jQuery('#'+dataDiv+'_list_type').val(); + var addition_type = jQuery('#'+dataDiv+'_addition_type').val(); + + var data = getData(dataDiv, selectText); + list_id = lo.newList(new_name); + if (list_id > 0) { + var elementsAdded; + if (addition_type == 'cross_progeny') { + elementsAdded = lo.addCrossProgenyToList(list_id, data); + } else { + elementsAdded = lo.addToList(list_id, data); + } + if (type) { + lo.setListType(list_id, type); + } + alert("Added "+elementsAdded+" list elements to list "+new_name+" and set type to "+type); + } + }); jQuery('#'+dataDiv+'_button').click( function() { diff --git a/lib/CXGN/Cross.pm b/lib/CXGN/Cross.pm new file mode 100644 index 000000000..0ec3b5b92 --- /dev/null +++ b/lib/CXGN/Cross.pm @@ -0,0 +1,40 @@ + +package CXGN::Cross; + +use Moose; +use SGN::Model::Cvterm; +use Data::Dumper; + +has 'bcs_schema' => ( isa => 'Bio::Chado::Schema', + is => 'rw', + required => 1, +); + +has 'cross_stock_id' => (isa => "Int", + is => 'rw', + required => 1, +); + +sub get_cross_relationships { + my $self = shift; + my $crs = $self->bcs_schema->resultset("Stock::StockRelationship")->search( { object_id => $self->cross_stock_id } ); + + my $maternal_parent = ""; + my $paternal_parent = ""; + my @progeny = (); + + foreach my $child ($crs->all()) { + if ($child->type->name() eq "female_parent") { + $maternal_parent = [ $child->subject->name, $child->subject->stock_id() ]; + } + if ($child->type->name() eq "male_parent") { + $paternal_parent = [ $child->subject->name, $child->subject->stock_id() ]; + } + if ($child->type->name() eq "member_of") { + push @progeny, [ $child->subject->name, $child->subject->stock_id() ]; + } + } + return ($maternal_parent, $paternal_parent, \@progeny); +} + +1; diff --git a/lib/CXGN/Pedigree/AddCrosses.pm b/lib/CXGN/Pedigree/AddCrosses.pm index 28c6cb654..77d52119f 100644 --- a/lib/CXGN/Pedigree/AddCrosses.pm +++ b/lib/CXGN/Pedigree/AddCrosses.pm @@ -57,7 +57,7 @@ has 'crosses' => (isa =>'ArrayRef[Pedigree]', is => 'rw', predicate => 'has_cros has 'location' => (isa =>'Str', is => 'rw', predicate => 'has_location', required => 1,); has 'program' => (isa =>'Str', is => 'rw', predicate => 'has_program', required => 1,); has 'owner_name' => (isa => 'Str', is => 'rw', predicate => 'has_owner_name', required => 1,); -has 'parent_folder_id' => (isa => 'Str', is => 'rw', predicate => 'has_owner_name', required => 0,); +has 'parent_folder_id' => (isa => 'Str', is => 'rw', predicate => 'has_parent_folder_id', required => 0,); sub add_crosses { my $self = shift; @@ -74,7 +74,7 @@ sub add_crosses { #lookup user by name my $owner_name = $self->get_owner_name(); - my $parent_folder_id = $self->get_parent_folder_id() || 0; + $parent_folder_id = $self->get_parent_folder_id() || 0; my $dbh = $self->get_dbh(); my $owner_sp_person_id = CXGN::People::Person->get_person_by_username($dbh, $owner_name); #add person id as an option. diff --git a/lib/SGN/Controller/AJAX/Cross.pm b/lib/SGN/Controller/AJAX/Cross.pm index 662c9d2d0..cf1dadef6 100644 --- a/lib/SGN/Controller/AJAX/Cross.pm +++ b/lib/SGN/Controller/AJAX/Cross.pm @@ -38,6 +38,7 @@ use CXGN::Trial::Folder; use Carp; use File::Path qw(make_path); use File::Spec::Functions qw / catfile catdir/; +use CXGN::Cross; BEGIN { extends 'Catalyst::Controller::REST' } @@ -400,27 +401,13 @@ sub get_cross_relationships :Path('/cross/ajax/relationships') :Args(1) { return; } - my $crs = $schema->resultset("Stock::StockRelationship")->search( { object_id => $cross_id } ); + my $cross_obj = CXGN::Cross->new({bcs_schema=>$schema, cross_stock_id=>$cross_id}); + my ($maternal_parent, $paternal_parent, $progeny) = $cross_obj->get_cross_relationships(); - my $maternal_parent = ""; - my $paternal_parent = ""; - my @progeny = (); - - foreach my $child ($crs->all()) { - if ($child->type->name() eq "female_parent") { - $maternal_parent = [ $child->subject->name, $child->subject->stock_id() ]; - } - if ($child->type->name() eq "male_parent") { - $paternal_parent = [ $child->subject->name, $child->subject->stock_id() ]; - } - if ($child->type->name() eq "member_of") { - push @progeny, [ $child->subject->name, $child->subject->stock_id() ]; - } - } - - $c->stash->{rest} = { maternal_parent => $maternal_parent, - paternal_parent => $paternal_parent, - progeny => \@progeny, + $c->stash->{rest} = { + maternal_parent => $maternal_parent, + paternal_parent => $paternal_parent, + progeny => $progeny, }; } diff --git a/lib/SGN/Controller/AJAX/List.pm b/lib/SGN/Controller/AJAX/List.pm index f25aea844..786b28f9d 100644 --- a/lib/SGN/Controller/AJAX/List.pm +++ b/lib/SGN/Controller/AJAX/List.pm @@ -9,6 +9,8 @@ use Data::Dumper; use CXGN::List; use CXGN::List::Validate; use CXGN::List::Transform; +use CXGN::Cross; +use JSON; BEGIN { extends 'Catalyst::Controller::REST'; } @@ -391,6 +393,41 @@ sub copy_public_list : Path('/list/public/copy') Args(0) { } } +sub add_cross_progeny : Path('/list/add_cross_progeny') Args(0) { + my $self = shift; + my $c = shift; + my $cross_id_list = decode_json($c->req->param("cross_id_list")); + print STDERR Dumper $cross_id_list; + my $list_id = $c->req->param("list_id"); + + my $list = CXGN::List->new( { dbh=>$c->dbc->dbh(), list_id => $list_id }); + + my %response; + $response{'count'} = 0; + foreach (@$cross_id_list) { + my $cross = CXGN::Cross->new({bcs_schema=>$c->dbic_schema("Bio::Chado::Schema"), cross_stock_id=>$_}); + my ($maternal_parent, $paternal_parent, $progeny) = $cross->get_cross_relationships(); + + my @accession_names; + foreach (@$progeny) { + push @accession_names, $_->[0]; + } + + my $r = $list->add_bulk(\@accession_names); + if ($r->{error}) { + $c->stash->{rest} = { error => $r->{error}}; + return; + } + if (scalar(@{$r->{duplicates}}) > 0){ + push $response{'duplicates'}, $r->{duplicates}; + } + $response{'count'} += $r->{count}; + } + print STDERR Dumper \%response; + $c->stash->{rest} = { duplicates => $response{'duplicates'} }; + $c->stash->{rest}->{success} = { count => $response{'count'} }; +} + sub add_bulk : Path('/list/add/bulk') Args(0) { my $self = shift; my $c = shift; diff --git a/lib/SGN/Controller/Folder.pm b/lib/SGN/Controller/Folder.pm index bb58bfa5d..c30402a0f 100644 --- a/lib/SGN/Controller/Folder.pm +++ b/lib/SGN/Controller/Folder.pm @@ -28,7 +28,21 @@ sub folder_page :Path("/folder") Args(1) { my $folder_project = $self->schema->resultset("Project::Project")->find( { project_id => $folder_id } ); my $folder = CXGN::Trial::Folder->new({ bcs_schema => $self->schema, folder_id => $folder_id }); - $c->stash->{children} = $folder->children(); + my $children = $folder->children(); + my @crosses; + my $cross_type_id = SGN::Model::Cvterm->get_cvterm_row($self->schema(), 'cross', 'stock_type')->cvterm_id(); + foreach (@$children) { + if ($_->folder_type eq 'cross') { + my $cross_stock = $self->schema->resultset("Project::Project")->search({ 'me.project_id' => $_->folder_id() })->search_related('nd_experiment_projects')->search_related('nd_experiment')->search_related('nd_experiment_stocks')->search_related('stock', {'stock.type_id'=>$cross_type_id})->first(); + if ($cross_stock) { + push @crosses, [$cross_stock->stock_id(), $cross_stock->uniquename()]; + } + } + } + print STDERR Dumper \@crosses; + + $c->stash->{children} = $children; + $c->stash->{crosses} = \@crosses; $c->stash->{project_parent} = $folder->project_parent(); $c->stash->{breeding_program} = $folder->breeding_program(); $c->stash->{folder_id} = $folder_id; diff --git a/mason/breeders_toolbox/folder/folder.mas b/mason/breeders_toolbox/folder/folder.mas index f8d4111aa..b6deecd29 100644 --- a/mason/breeders_toolbox/folder/folder.mas +++ b/mason/breeders_toolbox/folder/folder.mas @@ -18,6 +18,7 @@ $folder_id $folder_name $folder_description $children +$crosses $project_parent => undef $breeding_program => undef @@ -74,14 +75,21 @@ foreach (@$children) {

Crosses in Folder

-% foreach (@$children) { -% if ($_->folder_type eq 'cross') { - -% } +% foreach (@$crosses) { + % }
<% $_->name() %>
<% $_->[1] %>
+
+

Add All Cross Progeny to a List

+
+ +
@@ -156,6 +164,8 @@ jQuery(document).ready(function() { }); } }); + + addToListMenu('add_all_cross_progeny_in_folder_to_list', 'add_all_cross_progeny_in_folder_to_list_data', { listType: 'accessions', additionType: 'cross_progeny' } ); }); diff --git a/mason/breeders_toolbox/folder/folder_set.mas b/mason/breeders_toolbox/folder/folder_set.mas index 165ebe3dc..322ed0ffb 100644 --- a/mason/breeders_toolbox/folder/folder_set.mas +++ b/mason/breeders_toolbox/folder/folder_set.mas @@ -20,7 +20,7 @@ $breeding_program_name => undef