Jyoti wrote:
Hello All,
Hello,
I just want help to open and read a file. I have to make a script so that it
should open and read a file which is in fasta format. I have done something
with subroutine but getting some errors. May be everyone do not know the
fasta format....
Fasta format have peculiar feature: its starts with symbol " > " and
contains alpabets and numbers. So acc to that code have been given...
Can anyone please check the code n lemme know changes please!!!
#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
use Bio::SeqIO;
my $q = new CGI;
That should be:
my $q = CGI->new;
my $filename = $q->param("file");
my $line;
&get_file_data($filename);
Why are you calling the funtion in void context when it returns data
that you need? You shouldn't use an ampersand in front of a function
name when you call it.
my $line = get_file_data( $filename );
###############################################################################################################################
#subroutine to read fasta files.
sub get_file_data {
my @filename=();
You should indent your code consistently so it is easier to read.
my $line='';
my $j=0;
my($filename) = @_;
open(get_file_data, $filename);
You should *always* verify that the file opened correctly:
open my $GET_FILE_DATA, '<', $filename or die "Cannot open '$filename' $!";
foreach my $line (@_) {
At this point @_ contains the file name you got from $q->param("file")
but not the actual contents of that file. To get the file contents you
need:
while ( my $line = <$GET_FILE_DATA> ) {
chomp $line;
$line =~ s/[\s0-9]//g;
next;
You have an unconditional next here so nothing past this point in the
loop is executed.
# check blank line
if ($line =~ /^\s*$/) {
next;
}
# check comment line
else
{
$line =~ /^\s*#/ ;
next;
}
# check fasta header line
if($line =~ /^>/) {
next; }
else {
print STDERR "Cannot open file \"$filename\n\n";
exit;
The error message here is not related to opening the file so it makes no
sense.
}
close get_file_data;
}
return @_;
Since @_ contains the file name you got from $q->param("file") you are
just returning the same data from the function that was input to the
function.
}
#############################################################################################################
John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/