Nathalie Conte wrote:
HI,
Hello,
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
unless (open(FH, $file)){
print "Cannot open file \"$file\"\n\n";
}
What you are saying is "if the file doesn't open print a message but use
the filehandle anyway".
You should not try to use an invalid filehandle. The usual way to exit
the program if open fails:
open FH, '<', $file or die qq[Cannot open file "$file" because: $!\n\n];
my @list = <FH>;
Is there a good reason to read the entire file into memory instead of
just processing one line at a time?
close FH;
open(OUTFILE, ">grouped.txt");
You should always verify that the file opened correctly:
open OUTFILE, '>', 'grouped.txt' or die "Cannot open 'grouped.txt'
because: $!";
foreach my $line(@list){
chomp $line;
my @coordinates = split(/' '/, $region);
Your regular expression says to match a single quote character followed
by a space character followed by a single quote character. It looks
like you meant either:
my @coordinates = split ' ', $region;
Or:
my @coordinates = split /\s+/, $region;
Or possibly:
my @coordinates = split / +/, $region;
The first two would mean that the chomp() on the previous line is redundant.
my $chromosome = $coordinates[0];
my $start = $coordinates[1];
my $end = $coordinates[2];
my $strand = $coordinates[3];
There is no reason for the array @coordinates:
my ( $chromosome, $start, $end, $strand ) = split ' ', $region;
...???
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/