Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / lib / CXGN / Population.pm
blobbdfbfd39f5a5e698807dd60fc73cc96ce3bcc7d1
2 package CXGN::Population;
4 use Moose;
5 use SGN::Model::Cvterm;
6 use Data::Dumper;
7 use JSON;
9 extends 'CXGN::Stock';
11 has 'population_stock_id' => (isa => 'Maybe[Int]',
12 is => 'rw',
15 has 'population_name' => (isa => 'Maybe[Str]',
16 is => 'rw',
19 has 'accession_members' => (isa => 'ArrayRef',
20 is => 'rw',
23 has 'stock_relationship_id' => (isa => 'Maybe[Int]',
24 is => 'rw',
29 sub BUILD {
30 my $self = shift;
31 my $args = shift;
33 my $schema = $args->{schema};
34 my $population_id = $args->{population_stock_id};
36 $self->stock_id($population_id);
38 my $accession_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'accession', 'stock_type')->cvterm_id();
39 my $population_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'population', 'stock_type')->cvterm_id();
40 my $member_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'member_of', 'stock_relationship')->cvterm_id();
42 my $row = $schema->resultset("Stock::Stock")->find( { stock_id => $population_id, type_id => $population_cvterm_id });
44 if ($row) {
45 my $name = $row->uniquename();
46 $self->population_name($name);
47 $self->population_stock_id($population_id);
51 my $accession_members = $self->get_accession_members();
52 print STDERR Dumper($accession_members);
54 if ($accession_members) {
55 $self->accession_members($accession_members);
61 sub get_accession_members {
62 my $self = shift;
63 my $schema = $self->schema;
64 my $population_id = $self->population_stock_id;
66 my $member_of_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "member_of", "stock_relationship")->cvterm_id();
67 my $accession_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "accession", "stock_type")->cvterm_id();
69 my $q = "SELECT member.stock_id, member.uniquename
70 FROM stock
71 JOIN stock_relationship ON (stock_relationship.object_id = stock.stock_id) AND stock_relationship.type_id = ?
72 JOIN stock AS member ON (stock_relationship.subject_id = member.stock_id) AND member.type_id = ?
73 WHERE stock.stock_id = ?";
75 my $h = $schema->storage->dbh()->prepare($q);
76 $h->execute($member_of_type_id, $accession_type_id, $population_id);
77 my @accession_members = ();
78 while (my ($stock_id, $stock_name) = $h->fetchrow_array()){
79 push @accession_members, [$stock_id, $stock_name]
82 return \@accession_members;
86 sub delete_population {
87 my $self = shift;
88 my $dbh = $self->schema()->storage()->dbh();
89 my $schema = $self->schema();
90 my $population_id = $self->population_stock_id();
92 eval {
93 $dbh->begin_work();
95 my $population_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "population", "stock_type")->cvterm_id();
96 my $male_parent_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "male_parent", "stock_relationship")->cvterm_id();
98 my $male_parent_rs = $schema->resultset("Stock::StockRelationship")->search({subject_id => $population_id, type_id => $male_parent_type_id});
99 if ($male_parent_rs->count > 0){
100 print STDERR "Population has associated cross or pedigree. Cannot delete.\n";
101 die "Population has associated cross or pedigree: Cannot delete.\n";
104 #checking if the stock id has population stock type
105 my $population_rs = $schema->resultset("Stock::Stock")->find ({stock_id => $population_id, type_id => $population_type_id});
106 if (!$population_rs) {
107 print STDERR "This stock id is not a population. Cannot delete.\n";
108 die "This stock id is not a population. Cannot delete.\n";
111 my $q = "delete from phenome.stock_owner where stock_id = ?";
112 my $h = $dbh->prepare($q);
113 $h->execute($population_id);
115 my $q2 = "delete from stock where stock.stock_id = ? and stock.type_id = ?";
116 my $h2 = $dbh->prepare($q2);
117 $h2->execute($population_id, $population_type_id);
121 if ($@) {
122 print STDERR "An error occurred while deleting population id ".$population_id."$@\n";
123 $dbh->rollback();
124 return $@;
125 } else {
126 $dbh->commit();
127 return 0;
132 sub delete_population_member {
133 my $self = shift;
134 my $dbh = $self->schema()->storage()->dbh();
135 my $schema = $self->schema();
136 my $stock_relationship_id = $self->stock_relationship_id();
138 eval {
139 $dbh->begin_work();
141 my $population_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "population", "stock_type")->cvterm_id();
142 my $male_parent_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "male_parent", "stock_relationship")->cvterm_id();
143 my $member_of_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "member_of", "stock_relationship")->cvterm_id();
145 my $population_id;
146 my $population_member_rs = $schema->resultset("Stock::StockRelationship")->find({stock_relationship_id => $stock_relationship_id, type_id => $member_of_type_id});
147 if (!$population_member_rs) {
148 print STDERR "This accession is not a population member. Cannot delete.\n";
149 die "This accession is not a population memeber. Cannot delete.\n";
150 } else {
151 $population_id = $population_member_rs->object_id();
152 my $population_rs = $schema->resultset("Stock::Stock")->find ({stock_id => $population_id, type_id => $population_type_id});
153 if (!$population_rs) {
154 print STDERR "This stock id is not a population. Cannot delete.\n";
155 die "This stock is not a population. Cannot delete.\n";
156 } else {
157 my $male_parent_rs = $schema->resultset("Stock::StockRelationship")->search({subject_id => $population_id, type_id => $male_parent_type_id});
158 if ($male_parent_rs->count > 0){
159 print STDERR "Population has associated cross or pedigree. Cannot delete population member.\n";
160 die "Population has associated cross or pedigree: Cannot delete population member.\n";
165 $population_member_rs->delete;
168 if ($@) {
169 print STDERR "An error occurred while deleting accession member "."$@\n";
170 $dbh->rollback();
171 return $@;
172 } else {
173 $dbh->commit();
174 return 0;