Package Bio :: Package Phylo :: Module TreeConstruction :: Class _Matrix
[hide private]
[frames] | no frames]

Class _Matrix

source code

object --+
         |
        _Matrix
Known Subclasses:

Base class for distance matrix or scoring matrix

Accepts a list of names and a lower triangular matrix.:

matrix = [[0],
          [1, 0],
          [2, 3, 0],
          [4, 5, 6, 0]]
represents the symmetric matrix of
[0,1,2,4]
[1,0,3,5]
[2,3,0,6]
[4,5,6,0]

Example

>>> from Bio.Phylo.TreeConstruction import _Matrix
>>> names = ['Alpha', 'Beta', 'Gamma', 'Delta']
>>> matrix = [[0], [1, 0], [2, 3, 0], [4, 5, 6, 0]]
>>> m = _Matrix(names, matrix)
>>> m
_Matrix(names=['Alpha', 'Beta', 'Gamma', 'Delta'], matrix=[[0], [1, 0], [2, 3, 0], [4, 5, 6, 0]])

You can use two indices to get or assign an element in the matrix.

>>> m[1,2]
3
>>> m['Beta','Gamma']
3
>>> m['Beta','Gamma'] = 4
>>> m['Beta','Gamma']
4

Further more, you can use one index to get or assign a list of elements related to that index.

>>> m[0]
[0, 1, 2, 4]
>>> m['Alpha']
[0, 1, 2, 4]
>>> m['Alpha'] = [0, 7, 8, 9]
>>> m[0]
[0, 7, 8, 9]
>>> m[0,1]
7

Also you can delete or insert a column&row of elemets by index.

>>> m
_Matrix(names=['Alpha', 'Beta', 'Gamma', 'Delta'], matrix=[[0], [7, 0], [8, 4, 0], [9, 5, 6, 0]])
>>> del m['Alpha']
>>> m
_Matrix(names=['Beta', 'Gamma', 'Delta'], matrix=[[0], [4, 0], [5, 6, 0]])
>>> m.insert('Alpha', [0, 7, 8, 9] , 0)
>>> m
_Matrix(names=['Alpha', 'Beta', 'Gamma', 'Delta'], matrix=[[0], [7, 0], [8, 4, 0], [9, 5, 6, 0]])
Instance Methods [hide private]
 
__init__(self, names, matrix=None)
Initialize matrix by a list of names and a list of lower triangular matrix data
source code
 
__getitem__(self, item)
Access value(s) by the index(s) or name(s).
source code
 
__setitem__(self, item, value)
Set value by the index(s) or name(s).
source code
 
__delitem__(self, item)
Delete related distances by the index or name
source code
 
insert(self, name, value, index=None)
Insert distances given the name and value.
source code
 
__len__(self)
Matrix length
source code
 
__repr__(self)
repr(x)
source code
 
__str__(self)
Get a lower triangular matrix string
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, names, matrix=None)
(Constructor)

source code 
Initialize matrix by a list of names and a list of lower triangular matrix data
Parameters:
  • names (list) - names of elements, used for indexing
  • matrix (list) - nested list of numerical lists in lower triangular format
Overrides: object.__init__

__getitem__(self, item)
(Indexing operator)

source code 

Access value(s) by the index(s) or name(s).

For a _Matrix object 'dm':

dm[i]                   get a value list from the given 'i' to others;
dm[i, j]                get the value between 'i' and 'j';
dm['name']              map name to index first
dm['name1', 'name2']    map name to index first

__setitem__(self, item, value)
(Index assignment operator)

source code 

Set value by the index(s) or name(s).

Similar to __getitem__:

dm[1] = [1, 0, 3, 4]    set values from '1' to others;
dm[i, j] = 2            set the value from 'i' to 'j'

insert(self, name, value, index=None)

source code 
Insert distances given the name and value.
Parameters:
  • name (str) - name of a row/col to be inserted
  • value (list) - a row/col of values to be inserted

__repr__(self)
(Representation operator)

source code 
repr(x)

Overrides: object.__repr__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 
Get a lower triangular matrix string
Overrides: object.__str__