2 package SGN
::Controller
::AJAX
::BreedersToolbox
;
6 use URI
::FromHash
'uri';
8 use File
::Slurp
"read_file";
11 use CXGN
::BreedersToolbox
::Projects
;
12 use CXGN
::BreedersToolbox
::Delete
;
13 use CXGN
::Trial
::TrialDesign
;
14 use CXGN
::Trial
::TrialCreate
;
15 use CXGN
::Stock
::StockLookup
;
19 BEGIN { extends
'Catalyst::Controller::REST' }
22 default => 'application/json',
24 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
27 sub insert_new_project
: Path
("/ajax/breeders/project/insert") Args
(0) {
32 $c->stash->{rest
} = { error
=> "You must be logged in to add projects." } ;
36 my $params = $c->req->parameters();
38 my $schema = $c->dbic_schema('Bio::Chado::Schema');
40 my $exists = $schema->resultset('Project::Project')->search(
41 { name
=> $params->{project_name
} }
45 $c->stash->{rest
} = { error
=> "This trial name is already used." };
49 my $project = $schema->resultset('Project::Project')->find_or_create(
51 name
=> $params->{project_name
},
52 description
=> $params->{project_description
},
56 my $projectprop_year = $project->create_projectprops( { 'project year' => $params->{year
},}, {autocreate
=>1}); #cv_name => 'project_property' } );
58 $c->stash->{rest
} = { error
=> '' };
61 sub get_breeding_programs
: Path
('/ajax/breeders/all_programs') Args
(0) {
65 my $po = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $c->dbic_schema("Bio::Chado::Schema") });
67 my $breeding_programs = $po->get_breeding_programs();
69 $c->stash->{rest
} = $breeding_programs;
72 sub new_breeding_program
:Path
('/breeders/program/new') Args
(0) {
75 my $name = $c->req->param("name");
76 my $desc = $c->req->param("desc");
78 if (!($c->user() || $c->user()->check_roles('submitter'))) {
79 $c->stash->{rest
} = { error
=> 'You need to be logged in and have sufficient privileges to add a breeding program.' };
82 my $p = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $c->dbic_schema("Bio::Chado::Schema") });
84 my $error = $p->new_breeding_program($name, $desc);
87 $c->stash->{rest
} = { error
=> $error };
90 $c->stash->{rest
} = {};
94 sub delete_breeding_program
:Path
('/breeders/program/delete') Args
(1) {
97 my $program_id = shift;
99 if ($c->user && ($c->user->check_roles("curator"))) {
100 my $p = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $c->dbic_schema("Bio::Chado::Schema") });
101 $p->delete_breeding_program($program_id);
102 $c->stash->{rest
} = [ 1 ];
105 $c->stash->{rest
} = { error
=> "You don't have sufficient privileges to delete breeding programs." };
110 sub get_breeding_programs_by_trial
:Path
('/breeders/programs_by_trial/') Args
(1) {
113 my $trial_id = shift;
115 my $p = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $c->dbic_schema("Bio::Chado::Schema") } );
117 my $projects = $p->get_breeding_programs_by_trial($trial_id);
119 $c->stash->{rest
} = { projects
=> $projects };
123 sub add_data_agreement
:Path
('/breeders/trial/add/data_agreement') Args
(0) {
127 my $project_id = $c->req->param('project_id');
128 my $data_agreement = $c->req->param('text');
131 $c->stash->{rest
} = { error
=> 'You need to be logged in to add a data agreement' };
135 if (!($c->user()->check_roles('curator') || $c->user()->check_roles('submitter'))) {
136 $c->stash->{rest
} = { error
=> 'You do not have the required privileges to add a data agreement to this trial.' };
140 my $schema = $c->dbic_schema('Bio::Chado::Schema');
142 my $data_agreement_cvterm_id_rs = $schema->resultset('Cv::Cvterm')->search( { name
=> 'data_agreement' });
145 if ($data_agreement_cvterm_id_rs->count>0) {
146 $type_id = $data_agreement_cvterm_id_rs->first()->cvterm_id();
150 my $project_rs = $schema->resultset('Project::Project')->search(
151 { project_id
=> $project_id }
154 if ($project_rs->count() == 0) {
155 $c->stash->{rest
} = { error
=> "No such project $project_id", };
159 my $project = $project_rs->first();
161 my $projectprop_rs = $schema->resultset("Project::Projectprop")->search( { 'project_id' => $project_id, 'type_id'=>$type_id });
164 if ($projectprop_rs->count() > 0) {
165 $projectprop = $projectprop_rs->first();
166 $projectprop->value($data_agreement);
167 $projectprop->update();
168 $c->stash->{rest
} = { message
=> 'Updated data agreement.' };
171 $projectprop = $project->create_projectprops( { 'data_agreement' => $data_agreement,}, {autocreate
=>1});
172 $c->stash->{rest
} = { message
=> 'Inserted new data agreement.'};
176 $c->stash->{rest
} = { error
=> $@
};
181 sub get_data_agreement
:Path
('/breeders/trial/data_agreement/get') :Args
(0) {
185 my $project_id = $c->req->param('project_id');
187 my $schema = $c->dbic_schema('Bio::Chado::Schema');
189 my $data_agreement_cvterm_id_rs = $schema->resultset('Cv::Cvterm')->search( { name
=> 'data_agreement' });
191 if ($data_agreement_cvterm_id_rs->count() == 0) {
192 $c->stash->{rest
} = { error
=> "No data agreements have been added yet." };
196 my $type_id = $data_agreement_cvterm_id_rs->first()->cvterm_id();
198 print STDERR
"PROJECTID: $project_id TYPE_ID: $type_id\n";
200 my $projectprop_rs = $schema->resultset('Project::Projectprop')->search(
201 { project_id
=> $project_id, type_id
=>$type_id }
204 if ($projectprop_rs->count() == 0) {
205 $c->stash->{rest
} = { error
=> "No such project $project_id", };
208 my $projectprop = $projectprop_rs->first();
209 $c->stash->{rest
} = { prop_id
=> $projectprop->projectprop_id(), text
=> $projectprop->value() };
213 sub get_all_years
: Path
('/ajax/breeders/trial/all_years' ) Args
(0) {
217 my $bp = CXGN
::BreedersToolbox
::Projects
->new({ schema
=> $c->dbic_schema("Bio::Chado::Schema") });
218 my @years = $bp->get_all_years();
220 $c->stash->{rest
} = { years
=> \
@years };
223 sub get_trial_location
: Path
('/ajax/breeders/trial/location') Args
(1) {
226 my $trial_id = shift;
228 my $t = CXGN
::Trial
->new(
230 bcs_schema
=> $c->dbic_schema("Bio::Chado::Schema"),
231 trial_id
=> $trial_id
235 $c->stash->{rest
} = { location
=> $t->get_location() };
238 $c->stash->{rest
} = { error
=> "The trial with id $trial_id does not exist" };
243 sub get_trial_type
: Path
('/ajax/breeders/trial/type') Args
(1) {
246 my $trial_id = shift;
248 my $t = CXGN
::Trial
->new(
250 bcs_schema
=> $c->dbic_schema("Bio::Chado::Schema"),
251 trial_id
=> $trial_id
254 my $type = $t->get_project_type();
255 $c->stash->{rest
} = { type
=> $type };
258 sub get_all_trial_types
: Path
('/ajax/breeders/trial/alltypes') Args
(0) {
262 my @types = CXGN
::Trial
::get_all_project_types
($c->dbic_schema("Bio::Chado::Schema"));
264 $c->stash->{rest
} = { types
=> \
@types };
268 sub get_accession_plots
:Path
('/ajax/breeders/get_accession_plots') Args
(0) {
271 my $field_trial = $c->req->param("field_trial");
272 my $parent_accession = $c->req->param("parent_accession");
274 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
275 my $field_layout_typeid = $c->model("Cvterm")->get_cvterm_row($schema, "field_layout", "experiment_type")->cvterm_id();
276 my $dbh = $schema->storage->dbh();
278 my $trial = $schema->resultset("Project::Project")->find ({name
=> $field_trial});
279 my $trial_id = $trial->project_id();
281 my $cross_accession = $schema->resultset("Stock::Stock")->find ({uniquename
=> $parent_accession});
282 my $cross_accession_id = $cross_accession->stock_id();
284 my $q = "SELECT stock.stock_id, stock.uniquename
285 FROM nd_experiment_project join nd_experiment on (nd_experiment_project.nd_experiment_id=nd_experiment.nd_experiment_id) AND nd_experiment.type_id= ?
286 JOIN nd_experiment_stock ON (nd_experiment.nd_experiment_id=nd_experiment_stock.nd_experiment_id)
287 JOIN stock_relationship on (nd_experiment_stock.stock_id = stock_relationship.subject_id) AND stock_relationship.object_id = ?
288 JOIN stock on (stock_relationship.subject_id = stock.stock_id)
289 WHERE nd_experiment_project.project_id= ? ";
291 my $h = $dbh->prepare($q);
292 $h->execute($field_layout_typeid, $cross_accession_id, $trial_id, );
295 while(my ($plot_id, $plot_name) = $h->fetchrow_array()){
297 push @plots, [$plot_id, $plot_name];
299 #print STDERR Dumper \@plots;
300 $c->stash->{rest
} = {data
=>\
@plots};