Help: How to use filehandle to save files?

A

Amy Lee

Hello,

I use Perl to make a script to format EST sequences, and I meet a problem
that I can't solve.

When the script has formated the sequences, I hope the results can be
saved in files, not output to screen. For example, the sequence called
soybean_60.fasta, and I hope when it has been formated, the formation
result soybean_60.fasta_format can be created.

It's my codes:

foreach $EST (@ARGV)
{
if (! -e $EST)
{
print "Error: Couldn't find the $EST sequence.\n";
print "Error: Pass ......\n";
next;
}
open EST, "$EST";
while (<EST>)
{
s/ .*//g;
s/\./ /g;
s/ .*/>/g;
s/gi\|//g;
s/\|gb\|/>/g;
s/^\n$//g;
if ($. == 1)
{
s/^>//;
}
chomp;
}
print "$EST sequence formated okay.\n";
close EST;
}

Which part should I modify? How to accomplish this function by filehandle?

Thank you very much~

Regards,

Amy Lee
 
J

Jens Thoms Toerring

Amy Lee said:
I use Perl to make a script to format EST sequences, and I meet a problem
that I can't solve.
When the script has formated the sequences, I hope the results can be
saved in files, not output to screen. For example, the sequence called
soybean_60.fasta, and I hope when it has been formated, the formation
result soybean_60.fasta_format can be created.
It's my codes:
foreach $EST (@ARGV)
{
if (! -e $EST)
{
print "Error: Couldn't find the $EST sequence.\n";
print "Error: Pass ......\n";
next;
}
open EST, "$EST";

I would use

unless ( open $est_in, '<', $EST ) {
print "Error, can't read $EST for reading.\n";
print "Error: Pass ......\n";
next;
}

since the mere existence of the file (which you test above) doesn't
tell you that you have the necessary permissions to read the file.
I also don't like the use of 'EST' since nowadays you can use simple
variables as file handles. And note the additional '<' in the call
of open, it explicitely says that you want to open the file for
reading.

Once you have the file open I now would open the output file:

unless ( open $est_out, '>', $EST . '_format' ) {
print "Error, can't open file for writing.\n";
print "Error: Pass ......\n";
close $est_in;
next;
}
while (<EST>)

If you use $est_in instead you will need here

while ( said:
{
s/ .*//g;

You don't need the 'g' here since nothing will be left for a second
go once a space has been found.
s/\./ /g;
s/ .*/>/g;

Also here the 'g' isn't needed, everything including the first space
in the string will be replaced by a single '>'. And because of that
also the 'g' in the line above doesn't change anything. You could
replace these two lines for the same result by

s/\..*/>/;

i.e. find the first dot and replace it and everything possibly
following it by '>'.

s/gi\|//g;
s/\|gb\|/>/g;
s/^\n$//g;

What's that supposed to do?
if ($. == 1)
{
s/^>//;
}
chomp;

Leave out the chomp, it doesn't do anything useful here. Instead write
the result of your conversions into the output file:

print $est_out $_;
}
print "$EST sequence formated okay.\n";
close EST;

And here use

close $est_in;
close $est_out;
Regards, Jens
 
P

Paul Lalli

When the script has formated the sequences, I hope the results
can be saved in files, not output to screen.
Which part should I modify? How to accomplish this function by
filehandle?

There are two basic kinds of filehandles. Those opened for reading,
as you have here, and those opened for writing, which is what you
want.

You open a file for writing very similarly to how you open a file for
reading:

Read:
open my $rfh, '<', $filename or die $!;
Write:
open my $wfh, '>', $filename or die $!;

Then once you have your filehandle opened for writing, you simply
print to that filehandle whatever you want to print:

print $wfh "This text goes to the file\n";

and when you're done printing, close the filehandle:
close $wfh;

For a full description on how to work with filehandles, see:
perldoc -f open
perldoc perlopentut

Paul Lalli
 
M

Michele Dondi

When the script has formated the sequences, I hope the results can be
saved in files, not output to screen. For example, the sequence called

Then redirect from your shell. Or else

open my $out, '>', $somefile or die "Can't open $somefile: $!\n";
select $out;


Michele
 
A

Amy Lee

Thank you everyone very much~

However, I still got a problem, why I must use use some variables to open
files? In my source code, I use

open EST, '<', $EST

to open a file.

If I use <EST> to operate this function, how can I save the files?
In my first post, there's my source code.

Thank you very much~

Regards,

Amy Lee
 
T

Tad McClellan

Amy Lee said:
However, I still got a problem, why I must use use some variables to open
files?


I cannot understand your question.

In my source code, I use

open EST, '<', $EST

to open a file.

If I use <EST> to operate this function, how can I save the files?


By print()ing to some other filehandle.


open my $OUT, '>', "${EST}_format" or
die "could not open '${EST}_format for output $!";
while ( <EST> ) {
# do stuff to $_
print {$OUT};
}
close $OUT;
 
M

Michele Dondi

However, I still got a problem, why I must use use some variables to open
files? In my source code, I use

Your question doesn't make sense. You *probably* refer to lexical
filehandles e.g.

open my $fh, ...

well, that's just the same as

open FH, ....

except that the latter will be a package global, the former... a
lexical and it has the usual advantage of lexicals. As a filehandle,
it has the additional advantage of not requiring an explicit close()
because an implicit one will take effect on scope exit.
open EST, '<', $EST

to open a file.

You use this to open a file *for reading*.
If I use <EST> to operate this function, how can I save the files?

You use <EST> to *read* ("lines") from that filehandle. You can
continue to use print() to "save the files" - in your jargon. Just
either kick the filheandle in as in

print OUT "What I want to print to the file\n";

or if you prefer

select OUT;

then

print "What I want to print to the file\n";

will automatically go to the file. But do a favour to both yourself
and us by asking someone who can surely explain the whole thing much
better than any of us could, the docs:

perldoc -f open
perldoc -f print
perldoc -f select


See:


#!/usr/bin/perl

use strict;
use warnings;

print "Amy, please enter a filename:\n";
my $file;
TRY: {
chomp($file=<STDIN>);
for ($file) {
print "At least one charachter please, retry:\n" and
redo TRY unless length;
print "File exists, choose another one:\n" and
redo TRY if -e;
}
}

{
open my $out, '>', $file or
die "Can't open `$file': $!\n";
print $out "Stuffing something into `$file' for Amy.\n";
}

print "See Amy? We stuffed something into `$file', ",
"Check for yourself!";

__END__


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top