1
1
#!/usr/bin/env python
2
2
# Very slow. See compare.pyx
4
from __future__ import division
5
import cogent.util.progress_display as UI
6
from cogent.util.modules import importVersionedModule, ExpectedImportError
4
8
__author__ = "Peter Maxwell"
5
9
__copyright__ = "Copyright 2007-2009, The Cogent Project"
6
10
__credits__ = ["Peter Maxwell", "Gavin Huttley"]
9
13
__maintainer__ = "Gavin Huttley"
10
14
__email__ = "gavin.huttley@anu.edu.au"
11
15
__status__ = "Production"
13
def dotplot(seq1, seq2, window, threshold, min_gap_length=0, band=None, progress=False):
17
def py_segments_from_diagonal(seq1, seq2, window, threshold, min_gap_length,
23
(i_lo, i_hi) = max(0, -diagonal), min(len(seq1), len(seq2)-diagonal)
24
for i in range(i_lo, i_hi):
28
scores[k] = seq1[i] == seq2[j]
30
if score >= threshold:
32
start = max(i_lo, i - window)
33
if d_segments and start-d_segments[-1][1] < min_gap_length:
34
(start, jumped_end) = d_segments.pop()
38
d_segments.append((start, i))
41
d_segments.append((start, i_hi))
46
_compare = importVersionedModule('_compare', globals(),
47
(1, 3), "slow Python dotplot")
48
segments_from_diagonal = _compare.segments_from_diagonal
49
except ExpectedImportError:
50
segments_from_diagonal = py_segments_from_diagonal
53
def dotplot(seq1, seq2, window, threshold, min_gap_length=0, band=None, ui=None):
14
54
"""A list of line segments covering the window-mers with identical matches > threshold
16
56
Gaps of size less than min_gap will be hidden, which saves on line segments.
17
57
if 'band' is not None then it limits the searched area
59
def one_diagonal(dia):
60
segs = segments_from_diagonal(seq1, seq2, window, threshold,
62
return [((start, start+dia), (end, end+dia)) for (start, end) in segs]
22
65
band = max(len(seq1), len(seq2))
23
for diagonal in range(-min(len(seq1), band), min(len(seq2), band)+1):
28
(i_lo, i_hi) = max(0, -diagonal), min(len(seq1), len(seq2)-diagonal)
30
if diagonal %100 == 0:
31
print diagonal, (i_lo, i_lo+diagonal), (i_hi, i_hi+diagonal), len(result)
32
for i in range(i_lo, i_hi):
36
scores[k] = seq1[i] == seq2[j]
38
if score >= threshold:
40
start = max(i_lo, i - window)
45
if d_segments and start-d_segments[-1][1] < min_gap_length:
46
(start, jumped_end) = d_segments.pop()
47
d_segments.append((start, end))
51
d_segments.append((start, end))
53
for (start, end) in d_segments:
54
result.append(((start, start+diagonal), (end, end+diagonal)))
66
diagonals = range(-min(len(seq1), band), min(len(seq2), band)+1)
68
for diag_segments in ui.imap(one_diagonal, diagonals, noun='offset'):
69
result.extend(diag_segments)