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
;
29 has
'chado_schema' => (
31 isa
=> 'DBIx::Class::Schema',
39 has
'trait_names_and_props' => (
41 isa
=> 'ArrayRef[HashRef[Str]]',
49 has
'is_test_run' => (
57 my $chado_schema = $self->get_chado_schema();
60 my @trait_property_names = qw(
69 my $cv = $chado_schema->resultset("Cv::Cv")
71 name
=> 'trait_property',
75 foreach my $property_name (@trait_property_names) {
77 $cvterms{$property_name} = $chado_schema->resultset("Cv::Cvterm")
79 name
=> $property_name,
80 cv
=> 'trait_property',
91 my $chado_schema = $self->get_chado_schema();
93 my $db_rs = $chado_schema->resultset("General::Db")->search( { 'me.name' => $self->get_db_name() });
95 if (!$db_rs->first()) {
96 print STDERR
"Could not find trait ontology database: ".$self->get_db_name()."\n";
100 my $cvterms = $self->_get_cvterms();
102 my @trait_props_data = @
{$self->get_trait_names_and_props()};
104 if (scalar @trait_props_data == 0) {
105 print STDERR
"No trait props supplied\n";
110 foreach my $trait_props (@trait_props_data) {
111 if (!$trait_props->{'trait_name'}) {
112 print STDERR
"Bad data structure for trait properties: no trait_name\n";
115 my $trait_name = $trait_props->{'trait_name'};
119 if ($trait_name =~ /(.*)\|(.*$)/) {
124 print STDERR
"Working with trait $trait_name, accession $accession\n";
128 my $cvterm = CXGN
::Cvterm
->new( { schema
=> $chado_schema, accession
=> $accession });
129 $trait_cvterm = $cvterm->cvterm();
132 print STDERR
"Could not find trait $trait_name (with $accession)\n";
137 #make sure that the trait name is valid
139 $trait_cvterm = $chado_schema->resultset("Cv::Cvterm")
141 'dbxref.db_id' => $db_rs->first()->db_id(),
142 'name'=> $trait_name,
148 if (!$trait_cvterm) {
149 print STDERR
"Could not find trait $trait_name\n";
153 #make sure that the trait prop names are valid
154 foreach my $prop_name (keys %{$trait_props}) {
155 if ($prop_name ne 'trait_name') {
156 if (!$cvterms->{$prop_name}) {
157 print STDERR
"Bad data structure for trait properties: $prop_name is not valid";
161 my $prop_cvterm = $cvterms->{$prop_name};
163 #check for an existing prop of the same name
164 my $cvtermprop_search = $trait_cvterm
165 ->search_related('cvtermprops', {
166 'type_id' => $prop_cvterm->cvterm_id(),
170 if ($cvtermprop_search->count() > 1) {
171 print STDERR
"More than one cvtermprop for trait $trait_name with property $prop_name\n";
175 if ($cvtermprop_search->count() == 1) {
176 if (!$self->get_overwrite()) {
177 print STDERR
"A cvtermprop for trait $trait_name with property $prop_name already exists\n";
192 my $chado_schema = $self->get_chado_schema();
195 my $db_rs = $chado_schema->resultset("General::Db")->search( { 'me.name' => $self->get_db_name() });
198 print STDERR
"Could not find trait ontology database: ".$self->get_db_name()."\n";
202 my $cvterms = $self->_get_cvterms();
204 my @trait_props_data = @
{$self->get_trait_names_and_props()};
208 foreach my $trait_props (@trait_props_data) {
211 my $trait_name = $trait_props->{'trait_name'};
214 if ($trait_name =~ /(.*)\|(.*$)/) {
222 my $cvterm = CXGN
::Cvterm
->new( { schema
=> $chado_schema, accession
=> $accession });
223 $trait_cvterm = $cvterm->cvterm();
226 print STDERR
"Could not find trait $trait_name (with $accession)\n";
231 #get the cvterm for the trait
232 my $trait_cvterm = $chado_schema->resultset("Cv::Cvterm")
234 'dbxref.db_id' => $db_rs->first()->db_id(),
235 'name'=> $trait_name,
243 foreach my $prop_name (keys %{$trait_props}) {
244 if ($prop_name ne 'trait_name') {
246 my $prop_cvterm = $cvterms->{$prop_name};
248 my $trait_prop_value = $trait_props->{$prop_name};
250 #check for an existing prop of the same name
251 my $cvtermprop_search = $trait_cvterm
252 ->search_related('cvtermprops', {
253 'type_id' => $prop_cvterm->cvterm_id(),
257 if ($cvtermprop_search->count() > 1) {
258 die("More that one cvtermprop for trait $trait_name with property $prop_name\n");
261 #if the trait property already exists, update it overwrite is true or die if false
262 if ($cvtermprop_search->count() == 1) {
263 if ($self->get_overwrite()) {
264 my $found_cvtermprop = $cvtermprop_search->first();
265 $found_cvtermprop->update({value
=> $trait_prop_value});
267 die("A trait with property $prop_name already exists\n");
271 ->find_or_create_related('cvtermprops',{
272 'value' => $trait_prop_value,
273 'type_id' => $prop_cvterm->cvterm_id(),
282 if ($self->get_is_test_run()) {
284 die("\nTest run success. Rolling back\n\n");
290 my $transaction_error;
293 $chado_schema->txn_do($coderef);
295 $transaction_error = $_;
298 if ($transaction_error) {
299 if ($self->get_is_test_run()) {
301 print STDERR
"test success (rolling back)\n";
303 print STDERR
"test error\n$transaction_error\n";
306 print STDERR
"\nTransaction error storing trait props: $transaction_error\n";