Package Bio :: Package CAPS
[hide private]
[frames] | no frames]

Source Code for Package Bio.CAPS

  1  # Copyright 2005 by Jonathan Taylor. 
  2  # All rights reserved. 
  3  # This code is part of the Biopython distribution and governed by its 
  4  # license.  Please see the LICENSE file that should have been included 
  5  # as part of this package. 
  6  """This module deals with CAPS markers. 
  7   
  8  A CAPS marker is a location a DifferentialCutsite as described below and a 
  9  set of primers that can be used to visualize this.  More information can 
 10  be found in the paper `Konieczny and Ausubel (1993)`_ (PMID 8106085). 
 11   
 12  .. _`Konieczny and Ausubel (1993)`: http://dx.doi.org/10.1046/j.1365-313X.1993.04020403.x 
 13  """ 
 14   
 15   
16 -class DifferentialCutsite(object):
17 """Differential enzyme cutsite in an alignment. 18 19 A differential cutsite is a location in an alignment where an enzyme cuts 20 at least one sequence and also cannot cut at least one other sequence. 21 22 Members: 23 - start - Where it lives in the alignment. 24 - enzyme - The enzyme that causes this. 25 - cuts_in - A list of sequences (as indexes into the alignment) the 26 enzyme cuts in. 27 - blocked_in - A list of sequences (as indexes into the alignment) the 28 enzyme is blocked in. 29 30 """ 31
32 - def __init__(self, **kwds):
33 """Initialize a DifferentialCutsite. 34 35 Each member (as listed in the class description) should be included as a 36 keyword. 37 """ 38 self.start = int(kwds["start"]) 39 self.enzyme = kwds["enzyme"] 40 self.cuts_in = kwds["cuts_in"] 41 self.blocked_in = kwds["blocked_in"]
42 43
44 -class AlignmentHasDifferentLengthsError(Exception):
45 pass
46 47
48 -class CAPSMap(object):
49 """A map of an alignment showing all possible dcuts. 50 51 Members: 52 - alignment - The alignment that is mapped. 53 - dcuts - A list of possible CAPS markers in the form of 54 DifferentialCutsites. 55 """ 56
57 - def __init__(self, alignment, enzymes=None):
58 """Initialize the CAPSMap. 59 60 Required: 61 - alignment - The alignment to be mapped. 62 63 Optional: 64 - enzymes - List of enzymes to be used to create the map. 65 Defaults to an empty list. 66 """ 67 if enzymes is None: 68 enzymes = [] 69 self.sequences = [rec.seq for rec in alignment] 70 self.size = len(self.sequences) 71 self.length = len(self.sequences[0]) 72 for seq in self.sequences: 73 if len(seq) != self.length: 74 raise AlignmentHasDifferentLengthsError 75 76 self.alignment = alignment 77 self.enzymes = enzymes 78 79 # look for dcuts 80 self._digest()
81
82 - def _digest_with(self, enzyme):
83 cuts = [] # list of lists, one per sequence 84 all = [] 85 86 # go through each sequence 87 for seq in self.sequences: 88 # grab all the cuts in the sequence 89 seq_cuts = [cut - enzyme.fst5 for cut in enzyme.search(seq)] 90 # maintain a list of all cuts in all sequences 91 all.extend(seq_cuts) 92 cuts.append(seq_cuts) 93 94 # we sort the all list and remove duplicates 95 all.sort() 96 97 last = -999 98 new = [] 99 for cut in all: 100 if cut != last: 101 new.append(cut) 102 last = cut 103 all = new 104 # all now has indices for all sequences in the alignment 105 106 for cut in all: 107 # test for dcuts 108 109 cuts_in = [] 110 blocked_in = [] 111 112 for i in range(0, self.size): 113 seq = self.sequences[i] 114 if cut in cuts[i]: 115 cuts_in.append(i) 116 else: 117 blocked_in.append(i) 118 119 if cuts_in != [] and blocked_in != []: 120 self.dcuts.append(DifferentialCutsite(start=cut, 121 enzyme=enzyme, 122 cuts_in=cuts_in, 123 blocked_in=blocked_in))
124
125 - def _digest(self):
126 self.dcuts = [] 127 128 for enzyme in self.enzymes: 129 self._digest_with(enzyme)
130