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