in reply to Confused when reading Input within Perl and Shell Scripts

If I'm reading your example code correctly, you are closer than you think. When you say:

# EXAMPLE OF HOW THE OBJECT IS NORMALLY DECLARED #my $dssp_obj = new Bio::Structure::SecStr::DSSP::Res('-file'=>'3bit.d +ssp');

it looks like you are saying that normally the name of the of the file (and not a file handle) needs to be passed to Bio::Structure::SecStr::DSSP::Res::new method as the -file argument.

If that's the case, then you could do the following:

my $dssp_obj = new Bio::Structure::SecStr::DSSP::Res('-file'=>$ARGV[0] +);

if the file you want to open is on the command line of your script, like your example. Now, if you wanted to replicate this for all of the files on the command line, you would do something like:

my @dssp_objs = (); foreach my $file (@ARGV) { push @dssp_objs, Bio::Structure::SecStr::DSSP::Res->new('-file'= +>$file); }

giving you an array of DSSP objects to work with.

G. Wade

Replies are listed 'Best First'.
Re^2: Confused when reading Input within Perl and Shell Scripts
by Anonymous Monk on Nov 17, 2008 at 00:09 UTC

    Ok, bear with me. I have used the suggestion:

    my $dssp_obj = new Bio::Structure::SecStr::DSSP::Res('-file'=>$ARGV[0] +);

    Which works (and I'm ignoring several filehandle warnings). Now, I have written a shell script:

    #!/bin/bash while read DSSPLine ; do echo $DSSPLine DSSP_Output.pl $DSSPLine.dssp done

    QUESTION: In each interation, the output of DSSP_Output.pl is written to a CSV file. Is there a way such that (in perl) this output is written for all 30,000 iterations to one file only?

    The very obvious answer is to use a DSSP array like you mentioned, I have tried that (in a way):

    use strict; # 'use strict' requires that you use 'my' for all local va +riables, or explicitely qualify all globals. use warnings; use Bio::Structure::SecStr::DSSP::Res; my @dssp_objs =(); foreach my $file (@ARGV) { push @dssp_objs, Bio::Structure::SecStr::DSSP::Res->new('-file'=>$file +); } foreach my $dssp_obj(@dssp_objs) { #Get PDB ID and Compound representation for each file my $pdb_id = $dssp_obj->pdbID(); print "Analysis of PDB:: ". $pdb_id. "\n"; my $cmpd = $dssp_obj->pdbCompound(); print "Representing:: ". $cmpd. "\n"; etc... }

    But when I do this, and run the commandline with a txt file of the DSSP filenames:

     DSSP_Output.pl DSSP_codes.txt

    I get an exception!

    Please let me know what you think...apologies for all the headache...

    A <slowly deconfusing> InfoSeeker

      The approach I gave would have worked if you had all of the list of filenames on the command line. If you want to take the list of filenames from a file, then we need to modify the input loop somewhat:

      my @dssp_objs =(); #reads the files from the command line one line at a time. while(<>) { chomp; # remove the newline. push @dssp_objs, Bio::Structure::SecStr::DSSP::Res->new('-file'=>$_); }

      This code would read a line at a time out of DSSP_codes.txt (using your example).

      I changed to the while(<>) loop just to be consistent with the way most people do this kind of loop. Since it automatically loads the $_ variable (and the loop is pretty short), I removed the $file variable. Otherwise, this is a drop-in replacement for the previous loop that should meet your needs.

      G. Wade
Re^2: Confused when reading Input within Perl and Shell Scripts
by Anonymous Monk on Nov 17, 2008 at 00:09 UTC

    Ok, bear with me. I have used the suggestion:

    my $dssp_obj = new Bio::Structure::SecStr::DSSP::Res('-file'=>$ARGV[0] +);

    Which works (and I'm ignoring several filehandle warnings). Now, I have written a shell script:

    #!/bin/bash while read DSSPLine ; do echo $DSSPLine DSSP_Output.pl $DSSPLine.dssp done

    QUESTION: In each interation, the output of DSSP_Output.pl is written to a CSV file. Is there a way such that (in perl) this output is written for all 30,000 iterations to one file only?

    The very obvious answer is to use a DSSP array like you mentioned, I have tried that (in a way):

    use strict; # 'use strict' requires that you use 'my' for all local va +riables, or explicitely qualify all globals. use warnings; use Bio::Structure::SecStr::DSSP::Res; my @dssp_objs =(); foreach my $file (@ARGV) { push @dssp_objs, Bio::Structure::SecStr::DSSP::Res->new('-file'=>$file +); } foreach my $dssp_obj(@dssp_objs) { #Get PDB ID and Compound representation for each file my $pdb_id = $dssp_obj->pdbID(); print "Analysis of PDB:: ". $pdb_id. "\n"; my $cmpd = $dssp_obj->pdbCompound(); print "Representing:: ". $cmpd. "\n"; etc... }

    But when I do this, and run the commandline with a txt file of the DSSP filenames:

     DSSP_Output.pl DSSP_codes.txt

    I get an exception!

    Please let me know what you think...apologies for all the headache...

    A <slowly deconfusing> InfoSeeker