bisimen has asked for the wisdom of the Perl Monks concerning the following question:

I can't figure out how to apply this subroutine (pretty_print) to my data:

#!/usr/bin/perl use warnings; #use strict; ($sfile_1, $sfile_2) = @ARGV; ($id1, $seq1) = read_fasta($sfile_1); ($id2, $seq2) = read_fasta($sfile_2); pretty_print(\$id1, \$seq1); pretty_print(\$id2, \$seq2); sub read_fasta { ## reads a single sequence from a fasta file my $seqFile = shift @_; my $seq = ""; my $id = ""; open(my $in, "<", $seqFile) or die "unable to open $seqFile $!\n"; while(<$in>){ chomp; if($_ =~ /^>(\S+)/ ){ last if(length($id)); $id = $1; next; } if(length($id)){ $seq .= $_; } } return ($id, $seq); } sub pretty_print { my($seq, $colno) = shift @_; # We want to start from 0, and increment the starting position. # for this we use a classical loop. for (my $b=0; $b < length($seq); $b += $colno){ print( substr($seq, $b, $colno), "\n"); } } exit;

The two data files if needed: https://justpaste.it/1ch48 and https://justpaste.it/1ch4b

Replies are listed 'Best First'.
Re: How to use this subroutine
by LanX (Saint) on Oct 16, 2017 at 14:55 UTC
    >  my($seq, $colno) = shift @_;

    This looks non sensical.

    Remove the shift, otherwise $colno is never set.

    update

    BTW: IMHO you should have got a "is not initialized" warning.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Removed shift.

      And yeah, I did get that warning. But was confused by it... I see now that, I didn't declare a number how long each line should be.
Re: How to use this subroutine
by hippo (Archbishop) on Oct 16, 2017 at 14:52 UTC
    #use strict;

    That's not a good sign. Uncomment that and fix any of the issues arising first.

    pretty_print(\$id1, \$seq1); pretty_print(\$id2, \$seq2);

    Why are you passing the arguments as references?

      No reason, just experimenting... And I'm using strict now :)
Re: How to use this subroutine
by wjw (Priest) on Oct 16, 2017 at 15:55 UTC

    I modified your script just a bit to show you what I believe it is supposed to do. If you are just trying to get this done, what is below should work for you. I name the sequence files in the script instead of using ARGV, and otherwise just made changes to make it work.

    It is still not pretty, but it will run as re-written. I would suggest you look into the perlref tutorial to see why things did not work in the codes as you posted it.

    If you are really interested in why the code did not work for you as hoped, note each change made, then take both your original code and the little hack below and run them with perl -d. If the Perl debugger is too much for you (it should not be, it is pretty simple) Put print statements in various places that are useful to you so you can track what is happening in that subroutine. Better yet, do both.

    #!/usr/bin/perl use warnings; use strict; my $sfile_1 = "f0.txt"; my $sfile_2 = "f1.txt"; my ($id1, $seq1) = read_fasta($sfile_1); my ($id2, $seq2) = read_fasta($sfile_2); my $colno = 12; pretty_print($seq1, $colno); pretty_print($seq2, $colno); sub read_fasta { ## reads a single sequence from a fasta file my $seqFile = shift @_; my $seq = ""; my $id = ""; open(my $in, "<", $seqFile) or die "unable to open $seqFile $!\n"; while(<$in>){ chomp; if($_ =~ /^>(\S+)/ ){ last if(length($id)); $id = $1; next; } if(length($id)){ $seq .= $_; } } return ($id, $seq); } sub pretty_print { my($seq, $colno) = @_; # We want to start from 0, and increment the starting position. # for this we use a classical loop. for (my $b=0; $b < length($seq); $b += $colno){ print( substr($seq, $b, $colno), "\n"); } } exit;

    The bottom line is that the code you posted was not handing in what you thought it was to the pretty_print() sub. $colno wants the number of columns to print on a given line...

    Do take the time to learn about the Perl debugger, always 'use strict' and 'use warnings', and use this site and perldoc perltut to help you along. Hope that gets you started...

    ...the majority is always wrong, and always the last to know about it...

    A solution is nothing more than a clearly stated problem...

Re: How to use this subroutine
by Anonymous Monk on Oct 16, 2017 at 14:50 UTC
    pretty_print($seq1, 42);
      Thanks!