1 package CXGN
::Transformation
::CreateAutogeneratedNames
;
5 CXGN::Transformation::CreateAutogeneratedNames - a module to create autogenerated names based on autogenerated name metadata.
15 Titima Tantikanjana (tt15@cornell.edu)
20 use MooseX
::FollowPBP
;
21 use Moose
::Util
::TypeConstraints
;
23 use CXGN
::Stock
::StockLookup
;
24 use SGN
::Model
::Cvterm
;
30 isa
=> 'DBIx::Class::Schema',
31 predicate
=> 'has_schema',
37 predicate
=> 'has_dbh',
41 has
'breeding_program_id' => (
44 predicate
=> 'has_breeding_program_id',
48 has
'name_format' => (
51 predicate
=> 'has_name_format',
55 has
'number_of_names' => (
58 predicate
=> 'has_number_of_names',
62 has
'source_info' => (
65 predicate
=> 'has_source_info',
73 my $schema = $self->get_schema();
74 my $breeding_program_id = $self->get_breeding_program_id();
75 my $name_format = $self->get_name_format();
76 my $number_of_names = $self->get_number_of_names();
77 my $source_info_string = $self->get_source_info();
78 my $source_info = decode_json
$source_info_string;
80 my @autogenerated_names;
81 my $name_metadata_hash = {};
83 my $autogenerated_name_metadata_cvterm = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'autogenerated_name_metadata', 'project_property');
84 my $program = $schema->resultset('Project::Project')->find({ project_id
=> $breeding_program_id});
86 return {error
=> "Breeding Program not found!\n"};
89 my $name_metadata_projectprop_rs = $program->projectprops({type_id
=> $autogenerated_name_metadata_cvterm->cvterm_id});
90 if ($name_metadata_projectprop_rs->count == 1){
91 my $name_metadata_string = $name_metadata_projectprop_rs->first->value();
92 $name_metadata_hash = decode_json
$name_metadata_string;
94 return {error
=> "Error retrieving autogenerated name metadata!\n"};
96 my $name_format_metadata = $name_metadata_hash->{$name_format};
97 my $name_attributes = $name_format_metadata->{'name_attributes'};
98 my $last_serial_number = $name_format_metadata->{'last_serial_number'};
100 my @new_autogenerated_names;
102 foreach my $n (1..$number_of_names) {
103 my @all_name_components = ();
105 my $incremented_number = $n + $last_serial_number;
106 foreach my $attribute (@
$name_attributes) {
109 if (ref $attribute eq ref {}) {
110 %text_hash = %{$attribute};
111 $name_info = $text_hash{'text'};
112 push @all_name_components, $name_info;
114 $name_info = $source_info->{$attribute};
115 push @all_name_components, $name_info;
118 $new_name = join("_", @all_name_components).$incremented_number;
119 push @new_autogenerated_names, $new_name;
122 my $rs = $schema->resultset("Stock::Stock")->search({
123 'is_obsolete' => { '!=' => 't' },
124 'uniquename' => { -in => \
@new_autogenerated_names }
127 my @invalid_names = ();
128 while (my $r=$rs->next){
129 push @invalid_names, $r->uniquename;
132 if (scalar(@invalid_names) >= 1) {
133 my $names = join(",", @invalid_names);
134 return {error
=> "Invalid Names: $names!\n"};
137 my $new_last_serial_number = $last_serial_number + $number_of_names;
139 my $new_names_info = {};
140 $new_names_info->{'new_autogenerated_names'} = \
@new_autogenerated_names;
141 $new_names_info->{'new_last_serial_number'} = $new_last_serial_number;
143 return {new_names
=> $new_names_info};