scripts/last-dotplot
author Martin C. Frith
Tue Apr 04 11:51:15 2017 +0900 (2017-04-04)
changeset 845 16060c00b129
parent 844 23de4eb3be1d
child 846 1f46ab956351
permissions -rwxr-xr-x
Added last-dotplot options to show BED features.
     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 fnmatch, 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 myOpen(fileName):  # faster than fileinput
    19     if fileName == "-":
    20         return sys.stdin
    21     return open(fileName)
    22 
    23 def warn(message):
    24     prog = os.path.basename(sys.argv[0])
    25     sys.stderr.write(prog + ": " + message + "\n")
    26 
    27 def croppedBlocks(blocks, range1, range2):
    28     cropBeg1, cropEnd1 = range1
    29     cropBeg2, cropEnd2 = range2
    30     if blocks[0][0] < 0: cropBeg1, cropEnd1 = -cropEnd1, -cropBeg1
    31     if blocks[0][1] < 0: cropBeg2, cropEnd2 = -cropEnd2, -cropBeg2
    32     for beg1, beg2, size in blocks:
    33         b1 = max(cropBeg1, beg1)
    34         e1 = min(cropEnd1, beg1 + size)
    35         if b1 >= e1: continue
    36         offset = beg2 - beg1
    37         b2 = max(cropBeg2, b1 + offset)
    38         e2 = min(cropEnd2, e1 + offset)
    39         if b2 >= e2: continue
    40         yield b2 - offset, b2, e2 - b2
    41 
    42 def tabBlocks(beg1, beg2, blocks):
    43     '''Get the gapless blocks of an alignment, from LAST tabular format.'''
    44     for i in blocks.split(","):
    45         if ":" in i:
    46             x, y = i.split(":")
    47             beg1 += int(x)
    48             beg2 += int(y)
    49         else:
    50             size = int(i)
    51             yield beg1, beg2, size
    52             beg1 += size
    53             beg2 += size
    54 
    55 def mafBlocks(beg1, beg2, seq1, seq2):
    56     '''Get the gapless blocks of an alignment, from MAF format.'''
    57     size = 0
    58     for x, y in itertools.izip(seq1, seq2):
    59         if x == "-":
    60             if size:
    61                 yield beg1, beg2, size
    62                 beg1 += size
    63                 beg2 += size
    64                 size = 0
    65             beg2 += 1
    66         elif y == "-":
    67             if size:
    68                 yield beg1, beg2, size
    69                 beg1 += size
    70                 beg2 += size
    71                 size = 0
    72             beg1 += 1
    73         else:
    74             size += 1
    75     if size: yield beg1, beg2, size
    76 
    77 def alignmentInput(lines):
    78     '''Get alignments and sequence lengths, from MAF or tabular format.'''
    79     mafCount = 0
    80     for line in lines:
    81         w = line.split()
    82         if line[0].isdigit():  # tabular format
    83             chr1, beg1, seqlen1 = w[1], int(w[2]), int(w[5])
    84             if w[4] == "-": beg1 -= seqlen1
    85             chr2, beg2, seqlen2 = w[6], int(w[7]), int(w[10])
    86             if w[9] == "-": beg2 -= seqlen2
    87             blocks = list(tabBlocks(beg1, beg2, w[11]))
    88             yield chr1, seqlen1, chr2, seqlen2, blocks
    89         elif line[0] == "s":  # MAF format
    90             if mafCount == 0:
    91                 chr1, beg1, seqlen1, seq1 = w[1], int(w[2]), int(w[5]), w[6]
    92                 if w[4] == "-": beg1 -= seqlen1
    93                 mafCount = 1
    94             else:
    95                 chr2, beg2, seqlen2, seq2 = w[1], int(w[2]), int(w[5]), w[6]
    96                 if w[4] == "-": beg2 -= seqlen2
    97                 blocks = list(mafBlocks(beg1, beg2, seq1, seq2))
    98                 yield chr1, seqlen1, chr2, seqlen2, blocks
    99                 mafCount = 0
   100 
   101 def seqRangeFromText(text):
   102     if ":" in text:
   103         pattern, interval = text.rsplit(":", 1)
   104         if "-" in interval:
   105             beg, end = interval.rsplit("-", 1)
   106             return pattern, int(beg), int(end)  # beg may be negative
   107     return text, 0, sys.maxsize
   108 
   109 def rangeFromSeqName(seqRanges, name, seqLen):
   110     if not seqRanges: return 0, seqLen
   111     base = name.split(".")[-1]  # allow for names like hg19.chr7
   112     for pat, beg, end in seqRanges:
   113         if fnmatch.fnmatchcase(name, pat) or fnmatch.fnmatchcase(base, pat):
   114             return max(beg, 0), min(end, seqLen)
   115     return None
   116 
   117 def updateSeqLimits(isTrim, seqLimits, seqName, seqRange, blocks, index):
   118     if isTrim:
   119         beg = blocks[0][index]
   120         end = blocks[-1][index] + blocks[-1][2]
   121         if beg < 0: beg, end = -end, -beg
   122         if seqName in seqLimits:
   123             b, e = seqLimits[seqName]
   124             seqLimits[seqName] = min(b, beg), max(e, end)
   125         else:
   126             seqLimits[seqName] = beg, end
   127     else:
   128         seqLimits[seqName] = seqRange
   129 
   130 def readAlignments(fileName, opts):
   131     '''Get alignments and sequence limits, from MAF or tabular format.'''
   132     seqRanges1 = map(seqRangeFromText, opts.seq1)
   133     seqRanges2 = map(seqRangeFromText, opts.seq2)
   134 
   135     alignments = []
   136     seqLimits1 = {}
   137     seqLimits2 = {}
   138     lines = myOpen(fileName)
   139     for seqName1, seqLen1, seqName2, seqLen2, blocks in alignmentInput(lines):
   140         range1 = rangeFromSeqName(seqRanges1, seqName1, seqLen1)
   141         if not range1: continue
   142         range2 = rangeFromSeqName(seqRanges2, seqName2, seqLen2)
   143         if not range2: continue
   144         b = list(croppedBlocks(blocks, range1, range2))
   145         if not b: continue
   146         aln = seqName1, seqName2, b
   147         alignments.append(aln)
   148         updateSeqLimits(opts.trim1, seqLimits1, seqName1, range1, b, 0)
   149         updateSeqLimits(opts.trim2, seqLimits2, seqName2, range2, b, 1)
   150     return alignments, seqLimits1, seqLimits2
   151 
   152 def natural_sort_key(my_string):
   153     '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
   154     parts = re.split(r'(\d+)', my_string)
   155     parts[1::2] = map(int, parts[1::2])
   156     return parts
   157 
   158 def get_text_sizes(my_strings, font, fontsize, image_mode):
   159     '''Get widths & heights, in pixels, of some strings.'''
   160     if fontsize == 0: return [(0, 0) for i in my_strings]
   161     image_size = 1, 1
   162     im = Image.new(image_mode, image_size)
   163     draw = ImageDraw.Draw(im)
   164     return [draw.textsize(i, font=font) for i in my_strings]
   165 
   166 def get_seq_info(seqLimits, font, fontsize, image_mode):
   167     '''Return miscellaneous information about the sequences.'''
   168     seqNames = seqLimits.keys()
   169     seqNames.sort(key=natural_sort_key)
   170     seq_sizes = [seqLimits[i][1] - seqLimits[i][0] for i in seqNames]
   171     name_sizes = get_text_sizes(seqNames, font, fontsize, image_mode)
   172     margin = max(zip(*name_sizes)[1])  # maximum text height
   173     return seqNames, seq_sizes, name_sizes, margin
   174 
   175 def div_ceil(x, y):
   176     '''Return x / y rounded up.'''
   177     q, r = divmod(x, y)
   178     return q + (r != 0)
   179 
   180 def tot_seq_pix(seq_sizes, bp_per_pix):
   181     '''Return the total pixels needed for sequences of the given sizes.'''
   182     return sum([div_ceil(i, bp_per_pix) for i in seq_sizes])
   183 
   184 def get_bp_per_pix(seq_sizes, pix_tween_seqs, pix_limit):
   185     '''Get the minimum bp-per-pixel that fits in the size limit.'''
   186     seq_num = len(seq_sizes)
   187     seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
   188     if seq_pix_limit < seq_num:
   189         raise Exception("can't fit the image: too many sequences?")
   190     lower_bound = div_ceil(sum(seq_sizes), seq_pix_limit)
   191     for bp_per_pix in itertools.count(lower_bound):  # slow linear search
   192         if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
   193     return bp_per_pix
   194 
   195 def get_seq_starts(seq_pix, pix_tween_seqs, margin):
   196     '''Get the start pixel for each sequence.'''
   197     seq_starts = []
   198     pix_tot = margin - pix_tween_seqs
   199     for i in seq_pix:
   200         pix_tot += pix_tween_seqs
   201         seq_starts.append(pix_tot)
   202         pix_tot += i
   203     return seq_starts
   204 
   205 def get_pix_info(seq_sizes, bp_per_pix, pix_tween_seqs, margin):
   206     '''Return pixel information about the sequences.'''
   207     seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
   208     seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
   209     tot_pix = seq_starts[-1] + seq_pix[-1]
   210     return seq_pix, seq_starts, tot_pix
   211 
   212 def drawLineForward(hits, width, bp_per_pix, beg1, beg2, size):
   213     while True:
   214         q1, r1 = divmod(beg1, bp_per_pix)
   215         q2, r2 = divmod(beg2, bp_per_pix)
   216         hits[q2 * width + q1] |= 1
   217         next_pix = min(bp_per_pix - r1, bp_per_pix - r2)
   218         if next_pix >= size: break
   219         beg1 += next_pix
   220         beg2 += next_pix
   221         size -= next_pix
   222 
   223 def drawLineReverse(hits, width, bp_per_pix, beg1, beg2, size):
   224     beg2 = -1 - beg2
   225     while True:
   226         q1, r1 = divmod(beg1, bp_per_pix)
   227         q2, r2 = divmod(beg2, bp_per_pix)
   228         hits[q2 * width + q1] |= 2
   229         next_pix = min(bp_per_pix - r1, r2 + 1)
   230         if next_pix >= size: break
   231         beg1 += next_pix
   232         beg2 -= next_pix
   233         size -= next_pix
   234 
   235 def alignmentPixels(width, height, alignments, bp_per_pix, origins1, origins2):
   236     hits = [0] * (width * height)  # the image data
   237     for seq1, seq2, blocks in alignments:
   238         ori1 = origins1[seq1]
   239         ori2 = origins2[seq2]
   240         for beg1, beg2, size in blocks:
   241             if beg1 < 0:
   242                 beg1 = -(beg1 + size)
   243                 beg2 = -(beg2 + size)
   244             if beg2 >= 0:
   245                 drawLineForward(hits, width, bp_per_pix,
   246                                 beg1 + ori1, beg2 + ori2, size)
   247             else:
   248                 drawLineReverse(hits, width, bp_per_pix,
   249                                 beg1 + ori1, beg2 - ori2, size)
   250     return hits
   251 
   252 def expandedSeqDict(seqDict):
   253     '''Allow lookup by short sequence names, e.g. chr7 as well as hg19.chr7.'''
   254     newDict = {}
   255     for name, x in seqDict.items():
   256         base = name.split(".")[-1]
   257         newDict[name] = x
   258         newDict[base] = x
   259     return newDict
   260 
   261 def readBed(fileName, seqLimits):
   262     if not fileName: return
   263     for line in myOpen(fileName):
   264         w = line.split()
   265         if not w or w[0][0] == "#": continue
   266         seqName = w[0]
   267         if seqName not in seqLimits: continue
   268         cropBeg, cropEnd = seqLimits[seqName]
   269         beg = int(w[1])
   270         end = int(w[2])
   271         b = max(beg, cropBeg)
   272         e = min(end, cropEnd)
   273         if b >= e: continue
   274         if len(w) > 8:
   275             color = tuple(map(int, w[8].split(",")))
   276         elif len(w) > 5:
   277             if   w[5] == "+": color = 255, 244, 244
   278             elif w[5] == "-": color = 244, 244, 255
   279             else:             color = 255, 228, 255
   280         else:
   281             color = 255, 228, 255
   282         yield seqName, b, e, color
   283 
   284 def isExtraFirstGapField(fields):
   285     return fields[4].isdigit()
   286 
   287 def readGaps(fileName, seqLimits):
   288     '''Read locations of unsequenced gaps, from an agp or gap file.'''
   289     if not fileName: return
   290     for line in myOpen(fileName):
   291         w = line.split()
   292         if not w or w[0][0] == "#": continue
   293         if isExtraFirstGapField(w): w = w[1:]
   294         if w[4] not in "NU": continue
   295         seqName = w[0]
   296         if seqName not in seqLimits: continue
   297         cropBeg, cropEnd = seqLimits[seqName]
   298         end = int(w[2])
   299         beg = end - int(w[5])  # zero-based coordinate
   300         b = max(beg, cropBeg)
   301         e = min(end, cropEnd)
   302         if b >= e: continue
   303         bridgedText = w[7]
   304         yield seqName, b, e, bridgedText
   305 
   306 def drawAnnotations(im, beds, origins, margin, limit, isTop, bp_per_pix):
   307     # XXX no consideration of different-color overlaps
   308     for seqName, beg, end, color in beds:
   309         ori = origins[seqName]
   310         b = (ori + beg) // bp_per_pix
   311         e = div_ceil(ori + end, bp_per_pix)
   312         if isTop: box = b, margin, e, limit
   313         else:     box = margin, b, limit, e
   314         im.paste(color, box)
   315 
   316 def drawUnsequencedGaps(im, gaps, origins, margin, limit, isTop, bridgedText,
   317                         bp_per_pix, color):
   318     '''Draw rectangles representing unsequenced gaps.'''
   319     for seqName, beg, end, b in gaps:
   320         if b != bridgedText: continue
   321         ori = origins[seqName]
   322         b = div_ceil(ori + beg, bp_per_pix)  # use fully-covered pixels only
   323         e = (ori + end) // bp_per_pix
   324         if e <= b: continue
   325         if isTop: box = b, margin, e, limit
   326         else:     box = margin, b, limit, e
   327         im.paste(color, box)
   328 
   329 def make_label(text, text_size, range_start, range_size):
   330     '''Return an axis label with endpoint & sort-order information.'''
   331     text_width  = text_size[0]
   332     label_start = range_start + (range_size - text_width) // 2
   333     label_end   = label_start + text_width
   334     sort_key    = text_width - range_size
   335     return sort_key, label_start, label_end, text
   336 
   337 def get_nonoverlapping_labels(labels, label_space):
   338     '''Get a subset of non-overlapping axis labels, greedily.'''
   339     nonoverlapping_labels = []
   340     for i in labels:
   341         if True not in [i[1] < j[2] + label_space and j[1] < i[2] + label_space
   342                         for j in nonoverlapping_labels]:
   343             nonoverlapping_labels.append(i)
   344     return nonoverlapping_labels
   345 
   346 def get_axis_image(seqNames, name_sizes, seq_starts, seq_pix,
   347                    font, image_mode, opts):
   348     '''Make an image of axis labels.'''
   349     min_pos = seq_starts[0]
   350     max_pos = seq_starts[-1] + seq_pix[-1]
   351     height = max(zip(*name_sizes)[1])
   352     labels = [make_label(i, j, k, l) for i, j, k, l in
   353               zip(seqNames, name_sizes, seq_starts, seq_pix)]
   354     labels = [i for i in labels if i[1] >= min_pos and i[2] <= max_pos]
   355     labels.sort()
   356     labels = get_nonoverlapping_labels(labels, opts.label_space)
   357     image_size = max_pos, height
   358     im = Image.new(image_mode, image_size, opts.border_shade)
   359     draw = ImageDraw.Draw(im)
   360     for i in labels:
   361         position = i[1], 0
   362         draw.text(position, i[3], font=font, fill=opts.text_color)
   363     return im
   364 
   365 def seqOrigins(seqNames, seq_starts, seqLimits, bp_per_pix):
   366     for i, j in zip(seqNames, seq_starts):
   367         yield i, bp_per_pix * j - seqLimits[i][0]
   368 
   369 def lastDotplot(opts, args):
   370     if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
   371     else:              font = ImageFont.load_default()
   372 
   373     image_mode = 'RGB'
   374     forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
   375     reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
   376     zipped_colors = zip(forward_color, reverse_color)
   377     overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
   378 
   379     warn("reading alignments...")
   380     alignments, seqLimits1, seqLimits2 = readAlignments(args[0], opts)
   381     warn("done")
   382 
   383     if not alignments: raise Exception("there are no alignments")
   384 
   385     seq_info1 = get_seq_info(seqLimits1, font, opts.fontsize, image_mode)
   386     seq_info2 = get_seq_info(seqLimits2, font, opts.fontsize, image_mode)
   387     seqNames1, seq_sizes1, name_sizes1, margin1 = seq_info1
   388     seqNames2, seq_sizes2, name_sizes2, margin2 = seq_info2
   389 
   390     warn("choosing bp per pixel...")
   391     pix_limit1 = opts.width  - margin1
   392     pix_limit2 = opts.height - margin2
   393     bp_per_pix1 = get_bp_per_pix(seq_sizes1, opts.pix_tween_seqs, pix_limit1)
   394     bp_per_pix2 = get_bp_per_pix(seq_sizes2, opts.pix_tween_seqs, pix_limit2)
   395     bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   396     warn("bp per pixel = " + str(bp_per_pix))
   397 
   398     seq_pix1, seq_starts1, width  = get_pix_info(seq_sizes1, bp_per_pix,
   399                                                  opts.pix_tween_seqs, margin1)
   400     seq_pix2, seq_starts2, height = get_pix_info(seq_sizes2, bp_per_pix,
   401                                                  opts.pix_tween_seqs, margin2)
   402 
   403     origins1 = dict(seqOrigins(seqNames1, seq_starts1, seqLimits1, bp_per_pix))
   404     origins2 = dict(seqOrigins(seqNames2, seq_starts2, seqLimits2, bp_per_pix))
   405 
   406     warn("processing alignments...")
   407     hits = alignmentPixels(width, height, alignments, bp_per_pix,
   408                            origins1, origins2)
   409     warn("done")
   410 
   411     image_size = width, height
   412     im = Image.new(image_mode, image_size, opts.background_color)
   413 
   414     seqLimits1 = expandedSeqDict(seqLimits1)
   415     seqLimits2 = expandedSeqDict(seqLimits2)
   416     origins1 = expandedSeqDict(origins1)
   417     origins2 = expandedSeqDict(origins2)
   418 
   419     beds1 = list(readBed(opts.bed1, seqLimits1))
   420     beds2 = list(readBed(opts.bed2, seqLimits2))
   421     drawAnnotations(im, beds1, origins1, margin2, height, True, bp_per_pix)
   422     drawAnnotations(im, beds2, origins2, margin1, width, False, bp_per_pix)
   423 
   424     gaps1 = list(readGaps(opts.gap1, seqLimits1))
   425     gaps2 = list(readGaps(opts.gap2, seqLimits2))
   426     # draw bridged gaps first, then unbridged gaps on top:
   427     drawUnsequencedGaps(im, gaps1, origins1, margin2, height, True, "yes",
   428                         bp_per_pix, opts.bridged_color)
   429     drawUnsequencedGaps(im, gaps2, origins2, margin1, width, False, "yes",
   430                         bp_per_pix, opts.bridged_color)
   431     drawUnsequencedGaps(im, gaps1, origins1, margin2, height, True, "no",
   432                         bp_per_pix, opts.unbridged_color)
   433     drawUnsequencedGaps(im, gaps2, origins2, margin1, width, False, "no",
   434                         bp_per_pix, opts.unbridged_color)
   435 
   436     for i in range(height):
   437         for j in range(width):
   438             store_value = hits[i * width + j]
   439             xy = j, i
   440             if   store_value == 1: im.putpixel(xy, forward_color)
   441             elif store_value == 2: im.putpixel(xy, reverse_color)
   442             elif store_value == 3: im.putpixel(xy, overlap_color)
   443 
   444     if opts.fontsize != 0:
   445         axis1 = get_axis_image(seqNames1, name_sizes1, seq_starts1, seq_pix1,
   446                                font, image_mode, opts)
   447         axis2 = get_axis_image(seqNames2, name_sizes2, seq_starts2, seq_pix2,
   448                                font, image_mode, opts)
   449         axis2 = axis2.transpose(Image.ROTATE_270)  # !!! bug hotspot
   450         im.paste(axis1, (0, 0))
   451         im.paste(axis2, (0, 0))
   452 
   453     for i in seq_starts1[1:]:
   454         box = i - opts.pix_tween_seqs, margin2, i, height
   455         im.paste(opts.border_shade, box)
   456 
   457     for i in seq_starts2[1:]:
   458         box = margin1, i - opts.pix_tween_seqs, width, i
   459         im.paste(opts.border_shade, box)
   460 
   461     im.save(args[1])
   462 
   463 if __name__ == "__main__":
   464     usage = """%prog --help
   465    or: %prog [options] maf-or-tab-alignments dotplot.png
   466    or: %prog [options] maf-or-tab-alignments dotplot.gif
   467    or: ..."""
   468     description = "Draw a dotplot of pair-wise sequence alignments in MAF or tabular format."
   469     op = optparse.OptionParser(usage=usage, description=description)
   470     op.add_option("-1", "--seq1", metavar="PATTERN", action="append",
   471                   default=[],
   472                   help="which sequences to show from the 1st genome")
   473     op.add_option("-2", "--seq2", metavar="PATTERN", action="append",
   474                   default=[],
   475                   help="which sequences to show from the 2nd genome")
   476     # Replace "width" & "height" with a single "length" option?
   477     op.add_option("-x", "--width", type="int", default=1000,
   478                   help="maximum width in pixels (default: %default)")
   479     op.add_option("-y", "--height", type="int", default=1000,
   480                   help="maximum height in pixels (default: %default)")
   481     op.add_option("-f", "--fontfile", metavar="FILE",
   482                   help="TrueType or OpenType font file")
   483     op.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
   484                   help="TrueType or OpenType font size (default: %default)")
   485     op.add_option("-c", "--forwardcolor", metavar="COLOR", default="red",
   486                   help="color for forward alignments (default: %default)")
   487     op.add_option("-r", "--reversecolor", metavar="COLOR", default="blue",
   488                   help="color for reverse alignments (default: %default)")
   489     op.add_option("--trim1", action="store_true",
   490                   help="trim unaligned sequence flanks from the 1st genome")
   491     op.add_option("--trim2", action="store_true",
   492                   help="trim unaligned sequence flanks from the 2nd genome")
   493     op.add_option("--bed1", metavar="FILE",
   494                   help="read genome1 annotations from bed file")
   495     op.add_option("--bed2", metavar="FILE",
   496                   help="read genome2 annotations from bed file")
   497     og = optparse.OptionGroup(op, "Unsequenced gap options")
   498     og.add_option("--gap1", metavar="FILE",
   499                   help="read genome1 unsequenced gaps from agp or gap file")
   500     og.add_option("--gap2", metavar="FILE",
   501                   help="read genome2 unsequenced gaps from agp or gap file")
   502     og.add_option("--bridged-color", metavar="COLOR", default="yellow",
   503                   help="color for bridged gaps (default: %default)")
   504     og.add_option("--unbridged-color", metavar="COLOR", default="pink",
   505                   help="color for unbridged gaps (default: %default)")
   506     op.add_option_group(og)
   507     (opts, args) = op.parse_args()
   508     if len(args) != 2: op.error("2 arguments needed")
   509 
   510     opts.text_color = "black"
   511     opts.background_color = "white"
   512     opts.pix_tween_seqs = 2  # number of border pixels between sequences
   513     opts.border_shade = 239, 239, 239  # the shade of grey for border pixels
   514     opts.label_space = 5     # minimum number of pixels between axis labels
   515 
   516     try: lastDotplot(opts, args)
   517     except KeyboardInterrupt: pass  # avoid silly error message
   518     except Exception, e:
   519         prog = os.path.basename(sys.argv[0])
   520         sys.exit(prog + ": error: " + str(e))