Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / lib / solGS / queryJobs.pm
blobbd1ea17898f904bfbd1b88db68ff0a7e291333a2
1 package solGS::queryJobs;
4 use Moose;
5 use namespace::autoclean;
7 use CXGN::Tools::Run;
8 use File::Spec::Functions qw / catfile catdir/;
9 use File::Temp qw / tempfile tempdir /;
10 use File::Slurp qw /write_file read_file/;
11 use Try::Tiny;
12 use Storable qw/ nstore retrieve /;
13 use solGS::AnalysisReport;
14 use Carp qw/ carp confess croak /;
16 use SGN::Model::solGS::solGS;
17 use SGN::Controller::solGS::solGS;
18 use SGN::Controller::solGS::List;
19 use Data::Dumper;
20 use Bio::Chado::Schema;
21 use CXGN::People::Schema;
23 with 'MooseX::Getopt';
24 with 'MooseX::Runnable';
27 has 'data_type' => (
28 is => 'ro',
29 isa => 'Str',
30 required => 1,
33 has 'dbname' => (isa => 'Str',
34 is=>'rw',
35 required=> 1
39 has 'dbhost' => (isa => 'Str',
40 is=>'rw',
41 required=> 1
45 has 'dbport' => (isa => 'Int',
46 is=>'rw',
47 default => 5432
51 has 'dbpass' => (isa => 'Str',
52 is=>'rw',
53 required=> 1
57 has 'dbuser' => (isa => 'Str',
58 is=>'rw',
59 required=> 1
63 has 'population_type' => (
64 is => 'ro',
65 isa => 'Str',
66 required => 1,
69 has 'args_file' => (
70 is => 'ro',
71 isa => 'Str',
72 required => 1,
76 has 'check_data_exists' => (
77 is => 'ro',
78 isa => 'Num',
79 required => 0,
83 sub run {
84 my $self = shift;
86 if ($self->population_type =~ /trial/) {
87 if ($self->data_type =~ /phenotype/) {
88 $self->trial_phenotype_data();
89 } elsif ($self->data_type =~ /genotype/) {
90 $self->trial_genotype_data();
92 } elsif ($self->population_type =~ /list/) {
94 if ($self->data_type =~ /phenotype/) {
95 $self->plots_list_phenotype_data();
96 } elsif ($self->data_type =~ /genotype/) {
97 $self->genotypes_list_genotype_data();
99 } elsif ($self->population_type =~ /dataset/) {
101 if ($self->data_type =~ /phenotype/) {
102 $self->plots_list_phenotype_data();
103 } elsif ($self->data_type =~ /genotype/) {
104 $self->dataset_genotype_data();
110 sub get_args {
111 my $self = shift;
112 return retrieve($self->args_file);
116 sub trial_genotype_data {
117 my $self = shift;
119 my $args = $self->get_args();
120 my $geno_file = $args->{genotype_file};
122 my $model = $self->get_model();
124 my $search_obj = $model->genotype_data($args);
125 $self->write_geno_data($search_obj, $geno_file);
130 sub write_geno_data {
131 my ($self, $search_obj, $file) = @_;
133 my $exists = $self->check_data_exists;
134 my $count = 1;
135 my $marker_headers;
137 my $model = $self->get_model();
138 my $add_headers = 1;
139 while (my $geno = $search_obj->get_next_genotype_info())
141 my $geno_data;
143 if ($count == 1)
145 my $geno_hash = $geno->{selected_genotype_hash};
146 $marker_headers = $model->get_dataset_markers($geno_hash);
147 } else {
148 $add_headers = 0;
151 $geno_data = $model->structure_genotype_data($geno, $marker_headers, $add_headers);
152 write_file($file, {append => 1, binmode => ':utf8'}, $$geno_data);
154 $count++;
155 if ($self->check_data_exists)
157 last if $$geno_data;
164 sub trial_phenotype_data {
165 my $self = shift;
167 my $args = $self->get_args();
169 my $pheno_file = $args->{phenotype_file};
170 my $pop_id = $args->{population_id};
171 my $traits_file = $args->{traits_list_file};
172 my $metadata_file = $args->{metadata_file};
174 my $model = $self->get_model();
175 my $pheno_data = $model->phenotype_data($pop_id);
176 my $metadata = $model->trial_metadata();
178 if ($pheno_data)
180 my $pheno_data = SGN::Controller::solGS::solGS->format_phenotype_dataset($pheno_data, $metadata, $traits_file);
181 write_file($pheno_file, {binmode => ':utf8'}, $pheno_data);
184 write_file($metadata_file, {binmode => ':utf8'}, join("\t", @$metadata));
188 sub genotypes_list_genotype_data {
189 my $self = shift;
190 my $genotypes_ids = shift;
192 my $args = $self->get_args();
193 $genotypes_ids = $args->{genotypes_ids} if !$genotypes_ids->[0];
195 my $data_dir = $args->{data_dir};
196 my $geno_file = $args->{genotype_file};
197 my $protocol_id = $args->{genotyping_protocol_id};
199 my $model = $self->get_model();
200 my $search_obj = $model->genotypes_list_genotype_data($genotypes_ids, $protocol_id);
202 ###empty cached geno file first
203 write_file($geno_file);
204 $self->write_geno_data($search_obj, $geno_file);
209 sub plots_list_phenotype_data {
210 my $self= shift;
212 my $args = $self->get_args();
214 my $list_id = $args->{list_id};
215 my $plots_ids = $args->{plots_ids};
216 my $traits_file = $args->{traits_file};
217 #my $data_dir = $args->{data_dir};
218 my $pheno_file = $args->{phenotype_file};
219 my $metadata_file = $args->{metadata_file};
221 my $model = $self->get_model();
222 my $pheno_data = $model->plots_list_phenotype_data($plots_ids);
223 my $metadata = $model->trial_metadata();
225 $pheno_data = SGN::Controller::solGS::solGS->format_phenotype_dataset($pheno_data, $metadata, $traits_file);
227 write_file($pheno_file, {binmode => ':utf8'}, $pheno_data);
228 write_file($metadata_file, {binmode => ':utf8'}, join("\t", @$metadata));
233 sub dataset_genotype_data {
234 my $self = shift;
236 my $args = $self->get_args();
237 my $dataset_id = $args->{dataset_id};
238 my $model = $self->get_model();
240 my $genotypes_ids = $model->get_genotypes_from_dataset ($dataset_id);
241 # my $cnt = @$genotypes_ids;
242 if (@{$genotypes_ids})
244 $self->genotypes_list_genotype_data($genotypes_ids);
246 else
248 my $model = $self->get_model();
249 my $protocol_id = $args->{genotyping_protocol_id};
250 my $search_obj = $model->get_dataset_genotype_data($dataset_id);
251 my $geno_file = $args->{genotype_file};
252 ###empty cached dataset geno file first
253 write_file($geno_file);
254 $self->write_geno_data($model, $search_obj, $geno_file);
260 sub get_model {
261 my $self = shift;
263 my $dbname = $self->dbname;
264 my $dbhost = $self->dbhost;
266 my $dbuser = $self->dbuser;
267 my $dbpass = $self->dbpass;
269 my $dsn = "dbi:Pg:database=$dbname;host=$dbhost";
271 my $bcs_schema = Bio::Chado::Schema->connect($dsn, $dbuser, $dbpass);
272 my $people_schema = CXGN::People::Schema->connect($dsn, $dbuser, $dbpass, { on_connect_do => [ 'SET search_path TO sgn_people, public, sgn' ]});
275 my $model = SGN::Model::solGS::solGS->new({
276 schema => $bcs_schema,
277 people_schema =>$people_schema });
280 return $model;
286 __PACKAGE__->meta->make_immutable;
291 ####
292 1; #
293 ####