4 SGN::Controller::AJAX::Accessions - a REST controller class to provide the
5 backend for managing accessions
13 Jeremy Edwards <jde22@cornell.edu>
17 package SGN
::Controller
::AJAX
::Accessions
;
20 use JSON
-support_by_pp
;
21 use List
::MoreUtils qw
/any /;
22 use CXGN
::BreedersToolbox
::Accessions
;
23 use CXGN
::BreedersToolbox
::AccessionsFuzzySearch
;
24 use CXGN
::Stock
::AddStocks
;
28 BEGIN { extends
'Catalyst::Controller::REST' }
31 default => 'application/json',
33 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
36 sub verify_accession_list
: Path
('/ajax/accession_list/verify') : ActionClass
('REST') { }
38 sub verify_accession_list_GET
: Args
(0) {
41 $self->verify_accession_list_POST($c);
44 sub verify_accession_list_POST
: Args
(0) {
47 my $accession_list_json = $c->req->param('accession_list');
48 my @accession_list = @
{_parse_list_from_json
($accession_list_json)};
50 my $do_fuzzy_search = $c->req->param('do_fuzzy_search');
52 if ($do_fuzzy_search) {
53 $self->do_fuzzy_search($c, \
@accession_list);
56 $self->do_exact_search($c, \
@accession_list);
64 my $accession_list = shift;
66 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
67 my $fuzzy_accession_search = CXGN
::BreedersToolbox
::AccessionsFuzzySearch
->new({schema
=> $schema});
68 my $fuzzy_search_result;
69 my $max_distance = 0.2;
70 my @accession_list = @
$accession_list;
73 my @absent_accessions;
76 $c->stash->{rest
} = {error
=> "You need to be logged in to add accessions." };
79 if (!any
{ $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
80 $c->stash->{rest
} = {error
=> "You have insufficient privileges to add accessions." };
84 $fuzzy_search_result = $fuzzy_accession_search->get_matches(\
@accession_list, $max_distance);
85 print STDERR
"\n\nResult:\n".Data
::Dumper
::Dumper
($fuzzy_search_result)."\n\n";
87 @found_accessions = $fuzzy_search_result->{'found'};
88 @fuzzy_accessions = $fuzzy_search_result->{'fuzzy'};
89 @absent_accessions = $fuzzy_search_result->{'absent'};
93 absent
=> @absent_accessions,
94 fuzzy
=> @fuzzy_accessions,
95 found
=> @found_accessions
100 sub do_exact_search
{
103 my $accession_list = shift;
105 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
107 my @found_accessions;
108 my @absent_accessions;
109 my @fuzzy_accessions;
111 foreach my $a (@
$accession_list) {
112 print STDERR
"CHECKING $a...\n";
113 my $exact_search_rs = $schema->resultset("Stock::Stock")->search( { -or => { name
=> { -ilike
=> $a}, uniquename
=> { -ilike
=> $a }}});
114 if ($exact_search_rs->count() > 0) {
115 push @found_accessions, { unique_name
=> $a, matched_string
=> $a };
118 my $exact_synonym_rs = $schema->resultset("Stock::Stockprop")->search(
122 'lower(type.name)' => { like
=> '%synonym%' },
126 if ($exact_synonym_rs ->count() > 0) {
127 push @found_accessions, { unique_name
=> $a, matched_string
=> $a};
128 push @fuzzy_accessions, { unique_name
=> $a, matched_string
=> $a};
131 push @absent_accessions, $a;
138 absent
=> \
@absent_accessions,
139 found
=> \
@found_accessions,
140 fuzzy
=> \
@fuzzy_accessions
142 print STDERR Dumper
($rest);
143 $c->stash->{rest
} = $rest;
146 sub add_accession_list
: Path
('/ajax/accession_list/add') : ActionClass
('REST') { }
148 sub add_accession_list_POST
: Args
(0) {
150 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
151 my $accession_list_json = $c->req->param('accession_list');
152 my $species_name = $c->req->param('species_name');
153 my $population_name = $c->req->param('population_name');
158 my $dbh = $c->dbc->dbh;
161 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
164 $c->stash->{rest
} = {error
=> "You need to be logged in to create a field book" };
168 $user_id = $c->user()->get_object()->get_sp_person_id();
169 $owner_name = $c->user()->get_object()->get_username();
171 if (!any
{ $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
172 $c->stash->{rest
} = {error
=> "You have insufficient privileges to create a field book." };
176 @accession_list = @
{_parse_list_from_json
($accession_list_json)};
177 if ($population_name eq '') {
178 $stock_add = CXGN
::Stock
::AddStocks
->new({ schema
=> $schema, stocks
=> \
@accession_list, species
=> $species_name, owner_name
=> $owner_name,phenome_schema
=> $phenome_schema, dbh
=> $dbh} );
180 $stock_add = CXGN
::Stock
::AddStocks
->new({ schema
=> $schema, stocks
=> \
@accession_list, species
=> $species_name, owner_name
=> $owner_name,phenome_schema
=> $phenome_schema, dbh
=> $dbh, population_name
=> $population_name} );
182 $validated = $stock_add->validate_stocks();
184 $c->stash->{rest
} = {error
=> "Stocks already exist in the database" };
186 $added = $stock_add->add_accessions();
188 $c->stash->{rest
} = {error
=> "Could not add stocks to the database" };
190 $c->stash->{rest
} = {success
=> "1"};
194 sub populations
: Path
('/ajax/manage_accessions/populations') : ActionClass
('REST') { }
196 sub populations_GET
: Args
(0) {
200 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
201 my $ac = CXGN
::BreedersToolbox
::Accessions
->new( { schema
=>$schema });
202 my $populations = $ac->get_all_populations($c);
204 $c->stash->{rest
} = { populations
=> $populations };
207 sub _parse_list_from_json
{
208 my $list_json = shift;
211 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
212 #my $decoded_list = decode_json($list_json);
213 my @array_of_list_items = @
{$decoded_list};
214 return \
@array_of_list_items;