Added eval; site now shows clean dataset missing message instead of server error...
[sgn.git] / lib / SGN / Controller / AJAX / SpatialModel.pm
blobb952159154d267893a87a74214d8a45631130815
1 use strict;
3 package SGN::Controller::AJAX::SpatialModel;
5 use Moose;
6 use Data::Dumper;
7 use File::Temp qw | tempfile |;
8 use File::Slurp;
9 use File::Spec qw | catfile|;
10 use File::Basename qw | basename |;
11 use File::Copy;
12 use List::Util qw | any |;
13 use CXGN::Dataset;
14 use CXGN::Dataset::File;
15 use CXGN::Tools::Run;
16 use CXGN::Page::UserPrefs;
17 use CXGN::Tools::List qw/distinct evens/;
18 use CXGN::Blast::Parse;
19 use CXGN::Blast::SeqQuery;
20 use SGN::Model::Cvterm;
21 use Cwd qw(cwd);
23 BEGIN { extends 'Catalyst::Controller::REST' }
25 __PACKAGE__->config(
26 default => 'application/json',
27 stash_key => 'rest',
28 map => { 'application/json' => 'JSON' },
32 sub shared_phenotypes: Path('/ajax/spatial_model/shared_phenotypes') Args(0) {
33 my $self = shift;
34 my $c = shift;
35 my $dataset_id = $c->req->param('dataset_id');
36 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
37 my $people_schema = $c->dbic_schema("CXGN::People::Schema", undef, $sp_person_id);
38 my $schema = $c->dbic_schema("Bio::Chado::Schema", "sgn_chado", $sp_person_id);
39 my $ds = CXGN::Dataset->new(people_schema => $people_schema, schema => $schema, sp_dataset_id => $dataset_id);
40 my $traits = $ds->retrieve_traits();
42 $c->tempfiles_subdir("spatial_model_files");
43 my ($fh, $tempfile) = $c->tempfile(TEMPLATE=>"spatial_model_files/trait_XXXXX");
44 my $temppath = $c->config->{basepath}."/".$tempfile;
45 my $ds2 = CXGN::Dataset::File->new(people_schema => $people_schema, schema => $schema, sp_dataset_id => $dataset_id, file_name => $temppath, quotes => 0);
46 my $phenotype_data_ref = $ds2->retrieve_phenotypes();
48 print STDERR Dumper($traits);
49 $c->stash->{rest} = {
50 options => $traits,
51 tempfile => $tempfile."_phenotype.txt",
52 # tempfile => $file_response,
58 sub extract_trait_data :Path('/ajax/spatial_model/getdata') Args(0) {
59 my $self = shift;
60 my $c = shift;
62 my $file = $c->req->param("file");
63 my $trait = $c->req->param("trait");
65 $file = basename($file);
66 my @data;
68 my $temppath = File::Spec->catfile($c->config->{basepath}, "static/documents/tempfiles/spatial_model_files", $file);
69 print STDERR Dumper($temppath);
71 my $F;
72 if (! open($F, "<", $temppath)) {
73 $c->stash->{rest} = { error => "Can't find data." };
74 return;
77 $c->stash->{rest} = { data => \@data, trait => $trait};
80 sub generate_results: Path('/ajax/spatial_model/generate_results') Args(1) {
81 my $self = shift;
82 my $c = shift;
83 my $trial_id = shift;
85 print STDERR "TRIAL_ID: $trial_id\n";
87 $c->tempfiles_subdir("spatial_model_files");
88 my $spatial_model_tmp_output = $c->config->{cluster_shared_tempdir}."/spatial_model_files";
89 mkdir $spatial_model_tmp_output if ! -d $spatial_model_tmp_output;
90 my ($tmp_fh, $tempfile) = tempfile(
91 "spatial_model_download_XXXXX",
92 DIR=> $spatial_model_tmp_output,
94 my $temppath = $c->config->{basepath}."/".$tempfile;
96 my $pheno_filepath = $temppath . "_phenotype.txt";
98 print STDERR "pheno_filepath: $pheno_filepath\n";
100 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
101 my $people_schema = $c->dbic_schema("CXGN::People::Schema", undef, $sp_person_id);
102 my $schema = $c->dbic_schema("Bio::Chado::Schema", "sgn_chado", $sp_person_id);
107 my $ds = CXGN::Dataset::File->new(people_schema => $people_schema, schema => $schema, file_name => $temppath, quotes=>0);
108 $ds -> trials([$trial_id]);
109 print STDERR "DS: $ds\n";
110 open(my $PF, "<", $pheno_filepath) || die "Can't open pheno file $pheno_filepath";
111 open(my $CLEAN, ">", $pheno_filepath.".clean") || die "Can't open pheno_filepath clean for writing";
113 my $header = <$PF>;
114 chomp($header);
116 my @fields = split /\t/, $header;
118 my @file_traits = @fields[ 39 .. @fields-1 ];
119 my @other_headers = @fields[ 0 .. 38 ];
123 print STDERR "FIELDS: ".Dumper(\@file_traits);
125 foreach my $t (@file_traits) {
126 $t = make_R_trait_name($t);
129 my $si_traits = join(",", @file_traits);
131 print STDERR "FILE TRAITS: ".Dumper(\@file_traits);
133 my @new_header = (@other_headers, @file_traits);
134 print $CLEAN join("\t", @new_header)."\n";
136 my $last_index = scalar(@new_header)-1;
138 #while(<$PF>) {
139 #chomp;
140 #my @f = split /\t/;
143 my $cmd = CXGN::Tools::Run->new({
144 backend => $c->config->{backend},
145 submit_host=>$c->config->{cluster_host},
146 temp_base => $c->config->{cluster_shared_tempdir} . "/spatial_model_files",
147 queue => $c->config->{'web_cluster_queue'},
148 do_cleanup => 0,
149 # don't block and wait if the cluster looks full
150 max_cluster_jobs => 1_000_000_000,
153 $cmd->run_cluster(
154 "Rscript ",
155 $c->config->{basepath} . "/R/spatial_modeling.R",
156 $pheno_filepath.".clean",
157 "'".$si_traits."'",
161 while ($cmd->alive) {
162 sleep(1);
165 # my $figure_path = $c->config->{basepath} . "/static/documents/tempfiles/stability_files/";
167 my @data;
169 open(my $F, "<", $pheno_filepath.".clean.out") || die "Can't open result file $pheno_filepath".".clean.out";
170 my $header = <$F>;
171 while (<$F>) {
172 chomp;
173 my @fields = split /\,/;
174 foreach my $f (@fields) { $f =~ s/\"//g; }
175 push @data, \@fields;
178 print STDERR "FORMATTED DATA: ".Dumper(\@data);
180 my $basename = basename($pheno_filepath.".clean.out");
182 copy($pheno_filepath.".clean.out", $c->config->{basepath}."/static/documents/tempfiles/spatial_model_files/".$basename);
184 my $download_url = '/documents/tempfiles/spatial_model_files/'.$basename;
185 my $download_link = "<a href=\"$download_url\">Download Results</a>";
187 $c->stash->{rest} = {
188 data => \@data,
189 download_link => $download_link,
194 sub make_R_trait_name {
195 my $trait = shift;
196 $trait =~ s/\s/\_/g;
197 $trait =~ s/\//\_/g;
198 $trait =~ tr/ /./;
199 $trait =~ tr/\//./;
200 $trait =~ s/\:/\_/g;
201 $trait =~ s/\|/\_/g;
202 $trait =~ s/\-/\_/g;
204 return $trait;