3 package SGN
::Controller
::AJAX
::SpatialModel
;
7 use File
::Temp qw
| tempfile
|;
9 use File
::Spec qw
| catfile
|;
10 use File
::Basename qw
| basename
|;
12 use List
::Util qw
| any
|;
14 use CXGN
::Dataset
::File
;
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
;
23 BEGIN { extends
'Catalyst::Controller::REST' }
26 default => 'application/json',
28 map => { 'application/json' => 'JSON' },
32 sub shared_phenotypes
: Path
('/ajax/spatial_model/shared_phenotypes') Args
(0) {
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);
51 tempfile
=> $tempfile."_phenotype.txt",
52 # tempfile => $file_response,
58 sub extract_trait_data
:Path
('/ajax/spatial_model/getdata') Args
(0) {
62 my $file = $c->req->param("file");
63 my $trait = $c->req->param("trait");
65 $file = basename
($file);
68 my $temppath = File
::Spec
->catfile($c->config->{basepath
}, "static/documents/tempfiles/spatial_model_files", $file);
69 print STDERR Dumper
($temppath);
72 if (! open($F, "<", $temppath)) {
73 $c->stash->{rest
} = { error
=> "Can't find data." };
77 $c->stash->{rest
} = { data
=> \
@data, trait
=> $trait};
80 sub generate_results
: Path
('/ajax/spatial_model/generate_results') Args
(1) {
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";
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;
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'},
149 # don't block and wait if the cluster looks full
150 max_cluster_jobs
=> 1_000_000_000
,
155 $c->config->{basepath
} . "/R/spatial_modeling.R",
156 $pheno_filepath.".clean",
161 while ($cmd->alive) {
165 # my $figure_path = $c->config->{basepath} . "/static/documents/tempfiles/stability_files/";
169 open(my $F, "<", $pheno_filepath.".clean.out") || die "Can't open result file $pheno_filepath".".clean.out";
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
} = {
189 download_link
=> $download_link,
194 sub make_R_trait_name
{