scripts/last-dotplot.py
author Martin C. Frith
Mon Feb 01 08:09:02 2010 +0000 (2010-02-01)
changeset 95 f6c21d78378a
parent 67 d68ccbc06d73
child 128 3f6d0bf8c8dd
permissions -rwxr-xr-x
Enable dotplots to have zero margins.
Martin@1
     1
#! /usr/bin/env python
Martin@1
     2
Martin@1
     3
# Read LAST tabular output: write an "Oxford grid", a.k.a. dotplot.
Martin@1
     4
Martin@1
     5
# TODO: Currently, pixels with zero aligned nt-pairs are white, and
Martin@1
     6
# pixels with one or more aligned nt-pairs are black.  This can look
Martin@1
     7
# too crowded for large genome alignments.  I tried shading each pixel
Martin@1
     8
# according to the number of aligned nt-pairs within it, but the
Martin@1
     9
# result is too faint.  How can this be done better?
Martin@1
    10
Martin@1
    11
import sys, os, re, itertools, optparse, Image, ImageDraw, ImageFont
Martin@1
    12
Martin@1
    13
my_name = os.path.basename(sys.argv[0])
Martin@1
    14
usage = """
Martin@1
    15
  %prog --help
Martin@1
    16
  %prog [options] last-tabular-output dotplot.png
Martin@1
    17
  %prog [options] last-tabular-output dotplot.gif
Martin@1
    18
  etc."""
Martin@67
    19
parser = optparse.OptionParser(usage=usage)
Martin@51
    20
# Replace "width" & "height" with a single "length" option?
Martin@1
    21
parser.add_option("-x", "--width", type="int", dest="width", default=1000,
Martin@1
    22
                  help="maximum width in pixels (default: %default)")
Martin@1
    23
parser.add_option("-y", "--height", type="int", dest="height", default=1000,
Martin@1
    24
                  help="maximum height in pixels (default: %default)")
Martin@1
    25
parser.add_option("-f", "--fontfile", dest="fontfile",
Martin@1
    26
                  help="TrueType or OpenType font file")
Martin@1
    27
parser.add_option("-s", "--fontsize", type="int", dest="fontsize", default=11,
Martin@95
    28
                  help="TrueType or OpenType font size (default: %default)")
Martin@1
    29
(opts, args) = parser.parse_args()
Martin@1
    30
if len(args) != 2: parser.error("2 arguments needed")
Martin@1
    31
Martin@1
    32
if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
Martin@1
    33
else:              font = ImageFont.load_default()
Martin@1
    34
Martin@1
    35
# Make these options too?
Martin@1
    36
pix_tween_seqs = 3  # number of border pixels between sequences
Martin@1
    37
border_shade = 239  # the shade of grey to use for border pixels
Martin@1
    38
label_space = 5     # minimum number of pixels between axis labels
Martin@1
    39
Martin@1
    40
seq_size_dic1 = {}  # sizes of the first set of sequences
Martin@1
    41
seq_size_dic2 = {}  # sizes of the second set of sequences
Martin@1
    42
alignments = []
Martin@1
    43
Martin@1
    44
f = open(args[0])
Martin@1
    45
sys.stderr.write(my_name + ": reading alignments...\n")
Martin@1
    46
for line in f:
Martin@1
    47
    w = line.split()
Martin@1
    48
    if line.startswith('#') or not w: continue
Martin@1
    49
    seq1, pos1, strand1, size1 = w[1], int(w[2]), w[4], int(w[5])
Martin@1
    50
    seq2, pos2, strand2, size2 = w[6], int(w[7]), w[9], int(w[10])
Martin@1
    51
    blocks = w[11]
Martin@1
    52
    seq_size_dic1[seq1] = size1
Martin@1
    53
    seq_size_dic2[seq2] = size2
Martin@1
    54
    aln = seq1, seq2, pos1, pos2, strand1, strand2, blocks
Martin@1
    55
    alignments.append(aln)
Martin@1
    56
sys.stderr.write(my_name + ": done\n")
Martin@1
    57
f.close()
Martin@1
    58
Martin@1
    59
def natural_sort_key(my_string):
Martin@1
    60
    '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
Martin@1
    61
    parts = re.split(r'(\d+)', my_string)
Martin@1
    62
    parts[1::2] = map(int, parts[1::2])
Martin@1
    63
    return parts
Martin@1
    64
Martin@1
    65
def get_text_sizes(my_strings):
Martin@1
    66
    '''Get widths & heights, in pixels, of some strings.'''
Martin@95
    67
    if opts.fontsize == 0: return [(0, 0) for i in my_strings]
Martin@1
    68
    image_size = 1, 1
Martin@1
    69
    im = Image.new('L', image_size)
Martin@1
    70
    draw = ImageDraw.Draw(im)
Martin@1
    71
    return [draw.textsize(i, font=font) for i in my_strings]
Martin@1
    72
Martin@1
    73
def get_seq_info(seq_size_dic):
Martin@1
    74
    '''Return miscellaneous information about the sequences.'''
Martin@1
    75
    seq_names = seq_size_dic.keys()
Martin@1
    76
    seq_names.sort(key=natural_sort_key)
Martin@1
    77
    seq_sizes = [seq_size_dic[i] for i in seq_names]
Martin@1
    78
    name_sizes = get_text_sizes(seq_names)
Martin@28
    79
    margin = max(zip(*name_sizes)[1])  # maximum text height
Martin@1
    80
    return seq_names, seq_sizes, name_sizes, margin
Martin@1
    81
Martin@1
    82
seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
Martin@1
    83
seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
Martin@1
    84
Martin@1
    85
def div_ceil(x, y):
Martin@1
    86
    '''Return x / y rounded up.'''
Martin@1
    87
    q, r = divmod(x, y)
Martin@1
    88
    return q + (r != 0)
Martin@1
    89
Martin@1
    90
def tot_seq_pix(seq_sizes, bp_per_pix):
Martin@1
    91
    '''Return the total pixels needed for sequences of the given sizes.'''
Martin@28
    92
    return sum([div_ceil(i, bp_per_pix) for i in seq_sizes])
Martin@1
    93
Martin@1
    94
def get_bp_per_pix(seq_sizes, pix_limit):
Martin@1
    95
    '''Get the minimum bp-per-pixel that fits in the size limit.'''
Martin@1
    96
    seq_num = len(seq_sizes)
Martin@1
    97
    seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
Martin@1
    98
    if seq_pix_limit < seq_num:
Martin@1
    99
        sys.exit(my_name + ": can't fit the image: too many sequences?")
Martin@51
   100
    lower_bound = div_ceil(sum(seq_sizes), seq_pix_limit)
Martin@1
   101
    for bp_per_pix in itertools.count(lower_bound):  # slow linear search
Martin@1
   102
        if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
Martin@1
   103
    return bp_per_pix
Martin@1
   104
Martin@1
   105
sys.stderr.write(my_name + ": choosing bp per pixel...\n")
Martin@1
   106
bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
Martin@1
   107
bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
Martin@1
   108
bp_per_pix = max(bp_per_pix1, bp_per_pix2)
Martin@1
   109
sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
Martin@1
   110
Martin@1
   111
def get_seq_starts(seq_pix, pix_tween_seqs, margin):
Martin@1
   112
    '''Get the start pixel for each sequence.'''
Martin@1
   113
    seq_starts = []
Martin@1
   114
    pix_tot = margin - pix_tween_seqs
Martin@1
   115
    for i in seq_pix:
Martin@1
   116
        pix_tot += pix_tween_seqs
Martin@1
   117
        seq_starts.append(pix_tot)
Martin@1
   118
        pix_tot += i
Martin@1
   119
    return seq_starts
Martin@1
   120
Martin@1
   121
def get_pix_info(seq_sizes, margin):
Martin@1
   122
    '''Return pixel information about the sequences.'''
Martin@1
   123
    seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
Martin@1
   124
    seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
Martin@1
   125
    tot_pix = seq_starts[-1] + seq_pix[-1]
Martin@1
   126
    return seq_pix, seq_starts, tot_pix
Martin@1
   127
Martin@1
   128
seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
Martin@1
   129
seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
Martin@1
   130
seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
Martin@1
   131
seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
Martin@1
   132
hits = [255] * (width * height)  # the image data
Martin@1
   133
Martin@1
   134
sys.stderr.write(my_name + ": processing alignments...\n")
Martin@1
   135
for aln in alignments:
Martin@1
   136
    seq1, seq2, pos1, pos2, strand1, strand2, blocks = aln
Martin@1
   137
    last1 = seq_size_dic1[seq1] - 1
Martin@1
   138
    last2 = seq_size_dic2[seq2] - 1
Martin@1
   139
    seq_start1 = seq_start_dic1[seq1]
Martin@1
   140
    seq_start2 = seq_start_dic2[seq2]
Martin@1
   141
    block_list = map(int, re.split(r'\W', blocks))
Martin@1
   142
    for i, b in enumerate(block_list):
Martin@1
   143
        state = i % 3
Martin@1
   144
        if state == 0:
Martin@1
   145
            for j in range(b):
Martin@28
   146
                if strand1 == '+': real_pos1 = pos1
Martin@28
   147
                else:              real_pos1 = last1 - pos1
Martin@28
   148
                if strand2 == '+': real_pos2 = pos2
Martin@28
   149
                else:              real_pos2 = last2 - pos2
Martin@1
   150
                pix1 = seq_start1 + real_pos1 // bp_per_pix
Martin@1
   151
                pix2 = seq_start2 + real_pos2 // bp_per_pix
Martin@1
   152
                hits[pix2 * width + pix1] = 0
Martin@1
   153
                pos1 += 1
Martin@1
   154
                pos2 += 1
Martin@1
   155
        elif state == 1:
Martin@1
   156
            pos1 += b
Martin@1
   157
        else:
Martin@1
   158
            pos2 += b
Martin@1
   159
sys.stderr.write(my_name + ": done\n")
Martin@1
   160
Martin@1
   161
def make_label(text, text_size, range_start, range_size):
Martin@1
   162
    '''Return an axis label with endpoint & sort-order information.'''
Martin@1
   163
    text_width  = text_size[0]
Martin@1
   164
    label_start = range_start + (range_size - text_width) // 2
Martin@1
   165
    label_end   = label_start + text_width
Martin@1
   166
    sort_key    = text_width - range_size
Martin@1
   167
    return sort_key, label_start, label_end, text
Martin@1
   168
Martin@1
   169
def get_nonoverlapping_labels(labels):
Martin@1
   170
    '''Get a subset of non-overlapping axis labels, greedily.'''
Martin@1
   171
    nonoverlapping_labels = []
Martin@1
   172
    for i in labels:
Martin@28
   173
        if True not in [i[1] < j[2] + label_space and j[1] < i[2] + label_space
Martin@28
   174
                        for j in nonoverlapping_labels]:
Martin@1
   175
            nonoverlapping_labels.append(i)
Martin@1
   176
    return nonoverlapping_labels
Martin@1
   177
Martin@1
   178
def get_axis_image(seq_names, name_sizes, seq_starts, seq_pix):
Martin@1
   179
    '''Make an image of axis labels.'''
Martin@1
   180
    min_pos = seq_starts[0]
Martin@1
   181
    max_pos = seq_starts[-1] + seq_pix[-1]
Martin@28
   182
    height = max(zip(*name_sizes)[1])
Martin@1
   183
    labels = [make_label(i, j, k, l) for i, j, k, l in
Martin@1
   184
              zip(seq_names, name_sizes, seq_starts, seq_pix)]
Martin@1
   185
    labels = [i for i in labels if i[1] >= min_pos and i[2] <= max_pos]
Martin@1
   186
    labels.sort()
Martin@1
   187
    labels = get_nonoverlapping_labels(labels)
Martin@1
   188
    image_size = max_pos, height
Martin@51
   189
    im = Image.new('L', image_size, border_shade)
Martin@1
   190
    draw = ImageDraw.Draw(im)
Martin@1
   191
    for i in labels:
Martin@1
   192
        position = i[1], 0
Martin@1
   193
        draw.text(position, i[3], font=font)
Martin@1
   194
    return im
Martin@1
   195
Martin@1
   196
image_size = width, height
Martin@1
   197
im = Image.new('L', image_size)
Martin@1
   198
im.putdata(hits)
Martin@95
   199
Martin@95
   200
if opts.fontsize != 0:
Martin@95
   201
    axis1 = get_axis_image(seq_names1, name_sizes1, seq_starts1, seq_pix1)
Martin@95
   202
    axis2 = get_axis_image(seq_names2, name_sizes2, seq_starts2, seq_pix2)
Martin@95
   203
    axis2 = axis2.rotate(270)
Martin@95
   204
    im.paste(axis1, (0, 0))
Martin@95
   205
    im.paste(axis2, (0, 0))
Martin@1
   206
Martin@1
   207
for i in seq_starts1[1:]:
Martin@1
   208
    box = i - pix_tween_seqs, margin2, i, height
Martin@1
   209
    im.paste(border_shade, box)
Martin@1
   210
Martin@1
   211
for i in seq_starts2[1:]:
Martin@1
   212
    box = margin1, i - pix_tween_seqs, width, i
Martin@1
   213
    im.paste(border_shade, box)
Martin@1
   214
Martin@1
   215
im.save(args[1])