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