scripts/last-dotplot
changeset 479 e707702f53f8
parent 475 4cf8170b3538
child 482 f5b731ee703e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scripts/last-dotplot	Wed Sep 24 13:13:14 2014 +0900
     1.3 @@ -0,0 +1,271 @@
     1.4 +#! /usr/bin/env python
     1.5 +
     1.6 +# Read pair-wise alignments in MAF or LAST tabular format: write an
     1.7 +# "Oxford grid", a.k.a. dotplot.
     1.8 +
     1.9 +# TODO: Currently, pixels with zero aligned nt-pairs are white, and
    1.10 +# pixels with one or more aligned nt-pairs are black.  This can look
    1.11 +# too crowded for large genome alignments.  I tried shading each pixel
    1.12 +# according to the number of aligned nt-pairs within it, but the
    1.13 +# result is too faint.  How can this be done better?
    1.14 +
    1.15 +import sys, os, re, itertools, optparse
    1.16 +
    1.17 +# Try to make PIL/PILLOW work:
    1.18 +try: from PIL import Image, ImageDraw, ImageFont, ImageColor
    1.19 +except ImportError: import Image, ImageDraw, ImageFont, ImageColor
    1.20 +
    1.21 +my_name = os.path.basename(sys.argv[0])
    1.22 +usage = """
    1.23 +  %prog --help
    1.24 +  %prog [options] last-tabular-output dotplot.png
    1.25 +  %prog [options] last-tabular-output dotplot.gif
    1.26 +  etc."""
    1.27 +parser = optparse.OptionParser(usage=usage)
    1.28 +# Replace "width" & "height" with a single "length" option?
    1.29 +parser.add_option("-x", "--width", type="int", dest="width", default=1000,
    1.30 +                  help="maximum width in pixels (default: %default)")
    1.31 +parser.add_option("-y", "--height", type="int", dest="height", default=1000,
    1.32 +                  help="maximum height in pixels (default: %default)")
    1.33 +parser.add_option("-f", "--fontfile", dest="fontfile",
    1.34 +                  help="TrueType or OpenType font file")
    1.35 +parser.add_option("-s", "--fontsize", type="int", dest="fontsize", default=11,
    1.36 +                  help="TrueType or OpenType font size (default: %default)")
    1.37 +parser.add_option("-c", "--forwardcolor", dest="forwardcolor", default="red",
    1.38 +                  help="Color for forward alignments (default: %default)")
    1.39 +parser.add_option("-r", "--reversecolor", dest="reversecolor", default="blue",
    1.40 +                  help="Color for reverse alignments (default: %default)")
    1.41 +(opts, args) = parser.parse_args()
    1.42 +if len(args) != 2: parser.error("2 arguments needed")
    1.43 +
    1.44 +if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.45 +else:              font = ImageFont.load_default()
    1.46 +
    1.47 +# Make these options too?
    1.48 +text_color = "black"
    1.49 +background_color = "white"
    1.50 +pix_tween_seqs = 2  # number of border pixels between sequences
    1.51 +border_shade = 239, 239, 239  # the shade of grey to use for border pixels
    1.52 +label_space = 5     # minimum number of pixels between axis labels
    1.53 +
    1.54 +image_mode = 'RGB'
    1.55 +forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    1.56 +reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    1.57 +overlap_color = tuple([(i+j)//2 for i, j in zip(forward_color, reverse_color)])
    1.58 +
    1.59 +def isGapless(alignmentColumn):
    1.60 +    return "-" not in alignmentColumn
    1.61 +
    1.62 +def matchAndInsertLengths(alignmentColumns):
    1.63 +    for k, v in itertools.groupby(alignmentColumns, isGapless):
    1.64 +        if k:
    1.65 +            matchLength = sum(1 for i in v)
    1.66 +            yield str(matchLength)
    1.67 +        else:
    1.68 +            blockRows = itertools.izip(*v)
    1.69 +            insertLengths = (len(i) - i.count("-") for i in blockRows)
    1.70 +            yield ":".join(map(str, insertLengths))
    1.71 +
    1.72 +def alignmentInput(lines):  # read alignments in either tabular or MAF format
    1.73 +    for line in lines:
    1.74 +        w = line.split()
    1.75 +        if line[0].isdigit():  # tabular format
    1.76 +            yield w
    1.77 +        elif line[0] == "a":  # MAF format
    1.78 +            sLines = []
    1.79 +        elif line[0] == "s":  # MAF format
    1.80 +            sLines.append(w)
    1.81 +            if len(sLines) == 2:
    1.82 +                alignmentRows = (i[6] for i in sLines)
    1.83 +                alignmentColumns = itertools.izip(*alignmentRows)
    1.84 +                blocks = ",".join(matchAndInsertLengths(alignmentColumns))
    1.85 +                yield sLines[0][0:6] + sLines[1][1:6] + [blocks]
    1.86 +
    1.87 +seq_size_dic1 = {}  # sizes of the first set of sequences
    1.88 +seq_size_dic2 = {}  # sizes of the second set of sequences
    1.89 +alignments = []
    1.90 +
    1.91 +f = open(args[0])
    1.92 +sys.stderr.write(my_name + ": reading alignments...\n")
    1.93 +for w in alignmentInput(f):
    1.94 +    seq1, pos1, strand1, size1 = w[1], int(w[2]), w[4], int(w[5])
    1.95 +    seq2, pos2, strand2, size2 = w[6], int(w[7]), w[9], int(w[10])
    1.96 +    blocks = w[11]
    1.97 +    seq_size_dic1[seq1] = size1
    1.98 +    seq_size_dic2[seq2] = size2
    1.99 +    aln = seq1, seq2, pos1, pos2, strand1, strand2, blocks
   1.100 +    alignments.append(aln)
   1.101 +sys.stderr.write(my_name + ": done\n")
   1.102 +f.close()
   1.103 +
   1.104 +if not alignments:
   1.105 +    sys.exit(my_name + ": there are no alignments")
   1.106 +
   1.107 +def natural_sort_key(my_string):
   1.108 +    '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
   1.109 +    parts = re.split(r'(\d+)', my_string)
   1.110 +    parts[1::2] = map(int, parts[1::2])
   1.111 +    return parts
   1.112 +
   1.113 +def get_text_sizes(my_strings):
   1.114 +    '''Get widths & heights, in pixels, of some strings.'''
   1.115 +    if opts.fontsize == 0: return [(0, 0) for i in my_strings]
   1.116 +    image_size = 1, 1
   1.117 +    im = Image.new(image_mode, image_size)
   1.118 +    draw = ImageDraw.Draw(im)
   1.119 +    return [draw.textsize(i, font=font) for i in my_strings]
   1.120 +
   1.121 +def get_seq_info(seq_size_dic):
   1.122 +    '''Return miscellaneous information about the sequences.'''
   1.123 +    seq_names = seq_size_dic.keys()
   1.124 +    seq_names.sort(key=natural_sort_key)
   1.125 +    seq_sizes = [seq_size_dic[i] for i in seq_names]
   1.126 +    name_sizes = get_text_sizes(seq_names)
   1.127 +    margin = max(zip(*name_sizes)[1])  # maximum text height
   1.128 +    return seq_names, seq_sizes, name_sizes, margin
   1.129 +
   1.130 +seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
   1.131 +seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
   1.132 +
   1.133 +def div_ceil(x, y):
   1.134 +    '''Return x / y rounded up.'''
   1.135 +    q, r = divmod(x, y)
   1.136 +    return q + (r != 0)
   1.137 +
   1.138 +def tot_seq_pix(seq_sizes, bp_per_pix):
   1.139 +    '''Return the total pixels needed for sequences of the given sizes.'''
   1.140 +    return sum([div_ceil(i, bp_per_pix) for i in seq_sizes])
   1.141 +
   1.142 +def get_bp_per_pix(seq_sizes, pix_limit):
   1.143 +    '''Get the minimum bp-per-pixel that fits in the size limit.'''
   1.144 +    seq_num = len(seq_sizes)
   1.145 +    seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
   1.146 +    if seq_pix_limit < seq_num:
   1.147 +        sys.exit(my_name + ": can't fit the image: too many sequences?")
   1.148 +    lower_bound = div_ceil(sum(seq_sizes), seq_pix_limit)
   1.149 +    for bp_per_pix in itertools.count(lower_bound):  # slow linear search
   1.150 +        if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
   1.151 +    return bp_per_pix
   1.152 +
   1.153 +sys.stderr.write(my_name + ": choosing bp per pixel...\n")
   1.154 +bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
   1.155 +bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
   1.156 +bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   1.157 +sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
   1.158 +
   1.159 +def get_seq_starts(seq_pix, pix_tween_seqs, margin):
   1.160 +    '''Get the start pixel for each sequence.'''
   1.161 +    seq_starts = []
   1.162 +    pix_tot = margin - pix_tween_seqs
   1.163 +    for i in seq_pix:
   1.164 +        pix_tot += pix_tween_seqs
   1.165 +        seq_starts.append(pix_tot)
   1.166 +        pix_tot += i
   1.167 +    return seq_starts
   1.168 +
   1.169 +def get_pix_info(seq_sizes, margin):
   1.170 +    '''Return pixel information about the sequences.'''
   1.171 +    seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
   1.172 +    seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
   1.173 +    tot_pix = seq_starts[-1] + seq_pix[-1]
   1.174 +    return seq_pix, seq_starts, tot_pix
   1.175 +
   1.176 +seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
   1.177 +seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
   1.178 +seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
   1.179 +seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
   1.180 +hits = [0] * (width * height)  # the image data
   1.181 +
   1.182 +sys.stderr.write(my_name + ": processing alignments...\n")
   1.183 +for aln in alignments:
   1.184 +    seq1, seq2, pos1, pos2, strand1, strand2, blocks = aln
   1.185 +    last1 = seq_size_dic1[seq1] - 1
   1.186 +    last2 = seq_size_dic2[seq2] - 1
   1.187 +    seq_start1 = seq_start_dic1[seq1]
   1.188 +    seq_start2 = seq_start_dic2[seq2]
   1.189 +    my_start = seq_start2 * width + seq_start1
   1.190 +    if strand1 == strand2: store_value = 1
   1.191 +    else:                  store_value = 2
   1.192 +    for i in blocks.split(","):
   1.193 +        if ":" in i:  # it's a gap region: skip over it
   1.194 +            insertLength1, insertLength2 = i.split(":")
   1.195 +            pos1 += int(insertLength1)
   1.196 +            pos2 += int(insertLength2)
   1.197 +        else:  # it's a match region: draw pixels for it
   1.198 +            matchLength = int(i)
   1.199 +            end1 = pos1 + matchLength
   1.200 +            end2 = pos2 + matchLength
   1.201 +            if strand1 == '+': j = xrange(pos1, end1)
   1.202 +            else:              j = xrange(last1 - pos1, last1 - end1, -1)
   1.203 +            if strand2 == '+': k = xrange(pos2, end2)
   1.204 +            else:              k = xrange(last2 - pos2, last2 - end2, -1)
   1.205 +            for real_pos1, real_pos2 in itertools.izip(j, k):
   1.206 +                pix1 = real_pos1 // bp_per_pix
   1.207 +                pix2 = real_pos2 // bp_per_pix
   1.208 +                hits[my_start + pix2 * width + pix1] |= store_value
   1.209 +            pos1 = end1
   1.210 +            pos2 = end2
   1.211 +sys.stderr.write(my_name + ": done\n")
   1.212 +
   1.213 +def make_label(text, text_size, range_start, range_size):
   1.214 +    '''Return an axis label with endpoint & sort-order information.'''
   1.215 +    text_width  = text_size[0]
   1.216 +    label_start = range_start + (range_size - text_width) // 2
   1.217 +    label_end   = label_start + text_width
   1.218 +    sort_key    = text_width - range_size
   1.219 +    return sort_key, label_start, label_end, text
   1.220 +
   1.221 +def get_nonoverlapping_labels(labels):
   1.222 +    '''Get a subset of non-overlapping axis labels, greedily.'''
   1.223 +    nonoverlapping_labels = []
   1.224 +    for i in labels:
   1.225 +        if True not in [i[1] < j[2] + label_space and j[1] < i[2] + label_space
   1.226 +                        for j in nonoverlapping_labels]:
   1.227 +            nonoverlapping_labels.append(i)
   1.228 +    return nonoverlapping_labels
   1.229 +
   1.230 +def get_axis_image(seq_names, name_sizes, seq_starts, seq_pix):
   1.231 +    '''Make an image of axis labels.'''
   1.232 +    min_pos = seq_starts[0]
   1.233 +    max_pos = seq_starts[-1] + seq_pix[-1]
   1.234 +    height = max(zip(*name_sizes)[1])
   1.235 +    labels = [make_label(i, j, k, l) for i, j, k, l in
   1.236 +              zip(seq_names, name_sizes, seq_starts, seq_pix)]
   1.237 +    labels = [i for i in labels if i[1] >= min_pos and i[2] <= max_pos]
   1.238 +    labels.sort()
   1.239 +    labels = get_nonoverlapping_labels(labels)
   1.240 +    image_size = max_pos, height
   1.241 +    im = Image.new(image_mode, image_size, border_shade)
   1.242 +    draw = ImageDraw.Draw(im)
   1.243 +    for i in labels:
   1.244 +        position = i[1], 0
   1.245 +        draw.text(position, i[3], font=font, fill=text_color)
   1.246 +    return im
   1.247 +
   1.248 +image_size = width, height
   1.249 +im = Image.new(image_mode, image_size, background_color)
   1.250 +
   1.251 +for i in range(height):
   1.252 +    for j in range(width):
   1.253 +        store_value = hits[i * width + j]
   1.254 +        xy = j, i
   1.255 +        if   store_value == 1: im.putpixel(xy, forward_color)
   1.256 +        elif store_value == 2: im.putpixel(xy, reverse_color)
   1.257 +        elif store_value == 3: im.putpixel(xy, overlap_color)
   1.258 +
   1.259 +if opts.fontsize != 0:
   1.260 +    axis1 = get_axis_image(seq_names1, name_sizes1, seq_starts1, seq_pix1)
   1.261 +    axis2 = get_axis_image(seq_names2, name_sizes2, seq_starts2, seq_pix2)
   1.262 +    axis2 = axis2.rotate(270)
   1.263 +    im.paste(axis1, (0, 0))
   1.264 +    im.paste(axis2, (0, 0))
   1.265 +
   1.266 +for i in seq_starts1[1:]:
   1.267 +    box = i - pix_tween_seqs, margin2, i, height
   1.268 +    im.paste(border_shade, box)
   1.269 +
   1.270 +for i in seq_starts2[1:]:
   1.271 +    box = margin1, i - pix_tween_seqs, width, i
   1.272 +    im.paste(border_shade, box)
   1.273 +
   1.274 +im.save(args[1])