AG87 has asked for the wisdom of the Perl Monks concerning the following question:
I have a little bit problem in my code which I am unable to sort out. My input file is
536 ENERGY = -176.2 proMy code is
$data_file="HIVgag.ct"; open(MYFILE, $data_file) || die("Could not open file!"); $b=$between=$energy=0; while (<MYFILE>) { if (/energy/i) { $energy++; $between=$b; } elsif ($energy) {$b++} } print "$energy / $between\n"; close(MYFILE); open(MYFILE, $data_file) || die("Could not open file!"); open(WRITE, ">final.net"); print WRITE "*vertices $between\n"; while(<MYFILE>) { foreach $row (<MYFILE>) { @row = $row; (@row[0],@row[1],@row[2], @row[3], @row[4], @row[5], @row[6])=split(/\ +s+/,$row); if (@row[2] ne ENERGY) { print WRITE "@row[1] $row[2]\n"; print"@row[1] $row[2]\n"; } } } close(MYFILE); open(MYFILE, $data_file) || die("Could not open file!"); { print WRITE "*edges\n"; my $energy; while ( <MYFILE> ) { if ( /\bENERGY\b/ ) { ++$energy; next; } my ( $key, @fields ) = grep $_, split; for my $field ( @fields ) { if ( $field ne 'G' && $field ne 'A' && $field ne 'C' && $field ne +'G' ) { print WRITE "$key $field $energy\n"; } } } } close(WRITE);
My code rearranges the input file in a way such that the first two columns of the file is written in *vertices portion. And in the *edges portion the 1st column of the input file makes a pair with the 3rd, 4th , 5th and 6th column omitting the pairs containing zero in 3rd, 4th, 5th and 6th column. The 3rd column of *edges consists of the number of “energy block” in which the pair exists. Thus the output file is
*vertices 3What I want is in the *vertices portion it should only read the first two columns of the first “energy block” and then it should jump to *edges portion which implies that in this case it should only print 1 G, 2 G and 3 G and then jump to *edges portion. And in *edges portion the first column of input file should not make pair with 6th (last) column. So the output should be like
*vertices 3I shall be very thankful for help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Spliting file + removing column
by tilly (Archbishop) on Jan 13, 2011 at 15:22 UTC | |
by AG87 (Acolyte) on Jan 13, 2011 at 17:24 UTC | |
by tilly (Archbishop) on Jan 13, 2011 at 17:42 UTC | |
by AG87 (Acolyte) on Jan 13, 2011 at 17:59 UTC | |
by tilly (Archbishop) on Jan 13, 2011 at 18:20 UTC | |
|
Re: Spliting file + removing column
by Anonyrnous Monk (Hermit) on Jan 13, 2011 at 11:02 UTC | |
by jwkrahn (Abbot) on Jan 13, 2011 at 11:15 UTC | |
by Anonyrnous Monk (Hermit) on Jan 13, 2011 at 12:05 UTC | |
|
Re: Spliting file + removing column
by pat_mc (Pilgrim) on Jan 13, 2011 at 09:12 UTC | |
by AG87 (Acolyte) on Jan 13, 2011 at 17:47 UTC | |
|
Re: Spliting file + removing column
by locked_user sundialsvc4 (Abbot) on Jan 13, 2011 at 13:08 UTC |