add transformant
[sgn.git] / lib / SGN / Controller / AJAX / Transformation.pm
blob86f6b6845e147faca14257d14e91b59d9fe66737
1 package SGN::Controller::AJAX::Transformation;
3 use Moose;
4 use Data::Dumper;
5 use JSON;
6 use CXGN::People::Person;
7 use SGN::Image;
8 use CXGN::Stock::StockLookup;
9 use CXGN::Location::LocationLookup;
10 use SGN::Model::Cvterm;
11 use CXGN::List::Validate;
12 use CXGN::List;
13 use CXGN::Transformation::AddTransformationProject;
14 use CXGN::Transformation::AddTransformationIdentifier;
15 use CXGN::Transformation::Transformation;
16 use CXGN::Transformation::AddTransformant;
17 use List::MoreUtils qw /any /;
20 BEGIN { extends 'Catalyst::Controller::REST' }
22 __PACKAGE__->config(
23 default => 'application/json',
24 stash_key => 'rest',
25 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
29 sub add_transformation_project : Path('/ajax/transformation/add_transformation_project') : ActionClass('REST') {}
31 sub add_transformation_project_POST :Args(0){
32 my ($self, $c) = @_;
33 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
34 my $dbh = $c->dbc->dbh;
35 my $project_name = $c->req->param('project_name');
36 my $breeding_program_id = $c->req->param('project_program_id');
37 my $location = $c->req->param('project_location');
38 my $year = $c->req->param('year');
39 my $project_description = $c->req->param('project_description');
40 $project_name =~ s/^\s+|\s+$//g;
42 print STDERR "PROJECT NAME =".Dumper($project_name)."\n";
43 if (!$c->user()){
44 $c->stash->{rest} = {error => "You need to be logged in to add a transformation project."};
45 return;
48 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles)){
49 $c->stash->{rest} = {error => "you have insufficient privileges to add a transformation project." };
50 return;
53 my $user_id = $c->user()->get_object()->get_sp_person_id();
55 my $geolocation_lookup = CXGN::Location::LocationLookup->new(schema =>$schema);
56 $geolocation_lookup->set_location_name($location);
57 if(!$geolocation_lookup->get_geolocation()){
58 $c->stash->{rest}={error => "Location not found"};
59 return;
62 my $error;
63 eval{
64 my $add_transformation_project = CXGN::Transformation::AddTransformationProject->new({
65 chado_schema => $schema,
66 dbh => $dbh,
67 breeding_program_id => $breeding_program_id,
68 year => $year,
69 project_description => $project_description,
70 transformation_project_name => $project_name,
71 nd_geolocation_id => $geolocation_lookup->get_geolocation()->nd_geolocation_id(),
72 owner_id => $user_id
73 });
75 my $return = $add_transformation_project->save_transformation_project();
76 if ($return->{error}){
77 $error = $return->{error};
81 if ($@) {
82 $c->stash->{rest} = {error => $@};
83 return;
86 if ($error){
87 $c->stash->{rest} = {error => $error};
88 } else {
89 $c->stash->{rest} = {success => 1};
95 sub add_transformation_identifier : Path('/ajax/transformation/add_transformation_identifier') : ActionClass('REST') {}
97 sub add_transformation_identifier_POST :Args(0){
98 my ($self, $c) = @_;
99 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
100 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
101 my $dbh = $c->dbc->dbh;
102 my $transformation_identifier = $c->req->param('transformation_identifier');
103 my $plant_material = $c->req->param('plant_material');
104 my $vector_construct = $c->req->param('vector_construct');
105 my $notes = $c->req->param('notes');
106 my $transformation_project_id = $c->req->param('transformation_project_id');
107 $transformation_identifier =~ s/^\s+|\s+$//g;
109 if (!$c->user()){
110 $c->stash->{rest} = {error => "You need to be logged in to add a transformation transformation identifier."};
111 return;
114 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles)){
115 $c->stash->{rest} = {error => "you have insufficient privileges to add a transformation identifier." };
116 return;
119 my $user_id = $c->user()->get_object()->get_sp_person_id();
121 my $accession_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'accession', 'stock_type')->cvterm_id();
122 my $vector_construct_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema,'vector_construct', 'stock_type')->cvterm_id();
124 if ($schema->resultset("Stock::Stock")->find({uniquename => $transformation_identifier})){
125 $c->stash->{rest} = {error => "Transformation identifier already exists." };
126 return 0;
129 if (! $schema->resultset("Stock::Stock")->find({uniquename => $plant_material, type_id => $accession_cvterm_id })){
130 $c->stash->{rest} = {error => "Plant material does not exist or does not exist as accession uniquename." };
131 return;
134 if (! $schema->resultset("Stock::Stock")->find({uniquename => $vector_construct, type_id => $vector_construct_cvterm_id })){
135 $c->stash->{rest} = {error => "vector construct does not exist or does not exist as vector construct uniquename." };
136 return;
139 eval {
140 my $add_transformation = CXGN::Transformation::AddTransformationIdentifier->new({
141 chado_schema => $schema,
142 phenome_schema => $phenome_schema,
143 dbh => $dbh,
144 transformation_project_id => $transformation_project_id,
145 transformation_identifier => $transformation_identifier,
146 plant_material => $plant_material,
147 vector_construct => $vector_construct,
148 notes => $notes,
149 owner_id => $user_id,
152 $add_transformation->add_transformation_identifier();
155 if ($@) {
156 $c->stash->{rest} = { success => 0, error => $@ };
157 print STDERR "An error condition occurred, was not able to create transformation identifier. ($@).\n";
158 return;
161 $c->stash->{rest} = { success => 1 };
166 sub get_transformations_in_project :Path('/ajax/transformation/transformations_in_project') :Args(1) {
167 my $self = shift;
168 my $c = shift;
169 my $project_id = shift;
170 my $schema = $c->dbic_schema("Bio::Chado::Schema");
171 my $dbh = $c->dbc->dbh;
173 my $transformation_obj = CXGN::Transformation::Transformation->new({schema=>$schema, dbh=>$dbh, project_id=>$project_id});
175 my $result = $transformation_obj->get_transformations_in_project();
176 # print STDERR "RESULT =".Dumper($result)."\n";
177 my @transformations;
178 foreach my $r (@$result){
179 my ($transformation_id, $transformation_name, $plant_id, $plant_name, $vector_id, $vector_name) =@$r;
180 push @transformations, [qq{<a href="/transformation/$transformation_id">$transformation_name</a>}, qq{<a href="/stock/$plant_id/view">$plant_name</a>}, qq{<a href="/stock/$vector_id/view">$vector_name</a>},'' , ''];
183 $c->stash->{rest} = { data => \@transformations };
188 sub add_transformants : Path('/ajax/transformation/add_transformants') : ActionClass('REST') {}
190 sub add_transformants_POST :Args(0){
191 my ($self, $c) = @_;
192 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
193 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
194 my $dbh = $c->dbc->dbh;
195 my $transformation_name = $c->req->param('transformation_name');
196 my $transformation_stock_id = $c->req->param('transformation_stock_id');
197 my $new_name_count = $c->req->param('new_name_count');
198 print STDERR "TRANSFORMATION NAME =".Dumper($transformation_name)."\n";
199 print STDERR "COUNT =".Dumper($new_name_count)."\n";
201 if (!$c->user()){
202 $c->stash->{rest} = {error => "You need to be logged in to add new transformants."};
203 return;
206 if (!any { $_ eq "curator" || $_ eq "submitter" } ($c->user()->roles)){
207 $c->stash->{rest} = {error => "you have insufficient privileges to add new transformants." };
208 return;
211 my $user_id = $c->user()->get_object()->get_sp_person_id();
212 my $start_number = 1;
213 my $basename = $transformation_name.'_T';
214 my @new_transformant_names = ();
215 foreach my $n (1..$new_name_count) {
216 push @new_transformant_names, $basename. (sprintf "%04d", $n + $start_number -1);
219 foreach my $new_name (@new_transformant_names) {
220 my $validate_new_name_rs = $schema->resultset("Stock::Stock")->search({uniquename=> $new_name});
221 if ($validate_new_name_rs->count() > 0) {
222 $c->stash->{rest} = {error_string => "Error creating new transformant name",};
223 return;
227 eval {
228 my $add_transformants = CXGN::Transformation::AddTransformant->new({
229 schema => $schema,
230 phenome_schema => $phenome_schema,
231 dbh => $dbh,
232 transformation_stock_id => $transformation_stock_id,
233 transformant_names => \@new_transformant_names,
234 owner_id => $user_id,
237 $add_transformants->add_transformant();
240 if ($@) {
241 $c->stash->{rest} = { success => 0, error => $@ };
242 print STDERR "An error condition occurred, was not able to create transformation identifier. ($@).\n";
243 return;
246 $c->stash->{rest} = { success => 1 };