scripts/last-dotplot
author Martin C. Frith
Thu Nov 02 21:08:55 2017 +0900 (2017-11-02)
changeset 898 f6a9c15287ea
parent 897 f7bc7d099e2b
child 899 ccf8902bf86c
permissions -rwxr-xr-x
Change last-dotplot's label options
     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 gzip
    13 from fnmatch import fnmatchcase
    14 import itertools, optparse, os, re, sys
    15 
    16 # Try to make PIL/PILLOW work:
    17 try: from PIL import Image, ImageDraw, ImageFont, ImageColor
    18 except ImportError: import Image, ImageDraw, ImageFont, ImageColor
    19 
    20 def myOpen(fileName):  # faster than fileinput
    21     if fileName == "-":
    22         return sys.stdin
    23     if fileName.endswith(".gz"):
    24         return gzip.open(fileName)
    25     return open(fileName)
    26 
    27 def warn(message):
    28     if opts.verbose:
    29         prog = os.path.basename(sys.argv[0])
    30         sys.stderr.write(prog + ": " + message + "\n")
    31 
    32 def croppedBlocks(blocks, range1, range2):
    33     cropBeg1, cropEnd1 = range1
    34     cropBeg2, cropEnd2 = range2
    35     if blocks[0][0] < 0: cropBeg1, cropEnd1 = -cropEnd1, -cropBeg1
    36     if blocks[0][1] < 0: cropBeg2, cropEnd2 = -cropEnd2, -cropBeg2
    37     for beg1, beg2, size in blocks:
    38         b1 = max(cropBeg1, beg1)
    39         e1 = min(cropEnd1, beg1 + size)
    40         if b1 >= e1: continue
    41         offset = beg2 - beg1
    42         b2 = max(cropBeg2, b1 + offset)
    43         e2 = min(cropEnd2, e1 + offset)
    44         if b2 >= e2: continue
    45         yield b2 - offset, b2, e2 - b2
    46 
    47 def tabBlocks(beg1, beg2, blocks):
    48     '''Get the gapless blocks of an alignment, from LAST tabular format.'''
    49     for i in blocks.split(","):
    50         if ":" in i:
    51             x, y = i.split(":")
    52             beg1 += int(x)
    53             beg2 += int(y)
    54         else:
    55             size = int(i)
    56             yield beg1, beg2, size
    57             beg1 += size
    58             beg2 += size
    59 
    60 def mafBlocks(beg1, beg2, seq1, seq2):
    61     '''Get the gapless blocks of an alignment, from MAF format.'''
    62     size = 0
    63     for x, y in itertools.izip(seq1, seq2):
    64         if x == "-":
    65             if size:
    66                 yield beg1, beg2, size
    67                 beg1 += size
    68                 beg2 += size
    69                 size = 0
    70             beg2 += 1
    71         elif y == "-":
    72             if size:
    73                 yield beg1, beg2, size
    74                 beg1 += size
    75                 beg2 += size
    76                 size = 0
    77             beg1 += 1
    78         else:
    79             size += 1
    80     if size: yield beg1, beg2, size
    81 
    82 def alignmentInput(lines):
    83     '''Get alignments and sequence lengths, from MAF or tabular format.'''
    84     mafCount = 0
    85     for line in lines:
    86         w = line.split()
    87         if line[0].isdigit():  # tabular format
    88             chr1, beg1, seqlen1 = w[1], int(w[2]), int(w[5])
    89             if w[4] == "-": beg1 -= seqlen1
    90             chr2, beg2, seqlen2 = w[6], int(w[7]), int(w[10])
    91             if w[9] == "-": beg2 -= seqlen2
    92             blocks = tabBlocks(beg1, beg2, w[11])
    93             yield chr1, seqlen1, chr2, seqlen2, blocks
    94         elif line[0] == "s":  # MAF format
    95             if mafCount == 0:
    96                 chr1, beg1, seqlen1, seq1 = w[1], int(w[2]), int(w[5]), w[6]
    97                 if w[4] == "-": beg1 -= seqlen1
    98                 mafCount = 1
    99             else:
   100                 chr2, beg2, seqlen2, seq2 = w[1], int(w[2]), int(w[5]), w[6]
   101                 if w[4] == "-": beg2 -= seqlen2
   102                 blocks = mafBlocks(beg1, beg2, seq1, seq2)
   103                 yield chr1, seqlen1, chr2, seqlen2, blocks
   104                 mafCount = 0
   105 
   106 def seqRequestFromText(text):
   107     if ":" in text:
   108         pattern, interval = text.rsplit(":", 1)
   109         if "-" in interval:
   110             beg, end = interval.rsplit("-", 1)
   111             return pattern, int(beg), int(end)  # beg may be negative
   112     return text, 0, sys.maxsize
   113 
   114 def rangeFromSeqName(seqRequests, name, seqLen):
   115     if not seqRequests: return 0, seqLen
   116     base = name.split(".")[-1]  # allow for names like hg19.chr7
   117     for pat, beg, end in seqRequests:
   118         if fnmatchcase(name, pat) or fnmatchcase(base, pat):
   119             return max(beg, 0), min(end, seqLen)
   120     return None
   121 
   122 def updateSeqs(isTrim, seqNames, seqLimits, seqName, seqRange, blocks, index):
   123     if seqName not in seqLimits:
   124         seqNames.append(seqName)
   125     if isTrim:
   126         beg = blocks[0][index]
   127         end = blocks[-1][index] + blocks[-1][2]
   128         if beg < 0: beg, end = -end, -beg
   129         if seqName in seqLimits:
   130             b, e = seqLimits[seqName]
   131             seqLimits[seqName] = min(b, beg), max(e, end)
   132         else:
   133             seqLimits[seqName] = beg, end
   134     else:
   135         seqLimits[seqName] = seqRange
   136 
   137 def readAlignments(fileName, opts):
   138     '''Get alignments and sequence limits, from MAF or tabular format.'''
   139     seqRequests1 = map(seqRequestFromText, opts.seq1)
   140     seqRequests2 = map(seqRequestFromText, opts.seq2)
   141 
   142     alignments = []
   143     seqNames1 = []
   144     seqNames2 = []
   145     seqLimits1 = {}
   146     seqLimits2 = {}
   147     lines = myOpen(fileName)
   148     for seqName1, seqLen1, seqName2, seqLen2, blocks in alignmentInput(lines):
   149         range1 = rangeFromSeqName(seqRequests1, seqName1, seqLen1)
   150         if not range1: continue
   151         range2 = rangeFromSeqName(seqRequests2, seqName2, seqLen2)
   152         if not range2: continue
   153         b = list(croppedBlocks(list(blocks), range1, range2))
   154         if not b: continue
   155         aln = seqName1, seqName2, b
   156         alignments.append(aln)
   157         updateSeqs(opts.trim1, seqNames1, seqLimits1, seqName1, range1, b, 0)
   158         updateSeqs(opts.trim2, seqNames2, seqLimits2, seqName2, range2, b, 1)
   159     return alignments, seqNames1, seqNames2, seqLimits1, seqLimits2
   160 
   161 def natural_sort_key(my_string):
   162     '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
   163     parts = re.split(r'(\d+)', my_string)
   164     parts[1::2] = map(int, parts[1::2])
   165     return parts
   166 
   167 def textDimensions(imageDraw, font, textRot, text):
   168     x, y = imageDraw.textsize(text, font=font)
   169     return (y, x) if textRot else (x, y)
   170 
   171 def get_text_sizes(my_strings, font, fontsize, image_mode, textRot):
   172     '''Get widths & heights, in pixels, of some strings.'''
   173     if fontsize == 0: return [(0, 0) for i in my_strings]
   174     image_size = 1, 1
   175     im = Image.new(image_mode, image_size)
   176     draw = ImageDraw.Draw(im)
   177     return [textDimensions(draw, font, textRot, i) for i in my_strings]
   178 
   179 def prettyNum(n):
   180     t = str(n)
   181     groups = []
   182     while t:
   183         groups.append(t[-3:])
   184         t = t[:-3]
   185     return " ".join(reversed(groups))
   186 
   187 def sizeText(size):
   188     suffixes = "bp", "kb", "Mb", "Gb"
   189     for i, x in enumerate(suffixes):
   190         j = 10 ** (i * 3)
   191         if size < j * 10:
   192             return "%.2g" % (1.0 * size / j) + x
   193         if size < j * 1000 or i == len(suffixes) - 1:
   194             return "%.0f" % (1.0 * size / j) + x
   195 
   196 def labelText(seqRange, labelOpt):
   197     seqName, beg, end = seqRange
   198     if labelOpt == 1:
   199         return seqName + ": " + sizeText(end - beg)
   200     if labelOpt == 2:
   201         return seqName + ": " + prettyNum(beg) + ": " + sizeText(end - beg)
   202     if labelOpt == 3:
   203         return seqName + ": " + prettyNum(beg) + " - " + prettyNum(end)
   204     return seqName
   205 
   206 def getSeqInfo(sortOpt, seqNames, seqLimits,
   207                font, fontsize, image_mode, labelOpt, textRot):
   208     '''Return miscellaneous information about the sequences.'''
   209     if sortOpt == 1:
   210         seqNames.sort(key=natural_sort_key)
   211     seqSizes = [seqLimits[i][1] - seqLimits[i][0] for i in seqNames]
   212     for i in seqNames:
   213         r = seqLimits[i]
   214         out = i, str(r[0]), str(r[1])
   215         warn("\t".join(out))
   216     warn("")
   217     if sortOpt == 2:
   218         seqRecords = sorted(zip(seqSizes, seqNames), reverse=True)
   219         seqSizes = [i[0] for i in seqRecords]
   220         seqNames = [i[1] for i in seqRecords]
   221     seqRanges = [(i, seqLimits[i][0], seqLimits[i][1]) for i in seqNames]
   222     seqLabels = [labelText(i, labelOpt) for i in seqRanges]
   223     labelSizes = get_text_sizes(seqLabels, font, fontsize, image_mode, textRot)
   224     margin = max(i[1] for i in labelSizes)
   225     # xxx the margin may be too big, because some labels may get omitted
   226     return seqNames, seqSizes, seqLabels, labelSizes, margin
   227 
   228 def div_ceil(x, y):
   229     '''Return x / y rounded up.'''
   230     q, r = divmod(x, y)
   231     return q + (r != 0)
   232 
   233 def get_bp_per_pix(rangeSizes, pixTweenRanges, maxPixels):
   234     '''Get the minimum bp-per-pixel that fits in the size limit.'''
   235     numOfRanges = len(rangeSizes)
   236     maxPixelsInRanges = maxPixels - pixTweenRanges * (numOfRanges - 1)
   237     if maxPixelsInRanges < numOfRanges:
   238         raise Exception("can't fit the image: too many sequences?")
   239     negLimit = -maxPixelsInRanges
   240     negBpPerPix = sum(rangeSizes) // negLimit
   241     while True:
   242         if sum(i // negBpPerPix for i in rangeSizes) >= negLimit:
   243             return -negBpPerPix
   244         negBpPerPix -= 1
   245 
   246 def get_seq_starts(seq_pix, pixTweenRanges, margin):
   247     '''Get the start pixel for each sequence.'''
   248     seq_starts = []
   249     pix_tot = margin - pixTweenRanges
   250     for i in seq_pix:
   251         pix_tot += pixTweenRanges
   252         seq_starts.append(pix_tot)
   253         pix_tot += i
   254     return seq_starts
   255 
   256 def pixelData(rangeSizes, bp_per_pix, pixTweenRanges, margin):
   257     '''Return pixel information about the sequences.'''
   258     seq_pix = [div_ceil(i, bp_per_pix) for i in rangeSizes]
   259     seq_starts = get_seq_starts(seq_pix, pixTweenRanges, margin)
   260     tot_pix = seq_starts[-1] + seq_pix[-1]
   261     return seq_pix, seq_starts, tot_pix
   262 
   263 def drawLineForward(hits, width, bp_per_pix, beg1, beg2, size):
   264     while True:
   265         q1, r1 = divmod(beg1, bp_per_pix)
   266         q2, r2 = divmod(beg2, bp_per_pix)
   267         hits[q2 * width + q1] |= 1
   268         next_pix = min(bp_per_pix - r1, bp_per_pix - r2)
   269         if next_pix >= size: break
   270         beg1 += next_pix
   271         beg2 += next_pix
   272         size -= next_pix
   273 
   274 def drawLineReverse(hits, width, bp_per_pix, beg1, beg2, size):
   275     beg2 = -1 - beg2
   276     while True:
   277         q1, r1 = divmod(beg1, bp_per_pix)
   278         q2, r2 = divmod(beg2, bp_per_pix)
   279         hits[q2 * width + q1] |= 2
   280         next_pix = min(bp_per_pix - r1, r2 + 1)
   281         if next_pix >= size: break
   282         beg1 += next_pix
   283         beg2 -= next_pix
   284         size -= next_pix
   285 
   286 def alignmentPixels(width, height, alignments, bp_per_pix, origins1, origins2):
   287     hits = [0] * (width * height)  # the image data
   288     for seq1, seq2, blocks in alignments:
   289         ori1 = origins1[seq1]
   290         ori2 = origins2[seq2]
   291         for beg1, beg2, size in blocks:
   292             if beg1 < 0:
   293                 beg1 = -(beg1 + size)
   294                 beg2 = -(beg2 + size)
   295             if beg2 >= 0:
   296                 drawLineForward(hits, width, bp_per_pix,
   297                                 beg1 + ori1, beg2 + ori2, size)
   298             else:
   299                 drawLineReverse(hits, width, bp_per_pix,
   300                                 beg1 + ori1, beg2 - ori2, size)
   301     return hits
   302 
   303 def expandedSeqDict(seqDict):
   304     '''Allow lookup by short sequence names, e.g. chr7 as well as hg19.chr7.'''
   305     newDict = seqDict.copy()
   306     for name, x in seqDict.items():
   307         if "." in name:
   308             base = name.split(".")[-1]
   309             if base in newDict:  # an ambiguous case was found:
   310                 return seqDict   # so give up completely
   311             newDict[base] = x
   312     return newDict
   313 
   314 def readBed(fileName, seqLimits):
   315     if not fileName: return
   316     for line in myOpen(fileName):
   317         w = line.split()
   318         if not w: continue
   319         seqName = w[0]
   320         if seqName not in seqLimits: continue
   321         beg = int(w[1])
   322         end = int(w[2])
   323         layer = 900
   324         color = "#fbf"
   325         if len(w) > 4:
   326             if w[4] != ".":
   327                 layer = float(w[4])
   328             if len(w) > 5:
   329                 if len(w) > 8 and w[8].count(",") == 2:
   330                     color = "rgb(" + w[8] + ")"
   331                 elif w[5] == "+":
   332                     color = "#ffe8e8"
   333                 elif w[5] == "-":
   334                     color = "#e8e8ff"
   335         yield layer, color, seqName, beg, end
   336 
   337 def commaSeparatedInts(text):
   338     return map(int, text.rstrip(",").split(","))
   339 
   340 def readGenePred(opts, fileName, seqLimits):
   341     if not fileName: return
   342     for line in myOpen(fileName):
   343         fields = line.split()
   344         if not fields: continue
   345         if fields[2] not in "+-": fields = fields[1:]
   346         seqName = fields[1]
   347         if seqName not in seqLimits: continue
   348         #strand = fields[2]
   349         cdsBeg = int(fields[5])
   350         cdsEnd = int(fields[6])
   351         exonBegs = commaSeparatedInts(fields[8])
   352         exonEnds = commaSeparatedInts(fields[9])
   353         for beg, end in zip(exonBegs, exonEnds):
   354             yield 300, opts.exon_color, seqName, beg, end
   355             b = max(beg, cdsBeg)
   356             e = min(end, cdsEnd)
   357             if b < e: yield 400, opts.cds_color, seqName, b, e
   358 
   359 def readRmsk(fileName, seqLimits):
   360     if not fileName: return
   361     for line in myOpen(fileName):
   362         fields = line.split()
   363         if len(fields) == 17:  # rmsk.txt
   364             seqName = fields[5]
   365             if seqName not in seqLimits: continue  # do this ASAP for speed
   366             beg = int(fields[6])
   367             end = int(fields[7])
   368             strand = fields[9]
   369             repeatClass = fields[11]
   370         elif len(fields) == 15:  # .out
   371             seqName = fields[4]
   372             if seqName not in seqLimits: continue
   373             beg = int(fields[5]) - 1
   374             end = int(fields[6])
   375             strand = fields[8]
   376             repeatClass = fields[10]
   377         else:
   378             continue
   379         if repeatClass in ("Low_complexity", "Simple_repeat"):
   380             yield 200, "#fbf", seqName, beg, end
   381         elif strand == "+":
   382             yield 100, "#ffe8e8", seqName, beg, end
   383         else:
   384             yield 100, "#e8e8ff", seqName, beg, end
   385 
   386 def isExtraFirstGapField(fields):
   387     return fields[4].isdigit()
   388 
   389 def readGaps(opts, fileName, seqLimits):
   390     '''Read locations of unsequenced gaps, from an agp or gap file.'''
   391     if not fileName: return
   392     for line in myOpen(fileName):
   393         w = line.split()
   394         if not w or w[0][0] == "#": continue
   395         if isExtraFirstGapField(w): w = w[1:]
   396         if w[4] not in "NU": continue
   397         seqName = w[0]
   398         if seqName not in seqLimits: continue
   399         end = int(w[2])
   400         beg = end - int(w[5])  # zero-based coordinate
   401         if w[7] == "yes":
   402             yield 3000, opts.bridged_color, seqName, beg, end
   403         else:
   404             yield 2000, opts.unbridged_color, seqName, beg, end
   405 
   406 def bedBoxes(beds, seqLimits, origins, margin, edge, isTop, bpPerPix):
   407     for layer, color, seqName, bedBeg, bedEnd in beds:
   408         cropBeg, cropEnd = seqLimits[seqName]
   409         beg = max(bedBeg, cropBeg)
   410         end = min(bedEnd, cropEnd)
   411         if beg >= end: continue
   412         ori = origins[seqName]
   413         if layer <= 1000:
   414             # include partly-covered pixels
   415             b = (ori + beg) // bpPerPix
   416             e = div_ceil(ori + end, bpPerPix)
   417         else:
   418             # exclude partly-covered pixels
   419             b = div_ceil(ori + beg, bpPerPix)
   420             e = (ori + end) // bpPerPix
   421             if e <= b: continue
   422         if isTop:
   423             box = b, margin, e, edge
   424         else:
   425             box = margin, b, edge, e
   426         yield layer, color, box
   427 
   428 def drawAnnotations(im, boxes):
   429     # xxx use partial transparency for different-color overlaps?
   430     for layer, color, box in boxes:
   431         im.paste(color, box)
   432 
   433 def make_label(text, text_size, range_start, range_size):
   434     '''Return an axis label with endpoint & sort-order information.'''
   435     text_width, text_height = text_size
   436     label_start = range_start + (range_size - text_width) // 2
   437     label_end   = label_start + text_width
   438     sort_key    = text_width - range_size
   439     return sort_key, label_start, label_end, text, text_height
   440 
   441 def nonoverlappingLabels(labels, minPixTweenLabels):
   442     '''Get a subset of non-overlapping axis labels, greedily.'''
   443     out = []
   444     for i in labels:
   445         beg = i[1] - minPixTweenLabels
   446         end = i[2] + minPixTweenLabels
   447         if all(j[2] <= beg or j[1] >= end for j in out):
   448             out.append(i)
   449     return out
   450 
   451 def axisImage(seqNames, name_sizes, seq_starts, seq_pix, textRot,
   452               textAln, font, image_mode, opts):
   453     '''Make an image of axis labels.'''
   454     beg = seq_starts[0]
   455     end = seq_starts[-1] + seq_pix[-1]
   456     margin = max(i[1] for i in name_sizes)
   457     labels = map(make_label, seqNames, name_sizes, seq_starts, seq_pix)
   458     labels = [i for i in labels if i[1] >= beg and i[2] <= end]
   459     labels.sort()
   460     minPixTweenLabels = 0 if textRot else opts.label_space
   461     labels = nonoverlappingLabels(labels, minPixTweenLabels)
   462     image_size = (margin, end) if textRot else (end, margin)
   463     im = Image.new(image_mode, image_size, opts.margin_color)
   464     draw = ImageDraw.Draw(im)
   465     for i in labels:
   466         base = margin - i[4] if textAln else 0
   467         position = (base, i[1]) if textRot else (i[1], base)
   468         draw.text(position, i[3], font=font, fill=opts.text_color)
   469     return im
   470 
   471 def seqOrigins(seqNames, seq_starts, seqLimits, bp_per_pix):
   472     for i, j in zip(seqNames, seq_starts):
   473         yield i, bp_per_pix * j - seqLimits[i][0]
   474 
   475 def lastDotplot(opts, args):
   476     if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
   477     else:              font = ImageFont.load_default()
   478 
   479     image_mode = 'RGB'
   480     forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
   481     reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
   482     zipped_colors = zip(forward_color, reverse_color)
   483     overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
   484 
   485     warn("reading alignments...")
   486     alignmentInfo = readAlignments(args[0], opts)
   487     alignments, seqNames1, seqNames2, seqLimits1, seqLimits2 = alignmentInfo
   488     warn("done")
   489     if not alignments: raise Exception("there are no alignments")
   490 
   491     textRot1 = "vertical".startswith(opts.rot1)
   492     i1 = getSeqInfo(opts.sort1, seqNames1, seqLimits1,
   493                     font, opts.fontsize, image_mode, opts.labels1, textRot1)
   494     seqNames1, rangeSizes1, seqLabels1, labelSizes1, tMargin = i1
   495 
   496     textRot2 = "horizontal".startswith(opts.rot2)
   497     i2 = getSeqInfo(opts.sort2, seqNames2, seqLimits2,
   498                     font, opts.fontsize, image_mode, opts.labels2, textRot2)
   499     seqNames2, rangeSizes2, seqLabels2, labelSizes2, lMargin = i2
   500 
   501     warn("choosing bp per pixel...")
   502     maxPixels1 = opts.width  - lMargin
   503     maxPixels2 = opts.height - tMargin
   504     bpPerPix1 = get_bp_per_pix(rangeSizes1, opts.border_pixels, maxPixels1)
   505     bpPerPix2 = get_bp_per_pix(rangeSizes2, opts.border_pixels, maxPixels2)
   506     bpPerPix = max(bpPerPix1, bpPerPix2)
   507     warn("bp per pixel = " + str(bpPerPix))
   508 
   509     seq_pix1, seq_starts1, width  = pixelData(rangeSizes1, bpPerPix,
   510                                               opts.border_pixels, lMargin)
   511     seq_pix2, seq_starts2, height = pixelData(rangeSizes2, bpPerPix,
   512                                               opts.border_pixels, tMargin)
   513     warn("width:  " + str(width))
   514     warn("height: " + str(height))
   515 
   516     origins1 = dict(seqOrigins(seqNames1, seq_starts1, seqLimits1, bpPerPix))
   517     origins2 = dict(seqOrigins(seqNames2, seq_starts2, seqLimits2, bpPerPix))
   518 
   519     warn("processing alignments...")
   520     hits = alignmentPixels(width, height, alignments, bpPerPix,
   521                            origins1, origins2)
   522     warn("done")
   523 
   524     image_size = width, height
   525     im = Image.new(image_mode, image_size, opts.background_color)
   526 
   527     seqLimits1 = expandedSeqDict(seqLimits1)
   528     seqLimits2 = expandedSeqDict(seqLimits2)
   529     origins1 = expandedSeqDict(origins1)
   530     origins2 = expandedSeqDict(origins2)
   531 
   532     beds1 = itertools.chain(readBed(opts.bed1, seqLimits1),
   533                             readRmsk(opts.rmsk1, seqLimits1),
   534                             readGenePred(opts, opts.genePred1, seqLimits1),
   535                             readGaps(opts, opts.gap1, seqLimits1))
   536     b1 = bedBoxes(beds1, seqLimits1, origins1, tMargin, height, True, bpPerPix)
   537 
   538     beds2 = itertools.chain(readBed(opts.bed2, seqLimits2),
   539                             readRmsk(opts.rmsk2, seqLimits2),
   540                             readGenePred(opts, opts.genePred2, seqLimits2),
   541                             readGaps(opts, opts.gap2, seqLimits2))
   542     b2 = bedBoxes(beds2, seqLimits2, origins2, lMargin, width, False, bpPerPix)
   543 
   544     boxes = sorted(itertools.chain(b1, b2))
   545     drawAnnotations(im, boxes)
   546 
   547     for i in range(height):
   548         for j in range(width):
   549             store_value = hits[i * width + j]
   550             xy = j, i
   551             if   store_value == 1: im.putpixel(xy, forward_color)
   552             elif store_value == 2: im.putpixel(xy, reverse_color)
   553             elif store_value == 3: im.putpixel(xy, overlap_color)
   554 
   555     if opts.fontsize != 0:
   556         axis1 = axisImage(seqLabels1, labelSizes1, seq_starts1, seq_pix1,
   557                           textRot1, False, font, image_mode, opts)
   558         if textRot1:
   559             axis1 = axis1.transpose(Image.ROTATE_90)
   560         axis2 = axisImage(seqLabels2, labelSizes2, seq_starts2, seq_pix2,
   561                           textRot2, textRot2, font, image_mode, opts)
   562         if not textRot2:
   563             axis2 = axis2.transpose(Image.ROTATE_270)
   564         im.paste(axis1, (0, 0))
   565         im.paste(axis2, (0, 0))
   566 
   567     for i in seq_starts1[1:]:
   568         box = i - opts.border_pixels, tMargin, i, height
   569         im.paste(opts.border_color, box)
   570 
   571     for i in seq_starts2[1:]:
   572         box = lMargin, i - opts.border_pixels, width, i
   573         im.paste(opts.border_color, box)
   574 
   575     im.save(args[1])
   576 
   577 if __name__ == "__main__":
   578     usage = """%prog --help
   579    or: %prog [options] maf-or-tab-alignments dotplot.png
   580    or: %prog [options] maf-or-tab-alignments dotplot.gif
   581    or: ..."""
   582     description = "Draw a dotplot of pair-wise sequence alignments in MAF or tabular format."
   583     op = optparse.OptionParser(usage=usage, description=description)
   584     op.add_option("-v", "--verbose", action="count",
   585                   help="show progress messages & data about the plot")
   586     op.add_option("-1", "--seq1", metavar="PATTERN", action="append",
   587                   default=[],
   588                   help="which sequences to show from the 1st genome")
   589     op.add_option("-2", "--seq2", metavar="PATTERN", action="append",
   590                   default=[],
   591                   help="which sequences to show from the 2nd genome")
   592     # Replace "width" & "height" with a single "length" option?
   593     op.add_option("-x", "--width", type="int", default=1000,
   594                   help="maximum width in pixels (default: %default)")
   595     op.add_option("-y", "--height", type="int", default=1000,
   596                   help="maximum height in pixels (default: %default)")
   597     op.add_option("-c", "--forwardcolor", metavar="COLOR", default="red",
   598                   help="color for forward alignments (default: %default)")
   599     op.add_option("-r", "--reversecolor", metavar="COLOR", default="blue",
   600                   help="color for reverse alignments (default: %default)")
   601     op.add_option("--sort1", type="int", default=1, metavar="N",
   602                   help="genome1 sequence order: 0=input order, 1=name order, "
   603                   "2=length order (default=%default)")
   604     op.add_option("--sort2", type="int", default=1, metavar="N",
   605                   help="genome2 sequence order: 0=input order, 1=name order, "
   606                   "2=length order (default=%default)")
   607     op.add_option("--trim1", action="store_true",
   608                   help="trim unaligned sequence flanks from the 1st genome")
   609     op.add_option("--trim2", action="store_true",
   610                   help="trim unaligned sequence flanks from the 2nd genome")
   611     op.add_option("--border-pixels", metavar="INT", type="int", default=1,
   612                   help="number of pixels between sequences (default=%default)")
   613     op.add_option("--border-color", metavar="COLOR", default="black",
   614                   help="color for pixels between sequences (default=%default)")
   615     op.add_option("--margin-color", metavar="COLOR", default="#dcdcdc",
   616                   help="margin color")
   617 
   618     og = optparse.OptionGroup(op, "Text options")
   619     og.add_option("-f", "--fontfile", metavar="FILE",
   620                   help="TrueType or OpenType font file")
   621     og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
   622                   help="TrueType or OpenType font size (default: %default)")
   623     og.add_option("--labels1", type="int", default=0, metavar="N", help=
   624                   "genome1 labels: 0=name, 1=name:length, "
   625                   "2=name:start:length, 3=name:start-end (default=%default)")
   626     og.add_option("--labels2", type="int", default=0, metavar="N", help=
   627                   "genome2 labels: 0=name, 1=name:length, "
   628                   "2=name:start:length, 3=name:start-end (default=%default)")
   629     og.add_option("--rot1", metavar="ROT", default="h",
   630                   help="text rotation for the 1st genome (default=%default)")
   631     og.add_option("--rot2", metavar="ROT", default="v",
   632                   help="text rotation for the 2nd genome (default=%default)")
   633     op.add_option_group(og)
   634 
   635     og = optparse.OptionGroup(op, "Annotation options")
   636     og.add_option("--bed1", metavar="FILE",
   637                   help="read genome1 annotations from BED file")
   638     og.add_option("--bed2", metavar="FILE",
   639                   help="read genome2 annotations from BED file")
   640     og.add_option("--rmsk1", metavar="FILE", help="read genome1 repeats from "
   641                   "RepeatMasker .out or rmsk.txt file")
   642     og.add_option("--rmsk2", metavar="FILE", help="read genome2 repeats from "
   643                   "RepeatMasker .out or rmsk.txt file")
   644     op.add_option_group(og)
   645 
   646     og = optparse.OptionGroup(op, "Gene options")
   647     og.add_option("--genePred1", metavar="FILE",
   648                   help="read genome1 genes from genePred file")
   649     og.add_option("--genePred2", metavar="FILE",
   650                   help="read genome2 genes from genePred file")
   651     og.add_option("--exon-color", metavar="COLOR", default="PaleGreen",
   652                   help="color for exons (default=%default)")
   653     og.add_option("--cds-color", metavar="COLOR", default="LimeGreen",
   654                   help="color for protein-coding regions (default=%default)")
   655     op.add_option_group(og)
   656 
   657     og = optparse.OptionGroup(op, "Unsequenced gap options")
   658     og.add_option("--gap1", metavar="FILE",
   659                   help="read genome1 unsequenced gaps from agp or gap file")
   660     og.add_option("--gap2", metavar="FILE",
   661                   help="read genome2 unsequenced gaps from agp or gap file")
   662     og.add_option("--bridged-color", metavar="COLOR", default="yellow",
   663                   help="color for bridged gaps (default: %default)")
   664     og.add_option("--unbridged-color", metavar="COLOR", default="orange",
   665                   help="color for unbridged gaps (default: %default)")
   666     op.add_option_group(og)
   667     (opts, args) = op.parse_args()
   668     if len(args) != 2: op.error("2 arguments needed")
   669 
   670     opts.text_color = "black"
   671     opts.background_color = "white"
   672     opts.label_space = 5     # minimum number of pixels between axis labels
   673 
   674     try: lastDotplot(opts, args)
   675     except KeyboardInterrupt: pass  # avoid silly error message
   676     except Exception, e:
   677         prog = os.path.basename(sys.argv[0])
   678         sys.exit(prog + ": error: " + str(e))