3 CXGN::Chado::Stock - a second-level DBIC Bio::Chado::Schema::Stock::Stock object
9 Created to work with CXGN::Page::Form::AjaxFormPage
10 for eliminating the need to refactor the AjaxFormPage and Editable to work with DBIC objects.
11 Functions such as 'get_obsolete' , 'store' , and 'exists_in_database' are required , and do not use standard DBIC syntax.
15 Naama Menda <nm249@cornell.edu>
19 package CXGN
::Chado
::Stock
;
23 use Bio
::Chado
::Schema
;
24 use CXGN
::Metadata
::Schema
;
26 use base qw
/ CXGN::DB::Object / ;
30 Usage: my $stock = CXGN::Chado::Stock->new($schema, $stock_id);
32 Ret: a CXGN::Chado::Stock object
33 Args: a $schema a schema object,
34 $stock_id, if omitted, an empty stock object is created.
35 Side_Effects: accesses the database, check if exists the database columns that this object use. die if the id is not an integer.
44 ### First, bless the class to create the object and set the schema into the object.
45 #my $self = $class->SUPER::new($schema);
46 my $self = bless {}, $class;
47 $self->set_schema($schema);
50 $stock = $self->get_resultset('Stock::Stock')->find({ stock_id
=> $id });
52 ### Create an empty resultset object;
53 $stock = $self->get_resultset('Stock::Stock')->new( {} );
55 ###It's important to set the object row for using the accesor in other class functions
56 $self->set_object_row($stock);
65 Desc: store a new stock
68 Side Effects: checks if the stock exists in the database, and if does, will attempt to update
75 my $id = $self->get_stock_id();
76 my $schema=$self->get_schema();
77 #no stock id . Check first if the name exists in te database
79 my $exists= $self->exists_in_database();
81 my $new_row = $self->get_object_row();
84 $id=$new_row->stock_id();
86 my $existing_stock=$self->get_resultset('Stock::Stock')->find($exists);
87 #don't update here if stock already exist. User should call from the code exist_in_database
88 #and instantiate a new stock object with the database id
89 #updating here is not a good idea, since it might not be what the user intended to do
90 #and it can mess up the database.
93 $self->get_object_row()->update();
98 ########################
101 =head2 exists_in_database
103 Usage: $self->exists_in_database()
104 Desc: check if the uniquename exists in the stock table
112 sub exists_in_database
{
114 my $stock_id = $self->get_stock_id();
115 my $uniquename = $self->get_uniquename || '' ;
116 my ($s) = $self->get_resultset('Stock::Stock')->search(
118 uniquename
=> { 'ilike' => $uniquename },
120 #loading new stock - $stock_id is undef
121 if (defined($s) && !$stock_id ) { return $s->stock_id ; }
123 #updating an existing stock
124 elsif ($stock_id && defined($s) ) {
125 if ( ($s->stock_id == $stock_id) ) {
127 #trying to update the uniquename
128 } elsif ( $s->stock_id != $stock_id ) {
129 return " Can't update an existing stock $stock_id uniquename:$uniquename.";
130 # if the new name we're trying to update/insert does not exist in the stock table..
131 } elsif ($stock_id && !$s->stock_id) {
140 Usage: $self->get_organism
141 Desc: find the organism object of this stock
142 Ret: L<Bio::Chado::Schema::Organism::Organism> object
151 if (my $bcs_stock = $self->get_object_row) {
152 return $bcs_stock->organism;
160 Usage: $self->get_species
161 Desc: find the species name of this stock , if one exists
171 my $organism = $self->get_organism;
173 return $organism->species;
174 }else { return undef; }
179 Usage: $self->set_species
180 Desc: set organism_id for the stock using organism.species name
182 Args: species name (case insensitive)
183 Side Effects: sets the organism_id for the stock
190 my $species_name = shift; # this has to be EXACTLY as stored in the organism table
191 my $organism = $self->get_schema->resultset('Organism::Organism')->search(
192 { 'lower(species)' => { like
=> lc($species_name) } } )->single ; #should be 1 result
194 $self->get_object_row->set_column(organism_id
=> $organism->organism_id );
197 warn "NO organism found for species name $species_name!!\n";
203 Usage: $self->get_type
204 Desc: find the cvterm type of this stock
205 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
215 if (my $bcs_stock = $self->get_object_row ) {
216 return $bcs_stock->type;
226 return $self->{object_row
};
231 $self->{object_row
} = shift;
236 Usage: $self->get_resultset(ModuleName::TableName)
237 Desc: Get a ResultSet object for source_name
238 Ret: a ResultSet object
248 return $self->get_schema()->resultset("$source");
251 =head2 accessors get_schema, set_schema
263 return $self->{schema
};
268 $self->{schema
} = shift;
272 ###mapping accessors to DBIC
274 =head2 accessors get_name, set_name
286 return $self->get_object_row()->get_column("name");
291 $self->get_object_row()->set_column(name
=> shift);
294 =head2 accessors get_uniquename, set_uniquename
306 return $self->get_object_row()->get_column("uniquename");
311 $self->get_object_row()->set_column(uniquename
=> shift);
314 =head2 accessors get_organism_id, set_organism_id
324 sub get_organism_id
{
326 if (my $bcs_stock = $self->get_object_row ) {
327 return $bcs_stock->get_column("organism_id");
332 sub set_organism_id
{
334 $self->get_object_row()->set_column(organism_id
=> shift);
337 =head2 accessors get_type_id, set_type_id
349 if (my $bcs_stock = $self->get_object_row ) {
350 return $bcs_stock->get_column("type_id");
356 $self->get_object_row()->set_column(type_id
=> shift);
359 =head2 accessors get_description, set_description
369 sub get_description
{
371 return $self->get_object_row()->get_column("description");
374 sub set_description
{
376 $self->get_object_row()->set_column(description
=> shift);
379 =head2 accessors get_stock_id, set_stock_id
391 if ( my $bcs_stock = $self->get_object_row ) {
392 return $bcs_stock->get_column("stock_id");
399 $self->get_object_row()->set_column(stock_id
=> shift);
402 =head2 accessors get_is_obsolete, set_is_obsolete
412 sub get_is_obsolete
{
414 my $stock = $self->get_object_row();
415 return $stock->get_column("is_obsolete") if $stock;
418 sub set_is_obsolete
{
420 $self->get_object_row()->set_column(is_obsolete
=> shift);
423 =head2 function get_image_ids
425 Synopsis: my @images = $self->get_image_ids()
427 Returns: a list of image ids
429 Description: a method for fetching all images associated with a stock
435 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
436 ( "SELECT image_id FROM phenome.stock_image WHERE stock_id=? ",
443 =head2 associate_allele
445 Usage: $self->associate_allele($allele_id, $sp_person_id)
446 Desc: store a stock-allele link in phenome.stock_allele
448 Args: allele_id, sp_person_id
449 Side Effects: store a metadata row
454 sub associate_allele
{
456 my $allele_id = shift;
457 my $sp_person_id = shift;
458 if (!$allele_id || !$sp_person_id) {
459 warn "Need both allele_id and person_id for linking the stock with an allele!";
462 my $metadata_id = $self->_new_metadata_id($sp_person_id);
463 #check if the allele is already linked
464 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
465 ( "SELECT stock_allele_id FROM phenome.stock_allele WHERE stock_id = ? AND allele_id = ?",
470 if ($ids) { warn "Allele $allele_id is already linked with stock " . $self->get_stock_id ; }
471 #store the allele_id - stock_id link
472 my $q = "INSERT INTO phenome.stock_allele (stock_id, allele_id, metadata_id) VALUES (?,?,?) RETURNING stock_allele_id";
473 my $sth = $self->get_schema->storage->dbh->prepare($q);
474 $sth->execute($self->get_stock_id, $allele_id, $metadata_id);
475 my ($id) = $sth->fetchrow_array;
479 =head2 associate_owner
481 Usage: $self->associate_owner($owner_sp_person_id, $sp_person_id)
482 Desc: store a stock-owner link in phenome.stock_owner
484 Args: owner_id, sp_person_id
485 Side Effects: store a metadata row
490 sub associate_owner
{
492 my $owner_id = shift;
493 my $sp_person_id = shift;
494 if (!$owner_id || !$sp_person_id) {
495 warn "Need both owner_id and person_id for linking the stock with an owner!";
498 my $metadata_id = $self->_new_metadata_id($sp_person_id);
499 #check if the owner is already linked
500 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
501 ( "SELECT stock_owner_id FROM phenome.stock_owner WHERE stock_id = ? AND owner_id = ?",
506 if ($ids) { warn "Owner $owner_id is already linked with stock " . $self->get_stock_id ; }
507 #store the owner_id - stock_id link
508 my $q = "INSERT INTO phenome.stock_owner (stock_id, owner_id, metadata_id) VALUES (?,?,?) RETURNING stock_owner_id";
509 my $sth = $self->get_schema->storage->dbh->prepare($q);
510 $sth->execute($self->get_stock_id, $owner_id, $metadata_id);
511 my ($id) = $sth->fetchrow_array;
515 =head2 get_trait_list
518 Desc: gets the list of traits that have been measured
520 Ret: a list of lists ( [ cvterm_id, cvterm_name] , ...)
530 my $q = "select distinct(cvterm.cvterm_id), db.name || ':' || dbxref.accession, cvterm.name, avg(phenotype.value::Real), stddev(phenotype.value::Real) from stock as accession join stock_relationship on (accession.stock_id=stock_relationship.object_id) JOIN stock as plot on (plot.stock_id=stock_relationship.subject_id) JOIN nd_experiment_stock ON (plot.stock_id=nd_experiment_stock.stock_id) JOIN nd_experiment_phenotype USING(nd_experiment_id) JOIN phenotype USING (phenotype_id) JOIN cvterm ON (phenotype.cvalue_id = cvterm.cvterm_id) JOIN dbxref ON(cvterm.dbxref_id = dbxref.dbxref_id) JOIN db USING(db_id) where accession.stock_id=? group by cvterm.cvterm_id, db.name || ':' || dbxref.accession, cvterm.name";
531 my $h = $self->get_schema()->storage->dbh()->prepare($q);
532 $h->execute($self->get_stock_id());
534 while (my ($cvterm_id, $cvterm_accession, $cvterm_name, $avg, $stddev) = $h->fetchrow_array()) {
535 push @traits, [ $cvterm_id, $cvterm_accession, $cvterm_name, $avg, $stddev ];
538 # get directly associated traits
540 $q = "select distinct(cvterm.cvterm_id), db.name || ':' || dbxref.accession, cvterm.name, avg(phenotype.value::Real), stddev(phenotype.value::Real) from stock JOIN nd_experiment_stock ON (stock.stock_id=nd_experiment_stock.stock_id) JOIN nd_experiment_phenotype USING(nd_experiment_id) JOIN phenotype USING (phenotype_id) JOIN cvterm ON (phenotype.cvalue_id = cvterm.cvterm_id) JOIN dbxref ON(cvterm.dbxref_id = dbxref.dbxref_id) JOIN db USING(db_id) where stock.stock_id=? group by cvterm.cvterm_id, db.name || ':' || dbxref.accession, cvterm.name";
541 $h = $self->get_schema()->storage()->dbh()->prepare($q);
542 $h->execute($self->get_stock_id());
543 while (my ($cvterm_id, $cvterm_accession, $cvterm_name, $avg, $stddev) = $h->fetchrow_array()) {
544 push @traits, [ $cvterm_id, $cvterm_accession, $cvterm_name, $avg, $stddev ];
554 Desc: gets the list of trails this stock was used in
564 my $q = "select distinct(project.project_id), project.name, nd_geolocation_id, nd_geolocation.description from stock as accession join stock_relationship on (accession.stock_id=stock_relationship.object_id) JOIN stock as plot on (plot.stock_id=stock_relationship.subject_id) JOIN nd_experiment_stock ON (plot.stock_id=nd_experiment_stock.stock_id) JOIN nd_experiment_project USING(nd_experiment_id) JOIN project USING (project_id) LEFT JOIN projectprop ON (project.project_id=projectprop.project_id) JOIN cvterm AS geolocation_type ON (projectprop.type_id=geolocation_type.cvterm_id) LEFT JOIN nd_geolocation ON (projectprop.value::INT = nd_geolocation_id) where accession.stock_id=? AND (geolocation_type.name='project location' OR geolocation_type.name IS NULL) ";
565 my $h = $self->get_schema()->storage()->dbh()->prepare($q);
566 $h->execute($self->get_stock_id());
568 while (my ($project_id, $project_name, $nd_geolocation_id, $nd_geolocation) = $h->fetchrow_array()) {
569 push @trials, [ $project_id, $project_name, $nd_geolocation_id, $nd_geolocation ];
578 =head2 _new_metadata_id
580 Usage: my $md_id = $self->_new_metatada_id($sp_person_id)
581 Desc: Store a new md_metadata row with a $sp_person_id
587 sub _new_metadata_id
{
589 my $sp_person_id = shift;
590 my $metadata_schema = CXGN
::Metadata
::Schema
->connect(
591 sub { $self->get_schema->storage->dbh },
593 my $metadata = CXGN
::Metadata
::Metadbdata
->new($metadata_schema);
594 $metadata->set_create_person_id($sp_person_id);
595 my $metadata_id = $metadata->store()->get_metadata_id();
601 my $other_stock_id = shift;
603 if ($other_stock_id == $self->get_stock_id()) {
604 print STDERR
"Trying to merge stock into itself ($other_stock_id) Skipping...\n";
610 my $subject_rel_count;
611 my $object_rel_count;
612 my $stock_allele_count;
614 my $experiment_stock_count;
615 my $stock_dbxref_count;
616 my $stock_owner_count;
620 my $schema = $self->get_schema();
624 my $sprs = $schema->resultset("Stock::Stockprop")->search( { stock_id
=> $other_stock_id });
625 while (my $row = $sprs->next()) {
627 # check if this stockprop already exists for this stock; save only if not
629 my $thissprs = $schema->resultset("Stock::Stockprop")->search(
631 stock_id
=> $self->get_stock_id(),
632 type_id
=> $row->type_id(),
633 value
=> $row->value()
636 if ($thissprs->count() == 0) {
637 my $value = $row->value();
638 my $type_id = $row->type_id();
640 my $rank_rs = $schema->resultset("Stock::Stockprop")->search( { stock_id
=> $self->get_stock_id(), type_id
=> $type_id });
643 if ($rank_rs->count() > 0) {
644 $rank = $rank_rs->rank->max();
649 $row->stock_id($self->get_stock_id());
653 print STDERR
"MERGED stockprop_id ".$row->stockprop_id." for stock $other_stock_id type_id $type_id value $value into stock ".$self->get_stock_id()."\n";
658 # move subject relationships
660 my $ssrs = $schema->resultset("Stock::StockRelationship")->search( { subject_id
=> $other_stock_id });
662 while (my $row = $ssrs->next()) {
664 my $this_subject_rel_rs = $schema->resultset("Stock::StockRelationship")->search( { subject_id
=> $self->get_stock_id(), object_id
=> $row->object_id, type_id
=> $row->type_id() });
666 if ($this_subject_rel_rs->count() == 0) { # this stock does not have the relationship
668 my $rank_rs = $schema->resultset("Stock::StockRelationship")->search( { subject_id
=> $self->get_stock_id(), type_id
=> $row->type_id() });
670 if ($rank_rs->count() > 0) {
671 $rank = $rank_rs->rank()->max();
675 $row->subject_id($self->get_stock_id());
677 print STDERR
"Moving subject relationships from stock $other_stock_id to stock ".$self->get_stock_id()."\n";
678 $subject_rel_count++;
682 # move object relationships
684 my $osrs = $schema->resultset("Stock::StockRelationship")->search( { object_id
=> $other_stock_id });
685 while (my $row = $osrs->next()) {
686 my $this_object_rel_rs = $schema->resultset("Stock::StockRelationship")->search( { object_id
=> $self->get_stock_id, subject_id
=> $row->subject_id(), type_id
=> $row->type_id() });
688 if ($this_object_rel_rs->count() == 0) {
689 my $rank_rs = $schema->resultset("Stock::StockRelationship")->search( { object_id
=> $self->get_stock_id(), type_id
=> $row->type_id() });
691 if ($rank_rs->count() > 0) {
692 $rank = $rank_rs->get_column("rank")->max();
696 $row->object_id($self->get_stock_id());
698 print STDERR
"Moving object relationships from stock $other_stock_id to stock ".$self->get_stock_id()."\n";
703 # move experiment_stock
705 my $esrs = $schema->resultset("NaturalDiversity::NdExperimentStock")->search( { stock_id
=> $other_stock_id });
706 while (my $row = $esrs->next()) {
707 $row->stock_id($self->get_stock_id());
709 print STDERR
"Moving experiments for stock $other_stock_id to stock ".$self->get_stock_id()."\n";
710 $experiment_stock_count++;
713 # move stock_cvterm relationships
719 my $sdrs = $schema->resultset("Stock::StockDbxref")->search( { stock_id
=> $other_stock_id });
720 while (my $row = $sdrs->next()) {
721 $row->stock_id($self->get_stock_id());
723 $stock_dbxref_count++;
726 # move sgn.pcr_exp_accession relationships
730 # move sgn.pcr_experiment relationships
735 # move stock_genotype relationships
739 my $phenome_schema = CXGN
::Phenome
::Schema
->connect(
740 sub { $self->get_schema->storage->dbh() }, { on_connect_do
=> [ 'SET search_path TO phenome, public, sgn'] }
743 # move phenome.stock_allele relationships
745 my $sars = $phenome_schema->resultset("StockAllele")->search( { stock_id
=> $other_stock_id });
746 while (my $row = $sars->next()) {
747 $row->stock_id($self->get_stock_id());
749 print STDERR
"Moving stock alleles from stock $other_stock_id to stock ".$self->get_stock_id()."\n";
750 $stock_allele_count++;
753 # move image relationships
756 my $irs = $phenome_schema->resultset("StockImage")->search( { stock_id
=> $other_stock_id });
757 while (my $row = $irs->next()) {
759 my $this_rs = $phenome_schema->resultset("StockImage")->search( { stock_id
=> $self->get_stock_id(), image_id
=> $row->image_id() } );
760 if ($this_rs->count() == 0) {
761 $row->stock_id($self->get_stock_id());
763 print STDERR
"Moving image ".$row->image_id()." from stock $other_stock_id to stock ".$self->get_stock_id()."\n";
767 print STDERR
"Removing stock_image entry...\n";
768 $row->delete(); # there is no cascade delete on image relationships, so we need to remove dangling relationships.
774 my $sors = $phenome_schema->resultset("StockOwner")->search( { stock_id
=> $other_stock_id });
775 while (my $row = $sors->next()) {
777 my $this_rs = $phenome_schema->resultset("StockOwner")->search( { stock_id
=> $self->get_stock_id(), sp_person_id
=> $row->sp_person_id() });
778 if ($this_rs->count() == 0) {
779 $row->stock_id($self->get_stock_id());
781 print STDERR
"Moved stock_owner ".$row->sp_person_id()." of stock $other_stock_id to stock ".$self->get_stock_id()."\n";
782 $stock_owner_count++;
785 print STDERR
"(Deleting stock owner entry for stock $other_stock_id, owner ".$row->sp_person_id()."\n";
786 $row->delete(); # see comment for move image relationships
792 my $sgn_schema = SGN
::Schema
->connect(
793 sub { $self->get_schema->storage->dbh() },
796 my $mrs1 = $sgn_schema->resultset("Map")->search( { parent_1
=> $other_stock_id });
797 while (my $row = $mrs1->next()) {
798 $row->parent_1($self->get_stock_id());
800 print STDERR
"Move map parent_1 $other_stock_id to ".$self->get_stock_id()."\n";
804 my $mrs2 = $sgn_schema->resultset("Map")->search( { parent_2
=> $other_stock_id });
805 while (my $row = $mrs2->next()) {
806 $row->parent_2($self->get_stock_id());
808 print STDERR
"Move map parent_2 $other_stock_id to ".$self->get_stock_id()."\n";
812 print STDERR
"Done with merge of stock_id $other_stock_id into ".$self->get_stock_id()."\n";
813 print STDERR
"Relationships moved: \n";
814 print STDERR
<<COUNTS;
815 Stock props: $stockprop_count
816 Subject rels: $subject_rel_count
817 Object rels: $object_rel_count
818 Alleles: $stock_allele_count
820 Experiments: $experiment_stock_count
821 Dbxrefs: $stock_dbxref_count
822 Stock owners: $stock_owner_count
823 Map parents: $parent_1_count
824 Map parents: $parent_2_count