Re: Calculating values of a 2d array by comparison of 2 strings



Pepetideo <pepetideo@xxxxxxxxxxx> wrote in comp.lang.perl.misc:
anno4000@xxxxxxxxxxxxxxxxxxxxxx wrote:
Pepetideo <pepetideo@xxxxxxxxxxx> wrote in comp.lang.perl.misc:
Hi. I am having a hard time dealing with a problem And I was wondering
if anyone has a suggestion to how I should do this.

I have 2 strings that I want to compare. These strings are composed of
amino acid residues (long sequences of any of 20 characters) these
string are aligned to each other and I want to compare each position of
the two strings and fill up a matrix of 21*21 so that for eg:

AAAAAA....
ATLL_Y....

1 pos: A-A -> counts 1 and adds to position [0][0] on the matrix
2 pos: A-T -> counts 1 and adds to position [0][1] on the matrix
3 pos: A-L -> counts 1 and adds to position [0][2] on the matrix
4 pos: A-L -> counts 1 and adds to position [0][2] on the matrix
5 pos: A-_ -> counts 1 and adds to position [0][21] on the matrix
6 pos: A-Y -> counts 1 and adds to position [0][10] on the matrix
...


I've been suggested by a friend to use a switch but this ends up needing
441 cases of assignments in other to fill up every position of the matrix.

Does anyone know of a better way of dealing with this? I appreciate any
suggestions. Thanks

Probably. Use a two-dimensional hash to count the combinations
directly, without mapping the letters to array indices. Here is how:

my $str1 = 'AAAAAA';
my $str2 = 'ATLL_Y';

my %count;

my @str1 = split //, $str1;
my @str2 = split //, $str2;

for my $ch1 ( @str1 ) {
my $ch2 = shift @str2;
++ $count{ $ch1}->{ $ch2};
}

# example:
print "The count for 'A' and 'L' is $count{ A}->{ L}\n";

If you do need the array form you can generate it from the count hash.

Anno

Hi Anno.

Thank you very much for the suggestion... I think this is a good idea...
My doubt is that I am going this to many sequences and the arrays have
positions have to be the same...

I don't understand. The only difference between the hash and the
array is that the "positions" in the hash are directly determined
by the letters. In the array the letters are first mapped to numbers.
That's one more degree of indirection.

in order th convert the count ashe to
the matrix would i not need to create 431 assignment statements like :

@matrix[0][0] = $count{a}->{l};

No, that's what programming languages are for: Automate otherwise tedious
processes.

Just create another hash that holds the index for each key:

my %index_tab;
@index_tab{ qw( A T L ... Y ... _)} = 0 .. 21;

Then set up the count matrix like this:

for my $first ( keys %count ) {
for my $second ( keys %{ $count{ $first} } ) {
$matrix[ $index_tab{ $first}]->[ $index_tab{ $second}] =
$count{ $first}->{ $second};
}
}

I'm not convinced you actually need it. It may take a bit to get
used to using strings (single letters in this case) instead of integer
indices but they work exactly the same.

Anno
.



Relevant Pages

  • Re: Traversing a hash with array refs as keys?
    ... Hash keys can only be strings. ... So is your key an array or an array reference? ... As mentioned above keys must be strings. ...
    (comp.lang.perl.misc)
  • Re: Calculating values of a 2d array by comparison of 2 strings
    ... I have 2 strings that I want to compare. ... If you do need the array form you can generate it from the count hash. ... Thank you very much for the suggestion... ...
    (comp.lang.perl.misc)
  • Re: Searching a sorted array of strings
    ... reasonable thing to request in a programming language. ... Hash is easy in Perl doesn't make it an array of strings. ...
    (comp.lang.perl.misc)
  • Re: [9fans] rc funny
    ... decision Plan B should lift by representing, as I believe Rob Pike ... paths as arrays of strings rather than slash delimited ... your suggestion to use an array of char* ... even if you turn this into an array of char* inside a program, ...
    (comp.os.plan9)
  • Re: Searching a sorted array of strings
    ... Hash is easy in Perl doesn't make it an array of strings. ...
    (comp.lang.perl.misc)