Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions
Bio.SubsMat.SeqMat Class Reference
Inheritance diagram for Bio.SubsMat.SeqMat:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def make_entropy
def sum
def print_full_mat
def print_mat
def __str__
def __sub__
def __mul__
def __add__

Public Attributes

 alphabet
 ab_list
 mat_name
 sum_letters
 relative_entropy
 entropy

Private Member Functions

def _alphabet_from_matrix
def _correct_matrix
def _full_to_half
def _init_zero

Detailed Description

A Generic sequence matrix class
The key is a 2-tuple containing the letter indices of the matrix. Those
should be sorted in the tuple (low, high). Because each matrix is dealt
with as a half-matrix.

Definition at line 134 of file __init__.py.


Constructor & Destructor Documentation

def Bio.SubsMat.SeqMat.__init__ (   self,
  data = None,
  alphabet = None,
  mat_name = '',
  build_later = 0 
)

Definition at line 150 of file __init__.py.

00150 
00151    def __init__(self, data=None, alphabet=None, mat_name='', build_later=0):
00152       # User may supply:
00153       # data: matrix itself
00154       # mat_name: its name. See below.
00155       # alphabet: an instance of Bio.Alphabet, or a subclass. If not
00156       # supplied, constructor builds its own from that matrix."""
00157       # build_later: skip the matrix size assertion. User will build the
00158       # matrix after creating the instance. Constructor builds a half matrix
00159       # filled with zeroes.
00160 
00161       assert type(mat_name) == type('')
00162 
00163       # "data" may be:
00164       # 1) None --> then self.data is an empty dictionary
00165       # 2) type({}) --> then self takes the items in data
00166       # 3) An instance of SeqMat
00167       # This whole creation-during-execution is done to avoid changing
00168       # default values, the way Python does because default values are
00169       # created when the function is defined, not when it is created.
00170       if data:
00171           try:
00172               self.update(data)
00173           except ValueError:
00174               raise ValueError, "Failed to store data in a dictionary"
00175       if alphabet == None:
00176          alphabet = Alphabet.Alphabet()
00177       assert Alphabet.generic_alphabet.contains(alphabet)
00178       self.alphabet = alphabet
00179 
00180       # If passed alphabet is empty, use the letters in the matrix itself
00181       if not self.alphabet.letters:
00182          self._alphabet_from_matrix()
00183       # Assert matrix size: half or full
00184       if not build_later:
00185          N = len(self.alphabet.letters)
00186          assert len(self) == N**2 or len(self) == N*(N+1)/2
00187       self.ab_list = list(self.alphabet.letters)
00188       self.ab_list.sort()
00189       # Names: a string like "BLOSUM62" or "PAM250"
00190       self.mat_name = mat_name
00191       if build_later:
00192          self._init_zero()
00193       else:
00194          # Convert full to half
00195          self._full_to_half()
00196          self._correct_matrix()
00197       self.sum_letters = {}
00198       self.relative_entropy = 0

Here is the call graph for this function:


Member Function Documentation

def Bio.SubsMat.SeqMat.__add__ (   self,
  other 
)

Definition at line 353 of file __init__.py.

00353 
00354    def __add__(self, other):
00355       new_mat = copy.copy(self)
00356       for i in self:
00357          new_mat[i] += other[i]
00358       return new_mat

def Bio.SubsMat.SeqMat.__mul__ (   self,
  other 
)
returns a matrix for which each entry is the multiplication product of the
two matrices passed

Definition at line 345 of file __init__.py.

00345 
00346    def __mul__(self,other):
00347       """ returns a matrix for which each entry is the multiplication product of the
00348       two matrices passed"""
00349       new_mat = copy.copy(self)
00350       for i in self:
00351          new_mat[i] *= other[i]
00352       return new_mat

Print a nice half-matrix.

Definition at line 316 of file __init__.py.

00316 
00317    def __str__(self):
00318       """Print a nice half-matrix."""
00319       output = ""
00320       alphabet = self.ab_list
00321       n = len(alphabet)
00322       for i in range(n):
00323          c1 = alphabet[i]
00324          output += c1
00325          for j in range(i+1):
00326             c2 = alphabet[j]
00327             try:
00328                val = self[c2,c1]
00329             except KeyError:
00330                val = self[c1,c2]
00331             if val == -999:
00332                output += '  ND' 
00333             else:
00334                output += "%4d" % val
00335          output += '\n'
00336       output += '%4s' * n % tuple(alphabet) + "\n"
00337       return output

def Bio.SubsMat.SeqMat.__sub__ (   self,
  other 
)
returns a number which is the subtraction product of the two matrices

Definition at line 338 of file __init__.py.

00338 
00339    def __sub__(self,other):
00340       """ returns a number which is the subtraction product of the two matrices"""
00341       mat_diff = 0
00342       for i in self:
00343          mat_diff += (self[i] - other[i])
00344       return mat_diff

def Bio.SubsMat.SeqMat._alphabet_from_matrix (   self) [private]

Definition at line 140 of file __init__.py.

00140 
00141    def _alphabet_from_matrix(self):
00142       ab_dict = {}
00143       s = ''
00144       for i in self:
00145          ab_dict[i[0]] = 1
00146          ab_dict[i[1]] = 1
00147       for i in sorted(ab_dict):
00148          s += i
00149       self.alphabet.letters = s

def Bio.SubsMat.SeqMat._correct_matrix (   self) [private]

Definition at line 199 of file __init__.py.

00199 
00200    def _correct_matrix(self):
00201       keylist = self.keys()
00202       for key in keylist:
00203          if key[0] > key[1]:
00204             self[(key[1],key[0])] = self[key]
00205             del self[key]

Here is the call graph for this function:

def Bio.SubsMat.SeqMat._full_to_half (   self) [private]
Convert a full-matrix to a half-matrix

Definition at line 206 of file __init__.py.

00206 
00207    def _full_to_half(self):
00208       """
00209       Convert a full-matrix to a half-matrix
00210       """
00211       # For instance: two entries ('A','C'):13 and ('C','A'):20 will be summed
00212       # into ('A','C'): 33 and the index ('C','A') will be deleted
00213       # alphabet.letters:('A','A') and ('C','C') will remain the same.
00214 
00215       N = len(self.alphabet.letters)
00216       # Do nothing if this is already a half-matrix
00217       if len(self) == N*(N+1)/2:
00218          return
00219       for i in self.ab_list:
00220          for j in self.ab_list[:self.ab_list.index(i)+1]:
00221             if i != j:
00222                self[j,i] = self[j,i] + self[i,j]
00223                del self[i,j]

def Bio.SubsMat.SeqMat._init_zero (   self) [private]

Definition at line 224 of file __init__.py.

00224 
00225    def _init_zero(self):
00226       for i in self.ab_list:
00227          for j in self.ab_list[:self.ab_list.index(i)+1]:
00228             self[j,i] = 0.

Definition at line 229 of file __init__.py.

00229 
00230    def make_entropy(self):
00231       self.entropy = 0
00232       for i in self:
00233          if self[i] > EPSILON:
00234             self.entropy += self[i]*log(self[i])/log(2)
00235       self.entropy = -self.entropy

def Bio.SubsMat.SeqMat.print_full_mat (   self,
  f = None,
  format = "%4d",
  topformat = "%4s",
  alphabet = None,
  factor = 1,
  non_sym = None 
)

Definition at line 250 of file __init__.py.

00250 
00251               alphabet=None,factor=1,non_sym=None):
00252       f = f or sys.stdout 
00253       # create a temporary dictionary, which holds the full matrix for
00254       # printing
00255       assert non_sym == None or type(non_sym) == type(1.) or \
00256       type(non_sym) == type(1)
00257       full_mat = copy.copy(self)
00258       for i in self:
00259          if i[0] != i[1]:
00260             full_mat[(i[1],i[0])] = full_mat[i]
00261       if not alphabet:
00262          alphabet = self.ab_list
00263       topline = ''
00264       for i in alphabet:
00265          topline = topline + topformat % i
00266       topline = topline + '\n'
00267       f.write(topline)
00268       for i in alphabet:
00269          outline = i
00270          for j in alphabet:
00271             if alphabet.index(j) > alphabet.index(i) and non_sym is not None:
00272                val = non_sym
00273             else:
00274                val = full_mat[i,j]
00275                val *= factor
00276             if val <= -999:
00277                cur_str = '  ND' 
00278             else:
00279                cur_str = format % val
00280             
00281             outline = outline+cur_str
00282          outline = outline+'\n'
00283          f.write(outline)

Here is the call graph for this function:

Here is the caller graph for this function:

def Bio.SubsMat.SeqMat.print_mat (   self,
  f = None,
  format = "%4d",
  bottomformat = "%4s",
  alphabet = None,
  factor = 1 
)
Print a nice half-matrix. f=sys.stdout to see on the screen
User may pass own alphabet, which should contain all letters in the
alphabet of the matrix, but may be in a different order. This
order will be the order of the letters on the axes

Definition at line 285 of file __init__.py.

00285 
00286               alphabet=None,factor=1):
00287       """Print a nice half-matrix. f=sys.stdout to see on the screen
00288       User may pass own alphabet, which should contain all letters in the
00289       alphabet of the matrix, but may be in a different order. This
00290       order will be the order of the letters on the axes"""
00291       
00292       f = f or sys.stdout
00293       if not alphabet:
00294          alphabet = self.ab_list
00295       bottomline = ''
00296       for i in alphabet:
00297          bottomline = bottomline + bottomformat % i
00298       bottomline = bottomline + '\n'
00299       for i in alphabet:
00300          outline = i
00301          for j in alphabet[:alphabet.index(i)+1]:
00302             try:
00303                val = self[j,i]
00304             except KeyError:
00305                val = self[i,j]
00306             val *= factor
00307             if val == -999:
00308                cur_str = '  ND' 
00309             else:
00310                cur_str = format % val
00311             
00312             outline = outline+cur_str
00313          outline = outline+'\n'
00314          f.write(outline)
00315       f.write(bottomline)

Here is the caller graph for this function:

def Bio.SubsMat.SeqMat.sum (   self)

Definition at line 236 of file __init__.py.

00236 
00237    def sum(self):
00238       result = {}
00239       for letter in self.alphabet.letters:
00240           result[letter] = 0.0
00241       for pair, value in self.iteritems():
00242           i1, i2 = pair
00243           if i1==i2:
00244               result[i1] += value
00245           else:
00246               result[i1] += value / 2
00247               result[i2] += value / 2
00248       return result

Here is the call graph for this function:


Member Data Documentation

Definition at line 186 of file __init__.py.

Definition at line 177 of file __init__.py.

Definition at line 230 of file __init__.py.

Definition at line 189 of file __init__.py.

Definition at line 197 of file __init__.py.

Definition at line 196 of file __init__.py.


The documentation for this class was generated from the following file: