scripts/last-dotplot
author Martin C. Frith
Mon Oct 19 10:53:05 2015 +0900 (2015-10-19)
changeset 639 047baa399047
parent 482 f5b731ee703e
child 640 374bc3ee1115
permissions -rwxr-xr-x
Made last-dotplot much faster.
     1 #! /usr/bin/env python
     2 
     3 # Read pair-wise alignments in MAF or LAST tabular format: write an
     4 # "Oxford grid", a.k.a. dotplot.
     5 
     6 # TODO: Currently, pixels with zero aligned nt-pairs are white, and
     7 # pixels with one or more aligned nt-pairs are black.  This can look
     8 # too crowded for large genome alignments.  I tried shading each pixel
     9 # according to the number of aligned nt-pairs within it, but the
    10 # result is too faint.  How can this be done better?
    11 
    12 import fileinput, itertools, optparse, os, re, sys
    13 
    14 # Try to make PIL/PILLOW work:
    15 try: from PIL import Image, ImageDraw, ImageFont, ImageColor
    16 except ImportError: import Image, ImageDraw, ImageFont, ImageColor
    17 
    18 my_name = os.path.basename(sys.argv[0])
    19 usage = """
    20   %prog --help
    21   %prog [options] last-tabular-output dotplot.png
    22   %prog [options] last-tabular-output dotplot.gif
    23   etc."""
    24 parser = optparse.OptionParser(usage=usage)
    25 # Replace "width" & "height" with a single "length" option?
    26 parser.add_option("-x", "--width", type="int", dest="width", default=1000,
    27                   help="maximum width in pixels (default: %default)")
    28 parser.add_option("-y", "--height", type="int", dest="height", default=1000,
    29                   help="maximum height in pixels (default: %default)")
    30 parser.add_option("-f", "--fontfile", dest="fontfile",
    31                   help="TrueType or OpenType font file")
    32 parser.add_option("-s", "--fontsize", type="int", dest="fontsize", default=11,
    33                   help="TrueType or OpenType font size (default: %default)")
    34 parser.add_option("-c", "--forwardcolor", dest="forwardcolor", default="red",
    35                   help="Color for forward alignments (default: %default)")
    36 parser.add_option("-r", "--reversecolor", dest="reversecolor", default="blue",
    37                   help="Color for reverse alignments (default: %default)")
    38 (opts, args) = parser.parse_args()
    39 if len(args) != 2: parser.error("2 arguments needed")
    40 
    41 if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    42 else:              font = ImageFont.load_default()
    43 
    44 # Make these options too?
    45 text_color = "black"
    46 background_color = "white"
    47 pix_tween_seqs = 2  # number of border pixels between sequences
    48 border_shade = 239, 239, 239  # the shade of grey to use for border pixels
    49 label_space = 5     # minimum number of pixels between axis labels
    50 
    51 image_mode = 'RGB'
    52 forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    53 reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    54 overlap_color = tuple([(i+j)//2 for i, j in zip(forward_color, reverse_color)])
    55 
    56 def tabBlocks(beg1, beg2, blocks):
    57     '''Get the gapless blocks of an alignment, from LAST tabular format.'''
    58     for i in blocks.split(","):
    59         if ":" in i:
    60             x, y = i.split(":")
    61             beg1 += int(x)
    62             beg2 += int(y)
    63         else:
    64             size = int(i)
    65             yield beg1, beg2, size
    66             beg1 += size
    67             beg2 += size
    68 
    69 def mafBlocks(beg1, beg2, seq1, seq2):
    70     '''Get the gapless blocks of an alignment, from MAF format.'''
    71     size = 0
    72     for x, y in itertools.izip(seq1, seq2):
    73         if x == "-":
    74             if size:
    75                 yield beg1, beg2, size
    76                 beg1 += size
    77                 beg2 += size
    78                 size = 0
    79             beg2 += 1
    80         elif y == "-":
    81             if size:
    82                 yield beg1, beg2, size
    83                 beg1 += size
    84                 beg2 += size
    85                 size = 0
    86             beg1 += 1
    87         else:
    88             size += 1
    89     if size: yield beg1, beg2, size
    90 
    91 def alignmentInput(lines):
    92     '''Get alignments and sequence lengths, from MAF or tabular format.'''
    93     mafCount = 0
    94     for line in lines:
    95         w = line.split()
    96         if line[0].isdigit():  # tabular format
    97             chr1, beg1, seqlen1 = w[1], int(w[2]), int(w[5])
    98             if w[4] == "-": beg1 -= seqlen1
    99             chr2, beg2, seqlen2 = w[6], int(w[7]), int(w[10])
   100             if w[9] == "-": beg2 -= seqlen2
   101             blocks = tabBlocks(beg1, beg2, w[11])
   102             yield chr1, seqlen1, chr2, seqlen2, blocks
   103         elif line[0] == "s":  # MAF format
   104             if mafCount == 0:
   105                 chr1, beg1, seqlen1, seq1 = w[1], int(w[2]), int(w[5]), w[6]
   106                 if w[4] == "-": beg1 -= seqlen1
   107                 mafCount = 1
   108             else:
   109                 chr2, beg2, seqlen2, seq2 = w[1], int(w[2]), int(w[5]), w[6]
   110                 if w[4] == "-": beg2 -= seqlen2
   111                 blocks = mafBlocks(beg1, beg2, seq1, seq2)
   112                 yield chr1, seqlen1, chr2, seqlen2, blocks
   113                 mafCount = 0
   114 
   115 def readAlignments(lines):
   116     '''Get alignments and sequence lengths, from MAF or tabular format.'''
   117     alignments = []
   118     seqLengths1 = {}
   119     seqLengths2 = {}
   120     for chr1, seqlen1, chr2, seqlen2, blocks in alignmentInput(lines):
   121         aln = chr1, chr2, blocks
   122         alignments.append(aln)
   123         seqLengths1[chr1] = seqlen1
   124         seqLengths2[chr2] = seqlen2
   125     return alignments, seqLengths1, seqLengths2
   126 
   127 sys.stderr.write(my_name + ": reading alignments...\n")
   128 input = fileinput.input(args[0])
   129 alignments, seq_size_dic1, seq_size_dic2 = readAlignments(input)
   130 sys.stderr.write(my_name + ": done\n")
   131 
   132 if not alignments:
   133     sys.exit(my_name + ": there are no alignments")
   134 
   135 def natural_sort_key(my_string):
   136     '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
   137     parts = re.split(r'(\d+)', my_string)
   138     parts[1::2] = map(int, parts[1::2])
   139     return parts
   140 
   141 def get_text_sizes(my_strings):
   142     '''Get widths & heights, in pixels, of some strings.'''
   143     if opts.fontsize == 0: return [(0, 0) for i in my_strings]
   144     image_size = 1, 1
   145     im = Image.new(image_mode, image_size)
   146     draw = ImageDraw.Draw(im)
   147     return [draw.textsize(i, font=font) for i in my_strings]
   148 
   149 def get_seq_info(seq_size_dic):
   150     '''Return miscellaneous information about the sequences.'''
   151     seq_names = seq_size_dic.keys()
   152     seq_names.sort(key=natural_sort_key)
   153     seq_sizes = [seq_size_dic[i] for i in seq_names]
   154     name_sizes = get_text_sizes(seq_names)
   155     margin = max(zip(*name_sizes)[1])  # maximum text height
   156     return seq_names, seq_sizes, name_sizes, margin
   157 
   158 seq_names1, seq_sizes1, name_sizes1, margin1 = get_seq_info(seq_size_dic1)
   159 seq_names2, seq_sizes2, name_sizes2, margin2 = get_seq_info(seq_size_dic2)
   160 
   161 def div_ceil(x, y):
   162     '''Return x / y rounded up.'''
   163     q, r = divmod(x, y)
   164     return q + (r != 0)
   165 
   166 def tot_seq_pix(seq_sizes, bp_per_pix):
   167     '''Return the total pixels needed for sequences of the given sizes.'''
   168     return sum([div_ceil(i, bp_per_pix) for i in seq_sizes])
   169 
   170 def get_bp_per_pix(seq_sizes, pix_limit):
   171     '''Get the minimum bp-per-pixel that fits in the size limit.'''
   172     seq_num = len(seq_sizes)
   173     seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
   174     if seq_pix_limit < seq_num:
   175         sys.exit(my_name + ": can't fit the image: too many sequences?")
   176     lower_bound = div_ceil(sum(seq_sizes), seq_pix_limit)
   177     for bp_per_pix in itertools.count(lower_bound):  # slow linear search
   178         if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
   179     return bp_per_pix
   180 
   181 sys.stderr.write(my_name + ": choosing bp per pixel...\n")
   182 bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.width  - margin1)
   183 bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.height - margin2)
   184 bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   185 sys.stderr.write(my_name + ": bp per pixel = " + str(bp_per_pix) + "\n")
   186 
   187 def get_seq_starts(seq_pix, pix_tween_seqs, margin):
   188     '''Get the start pixel for each sequence.'''
   189     seq_starts = []
   190     pix_tot = margin - pix_tween_seqs
   191     for i in seq_pix:
   192         pix_tot += pix_tween_seqs
   193         seq_starts.append(pix_tot)
   194         pix_tot += i
   195     return seq_starts
   196 
   197 def get_pix_info(seq_sizes, margin):
   198     '''Return pixel information about the sequences.'''
   199     seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
   200     seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
   201     tot_pix = seq_starts[-1] + seq_pix[-1]
   202     return seq_pix, seq_starts, tot_pix
   203 
   204 seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, margin1)
   205 seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, margin2)
   206 seq_start_dic1 = dict(zip(seq_names1, seq_starts1))
   207 seq_start_dic2 = dict(zip(seq_names2, seq_starts2))
   208 
   209 def drawLineForward(hits, width, bp_per_pix, origin, beg1, beg2, size):
   210     while True:
   211         q1, r1 = divmod(beg1, bp_per_pix)
   212         q2, r2 = divmod(beg2, bp_per_pix)
   213         hits[origin + q2 * width + q1] |= 1
   214         next_pix = min(bp_per_pix - r1, bp_per_pix - r2)
   215         if next_pix >= size: break
   216         beg1 += next_pix
   217         beg2 += next_pix
   218         size -= next_pix
   219 
   220 def drawLineReverse(hits, width, bp_per_pix, origin, beg1, beg2, size):
   221     beg2 = -1 - beg2
   222     while True:
   223         q1, r1 = divmod(beg1, bp_per_pix)
   224         q2, r2 = divmod(beg2, bp_per_pix)
   225         hits[origin + q2 * width + q1] |= 2
   226         next_pix = min(bp_per_pix - r1, r2 + 1)
   227         if next_pix >= size: break
   228         beg1 += next_pix
   229         beg2 -= next_pix
   230         size -= next_pix
   231 
   232 hits = [0] * (width * height)  # the image data
   233 
   234 sys.stderr.write(my_name + ": processing alignments...\n")
   235 for seq1, seq2, blocks in alignments:
   236     seq_start1 = seq_start_dic1[seq1]
   237     seq_start2 = seq_start_dic2[seq2]
   238     my_start = seq_start2 * width + seq_start1
   239     for beg1, beg2, size in blocks:
   240         if beg1 < 0:
   241             beg1 = -(beg1 + size)
   242             beg2 = -(beg2 + size)
   243         if beg2 >= 0:
   244             drawLineForward(hits, width, bp_per_pix, my_start,
   245                             beg1, beg2, size)
   246         else:
   247             drawLineReverse(hits, width, bp_per_pix, my_start,
   248                             beg1, beg2, size)
   249 sys.stderr.write(my_name + ": done\n")
   250 
   251 def make_label(text, text_size, range_start, range_size):
   252     '''Return an axis label with endpoint & sort-order information.'''
   253     text_width  = text_size[0]
   254     label_start = range_start + (range_size - text_width) // 2
   255     label_end   = label_start + text_width
   256     sort_key    = text_width - range_size
   257     return sort_key, label_start, label_end, text
   258 
   259 def get_nonoverlapping_labels(labels):
   260     '''Get a subset of non-overlapping axis labels, greedily.'''
   261     nonoverlapping_labels = []
   262     for i in labels:
   263         if True not in [i[1] < j[2] + label_space and j[1] < i[2] + label_space
   264                         for j in nonoverlapping_labels]:
   265             nonoverlapping_labels.append(i)
   266     return nonoverlapping_labels
   267 
   268 def get_axis_image(seq_names, name_sizes, seq_starts, seq_pix):
   269     '''Make an image of axis labels.'''
   270     min_pos = seq_starts[0]
   271     max_pos = seq_starts[-1] + seq_pix[-1]
   272     height = max(zip(*name_sizes)[1])
   273     labels = [make_label(i, j, k, l) for i, j, k, l in
   274               zip(seq_names, name_sizes, seq_starts, seq_pix)]
   275     labels = [i for i in labels if i[1] >= min_pos and i[2] <= max_pos]
   276     labels.sort()
   277     labels = get_nonoverlapping_labels(labels)
   278     image_size = max_pos, height
   279     im = Image.new(image_mode, image_size, border_shade)
   280     draw = ImageDraw.Draw(im)
   281     for i in labels:
   282         position = i[1], 0
   283         draw.text(position, i[3], font=font, fill=text_color)
   284     return im
   285 
   286 image_size = width, height
   287 im = Image.new(image_mode, image_size, background_color)
   288 
   289 for i in range(height):
   290     for j in range(width):
   291         store_value = hits[i * width + j]
   292         xy = j, i
   293         if   store_value == 1: im.putpixel(xy, forward_color)
   294         elif store_value == 2: im.putpixel(xy, reverse_color)
   295         elif store_value == 3: im.putpixel(xy, overlap_color)
   296 
   297 if opts.fontsize != 0:
   298     axis1 = get_axis_image(seq_names1, name_sizes1, seq_starts1, seq_pix1)
   299     axis2 = get_axis_image(seq_names2, name_sizes2, seq_starts2, seq_pix2)
   300     axis2 = axis2.rotate(270)
   301     im.paste(axis1, (0, 0))
   302     im.paste(axis2, (0, 0))
   303 
   304 for i in seq_starts1[1:]:
   305     box = i - pix_tween_seqs, margin2, i, height
   306     im.paste(border_shade, box)
   307 
   308 for i in seq_starts2[1:]:
   309     box = margin1, i - pix_tween_seqs, width, i
   310     im.paste(border_shade, box)
   311 
   312 im.save(args[1])