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.


In reply to GD graph Invalid data set by sramazan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.