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  from __future__ import print_function 
 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   
28 -class AlignmentColumnFullException(Exception):
29 pass
30 31
32 -class Alignment(list):
33 - def append(self, column_unit):
34 try: 35 self[-1].append(column_unit) 36 except AlignmentColumnFullException: 37 list.append(self, AlignmentColumn(column_unit)) 38 except IndexError: 39 list.append(self, AlignmentColumn(column_unit))
40 41
42 -class AlignmentColumn(list):
43 - def _set_kind(self, column_unit):
44 if self.kind == "SEQUENCE": 45 self.kind = column_unit.kind
46
47 - def __init__(self, column_unit):
48 assert column_unit.unit == 0 49 self.kind = column_unit.kind 50 list.__init__(self, [column_unit.column, None])
51
52 - def __repr__(self):
53 return "%s(%s, %s)" % (self.kind, self[0], self[1])
54
55 - def append(self, column_unit):
56 if self[1] is not None: 57 raise AlignmentColumnFullException 58 59 assert column_unit.unit == 1 60 61 self._set_kind(column_unit) 62 self[1] = column_unit.column
63 64
65 -class ColumnUnit(object):
66 - def __init__(self, unit, column, kind):
67 self.unit = unit 68 self.column = column 69 self.kind = kind
70
71 - def __str__(self):
72 return "ColumnUnit(unit=%s, column=%s, %s)" % (self.unit, self.column, self.kind)
73 74 __repr__ = __str__
75 76 _re_unit = re.compile(r"^Unit +([01])- \[ *(-?\d+)- *(-?\d+)\] \[(\w+)\]$") 77 78
79 -def parse_line(line):
80 """ 81 >>> print(parse_line("Column 0:")) 82 None 83 >>> parse_line("Unit 0- [ -1- 0] [SEQUENCE]") 84 ColumnUnit(unit=0, column=0, SEQUENCE) 85 >>> parse_line("Unit 1- [ 85- 86] [SEQUENCE]") 86 ColumnUnit(unit=1, column=86, SEQUENCE) 87 """ 88 match = _re_unit.match(line.rstrip()) 89 90 if not match: 91 return 92 93 return ColumnUnit(int(match.group(1)), int(match.group(3)), match.group(4))
94 95
96 -def parse(iterable):
97 """ 98 format 99 100 Column 0: 101 Unit 0- [ -1- 0] [SEQUENCE] 102 Unit 1- [ 85- 86] [SEQUENCE] 103 104 means that seq1[0] == seq2[86] (0-based) 105 """ 106 107 alignment = Alignment() 108 for line in iterable: 109 try: 110 if os.environ["WISE_PY_DEBUG"]: 111 print(line) 112 except KeyError: 113 pass 114 115 column_unit = parse_line(line) 116 if column_unit: 117 alignment.append(column_unit) 118 119 return alignment
120 121
122 -def align(pair, 123 scores=None, 124 gap_start=None, 125 gap_extension=None, 126 *args, **keywds):
127 128 cmdline = _CMDLINE_PSW[:] 129 if scores: 130 cmdline.extend((_OPTION_SCORES, scores)) 131 if gap_start: 132 cmdline.extend((_OPTION_GAP_START, str(gap_start))) 133 if gap_extension: 134 cmdline.extend((_OPTION_GAP_EXTENSION, str(gap_extension))) 135 temp_file = Wise.align(cmdline, pair, *args, **keywds) 136 return parse(temp_file)
137 138
139 -def main():
140 print(align(sys.argv[1:3]))
141 142
143 -def _test(*args, **keywds):
144 import doctest 145 doctest.testmod(sys.modules[__name__], *args, **keywds)
146 147 if __name__ == "__main__": 148 if __debug__: 149 _test() 150 main() 151