[Bioperl-l] sorting and indexing matricies

Shawn Hoon shawnh at fugu-sg.org
Tue Jun 24 16:45:40 EDT 2003


Hi there,
THere  is also a module for representing PhylipDist matrices in bioperl
if u want to pull out columns or rows, this might be useful etc
This is normally returned by the Bio::Tools::Phylo::Phylip::ProtDist
parser but you can pass in a protdist formatted file and it will 
populate
the object.

          use Bio::Tools::Phylo::Phylip::ProtDist;

	my $dist = Bio::Tools::Phylo::Phylip::ProtDist->new(
            -file=>"protdist.out",
            -program=>"ProtDist");
          #or
           my $dist = Bio::Tools::Phylo::Phylip::ProtDist->new(
            -fh=>"protdist.out",
            -program=>"ProtDist");

          #get specific entries
          my $distance_value = $dist->get_entry('ALPHA','BETA');
          my @columns        = $dist->get_column('ALPHA');
          my @rows           = $dist->get_row('BETA');
          my @diagonal       = $dist->get_diagonal();

          #print the matrix in phylip numerical format
          print $dist->print_matrix;


DESCRIPTION
        Simple object for holding Distance Matrices generated by
        the following Phylip programs:

        1) dnadist 2) protdist 3) restdist

        It currently handles parsing of the matrix without the
        data output option.

            5
        Alpha          0.00000  4.23419  3.63330  6.20865  3.45431
        Beta            4.23419  0.00000  3.49289  3.36540  4.29179
        Gamma      3.63330  3.49289  0.00000  3.68733  5.84929
        Delta           6.20865  3.36540  3.68733  0.00000  4.43345
        Epsilon       3.45431  4.29179  5.84929  4.43345  0.00000


it will work if your matrix looks like the output above


hth,

Shawn


On Wednesday, June 25, 2003, at 08:31  AM, Morten Lindow wrote:

> I think this might help,
> (but this _BIO_perl list is probably not the proper place to ask..?)
>
> PD Schloss wrote:
>
>> Hello,
>>
>> I have a distance matrix that I am working with and I would like to 
>> sort
>> it so that the columns and rows containing the smallest distance are 
>> in
>> the first and second rows and columns.  I understand how to sort a
>> simple array, however, I am confused as to how I do it with an array 
>> of
>> arrays.  I'm a beginner so I realize that constructing a matrix this 
>> way
>> may not be the most efficient, but at this point I think I would 
>> prefer
>> to do it this way.  If you have any thoughts on how to do this type of
>> sort, I'd appreciate it.
>>
> You can use the sort function - there is a good explanation in the 
> camel-book, otherwise see this example I use for sorting 
> commaseparated text files.
>
> #!/usr/bin/perl
>
> #Read comma separated values from STDIN. Output sorted
>
>
> use warnings;
>
>
> my @lines = <STDIN>;
> my $header = shift @lines;
>
> my @temp = map { [$_, split /,/] } @lines;
>
> @temp = sort {
>        @a_fields = @$a[1..$#$a];
>        @b_fields = @$b[1..$#$b];
>
>        $a_fields[1] cmp $b_fields[1]
>                ||
>        $a_fields[3] <=> $b_fields[3]
>                ||
>        $a_fields[0] cmp $b_fields[0]
>        } @temp;
>
> my @sorted_lines = map { $_->[0] } @temp;
> print STDERR "\nHeader line following:\n$header\n";
> print $header;
> print @sorted_lines;
>
> exit;
>
> --
> Morten
>
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org
> http://portal.open-bio.org/mailman/listinfo/bioperl-l
>



More information about the Bioperl-l mailing list