Help: How to use filehandle to save files?
Help: How to use filehandle to save files?
am 16.08.2007 15:58:45 von 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 ()
{
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
Re: Help: How to use filehandle to save files?
am 16.08.2007 16:24:17 von JT
Amy Lee wrote:
> 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 ()
If you use $est_in instead you will need here
while ( <$est_in> ) {
> {
> 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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Re: Help: How to use filehandle to save files?
am 16.08.2007 16:51:56 von Paul Lalli
On Aug 16, 9:58 am, Amy Lee wrote:
> 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
Re: Help: How to use filehandle to save files?
am 16.08.2007 17:36:58 von Michele Dondi
On Thu, 16 Aug 2007 21:58:45 +0800, Amy Lee
wrote:
>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Help: How to use filehandle to save files?
am 17.08.2007 02:03:52 von 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 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
Re: Help: How to use filehandle to save files?
am 17.08.2007 04:15:20 von Tad McClellan
Amy Lee wrote:
> 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 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 ( ) {
# do stuff to $_
print {$OUT};
}
close $OUT;
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Help: How to use filehandle to save files?
am 17.08.2007 11:11:21 von Michele Dondi
On Fri, 17 Aug 2007 08:03:52 +0800, Amy Lee
wrote:
>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 to operate this function, how can I save the files?
You use 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=);
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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,