add is_variable accessor.
[sgn.git] / lib / CXGN / Genotype / GenotypingProject.pm
blob01cef6081bafb4b5b4cf81cb5f46c07bb4cab4dc
2 =head1 NAME
4 CXGN::Genotype::GenotypingProject - an object representing a genotyping project in the database
6 =head1 DESCRIPTION
8 my $genotyping_project = CXGN::Genotype::GenotypingProject->new( { schema => $schema, trial_id => 37347 });
11 =head1 AUTHORS
13 Titima Tantikanjana
15 =head1 METHODS
17 =cut
18 package CXGN::Genotype::GenotypingProject;
20 use Moose;
21 use SGN::Model::Cvterm;
22 use Data::Dumper;
23 use JSON;
24 use CXGN::Trial::Search;
25 use Try::Tiny;
26 use CXGN::Trial;
27 use CXGN::Trial::TrialLayout;
29 has 'bcs_schema' => (
30 isa => 'Bio::Chado::Schema',
31 is => 'rw',
32 required => 1,
35 has 'project_id' => (
36 isa => 'Int',
37 is => 'rw',
38 required => 1,
41 has 'project_facility' => (isa => 'Str',
42 is => 'rw',
43 required => 0,
46 has 'project_and_plate_relationship_cvterm_id' => (
47 isa => 'Int',
48 is => 'rw',
51 has 'genotyping_plate_list' => (
52 isa => 'ArrayRef[Int]|Undef',
53 is => 'rw',
56 has 'new_genotyping_plate_list' => (
57 isa => 'ArrayRef[Int]|Undef',
58 is => 'rw',
61 sub BUILD {
63 my $self = shift;
64 my $schema = $self->bcs_schema();
65 my $genotyping_project_id = $self->project_id();
67 my $genotyping_project = CXGN::Trial->new( { bcs_schema => $schema, trial_id => $genotyping_project_id });
68 my $project_facility = $genotyping_project->get_genotyping_facility();
70 my $genotyping_project_relationship_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'genotyping_project_and_plate_relationship', 'project_relationship');
71 my $project_and_plate_relationship_cvterm_id = $genotyping_project_relationship_cvterm->cvterm_id();
72 my $relationships_rs = $schema->resultset("Project::ProjectRelationship")->search ({
73 object_project_id => $genotyping_project_id,
74 type_id => $project_and_plate_relationship_cvterm_id
75 });
77 my @plate_list;
78 if ($relationships_rs) {
79 while (my $each_relationship = $relationships_rs->next()) {
80 push @plate_list, $each_relationship->subject_project_id();
83 $self->project_facility($project_facility);
84 $self->project_and_plate_relationship_cvterm_id($project_and_plate_relationship_cvterm_id);
85 $self->genotyping_plate_list(\@plate_list);
90 sub get_genotyping_plate_ids {
91 my $self = shift;
92 my $plate_list = $self->genotyping_plate_list();
93 return $plate_list;
97 sub validate_relationship {
98 my $self = shift;
99 my $schema = $self->bcs_schema();
100 my $new_plate_ids = $self->new_genotyping_plate_list();
101 my $genotyping_project_id = $self->project_id();
102 my $project_facility = $self->project_facility();
103 my @plate_ids = @$new_plate_ids;
104 my @genotyping_plate_errors;
106 foreach my $plate_id (@plate_ids) {
107 my $genotyping_plate = CXGN::Trial->new( { bcs_schema => $schema, trial_id => $plate_id });
108 my $plate_facility = $genotyping_plate->get_genotyping_facility();
110 if (($plate_facility ne 'None') && ($project_facility ne 'None')) {
111 if ($plate_facility ne $project_facility) {
112 my $genotyping_plate_name = $genotyping_plate->get_name();
113 push @genotyping_plate_errors, $genotyping_plate_name;
118 return {error_messages => \@genotyping_plate_errors}
122 sub set_project_for_genotyping_plate {
123 my $self = shift;
124 my $schema = $self->bcs_schema();
125 my $genotyping_project_id = $self->project_id();
126 my $new_genotyping_plate_list = $self->new_genotyping_plate_list();
127 my @new_genotyping_plates = @$new_genotyping_plate_list;
128 my $transaction_error;
130 my $coderef = sub {
132 foreach my $plate_id (@new_genotyping_plates) {
133 my $relationship_rs = $schema->resultset("Project::ProjectRelationship")->find ({
134 subject_project_id => $plate_id,
135 type_id => $self->project_and_plate_relationship_cvterm_id()
138 if($relationship_rs){
139 print STDERR "UPDATING...."."\n";
140 $relationship_rs->object_project_id($genotyping_project_id);
141 $relationship_rs->update();
142 } else {
143 $relationship_rs = $schema->resultset('Project::ProjectRelationship')->create({
144 object_project_id => $genotyping_project_id,
145 subject_project_id => $plate_id,
146 type_id => $self->project_and_plate_relationship_cvterm_id()
148 $relationship_rs->insert();
153 try {
154 $schema->txn_do($coderef);
155 } catch {
156 $transaction_error = $_;
159 if ($transaction_error) {
160 print STDERR "Transaction error associating genotyping plate: $transaction_error\n";
161 return;
164 return 1;
169 sub get_plate_info {
170 my $self = shift;
171 my $schema = $self->bcs_schema();
172 my $plate_list = $self->genotyping_plate_list();
173 my $number_of_plates = scalar (@$plate_list);
174 my $data;
175 my $total_count;
176 my @all_plates;
177 my $number_of_samples;
178 if ($number_of_plates > 0) {
179 my $trial_search = CXGN::Trial::Search->new({
180 bcs_schema => $schema,
181 trial_design_list => ['genotyping_plate'],
182 trial_id_list => $plate_list
184 ($data, $total_count) = $trial_search->search();
186 foreach my $plate (@$data){
187 my $plate_layout = CXGN::Trial::TrialLayout->new({schema => $schema, trial_id => $plate->{trial_id}, experiment_type => 'genotyping_layout'});
188 my $sample_names = $plate_layout->get_plot_names();
189 my $number_of_samples = '';
190 if ($sample_names){
191 $number_of_samples = scalar(@{$sample_names});
194 push @all_plates, {
195 plate_id => $plate->{trial_id},
196 plate_name => $plate->{trial_name},
197 plate_description => $plate->{description},
198 plate_format => $plate->{genotyping_plate_format},
199 sample_type => $plate->{genotyping_plate_sample_type},
200 number_of_samples => $number_of_samples,
205 return (\@all_plates, $number_of_plates);
210 sub get_associated_protocol {
211 my $self = shift;
212 my $schema = $self->bcs_schema();
213 my $genotyping_project_id = $self->project_id();
215 my $q = "SELECT DISTINCT nd_protocol.nd_protocol_id, nd_protocol.name
216 FROM nd_experiment_project
217 JOIN nd_experiment_genotype ON (nd_experiment_project.nd_experiment_id = nd_experiment_genotype.nd_experiment_id)
218 JOIN nd_experiment_protocol ON (nd_experiment_project.nd_experiment_id = nd_experiment_protocol.nd_experiment_id)
219 JOIN nd_protocol ON (nd_experiment_protocol.nd_protocol_id = nd_protocol.nd_protocol_id)
220 WHERE nd_experiment_project.project_id = ?";
222 my $h = $schema->storage->dbh()->prepare($q);
223 $h->execute($genotyping_project_id);
225 my @associated_protocol = ();
226 while (my ($protocol_id, $protocol_name) = $h->fetchrow_array()){
227 push @associated_protocol, [$protocol_id, $protocol_name]
230 return \@associated_protocol;