4 package SGN
::Controller
::AJAX
::TrialComparison
;
8 use File
::Temp qw
| tempfile
|;
11 use SGN
::Model
::Cvterm
;
13 use CXGN
::List
::Validate
;
14 use CXGN
::Trial
::Download
;
15 use CXGN
::Phenotypes
::PhenotypeMatrix
;
16 use CXGN
::BreederSearch
;
19 BEGIN { extends
'Catalyst::Controller::REST' }
22 default => 'application/json',
24 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
30 isa
=> 'DBIx::Class::Schema',
35 # /ajax/trial/compare?trial_id=345&trial_id=4848&trial_id=38484&cvterm_id=84848
37 sub compare_trail_list_data_test
: Path
('/ajax/trial/compare_list_data_test') : ActionClass
('REST') {}
39 sub compare_trail_list_data_test_GET
: Args
(0) {
42 my $result = `python bin/test/TrialComparisonFakeDataGen.py`;#`python TrialComparison_test.py`;
43 my $cvterm_id = "TEST_TEST";
44 $c->stash->{rest
} = { csv
=> $result, cvterm_id
=> $cvterm_id};
47 sub compare_trail_list_data
: Path
('/ajax/trial/compare_list_data') : ActionClass
('REST') {}
49 sub compare_trail_list_data_GET
: Args
(0) {
53 my $list_id = $c->req->param("list_id");
55 my $user = $c->user();
58 $c->stash->{rest
} = { error
=> "Must be logged in to use functionality associated with lists." };
62 my $user_id = $user->get_object()->get_sp_person_id();
64 print STDERR
"USER ID : $user_id\n";
67 $c->stash->{rest
} = { error
=> "Error: No list_id provided." };
71 my $cvterm_id = $c->req->param("cvterm_id");
74 $c->stash->{rest
} = { error
=> "Error: No cvterm_id provided." };
78 my $schema = $c->dbic_schema("Bio::Chado::Schema");
80 my $v = CXGN
::List
::Validate
->new();
81 my $r = $v->validate($schema, "trial", $list_id);
84 $c->stash->{rest
} = { error
=> "Not all trials could be found in the database." };
88 my $dbh = $schema->storage()->dbh();
89 my $tl = CXGN
::List
->new({ dbh
=> $dbh, list_id
=> $list_id, owner
=> $user_id });
92 $c->stash->{rest
} = { error
=> "The specified list does not exist, is not owned by you, or is not a trial list" };
96 my $trials = $tl->elements();
98 my $trial_id_rs = $schema->resultset("Project::Project")->search( { name
=> { in => [ @
$trials ]} });
100 my @trial_ids = map { $_->project_id() } $trial_id_rs->all();
102 if (@trial_ids < 2) {
103 $c->stash->{rest
} = { error
=> "One or both trials are not found in the database. Please try again." };
107 my $csv_loc = $self->make_csv($c, $cvterm_id, @trial_ids);
108 my $csv = read_file
($c->config->{basepath
}."/".$csv_loc);
109 $c->stash->{rest
} = { csv
=> $csv, cvterm_id
=> $cvterm_id };
115 my $cvterm_id = shift;
118 my $schema = $c->dbic_schema("Bio::Chado::Schema");
120 $c->tempfiles_subdir("compare_trials");
122 my ($fh, $tempfile) = $c->tempfile(TEMPLATE
=>"compare_trials/trial_phenotypes_download_XXXXX");
124 my $temppath = $c->config->{basepath
}."/".$tempfile;
126 my $download = CXGN
::Trial
::Download
->new({
127 bcs_schema
=> $c->dbic_schema("Bio::Chado::Schema"),
128 trial_list
=> \
@trial_ids,
129 trait_list
=> [ $cvterm_id ],
130 filename
=> $temppath,
131 format
=> 'TrialPhenotypeCSV',
132 data_level
=> 'plot', #'plot' or 'plant' or 'all'. CXGN::Dataset would default to 'plot'
133 search_type
=> 'complete', #'complete' or 'fast'. CXGN::Dataset would default to 'fast'
136 my $error = $download->download();
137 my $errorfile = $temppath.".err";
139 my $error = read_file
($errorfile);
140 $c->stash->{rest
} = { error
=> $error };
143 my $file = $tempfile.""; # convert from object to string
147 sub common_traits
: Path
('/ajax/trial/common_traits') : ActionClass
('REST') {}
149 sub common_traits_GET
: Args
(0) {
153 my @trials = $c->req->param("trial_id");
155 $self->get_common_traits($c, @trials);
159 sub get_common_traits
{
163 my $schema = $c->dbic_schema("Bio::Chado::Schema");
164 print STDERR
'@trials: '.Dumper
(@trials);
165 my $trials_string = "\'".join( "\',\'",@trials)."\'";
166 print STDERR
'$trials_string: '.Dumper
($trials_string);
167 my @criteria = ['trials','traits'];
170 $dataref{traits
}->{trials
} = $trials_string;
171 $queryref{traits
}->{trials
} = 1;
172 print STDERR
'data: '.Dumper
(\
%dataref);
173 print STDERR
'query: '.Dumper
(\
%queryref);
174 my $breedersearch = CXGN
::BreederSearch
->new({"dbh"=>$c->dbc->dbh});
175 my $results_ref = $breedersearch->metadata_query(@criteria, \
%dataref, \
%queryref);
176 print STDERR
"Results: \n";
177 #print STDERR Dumper($results_ref);
178 $c->stash->{rest
} = {
179 options
=> $results_ref->{results
},
180 list_trial_count
=> scalar(@trials),
181 common_trait_count
=> scalar(@
{$results_ref->{results
}}),