4 CXGN::Stock::SequencingInfo - a class to match keys to a standardized JSON structure
8 The stockprop of type "sequencing_project_info" is stored as JSON. This class maps keys to the JSON structure and retrieves and saves the stockprop.
12 my $si = CXGN::Stock::SequencingInfo->new( { schema => $schema, $stockprop_id });
16 Lukas Mueller <lam87@cornell.edu>
20 package CXGN
::Stock
::SequencingInfo
;
25 use SGN
::Model
::Cvterm
;
39 =head2 genbank_accession
43 =head2 funder_project_id
47 =head2 sequencing_year
57 has
'schema' => (isa
=> 'Ref', is
=> 'rw', required
=> 1);
59 has
'stockprop_id' => (isa
=> 'Maybe[Int]', is
=> 'rw');
61 has
'stock_id' => (isa
=> 'Int', is
=> 'rw');
63 has
'type_id' => (isa
=> 'Int', is
=> 'rw');
65 has
'type' => (isa
=> 'Str', is
=> 'ro', default => "sequencing_project_info" );
67 has
'organization' => (isa
=> 'Maybe[Str]', is
=> 'rw');
69 has
'website' => (isa
=> 'Maybe[Str]', is
=> 'rw');
71 has
'genbank_accession' => (isa
=> 'Maybe[Str]', is
=> 'rw');
73 has
'funded_by' => (isa
=> 'Maybe[Str]', is
=> 'rw');
75 has
'funder_project_id' => (isa
=> 'Maybe[Str]', is
=> 'rw');
77 has
'contact_email' => (isa
=> 'Maybe[Str]', is
=> 'rw');
79 has
'sequencing_year' => (isa
=> 'Maybe[Str]', is
=> 'rw');
81 has
'publication' => (isa
=> 'Maybe[Str]', is
=> 'rw');
83 has
'jbrowse_link' => (isa
=> 'Maybe[Str]', is
=> 'rw');
85 has
'blast_db_id' => (isa
=> 'Maybe[Int]', is
=> 'rw');
87 has
'allowed_fields' => (isa
=> 'Ref', is
=> 'ro', default => sub { [ qw
| organization website genbank_accession funded_by funder_project_id contact_email sequencing_year publication jbrowse_link blast_db_id stockprop_id stock_id
| ] } );
94 if ($args->{stockprop_id
} eq "undefined") { $args->{stockprop_id
} = undef; }
96 print STDtERR
"STOCKPROPID: ".$self->stockprop_id.", TYPE: ".$self->type()."\n";
97 my $type_id = SGN
::Model
::Cvterm
->get_cvterm_row($self->schema(), $self->type(), 'stock_property')->cvterm_id();
99 $self->type_id($type_id);
101 $self->_load_object();
103 $self->stockprop_id($args->{stockprop_id
});
110 =head2 get_sequencing_project_infos($schema, $stock_id)
112 Usage: my @seq_projects = $se_info->get_sequencing_project_infos($schema, $stock_id);
121 sub get_sequencing_project_infos
{
124 my $stock_id = shift;
126 my @stockprops = $class->_retrieve_stockprops($schema, $stock_id, "sequencing_project_info");
128 print STDERR
"Stockprops = ".Dumper
(\
@stockprops);
131 foreach my $sp (@stockprops) {
137 $hash = JSON
::Any
->jsonToObj($json);
139 $hash->{stockprop_id
} = $sp->[0];
140 $hash->{uniquename
} = $schema->resultset("Stock::Stock")->find( { stock_id
=> $stock_id })->uniquename();
142 print STDERR
"Warning: $json is not valid json in stockprop ".$sp->[0].".!\n";
147 print STDERR
"Hashes = ".Dumper
(\
@infos);
151 =head2 all_sequenced_stocks()
153 Usage: @sequenced_stocks = CXGN::Stock->sequenced_stocks();
162 sub all_sequenced_stocks
{
166 print STDERR
"all_sequenced_stocks with ".ref($schema)." as parameter...\n";
167 my $type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'sequencing_project_info', 'stock_property')->cvterm_id();
168 print STDERR
"type_id = $type_id\n";
170 my $sp_rs = $schema->resultset("Stock::Stockprop")->search({ type_id
=> $type_id });
172 my @sequenced_stocks = ();
173 while (my $row = $sp_rs->next()) {
174 print STDERR
"found stock with stock_id ".$row->stock_id()."\n";
175 push @sequenced_stocks, $row->stock_id();
178 return @sequenced_stocks;
182 =head2 OBJECT METHODS
184 =head2 method store()
186 Usage: $s->set_sequencing_project_info($si, $stockprop_id)
187 Desc: creates a sequencing project info in the stockprop
189 Args: a CXGN::Stock::SequencingInfo object, and an optional
190 stockprop_id (which will trigger an update instead
200 if (!$self->stock_id()) {
201 die "Need a stock_id to save SequencingInfo object.";
204 if ($self->stockprop_id()) {
206 print STDERR
"updating stockprop...\n";
207 my $row = $self->schema()->resultset("Stock::Stockprop")->find( { stockprop_id
=> $self->stockprop_id() } );
209 $row->value($self->to_json());
215 print STDERR
"inserting stockprop...\n";
216 my $row = $self->schema()->resultset("Stock::Stockprop")->create(
218 stock_id
=> $self->stock_id(),
219 type_id
=> $self->type_id(),
220 value
=> $self->to_json()
223 $self->stockprop_id($row->stockprop_id());
224 $self->stock_id($row->stock_id());
225 return $row->stockprop_id();
230 =head2 method delete()
244 print STDERR
"CXGN::Stock::SequencingInfo: Deleting sequencing info object ".$self->stockprop_id()."\n";
247 print STDERR
"stock_id = ".$self->stock_id().", stockprop_id = ".$self->stockprop_id()."\n";
250 $stockprop = $self->schema()->resultset("Stock::Stockprop")->find({ type_id
=>$self->type_id(), stock_id
=> $self->stock_id(), stockprop_id
=>$self->stockprop_id() });
254 die "Delete failed!\n";
258 print STDERR
"No such stockprop associated with such type or stock. Not deleting.\n";
262 print STDERR
"Deleting stcokprop now.\n";
263 $stockprop->delete();
264 print STDERR
"SequencingInfo successfully deleted.\n";
274 Side Effects: requires stockprop_id and schema to be set
282 print STDERR
"_load_object...\n";
284 print STDERR
"Stockprop_id is now ".$self->stockprop_id()."\n";
285 if ($self->stockprop_id()) {
286 print STDERR
"configuring object...\n";
287 my $stockprop_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($self->schema, $self->type(), 'stock_property')->cvterm_id();
288 my $rs = $self->schema->resultset("Stock::Stockprop")->search({ stockprop_id
=> $self->stockprop_id(), type_id
=> $stockprop_type_id } );
290 if ($rs->count() == 0) { die "No stockprops could be retrieved." }
292 my $row = $rs->next(); # should only be one
294 $self->type_id($stockprop_type_id);
295 $self->stock_id($row->stock_id());
296 $self->from_json($row->value());
303 =head2 _retrieve_stockprops
306 Desc: Retrieves stockprop as a list of [stockprop_id, value]
314 sub _retrieve_stockprops
{
317 my $stock_id = shift;
322 print STDERR
"_retrieve_stockprops...\n";
325 my $stockprop_type_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, $type, 'stock_property')->cvterm_id();
326 my $rs = $schema->resultset("Stock::Stockprop")->search({ stock_id
=> $stock_id, type_id
=> $stockprop_type_id }, { order_by
=> {-asc
=> 'stockprop_id'} });
328 while (my $r = $rs->next()){
329 push @results, [ $r->stockprop_id(), $r->value() ];
334 print STDERR
"Cvterm $type does not exist in this database\n";
355 my $data = JSON
::Any
->decode($json);
357 $self->from_hash($data);
375 my $allowed_fields = $self->allowed_fields();
377 print STDERR Dumper
($hash);
379 foreach my $f (@
$allowed_fields) {
380 print STDERR
"Processing $f ($hash->{$f})...\n";
381 if ($hash->{$f} eq "undefined") { $hash->{$f} = undef; }
382 $self->$f($hash->{$f});
400 my $allowed_fields = $self->allowed_fields();
402 print STDERR Dumper
($allowed_fields);
405 foreach my $f (@
$allowed_fields) {
406 if (defined($self->$f())) {
407 $data->{$f} = $self->$f();
411 my $json = JSON
::Any
->encode($data);
430 foreach my $f (@
{$self->allowed_fields()}) {
431 $hashref->{$f} = $self->$f;
453 # check keys in the info hash...
454 if (!defined($self->sequencing_year())) {
455 push @errors, "Need year for sequencing project";
457 if (!defined($self->organization())) {
458 push @errors, "Need organization for sequencing project";
460 if (!defined($self->website())) {
461 push @errors, "Need website for sequencing project";
463 if (!defined($self->publication())) {
464 push @warnings, "Need publication for sequencing project";
466 if (!defined($self->website())) {
467 push @warnings, "Need project url for sequencing project";
469 if (!defined($self->jbrowse_link())) {
470 push @warnings, "Need jbrowse link for sequencing project";
474 die join("\n", @errors);