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 39 self.start = int(kwds["start"]) 40 self.enzyme = kwds["enzyme"] 41 self.cuts_in = kwds["cuts_in"] 42 self.blocked_in = kwds["blocked_in"]
43 44
45 -class AlignmentHasDifferentLengthsError(Exception):
46 pass
47 48
49 -class CAPSMap(object):
50 """A map of an alignment showing all possible dcuts. 51 52 Members: 53 - alignment - The alignment that is mapped. 54 - dcuts - A list of possible CAPS markers in the form of 55 DifferentialCutsites. 56 """ 57
58 - def __init__(self, alignment, enzymes=None):
59 """Initialize the CAPSMap. 60 61 Required: 62 - alignment - The alignment to be mapped. 63 64 Optional: 65 - enzymes - List of enzymes to be used to create the map. 66 Defaults to an empty list. 67 """ 68 if enzymes is None: 69 enzymes = [] 70 self.sequences = [rec.seq for rec in alignment] 71 self.size = len(self.sequences) 72 self.length = len(self.sequences[0]) 73 for seq in self.sequences: 74 if len(seq) != self.length: 75 raise AlignmentHasDifferentLengthsError 76 77 self.alignment = alignment 78 self.enzymes = enzymes 79 80 # look for dcuts 81 self._digest()
82
83 - def _digest_with(self, enzyme):
84 cuts = [] # list of lists, one per sequence 85 all = [] 86 87 # go through each sequence 88 for seq in self.sequences: 89 # grab all the cuts in the sequence 90 seq_cuts = [cut - enzyme.fst5 for cut in enzyme.search(seq)] 91 # maintain a list of all cuts in all sequences 92 all.extend(seq_cuts) 93 cuts.append(seq_cuts) 94 95 # we sort the all list and remove duplicates 96 all.sort() 97 98 last = -999 99 new = [] 100 for cut in all: 101 if cut != last: 102 new.append(cut) 103 last = cut 104 all = new 105 # all now has indices for all sequences in the alignment 106 107 for cut in all: 108 # test for dcuts 109 110 cuts_in = [] 111 blocked_in = [] 112 113 for i in range(0, self.size): 114 seq = self.sequences[i] 115 if cut in cuts[i]: 116 cuts_in.append(i) 117 else: 118 blocked_in.append(i) 119 120 if cuts_in != [] and blocked_in != []: 121 self.dcuts.append(DifferentialCutsite(start=cut, 122 enzyme=enzyme, 123 cuts_in=cuts_in, 124 blocked_in=blocked_in))
125
126 - def _digest(self):
127 self.dcuts = [] 128 129 for enzyme in self.enzymes: 130 self._digest_with(enzyme)
131