modified naming format
[sgn.git] / lib / CXGN / Transformation / CreateAutogeneratedNames.pm
blob9f4b9f5b1df40fbecce7094d3444fa5c5da72ee4
1 package CXGN::Transformation::CreateAutogeneratedNames;
3 =head1 NAME
5 CXGN::Transformation::CreateAutogeneratedNames - a module to create autogenerated names based on autogenerated name metadata.
7 =head1 USAGE
10 =head1 DESCRIPTION
13 =head1 AUTHORS
15 Titima Tantikanjana (tt15@cornell.edu)
17 =cut
19 use Moose;
20 use MooseX::FollowPBP;
21 use Moose::Util::TypeConstraints;
22 use Try::Tiny;
23 use CXGN::Stock::StockLookup;
24 use SGN::Model::Cvterm;
25 use Data::Dumper;
26 use JSON;
28 has 'schema' => (
29 is => 'rw',
30 isa => 'DBIx::Class::Schema',
31 predicate => 'has_schema',
32 required => 1,
35 has 'dbh' => (
36 is => 'rw',
37 predicate => 'has_dbh',
38 required => 1,
41 has 'breeding_program_id' => (
42 isa =>'Int',
43 is => 'rw',
44 predicate => 'has_breeding_program_id',
45 required => 1,
48 has 'name_format' => (
49 isa =>'Str',
50 is => 'rw',
51 predicate => 'has_name_format',
52 required => 1,
55 has 'number_of_names' => (
56 isa => 'Int',
57 is => 'rw',
58 predicate => 'has_number_of_names',
59 required => 1,
62 has 'source_info' => (
63 isa => 'Str',
64 is => 'rw',
65 predicate => 'has_source_info',
66 required => 1,
70 sub create_names {
72 my $self = shift;
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});
85 if (!$program) {
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;
93 } else {
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;
101 my $prefix;
102 foreach my $n (1..$number_of_names) {
103 my @all_name_components = ();
104 my $new_name = '';
105 my $incremented_number = $n + $last_serial_number;
106 foreach my $attribute (@$name_attributes) {
107 my $name_info;
108 my %text_hash = ();
109 if (ref $attribute eq ref {}) {
110 %text_hash = %{$attribute};
111 $name_info = $text_hash{'text'};
112 push @all_name_components, $name_info;
113 } else {
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};
149 #######
151 #######