add mixed model tools to new branch (excessive conflicts with old branch).
[sgn.git] / lib / CXGN / Trial / TrialDesign.pm
blob9b4e96f6f64aa50e401d81d1db6eac0204767d91
1 package CXGN::Trial::TrialDesign;
3 =head1 NAME
5 CXGN::Trial::TrialDesign - a module to create a trial design using the R CRAN package Agricolae.
7 =head1 USAGE
9 my $trial_design = CXGN::Trial::TrialDesign->new();
10 $trial_design->set_trial_name("blabla");
11 $trial_design->set_stock_list( qw | A B C D |);
12 $trial_design->set_seedlot_hash(\%seedlothash);
13 $trial_design->set_control_list( qw | E F |);
14 $trial_design->set_number_of_blocks(3);
15 $trial_design->set_randomization_method("RCBD");
16 if ($trial_design->calculate_design()) { # true if no error
17 $design = $trial_design->get_design();
20 =head1 DESCRIPTION
22 This module uses the the R CRAN package "Agricolae" to calculate experimental designs for field layouts.
24 =head1 AUTHORS
26 Jeremy D. Edwards (jde22@cornell.edu)
27 Aimin Yan (ay247@cornell.edu)
29 =cut
31 use Moose;
32 use MooseX::FollowPBP;
33 use Moose::Util::TypeConstraints;
34 use Data::Dumper;
35 use R::YapRI::Base;
36 use R::YapRI::Data::Matrix;
37 use POSIX;
38 use List::Util 'max';
40 with 'MooseX::Object::Pluggable';
42 has 'trial_name' => (isa => 'Str', is => 'rw', predicate => 'has_trial_name', clearer => 'clear_trial_name');
44 has 'stock_list' => (isa => 'ArrayRef[Str]', is => 'rw', predicate => 'has_stock_list', clearer => 'clear_stock_list');
46 has 'seedlot_hash' => (isa => 'HashRef', is => 'rw', predicate => 'has_seedlot_hash', clearer => 'clear_seedlot_hash');
48 has 'control_list' => (isa => 'ArrayRef[Str]', is => 'rw', predicate => 'has_control_list', clearer => 'clear_control_list');
50 has 'control_list_crbd' => (isa => 'ArrayRef[Str]', is => 'rw', predicate => 'has_control_list_crbd', clearer => 'clear_control_list_crbd');
52 has 'number_of_blocks' => (isa => 'Int', is => 'rw', predicate => 'has_number_of_blocks', clearer => 'clear_number_of_blocks');
54 has 'block_row_numbers' => (isa => 'Int', is => 'rw', predicate => 'has_block_row_numbers', clearer => 'clear_block_row_numbers');
56 has 'block_col_numbers' => (isa => 'Int', is => 'rw', predicate => 'has_block_col_numbers', clearer => 'clear_block_col_numbers');
58 has 'number_of_rows' => (isa => 'Int',is => 'rw',predicate => 'has_number_of_rows',clearer => 'clear_number_of_rows');
60 has 'number_of_cols' => (isa => 'Int',is => 'rw',predicate => 'has_number_of_cols',clearer => 'clear_number_of_cols');
62 has 'number_of_reps' => (isa => 'Int', is => 'rw', predicate => 'has_number_of_reps', clearer => 'clear_number_of_reps');
64 has 'block_size' => (isa => 'Int', is => 'rw', predicate => 'has_block_size', clearer => 'clear_block_size');
66 has 'greenhouse_num_plants' => (isa => 'ArrayRef[Int]', is => 'rw', predicate => 'has_greenhouse_num_plants', clearer => 'clear_greenhouse_num_plants');
68 has 'maximum_block_size' => (isa => 'Int', is => 'rw', predicate => 'has_maximum_block_size', clearer => 'clear_maximum_block_size');
70 has 'plot_name_prefix' => (isa => 'Str', is => 'rw', predicate => 'has_plot_name_prefix', clearer => 'clear_plot_name_prefix');
72 has 'plot_name_suffix' => (isa => 'Str', is => 'rw', predicate => 'has_plot_name_suffix', clearer => 'clear_plot_name_suffix');
74 has 'plot_start_number' => (isa => 'Int', is => 'rw', predicate => 'has_plot_start_number', clearer => 'clear_plot_start_number', default => 1);
76 has 'plot_number_increment' => (isa => 'Int', is => 'rw', predicate => 'has_plot_number_increment', clearer => 'clear_plot_number_increment', default => 1);
78 has 'randomization_seed' => (isa => 'Int', is => 'rw', predicate => 'has_randomization_seed', clearer => 'clear_randomization_seed');
80 has 'blank' => ( isa => 'Str', is => 'rw', predicate=> 'has_blank' );
82 has 'fieldmap_col_number' => (isa => 'Int',is => 'rw',predicate => 'has_fieldmap_col_number',clearer => 'clear_fieldmap_col_number');
84 has 'fieldmap_row_number' => (isa => 'Int',is => 'rw',predicate => 'has_fieldmap_row_number',clearer => 'clear_fieldmap_row_number');
86 has 'plot_layout_format' => (isa => 'Str', is => 'rw', predicate => 'has_plot_layout_format', clearer => 'clear_plot_layout_format');
88 has 'treatments' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_treatments', clearer => 'clear_treatments');
90 has 'num_plants_per_plot' => (isa => 'Int',is => 'rw',predicate => 'has_num_plants_per_plot',clearer => 'clear_num_plants_per_plot');
92 has 'num_seed_per_plot' => (isa => 'Int',is => 'rw',predicate => 'has_num_seed_per_plot',clearer => 'clear_num_seed_per_plot');
94 has 'replicated_accession_no' => (isa => 'Int',is => 'rw',predicate => 'has_replicated_accession_no',clearer => 'clear_replicated_accession_no');
96 has 'unreplicated_accession_no' => (isa => 'Int',is => 'rw',predicate => 'has_unreplicated_accession_no',clearer => 'clear_unreplicated_accession_no');
98 has 'num_of_replicated_times' => (isa => 'Int',is => 'rw',predicate => 'has_num_of_replicated_times',clearer => 'clear_num_of_replicated_times');
100 has 'sub_block_sequence' => (isa => 'Str', is => 'rw', predicate => 'has_sub_block_sequence', clearer => 'clear_sub_block_sequence');
102 has 'block_sequence' => (isa => 'Str', is => 'rw', predicate => 'has_block_sequence', clearer => 'clear_block_sequence');
104 has 'col_in_design_number' => (isa => 'Int',is => 'rw',predicate => 'has_col_in_design_number',clearer => 'clear_col_in_design_number');
106 has 'row_in_design_number' => (isa => 'Int',is => 'rw',predicate => 'has_row_in_design_number',clearer => 'clear_row_in_design_number');
108 has 'westcott_col' => (isa => 'Int',is => 'rw',predicate => 'has_westcott_col',clearer => 'clear_westcott_col');
110 has 'westcott_col_between_check' => (isa => 'Int',is => 'rw',predicate => 'has_westcott_col_between_check',clearer => 'clear_westcott_col_between_check');
112 has 'westcott_check_1' => (isa => 'Str',is => 'rw',predicate => 'has_westcott_check_1',clearer => 'clear_westcott_check_1');
114 has 'westcott_check_2' => (isa => 'Str',is => 'rw',predicate => 'has_westcott_check_2',clearer => 'clear_westcott_check_2');
116 subtype 'RandomizationMethodType',
117 as 'Str',
118 where { $_ eq "Wichmann-Hill" || $_ eq "Marsaglia-Multicarry" || $_ eq "Super-Duper" || $_ eq "Mersenne-Twister" || $_ eq "Knuth-
119 TAOCP" || $_ eq "Knuth-TAOCP-2002"},
120 message { "The string, $_, was not a valid randomization method"};
122 has 'randomization_method' => (isa => 'RandomizationMethodType', is => 'rw', default=> "Mersenne-Twister");
124 subtype 'DesignType',
125 as 'Str',
126 where { $_ eq "CRD" || $_ eq "RCBD" || $_ eq "Alpha" || $_ eq "Lattice" || $_ eq "Augmented" || $_ eq "MAD" || $_ eq "genotyping_plate" || $_ eq "greenhouse" || $_ eq "p-rep" || $_ eq "splitplot" || $_ eq "westcott" },
127 message { "The string, $_, was not a valid design type" };
129 has 'design_type' => (isa => 'DesignType', is => 'rw', predicate => 'has_design_type', clearer => 'clear_design_type');
131 my $design;
133 sub get_design {
134 print STDERR Dumper $design;
135 return $design;
139 sub calculate_design {
140 my $self = shift;
142 my $design;
144 if ($self->has_design_type()) {
145 my $design_type = $self->get_design_type();
146 if ($design_type eq "p-rep") { $design_type="Prep"; }
147 print STDERR "DESIGN TYPE = ".$design_type."\n";
148 $self->load_plugin($design_type);
149 $design = $self->create_design();
152 if ($design) {
153 return 1;
155 else {
156 return 0;
160 sub isint{
161 my $val = shift;
162 return ($val =~ m/^\d+$/);
166 sub _validate_field_colNumber {
167 my $colNum = shift;
168 if (isint($colNum)){
169 return $colNum;
170 } else {
171 die "Choose a different row number for field map generation. The product of number of accessions and rep when divided by row number should give an integer\n";
172 return;
177 sub _convert_plot_numbers {
178 my $self = shift;
179 my $plot_numbers_ref = shift;
180 my $rep_numbers_ref = shift;
181 my $number_of_reps = shift;
182 my @plot_numbers = @{$plot_numbers_ref};
183 my @rep_numbers = @{$rep_numbers_ref};
184 my $total_plot_count = scalar(@plot_numbers);
185 my $rep_plot_count = $total_plot_count / $number_of_reps;
186 for (my $i = 0; $i < scalar(@plot_numbers); $i++) {
187 my $plot_number;
188 my $first_plot_number;
189 if($self->has_plot_start_number || $self->has_plot_number_increment){
190 if ($self->has_plot_start_number()){
191 $first_plot_number = $self->get_plot_start_number();
192 } else {
193 $first_plot_number = 1;
195 if ($self->has_plot_number_increment()){
196 $plot_number = $first_plot_number + ($i * $self->get_plot_number_increment());
199 my $cheking = ($rep_numbers[$i] * $rep_plot_count) / $rep_plot_count;
200 #print STDERR Dumper($cheking);
201 my $new_plot;
202 if ($cheking != 1){
203 if (length($first_plot_number) == 3 ){
204 $new_plot = $cheking * 100;
205 $plot_number = ($i * $self->get_plot_number_increment()) + $new_plot - (($cheking -1) * $rep_plot_count) + 1;
207 #print STDERR Dumper($new_plot);
208 if (length($first_plot_number) == 4 ){
209 $new_plot = $cheking * 1000;
210 $plot_number = ($i * $self->get_plot_number_increment()) + $new_plot - (($cheking -1) * $rep_plot_count) + 1;
215 else {
216 $plot_number = $first_plot_number + $i;
219 else {
220 $plot_number = $plot_numbers[$i];
222 $plot_numbers[$i] = $plot_number;
224 return \@plot_numbers;
227 # the function below should be split up and moved to the relevant plugin...
229 sub _build_plot_names {
230 my $self = shift;
231 my $design_ref = shift;
232 my %design = %{$design_ref};
233 my $prefix = '';
234 my $suffix = '';
235 my $trial_name = $self->get_trial_name;
237 if ($self->has_plot_name_prefix()) {
238 $prefix = $self->get_plot_name_prefix()."_";
240 if ($self->has_plot_name_suffix()) {
241 $suffix = $self->get_plot_name_suffix();
244 foreach my $key (keys %design) {
245 $trial_name ||="";
246 my $block_number = $design{$key}->{block_number};
247 my $stock_name = $design{$key}->{stock_name};
248 my $rep_number = $design{$key}->{rep_number};
249 $design{$key}->{plot_number} = $key;
251 if ($self->get_design_type() eq "RCBD") { # as requested by IITA (Prasad)
252 my $plot_num_per_block = $design{$key}->{plot_num_per_block};
253 $design{$key}->{plot_number} = $design{$key}->{plot_num_per_block};
254 #$design{$key}->{plot_name} = $prefix.$trial_name."_rep_".$rep_number."_".$stock_name."_".$block_number."_".$plot_num_per_block."".$suffix;
255 $design{$key}->{plot_name} = $prefix.$trial_name."_rep".$rep_number."_".$stock_name."_".$plot_num_per_block."".$suffix;
257 elsif ($self->get_design_type() eq "Augmented") {
258 $design{$key}->{plot_name} = $prefix.$trial_name."_plotno".$key."_".$stock_name."_".$suffix;
260 elsif ($self->get_design_type() eq "greenhouse") {
261 $design{$key}->{plot_name} = $prefix.$trial_name."_".$stock_name."_".$key.$suffix;
263 else {
264 $design{$key}->{plot_name} = $prefix.$trial_name."_".$key.$suffix;
267 if($design{$key}->{subplots_names}){
268 my $nums = $design{$key}->{subplots_names};
269 my @named_subplots;
270 foreach (@$nums){
271 push @named_subplots, $design{$key}->{plot_name}."_subplot_".$_;
273 $design{$key}->{subplots_names} = \@named_subplots;
277 #print STDERR Dumper(\%design);
279 return \%design;
282 sub _check_controls_and_accessions_lists {
283 my $self = shift;
284 my @stock_list = $self->get_stock_list() ? @{$self->get_stock_list()} : ();
285 my @control_list_crbd = $self->get_control_list_crbd() ? @{$self->get_control_list_crbd()} : ();
286 my %control_names_lookup = map { $_ => 1 } @control_list_crbd;
287 foreach my $stock_name_iter (@stock_list) {
288 if (exists($control_names_lookup{$stock_name_iter})) {
289 #die "Names in accessions list cannot be used also as controls. Please use separate lists for your controls and your accessions. The following accession is in both lists and is a problem: $stock_name_iter\n";