Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Script ignoring while loop on Mountain Lion and Mavericks

by maklujkar (Initiate)
on Feb 13, 2014 at 00:00 UTC ( [id://1074716]=perlquestion: print w/replies, xml ) Need Help??

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

Dear PerlMonks,

I would appreciate any advice you have about a problem I have running a Perl script on the newer Mac operating systems.

I wrote a script to read each line of an input table for a pair of coordinates to extract segments from a long DNA sequence. (The whole script is below.) It worked on my old MacBook Pro with the Snow Leopard operating system. It still works on my student's non-Mac laptop. However, when I run it on my new MacBook Pro (Mavericks) or my Mac desktop (Mountain Lion), it only takes the first line of input, as if it is ignoring the while loop. It never gives me an error message.

I tried installing xcode, perlbrew, and Perl 5.18, but that didn't help. The perl -v command told me that I was running Perl 5.16. I deleted xcode and everything in the Perl folder in the Library (versions 5.8, 5.12) and any other Perl folder I could find, and it still runs the script with just the first line of input, and there is still a version 5.16 somewhere inside the computer. I don't know what to do, and Apple support has no idea either.

Muktak Aklujkar

The script:

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my $inputFile; my $sequenceFile; my $outputFile; my $template; my %hashComps = ("T"=>"A", "G"=>"C", "C"=>"G", "A"=>"T", "N"=>"N"); my %hashStrand = ("f"=>"+", "r"=>"-", "+"=>"+", "-"=>"-"); Getopt::Long::Configure ('bundling'); GetOptions ('i|input_file=s' => \$inputFile, 's|sequence_file=s' => \$sequenceFile, 'o|output_file=s' => \$outputFile); if(!defined($inputFile)) { die ("Usage: FASTAgeneratorDNA.pl -i <input file> -s <sequence file> - +o <output file>\n"); } if(!defined($sequenceFile)) { die ("Usage: FASTAgeneratorDNA.pl -i <input file> -s <sequence file> - +o <output file>\n"); } if(!defined($outputFile)) { die ("Usage: FASTAgeneratorDNA.pl -i <input file> -s <sequence file> - +o <output file>\n"); } open (INFILE, "<$inputFile") or die( "Cannot open file : $!" ); open (SEQFILE, "<$sequenceFile") or die( "Cannot open file : $!" ); open (OUTFILE, ">$outputFile") or die ("Cannot open file for output: $ +!"); my $DNAsequence = ""; while (my $line = <SEQFILE>) { chomp($line); if ($line =~ />/) { #if line contains FASTA header after any amount of + whitespace next; } else { $DNAsequence .= $line; } #end of else structure } #end of while loop for each line while (my $coordinates = <INFILE>) { my @coordinates_array = split /\s+/, $coordinates; my $seqid = $coordinates_array[0]; my $strand = $hashStrand{$coordinates_array[1]}; my $start = $coordinates_array[2] - 1; my $stop = $coordinates_array[3] - $start; print (OUTFILE "\n>$seqid"); $template = substr($DNAsequence, $start, $stop); my $revcomp = ""; if ($strand eq "+") { print (OUTFILE "\n$template"); } elsif ($strand eq "-") { while ($template) { my $lastbase = uc(chop($template)); my $nextbase = $hashComps{$lastbase}; $revcomp .= $nextbase; } #end of while loop for revcomp print (OUTFILE "\n$revcomp"); } else { die( "Please specify the strand with a plus or minus symbol."); } #end of else structure } #end of while loop for each set of coordinates close (OUTFILE) or die( "Cannot close file : $!"); close (SEQFILE) or die( "Cannot close file : $!"); close (INFILE) or die( "Cannot close file : $!");

Replies are listed 'Best First'.
Re: Script ignoring while loop on Mountain Lion and Mavericks
by kcott (Archbishop) on Feb 13, 2014 at 02:03 UTC

    G'day maklujkar,

    Welcome to the monastery.

    "... please e-mail me at ..."

    Please don't request responses be sent to your email address: they should be posted here. This is because:

    • We don't know what replies have been sent.
      • This negates any collaborative effort.
      • It potentially duplicates information.
      • Monks may be disinclined to possibly repeat an answer you've already received: you may miss out on an exceptionally good answer.
    • Bad information (whether incorrect, poor practice, out-of-date, etc.) cannot be commented on by other monks.
    • The next person with the same (or similar) question cannot search for the answer and we have to repeat the process again.

    Please remove the email request from your post. (If you don't know how to do this, see "How do I change/delete my post?".)

    On to your question. I have Lion (Mac OS X 10.7.5), not Mountain Lion: there may be minor differences but the basic information I've provided should still be valid.

    Apple provides a version of Perl (with its own patches) for use with Mac OS X: this is referred to as the System Perl. When you update Mac OS X, the System Perl may change which could affect your code; when you modify the System Perl (e.g. update a module) this could affect Mac OS X functionality. For these reasons, it is generally a bad idea to use the System Perl and a much better idea to install your own version(s) of Perl for your own use.

    Your script starts with the shebang line "#!/usr/bin/perl": this says "Use the System Perl". I start all my scripts with "#!/usr/bin/env perl": this says "Use my current version of Perl". I recommend you do this also.

    Telling us about the output of 'perl -v' is not all that helpful because we don't know which perl that is. From the commandline, 'which perl' will tell you this. Using the output from that, '/path/to/perl -v' will tell you its version (e.g. '/usr/bin/perl -v' will give you the version of the System Perl). The command 'perlbrew list' will tell you about all the Perls you have: this will not be limited to those installed via perlbrew but will also include the System Perl (/usr/bin/perl) and any others you might have, e.g. MacPorts Perl (/opt/local/bin/perl). [Also note that Perl versions are reported with three numbers (e.g. 5.8.8 or 5.18.2) — you should tell us the full version number as it oftens makes a difference (sometimes a big difference, e.g. there were substantial changes between 5.10.0 and 5.10.1).]

    "The perl -v command told me that I was running Perl 5.16. I deleted xcode and everything in the Perl folder in the Library (versions 5.8, 5.12) and any other Perl folder I could find, ..."

    You may have caused some serious damage doing this. My System Perl is 5.12.3; what's yours? — you may need to [find out how to] restore it. While there are alternatives to perlbrew, that's what I use and I have six different versions of Perl installed: 'perlbrew switch' allows you to choose whichever version you want. There's also a perlbrew uninstall command: I haven't used it but suspect it would be preferable to flailing around with rm. :-) [Note: it sounds like you'll also need to reinstall xcode.]

    When you've got your Perl issues sorted out, your script may run as expected. Without knowing what your input is, I can't really tell you if you're getting the correct output. Try adding print statements to see exactly what data you're reading and which branches of your conditional statements are being executed.

    -- Ken

      Thank you for taking the time to help me. I just discovered that the Show Invisibles trick no longer works. However, I found another trick that works: I replaced the old file with a new file, created with the latest version of TextWrangler. When I saved it, the dialog box had a drop-down menu for line breaks with the default setting Unix (LF). My guess is that the old file had a different setting for line breaks, but the new default setting is what Perl needed to recognize the input correctly.
Re: Script ignoring while loop on Mountain Lion and Mavericks (newline)
by Anonymous Monk on Feb 13, 2014 at 00:49 UTC

    it only takes the first line of input, as if it is ignoring the while loop

    Nope, your file only contains one line :)

    You can use

    use Path::Tiny qw/ path /; dd( path( shift )->slurp_raw ); sub dd { use Data::Dumper; print Data::Dumper->new([@_])->Sortkeys(1) ->Indent(1)->Useqq(1)->Dump . "\n"; }
    to find out what kind of line endings your files are in, then you can set $/ appropriately and there will be more than one line to read

    Your other alternative is to readline n-bytes at a time, see perlvar#$/

      Thank you for taking the time to reply. It has been several years since I had to write anything in Perl, so I have no idea how to apply the code that you provided. However, because you pointed me in the direction of the line endings, I tried a simpleton's approach to find out what they were. I turned on the "Show Invisibles" option in TextWrangler, where I had created the table as a .txt file after pasting entries from Excel. The lines all ended in the faucet-shaped character. (Sorry, I've forgotten what that's called.) Then I ran the script again, and it worked perfectly. I don't know why showing the line endings makes a difference, but I'll take it. I guess my script used to work because I would paste from the older version of Excel into TextWrangler, but when I had to switch to the new Excel for Mavericks, the line endings disappeared. Thanks again, and good night!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1074716]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-23 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found