add blocksdesign R code
[sgn.git] / lib / CXGN / Trial / TrialDesign / Plugin / RowColumn.pm
blob15049d8e12c7e04b73a9a9ec71b8f303ef07bd04
2 package CXGN::Trial::TrialDesign::Plugin::RCD;
4 use Moose::Role;
6 sub create_design {
7 my $self = shift;
8 my %rcd_design;
9 my $rbase = R::YapRI::Base->new();
10 my @stock_list;
11 my $number_of_blocks;
12 my $stock_data_matrix;
13 my $control_data_matrix;
14 my $r_block;
15 my $result_matrix;
16 my @plot_numbers;
17 my @stock_names;
18 my @control_names_crbd;
19 my @block_numbers;
20 my @converted_plot_numbers;
21 my @control_list;
22 my %control_names_lookup;
23 my $fieldmap_row_number;
24 my @fieldmap_row_numbers;
25 my $fieldmap_col_number;
26 my $plot_layout_format;
27 my @col_number_fieldmaps;
29 if ($self->has_stock_list()) {
30 @stock_list = @{$self->get_stock_list()};
31 } else {
32 die "No stock list specified\n";
34 if ($self->has_control_list()) {
35 @control_list = @{$self->get_control_list()};
36 %control_names_lookup = map { $_ => 1 } @control_list;
37 $self->_check_controls_and_accessions_lists;
39 if ($self->has_number_of_blocks()) {
40 $number_of_blocks = $self->get_number_of_blocks();
41 } else {
42 die "Number of blocks not specified\n";
45 if ($self->has_fieldmap_col_number()) {
46 $fieldmap_col_number = $self->get_fieldmap_col_number();
48 if ($self->has_fieldmap_row_number()) {
49 $fieldmap_row_number = $self->get_fieldmap_row_number();
50 my $colNumber = ((scalar(@stock_list) * $number_of_blocks)/$fieldmap_row_number);
51 $fieldmap_col_number = CXGN::Trial::TrialDesign::validate_field_colNumber($colNumber);
53 if ($self->has_plot_layout_format()) {
54 $plot_layout_format = $self->get_plot_layout_format();
57 $stock_data_matrix = R::YapRI::Data::Matrix->new(
59 name => 'stock_data_matrix',
60 rown => 1,
61 coln => scalar(@stock_list),
62 data => \@stock_list,
66 $control_data_matrix = R::YapRI::Data::Matrix->new(
68 name => 'control_data_matrix',
69 rown => 1,
70 coln => scalar(@control_list),
71 data => \@control_list,
76 my $plot_start = $self->get_plot_start_number();
77 my $serie;
78 if($plot_start == 1){
79 $serie = 1;
80 }elsif($plot_start == 101){
81 $serie = 2;
82 }elsif($plot_start == 1001){
83 $serie = 3;
86 $r_block = $rbase->create_block('r_block');
87 $stock_data_matrix->send_rbase($rbase, 'r_block');
88 $control_data_matrix->send_rbase($rbase, 'r_block');
89 $r_block->add_command('library(agricolae)');
90 $r_block->add_command('library(blocksdesign)');
91 $r_block->add_command('treatments <- stock_data_matrix[1,]');
92 $r_block->add_command('controls <- control_data_matrix[1,]');
93 $r_block->add_command('number_of_blocks <- '.$number_of_blocks);
94 $r_block->add_command('number_of_rows <- '.$fieldmap_row_number);
95 $r_block->add_command('number_of_cols <- '.$fieldmap_col_number);
97 $r_block->add_command('RCDblocks <- data.frame(
98 block = gl(number_of_blocks,length(treatments)),
99 row = gl(number_of_rows,1),
100 col = gl(number_of_cols,number_of_rows)
101 )');
102 $r_block->add_command('RCD <- design(treatments, RCDblocks)$Design');
103 # $r_block->add_command('RCD <- transform(RCD, is_a_control = ifelse(RCD$treatments %in% controls, TRUE, FALSE))');
105 $r_block->run_block();
106 $result_matrix = R::YapRI::Data::Matrix->read_rbase( $rbase,'r_block','RCD');
107 print STDERR Dumper $result_matrix;
108 @plot_numbers = $result_matrix->get_column("plots");
109 print STDERR Dumper \@plot_numbers;
110 @block_numbers = $result_matrix->get_column("block");
111 @stock_names = $result_matrix->get_column("treatments");
112 @fieldmap_row_numbers = $result_matrix->get_column("row");
113 @col_number_fieldmaps = $result_matrix->get_column("col");
115 # alter plot numbers for serpentine or custom start number
117 my %seedlot_hash;
118 if($self->get_seedlot_hash){
119 %seedlot_hash = %{$self->get_seedlot_hash};
121 for (my $i = 0; $i < scalar(@plot_numbers); $i++) {
122 my %plot_info;
123 $plot_info{'stock_name'} = $stock_names[$i];
124 $plot_info{'seedlot_name'} = $seedlot_hash{$stock_names[$i]}->[0];
125 if ($plot_info{'seedlot_name'}){
126 $plot_info{'num_seed_per_plot'} = $self->get_num_seed_per_plot;
128 $plot_info{'block_number'} = $block_numbers[$i];
129 $plot_info{'plot_name'} = $plot_numbers[$i];
130 $plot_info{'rep_number'} = $block_numbers[$i];
131 #$plot_info{'plot_num_per_block'} = $plot_numbers[$i];
132 $plot_info{'plot_number'} = $plot_numbers[$i];
133 $plot_info{'plot_num_per_block'} = $plot_numbers[$i];
134 $plot_info{'is_a_control'} = exists($control_names_lookup{$stock_names[$i]});
135 #$plot_info_per_block{}
136 if ($fieldmap_row_numbers[$i]){
137 $plot_info{'row_number'} = $fieldmap_row_numbers[$i];
138 $plot_info{'col_number'} = $col_number_fieldmaps[$i];
140 $rcd_design{$plot_numbers[$i]} = \%plot_info;
142 %rcd_design = %{$self->_build_plot_names(\%rcd_design)};
143 return \%rcd_design;