This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Why is my code only printing the first position?

Hi, this is my first time posting so sorry ahead of time if I leave something out, I am writing a code to end up joining information out of two files. Right now I am trying to split the first file so I can get the IDs in the first column and the information in the 9th column. My code at the moment will only print out 1 of the entries on command and nothing is printing to my file. I can get it to print other than the first entry but it still only prints one.

Here's what I have:

#!/usr/bin/perl


if ($ARGV != 1)
{
    print "FinalProject.pl input01 output.\n";
    exit;
}
print "it is running...\n";

#variables
my $input01 = $ARGV[0];
my $output = $ARGV[1];
#my $file01 = "male.sp4";
#my $file02 = 'ps.parse';
my @PeptideValue = (); #to store value from column 9
my @ID = (); #to store all protein IDs
my @Final = (); #to save all into 


#check IDs in both files against each other
#if ID is present in both files "ER" is desingated, if not "-"

open my $infile, "<", "male.sp4" or die "Can't read from $infile: $!";
open my $outfile, ">", "OUT.txt" or die "Can't create $outfile: $!";
my (@PeptideValue, @ID);
while (<$infile>)
{
    my @Values = split(/\s+/), $infile;
    push @ID, $Values[0];
    push @PeptideValue, $Values[9];
}

open (OUT, ">@Values");
print OUT (@ID);
close (OUT);

open (OUT, ">@Values");
print OUT (@PeptideValue);
close (OUT);

print "$ID[]\n";
print "$PeptideValue[]\n";


#retrieve values in column 9
#open (File01, $file01);
#$file01 = <File01>;



close $infile;

my first file looks like:

PisGene40001               0.142  21  0.193  21  0.548   4  0.256   0.227 N  0.450      SignalP-noTM
PisGene40002               0.135  14  0.126  14  0.153  12  0.121   0.124 N  0.450      SignalP-noTM
PisGene40003               0.134  52  0.158  17  0.357   6  0.226   0.185 N  0.500      SignalP-TM
PisGene40004               0.478  30  0.642  30  0.984  17  0.879   0.770 Y  0.450      SignalP-noTM
PisGene40005               0.115  17  0.109   1  0.116   1  0.000   0.050 N  0.450      SignalP-noTM

my output looks like:

PisGene40001
N

Why is it only printing the first one? Thanks in advance for help!

perl homework
my @Values = split(/\s+/), $infile;

this doesn't look right in a couple ways. can you change it to:

my @Values = split(/\s+/, $_)

when I changed that it printed out "FinalProject.pl input01 output."

ok this script has a few more problems. kind of looks like you are not entirely understanding filehandles. is this a homework assignment or your day job?

1 answer

ok try this

#!/usr/bin/perl

if (scalar(@ARGV) != 2)
{
    print "FinalProject.pl input01 output.\n";
    exit;
}

my $input_filename = $ARGV[0];
my $output_filename = $ARGV[1];

my @PeptideValues = (); #to store value from column 9
my @IDs = (); #to store all protein IDs
my @Finals = (); #to save all into 

open(IN_FILEHANDLE, "<$input_filename") or die "Can't read from $input_filename: $!";
open(OUT_FILEHANDLE, ">$output_filename") or die "Can't create filehandle from $output_filename: $!";

while (<IN_FILEHANDLE>)
{
    my @Values = split(/\s+/, $_);
    push @IDs, $Values[0];
    push @PeptideValues, $Values[9];
    push @Finals,@Values;
}
close IN_FILEHANDLE; 

for $ID(@IDs){
        print OUT_FILEHANDLE ($ID."\n");
}
for $peptideValue(@PeptideValues){
        print OUT_FILEHANDLE ($peptideValue."\n");
}
close OUT_FILEHANDLE;

Log in to answer this question.