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;
159 Usage: $self->get_type
160 Desc: find the cvterm type of this stock
161 Ret: L<Bio::Chado::Schema::Cv::Cvterm> object
171 if (my $bcs_stock = $self->get_object_row ) {
172 return $bcs_stock->type;
182 return $self->{object_row
};
187 $self->{object_row
} = shift;
192 Usage: $self->get_resultset(ModuleName::TableName)
193 Desc: Get a ResultSet object for source_name
194 Ret: a ResultSet object
204 return $self->get_schema()->resultset("$source");
207 =head2 accessors get_schema, set_schema
219 return $self->{schema
};
224 $self->{schema
} = shift;
228 ###mapping accessors to DBIC
230 =head2 accessors get_name, set_name
242 return $self->get_object_row()->get_column("name");
247 $self->get_object_row()->set_column(name
=> shift);
250 =head2 accessors get_uniquename, set_uniquename
262 return $self->get_object_row()->get_column("uniquename");
267 $self->get_object_row()->set_column(uniquename
=> shift);
270 =head2 accessors get_organism_id, set_organism_id
280 sub get_organism_id
{
282 if (my $bcs_stock = $self->get_object_row ) {
283 return $bcs_stock->get_column("organism_id");
288 sub set_organism_id
{
290 $self->get_object_row()->set_column(organism_id
=> shift);
293 =head2 accessors get_type_id, set_type_id
305 if (my $bcs_stock = $self->get_object_row ) {
306 return $bcs_stock->get_column("type_id");
312 $self->get_object_row()->set_column(type_id
=> shift);
315 =head2 accessors get_description, set_description
325 sub get_description
{
327 return $self->get_object_row()->get_column("description");
330 sub set_description
{
332 $self->get_object_row()->set_column(description
=> shift);
335 =head2 accessors get_stock_id, set_stock_id
347 if ( my $bcs_stock = $self->get_object_row ) {
348 return $bcs_stock->get_column("stock_id");
355 $self->get_object_row()->set_column(stock_id
=> shift);
358 =head2 accessors get_is_obsolete, set_is_obsolete
368 sub get_is_obsolete
{
370 my $stock = $self->get_object_row();
371 return $stock->get_column("is_obsolete") if $stock;
374 sub set_is_obsolete
{
376 $self->get_object_row()->set_column(is_obsolete
=> shift);
379 =head2 function get_image_ids
381 Synopsis: my @images = $self->get_image_ids()
383 Returns: a list of image ids
385 Description: a method for fetching all images associated with a stock
391 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
392 ( "SELECT image_id FROM phenome.stock_image WHERE stock_id=? ",
399 =head2 associate_allele
401 Usage: $self->associate_allele($allele_id, $sp_person_id)
402 Desc: store a stock-allele link in phenome.stock_allele
404 Args: allele_id, sp_person_id
405 Side Effects: store a metadata row
410 sub associate_allele
{
412 my $allele_id = shift;
413 my $sp_person_id = shift;
414 if (!$allele_id || !$sp_person_id) {
415 warn "Need both allele_id and person_id for linking the stock with an allele!";
418 my $metadata_schema = CXGN
::Metadata
::Schema
->connect(
419 sub { $self->get_schema->storage->dbh },
420 { on_connect_do
=> ['SET search_path TO metadata;'] },
422 my $metadata = CXGN
::Metadata
::Metadbdata
->new($metadata_schema);
423 $metadata->set_create_person_id($sp_person_id);
424 my $metadata_id = $metadata->store()->get_metadata_id();
425 #check if the allele is already linked
426 my $ids = $self->get_schema->storage->dbh->selectcol_arrayref
427 ( "SELECT stock_allele_id FROM phenome.stock_allele WHERE stock_id = ? AND allele_id = ?",
432 if ($ids) { warn "Allele $allele_id is already linked with stock " . $self->get_stock_id ; }
433 #store the allele_id - stock_id link
434 my $q = "INSERT INTO phenome.stock_allele (stock_id, allele_id, metadata_id) VALUES (?,?,?) RETURNING stock_allele_id";
435 my $sth = $self->get_schema->storage->dbh->prepare($q);
436 $sth->execute($self->get_stock_id, $allele_id, $metadata_id);
437 my ($id) = $sth->fetchrow_array;