print first line,where Data match.

B

BD

Hi All,
I am trying to make program that has First Input file like
seq1 AAAAAAAAAAABBBBBBBBBBCCCCCCCCCCCCCCCCCCCDDDDDDDD
DDDDDDDDEEEEEEEFFFFFFFFFFFFFFF
seq2
KKKKKKKKLLLLLLLLLMMMMMMMMMMMMNNNNNNNOOOOOOOOOOOOO
OPPPPPPPPPPPPPPRRRRRSSSSSS
and second Inputfile like
KKKKKKKKLLLLLLLLLMMMMMMMMMMMMNNNNNNNOOOOOOOOOOOOOO
PPPPPPPPPPPPPPRRRRRSSSSSS
now I wrote a script which take data from second Input file and match

motif with First Input file and where it match print that header
Like here it match at second Sequence so in Output it should print
seq2
here is my code :
use warnings;
use strict;
my $filename ='file1.txt';
open FILE1,$filename or die "Cannot open file $filename \n $!";
my @seq=<FILE1>;
my $header=' ';
my $Seq=' ' ;
my $i=0;
my (@Header,@Sequence);
foreach my $line (@seq){
chomp $line;
if ($line =~ /^>/){
$i++;
$Header[$i]=$header.$line;
}else{
$Sequence[$i]=$Seq.$line;
}
}
my $Input='file2.txt';
open FILE2,$Input or die "Cannot open file $Input \n $!";
<FILE2>;
my $motif = <FILE2>;
if ($Sequence[$i] =~ /$motif/ ){
print "$Header[$i] \n";
}
else{
print "I couldnot found it.\n";
print $i,"\n";
}
I am getting output that I couldnot found it.
If anybody can help me to get Output or debugging my Code?
I will really appreciate.
Thanks.
 
B

Brian McCauley

BD said:
Hi All,
I am trying to make program that has First Input file like
KKKKKKKKLLLLLLLLLMMMMMMMMMMMMNNNNNNNOOOOOOOOOOOOO
OPPPPPPPPPPPPPPRRRRRSSSSSS
and second Inputfile like
KKKKKKKKLLLLLLLLLMMMMMMMMMMMMNNNNNNNOOOOOOOOOOOOOO
PPPPPPPPPPPPPPRRRRRSSSSSS
now I wrote a script which take data from second Input file and match

motif with First Input file and where it match print that header
Like here it match at second Sequence so in Output it should print
seq2
here is my code :
use warnings;
use strict;

Good start.
my $filename ='file1.txt';
open FILE1,$filename or die "Cannot open file $filename \n $!";

Nothing wrong wtih that but you are just learning Perl now there's
little point bothering with the legacy 2-argument form of open() and
bareword filehandles/
open my $file1,'<',$filename or die "Cannot open file $filename \n $!";
my @seq=<FILE1>;

my $header=' ';
my $Seq=' ' ;

What is the point of these variables - as far as I can see they never
change.

Perl array indexes start a 0. Since you $i++ before you first use it
you should initialise it to -1 not 0.
my (@Header,@Sequence);
foreach my $line (@seq){
chomp $line;
if ($line =~ /^>/){
$i++;
$Header[$i]=$header.$line;
}else{
$Sequence[$i]=$Seq.$line;
}
}
my $Input='file2.txt';
open FILE2,$Input or die "Cannot open file $Input \n $!";
<FILE2>;
my $motif = <FILE2>;
if ($Sequence[$i] =~ /$motif/ ){
print "$Header[$i] \n";
}

What was the point of having the array @Sequence if you are only going
to compare the last element? Surely you really wanted a loop for my $i
( 0 .. $#Sequence ) {...}

As someone else pointed out you forgot to chomp($motif)

Note: $foo =~ /$bar/ is not the way to check if the string in $bar
appears in $foo. It checks if the _pattern_ in $bar matches something
in $foo. If, as in the case of $motif, the pattern is all just letters
this actually makes no difference but you should still get into the
habit of doing /\Q$motif/ or using index() when you want to look for a
simple string-contains condition.

If, of course, you actually wanted to check string equality you should
have used the string equality operator instead.
 
B

BD

Brian said:
Note: $foo =~ /$bar/ is not the way to check if the string in $bar
appears in $foo. It checks if the _pattern_ in $bar matches something
in $foo. If, as in the case of $motif, the pattern is all just letters
this actually makes no difference but you should still get into the
habit of doing /\Q$motif/ or using index() when you want to look for a
simple string-contains condition.

If, of course, you actually wanted to check string equality you should
have used the string equality operator instead.

Thanks.
 
B

BD

Brian said:
Note: $foo =~ /$bar/ is not the way to check if the string in $bar
appears in $foo. It checks if the _pattern_ in $bar matches something
in $foo. If, as in the case of $motif, the pattern is all just letters
this actually makes no difference but you should still get into the
habit of doing /\Q$motif/ or using index() when you want to look for a
simple string-contains condition.

If, of course, you actually wanted to check string equality you should
have used the string equality operator instead.

Thanks to all ,who help me. I really appreciated it.
 
B

BD

Nothing wrong wtih that but you are just learning Perl now there's
little point bothering with the legacy 2-argument form of open() and
bareword filehandles/



You don't need to slurp. You could have read <$file1> directly into
$line a line at a time.

If you dont mind ,I am just wondering that what is diffrence if you use
Filehandle FILE1 or $file1?
 
A

A. Sinan Unur

If you dont mind ,I am just wondering that what is diffrence if you
use Filehandle FILE1 or $file1?

open my $file1, '<', $filename or die $!;

$file1 is a lexical variable limited to the enclosing scope.

open FILE1, '<', $filename or die $!;

FILE1 is global.

Most of the time, your life will be simpler, happier, if you stick with
lexical filehandles.

Sinan
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top