Merge pull request #5205 from solgenomics/topic/generic_trial_upload
[sgn.git] / lib / SGN / View / Trial.pm
blob2dc806db673c80558f9218ecc0d8067c795cb6d8
1 package SGN::View::Trial;
2 use base 'Exporter';
3 use List::MoreUtils ':all';
4 use Data::Dumper;
5 use strict;
6 use warnings;
8 our @EXPORT_OK = qw/
9 design_layout_view
10 design_info_view
11 trial_detail_design_view
12 design_layout_map_view
14 our @EXPORT = ();
16 sub trial_detail_design_view {
17 my $design_ref = shift;
18 my %design = %{$design_ref};
19 my $design_result_html;
21 $design_result_html .= '<table border="1">';
22 $design_result_html .= qq{<tr><th>Plot Name</th><th>Accession Name</th><th>Check Name</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th></tr>};
24 foreach my $key (sort { $a <=> $b} keys %design) {
25 $design_result_html .= "<tr>";
26 if ($design{$key}->{plot_name}) {
27 $design_result_html .= "<td>".$design{$key}->{plot_name}."</td>";
28 } else {
29 $design_result_html .= "<td></td>";
31 if ($design{key}->{accession_name}) {
32 $design_result_html .= "<td>".$design{$key}->{accession_name}."</td>";
33 } else {
34 $design_result_html .= "<td></td>";
36 if ($design{key}->{check_name}) {
37 $design_result_html .= "<td>".$design{$key}->{check_name}."</td>";
38 } else {
39 $design_result_html .= "<td></td>";
41 if ($design{key}->{row_number}) {
42 $design_result_html .= "<td>".$design{$key}->{row_number}."</td>";
43 } else {
44 $design_result_html .= "<td></td>";
46 if ($design{key}->{col_number}) {
47 $design_result_html .= "<td>".$design{$key}->{col_number}."</td>";
48 } else {
49 $design_result_html .= "<td></td>";
51 if ($design{key}->{block_number}) {
52 $design_result_html .= "<td>".$design{$key}->{block_number}."</td>";
53 } else {
54 $design_result_html .= "<td></td>";
56 if ($design{key}->{block_row_number}) {
57 $design_result_html .= "<td>".$design{$key}->{block_row_number}."</td>";
58 } else {
59 $design_result_html .= "<td></td>";
61 if ($design{key}->{block_col_number}) {
62 $design_result_html .= "<td>".$design{$key}->{block_col_number}."</td>";
63 } else {
64 $design_result_html .= "<td></td>";
66 if ($design{key}->{rep_number}) {
67 $design_result_html .= "<td>".$design{$key}->{rep_number}."</td>";
68 } else {
69 $design_result_html .= "<td></td>";
71 $design_result_html .= "</tr>";
73 $design_result_html .= "</table>";
74 return "$design_result_html";
78 sub design_layout_map_view {
79 my $design_ref = shift;
80 my $design_type = shift;
81 my $result;
82 my %design = %{$design_ref};
83 my @layout_info;
84 my @row_numbers = ();
85 my @col_numbers = ();
86 my @rep_numbers = ();
87 my @block_numbers = ();
88 my @check_names = ();
90 foreach my $key (sort { $a <=> $b} keys %design) {
91 my $plot_name = $design{$key}->{plot_name} || '';
92 my $stock_name = $design{$key}->{stock_name} || '';
93 my $check_name = $design{$key}->{is_a_control} || '';
94 my $row_number = $design{$key}->{row_number} || '';
95 my $col_number = $design{$key}->{col_number} || '';
96 my $block_number = $design{$key}->{block_number} || '';
97 my $rep_number = $design{$key}->{rep_number} || '';
98 my $plot_number = $key;
100 if ($col_number) {
101 push @col_numbers, $col_number;
103 if ($row_number) {
104 push @row_numbers, $row_number;
105 }elsif (!$row_number){
106 if ($block_number && $design_type ne 'splitplot'){
107 $row_number = $block_number;
108 push @row_numbers, $row_number;
109 }elsif ($rep_number && !$block_number && $design_type ne 'splitplot'){
110 $row_number = $rep_number;
111 push @row_numbers, $row_number;
112 }elsif ($design_type eq 'splitplot'){
113 $row_number = $rep_number;
114 push @row_numbers, $row_number;
117 if ($rep_number) {
118 push @rep_numbers, $rep_number;
120 if ($block_number) {
121 push @block_numbers, $block_number;
123 if ($check_name){
124 push @check_names, $stock_name;
127 push @layout_info, {
128 plot_number => $plot_number,
129 row_number => $row_number,
130 col_number => $col_number,
131 block_number=> $block_number,
132 rep_number => $rep_number,
133 plot_name => $plot_name,
134 accession_name => $stock_name,
138 @layout_info = sort { $a->{plot_number} <=> $b->{plot_number}} @layout_info;
139 my $false_coord;
140 if (scalar(@col_numbers) < 1){
141 @col_numbers = ();
142 $false_coord = 'false_coord';
143 my @row_instances = uniq @row_numbers;
144 my %unique_row_counts;
145 $unique_row_counts{$_}++ for @row_numbers;
146 my @col_number2;
147 for my $key (keys %unique_row_counts){
148 push @col_number2, (1..$unique_row_counts{$key});
150 for (my $i=0; $i < scalar(@layout_info); $i++){
151 $layout_info[$i]->{'col_number'} = $col_number2[$i];
152 push @col_numbers, $col_number2[$i];
156 my $plot_popUp;
157 foreach my $hash (@layout_info){
158 $plot_popUp = $hash->{'plot_name'}."\nplot_No:".$hash->{'plot_number'}."\nblock_No:".$hash->{'block_number'}."\nrep_No:".$hash->{'rep_number'}."\nstock:".$hash->{'accession_name'};
159 push @$result, {plotname => $hash->{'plot_name'}, stock => $hash->{'accession_name'}, plotn => $hash->{'plot_number'}, blkn=>$hash->{'block_number'}, rep=>$hash->{'rep_number'}, row=>$hash->{'row_number'}, col=>$hash->{'col_number'}, plot_msg=>$plot_popUp} ;
162 my @sorted_block = sort@block_numbers;
163 my @uniq_block = uniq(@sorted_block);
164 @check_names = uniq(@check_names);
165 #print STDERR Dumper(\@check_names);
166 my ($min_rep, $max_rep) = minmax @rep_numbers;
167 my ($min_block, $max_block) = minmax @block_numbers;
168 my ($min_col, $max_col) = minmax @col_numbers;
169 my ($min_row, $max_row) = minmax @row_numbers;
170 my (@unique_col,@unique_row);
171 for my $x (1..$max_col){
172 push @unique_col, $x;
174 for my $y (1..$max_row){
175 push @unique_row, $y;
178 my %return = (
179 coord_row => \@row_numbers,
180 coord_col => \@col_numbers,
181 max_row => $max_row,
182 max_col => $max_col,
183 max_rep => $max_rep,
184 max_block => $max_block,
185 controls => \@check_names,
186 unique_col => \@unique_col,
187 unique_row => \@unique_row,
188 false_coord => $false_coord,
189 result => $result,
192 return \%return;
195 #For printing the table view of the generated design, splitplot design is different from the others:
196 # The splitplot generates plots, subplots, and plant entries, so the table should reflect that.
198 sub design_layout_view {
199 my $design_ref = shift;
200 my $design_info_ref = shift;
201 my $design_type = shift;
202 my $trial_stock_type = shift;
203 my %design = %{$design_ref};
204 my %design_info = %{$design_info_ref};
205 my $design_result_html;
207 $design_result_html .= '<table class="table table-bordered table-hover">';
209 if ($trial_stock_type eq 'family_name') {
210 if ($design_type eq 'greenhouse') {
211 $design_result_html .= qq{<tr><th>Plant Name</th><th>Plot Name</th><th>Family Name</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
212 } elsif ($design_type eq 'splitplot') {
213 $design_result_html .= qq{<tr><th>Plant Name</th><th>Subplot Name</th><th>Plot Name</th><th>Family Name</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
214 } else {
215 $design_result_html .= qq{<tr><th>Plot Name</th><th>Family Name</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
217 } elsif ($trial_stock_type eq 'cross') {
218 if ($design_type eq 'greenhouse') {
219 $design_result_html .= qq{<tr><th>Plant Name</th><th>Plot Name</th><th>Cross Unique ID</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
220 } elsif ($design_type eq 'splitplot') {
221 $design_result_html .= qq{<tr><th>Plant Name</th><th>Subplot Name</th><th>Plot Name</th><th>Cross Unique ID</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
222 } else {
223 $design_result_html .= qq{<tr><th>Plot Name</th><th>Cross Unique ID</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
225 } else {
226 if ($design_type eq 'greenhouse') {
227 $design_result_html .= qq{<tr><th>Plant Name</th><th>Plot Name</th><th>Accession Name</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
228 } elsif ($design_type eq 'splitplot') {
229 $design_result_html .= qq{<tr><th>Plant Name</th><th>Subplot Name</th><th>Plot Name</th><th>Accession Name</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
230 } else {
231 $design_result_html .= qq{<tr><th>Plot Name</th><th>Accession Name</th><th>Check Name</th><th>Plot Number</th><th>Row number</th><th>Col number</th><th>Block Number</th><th>Block Row Number</th><th>Block Col Number</th><th>Rep Number</th><th>Seedlot Name</th><th>Num Seeds Per Plot</th></tr>};
235 foreach my $key (sort { $a <=> $b} keys %design) {
236 if ($key eq 'treatments'){
237 next;
239 my $plot_name = $design{$key}->{plot_name} || '';
240 my $stock_name = $design{$key}->{stock_name} || '';
241 my $seedlot_name = $design{$key}->{seedlot_name} || '';
242 my $num_seed_per_plot = $design{$key}->{num_seed_per_plot} || '';
243 my $check_name = $design{$key}->{is_a_control} || '';
244 my $row_number = $design{$key}->{row_number} || '';
245 my $col_number = $design{$key}->{col_number} || '';
246 my $block_number = $design{$key}->{block_number} || '';
247 my $block_row_number = $design{$key}->{block_row_number} || '';
248 my $block_col_number = $design{$key}->{block_col_number} || '';
249 my $rep_number = $design{$key}->{rep_number} || '';
250 my $plot_number = $key;
252 if ($design{$key}->{subplots_plant_names}) {
253 foreach my $subplot_name (sort keys %{$design{$key}->{subplots_plant_names}}) {
254 my $plant_names = $design{$key}->{subplots_plant_names}->{$subplot_name};
255 foreach my $plant_name (@$plant_names){
256 $design_result_html .= "<tr><td>".$plant_name."</td><td>".$subplot_name."</td><td>".$plot_name."</td><td>".$stock_name."</td><td>".$check_name."</td><td>".$plot_number."</td><td>".$row_number."</td><td>".$col_number."</td><td>".$block_number."</td><td>".$block_row_number."</td><td>".$block_col_number."</td><td>".$rep_number."</td><td>".$seedlot_name."</td><td>".$num_seed_per_plot."</td></tr>";
259 } elsif ($design{$key}->{plant_names}) {
260 foreach my $plant_name (@{$design{$key}->{plant_names}}) {
261 $design_result_html .= "<tr><td>".$plant_name."</td><td>".$plot_name."</td><td>".$stock_name."</td><td>".$check_name."</td><td>".$plot_number."</td><td>".$row_number."</td><td>".$col_number."</td><td>".$block_number."</td><td>".$block_row_number."</td><td>".$block_col_number."</td><td>".$rep_number."</td><td>".$seedlot_name."</td><td>".$num_seed_per_plot."</td></tr>";
263 } else {
264 $design_result_html .= "<tr><td>".$plot_name."</td><td>".$stock_name."</td><td>".$check_name."</td><td>".$plot_number."</td><td>".$row_number."</td><td>".$col_number."</td><td>".$block_number."</td><td>".$block_row_number."</td><td>".$block_col_number."</td><td>".$rep_number."</td><td>".$seedlot_name."</td><td>".$num_seed_per_plot."</td></tr>";
268 $design_result_html .= "</table>";
269 return "$design_result_html";
272 sub design_info_view {
273 my $design_ref = shift;
274 my $design_info_ref = shift;
275 my $trial_stock_type = shift;
276 my %design = %{$design_ref};
277 my %design_info = %{$design_info_ref};
278 my %block_hash;
279 my %rep_hash;
280 my $design_info_html;
281 my $design_description;
284 $design_info_html .= "<dl>";
286 if ($design_info{'design_type'}) {
287 $design_description = $design_info{'design_type'};
288 if ($design_info{'design_type'} eq "CRD") {
289 $design_description = "Completely Randomized Design";
291 if ($design_info{'design_type'} eq "RCBD") {
292 $design_description = "Randomized Complete Block Design";
294 if ($design_info{'design_type'} eq "Alpha") {
295 $design_description = "Alpha Lattice Incomplete Block Design";
297 if ($design_info{'design_type'} eq "Augmented") {
298 $design_description = "Augmented Incomplete Block Design";
300 if ($design_info{'design_type'} eq "MAD") {
301 $design_description = "Modified Augmented Design";
303 if ($design_info{'design_type'} eq "greenhouse") {
304 $design_description = "Greenhouse Design";
306 # if ($design_info{'design_type'} eq "MADII") {
307 # $design_description = "Modified Augmented Design II";
309 # if ($design_info{'design_type'} eq "MADIII") {
310 # $design_description = "Modified Augmented Design III";
312 # if ($design_info{'design_type'} eq "MADIV") {
313 # $design_description = "Modified Augmented Design IV";
315 $design_info_html .= "<dt>Design type</dt><dd>".$design_description."</dd>";
317 if ($design_info{'number_of_locations'}) {
318 $design_info_html .= "<dt>Number of locations</dt><dd>".$design_info{'number_of_locations'}."</dd>";
320 if ($design_info{'number_of_stocks'}) {
321 if ($trial_stock_type eq 'family_name') {
322 $design_info_html .= "<dt>Number of family names</dt><dd>".$design_info{'number_of_stocks'}."</dd>";
323 } elsif ($trial_stock_type eq 'cross') {
324 $design_info_html .= "<dt>Number of cross unique ids</dt><dd>".$design_info{'number_of_stocks'}."</dd>";
325 } else {
326 $design_info_html .= "<dt>Number of accessions</dt><dd>".$design_info{'number_of_stocks'}."</dd>";
329 if ($design_info{'number_of_checks'}) {
330 $design_info_html .= "<dt>Number of checks</dt><dd>".$design_info{'number_of_checks'}."</dd>";
332 if ($design_info{'number_of_controls'}) {
333 $design_info_html .= "<dt>Number of controls</dt><dd>".$design_info{'number_of_controls'}."</dd>";
336 my $treatment_info_string = "";
337 foreach my $key (sort { $a <=> $b} keys %design) {
338 my $current_block_number = $design{$key}->{block_number};
339 my $current_rep_number;
340 if ($current_block_number) {
341 if ($block_hash{$current_block_number}) {
342 $block_hash{$current_block_number} += 1;
343 } else {
344 $block_hash{$current_block_number} = 1;
347 if ($design{$key}->{rep_number}) {
348 $current_rep_number = $design{$key}->{rep_number};
349 if ($rep_hash{$current_rep_number}) {
350 $rep_hash{$current_rep_number} += 1;
351 } else {
352 $rep_hash{$current_rep_number} = 1;
356 if($key eq 'treatments'){
357 while(my($k,$v) = each %{$design{$key}}){
358 my $treatment_units = join ',', @{$v};
359 $treatment_info_string .= "<b>$k:</b> $treatment_units<br/>";
364 if (%block_hash) {
365 $design_info_html .= "<dt>Number of blocks</dt><dd>".scalar(keys %block_hash)."</dd>";
366 if ($trial_stock_type eq 'family_name') {
367 $design_info_html .= "<dt>Number of family names per block</dt><dd>";
368 foreach my $key (sort { $a <=> $b} keys %block_hash) {
369 $design_info_html .= "Block ".$key.": ".$block_hash{$key}." family names <br>";
371 } elsif ($trial_stock_type eq 'cross') {
372 $design_info_html .= "<dt>Number of cross unique ids per block</dt><dd>";
373 foreach my $key (sort { $a <=> $b} keys %block_hash) {
374 $design_info_html .= "Block ".$key.": ".$block_hash{$key}." cross unique ids <br>";
376 } else {
377 $design_info_html .= "<dt>Number of accessions per block</dt><dd>";
378 foreach my $key (sort { $a <=> $b} keys %block_hash) {
379 $design_info_html .= "Block ".$key.": ".$block_hash{$key}." accessions <br>";
382 $design_info_html .= "</dt>";
386 if (%rep_hash) {
387 $design_info_html .= "<dt>Number of reps</dt><dd>".scalar(keys %rep_hash)."</dd>";
390 $design_info_html .= "<dt>Treatments:</dt><dd><div id='trial_design_confirm_treatments' >$treatment_info_string</div></dd>";
392 $design_info_html .= "</dl>";
394 return $design_info_html;
399 ######
401 ######