Adrade,
Thanks so much for your reply. I've included your part in my code. Somehow your approach doesn't consider the maximum span *in the given query*.
For example with query: "ACD" it gives
S4 ABIC S1 AB-C S2 A--C
i.e it left out the "D" for alignment (kindly see answers above). In other case, given query "AB" it over-aligned them, it gives:
S4 ABICID S2 A--C-D S1 AB-C-D
Is there any way I can go about it in your code. Hope to hear from you again.

Update: Finally! with some tweak suggested by Adrade below. I can get the problem SOLVED! Many thanks for the rest of you, especially Adrade. Don't know what to say! For those who are interested in the final form of the code, check this out:
# With major improvement by "canonizable" Adrade :-) sub align_v3 { my ($hashref,$seq) = @_; my @in = split(/\s/,$seq); my $test = join('(.*?)',@in); my @hyph_padded_seq; my @sequences; my $reduce; for (keys %$hashref) { $reduce = length(${$hashref->{$_}}[0]); last; } for my $key( sort {$a cmp $b} keys(%$hashref)) { my $string = join('',@{$hashref->{$key}}); if($string =~ $test) { push(@sequences, $hashref->{$key}); } } my $regex = '.*'.join('(.*)',@in).'.*'; my @in_the_running; my $biglen = 0; for (@sequences) { my @letters = @$_; # split(//,$_); s/.*($in[0].*$in[$#in]).*/$1/; my ($first, $last) = (0,0); for (@letters) { last if $_ eq $in[0]; $first++; } for (reverse @letters) { last if $_ eq $in[$#in]; $last++; } $last = $#letters - $last; @letters = @letters[$first..$last]; my $asstr = join('',@letters); if ((my @gaps) = ($asstr =~ m/$regex/o)) { my $len = length(join('',@gaps)) / $reduce; if ($len > $biglen) { unshift(@in_the_running, [ @letters ]); $biglen = length(join('',@gaps)); } elsif ($len == $biglen) { unshift(@in_the_running, [ @letters ]); } else { push(@in_the_running, [ @letters ]); } } } #print @{$in_the_running[0]}, "\n"; push @hyph_padded_seq, join('',@{$in_the_running[0]}); my @base = @{$in_the_running[0]}; for my $seqno (1..$#in_the_running) { # my @seq = split(//,$in_the_running[$seqno]); my @seq = @{$in_the_running[$seqno]}; my $count = $#base; my @disp; for my $q (reverse @base) { if (($seq[$#seq] eq $q) || ($count == $#seq)) { push(@disp, pop(@seq)); } else { push(@disp, '-' x $reduce); } $count--; } push @hyph_padded_seq, join('', reverse(@disp)); #print reverse(@disp), "\n"; } return @hyph_padded_seq; } # A hash version can be found here: http://paste.phpfi.com/61624
Regards,
Edward

In reply to Re^2: Simple String Alignment by monkfan
in thread Simple String Alignment by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.