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