Merge pull request #5248 from solgenomics/topic/batch_update_trials
[sgn.git] / lib / CXGN / Pedigree / AddCrossInfo.pm
blobeda0c6b6f9ec4e9aad37ddebed64db5322e41608
1 package CXGN::Pedigree::AddCrossInfo;
3 =head1 NAME
5 CXGN::Pedigree::AddCrossInfo - a module to add cross information such as date of pollination, number of flowers pollinated, number of fruits set as stock properties for cross.
7 =head1 USAGE
9 my $cross_add_info = CXGN::Pedigree::AddCrossInfo->new({ chado_schema => $chado_schema, cross_name => $cross_name, key => $info_type, value => $value, data_type => $data_type} );
10 $cross_add_info->add_info();
12 =head1 DESCRIPTION
14 Adds cross properties in json string format to stock of type cross. The cross must already exist in the database. This module is intended to be used in independent loading scripts and interactive dialogs.
16 =head1 AUTHORS
18 Jeremy D. Edwards (jde22@cornell.edu)
19 Titima Tantikanjana (tt15@cornell.edu)
21 =cut
23 use Moose;
24 use MooseX::FollowPBP;
25 use Moose::Util::TypeConstraints;
26 use Try::Tiny;
27 use CXGN::Stock::StockLookup;
28 use SGN::Model::Cvterm;
29 use Data::Dumper;
30 use JSON;
32 has 'chado_schema' => (
33 is => 'rw',
34 isa => 'DBIx::Class::Schema',
35 predicate => 'has_chado_schema',
36 required => 1,
38 has 'cross_name' => (isa =>'Str', is => 'rw', predicate => 'has_cross_name', required => 1,);
39 has 'key' => (isa =>'Str', is => 'rw', predicate => 'has_key', required => 1,);
40 has 'value' => (isa =>'Str', is => 'rw', predicate => 'has_value', required => 1,);
41 has 'data_type' => (isa =>'Str', is => 'rw', predicate => 'has_type', required => 1,);
43 sub add_info {
44 my $self = shift;
45 my $schema = $self->get_chado_schema();
46 my $transaction_error;
48 #add all cross info in a single transaction
49 my $coderef = sub {
51 #get cross (stock of type cross)
52 my $cross_stock = $self->_get_cross($self->get_cross_name());
53 if (!$cross_stock) {
54 print STDERR "Cross could not be found\n";
55 return;
58 # get cvterm of cross info type (crossing_metadata_json or cross_additional_info)
59 my $cross_info_type = $self->get_data_type();
60 # print STDERR "DATA TYPE =".Dumper($cross_info_type)."\n";
61 my $cross_info_cvterm;
62 if (($cross_info_type eq 'crossing_metadata_json') || ($cross_info_type eq 'cross_additional_info')) {
63 $cross_info_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, $cross_info_type, 'stock_property');
64 } else {
65 print STDERR "Invalid type"."\n";
66 return;
69 my $cross_json_string;
70 my $cross_json_hash = {};
71 my $previous_stockprop_rs = $cross_stock->stockprops({type_id=>$cross_info_cvterm->cvterm_id});
72 if ($previous_stockprop_rs->count == 1){
73 $cross_json_string = $previous_stockprop_rs->first->value();
74 $cross_json_hash = decode_json $cross_json_string;
75 $cross_json_string = _generate_property_hash($self->get_key, $self->get_value, $cross_json_hash);
76 $previous_stockprop_rs->first->update({value=>$cross_json_string});
77 } elsif ($previous_stockprop_rs->count > 1) {
78 print STDERR "More than one found!\n";
79 return;
80 } else {
81 $cross_json_string = _generate_property_hash($self->get_key, $self->get_value, $cross_json_hash);
82 $cross_stock->create_stockprops({$cross_info_cvterm->name() => $cross_json_string});
84 # print STDERR "CROSS JSON STRING =".Dumper($cross_json_string)."\n";
87 #try to add all cross info in a transaction
88 try {
89 $schema->txn_do($coderef);
90 } catch {
91 $transaction_error = $_;
94 if ($transaction_error) {
95 print STDERR "Transaction error storing information for cross: $transaction_error\n";
96 return;
99 return 1;
102 sub _generate_property_hash {
103 my $key = shift;
104 my $value = shift;
105 my $cross_json_hash = shift;
106 $cross_json_hash->{$key} = $value;
107 #print STDERR Dumper $cross_json_hash;
108 my $cross_json_string = encode_json $cross_json_hash;
109 return $cross_json_string;
113 sub _get_cross {
114 my $self = shift;
115 my $cross_name = shift;
116 my $schema = $self->get_chado_schema();
117 my $stock_lookup = CXGN::Stock::StockLookup->new(schema => $schema);
118 my $stock;
119 my $cross_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'cross', 'stock_type');
121 $stock_lookup->set_stock_name($cross_name);
122 $stock = $stock_lookup->get_cross_exact();
124 if (!$stock) {
125 print STDERR "Cross name does not exist\n";
126 return;
128 return $stock;
131 #######
133 #######