Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / CXGN / Genotype / CreatePlateOrder.pm
blob3b0ab58752cbb3c8db665104833153a1e0739c69
1 package CXGN::Genotype::CreatePlateOrder;
2 =head1 NAME
4 CXGN::Genotype::CreatePlateOrder - an object to submit genotyping plates to facilities
6 =head1 USAGE
8 PLEASE BE AWARE THAT THE DEFAULT OPTIONS FOR genotypeprop_hash_select, protocolprop_top_key_select, protocolprop_marker_hash_select ARE PRONE TO EXCEEDING THE MEMORY LIMITS OF VM. CHECK THE MOOSE ATTRIBUTES BELOW TO SEE THE DEFAULTS, AND ADJUST YOUR MOOSE INSTANTIATION ACCORDINGLY
10 my $create_order = CXGN::Genotype::CreatePlateOrder->new({
11 bcs_schema=>$schema,
12 people_schema=>$people_schema,
13 client_id=>$client_id,
14 order_id=>$order_id,
15 extract_dna=>$extract_dna,
16 service_id_list=>$service_id_list,
17 plate_id => $plate_id,
18 });
19 my $errors = $submit_samples->validate();
20 $submit_samples->send();
23 # RECOMMENDED
24 If you just want to send genotyping plates with different volume per well
26 =head1 DESCRIPTION
29 =head1 AUTHORS
31 Mirella Flores <mrf252@cornell.edu>
32 Lukas Mueller <lam87@cornell.edu>
34 =cut
36 use strict;
37 use warnings;
38 use Moose;
39 use Try::Tiny;
40 use Data::Dumper;
41 use SGN::Model::Cvterm;
42 use CXGN::Stock::TissueSample::Search;
43 use JSON;
45 has 'bcs_schema' => (
46 isa => 'Bio::Chado::Schema',
47 is => 'rw',
48 required => 1,
52 has 'client_id' => (
53 is => 'ro',
54 isa => 'Str',
55 required => 1,
59 has 'requeriments' => (
60 isa => 'HashRef|Undef',
61 is => 'rw',
62 required => 0,
65 has 'service_id_list' => (
66 isa => 'ArrayRef[Str]|Undef',
67 is => 'ro',
68 required => 1,
71 has 'plate_id' => (
72 isa => 'Str',
73 is => 'ro',
74 required => 1,
77 has 'facility_id' => (
78 isa => 'Str',
79 is => 'ro',
80 required => 0,
83 has 'organism_name' => (
84 isa => 'Str',
85 is => 'ro',
86 required => 1,
89 # sub BUILD {
90 # my $self = shift;
91 # }
93 =head2 validate()
95 Function for validating data before sending to facilities
97 my $submit_samples = CXGN::Genotype::CreatePlateOrder->new({
98 bcs_schema=>$schema,
99 etc...
101 my $get_errors = $submit_samples->validate();
103 =cut
105 sub validate {
106 my $self = shift;
107 # my $schema = $self->bcs_schema;
108 # my $dbh = $schema->storage->dbh;
111 =head2 submit()
113 Function for submit genotyping plates to facilities
115 my $create_order = CXGN::Genotype::CreatePlateOrder->new({
116 bcs_schema=>$schema,
117 etc...
119 my $errors = $submit_samples->submit();
121 =cut
123 sub create {
124 my $self = shift;
125 my $c = shift;
127 my $schema= $self->bcs_schema;
129 my $plate_id = $self->plate_id;
130 my $facility_id = $self->facility_id;
131 my $client_id = $self->client_id;
132 my $get_requeriments = $self->requeriments;
133 my $service_id_list = $self->service_id_list;
134 my $requeriments = $get_requeriments ? $get_requeriments : {};
135 my $organism_name = $self->organism_name;
137 my $genotyping_trial;
138 eval {
139 $genotyping_trial = CXGN::Trial->new( { bcs_schema => $schema, trial_id => $plate_id });
142 my $sample_type = $genotyping_trial->get_genotyping_plate_sample_type;
144 my $plate_format = 'PLATE_96' if ($genotyping_trial->get_genotyping_plate_format eq 96);
146 my $samples = _formated_samples($schema,$plate_id,$organism_name);
148 my $plate;
149 push @$plate, {
150 clientPlateBarcode=> $genotyping_trial->get_name(),
151 clientPlateId=> $plate_id,
152 sampleSubmissionFormat=> $plate_format,
153 samples=> $samples
156 my $order = {
157 clientId=>qq|$client_id|,
158 numberOfSamples=>scalar @$samples,
159 plates=>$plate,
160 requiredServiceInfo => $requeriments,
161 sampleType=>$sample_type,
162 serviceIds=>$service_id_list,
165 return $order;
168 sub _formated_samples {
169 my $schema = shift;
170 my $plate_id = shift;
171 my $organism_name = shift;
173 if (!$plate_id || !$organism_name){
174 return
177 my $concent_unit = 'ng';
178 my $volume_unit = 'ul';
180 my $sample_search = CXGN::Stock::TissueSample::Search->new({
181 bcs_schema=>$schema,
182 plate_db_id_list => [$plate_id],
183 order_by => '',
186 my ($results, $total_count) = $sample_search->search();
187 my @samples;
189 foreach my $result (@$results){
190 if ($result->{germplasmName} ne 'BLANK'){
191 push @samples, {
192 clientSampleBarCode=> $result->{sampleName},
193 clientSampleId=> qq|$result->{sampleDbId}|,
194 column=> $result->{col_number} + 0,
195 row=> $result->{row_number},
196 comments=> $result->{notes},
197 concentration=> {
198 units=> $concent_unit,
199 value=> ($result->{concentration} eq 'NA' || $result->{concentration} eq '') ? 0 : $result->{concentration} + 0,
201 tissueType=> $result->{tissue_type},
202 volume=> {
203 units=> $volume_unit,
204 value=> ($result->{volume} eq 'NA' || $result->{volume} eq '') ? 0 : $result->{volume} + 0,
206 well=> $result->{well} ? $result->{well} : $result->{row_number} . $result->{col_number},
207 organismName=> $organism_name,
208 speciesName=> $result->{species} ? $result->{species} : "",
209 taxonomyOntologyReference=> {},
210 tissueTypeOntologyReference=> {},
215 return \@samples;