scripts/last-dotplot
author Martin C. Frith
Tue May 02 14:04:09 2017 +0900 (2017-05-02)
changeset 852 f3b6c666afad
parent 851 5b2acb7fdb3e
child 855 09a9d7ef13ae
permissions -rwxr-xr-x
Add border options to last-dotplot
     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 = 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 = 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 updateSeqs(isTrim, seqNames, seqLimits, seqName, seqRange, blocks, index):
   118     if seqName not in seqLimits:
   119         seqNames.append(seqName)
   120     if isTrim:
   121         beg = blocks[0][index]
   122         end = blocks[-1][index] + blocks[-1][2]
   123         if beg < 0: beg, end = -end, -beg
   124         if seqName in seqLimits:
   125             b, e = seqLimits[seqName]
   126             seqLimits[seqName] = min(b, beg), max(e, end)
   127         else:
   128             seqLimits[seqName] = beg, end
   129     else:
   130         seqLimits[seqName] = seqRange
   131 
   132 def readAlignments(fileName, opts):
   133     '''Get alignments and sequence limits, from MAF or tabular format.'''
   134     seqRanges1 = map(seqRangeFromText, opts.seq1)
   135     seqRanges2 = map(seqRangeFromText, opts.seq2)
   136 
   137     alignments = []
   138     seqNames1 = []
   139     seqNames2 = []
   140     seqLimits1 = {}
   141     seqLimits2 = {}
   142     lines = myOpen(fileName)
   143     for seqName1, seqLen1, seqName2, seqLen2, blocks in alignmentInput(lines):
   144         range1 = rangeFromSeqName(seqRanges1, seqName1, seqLen1)
   145         if not range1: continue
   146         range2 = rangeFromSeqName(seqRanges2, seqName2, seqLen2)
   147         if not range2: continue
   148         b = list(croppedBlocks(list(blocks), range1, range2))
   149         if not b: continue
   150         aln = seqName1, seqName2, b
   151         alignments.append(aln)
   152         updateSeqs(opts.trim1, seqNames1, seqLimits1, seqName1, range1, b, 0)
   153         updateSeqs(opts.trim2, seqNames2, seqLimits2, seqName2, range2, b, 1)
   154     return alignments, seqNames1, seqNames2, seqLimits1, seqLimits2
   155 
   156 def natural_sort_key(my_string):
   157     '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
   158     parts = re.split(r'(\d+)', my_string)
   159     parts[1::2] = map(int, parts[1::2])
   160     return parts
   161 
   162 def get_text_sizes(my_strings, font, fontsize, image_mode):
   163     '''Get widths & heights, in pixels, of some strings.'''
   164     if fontsize == 0: return [(0, 0) for i in my_strings]
   165     image_size = 1, 1
   166     im = Image.new(image_mode, image_size)
   167     draw = ImageDraw.Draw(im)
   168     return [draw.textsize(i, font=font) for i in my_strings]
   169 
   170 def sizeText(size):
   171     suffixes = "bp", "kb", "Mb", "Gb"
   172     for i, x in enumerate(suffixes):
   173         j = 10 ** (i * 3)
   174         if size < j * 10:
   175             return "%.2g" % (1.0 * size / j) + x
   176         if size < j * 1000 or i == len(suffixes) - 1:
   177             return "%.0f" % (1.0 * size / j) + x
   178 
   179 def seqNameAndSizeText(seqName, seqSize):
   180     return seqName + ": " + sizeText(seqSize)
   181 
   182 def getSeqInfo(sortOpt, seqNames, seqLimits,
   183                font, fontsize, image_mode, isShowSize):
   184     '''Return miscellaneous information about the sequences.'''
   185     if sortOpt == 1:
   186         seqNames.sort(key=natural_sort_key)
   187     seqSizes = [seqLimits[i][1] - seqLimits[i][0] for i in seqNames]
   188     if sortOpt == 2:
   189         seqRecords = sorted(zip(seqSizes, seqNames), reverse=True)
   190         seqSizes = [i[0] for i in seqRecords]
   191         seqNames = [i[1] for i in seqRecords]
   192     if isShowSize:
   193         seqLabels = map(seqNameAndSizeText, seqNames, seqSizes)
   194     else:
   195         seqLabels = seqNames
   196     labelSizes = get_text_sizes(seqLabels, font, fontsize, image_mode)
   197     margin = max(zip(*labelSizes)[1])  # maximum text height
   198     return seqNames, seqSizes, seqLabels, labelSizes, margin
   199 
   200 def div_ceil(x, y):
   201     '''Return x / y rounded up.'''
   202     q, r = divmod(x, y)
   203     return q + (r != 0)
   204 
   205 def tot_seq_pix(seq_sizes, bp_per_pix):
   206     '''Return the total pixels needed for sequences of the given sizes.'''
   207     return sum([div_ceil(i, bp_per_pix) for i in seq_sizes])
   208 
   209 def get_bp_per_pix(seq_sizes, pix_tween_seqs, pix_limit):
   210     '''Get the minimum bp-per-pixel that fits in the size limit.'''
   211     seq_num = len(seq_sizes)
   212     seq_pix_limit = pix_limit - pix_tween_seqs * (seq_num - 1)
   213     if seq_pix_limit < seq_num:
   214         raise Exception("can't fit the image: too many sequences?")
   215     lower_bound = div_ceil(sum(seq_sizes), seq_pix_limit)
   216     for bp_per_pix in itertools.count(lower_bound):  # slow linear search
   217         if tot_seq_pix(seq_sizes, bp_per_pix) <= seq_pix_limit: break
   218     return bp_per_pix
   219 
   220 def get_seq_starts(seq_pix, pix_tween_seqs, margin):
   221     '''Get the start pixel for each sequence.'''
   222     seq_starts = []
   223     pix_tot = margin - pix_tween_seqs
   224     for i in seq_pix:
   225         pix_tot += pix_tween_seqs
   226         seq_starts.append(pix_tot)
   227         pix_tot += i
   228     return seq_starts
   229 
   230 def get_pix_info(seq_sizes, bp_per_pix, pix_tween_seqs, margin):
   231     '''Return pixel information about the sequences.'''
   232     seq_pix = [div_ceil(i, bp_per_pix) for i in seq_sizes]
   233     seq_starts = get_seq_starts(seq_pix, pix_tween_seqs, margin)
   234     tot_pix = seq_starts[-1] + seq_pix[-1]
   235     return seq_pix, seq_starts, tot_pix
   236 
   237 def drawLineForward(hits, width, bp_per_pix, beg1, beg2, size):
   238     while True:
   239         q1, r1 = divmod(beg1, bp_per_pix)
   240         q2, r2 = divmod(beg2, bp_per_pix)
   241         hits[q2 * width + q1] |= 1
   242         next_pix = min(bp_per_pix - r1, bp_per_pix - r2)
   243         if next_pix >= size: break
   244         beg1 += next_pix
   245         beg2 += next_pix
   246         size -= next_pix
   247 
   248 def drawLineReverse(hits, width, bp_per_pix, beg1, beg2, size):
   249     beg2 = -1 - beg2
   250     while True:
   251         q1, r1 = divmod(beg1, bp_per_pix)
   252         q2, r2 = divmod(beg2, bp_per_pix)
   253         hits[q2 * width + q1] |= 2
   254         next_pix = min(bp_per_pix - r1, r2 + 1)
   255         if next_pix >= size: break
   256         beg1 += next_pix
   257         beg2 -= next_pix
   258         size -= next_pix
   259 
   260 def alignmentPixels(width, height, alignments, bp_per_pix, origins1, origins2):
   261     hits = [0] * (width * height)  # the image data
   262     for seq1, seq2, blocks in alignments:
   263         ori1 = origins1[seq1]
   264         ori2 = origins2[seq2]
   265         for beg1, beg2, size in blocks:
   266             if beg1 < 0:
   267                 beg1 = -(beg1 + size)
   268                 beg2 = -(beg2 + size)
   269             if beg2 >= 0:
   270                 drawLineForward(hits, width, bp_per_pix,
   271                                 beg1 + ori1, beg2 + ori2, size)
   272             else:
   273                 drawLineReverse(hits, width, bp_per_pix,
   274                                 beg1 + ori1, beg2 - ori2, size)
   275     return hits
   276 
   277 def expandedSeqDict(seqDict):
   278     '''Allow lookup by short sequence names, e.g. chr7 as well as hg19.chr7.'''
   279     newDict = {}
   280     for name, x in seqDict.items():
   281         base = name.split(".")[-1]
   282         newDict[name] = x
   283         newDict[base] = x
   284     return newDict
   285 
   286 def readBed(fileName, seqLimits):
   287     if not fileName: return
   288     for line in myOpen(fileName):
   289         w = line.split()
   290         if not w: continue
   291         seqName = w[0]
   292         if seqName not in seqLimits: continue
   293         cropBeg, cropEnd = seqLimits[seqName]
   294         beg = int(w[1])
   295         end = int(w[2])
   296         b = max(beg, cropBeg)
   297         e = min(end, cropEnd)
   298         if b >= e: continue
   299         if len(w) > 8:
   300             color = tuple(map(int, w[8].split(",")))
   301         elif len(w) > 5:
   302             if   w[5] == "+": color = 255, 244, 244
   303             elif w[5] == "-": color = 244, 244, 255
   304             else:             color = 255, 228, 255
   305         else:
   306             color = 255, 228, 255
   307         yield seqName, b, e, color
   308 
   309 def isExtraFirstGapField(fields):
   310     return fields[4].isdigit()
   311 
   312 def readGaps(fileName, seqLimits):
   313     '''Read locations of unsequenced gaps, from an agp or gap file.'''
   314     if not fileName: return
   315     for line in myOpen(fileName):
   316         w = line.split()
   317         if not w or w[0][0] == "#": continue
   318         if isExtraFirstGapField(w): w = w[1:]
   319         if w[4] not in "NU": continue
   320         seqName = w[0]
   321         if seqName not in seqLimits: continue
   322         cropBeg, cropEnd = seqLimits[seqName]
   323         end = int(w[2])
   324         beg = end - int(w[5])  # zero-based coordinate
   325         b = max(beg, cropBeg)
   326         e = min(end, cropEnd)
   327         if b >= e: continue
   328         bridgedText = w[7]
   329         yield seqName, b, e, bridgedText
   330 
   331 def drawAnnotations(im, beds, origins, margin, limit, isTop, bp_per_pix):
   332     # XXX no consideration of different-color overlaps
   333     for seqName, beg, end, color in beds:
   334         ori = origins[seqName]
   335         b = (ori + beg) // bp_per_pix
   336         e = div_ceil(ori + end, bp_per_pix)
   337         if isTop: box = b, margin, e, limit
   338         else:     box = margin, b, limit, e
   339         im.paste(color, box)
   340 
   341 def drawUnsequencedGaps(im, gaps, origins, margin, limit, isTop, bridgedText,
   342                         bp_per_pix, color):
   343     '''Draw rectangles representing unsequenced gaps.'''
   344     for seqName, beg, end, b in gaps:
   345         if b != bridgedText: continue
   346         ori = origins[seqName]
   347         b = div_ceil(ori + beg, bp_per_pix)  # use fully-covered pixels only
   348         e = (ori + end) // bp_per_pix
   349         if e <= b: continue
   350         if isTop: box = b, margin, e, limit
   351         else:     box = margin, b, limit, e
   352         im.paste(color, box)
   353 
   354 def make_label(text, text_size, range_start, range_size):
   355     '''Return an axis label with endpoint & sort-order information.'''
   356     text_width  = text_size[0]
   357     label_start = range_start + (range_size - text_width) // 2
   358     label_end   = label_start + text_width
   359     sort_key    = text_width - range_size
   360     return sort_key, label_start, label_end, text
   361 
   362 def get_nonoverlapping_labels(labels, label_space):
   363     '''Get a subset of non-overlapping axis labels, greedily.'''
   364     nonoverlapping_labels = []
   365     for i in labels:
   366         if True not in [i[1] < j[2] + label_space and j[1] < i[2] + label_space
   367                         for j in nonoverlapping_labels]:
   368             nonoverlapping_labels.append(i)
   369     return nonoverlapping_labels
   370 
   371 def get_axis_image(seqNames, name_sizes, seq_starts, seq_pix,
   372                    font, image_mode, opts):
   373     '''Make an image of axis labels.'''
   374     min_pos = seq_starts[0]
   375     max_pos = seq_starts[-1] + seq_pix[-1]
   376     height = max(zip(*name_sizes)[1])
   377     labels = map(make_label, seqNames, name_sizes, seq_starts, seq_pix)
   378     labels = [i for i in labels if i[1] >= min_pos and i[2] <= max_pos]
   379     labels.sort()
   380     labels = get_nonoverlapping_labels(labels, opts.label_space)
   381     image_size = max_pos, height
   382     im = Image.new(image_mode, image_size, opts.border_color)
   383     draw = ImageDraw.Draw(im)
   384     for i in labels:
   385         position = i[1], 0
   386         draw.text(position, i[3], font=font, fill=opts.text_color)
   387     return im
   388 
   389 def seqOrigins(seqNames, seq_starts, seqLimits, bp_per_pix):
   390     for i, j in zip(seqNames, seq_starts):
   391         yield i, bp_per_pix * j - seqLimits[i][0]
   392 
   393 def lastDotplot(opts, args):
   394     if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
   395     else:              font = ImageFont.load_default()
   396 
   397     image_mode = 'RGB'
   398     forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
   399     reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
   400     zipped_colors = zip(forward_color, reverse_color)
   401     overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
   402 
   403     warn("reading alignments...")
   404     alignmentInfo = readAlignments(args[0], opts)
   405     alignments, seqNames1, seqNames2, seqLimits1, seqLimits2 = alignmentInfo
   406     warn("done")
   407     if not alignments: raise Exception("there are no alignments")
   408 
   409     i1 = getSeqInfo(opts.sort1, seqNames1, seqLimits1,
   410                     font, opts.fontsize, image_mode, opts.lengths1)
   411     seqNames1, seqSizes1, seqLabels1, labelSizes1, margin1 = i1
   412 
   413     i2 = getSeqInfo(opts.sort2, seqNames2, seqLimits2,
   414                     font, opts.fontsize, image_mode, opts.lengths2)
   415     seqNames2, seqSizes2, seqLabels2, labelSizes2, margin2 = i2
   416 
   417     warn("choosing bp per pixel...")
   418     pix_limit1 = opts.width  - margin1
   419     pix_limit2 = opts.height - margin2
   420     bp_per_pix1 = get_bp_per_pix(seqSizes1, opts.border_pixels, pix_limit1)
   421     bp_per_pix2 = get_bp_per_pix(seqSizes2, opts.border_pixels, pix_limit2)
   422     bp_per_pix = max(bp_per_pix1, bp_per_pix2)
   423     warn("bp per pixel = " + str(bp_per_pix))
   424 
   425     seq_pix1, seq_starts1, width  = get_pix_info(seqSizes1, bp_per_pix,
   426                                                  opts.border_pixels, margin1)
   427     seq_pix2, seq_starts2, height = get_pix_info(seqSizes2, bp_per_pix,
   428                                                  opts.border_pixels, margin2)
   429     warn("width:  " + str(width))
   430     warn("height: " + str(height))
   431 
   432     origins1 = dict(seqOrigins(seqNames1, seq_starts1, seqLimits1, bp_per_pix))
   433     origins2 = dict(seqOrigins(seqNames2, seq_starts2, seqLimits2, bp_per_pix))
   434 
   435     warn("processing alignments...")
   436     hits = alignmentPixels(width, height, alignments, bp_per_pix,
   437                            origins1, origins2)
   438     warn("done")
   439 
   440     image_size = width, height
   441     im = Image.new(image_mode, image_size, opts.background_color)
   442 
   443     seqLimits1 = expandedSeqDict(seqLimits1)
   444     seqLimits2 = expandedSeqDict(seqLimits2)
   445     origins1 = expandedSeqDict(origins1)
   446     origins2 = expandedSeqDict(origins2)
   447 
   448     beds1 = list(readBed(opts.bed1, seqLimits1))
   449     beds2 = list(readBed(opts.bed2, seqLimits2))
   450     drawAnnotations(im, beds1, origins1, margin2, height, True, bp_per_pix)
   451     drawAnnotations(im, beds2, origins2, margin1, width, False, bp_per_pix)
   452 
   453     gaps1 = list(readGaps(opts.gap1, seqLimits1))
   454     gaps2 = list(readGaps(opts.gap2, seqLimits2))
   455     # draw bridged gaps first, then unbridged gaps on top:
   456     drawUnsequencedGaps(im, gaps1, origins1, margin2, height, True, "yes",
   457                         bp_per_pix, opts.bridged_color)
   458     drawUnsequencedGaps(im, gaps2, origins2, margin1, width, False, "yes",
   459                         bp_per_pix, opts.bridged_color)
   460     drawUnsequencedGaps(im, gaps1, origins1, margin2, height, True, "no",
   461                         bp_per_pix, opts.unbridged_color)
   462     drawUnsequencedGaps(im, gaps2, origins2, margin1, width, False, "no",
   463                         bp_per_pix, opts.unbridged_color)
   464 
   465     for i in range(height):
   466         for j in range(width):
   467             store_value = hits[i * width + j]
   468             xy = j, i
   469             if   store_value == 1: im.putpixel(xy, forward_color)
   470             elif store_value == 2: im.putpixel(xy, reverse_color)
   471             elif store_value == 3: im.putpixel(xy, overlap_color)
   472 
   473     if opts.fontsize != 0:
   474         axis1 = get_axis_image(seqLabels1, labelSizes1, seq_starts1, seq_pix1,
   475                                font, image_mode, opts)
   476         axis2 = get_axis_image(seqLabels2, labelSizes2, seq_starts2, seq_pix2,
   477                                font, image_mode, opts)
   478         axis2 = axis2.transpose(Image.ROTATE_270)  # !!! bug hotspot
   479         im.paste(axis1, (0, 0))
   480         im.paste(axis2, (0, 0))
   481 
   482     for i in seq_starts1[1:]:
   483         box = i - opts.border_pixels, margin2, i, height
   484         im.paste(opts.border_color, box)
   485 
   486     for i in seq_starts2[1:]:
   487         box = margin1, i - opts.border_pixels, width, i
   488         im.paste(opts.border_color, box)
   489 
   490     im.save(args[1])
   491 
   492 if __name__ == "__main__":
   493     usage = """%prog --help
   494    or: %prog [options] maf-or-tab-alignments dotplot.png
   495    or: %prog [options] maf-or-tab-alignments dotplot.gif
   496    or: ..."""
   497     description = "Draw a dotplot of pair-wise sequence alignments in MAF or tabular format."
   498     op = optparse.OptionParser(usage=usage, description=description)
   499     op.add_option("-1", "--seq1", metavar="PATTERN", action="append",
   500                   default=[],
   501                   help="which sequences to show from the 1st genome")
   502     op.add_option("-2", "--seq2", metavar="PATTERN", action="append",
   503                   default=[],
   504                   help="which sequences to show from the 2nd genome")
   505     # Replace "width" & "height" with a single "length" option?
   506     op.add_option("-x", "--width", type="int", default=1000,
   507                   help="maximum width in pixels (default: %default)")
   508     op.add_option("-y", "--height", type="int", default=1000,
   509                   help="maximum height in pixels (default: %default)")
   510     op.add_option("-c", "--forwardcolor", metavar="COLOR", default="red",
   511                   help="color for forward alignments (default: %default)")
   512     op.add_option("-r", "--reversecolor", metavar="COLOR", default="blue",
   513                   help="color for reverse alignments (default: %default)")
   514     op.add_option("--sort1", type="int", default=1, metavar="N",
   515                   help="genome1 sequence order: 0=input order, 1=name order, "
   516                   "2=length order (default=%default)")
   517     op.add_option("--sort2", type="int", default=1, metavar="N",
   518                   help="genome2 sequence order: 0=input order, 1=name order, "
   519                   "2=length order (default=%default)")
   520     op.add_option("--trim1", action="store_true",
   521                   help="trim unaligned sequence flanks from the 1st genome")
   522     op.add_option("--trim2", action="store_true",
   523                   help="trim unaligned sequence flanks from the 2nd genome")
   524     op.add_option("--border-pixels", metavar="INT", type="int", default=1,
   525                   help="number of pixels between sequences (default=%default)")
   526     op.add_option("--border-color", metavar="COLOR", default="#dcdcdc",
   527                   help="color for pixels between sequences (default=%default)")
   528     # xxx --margin-color?
   529     op.add_option("--bed1", metavar="FILE",
   530                   help="read genome1 annotations from bed file")
   531     op.add_option("--bed2", metavar="FILE",
   532                   help="read genome2 annotations from bed file")
   533 
   534     og = optparse.OptionGroup(op, "Text options")
   535     og.add_option("-f", "--fontfile", metavar="FILE",
   536                   help="TrueType or OpenType font file")
   537     og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
   538                   help="TrueType or OpenType font size (default: %default)")
   539     og.add_option("--lengths1", action="store_true",
   540                   help="show sequence lengths for the 1st (horizontal) genome")
   541     og.add_option("--lengths2", action="store_true",
   542                   help="show sequence lengths for the 2nd (vertical) genome")
   543     op.add_option_group(og)
   544 
   545     og = optparse.OptionGroup(op, "Unsequenced gap options")
   546     og.add_option("--gap1", metavar="FILE",
   547                   help="read genome1 unsequenced gaps from agp or gap file")
   548     og.add_option("--gap2", metavar="FILE",
   549                   help="read genome2 unsequenced gaps from agp or gap file")
   550     og.add_option("--bridged-color", metavar="COLOR", default="yellow",
   551                   help="color for bridged gaps (default: %default)")
   552     og.add_option("--unbridged-color", metavar="COLOR", default="pink",
   553                   help="color for unbridged gaps (default: %default)")
   554     op.add_option_group(og)
   555     (opts, args) = op.parse_args()
   556     if len(args) != 2: op.error("2 arguments needed")
   557 
   558     opts.text_color = "black"
   559     opts.background_color = "white"
   560     opts.label_space = 5     # minimum number of pixels between axis labels
   561 
   562     try: lastDotplot(opts, args)
   563     except KeyboardInterrupt: pass  # avoid silly error message
   564     except Exception, e:
   565         prog = os.path.basename(sys.argv[0])
   566         sys.exit(prog + ": error: " + str(e))