clean
[sgn.git] / lib / SGN / Controller / AJAX / Accessions.pm
blob0e64a7839580e883b35b8699fea88e2395cc2418
2 =head1 NAME
4 SGN::Controller::AJAX::Accessions - a REST controller class to provide the
5 backend for managing accessions
7 =head1 DESCRIPTION
9 Managing accessions
11 =head1 AUTHOR
13 Jeremy Edwards <jde22@cornell.edu>
15 =cut
17 package SGN::Controller::AJAX::Accessions;
19 use Moose;
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;
25 use Data::Dumper;
26 #use JSON;
28 BEGIN { extends 'Catalyst::Controller::REST' }
30 __PACKAGE__->config(
31 default => 'application/json',
32 stash_key => 'rest',
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) {
39 my $self = shift;
40 my $c = shift;
41 $self->verify_accession_list_POST($c);
44 sub verify_accession_list_POST : Args(0) {
45 my ($self, $c) = @_;
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);
55 else {
56 $self->do_exact_search($c, \@accession_list);
61 sub do_fuzzy_search {
62 my $self = shift;
63 my $c = shift;
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;
71 my @found_accessions;
72 my @fuzzy_accessions;
73 my @absent_accessions;
75 if (!$c->user()) {
76 $c->stash->{rest} = {error => "You need to be logged in to add accessions." };
77 return;
79 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles) ) {
80 $c->stash->{rest} = {error => "You have insufficient privileges to add accessions." };
81 return;
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'};
91 $c->stash->{rest} = {
92 success => "1",
93 absent => @absent_accessions,
94 fuzzy => @fuzzy_accessions,
95 found => @found_accessions
97 return;
100 sub do_exact_search {
101 my $self = shift;
102 my $c = shift;
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 };
117 else {
118 my $exact_synonym_rs = $schema->resultset("Stock::Stockprop")->search(
119 { value =>
121 -ilike => $a },
122 'lower(type.name)' => { like => '%synonym%' },
124 {join => 'type' }
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};
130 else {
131 push @absent_accessions, $a;
136 my $rest = {
137 success => "1",
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) {
149 my ($self, $c) = @_;
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');
154 my @accession_list;
155 my $stock_add;
156 my $validated;
157 my $added;
158 my $dbh = $c->dbc->dbh;
159 my $user_id;
160 my $owner_name;
161 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
163 if (!$c->user()) {
164 $c->stash->{rest} = {error => "You need to be logged in to create a field book" };
165 return;
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." };
173 return;
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} );
179 } else {
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();
183 if (!$validated) {
184 $c->stash->{rest} = {error => "Stocks already exist in the database" };
186 $added = $stock_add->add_accessions();
187 if (!$added) {
188 $c->stash->{rest} = {error => "Could not add stocks to the database" };
190 $c->stash->{rest} = {success => "1"};
191 return;
194 sub populations : Path('/ajax/manage_accessions/populations') : ActionClass('REST') { }
196 sub populations_GET : Args(0) {
197 my $self = shift;
198 my $c = shift;
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;
209 my $json = new JSON;
210 if ($list_json) {
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;
216 else {
217 return;