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