seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / TrialComparison.pm
blob2c6c7069b45db2fe453484cc673b098dba315693
2 use strict;
4 package SGN::Controller::AJAX::TrialComparison;
6 use Moose;
7 use Data::Dumper;
8 use File::Temp qw | tempfile |;
9 use File::Slurp;
10 use CXGN::Dataset;
11 use SGN::Model::Cvterm;
12 use CXGN::List;
13 use CXGN::List::Validate;
14 use CXGN::Trial::Download;
15 use CXGN::Phenotypes::PhenotypeMatrix;
16 use CXGN::BreederSearch;
17 use Cwd qw(cwd);
19 BEGIN { extends 'Catalyst::Controller::REST' }
21 __PACKAGE__->config(
22 default => 'application/json',
23 stash_key => 'rest',
24 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
28 has 'schema' => (
29 is => 'rw',
30 isa => 'DBIx::Class::Schema',
31 lazy_build => 1,
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) {
40 my $self = shift;
41 my $c = shift;
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) {
50 my $self = shift;
51 my $c = shift;
53 my $list_id = $c->req->param("list_id");
55 my $user = $c->user();
57 if (!$user) {
58 $c->stash->{rest} = { error => "Must be logged in to use functionality associated with lists." };
59 return;
62 my $user_id = $user->get_object()->get_sp_person_id();
64 print STDERR "USER ID : $user_id\n";
66 if (!$list_id) {
67 $c->stash->{rest} = { error => "Error: No list_id provided." };
68 return;
71 my $cvterm_id = $c->req->param("cvterm_id");
73 if (!$cvterm_id) {
74 $c->stash->{rest} = { error => "Error: No cvterm_id provided." };
75 return;
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);
83 if ($r->{missing}) {
84 $c->stash->{rest} = { error => "Not all trials could be found in the database." };
85 return;
88 my $dbh = $schema->storage()->dbh();
89 my $tl = CXGN::List->new({ dbh => $dbh, list_id => $list_id, owner => $user_id });
91 if (! $tl) {
92 $c->stash->{rest} = { error => "The specified list does not exist, is not owned by you, or is not a trial list" };
93 return;
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." };
104 return;
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 };
112 sub make_csv {
113 my $self = shift;
114 my $c = shift;
115 my $cvterm_id = shift;
116 my @trial_ids = @_;
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'
134 has_header => 0
136 my $error = $download->download();
137 my $errorfile = $temppath.".err";
138 if (-e $errorfile) {
139 my $error = read_file($errorfile);
140 $c->stash->{rest} = { error => $error };
141 return;
143 my $file = $tempfile.""; # convert from object to string
144 return ($tempfile);
147 sub common_traits : Path('/ajax/trial/common_traits') : ActionClass('REST') {}
149 sub common_traits_GET : Args(0) {
150 my $self = shift;
151 my $c = shift;
153 my @trials = $c->req->param("trial_id");
155 $self->get_common_traits($c, @trials);
159 sub get_common_traits {
160 my $self = shift;
161 my $c = shift;
162 my @trials = @_;
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'];
168 my %dataref;
169 my %queryref;
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}}),