ravimore has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Pattern Matching using Array
by LanX (Saint) on Jul 23, 2010 at 11:11 UTC
    unfortunately your post is unreadable

    please try to update it and add tags like  <p>,<c> ...

    see Markup in the Monastery

    Cheers Rolf

    UPDATE: since you're a beginner

    -----------------

    I wanna match array element patterns($pats[$i]) with the row sequence($str). Also find the overlapping region location in the sequence($str). Please help me. But my loop is not work properly.

    Thanks.

    INPUT FILES:

    seq.txt >seq1 AAAAAA >seq2 TTTTT

    pat.txt seq1 AAAAA TTTTT GGGGG

    I want following output: Patterns sequence name start end length of seq. AAAAA seq1 1 5 6 AAAAA seq1 2 6 6 TTTTT seq2 1 5 5

    not found GGGGG

    I wrote following PERL script:

    #!/usr/bin/perl-w + # perl program to find motif in text file eith its positions. + use strict; use Bio::SeqIO; open(my $outfile, "&gt;", "Motif_Result.txt"); my $file = 'seq.txt'; print {$outfile}"\t\t\t\t\t Positions\n"; print {$outfile}"\tPattern Name\tSequence Name\tStart\tEnd\tLength of +Sequence\n\n"; my $patternseq= 'pat.txt'; open(FIH, $patternseq); my $in; my %patsmap; my @residue; my @pats; my $seqcount = 0; while ($in=&lt;FIH&gt;) { chomp($in); my @pats=split " ",$in; my $residue=shift@pats; foreach my $nnn (@pats) # @pats = (AAAAA, TTTTT, GGGGG); { $patsmap{$nnn}=@residue; } print "Patterns\t"; print "@pats"; print "\n"; my $length = @pats; print "Total Patterns are: $length"; print "\n"; my $seqobj; my $in = Bio::SeqIO-&gt;new(-format =&gt; 'fasta', -file =&gt; $file); my $motif_count = 0; while ( my $seq = $in-&gt;next_seq) { $seqcount++; # count the number of sequences my $head = $seq-&gt;display_id(); my $str = $seq-&gt;seq; # get the sequence as a strin +g $str =~ s/\*//g; # remove all '*' from sequence my $i; for ($i=0; $i&lt;=$length; $i++) { while ( $str =~ m/(?=$pats<a href="?node=%24i">$i</a>)/ig +) { my @l = split('', $str); my $length=@l; print {$outfile}"\t"; print {$outfile}$pats<a href="?node=%24 +i">$i</a>; printf {$outfile}"\t\t$head\t\t%d", pos($str)+1; printf {$outfile}"\t%d", pos($str)+5; print {$outfile}"\t"; print {$outfile}$length; print {$outfi +le}"\n"; } } } print {$outfile}"\n\tTotal Sequences in file name $file\t:$seqcount\n" +; exit; }

    ---------------

Re: Pattern Matching using Array
by jwkrahn (Abbot) on Jul 23, 2010 at 15:47 UTC
    #!/usr/bin/perl-w

    You need a space between the program name and any switches you need to pass to the program.    Better to just use the warnings pragma:

    #!/usr/bin/perl use warnings;

    open(my $outfile, ">", "Motif_Result.txt"); ... open(FIH, $patternseq);

    You should always verify that the file opened correctly:

    open my $outfile, '>', 'Motif_Result.txt' or die "Cannot open 'Motif_R +esult.txt' $!"; ... open FIH, '<', $patternseq or die "Cannot open '$patternseq' $!";

    my $in; while ($in=<FIH>) { chomp($in); my @pats=split " ",$in; my $residue=shift@pats;

    That could more simply be written as:

    while ( <FIH> ) { my ( $residue, @pats ) = split;

    foreach my $nnn (@pats) # @pats = (AAAAA, TTTTT, GGGGG); { $patsmap{$nnn}=@residue; }

    You don't put any values into @residue so that is the same as saying:

    foreach my $nnn (@pats) # @pats = (AAAAA, TTTTT, GGGGG); { $patsmap{$nnn} = 0; }

    Perhaps you meant to use $residue instead of @residue?


    print "Patterns\t"; print "@pats"; print "\n"; my $length = @pats; print "Total Patterns are: $length"; print "\n";

    Instead of five separate prints you just need one:

    print "Patterns\t@pats\nTotal Patterns are: ", scalar @pats, " +\n";

    my @l = split('', $str); my $length=@l;

    That is usually written as:

    my $length = length $str;

    exit; }

    You exit the program at the bottom of the loop so you only ever read one line from your 'pat.txt' file.


Re: Pattern Matching using Array
by MidLifeXis (Monsignor) on Jul 23, 2010 at 15:13 UTC