1 package CXGN
::Fieldbook
::TraitProps
;
5 CXGN::Fieldbook::TraitProps - a module to store trait properties in the database.
9 my $trait_props = CXGN::Fieldbook::TraitProps->new({ chado_schema => $chado_schema, db_name => $db_name, trait_names_and_props => \@trait_props_data, overwrite => 1, });
10 my $validate = $trait_props->validate(); #returns true if the trait props are valid and can be stored and returns false otherwise.
11 my $store = $trait_props->store(); #returns true if the trait props are stored and returns false otherwise.
15 Stores trait properties in the database to build trait files to use in Fieldbook data collection
19 Jeremy D. Edwards (jde22@cornell.edu)
24 use MooseX
::FollowPBP
;
25 use Moose
::Util
::TypeConstraints
;
28 has
'chado_schema' => (
30 isa
=> 'DBIx::Class::Schema',
38 has
'trait_names_and_props' => (
40 isa
=> 'ArrayRef[HashRef[Str]]',
48 has
'is_test_run' => (
56 my $chado_schema = $self->get_chado_schema();
59 my @trait_property_names = qw(
68 my $cv = $chado_schema->resultset("Cv::Cv")
70 name
=> 'trait_property',
74 foreach my $property_name (@trait_property_names) {
76 $cvterms{$property_name} = $chado_schema->resultset("Cv::Cvterm")
78 name
=> $property_name,
79 cv
=> 'trait_property',
90 my $chado_schema = $self->get_chado_schema();
92 my $db_rs = $chado_schema->resultset("General::Db")->search( { 'me.name' => $self->get_db_name() });
94 if (!$db_rs->first()) {
95 print STDERR
"Could not find trait ontology database: ".$self->get_db_name()."\n";
99 my $cvterms = $self->_get_cvterms();
101 my @trait_props_data = @
{$self->get_trait_names_and_props()};
103 if (scalar @trait_props_data == 0) {
104 print STDERR
"No trait props supplied\n";
109 foreach my $trait_props (@trait_props_data) {
110 if (!$trait_props->{'trait_name'}) {
111 print STDERR
"Bad data structure for trait properties: no trait_name\n";
114 my $trait_name = $trait_props->{'trait_name'};
116 #make sure that the trait name is valid
118 $trait_cvterm = $chado_schema->resultset("Cv::Cvterm")
120 'dbxref.db_id' => $db_rs->first()->db_id(),
121 'name'=> $trait_name,
127 if (!$trait_cvterm) {
128 print STDERR
"Could not find trait $trait_name\n";
132 #make sure that the trait prop names are valid
133 foreach my $prop_name (keys %{$trait_props}) {
134 if ($prop_name ne 'trait_name') {
135 if (!$cvterms->{$prop_name}) {
136 print STDERR
"Bad data structure for trait properties: $prop_name is not valid";
140 my $prop_cvterm = $cvterms->{$prop_name};
142 #check for an existing prop of the same name
143 my $cvtermprop_search = $trait_cvterm
144 ->search_related('cvtermprops', {
145 'type_id' => $prop_cvterm->cvterm_id(),
149 if ($cvtermprop_search->count() > 1) {
150 print STDERR
"More than one cvtermprop for trait $trait_name with property $prop_name\n";
154 if ($cvtermprop_search->count() == 1) {
155 if (!$self->get_overwrite()) {
156 print STDERR
"A cvtermprop for trait $trait_name with property $prop_name already exists\n";
171 my $chado_schema = $self->get_chado_schema();
174 my $db_rs = $chado_schema->resultset("General::Db")->search( { 'me.name' => $self->get_db_name() });
177 print STDERR
"Could not find trait ontology database: ".$self->get_db_name()."\n";
181 my $cvterms = $self->_get_cvterms();
183 my @trait_props_data = @
{$self->get_trait_names_and_props()};
187 foreach my $trait_props (@trait_props_data) {
189 my $trait_name = $trait_props->{'trait_name'};
191 #get the cvterm for the trait
192 my $trait_cvterm = $chado_schema->resultset("Cv::Cvterm")
194 'dbxref.db_id' => $db_rs->first()->db_id(),
195 'name'=> $trait_name,
202 foreach my $prop_name (keys %{$trait_props}) {
203 if ($prop_name ne 'trait_name') {
205 my $prop_cvterm = $cvterms->{$prop_name};
207 my $trait_prop_value = $trait_props->{$prop_name};
209 #check for an existing prop of the same name
210 my $cvtermprop_search = $trait_cvterm
211 ->search_related('cvtermprops', {
212 'type_id' => $prop_cvterm->cvterm_id(),
216 if ($cvtermprop_search->count() > 1) {
217 die("More that one cvtermprop for trait $trait_name with property $prop_name\n");
220 #if the trait property already exists, update it overwrite is true or die if false
221 if ($cvtermprop_search->count() == 1) {
222 if ($self->get_overwrite()) {
223 my $found_cvtermprop = $cvtermprop_search->first();
224 $found_cvtermprop->update({value
=> $trait_prop_value});
226 die("A trait with property $prop_name already exists\n");
230 ->find_or_create_related('cvtermprops',{
231 'value' => $trait_prop_value,
232 'type_id' => $prop_cvterm->cvterm_id(),
241 if ($self->get_is_test_run()) {
243 die("\nTest run success. Rolling back\n\n");
249 my $transaction_error;
252 $chado_schema->txn_do($coderef);
254 $transaction_error = $_;
257 if ($transaction_error) {
258 if ($self->get_is_test_run()) {
260 print STDERR
"test success (rolling back)\n";
262 print STDERR
"test error\n$transaction_error\n";
265 print STDERR
"\nTransaction error storing trait props: $transaction_error\n";