minor fixes
[sgn.git] / lib / CXGN / Stock / AddStocks.pm
blob0c7da1e83434e8d1c42fa5925b4d55b99f2b6938
1 package CXGN::Stock::AddStocks;
3 =head1 NAME
5 CXGN::Stock::AddStocks - a module to add a list of stocks.
7 =head1 USAGE
9 my $stock_add = CXGN::Stock::AddStocks->new({ schema => $schema, phenome_schema => $phenome_schema, dbh => $dbh, stocks => \@stocks, species => $species_name, owner_name => $owner_name } );
10 my $validated_stocks = $stock_add->validate_stocks(); #is true when none of the stock names in the array exist in the database.
11 my $validated_owner = $stock_add->validate_owner(); #is true when the owner exists in the database.
12 my $validated_organism = $stock_add->validate_organism(); #is true when the organism name exists in the database.
13 $stock_add->add_accessions();
15 =head1 DESCRIPTION
17 Adds an array of stocks. The stock names must not already exist in the database, and the verify function does this check. This module is intended to be used in independent loading scripts and interactive dialogs. Stock types "accession" and "plot" are supported by the methods add_accessions() and add_plots().
19 =head1 AUTHORS
21 Jeremy D. Edwards (jde22@cornell.edu)
23 =cut
25 use Moose;
26 use MooseX::FollowPBP;
27 use Moose::Util::TypeConstraints;
28 use Try::Tiny;
29 use CXGN::People::Person;
30 use SGN::Model::Cvterm;
33 has 'schema' => (
34 is => 'rw',
35 isa => 'DBIx::Class::Schema',
36 lazy_build => 1,
37 predicate => 'has_schema',
39 has 'stocks' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_stocks');
40 has 'species' => (isa => 'Str', is => 'rw', predicate => 'has_species');
41 has 'owner_name' => (isa => 'Str', is => 'rw', predicate => 'has_owner_name',required => 1,);
42 has 'population_name' => (isa => 'Str', is => 'rw', predicate => 'has_population_name');
43 has 'dbh' => (is => 'rw',predicate => 'has_dbh', required => 1,);
44 has 'phenome_schema' => (
45 is => 'rw',
46 isa => 'DBIx::Class::Schema',
47 predicate => 'has_phenome_schema',
48 required => 1,
51 sub add_accessions {
52 my $self = shift;
53 my $added = $self->_add_stocks('accession');
54 return $added;
57 sub add_plots {
58 my $self = shift;
59 my $added = $self->_add_stocks('plot');
60 return $added;
63 #### Jeremy Edwards needs the ability to create accession groups
64 sub add_population {
65 my $self = shift;
66 my $added = $self->_add_stocks('population');
67 return $added;
69 ####
71 sub _add_stocks {
72 my $self = shift;
73 my $stock_type = shift;
74 if (!$self->validate_stocks()) {
75 return;
77 my $schema = $self->get_schema();
78 my $species = $self->get_species();
79 my $stocks_rs = $self->get_stocks();
80 my @stocks = @$stocks_rs;
81 my @added_stock_ids;
82 my $phenome_schema = $self->get_phenome_schema();
84 my $organism = $schema->resultset("Organism::Organism")
85 ->find({
86 species => $species,
87 } );
88 my $organism_id = $organism->organism_id();
90 #lookup user by name
91 my $owner_name = $self->get_owner_name();;
92 my $dbh = $self->get_dbh();
93 my $owner_sp_person_id = CXGN::People::Person->get_person_by_username($dbh, $owner_name); #add person id as an option.
95 my $coderef = sub {
97 my $stock_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, $stock_type,'stock_type');
99 my $population_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'population','stock_type');
101 my $population_member_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'member_of','stock_relationship');
103 #### assign accessions to populations
104 my $population;
105 if ($self->has_population_name()) {
106 $population = $schema->resultset("Stock::Stock")
107 ->find_or_create({
108 uniquename => $self->get_population_name(),
109 name => $self->get_population_name(),
110 organism_id => $organism_id,
111 type_id => $population_cvterm->cvterm_id(),
113 if (!$population){
114 print STDERR "Could not find population $population\n";
115 return;
118 ####
120 foreach my $stock_name (@stocks) {
121 my $stock = $schema->resultset("Stock::Stock")
122 ->create({
123 organism_id => $organism_id,
124 name => $stock_name,
125 uniquename => $stock_name,
126 type_id => $stock_cvterm->cvterm_id,
127 } );
128 if ($population) {
129 $stock->find_or_create_related('stock_relationship_objects', {
130 type_id => $population_member_cvterm->cvterm_id(),
131 object_id => $population->stock_id(),
132 subject_id => $stock->stock_id(),
133 } );
136 push (@added_stock_ids, $stock->stock_id());
140 my $transaction_error;
141 try {
142 $schema->txn_do($coderef);
143 } catch {
144 $transaction_error = $_;
146 if ($transaction_error) {
147 print STDERR "Transaction error storing stocks: $transaction_error\n";
148 return;
151 foreach my $stock_id (@added_stock_ids) {
152 #add the owner for this stock
153 $phenome_schema->resultset("StockOwner")
154 ->find_or_create({
155 stock_id => $stock_id,
156 sp_person_id => $owner_sp_person_id,
160 return 1;
163 sub validate_stocks {
164 my $self = shift;
165 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
166 return;
168 my $schema = $self->get_schema();
169 my $species = $self->get_species();
170 my $stocks_rs = $self->get_stocks();
171 my @stocks = @$stocks_rs;
173 my $name_conflicts = 0;
174 foreach my $stock_name (@stocks) {
175 my $stock_search = $schema->resultset("Stock::Stock")
176 ->search({
177 uniquename => $stock_name,
178 } );
179 if ($stock_search->first()) {
180 $name_conflicts++;
181 print STDERR "Stock name conflict for: $stock_name\n";
185 if ($name_conflicts > 0) {
186 print STDERR "There were $name_conflicts conflict(s)\n";
187 return;
190 return 1;
193 sub validate_organism {
194 my $self = shift;
195 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
196 return;
198 my $schema = $self->get_schema();
199 my $species = $self->get_species();
200 my $organism = $schema->resultset("Organism::Organism")
201 ->find({
202 species => $species,
203 } );
204 if ($organism->organism_id()) {
205 return 1;
207 return;
210 sub validate_owner {
211 my $self = shift;
212 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
213 return;
215 my $schema = $self->get_schema();
216 my $phenome_schema = $self->get_phenome_schema();
217 my $owner_name = $self->get_owner_name();;
218 my $dbh = $self->get_dbh();
219 my $owner_sp_person_id = CXGN::People::Person->get_person_by_username($dbh, $owner_name);
220 my $owner_search = $phenome_schema->resultset("StockOwner")
221 ->find({
222 sp_person_id => $owner_sp_person_id,
224 if ($owner_search) {
225 return 1;
227 return;
230 #### Check that accession group exists
231 sub validate_population {
232 my $self = shift;
233 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
234 return;
236 my $schema = $self->get_schema();
237 my $population_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'population', 'stock_type');
238 my $population_search = $schema->resultset("Stock::Stock")
239 ->search({
240 uniquename => $self->get_population_name(),
241 type_id => $population_cvterm->cvterm_id(),
242 } );
243 if ($population_search->first()) {
244 return 1;
246 return;
250 #######
252 #######