Add dereferencing to avoid sort keys blessed hash exception
[sgn.git] / lib / CXGN / Dataset / File.pm
bloba3f5293c5cc935e2d8b2cdd5915ab022b2dfa8aa
2 package CXGN::Dataset::File;
4 use Moose;
5 use File::Slurp qw | write_file |;
6 use JSON::Any;
8 extends 'CXGN::Dataset';
10 has 'file_name' => ( isa => 'Str',
11 is => 'rw',
12 default => '/tmp/dataset_file',
15 override('retrieve_genotypes',
16 sub {
17 my $self = shift;
18 my $protocol_id = shift;
19 my $file = shift || $self->file_name()."_genotype.txt";
20 my $genotypes = $self->SUPER::retrieve_genotypes($protocol_id);
21 my $genotype_string = "";
22 my $genotype_example = $genotypes->[0];
23 foreach my $key (sort keys %{$genotype_example->{genotype_hash}}) {
24 $genotype_string .= $key."\t";
26 $genotype_string .= "\n";
27 foreach my $element (@$genotypes) {
28 my $genotype_id = $element->{germplasmDbId};
29 my $genotype_data_string = "";
30 foreach my $key (sort keys %{$element->{genotype_hash}}) {
31 my $value = $element->{genotype_hash}->{$key};
32 my $current_genotype = $value;
33 $genotype_data_string .= $current_genotype."\t";
35 my $s = join "\t", $genotype_id;
36 $genotype_string .= $s."\t".$genotype_data_string."\n";
38 write_file($file, $genotype_string);
41 # my $genotype_json = JSON::Any->encode($genotypes);
42 # write_file($file, $genotype_json);
43 return $genotypes;
44 });
46 override('retrieve_phenotypes',
47 sub {
48 my $self = shift;
49 my $file = shift || $self->file_name()."_phenotype.txt";
50 my $phenotypes = $self->SUPER::retrieve_phenotypes();
51 my $phenotype_string = "";
52 foreach my $line (@$phenotypes) {
53 my $s = join "\t", @$line;
54 $phenotype_string .= $s."\n";
56 write_file($file, $phenotype_string);
57 return $phenotypes;
58 });
60 override('retrieve_accessions',
61 sub {
62 my $self = shift;
63 my $file = shift || $self->file_name()."_accessions.txt";
64 my $accessions = $self->SUPER::retrieve_accessions();
65 my $accession_json = JSON::Any->encode($accessions);
66 write_file($file, $accession_json);
67 return $accessions;
68 });
70 override('retrieve_plots',
71 sub {
72 my $self = shift;
73 my $file = shift || $self->file_name()."_plots.txt";
74 my $plots = $self->SUPER::retrieve_plots();
75 my $plot_json = JSON::Any->encode($plots);
76 write_file($file, $plot_json);
77 return $plots;
78 });
80 override('retrieve_trials',
81 sub {
82 my $self = shift;
83 my $file = shift || $self->file_name()."_trials.txt";
84 my $trials = $self->SUPER::retrieve_trials();
85 my $trial_json = JSON::Any->encode($trials);
86 write_file($file, $trial_json);
87 return $trials;
88 });
90 override('retrieve_traits',
91 sub {
92 my $self = shift;
93 my $file = shift || $self->file_name()."_traits.txt";
94 my $traits = $self->SUPER::retrieve_traits();
95 my $trait_json = JSON::Any->encode($traits);
96 write_file($file, $trait_json);
97 return $traits;
98 });
100 override('retrieve_years',
101 sub {
102 my $self = shift;
103 my $file = shift || $self->file_name()."_years.txt";
104 my $years = $self->SUPER::retrieve_years();
105 my $year_json = JSON::Any->encode($years);
106 write_file($file, $year_json);
107 return $years;