1 package CXGN
::Stock
::AddStocks
;
5 CXGN::Stock::AddStocks - a module to add a list of stocks.
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();
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().
21 Jeremy D. Edwards (jde22@cornell.edu)
26 use MooseX
::FollowPBP
;
27 use Moose
::Util
::TypeConstraints
;
29 use CXGN
::People
::Person
;
30 use SGN
::Model
::Cvterm
;
35 isa
=> 'DBIx::Class::Schema',
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' => (
46 isa
=> 'DBIx::Class::Schema',
47 predicate
=> 'has_phenome_schema',
53 my $added = $self->_add_stocks('accession');
59 my $added = $self->_add_stocks('plot');
63 #### Jeremy Edwards needs the ability to create accession groups
66 my $added = $self->_add_stocks('population');
73 my $stock_type = shift;
74 if (!$self->validate_stocks()) {
77 my $schema = $self->get_schema();
78 my $species = $self->get_species();
79 my $stocks_rs = $self->get_stocks();
80 my @stocks = @
$stocks_rs;
82 my $phenome_schema = $self->get_phenome_schema();
84 my $organism = $schema->resultset("Organism::Organism")
88 my $organism_id = $organism->organism_id();
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.
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
105 if ($self->has_population_name()) {
106 $population = $schema->resultset("Stock::Stock")
108 uniquename
=> $self->get_population_name(),
109 name
=> $self->get_population_name(),
110 organism_id
=> $organism_id,
111 type_id
=> $population_cvterm->cvterm_id(),
114 print STDERR
"Could not find population $population\n";
120 foreach my $stock_name (@stocks) {
121 my $stock = $schema->resultset("Stock::Stock")
123 organism_id
=> $organism_id,
125 uniquename
=> $stock_name,
126 type_id
=> $stock_cvterm->cvterm_id,
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(),
136 push (@added_stock_ids, $stock->stock_id());
140 my $transaction_error;
142 $schema->txn_do($coderef);
144 $transaction_error = $_;
146 if ($transaction_error) {
147 print STDERR
"Transaction error storing stocks: $transaction_error\n";
151 foreach my $stock_id (@added_stock_ids) {
152 #add the owner for this stock
153 $phenome_schema->resultset("StockOwner")
155 stock_id
=> $stock_id,
156 sp_person_id
=> $owner_sp_person_id,
163 sub validate_stocks
{
165 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
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")
177 uniquename
=> $stock_name,
179 if ($stock_search->first()) {
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";
193 sub validate_organism
{
195 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
198 my $schema = $self->get_schema();
199 my $species = $self->get_species();
200 my $organism = $schema->resultset("Organism::Organism")
204 if ($organism->organism_id()) {
212 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
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")
222 sp_person_id
=> $owner_sp_person_id,
230 #### Check that accession group exists
231 sub validate_population
{
233 if (!$self->has_schema() || !$self->has_species() || !$self->has_stocks()) {
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")
240 uniquename
=> $self->get_population_name(),
241 type_id
=> $population_cvterm->cvterm_id(),
243 if ($population_search->first()) {