Merge pull request #5163 from solgenomics/audit-error-checking
[sgn.git] / lib / CXGN / Trial / TrialDesignStore / SamplingTrial.pm
blob133550f5d9373efc6617171333449a6c920e673d
2 package CXGN::Trial::TrialDesignStore::SamplingTrial;
4 use Moose;
5 use Try::Tiny;
7 BEGIN { extends 'CXGN::Trial::TrialDesignStore::AbstractTrial';}
9 sub BUILD {
10 my $self = shift;
11 my $args = shift;
13 print STDERR "BUILD CXGN::Trial::TrialDesignStore::AbstractTrial\n";
14 $self->set_nd_experiment_type_id(SGN::Model::Cvterm->get_cvterm_row($self->get_bcs_schema(), 'sampling_layout', 'experiment_type')->cvterm_id());
15 $self->set_stock_type_id($self->get_tissue_sample_cvterm_id);
16 $self->set_stock_relationship_type_id($self->get_tissue_sample_of_cvterm_id);
18 $self->set_source_stock_types([
19 $self->get_accession_cvterm_id,
20 $self->get_plot_cvterm_id,
21 $self->get_plant_cvterm_id,
22 $self->get_tissue_sample_cvterm_id
23 ]);
25 $self->set_valid_properties([
26 'stock_name',
27 'plot_name',
28 'block_number',
29 'rep_number',
30 'row_number',
31 'col_number',
32 'plot_number',
33 'extraction',
34 'dna_person',
35 'concentration',
36 'volume',
37 'tissue_type',
38 'notes',
39 'acquisition_date',
40 'ncbi_taxonomy_id'
41 ]);
45 sub validate_design {
46 my $self = shift;
48 print STDERR "validating design\n";
50 my $chado_schema = $self->get_bcs_schema();
51 my $design_type = $self->get_design_type();
52 my %design = %{$self->get_design};
53 my $error = '';
55 if ($design_type ne 'sampling_trial') {
56 $error .= "design_type not equal to 'sampling_trial'";
57 return $error;
60 my @valid_properties = @{$self->get_valid_properties()};
62 my %allowed_properties = map {$_ => 1} @valid_properties;
64 my %seen_stock_names;
65 my %seen_accession_names;
66 foreach my $stock (keys %design){
67 foreach my $property (keys %{$design{$stock}}){
68 if (!exists($allowed_properties{$property})) {
69 $error .= "Property: $property not allowed! ";
71 if ($property eq 'stock_name') {
72 my $stock_name = $design{$stock}->{$property};
73 $seen_accession_names{$stock_name}++;
75 if ($property eq 'plot_name') {
76 my $plot_name = $design{$stock}->{$property};
77 $seen_stock_names{$plot_name}++;
82 my @stock_names = keys %seen_stock_names;
83 my @accession_names = keys %seen_accession_names;
84 if(scalar(@stock_names)<1){
85 $error .= "You cannot create a trial with less than one sample.";
87 if(scalar(@accession_names)<1){
88 $error .= "You cannot create a trial with less than one accession.";
90 my $subplot_type_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'subplot', 'stock_type')->cvterm_id();
91 my $accession_type_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'accession', 'stock_type')->cvterm_id();
92 my $plot_type_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'plot', 'stock_type')->cvterm_id();
93 my $plant_type_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'plant', 'stock_type')->cvterm_id();
94 my $tissue_type_id = SGN::Model::Cvterm->get_cvterm_row($chado_schema, 'tissue_sample', 'stock_type')->cvterm_id();
95 my $stocks = $chado_schema->resultset('Stock::Stock')->search({
96 type_id=>$tissue_type_id,
97 uniquename=>{-in=>\@stock_names}
98 });
99 while (my $s = $stocks->next()) {
100 $error .= "Name $s->uniquename already exists in the database.";
103 my @source_stock_types = ($accession_type_id, $plot_type_id, $plant_type_id, $tissue_type_id);
105 my $rs = $chado_schema->resultset('Stock::Stock')->search({
106 'is_obsolete' => { '!=' => 't' },
107 'type_id' => {-in=>\@source_stock_types},
108 'uniquename' => {-in=>\@accession_names}
110 my %found_data;
111 while (my $s = $rs->next()) {
112 $found_data{$s->uniquename} = 1;
114 foreach (@accession_names){
115 if (!$found_data{$_}){
116 $error .= "The following name is not in the database: $_ .";
120 return $error;