how to open this file

R

ritu_minda

Hi Members,
i am new in perl , so i want your's help. Please help me.
i am making a programm amd i want to open a file of different
directory. and i worte the code
$filename1=012945;
unless (open(FILE1,"C:/Ritu/FastaSeqs/$filename1.CONSENS") )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n";
exit;


}


@sts_seq =<FILE1>;
but i got message that cannot open file.
am i doing something wrong.
 
P

Paul Lalli

i am new in perl , so i want your's help. Please help me.
i am making a programm amd i want to open a file of different
directory. and i worte the code
$filename1=012945;
unless (open(FILE1,"C:/Ritu/FastaSeqs/$filename1.CONSENS") )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n";
exit;
}

@sts_seq =<FILE1>;
but i got message that cannot open file.
am i doing something wrong.

Yes. First, and foremost, you're not using either strict or warnings.
Either of them would have told you at least one of the two separate
problems you have here. Secondly, you're not asking Perl to tell you
*why* it couldn't open the file. Always include the $! in your error
output from system calls.
Also, try to get into the habbit of using lexical filehandles rather
than global barewords.

use strict;
use warnings;
my $filename = 012945;
open my $FILE1, "C:/Ritu/FastaSeqs/$filename1.CONSENS"
or die "Cannot open '$filename1.CONSENS': $!";

Once you add strict and warnings, you should be able to see what you're
doing wrong in telling Perl what file to open.

Once you've figured those out, you should read the following
documentation to help you understand how to fix it:
http://perldoc.perl.org/perldata.html#Scalar-value-constructors

Read that entire section. It contains the explanations for both of
your two problems. If you are still confused after reading, feel free
to post back here for further assistance.

Paul Lalli
 
R

Rita

Thanks ,But yes i am still confuse bcoz perl is giving me reason that
No such files or directory but i am giving right path.
use warnings;
use strict;
# Get the name of the text file from the user that has the seqid,
source seq :
print "Please type the filename of the seqid and source seq : ";
my $filename = <STDIN>;

# Remove the newline from the filename
chomp $filename;
# either open the file, or exit from the program
unless ( open(FILE, $filename) )
{
print "Cannot open file \"$filename\"\n\n";
exit;
}
my @sequence=<FILE>;
my $sequence=join('',@sequence);
my@lines=split("\t",$sequence);
my $seqid=$lines[1];
my $source_seq=$lines[4];
print $seqid,"\t";
print $source_seq,"\n";

my$filename1=$seqid;
unless (open(FILE1,"C:/Ritu/FastaSeqs/$filename1.CONSENS") )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}
my @sts_seq =<FILE1>;
is i giving the path at wrong place
 
J

John Bokma

Rita said:

for quoting:
unless ( open(FILE, $filename) )
{
print "Cannot open file \"$filename\"\n\n";
exit;
}

Which is usually written as:

open( FILE, $filename )
or die "Cannot open '$filename' for reading: $!";

Note the $! in the die.

exit is also wrong, technically, since you exit(0)...,
see perldoc -f exit
 
R

Rita

thanks,but that first file is opening but second one is giving me
problam it's giving me message that no such files or directory so am i
giving path at wrong place or in wrong way or i am doing something
wrong?
use warnings;
use strict;
# Get the name of the text file from the user that has the
# seqid, source seq :
print "Please type the filename of the seqid and source seq : ";
my $filename = <STDIN>;

# Remove the newline from the filename
chomp $filename;
# either open the file, or exit from the program
unless ( open(FILE, $filename) )
{
print "Cannot open file \"$filename\"\n\n $!";
exit;
}
my @sequence=<FILE>;
my $sequence=join('',@sequence);
my@lines=split("\t",$sequence);
my $seqid=$lines[1];
my $source_seq=$lines[4];
print $seqid,"\t";
print $source_seq,"\n";

my$filename1=$seqid;
unless (open(FILE1,'C:/Ritu/FastaSeqs/$filename1.CONSENS') )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}
Rita
 
P

Paul Lalli

Rita wrote:

Please start appropriately quoting some context when you reply. That
means trim the previous message down to the relevant bits, and putting
your response below the quote. I will not be responding any further in
this thread if you do not. This would be a good time for you to read
the posting guidelines for this group. They are posted here twice a
week. See also the link that John gave you in his previous reply.
thanks,but that first file is opening but second one is giving me
problam it's giving me message that no such files or directory so am i
giving path at wrong place or in wrong way or i am doing something
wrong?

Don't retype error messages. Copy and paste them, word for word,
including line numbers.
use warnings;
use strict;
# Get the name of the text file from the user that has the
# seqid, source seq :
print "Please type the filename of the seqid and source seq : ";
my $filename = <STDIN>;

# Remove the newline from the filename
chomp $filename;
# either open the file, or exit from the program
unless ( open(FILE, $filename) )
{
print "Cannot open file \"$filename\"\n\n $!";
exit;
}

You've been told three times now the better way to do this. Why are
you refusing to follow the advise you've been given?
my @sequence=<FILE>;
my $sequence=join('',@sequence);
my@lines=split("\t",$sequence);

This makes astonishingly little sense. You have an array of lines that
is made up of tab-delimited fields? That means that for a file such
as:
foo bar baz
abc def ghi
Your array will contain: "foo", "bar", "baz\nabc", "def", "ghi\n"
Is that really what you want?
my $seqid=$lines[1];
my $source_seq=$lines[4];
print $seqid,"\t";
print $source_seq,"\n";

my$filename1=$seqid;
unless (open(FILE1,'C:/Ritu/FastaSeqs/$filename1.CONSENS') )

This is VERY different from the code you posted in your last reply.
Are you retyping your code or copy and pasting? Always copy and paste.

Single-quoted strings do not interpolate. You are trying to open a
file literally named '$filename1.CONSENS'. That string needs to be in
double-quotes, as your error output is.
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}

And again with the error handling counter to what you've been
recommended.

Paul Lalli
 
R

Rita

Paul said:
Rita wrote:

Please start
thanks ,sure i will see that rules.
Don't retype error messages. Copy and paste them, word for word,
including line numbers.
my programm is not giving me error but it's not opening the second file.which is in another directory and has .CONSENS extansion.
my @sequence=<FILE>;
my $sequence=join('',@sequence);
my@lines=split("\t",$sequence);

This makes astonishingly little sense. You have an array of lines that
is made up of tab-delimited fields? That means that for a file such
as:
foo bar baz
abc def ghi
Your array will contain: "foo", "bar", "baz\nabc", "def", "ghi\n"
Is that really what you want?
i want here that i have tab -delimited file and i want to read just 2 field of that file and only first data of that 2 field.so am i doing wrong?
my $seqid=$lines[1];
my $source_seq=$lines[4];
print $seqid,"\t";
print $source_seq,"\n";
my$filename1=$seqid;
unless (open(FILE1,"C:/Ritu/FastaSeqs/$filename1.CONSENS") )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}
this time i copy and paste but this one is also not running.
 
P

Paul Lalli

Rita said:
thanks ,sure i will see that rules.

Better, but not quite correct. Don't prepend your new text with any
'>' characters. It comes out looking like all your new responses are
actually parts of my quoted reply. I've fixed the quoting below.
my programm is not giving me error but it's not opening the second file.which is in
another directory and has .CONSENS extansion.

I don't understand how you can tell it's not opening the second file.
You didn't try to do anything with the contents of that second file.
What is leading you to believe it's not opening?

Have you read the posting guidelines yet? They suggest telling us
exactly what output you are expecting to see, and exactly what output
you are seeing instead.
i want here that i have tab -delimited file and i want to read just 2 field of that file and
only first data of that 2 field.so am i doing wrong?

Rita, it is obvious that English is not your native language. There is
nothing wrong with that of course. But because communication may be an
issue, it is even more important for you to show us exactly what your
input and desired output looks like. I am not at all sure that I
understand what you're saying you have, nor what you're saying you
want.

If you only want to read the second field of the first line of the
file, why are you reading the entire file into memory? Just read one
line:

my $seqid=$lines[1];
my $source_seq=$lines[4];

Didn't you just say you only wanted the second field? Why are you also
getting the fifth field?

Are these printing out the values you are expecting to see?
this time i copy and paste but this one is also not running.

We have no way of knowing what "not running" means. Again, what
results *are* you seeing that you do not want? What results are you
*not* seeing that you do want? Copy and paste your sample input, your
desired output, and your actual output.

Paul Lalli
 
R

Rita

Rita, it is obvious that English is not your native language. There is
nothing wrong with that of course. But because communication may be an
I am sorry that i am unable to know you that what i want but i am
trying.
issue, it is even more important for you to show us exactly what your
input and desired output looks like.
okay
i have a data .txt file
which is like
Submitter Name Seq ID Accession NO Comments sequence
Name1 012945 AW348161 good seq
TBCDEFGTRHJIRKDKDKDKLDFKDFCATACAACTAATTCCCTTGATCTGTCTCAAAATTGATALKJSHHDFA

Name2 012971 AW348179 good seq
AACTHJKIKJDKIDSIRFOJFOFJOFJTAAAGGGCATATGCCCHHHHKKKKKKKAGACAGATAATTTAA
so i want to extract two fields [Seqid and Seq]of this file but only
first data of that two field .
so i am doing
print "Please type the filename of the seqid and source seq : ";
my $filename = <STDIN>;

# Remove the newline from the filename
chomp $filename;
# either open the file, or exit from the program
unless ( open(FILE, $filename) )
{
print "Cannot open file \"$filename\"\n\n $!";
exit;
}
my @sequence=<FILE>;
my $sequence=join('',@sequence);
my@lines=split("\t",$sequence);
my $seqid=$lines[1];
my $source_seq=$lines[4];
print $seqid,"\t";
print $source_seq,"\n";
but it is giving me field name not field data.
and then i want a file which name is same as $seqid but this is in
another directory so i want to open that file so i am doing
my$filename1=$seqid;
unless (open(FILE1,'C:/Ritu/FastaSeqs/$filename1.CONSENS') )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}
my @sts_seq =<FILE1>;
but this program is giving me message
that No Such file or directory though i am giving right path.

My output is
SeqID sequence
Cannot open file "012945.CONSENS"
No such file or directory
though i want output
012945
TBCDEFGTRHJIRKDKDKDKLDFKDFCATACAACTAATTCCCTTGATCTGTCTCAAAATTGATALKJSHHDFA

and open the second file too.
hope it's Better.
thanks
Rita
 
P

Paul Lalli

Rita said:
I am sorry that i am unable to know you that what i want but i am
trying.

Yes, I can see that. I am trying too. We're getting there. . .
okay
i have a data .txt file
which is like
Submitter Name Seq ID Accession NO Comments sequence
Name1 012945 AW348161 good seq
TBCDEFGTRHJIRKDKDKDKLDFKDFCATACAACTAATTCCCTTGATCTGTCTCAAAATTGATALKJSHHDFA

Name2 012971 AW348179 good seq
AACTHJKIKJDKIDSIRFOJFOFJOFJTAAAGGGCATATGCCCHHHHKKKKKKKAGACAGATAATTTAA

Okay. This is important information. Your datafile contains a header
row. That is, the first line in the file does not contain any data,
only field names. You probably should have mentioned this earlier.
so i want to extract two fields [Seqid and Seq]of this file but only
first data of that two field .
so i am doing
print "Please type the filename of the seqid and source seq : ";
my $filename = <STDIN>;

# Remove the newline from the filename
chomp $filename;
# either open the file, or exit from the program
unless ( open(FILE, $filename) )
{
print "Cannot open file \"$filename\"\n\n $!";
exit;
}

Nothing especially wrong with any of this.
my @sequence=<FILE>;
my $sequence=join('',@sequence);
my@lines=split("\t",$sequence);
my $seqid=$lines[1];
my $source_seq=$lines[4];

Okay. Here's where we run into problems. First of all, you're again
reading in the entire file into memory. Including that header row.
You don't want that header row at all. It should be discarded.
Preferably, it shouldn't be stored to begin with.

Then you are joining together all of the lines of that file - newlines
included - into one big scalar variable $sequence. Then you are
separating $sequence by the tab characters. See my previous reply
(about the "baz\nabc") for why this won't do what you want.

You've stated you only want the first line of data from the file. So
only read and store that one line. Do not read all the lines into a
variable. Elminate everything between your "unless (open FILE ..."
line and the "my $source_seq = $lines[4]" line, and replace it with:

open FILE, $filename or die "cannot open $filename: $!";
#read and discard the first (header) line:
<FILE>;
#read and *store* the second line (first data line), removing the
newline:
chomp (my $line = <FILE>);
#parse the line on tabs, saving the 2nd and fifth fields:
my ($seqid, $source_seq) = (split /\t/, $line)[1,4];
print $seqid,"\t";
print $source_seq,"\n";

Using my code above, these should now give you the output you wanted.
but it is giving me field name not field data.

That's because you were working with that header line that you didn't
discard.
and then i want a file which name is same as $seqid but this is in
another directory so i want to open that file so i am doing
my$filename1=$seqid;
unless (open(FILE1,'C:/Ritu/FastaSeqs/$filename1.CONSENS') )

And now we're back to using single quotes. Why? Are you retyping your
code again? Don't do that. Those have to be double quotes.
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}
my @sts_seq =<FILE1>;
but this program is giving me message
that No Such file or directory though i am giving right path.

My output is
SeqID sequence
Cannot open file "012945.CONSENS"

I cannot see how you could possibly be getting that output. You
printed out $seqid, and found that it is the string "SeqID". Then you
assigned $filename1 = $seqid. So how did $filename1 suddenly become
'012945'?

I don't think you're showing us your real code and real output, still.
No such file or directory
though i want output
012945
TBCDEFGTRHJIRKDKDKDKLDFKDFCATACAACTAATTCCCTTGATCTGTCTCAAAATTGATALKJSHHDFA

and open the second file too.
hope it's Better.

Fix your code using my suggestions above. Please make sure you are
copy and pasting your code, and copy and pasting your output. Please
make sure the output you are pasting is the output generated by the
most recently modified version of your code.

Paul Lalli
 
B

Brian Wakem

Rita said:
unless (open(FILE1,'C:/Ritu/FastaSeqs/$filename1.CONSENS') )
{
print "Cannot open file \"$filename1.CONSENS\"\n\n $!";
exit;
}
my @sts_seq =<FILE1>;
but this program is giving me message
that No Such file or directory though i am giving right path.

My output is
SeqID sequence
Cannot open file "012945.CONSENS"
No such file or directory


That is not the output because you don't try to open 012945.CONSENS, you are
trying to open $filename1.CONSENS, which does not exist. You need to
double quote - which you have already been told.

Either you are not posting actual code are you are making up the error,
which is it?
 
R

Rita

Thanks it's working I greatly appreciated it.
And now we're back to using single quotes. Why? Are you retyping your
code again? Don't do that. Those have to be double quotes.

I cannot see how you could possibly be getting that output. You
printed out $seqid, and found that it is the string "SeqID". Then you
assigned $filename1 = $seqid. So how did $filename1 suddenly become
'012945'?

I don't think you're showing us your real code and real output, still.
i am sorry that i am using there Double quoat "" but still it's not
opening my file.
and it's not running so i delete the header line that's why it's giving
me that output that cannot open file 012945.CONSENS.
My Program is
use warnings;
use strict;
# Get the name of the text file from the user that has the
# seqid, source seq :
print "Please type the filename of the seqid and source seq : ";
my $filename = <STDIN>;
# Remove the newline from the filename
chomp $filename;
# either open the file, or exit from the program
open FILE,$filename ||die "Cannot open file \"$filename\"\n\n $!";
<FILE>;
chomp(my $line = <FILE>);
my ($seqid, $source_seq) = (split /\t/, $line)[1,4];
print $seqid,"\t";
print $source_seq,"\n";
my$filename1=$seqid;
open (FILE1,"C:\ Ritu\ FastaSeqs\$filename1.CONSENS")||die "Cannot open
file \"$filename1.CONSENS\"\n\n $!";
my @sts_seq =<FILE1>;
#Close the file
close FILE;
close FILE1;
and output is
012945
TBCDEFGTRHJIRKDKDKDKLDFKDFCATACAACTAATTCCCTTGATCTGTCTCAAAATTGATALKJSHHDFA

Cannot open file "012945.CONSENS"
No such file or directory at test5.txt line 19,<FILE> line 2.
Why still my file is not open?
Rita
 
D

Dr.Ruud

Rita:
i have a data .txt file
which is like
Submitter Name Seq ID Accession NO Comments sequence
Name1 012945 AW348161 good seq
TBCDEFGTRHJIRKDKDKDKLDFKDFCATACAACTAATTCCCTTGATCTGTCTCAAAATTGATALKJSHHDF
A

Name2 012971 AW348179 good seq
AACTHJKIKJDKIDSIRFOJFOFJOFJTAAAGGGCATATGCCCHHHHKKKKKKKAGACAGATAATTTAA

Are these tab-separated values?

my $sequence=join('',@sequence);

This looks like a bad idea, I suggest DBD::CSV.
http://search.cpan.org/~jzucker/DBD-CSV/lib/DBD/CSV.pm

unless (open(FILE1,'C:/Ritu/FastaSeqs/$filename1.CONSENS') )

Still single quotes?
 
J

John Bokma

Purl Gurl said:
Paul Lalli wrote:
Oh, oh, you are displaying an attitude much like mine. Careful! The
regulars here will troll you like crazy because of your high
expectations of posters.

Since when stopped Paul being a regular?
 
J

John Bokma

Oh, and when you get a reply in this group that doesn't satisfy you /
doesn't help you outright, please do not try to get help faster by
emailing people who did try to help you.

I consider that very rude (especially if I get the email not only via a
reply-to on a posting, but *also* via another email address, found on one
of my sites).
 
P

Paul Lalli

Rita said:
open (FILE1,"C:\ Ritu\ FastaSeqs\$filename1.CONSENS")

When did you change this line? WHY did you change this line? Why do
you keep posting code that's different from the previous post, while
acting as though nothing changed? If you had included this actual code
the first time around - in your first post, we could have told you what
was wrong with it way back then!

I'm sorry, but I'm done with this thread.

Paul Lalli
 
R

Rita

When did you change this line? WHY did you change this line? Why do
you keep posting code that's different from the previous post, while
acting as though nothing changed? If you had included this actual code
the first time around - in your first post, we could have told you what
was wrong with it way back then!

I'm sorry, but I'm done with this thread.
Don't mind it . i already told you that i am new t perl so i am trying
diffrent way and somebody told me that try backslash like a
windowsformat so i did and then it's reading like \R and \F (like \n)
so i give the space .but trust me both syntax is not working.
Rita
 
R

Rita

Rita said:
Don't mind it . i already told you that i am new t perl so i am trying
diffrent way and somebody told me that try backslash like a
windowsformat so i did and then it's reading like \R and \F (like \n)
so i give the space .but trust me both syntax is not working.
Rita
thanks to all you make my problam easy and you are right the syntax is
right but i am making wrong to it so sorry about that but i really
appritiate the help of group members
 

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
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top