Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: transforming XY data to X and Multiple Y column data?

by Anonymous Monk
on Sep 26, 2010 at 19:47 UTC ( #862115=note: print w/replies, xml ) Need Help??

in reply to transforming XY data to X and Multiple Y column data?

  1. show your effort
  • Comment on Re: transforming XY data to X and Multiple Y column data?

Replies are listed 'Best First'.
Re^2: transforming XY data to X and Multiple Y column data?
by ihperlbeg (Novice) on Sep 27, 2010 at 01:00 UTC

    here is what I have and as I said it didn't work with the data format I am working with:

    #!/usr/bin/perl use strict; use integer; my $input = shift @ARGV || 'Data.txt'; my $output = shift @ARGV || 'Output.txt'; print $input, "\n"; open(DATA,"$input") || die "cannot open $input for reading"; open(OUT, ">$output") || die "cannot open $output for writing" +; my @newcols=(); my ($genome, $id, $abd); my %phage=(); my @pg=(); my @id=(); while (my $line=<DATA>){ chomp $line; #$line =~ s/"//g; if($line =~ m/#/){ $genome = $line; push(@pg, $genome);} elsif($line !=~ m/#/){ my @cols = split(/\t/, $line); $id = $cols[0]; $abd = $cols[1]; push(@id, $id); $phage{$id}{$genome}=$abd;} #print OUT "$genome $id, $abd\n"; } my %hash = map { $_ => 1 } @id; my @unique = keys %hash; my @sorted_id = sort { ( $a <=> $b) } @unique; print OUT "\t"; for my $phagegenome(@pg){ print OUT $phagegenome, "\t"; } print OUT "\n"; for my $sorted_id(@sorted_id){ print OUT $sorted_id, "\t"; for my $pg(@pg){ print OUT "$phage{$sorted_id}{$pg}\t"; } print OUT "\n"; }
      In the OP, you say:

      I tried using hashes, but didn't seem to work as the first column has duplicates.

      But just above that, you show lines that you are "trying to output" and those have duplicates in the first column. Either you actually do want some duplicates (apparently, when the same "column 1" value appears more than once under a single "id" value), or else you made a mistake in showing us what you are "trying to output".

      Apart from that, I have a question about your input data. Your code assumes that the columns in your "Data.txt" file are separated by tabs, but the sample data you posted appears to be separated by spaces.

      On top of that, I have to complain about your strange use of indentation. It runs, but it's hard to read.

      The output I got using the OP data (with spaces instead of tabs) looked like this:

      #id1 #id2 #id3 0 0 1 90 1 70 2 80 2 40 3 70 3 20 4 5

      First suggestion: since you are using a data structure, try using Data::Dumper together with the perl debugger perldebug -- e.g. you use the debugger "b" command to set a break-point at a specific line (like, inside a "for" loop), use the "c" command to continue execution (which will then stop at the break-point), and use a command like "p Dumper(\%phage)" to see what your data structure contains at that point.

      When I did that on (a properly indented version of) your code, I noticed the "tab-vs-space" issue, changed the "split" regex, and got something closer to what you seem to want (updated to convert tabs to spaces):

      #id1 #id2 #id3 0 0 1 90 70 2 80 40 3 70 20 4 5
      Of course, this does not have any repeated values in the first column, which makes it different from your OP output sample. So... do you really want some things repeated and not others?

      BTW, here's a cleaned-up version of your code, fixing the indentation and the split regex, adding Data::Dumper, moving some uses of "my" into their proper scope, and simplifying a few other things (also removed "use integer" -- it would have no effect on the code as-is, and if you ever do division, it's better to use the "int()" function as needed):

      There's still room for improvement... (especially if you really want it to do something different).
      Here is my version, reads from __DATA__ if there is no 'Data.txt' output seems to be what you want (also included debugging DDS Dump )

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://862115]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2022-01-24 04:44 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.