match sequence

Discussion in 'Perl Misc' started by BD, May 9, 2006.

  1. BD

    BD Guest

    Hi All, I allredy asked this type of question in some thread before but
    I am trying that same Program in another way, But I am not getting
    output what I want...

    I am trying to make program that has First Input file like
    >seq1

    AAAAAAAAAAABBBBBBBBBBCCCCCCC
    CCCCCCCCCCCCDDDDDDDD
    DDDDDDDDEEEEEEEFFFFFFFFFFFFFFF
    >seq2

    KKKKKKKKLLLLLLLLLMMMMMM
    MMMMMMNNNNNNNOOOOOOOOOOOOO
    OPPPPPPPPPPPPPPRRRRRSSSSSS
    >seq3

    DDDDDFGDSDSDRTGDGDGDGDH
    SHFFHGLFHDFHBVVBBVJGFJGDKG
    HDSFHLIHFL
    and second Inputfile like
    >seq1

    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 $header;
    my $seq;

    my $filename ='file1.txt' ;
    open my $file1,'<',$filename or die "Cannot open file $filename \n $!";
    $/='>';
    $,="\t",$\="\n";
    while(<$file1>){
    chomp;
    next unless length $_;
    ($header,$seq)=split"\n",$_,2;
    $seq =~s/\n//g;
    print $header,$seq;
    }
    close $file1;
    my $Input='file2.txt ' ;
    open my $file2,'<',$Input or die "Cannot open file $Input \n $!";
    <$file2>;
    my $motif = <$file2>;
    chomp $motif;
    print "Motif- $motif";
    if ($seq=$motif){
    print "\nI Found It ! $header\n\n";
    }
    close $file2;
    I am getting output frome this script that

    I Found It ! >seq3

    whatever will be last Header,it will print ,though I want that in
    which sequence it will found motif,it should print that header.
    help will be appriciate.
    Thanks
     
    BD, May 9, 2006
    #1
    1. Advertising

  2. BD

    Guest

    "BD" <> wrote:

    > use warnings;
    > use strict;
    > my $header;
    > my $seq;
    >
    > my $filename ='file1.txt' ;
    > open my $file1,'<',$filename or die "Cannot open file $filename \n $!";
    > $/='>';
    > $,="\t",$\="\n";
    > while(<$file1>){
    > chomp;
    > next unless length $_;
    > ($header,$seq)=split"\n",$_,2;
    > $seq =~s/\n//g;
    > print $header,$seq;
    > }


    You are not saving any values other than the last one, so how could
    you expect anything other than those last values to be printed.
    In the loop, you need to do something like this:
    $hash{$seq}=$header;


    > close $file1;
    > my $Input='file2.txt ' ;
    > open my $file2,'<',$Input or die "Cannot open file $Input \n $!";
    > <$file2>;
    > my $motif = <$file2>;
    > chomp $motif;
    > print "Motif- $motif";


    You need to split and s/// the motif the same way you do the file1.txt

    > if ($seq=$motif){


    That is not a test, that is an assignment. It will be true as long
    as $motif holds a true value, regardless of what $seq (used to) have.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 9, 2006
    #2
    1. Advertising

  3. BD

    BD Guest

    All right,I changed my script but it is giving me some warnings,and I
    check that too ,everything is fine so should I Ignore that warnings?
    use warnings;
    use strict;
    my $header;
    my $header1;
    my $seq;
    my $seq1;
    my %hash;
    $/='>';
    $,="\t",$\="\n";
    my $filename ='file1.txt';
    open my $file1,'<',$filename or die "Cannot open file $filename \n $!";
    while(<$file1>){
    chomp;
    next unless length $_;
    ($header,$seq)=split"\n",$_,2;
    $seq =~s/\n//g;
    #print $header,$seq;
    $hash{$seq}=$header;
    }
    close $file1;
    my $Input='file2.txt';
    open my $file2,'<',$Input or die "Cannot open file $Input \n $!";
    while( <$file2>){
    chomp ;
    ($header1,$seq1)=split"\n",$_,2;
    $seq1 =~s/\n//g;
    #print $header1,$seq1;
    print $hash{$seq1};
    }
    close $file2;

    Warnings are
    Use of uninitialized value in substitution (s///) at file1.pl line 26,
    <$file2> chunk 1.
    Use of uninitialized value in hash element at file1.pl line 28,
    <$file2> chunk 1.
    Use of uninitialized value in print at file1.pl line 28, <$file2> chunk
    1.

    Here it is considering that $seq1 has no value ,but it is giving output
    too.
    and Is there any way that along the output I can print the header no.
    too in this case it will print 2.
    Thanks
     
    BD, May 9, 2006
    #3
  4. BD

    Guest

    "BD" <> wrote:
    > All right,I changed my script but it is giving me some warnings,and I
    > check that too ,everything is fine so should I Ignore that warnings?


    No, don't ignore the warnings.

    > while( <$file2>){
    > chomp ;
    > ($header1,$seq1)=split"\n",$_,2;
    > $seq1 =~s/\n//g;
    > #print $header1,$seq1;
    > print $hash{$seq1};
    > }
    > close $file2;


    Please indent your code for readability. Also, you should
    declare $header1 and $seq1 here, rather than up top.

    >
    > Warnings are
    > Use of uninitialized value in substitution (s///) at file1.pl line 26,
    > <$file2> chunk 1.
    > Use of uninitialized value in hash element at file1.pl line 28,
    > <$file2> chunk 1.
    > Use of uninitialized value in print at file1.pl line 28, <$file2> chunk
    > 1.
    >
    > Here it is considering that $seq1 has no value ,but it is giving output
    > too.


    You are in a loop. One time though the loop it is undefined. Another
    time through the loop it does have a value. Note that your first loop
    has a "next unless length $_;" which your second loop doesn't have.

    When trying to warnings from inside loops, it is often useful to add a
    'warn "Starting loop";' at the start and a 'warn "Ending loop";' at the
    end. Then you will know which iteration is producing which warning.

    > and Is there any way that along the output I can print the header no.
    > too in this case it will print 2.


    Since the header number is already embedded in the header, I fail to see
    the point of that. But it is easy to do, in a variety of ways.

    $hash{$seq}=(1+keys %hash) . ": $header";

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 9, 2006
    #4
  5. BD

    BD Guest

    > Since the header number is already embedded in the header, I fail to see
    > the point of that. But it is easy to do, in a variety of ways.
    >
    > $hash{$seq}=(1+keys %hash) . ": $header";


    How this line works ?

    Actually what I am trying to do with this programm .
    I am matching the sequences of two files .one file has 1 or more than 1
    sequence and second file has only one sequence so wherever it will
    match ,I am trying to make sequence Id according to that .
    in this case sequence Id was 01011 but than it will be 01011-2.
    so i want some variable which has header no. so i can put like
    $seqid.'-'.$header_no

    Thanks
     
    BD, May 9, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    640
  2. Victor
    Replies:
    2
    Views:
    649
    Victor
    May 17, 2004
  3. Roger L. Cauvin

    Match First Sequence in Regular Expression?

    Roger L. Cauvin, Jan 26, 2006, in forum: Python
    Replies:
    43
    Views:
    1,144
    Armin Steinhoff
    Jan 28, 2006
  4. stef mientki
    Replies:
    13
    Views:
    647
    stef mientki
    Oct 20, 2007
  5. Big Tony
    Replies:
    2
    Views:
    111
    Anno Siegel
    Sep 27, 2004
Loading...

Share This Page