2 package CXGN
::Population
;
5 use SGN
::Model
::Cvterm
;
11 has
'population_stock_id' => (isa
=> 'Maybe[Int]',
15 has
'population_name' => (isa
=> 'Maybe[Str]',
19 has
'accession_members' => (isa
=> 'ArrayRef',
23 has
'stock_relationship_id' => (isa
=> 'Maybe[Int]',
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 });
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
{
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
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
{
88 my $dbh = $self->schema()->storage()->dbh();
89 my $schema = $self->schema();
90 my $population_id = $self->population_stock_id();
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);
122 print STDERR
"An error occurred while deleting population id ".$population_id."$@\n";
132 sub delete_population_member
{
134 my $dbh = $self->schema()->storage()->dbh();
135 my $schema = $self->schema();
136 my $stock_relationship_id = $self->stock_relationship_id();
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();
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";
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";
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;
169 print STDERR
"An error occurred while deleting accession member "."$@\n";