CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl script help

 



agl
Novice

May 28, 2009, 8:28 PM

Post #1 of 6 (1546 views)
Perl script help Can't Post

Hello. I wrote my first perl script today on the fly, but it simply is not outputting. I am using a Linux platform so I have added the appropriate command at the top of my script.

All it is supposed to do is read through a file in my directory, examine if a particular structure is present ('ILE' in this case) in a particular line, and then using the substring function, write out the appropriate parts of that line to a given text fie. The issue is that it is not appearing on my output file, and my paths have been rechecked as correct. Here is my script. Any suggestions?

#!/usr/bin/perl
#Script to obtain dihedral angles based on 3-d coordinates
#Focus: Isoleucines
#First task, find isoleucine resides within handcurated db and visit every file in database
#Second task, find isoleucine residues for chi1- ca,cb,cg1,cd1
#Third task, find isoleucine residues for chi2- n,ca,cb,cgl -separate script?
#Finally, tabulate those with registered chemical shifts
#Open file in folder '/home/lockhartag/Wim/only_XrayGeoff/' #to grab all files use 'glob' command in Perl

open (INFILE, "<\\home\\lockhartag\\Wim\\only_XrayGeoff\\bmr4010.1ey0.cosh") or die "Input file failed to open $!";

open (OUTFILE, ">\\home\\lockhartag\\Wim\\chi1_out\\ILE.txt") or die "output file failed to open $!";
while(<INFILE>)
{
my($line) = $_;
chomp($line);
#Only lines containing Isoleucine structure
#Copy Isoleucine and its respective parameters for Chi1 to textfile 'Chi1' located in Wim#"""""""Chi2 """ located in Wim
#Dihedral angles check
while ($line =~ m/ILE/)
{
print OUTFILE substr($line,13,16);
print OUTFILE substr($line,18,21);
print OUTFILE substr($line,25,27);
print OUTFILE substr($line,33,39);
print OUTFILE substr($line,41,47);
print OUTFILE substr($line,49,55);
print OUTFILE substr($line,72,79);
}
}

close(INFILE);
close(OUTFILE);


alex_v2
User

May 29, 2009, 2:44 AM

Post #2 of 6 (1541 views)
Re: [agl] Perl script help [In reply to] Can't Post

2 thing linux uses / not \ for directory structures at least every flavor I have ever used and I have used way to many to count.

second to append to a file you need to use


Code
open( OUTFILE, ">>/some/file/path/outfile");


the > means write over the file. So it will touch completely overwrite it. >> means append to the file if it exists otherwise create it. Hope this helps you a bit.

-Alexander


agl
Novice

May 29, 2009, 7:16 AM

Post #3 of 6 (1534 views)
Re: [agl] Perl script help [In reply to] Can't Post

Thank you, but this is what now appears:

prse.pl: line 15: syntax error near unexpected token `INFILE,'
prse.pl: line 15: `open (INFILE, "< $/home/lockhartag/Wim/onlyXray_Geoff/bmr4010.1ey0.cosh") or die "Input file failed to open $!";'


FishMonger
Veteran / Moderator

May 29, 2009, 7:48 AM

Post #4 of 6 (1533 views)
Re: [agl] Perl script help [In reply to] Can't Post


In Reply To
Thank you, but this is what now appears:

prse.pl: line 15: syntax error near unexpected token `INFILE,'
prse.pl: line 15: `open (INFILE, "< $/home/lockhartag/Wim/onlyXray_Geoff/bmr4010.1ey0.cosh") or die "Input file failed to open $!";'


Change that line to:

Code
$input_file = '/home/lockhartag/Wim/onlyXray_Geoff/bmr4010.1ey0.cosh'; 
open (INFILE, '<', $input_file) or die "Input file '$input_file' failed to open $!";



(This post was edited by FishMonger on May 29, 2009, 7:49 AM)


agl
Novice

May 29, 2009, 10:14 AM

Post #5 of 6 (1524 views)
Re: [agl] Perl script help [In reply to] Can't Post

One more thing, I can get everything from my file outputted nicely, but I wanted to open and read ALL files in my directory '/onlyXray_geoff/' that have the same extension to my output file using my prior criteria. I've tried glob but it seems to say there is more than one argument to open :

#!/usr/bin/perl

#to grab all files use 'glob' command in Perl

my@files= glob("/home/lockhartag/Wim/onlyXray_geoff/*.cosh");

open (INFILE, "<", @files) or die "Input file failed to open $!";
open (OUTFILE, ">> /home/lockhartag/Wim/chi1_out/ILE.txt") or die "output file failed to open $!";


while(<INFILE>)
{
my($line) = $_;
chomp($line);
if ($line =~ m/ILE/)
{


printf OUTFILE "%9s", substr($line,13,16);
printf OUTFILE "%9s", substr($line,18,21);
printf OUTFILE "%8.3d", substr($line,25,27);
printf OUTFILE "%8.3f", substr($line,33,39);
printf OUTFILE "%8.3f", substr($line,41,47);
printf OUTFILE "%8.3f", substr($line,49,55);
printf OUTFILE "%8.3f", substr($line,72,79);
print OUTFILE "\n"

}
}


close(INFILE);
close(OUTFILE);


FishMonger
Veteran / Moderator

May 29, 2009, 11:37 AM

Post #6 of 6 (1522 views)
Re: [agl] Perl script help [In reply to] Can't Post

The open function works on a sigle file, not a list.

You need to use a for/foreach loop and open each file.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives