3 package SGN
::Controller
::AJAX
::Heritability
;
7 use File
::Temp qw
| tempfile
|;
9 use File
::Spec qw
| catfile
|;
10 use File
::Basename qw
| basename
|;
13 use CXGN
::Dataset
::File
;
15 use CXGN
::Page
::UserPrefs
;
16 use CXGN
::Tools
::List qw
/distinct evens/;
17 use CXGN
::Blast
::Parse
;
18 use CXGN
::Blast
::SeqQuery
;
21 BEGIN { extends
'Catalyst::Controller::REST' }
24 default => 'application/json',
26 map => { 'application/json' => 'JSON' },
30 sub shared_phenotypes
: Path
('/ajax/heritability/shared_phenotypes') : {
33 my $dataset_id = $c->req->param('dataset_id');
35 my $exclude_outliers = $c->req->param('dataset_trait_outliers');
37 my $sp_person_id = $c->user() ?
$c->user->get_object()->get_sp_person_id() : undef;
38 my $people_schema = $c->dbic_schema("CXGN::People::Schema", undef, $sp_person_id);
39 my $schema = $c->dbic_schema("Bio::Chado::Schema", "sgn_chado", $sp_person_id);
40 my $ds = CXGN
::Dataset
->new(people_schema
=> $people_schema, schema
=> $schema, sp_dataset_id
=> $dataset_id);
41 my $traits = $ds->retrieve_traits();
43 foreach my $t (@
$traits) {
44 my $tobj = CXGN
::Cvterm
->new({ schema
=>$schema, cvterm_id
=> $t->[0] });
45 push @trait_info, [ $tobj->cvterm_id(), $tobj->name()];
48 $c->tempfiles_subdir("heritability_files");
49 my ($fh, $tempfile) = $c->tempfile(TEMPLATE
=>"heritability_files/trait_XXXXX");
51 my $temppath = $c->config->{basepath
}."/".$tempfile;
52 print STDERR
"***** temppath = $temppath\n";
53 my $ds2 = CXGN
::Dataset
::File
->new(people_schema
=> $people_schema, schema
=> $schema, sp_dataset_id
=> $dataset_id, exclude_dataset_outliers
=> $exclude_outliers, file_name
=> $temppath, quotes
=> 0);
54 my $phenotype_data_ref = $ds2->retrieve_phenotypes();
56 print STDERR Dumper
(@trait_info);
58 options
=> \
@trait_info,
59 tempfile
=> $tempfile."_phenotype.txt",
65 sub extract_trait_data
:Path
('/ajax/heritability/getdata') Args
(0) {
69 my $file = $c->req->param("file");
70 my $trait = $c->req->param("trait");
72 $file = basename
($file);
74 my $temppath = File
::Spec
->catfile($c->config->{basepath
}, "static/documents/tempfiles/heritability_files/".$file);
75 print STDERR Dumper
($temppath);
78 if (! open($F, "<", $temppath)) {
79 $c->stash->{rest
} = { error
=> "Can't find data." };
85 print STDERR Dumper
($header);
86 my @keys = split("\t", $header);
87 print STDERR Dumper
($keys[1]);
88 for(my $n=0; $n <@keys; $n++) {
89 if ($keys[$n] =~ /\|CO\_/) {
90 $keys[$n] =~ s/\|CO\_.*//;
98 my @fields = split "\t";
100 for(my $n=0; $n <@keys; $n++) {
101 if (exists($fields[$n]) && defined($fields[$n])) {
102 $line{$keys[$n]}=$fields[$n];
105 #print STDERR Dumper(\%line);
109 $c->stash->{rest
} = { data
=> \
@data, trait
=> $trait};
112 sub generate_results
: Path
('/ajax/heritability/generate_results') : {
115 my $dataset_id = $c->req->param('dataset_id');
116 my $trait_id = $c->req->param('trait_id');
118 print STDERR
"The dataset is $dataset_id\n";
119 print STDERR
$dataset_id;
120 print STDERR
$trait_id;
123 my $exclude_outliers = $c->req->param('dataset_trait_outliers');
125 $c->tempfiles_subdir("heritability_files");
126 my $heritability_tmp_output = $c->config->{cluster_shared_tempdir
}."/heritability_files";
127 mkdir $heritability_tmp_output if ! -d
$heritability_tmp_output;
128 print STDERR
"heritability_files subdir = $heritability_tmp_output\n";
129 my ($tmp_fh, $tempfile) = tempfile
(
131 DIR
=> $heritability_tmp_output,
134 print STDERR
"TEMPFILE NOW = $tempfile\n";
136 my $pheno_filepath = $tempfile . "_phenotype.txt";
138 my $sp_person_id = $c->user() ?
$c->user->get_object()->get_sp_person_id() : undef;
139 my $people_schema = $c->dbic_schema("CXGN::People::Schema", undef, $sp_person_id);
140 my $schema = $c->dbic_schema("Bio::Chado::Schema", "sgn_chado", $sp_person_id);
142 #my $temppath = $heritability_tmp_output . "/" . $tempfile;
143 my $temppath = $tempfile;
145 my $ds = CXGN
::Dataset
::File
->new(people_schema
=> $people_schema, schema
=> $schema, sp_dataset_id
=> $dataset_id, exclude_dataset_outliers
=> $exclude_outliers, file_name
=> $temppath, quotes
=> 0);
147 my $phenotype_data_ref = $ds->retrieve_phenotypes($pheno_filepath);
151 my $h2File = $tempfile . "_" . "h2File.json";
152 my $h2CsvFile = $tempfile . "_" . "h2CsvFile.csv";
153 my $errorFile = $tempfile . "_" . "error.txt";
156 $trait_id =~ tr/ /./;
157 $trait_id =~ tr/\//./;
159 my $cmd = CXGN
::Tools
::Run
->new({
160 backend
=> $c->config->{backend
},
161 submit_host
=>$c->config->{cluster_host
},
162 temp_base
=> $c->config->{cluster_shared_tempdir
} . "/heritability_files",
163 queue
=> $c->config->{'web_cluster_queue'},
165 # don't block and wait if the cluster looks full
166 max_cluster_jobs
=> 1_000_000_000
,
169 print STDERR Dumper
$pheno_filepath;
174 $c->config->{basepath
} . "/R/heritability/h2_blup_rscript.R",
186 my $figure_path = $c->{basepath
} . "./documents/tempfiles/heritability_files/";
187 copy
($h2File, $figure_path);
188 copy
($h2CsvFile, $figure_path);
191 my $h2Filebasename = basename
($h2File);
192 my $h2File_response = "/documents/tempfiles/heritability_files/" . $h2Filebasename;
194 my $h2CsvFilebasename = basename
($h2CsvFile);
195 my $h2CsvFile_response = "/documents/tempfiles/heritability_files/" . $h2CsvFilebasename;
198 if ( -e
$errorFile ) {
199 open my $fh, '<', $errorFile or die "Can't open error file $!";
200 $errors = do { local $/; <$fh> };
203 $c->stash->{rest
} = {
204 h2Table
=> $h2File_response,
205 dummy_response
=> $dataset_id,
207 h2CsvTable
=> $h2CsvFile_response