On Thu, May 12, 2011 at 11:23 AM, Nathalie Conte <n...@sanger.ac.uk> wrote:
HI,

I have this file format
chr    start    end    strand
x     12    24    1
x    24    48    1
1    100    124    -1
1    124    148    -1

Basically I would like to create a new file by grouping the start of the
first line (12) with the end of the second line (48) and so on
the output should look like this:
x     12    48    1
1    100    148    -1

I have this script to split and iterate over each line, but I don't know
how to group 2 lines together, and take the start of the firt line and the
end on the second line? could you please advise? thanks


use strict;
use warnings;

my %first_start;

while (<>) {
  next if /^chr/;   # skip header line
  chomp;
  my ($chr, $start, $end, $strand) = split;
  if ( exists $first_start{$chr} ) {
    print "$chr $first_start{$chr} $end $strand\n";
  }
  else {
    $first_start{$chr} = $start;
  }
}


Put this in a file, "combine-chromosomes" for example
and then:
perl combine-chromosomes infile.txt >grouped.txt


Hope it helps

  - Pete




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to