Package Bio :: Package Wise :: Module psw
[hide private]
[frames] | no frames]

Source Code for Module Bio.Wise.psw

  1  #!/usr/bin/env python 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  # 
  6  # Bio.Wise contains modules for running and processing the output of 
  7  # some of the models in the Wise2 package by Ewan Birney available from: 
  8  # ftp://ftp.ebi.ac.uk/pub/software/unix/wise2/ 
  9  # http://www.ebi.ac.uk/Wise2/ 
 10  #  
 11  # Bio.Wise.psw is for protein Smith-Waterman alignments 
 12  # Bio.Wise.dnal is for Smith-Waterman DNA alignments 
 13   
 14  __version__ = "$Revision: 1.5 $" 
 15   
 16  import os 
 17  import re 
 18  import sys 
 19   
 20  from Bio import Wise 
 21   
 22  _CMDLINE_PSW = ["psw", "-l", "-F"] 
 23  _OPTION_GAP_START = "-g" 
 24  _OPTION_GAP_EXTENSION = "-e" 
 25  _OPTION_SCORES = "-m" 
 26   
27 -class AlignmentColumnFullException(Exception):
28 pass
29
30 -class Alignment(list):
31 - def append(self, column_unit):
32 try: 33 self[-1].append(column_unit) 34 except AlignmentColumnFullException: 35 list.append(self, AlignmentColumn(column_unit)) 36 except IndexError: 37 list.append(self, AlignmentColumn(column_unit))
38
39 -class AlignmentColumn(list):
40 - def _set_kind(self, column_unit):
41 if self.kind == "SEQUENCE": 42 self.kind = column_unit.kind
43
44 - def __init__(self, column_unit):
45 assert column_unit.unit == 0 46 self.kind = column_unit.kind 47 list.__init__(self, [column_unit.column, None])
48
49 - def __repr__(self):
50 return "%s(%s, %s)" % (self.kind, self[0], self[1])
51
52 - def append(self, column_unit):
53 if self[1] is not None: 54 raise AlignmentColumnFullException 55 56 assert column_unit.unit == 1 57 58 self._set_kind(column_unit) 59 self[1] = column_unit.column
60
61 -class ColumnUnit(object):
62 - def __init__(self, unit, column, kind):
63 self.unit = unit 64 self.column = column 65 self.kind = kind
66
67 - def __str__(self):
68 return "ColumnUnit(unit=%s, column=%s, %s)" % (self.unit, self.column, self.kind)
69 70 __repr__ = __str__
71 72 _re_unit = re.compile(r"^Unit +([01])- \[ *(-?\d+)- *(-?\d+)\] \[(\w+)\]$")
73 -def parse_line(line):
74 """ 75 >>> print parse_line("Column 0:") 76 None 77 >>> parse_line("Unit 0- [ -1- 0] [SEQUENCE]") 78 ColumnUnit(unit=0, column=0, SEQUENCE) 79 >>> parse_line("Unit 1- [ 85- 86] [SEQUENCE]") 80 ColumnUnit(unit=1, column=86, SEQUENCE) 81 """ 82 match = _re_unit.match(line.rstrip()) 83 84 if not match: 85 return 86 87 return ColumnUnit(int(match.group(1)), int(match.group(3)), match.group(4))
88
89 -def parse(iterable):
90 """ 91 format 92 93 Column 0: 94 Unit 0- [ -1- 0] [SEQUENCE] 95 Unit 1- [ 85- 86] [SEQUENCE] 96 97 means that seq1[0] == seq2[86] (0-based) 98 """ 99 100 alignment = Alignment() 101 for line in iterable: 102 try: 103 if os.environ["WISE_PY_DEBUG"]: 104 print line, 105 except KeyError: 106 pass 107 108 column_unit = parse_line(line) 109 if column_unit: 110 alignment.append(column_unit) 111 112 return alignment
113
114 -def align(pair, 115 scores=None, 116 gap_start=None, 117 gap_extension=None, 118 *args, **keywds):
119 120 cmdline = _CMDLINE_PSW[:] 121 if scores: 122 cmdline.extend((_OPTION_SCORES, scores)) 123 if gap_start: 124 cmdline.extend((_OPTION_GAP_START, str(gap_start))) 125 if gap_extension: 126 cmdline.extend((_OPTION_GAP_EXTENSION, str(gap_extension))) 127 temp_file = Wise.align(cmdline, pair, *args, **keywds) 128 return parse(temp_file)
129
130 -def main():
131 print align(sys.argv[1:3])
132
133 -def _test(*args, **keywds):
134 import doctest, sys 135 doctest.testmod(sys.modules[__name__], *args, **keywds)
136 137 if __name__ == "__main__": 138 if __debug__: 139 _test() 140 main() 141