sramazan has asked for the wisdom of the Perl Monks concerning the following question:

Hello I am new to perl monks and I am using GD::Graph::lines to create a plot but for some reason I am getting an error "Invalid data set: 0". I can't seem to figure out the reason why I am getting this error. Below is my code, I know there are some initialized variables which haven't been used yet. I am working on creating the graph for "R1". Once that works I'll just copy and paste to create it for "R2"

#!/usr/bin/perl -w + + use strict; use warnings; use Data::Dumper; use Cwd; use GD::Graph::lines; use GD::Graph::colour; GD::Graph::lines->import(); GD::Graph::colour->import(qw(:colours :lists :files :convert)); my $dir = getcwd(); my $meth = 0; my $unmeth = 0; my $meth_perc = 0; my @data_R1 = (); my @data_R2 = (); my %sum_R1 = (); my %sum_R2 = (); my $tmp = $ENV{TMP}; my @graph_R1 = (); my @graph_R2 = (); #print $tmp."\n"; + + opendir(DIR,$dir) or die "Cannot open directory path $dir\n"; my @mbias = grep(/\.M-bias.txt$/,readdir(DIR)); closedir(DIR); for(my $i=0;$i<scalar(@mbias);$i++){ open (FILE,$mbias[$i]) or die "Could not open: $!"; open (IN,">".$tmp.($i+1)."_R1.txt") or die "Could not write to: $! +"; while (<FILE>) { push @data_R1, $_ if (/^CpG context \Q(R1)/ .. /^125/); } for(my $j=3;$j<scalar(@data_R1);$j++){ print IN $data_R1[$j]; } close (IN); @data_R1 = (); } opendir(TMP,$tmp) or die "Cannot open directory path $tmp\n"; my @R1 = grep(/\_R1.txt$/,readdir(TMP)); for(my $i=0;$i<125;$i++){ push (@{$sum_R1{$i+1}},($i+1),0,0,0); } for(my $i=0;$i<scalar(@R1);$i++){ open(FILE,$tmp."$R1[$i]") or die "Could not open: $!"; my @file = <FILE>; chomp @file; foreach my $key(keys %sum_R1){ for(my $j=0;$j<scalar(@file);$j++){ my @split = split("\t",$file[$j]); $meth = $split[1]; $unmeth = $split[2]; if($key == $split[0]){ @{$sum_R1{$key}}[1] += $meth; @{$sum_R1{$key}}[2] += $unmeth; } } $meth=0; $unmeth=0; } close (FILE); } foreach my $key(keys %sum_R1){ $meth = @{$sum_R1{$key}}[1]; $unmeth = @{$sum_R1{$key}}[2]; $meth_perc = 100*($meth/($meth + $unmeth)); @{$sum_R1{$key}}[3] = $meth_perc; } foreach my $key(sort {$a <=> $b} keys %sum_R1){ push @graph_R1, @{$sum_R1{$key}}[3]; } my $graph1 = GD::Graph::lines->new(800,600); add_colour(nice_blue => [31,120,180]); $graph1->set( x_label => 'position (bp)', y1_label => '% methylation', y2_label => '# methylation calls', title => 'Mbias plot Read 1', line_width => 2, x_max_value => 125, x_min_value => 0, y_tick_number => 10, y_label_skip => 2, y1_max_value => 100, y1_min_value => 0, y_label_skip => 2, y2_min_value => 0, x_label_skip => 5, x_label_position => 0.5, x_tick_offset => -1, bgclr => 'white', transparent => 0, two_axes => 1, use_axis => [1,1,1,2,2,2], legend_placement => 'RC', legend_spacing => 6, legend_marker_width => 24, legend_marker_height => 18, dclrs => [ qw(nice_blue) ], )or die $graph1->error; $graph1->set_legend('CpG methylation'); my $gd = $graph1->plot(\@graph_R1) or die $graph1->error; open(IMG, '>test.png') or die $!; binmode IMG; print IMG $graph1->png;

I hope I was thorough enough in explaining my problem. Thanks in advance.

Replies are listed 'Best First'.
Re: GD graph Invalid data set
by poj (Abbot) on Jan 16, 2015 at 20:14 UTC

    print IMG $graph1->png; should be
    print IMG $gd->png;

    You didn't provide any data so I made some up to get a working program :-

    #!/usr/bin/perl use strict; use warnings; use GD::Graph::lines; use GD::Graph::colour; GD::Graph::lines->import(); GD::Graph::colour->import(qw(:colours :lists :files :convert)); my @graph_R1 = ([1..50], [1..50], [11..60], [21..70]); my $graph1 = GD::Graph::lines->new(800,600); add_colour(nice_blue => [31,120,180]); $graph1->set( x_label => 'position (bp)', y1_label => '% methylation', y2_label => '# methylation calls', title => 'Mbias plot Read 1', line_width => 2, x_max_value => 125, x_min_value => 0, y_tick_number => 10, y_label_skip => 2, y1_max_value => 100, y1_min_value => 0, y_label_skip => 2, y2_min_value => 0, x_label_skip => 5, x_label_position => 0.5, x_tick_offset => -1, bgclr => 'white', transparent => 0, two_axes => 1, use_axis => [1,1,1,2,2,2], legend_placement => 'RC', legend_spacing => 6, legend_marker_width => 24, legend_marker_height => 18, dclrs => [ qw(nice_blue) ], )or die $graph1->error; $graph1->set_legend('CpG methylation'); my $gd = $graph1->plot(\@graph_R1) or die $graph1->error; open(IMG, '>','test.png') or die $!; binmode IMG; print IMG $gd->png;
    poj

      My apologizes I knew I was missing something. In my @graph_R1 array all I have are integers Ex: my @graph_R1 = (1....125); My array is not initialized the way you have in the above code. Also is that a complex data structure with array of arrays? Thanks.

        You need to show some data and explain what sort of graph you want.

        From this use_axis => [1,1,1,2,2,2], it appears you want 6 plots. If so you need a more complex structure than a single array.

        poj
Re: GD graph Invalid data set
by Util (Priest) on Jan 16, 2015 at 19:01 UTC
    Your code fails to compile, because the non-lexical filehandles are forbidden by use strict.
    1. Would you please post the *actual* code?
    2. Would you please provide a minimal data set that produces the problem?
    When we try to help with this kind of problem, we need to be able to *recreate* the problem.

      My apologizes I knew I was missing something. In my @graph_R1 array all I have are integers Ex: my @graph_R1 = (1....125); Thanks.