do not attempt get the person object when no one is logged in...
[sgn.git] / lib / CXGN / Stock / Accession.pm
blobed2590b7732108c4ac7f4d1530482b8059b87d2b
2 =head1 NAME
4 CXGN::Stock::Accession - a class to represent accessions in the database
6 =head1 DESCRIPTION
9 CXGN::Stock::Accession inherits from CXGN::Stock. The required fields are:
11 uniquename
13 Code structure copied from CXGN::Stock::Seedlot, with inheritance from CXGN::Stock
15 =head1 AUTHOR
18 =head1 ACCESSORS & METHODS
20 =cut
22 package CXGN::Stock::Accession;
24 use Moose;
25 use JSON;
27 extends 'CXGN::Stock';
29 use Data::Dumper;
30 use CXGN::BreedersToolbox::Projects;
31 use SGN::Model::Cvterm;
33 has 'owner_sp_person' => (
34 isa => 'ArrayRef',
35 is => 'rw',
36 lazy => 1,
37 builder => '_build_owner_sp_person',
40 has 'main_production_site_url' => (
41 isa => 'Str',
42 is => 'rw',
45 has 'accessionNumber' => (
46 isa => 'Maybe[Str]',
47 is => 'rw',
48 lazy => 1,
49 builder => '_retrieve_accessionNumber',
52 has 'germplasmPUI' => (
53 isa => 'Maybe[Str]',
54 is => 'rw',
55 lazy => 1,
56 builder => '_retrieve_germplasmPUI',
59 has 'pedigree' => (
60 isa => 'Maybe[Str]',
61 is => 'rw',
64 has 'mother_accession' => (
65 isa => 'Maybe[Str]',
66 is => 'rw',
69 has 'father_accession' => (
70 isa => 'Maybe[Str]',
71 is => 'rw',
74 has 'additional_info' => (
75 isa => 'Maybe[HashRef]',
76 is => 'rw'
79 has 'germplasmSeedSource' => (
80 isa => 'Maybe[Str]',
81 is => 'rw',
82 lazy => 1,
83 builder => '_retrieve_germplasmSeedSource',
86 has 'synonyms' => (
87 isa => 'Maybe[ArrayRef[Str]]',
88 is => 'rw',
89 lazy => 1,
90 builder => '_retrieve_synonyms',
93 has 'instituteCode' => (
94 isa => 'Maybe[Str]',
95 is => 'rw',
96 lazy => 1,
97 builder => '_retrieve_instituteCode',
100 has 'instituteName' => (
101 isa => 'Maybe[Str]',
102 is => 'rw',
103 lazy => 1,
104 builder => '_retrieve_instituteName',
107 has 'biologicalStatusOfAccessionCode' => (
108 isa => 'Maybe[Str]',
109 is => 'rw',
110 lazy => 1,
111 builder => '_retrieve_biologicalStatusOfAccessionCode',
114 has 'countryOfOriginCode' => (
115 isa => 'Maybe[Str]',
116 is => 'rw',
117 lazy => 1,
118 builder => '_retrieve_countryOfOriginCode',
121 has 'typeOfGermplasmStorageCode' => (
122 isa => 'Maybe[Str]',
123 is => 'rw',
124 lazy => 1,
125 builder => '_retrieve_typeOfGermplasmStorageCode',
128 has 'donors' => (
129 isa => 'Maybe[ArrayRef[HashRef]]',
130 is => 'rw',
131 lazy => 1,
132 builder => '_retrieve_donors',
135 has 'acquisitionDate' => (
136 isa => 'Maybe[Str]',
137 is => 'rw',
138 lazy => 1,
139 builder => '_retrieve_acquisitionDate',
142 has 'entryNumber' => (
143 isa => 'Maybe[Str]',
144 is => 'rw',
145 lazy => 1,
146 builder => '_retrieve_entryNumber',
149 has 'variety' => (
150 isa => 'Maybe[Str]',
151 is => 'rw',
152 lazy => 1,
153 builder => '_retrieve_variety',
156 has 'state' => (
157 isa => 'Maybe[Str]',
158 is => 'rw',
159 lazy => 1,
160 builder => '_retrieve_state',
163 has 'notes' => (
164 isa => 'Maybe[Str]',
165 is => 'rw',
166 lazy => 1,
167 builder => '_retrieve_notes',
170 has 'locationCode' => (
171 isa => 'Maybe[Str]',
172 is => 'rw',
173 lazy => 1,
174 builder => '_retrieve_location_code',
177 has 'ploidyLevel' => (
178 isa => 'Maybe[Str]',
179 is => 'rw',
180 lazy => 1,
181 builder => '_retrieve_ploidy_level',
184 has 'genomeStructure' => (
185 isa => 'Maybe[Str]',
186 is => 'rw',
187 lazy => 1,
188 builder => '_retrieve_genome_structure',
191 has 'transgenic' => (
192 isa => 'Maybe[Str]',
193 is => 'rw',
194 lazy => 1,
195 builder => '_retrieve_transgenic',
198 has 'introgression_parent' => (
199 isa => 'Maybe[Str]',
200 is => 'rw',
201 lazy => 1,
202 builder => '_retrieve_introgression_parent',
205 has 'introgression_backcross_parent' => (
206 isa => 'Maybe[Str]',
207 is => 'rw',
208 lazy => 1,
209 builder => '_retrieve_introgression_backcross_parent',
212 has 'introgression_map_version' => (
213 isa => 'Maybe[Str]',
214 is => 'rw',
215 lazy => 1,
216 builder => '_retrieve_introgression_map_version',
219 has 'introgression_chromosome' => (
220 isa => 'Maybe[Str]',
221 is => 'rw',
222 lazy => 1,
223 builder => '_retrieve_introgression_chromosome',
226 has 'introgression_start_position_bp' => (
227 isa => 'Maybe[Str]',
228 is => 'rw',
229 lazy => 1,
230 builder => '_retrieve_introgression_start_position_bp',
233 has 'introgression_end_position_bp' => (
234 isa => 'Maybe[Str]',
235 is => 'rw',
236 lazy => 1,
237 builder => '_retrieve_introgression_end_position_bp',
240 has 'other_editable_stock_props' => (
241 isa => 'Maybe[HashRef]',
242 is => 'rw'
245 sub BUILD {
246 my $self = shift;
250 sub _retrieve_germplasmPUI {
251 my $self = shift;
252 $self->germplasmPUI($self->_retrieve_stockprop('PUI'));
255 sub _retrieve_accessionNumber {
256 my $self = shift;
257 $self->accessionNumber($self->_retrieve_stockprop('accession number'));
260 sub _retrieve_germplasmSeedSource {
261 my $self = shift;
262 $self->germplasmSeedSource($self->_retrieve_stockprop('seed source'));
265 sub _retrieve_synonyms {
266 my $self = shift;
267 my @synonyms = $self->_retrieve_stockprop('stock_synonym') ? split ',', $self->_retrieve_stockprop('stock_synonym') : ();
268 $self->synonyms(\@synonyms);
271 sub _retrieve_instituteCode {
272 my $self = shift;
273 $self->instituteCode($self->_retrieve_stockprop('institute code'));
276 sub _retrieve_instituteName {
277 my $self = shift;
278 $self->instituteName($self->_retrieve_stockprop('institute name'));
281 sub _retrieve_entryNumber {
282 my $self = shift;
283 $self->entryNumber($self->_retrieve_stockprop('entry number'));
286 sub _retrieve_variety {
287 my $self = shift;
288 $self->variety($self->_retrieve_stockprop('variety'));
291 sub _retrieve_state {
292 my $self = shift;
293 $self->state($self->_retrieve_stockprop('state'));
296 sub _retrieve_biologicalStatusOfAccessionCode {
297 my $self = shift;
298 $self->biologicalStatusOfAccessionCode($self->_retrieve_stockprop('biological status of accession code'));
301 sub _retrieve_countryOfOriginCode {
302 my $self = shift;
303 $self->countryOfOriginCode($self->_retrieve_stockprop('country of origin'));
306 sub _retrieve_typeOfGermplasmStorageCode {
307 my $self = shift;
308 $self->typeOfGermplasmStorageCode($self->_retrieve_stockprop('type of germplasm storage code'));
311 sub _retrieve_acquisitionDate {
312 my $self = shift;
313 $self->acquisitionDate($self->_retrieve_stockprop('acquisition date'));
316 sub _retrieve_donors {
317 my $self = shift;
318 my @donor_accessions = $self->_retrieve_stockprop('donor') ? split ',', $self->_retrieve_stockprop('donor') : ();
319 my @donor_institutes = $self->_retrieve_stockprop('donor institute') ? split ',', $self->_retrieve_stockprop('donor institute') : ();
320 my @donor_puis = $self->_retrieve_stockprop('donor PUI') ? split ',', $self->_retrieve_stockprop('donor PUI') : ();
321 my @donor_array;
322 if (scalar(@donor_accessions)>0 && scalar(@donor_institutes)>0 && scalar(@donor_puis)>0 && scalar(@donor_accessions) == scalar(@donor_institutes) && scalar(@donor_accessions) == scalar(@donor_puis)){
323 for (0 .. scalar(@donor_accessions)-1){
324 push @donor_array, {
325 'donorGermplasmName'=>$donor_accessions[$_],
326 'donorAccessionNumber'=>$donor_accessions[$_],
327 'donorInstituteCode'=>$donor_institutes[$_],
328 'germplasmPUI'=>$donor_puis[$_]
332 $self->donors(\@donor_array);
335 sub _retrieve_notes {
336 my $self = shift;
337 $self->notes($self->_retrieve_stockprop('notes'));
340 sub _retrieve_location_code {
341 my $self = shift;
342 $self->locationCode($self->_retrieve_stockprop('location_code'));
345 sub _retrieve_ploidy_level {
346 my $self = shift;
347 $self->ploidyLevel($self->_retrieve_stockprop('ploidy_level'));
350 sub _retrieve_genome_structure {
351 my $self = shift;
352 $self->genomeStructure($self->_retrieve_stockprop('genome_structure'));
355 sub _retrieve_transgenic {
356 my $self = shift;
357 $self->transgenic($self->_retrieve_stockprop('transgenic'));
360 sub _retrieve_introgression_parent {
361 my $self = shift;
362 $self->introgression_parent($self->_retrieve_stockprop('introgression_parent'));
365 sub _retrieve_introgression_backcross_parent {
366 my $self = shift;
367 $self->introgression_backcross_parent($self->_retrieve_stockprop('introgression_backcross_parent'));
370 sub _retrieve_introgression_map_version {
371 my $self = shift;
372 $self->introgression_map_version($self->_retrieve_stockprop('introgression_map_version'));
375 sub _retrieve_introgression_chromosome {
376 my $self = shift;
377 $self->introgression_chromosome($self->_retrieve_stockprop('introgression_chromosome'));
380 sub _retrieve_introgression_start_position_bp {
381 my $self = shift;
382 $self->introgression_start_position_bp($self->_retrieve_stockprop('introgression_start_position_bp'));
385 sub _retrieve_introgression_end_position_bp {
386 my $self = shift;
387 $self->introgression_end_position_bp($self->_retrieve_stockprop('introgression_end_position_bp'));
390 =head2 store()
392 Usage: my $stock_id = $accession->store();
393 Desc: stores the current state of the object to the db
394 Ret: the created stock id.
395 Args: none
396 Side Effects:
397 Example:
399 =cut
401 sub store {
402 my $self = shift;
404 print STDERR "storing: UNIQUENAME=".$self->uniquename()."\n";
406 my $id = $self->SUPER::store();
408 if ($self->accessionNumber){
409 $self->_update_stockprop('accession number', $self->accessionNumber);
412 #Include the newly created entry in this database as a PUI
413 my @germplasm_pui = ($self->main_production_site_url."/stock/".$id."/view");
414 if ($self->germplasmPUI){
415 push @germplasm_pui, $self->germplasmPUI;
417 $self->_update_stockprop('PUI', join(',',@germplasm_pui));
419 if ($self->germplasmSeedSource){
420 $self->_update_stockprop('seed source', $self->germplasmSeedSource);
422 # Delete synonyms
423 $self->_remove_stockprop_all_of_type('stock_synonym');
424 # Reinsert
425 if ($self->synonyms){
426 $self->_store_stockprop('stock_synonym', join(',', @{ $self->synonyms }));
428 if ($self->instituteCode){
429 $self->_update_stockprop('institute code', $self->instituteCode);
431 if ($self->instituteName){
432 $self->_update_stockprop('institute name', $self->instituteName);
434 if ($self->biologicalStatusOfAccessionCode){
435 $self->_update_stockprop('biological status of accession code', $self->biologicalStatusOfAccessionCode);
437 if ($self->countryOfOriginCode){
438 $self->_update_stockprop('country of origin', $self->countryOfOriginCode);
440 if ($self->typeOfGermplasmStorageCode){
441 $self->_update_stockprop('type of germplasm storage code', $self->typeOfGermplasmStorageCode);
443 if ($self->acquisitionDate){
444 $self->_update_stockprop('acquisition date', $self->acquisitionDate);
446 if ($self->donors){
447 $self->_remove_stockprop_all_of_type('donor');
448 $self->_remove_stockprop_all_of_type('donor institute');
449 $self->_remove_stockprop_all_of_type('donor PUI');
450 foreach (@{$self->donors}){
451 my $accession = $_->{donorAccessionNumber} || $_->{donorGermplasmName};
452 $self->_store_stockprop('donor', $accession);
453 $self->_store_stockprop('donor institute', $_->{donorInstituteCode});
454 $self->_store_stockprop('donor PUI', $_->{germplasmPUI});
457 $self->_remove_parent_relationship('female_parent');
458 if ($self->mother_accession) {
459 my $return = $self->_store_parent_relationship('female_parent', $self->mother_accession, 'biparental');
460 # TODO: delete accession if error and return error
462 $self->_remove_parent_relationship('male_parent');
463 if ($self->father_accession) {
464 my $return = $self->_store_parent_relationship('male_parent', $self->father_accession, 'biparental');
465 # TODO: delete accession if error and return error
467 if ($self->additional_info) {
468 $self->_update_stockprop('stock_additional_info', encode_json $self->additional_info);
471 if($self->pedigree){
472 print STDERR "CXGN::Stock::Accession->store does not store pedigree info yet!\n";
474 if ($self->variety){
475 $self->_update_stockprop('variety', $self->variety);
477 if ($self->state){
478 $self->_update_stockprop('state', $self->state);
480 if ($self->notes){
481 $self->_update_stockprop('notes', $self->notes);
483 if ($self->locationCode){
484 $self->_update_stockprop('location_code', $self->locationCode);
486 if ($self->ploidyLevel){
487 $self->_update_stockprop('ploidy_level', $self->ploidyLevel);
489 if ($self->genomeStructure){
490 $self->_update_stockprop('genome_structure', $self->genomeStructure);
492 if ($self->transgenic){
493 $self->_update_stockprop('transgenic', $self->transgenic);
495 if ($self->introgression_parent){
496 $self->_update_stockprop('introgression_parent', $self->introgression_parent);
498 if ($self->introgression_backcross_parent){
499 $self->_update_stockprop('introgression_backcross_parent', $self->introgression_backcross_parent);
501 if ($self->introgression_map_version){
502 $self->_update_stockprop('introgression_map_version', $self->introgression_map_version);
504 if ($self->introgression_chromosome){
505 $self->_update_stockprop('introgression_chromosome', $self->introgression_chromosome);
507 if ($self->introgression_start_position_bp){
508 $self->_update_stockprop('introgression_start_position_bp', $self->introgression_start_position_bp);
510 if ($self->introgression_end_position_bp){
511 $self->_update_stockprop('introgression_end_position_bp', $self->introgression_end_position_bp);
513 if ($self->other_editable_stock_props){
514 while (my ($key, $value) = each %{$self->other_editable_stock_props}) {
516 # For other_editable_stock_props that can come from accession file upload and are defined in the editable_stock_props configuration
517 my $q = "SELECT t.cvterm_id FROM cvterm as t JOIN cv ON(t.cv_id=cv.cv_id) WHERE t.name=? and cv.name=?;";
518 my $h = $self->schema->storage->dbh()->prepare($q);
519 $h->execute($key, 'stock_property');
520 my ($cvterm_id) = $h->fetchrow_array();
521 if (!$cvterm_id) {
522 my $new_term = $self->schema->resultset("Cv::Cvterm")->create_with({
523 name => $key,
524 cv => 'stock_property'
526 $cvterm_id = $new_term->cvterm_id();
529 $self->_update_stockprop($key, $value);
533 print STDERR "Saving returned ID $id.\n";
534 $self->stock_id($id);
536 return $self->stock_id();
540 no Moose;
541 __PACKAGE__->meta->make_immutable;