2 package SGN
::Controller
::BreedersToolbox
;
6 use CXGN
::Trial
::TrialLayout
;
7 use URI
::FromHash
'uri';
8 use CXGN
::BreedersToolbox
::Projects
;
10 BEGIN { extends
'Catalyst::Controller'; }
13 sub manage_breeding_programs
: Path
("/breeders/manage_programs") :Args
(0) {
19 # redirect to login page
20 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
24 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
26 my $projects = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $schema } );
28 my $breeding_programs = $projects->get_breeding_programs();
30 $c->stash->{breeding_programs
} = $breeding_programs;
32 $c->stash->{template
} = '/breeders_toolbox/breeding_programs.mas';
37 sub manage_trials
: Path
("/breeders/trials") Args
(0) {
43 # redirect to login page
44 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
48 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
50 my $projects = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $schema } );
52 my $breeding_projects = $projects->get_breeding_programs();
54 my %trials_by_breeding_project = ();
56 foreach my $bp (@
$breeding_projects) {
57 $trials_by_breeding_project{$bp->[1]}= $projects->get_trials_by_breeding_program($bp->[0]);
60 $trials_by_breeding_project{'Other'} = $projects->get_trials_by_breeding_program();
62 $c->stash->{locations
} = $self->get_locations($c);
64 $c->stash->{trials_by_breeding_project
} = \
%trials_by_breeding_project; #$self->get_projects($c);
66 $c->stash->{template
} = '/breeders_toolbox/manage_projects.mas';
69 sub manage_accessions
: Path
("/breeders/accessions") Args
(0) {
74 # redirect to login page
76 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
80 $c->stash->{accessions
} = ();
82 $c->stash->{template
} = '/breeders_toolbox/manage_accessions.mas';
87 sub manage_locations
: Path
("/breeders/locations") Args
(0) {
93 # redirect to login page
95 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
99 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
100 my $bp = CXGN
::BreedersToolbox
::Projects
->new( { schema
=>$schema });
101 my $breeding_programs = $bp->get_breeding_programs();
103 foreach my $b (@
$breeding_programs) {
104 $locations->{$b->[1]} = $bp->get_locations_by_breeding_program($b->[0]);
106 $locations->{'Other'} = $bp->get_locations_by_breeding_program();
108 $c->stash->{user_id
} = $c->user()->get_object()->get_sp_person_id();
110 $c->stash->{locations
} = $locations;
112 $c->stash->{template
} = '/breeders_toolbox/manage_locations.mas';
115 sub manage_crosses
: Path
("/breeders/crosses") Args
(0) {
121 # redirect to login page
123 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
126 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
127 my $bp = CXGN
::BreedersToolbox
::Projects
->new( { schema
=>$schema });
128 my $breeding_programs = $bp->get_breeding_programs();
130 $c->stash->{user_id
} = $c->user()->get_object()->get_sp_person_id();
132 $c->stash->{locations
} = $self->get_locations($c);
134 #$c->stash->{projects} = $self->get_projects($c);
136 $c->stash->{programs
} = $breeding_programs;;
138 $c->stash->{roles
} = $c->user()->roles();
140 $c->stash->{cross_populations
} = $self->get_crosses($c);
142 $c->stash->{template
} = '/breeders_toolbox/manage_crosses.mas';
146 sub manage_phenotyping
:Path
("/breeders/phenotyping") Args
(0) {
151 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
155 my $data = $self->get_phenotyping_data($c);
157 $c->stash->{phenotype_files
} = $data->{file_info
};
158 $c->stash->{deleted_phenotype_files
} = $data->{deleted_file_info
};
160 $c->stash->{template
} = '/breeders_toolbox/manage_phenotyping.mas';
165 sub breeder_home
:Path
("/breeders/home") Args
(0) {
166 my ($self , $c) = @_;
171 # redirect to login page
172 $c->res->redirect( uri
( path
=> '/solpeople/login.pl', query
=> { goto_url
=> $c->req->uri->path_query } ) );
176 my $schema = $c->dbic_schema("Bio::Chado::Schema");
178 $c->stash->{projects
} = $self->get_projects($c);
182 my @roles = $c->user->roles();
183 $c->stash->{roles
}=\
@roles;
185 $c->stash->{cross_populations
} = $self->get_crosses($c);
187 $c->stash->{stockrelationships
} = $self->get_stock_relationships($c);
189 my $locations = $self->get_locations($c);
191 # get uploaded phenotype files
194 my $data = $self->get_phenotyping_data($c);
198 $c->stash->{phenotype_files
} = $data->{file_info
};
199 $c->stash->{deleted_phenotype_files
} = $data->{deleted_file_info
};
201 $c->stash->{template
} = '/breeders_toolbox/home.mas';
205 sub breeder_search
: Path
('/breeder_search/') :Args
(0) {
208 $c->stash->{template
} = '/breeders_toolbox/breeder_search.mas';
212 # sub trial_info : Path('/breeders_toolbox/trial') Args(1) {
216 # my $trial_id = shift;
221 # $c->stash->{template} = '/generic_message.mas';
222 # $c->stash->{message} = 'You must be logged in to access this page.';
225 # my $dbh = $c->dbc->dbh();
227 # my $h = $dbh->prepare("SELECT project.name FROM project WHERE project_id=?");
228 # $h->execute($trial_id);
230 # my ($name) = $h->fetchrow_array();
232 # $c->stash->{trial_name} = $name;
234 # $h = $dbh->prepare("SELECT distinct(nd_geolocation.nd_geolocation_id), nd_geolocation.description, count(*) FROM nd_geolocation JOIN nd_experiment USING(nd_geolocation_id) JOIN nd_experiment_project USING (nd_experiment_id) JOIN project USING (project_id) WHERE project_id=? GROUP BY nd_geolocation_id, nd_geolocation.description");
235 # $h->execute($trial_id);
237 # my @location_data = ();
238 # while (my ($id, $desc, $count) = $h->fetchrow_array()) {
239 # push @location_data, [$id, $desc, $count];
242 # $c->stash->{location_data} = \@location_data;
244 # $h = $dbh->prepare("SELECT distinct(cvterm.name), count(*) FROM cvterm JOIN phenotype ON (cvterm_id=cvalue_id) JOIN nd_experiment_phenotype USING(phenotype_id) JOIN nd_experiment_project USING(nd_experiment_id) WHERE project_id=? GROUP BY cvterm.name");
246 # $h->execute($trial_id);
248 # my @phenotype_data;
249 # while (my ($trait, $count) = $h->fetchrow_array()) {
250 # push @phenotype_data, [$trait, $count];
252 # $c->stash->{phenotype_data} = \@phenotype_data;
254 # $h = $dbh->prepare("SELECT distinct(projectprop.value) FROM projectprop WHERE project_id=? AND type_id=(SELECT cvterm_id FROM cvterm WHERE name='project year')");
255 # $h->execute($trial_id);
258 # while (my ($year) = $h->fetchrow_array()) {
259 # push @years, $year;
263 # $c->stash->{years} = \@years;
265 # $c->stash->{plot_data} = [];
267 # $c->stash->{template} = '/breeders_toolbox/trial.mas';
270 sub get_locations
: Private
{
274 my $schema = $c->dbic_schema("Bio::Chado::Schema");
276 my @rows = $schema->resultset('NaturalDiversity::NdGeolocation')->all();
278 my $type_id = $schema->resultset('Cv::Cvterm')->search( { 'name'=>'plot' })->first->cvterm_id;
282 foreach my $row (@rows) {
283 my $plot_count = "SELECT count(*) from stock join cvterm on(type_id=cvterm_id) join nd_experiment_stock using(stock_id) join nd_experiment using(nd_experiment_id) where cvterm.name='plot' and nd_geolocation_id=?"; # and sp_person_id=?";
284 my $sh = $c->dbc->dbh->prepare($plot_count);
285 $sh->execute($row->nd_geolocation_id); #, $c->user->get_object->get_sp_person_id);
287 my ($count) = $sh->fetchrow_array();
289 print STDERR
"PLOTS: $count\n";
293 push @locations, [ $row->nd_geolocation_id,
298 $count, # number of experiments TBD
306 # sub get_projects : Private {
310 # my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
313 # # get breeding programs
320 # foreach my $bp (@bp_rows) {
321 # my @project_rows = $schema->resultset('Project::Project')->search( { }, { join => 'project_relationship', { join => 'project' }}) ;
324 # foreach my $row (@project_rows) {
325 # push @projects, [ $row->project_id, $row->name, $row->description ];
332 sub get_crosses
: Private
{
336 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
340 my $cross_cvterm = $schema->resultset("Cv::Cvterm")->find(
343 my @cross_populations = ();
347 my @cross_population_stocks = $schema->resultset("Stock::Stock")->search(
348 { type_id
=> $cross_cvterm->cvterm_id,
350 foreach my $cross_pop (@cross_population_stocks) {
351 push @cross_populations, [$cross_pop->name,$cross_pop->stock_id];
354 return \
@cross_populations;
358 sub get_stock_relationships
: Private
{
362 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
364 my $stockrel = $schema->resultset("Cv::Cvterm")->create_with(
366 cv
=> 'stock relationship',
373 #my $stockrel_type_id = $schema->resultset('Cv::Cvterm')->search( { 'name'=>'cross' })->first->cvterm_id;
375 my @rows = $schema->resultset('Stock::StockRelationship')->search( {type_id
=> $stockrel->cvterm_id });
377 my @stockrelationships = ();
379 foreach my $row (@rows) {
380 push @stockrelationships, [$row->type_id];
383 push @stockrelationships, ["example"];
385 return \
@stockrelationships;
389 sub get_phenotyping_data
: Private
{
393 my $metadata_schema = $c->dbic_schema("CXGN::Metadata::Schema");
396 my $deleted_file_info = [];
398 my $metadata_rs = $metadata_schema->resultset("MdMetadata")->search( { create_person_id
=> $c->user()->get_object->get_sp_person_id(), obsolete
=> 0 }, { order_by
=> 'create_date' } );
400 while (my $md_row = ($metadata_rs->next())) {
401 my $file_rs = $metadata_schema->resultset("MdFiles")->search( { metadata_id
=> $md_row->metadata_id() } );
403 if (!$md_row->obsolete) {
404 while (my $file_row = $file_rs->next()) {
405 push @
$file_info, { basename
=> $file_row->basename,
406 dirname
=> $file_row->dirname,
407 file_type
=> $file_row->filetype,
408 md5checksum
=> $file_row->md5checksum,
409 create_date
=> $md_row->create_date,
414 while (my $file_row = $file_rs->next()) {
415 push @
$deleted_file_info, { basename
=> $file_row->basename,
416 dirname
=> $file_row->dirname,
417 file_type
=> $file_row->filetype,
418 md5checksum
=> $file_row->md5checksum,
419 create_date
=> $md_row->create_date,
425 my $data = { phenotype_files
=> $file_info,
426 deleted_phenotype_files
=> $deleted_file_info,