scripts/last-dotplot
author Martin C. Frith
Tue Dec 05 17:57:24 2017 +0900 (2017-12-05)
changeset 914 f4ca19d126e3
parent 913 e87abc7ae9c6
child 915 6079fabb49a5
permissions -rwxr-xr-x
last-dotplot: add alignment-order options
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@914
    12
import functools
Martin@875
    13
import gzip
Martin@896
    14
from fnmatch import fnmatchcase
Martin@906
    15
from operator import itemgetter
Martin@903
    16
import subprocess
Martin@896
    17
import itertools, optparse, os, re, sys
Martin@475
    18
Martin@475
    19
# Try to make PIL/PILLOW work:
Martin@475
    20
try: from PIL import Image, ImageDraw, ImageFont, ImageColor
Martin@475
    21
except ImportError: import Image, ImageDraw, ImageFont, ImageColor
Martin@1
    22
Martin@844
    23
def myOpen(fileName):  # faster than fileinput
Martin@904
    24
    if fileName is None:
Martin@904
    25
        return []
Martin@844
    26
    if fileName == "-":
Martin@844
    27
        return sys.stdin
Martin@875
    28
    if fileName.endswith(".gz"):
Martin@875
    29
        return gzip.open(fileName)
Martin@844
    30
    return open(fileName)
Martin@844
    31
Martin@644
    32
def warn(message):
Martin@866
    33
    if opts.verbose:
Martin@866
    34
        prog = os.path.basename(sys.argv[0])
Martin@866
    35
        sys.stderr.write(prog + ": " + message + "\n")
Martin@644
    36
Martin@911
    37
def groupByFirstItem(things):
Martin@911
    38
    for k, v in itertools.groupby(things, itemgetter(0)):
Martin@911
    39
        yield k, [i[1:] for i in v]
Martin@911
    40
Martin@908
    41
def croppedBlocks(blocks, ranges1, ranges2):
Martin@908
    42
    headBeg1, headBeg2, headSize = blocks[0]
Martin@908
    43
    for r1 in ranges1:
Martin@908
    44
        for r2 in ranges2:
Martin@908
    45
            cropBeg1, cropEnd1 = r1
Martin@908
    46
            if headBeg1 < 0:
Martin@908
    47
                cropBeg1, cropEnd1 = -cropEnd1, -cropBeg1
Martin@908
    48
            cropBeg2, cropEnd2 = r2
Martin@908
    49
            if headBeg2 < 0:
Martin@908
    50
                cropBeg2, cropEnd2 = -cropEnd2, -cropBeg2
Martin@908
    51
            for beg1, beg2, size in blocks:
Martin@908
    52
                b1 = max(cropBeg1, beg1)
Martin@908
    53
                e1 = min(cropEnd1, beg1 + size)
Martin@908
    54
                if b1 >= e1: continue
Martin@908
    55
                offset = beg2 - beg1
Martin@908
    56
                b2 = max(cropBeg2, b1 + offset)
Martin@908
    57
                e2 = min(cropEnd2, e1 + offset)
Martin@908
    58
                if b2 >= e2: continue
Martin@908
    59
                yield b2 - offset, b2, e2 - b2
Martin@840
    60
Martin@482
    61
def tabBlocks(beg1, beg2, blocks):
Martin@482
    62
    '''Get the gapless blocks of an alignment, from LAST tabular format.'''
Martin@482
    63
    for i in blocks.split(","):
Martin@482
    64
        if ":" in i:
Martin@482
    65
            x, y = i.split(":")
Martin@482
    66
            beg1 += int(x)
Martin@482
    67
            beg2 += int(y)
Martin@482
    68
        else:
Martin@482
    69
            size = int(i)
Martin@482
    70
            yield beg1, beg2, size
Martin@482
    71
            beg1 += size
Martin@482
    72
            beg2 += size
Martin@272
    73
Martin@482
    74
def mafBlocks(beg1, beg2, seq1, seq2):
Martin@482
    75
    '''Get the gapless blocks of an alignment, from MAF format.'''
Martin@482
    76
    size = 0
Martin@482
    77
    for x, y in itertools.izip(seq1, seq2):
Martin@482
    78
        if x == "-":
Martin@482
    79
            if size:
Martin@482
    80
                yield beg1, beg2, size
Martin@482
    81
                beg1 += size
Martin@482
    82
                beg2 += size
Martin@482
    83
                size = 0
Martin@482
    84
            beg2 += 1
Martin@482
    85
        elif y == "-":
Martin@482
    86
            if size:
Martin@482
    87
                yield beg1, beg2, size
Martin@482
    88
                beg1 += size
Martin@482
    89
                beg2 += size
Martin@482
    90
                size = 0
Martin@482
    91
            beg1 += 1
Martin@272
    92
        else:
Martin@482
    93
            size += 1
Martin@482
    94
    if size: yield beg1, beg2, size
Martin@272
    95
Martin@482
    96
def alignmentInput(lines):
Martin@482
    97
    '''Get alignments and sequence lengths, from MAF or tabular format.'''
Martin@482
    98
    mafCount = 0
Martin@272
    99
    for line in lines:
Martin@272
   100
        w = line.split()
Martin@272
   101
        if line[0].isdigit():  # tabular format
Martin@482
   102
            chr1, beg1, seqlen1 = w[1], int(w[2]), int(w[5])
Martin@482
   103
            if w[4] == "-": beg1 -= seqlen1
Martin@482
   104
            chr2, beg2, seqlen2 = w[6], int(w[7]), int(w[10])
Martin@482
   105
            if w[9] == "-": beg2 -= seqlen2
Martin@847
   106
            blocks = tabBlocks(beg1, beg2, w[11])
Martin@482
   107
            yield chr1, seqlen1, chr2, seqlen2, blocks
Martin@272
   108
        elif line[0] == "s":  # MAF format
Martin@482
   109
            if mafCount == 0:
Martin@482
   110
                chr1, beg1, seqlen1, seq1 = w[1], int(w[2]), int(w[5]), w[6]
Martin@482
   111
                if w[4] == "-": beg1 -= seqlen1
Martin@482
   112
                mafCount = 1
Martin@482
   113
            else:
Martin@482
   114
                chr2, beg2, seqlen2, seq2 = w[1], int(w[2]), int(w[5]), w[6]
Martin@482
   115
                if w[4] == "-": beg2 -= seqlen2
Martin@847
   116
                blocks = mafBlocks(beg1, beg2, seq1, seq2)
Martin@482
   117
                yield chr1, seqlen1, chr2, seqlen2, blocks
Martin@482
   118
                mafCount = 0
Martin@272
   119
Martin@897
   120
def seqRequestFromText(text):
Martin@840
   121
    if ":" in text:
Martin@840
   122
        pattern, interval = text.rsplit(":", 1)
Martin@840
   123
        if "-" in interval:
Martin@840
   124
            beg, end = interval.rsplit("-", 1)
Martin@840
   125
            return pattern, int(beg), int(end)  # beg may be negative
Martin@840
   126
    return text, 0, sys.maxsize
Martin@840
   127
Martin@909
   128
def rangesFromSeqName(seqRequests, name, seqLen):
Martin@908
   129
    if seqRequests:
Martin@908
   130
        base = name.split(".")[-1]  # allow for names like hg19.chr7
Martin@908
   131
        for pat, beg, end in seqRequests:
Martin@908
   132
            if fnmatchcase(name, pat) or fnmatchcase(base, pat):
Martin@909
   133
                yield max(beg, 0), min(end, seqLen)
Martin@908
   134
    else:
Martin@909
   135
        yield 0, seqLen
Martin@651
   136
Martin@909
   137
def updateSeqs(coverDict, seqRanges, seqName, ranges, coveredRange):
Martin@909
   138
    beg, end = coveredRange
Martin@909
   139
    if beg < 0:
Martin@909
   140
        coveredRange = -end, -beg
Martin@909
   141
    if seqName in coverDict:
Martin@909
   142
        coverDict[seqName].append(coveredRange)
Martin@839
   143
    else:
Martin@909
   144
        coverDict[seqName] = [coveredRange]
Martin@909
   145
        for beg, end in ranges:
Martin@909
   146
            r = seqName, beg, end
Martin@909
   147
            seqRanges.append(r)
Martin@839
   148
Martin@651
   149
def readAlignments(fileName, opts):
Martin@839
   150
    '''Get alignments and sequence limits, from MAF or tabular format.'''
Martin@897
   151
    seqRequests1 = map(seqRequestFromText, opts.seq1)
Martin@897
   152
    seqRequests2 = map(seqRequestFromText, opts.seq2)
Martin@840
   153
Martin@482
   154
    alignments = []
Martin@909
   155
    seqRanges1 = []
Martin@909
   156
    seqRanges2 = []
Martin@909
   157
    coverDict1 = {}
Martin@909
   158
    coverDict2 = {}
Martin@844
   159
    lines = myOpen(fileName)
Martin@838
   160
    for seqName1, seqLen1, seqName2, seqLen2, blocks in alignmentInput(lines):
Martin@909
   161
        ranges1 = sorted(rangesFromSeqName(seqRequests1, seqName1, seqLen1))
Martin@909
   162
        if not ranges1: continue
Martin@909
   163
        ranges2 = sorted(rangesFromSeqName(seqRequests2, seqName2, seqLen2))
Martin@909
   164
        if not ranges2: continue
Martin@909
   165
        b = list(croppedBlocks(list(blocks), ranges1, ranges2))
Martin@840
   166
        if not b: continue
Martin@840
   167
        aln = seqName1, seqName2, b
Martin@482
   168
        alignments.append(aln)
Martin@909
   169
        coveredRange1 = b[0][0], b[-1][0] + b[-1][2]
Martin@909
   170
        updateSeqs(coverDict1, seqRanges1, seqName1, ranges1, coveredRange1)
Martin@909
   171
        coveredRange2 = b[0][1], b[-1][1] + b[-1][2]
Martin@909
   172
        updateSeqs(coverDict2, seqRanges2, seqName2, ranges2, coveredRange2)
Martin@909
   173
    return alignments, seqRanges1, coverDict1, seqRanges2, coverDict2
Martin@1
   174
Martin@911
   175
def nameAndRangesFromDict(cropDict, seqName):
Martin@911
   176
    if seqName in cropDict:
Martin@911
   177
        return seqName, cropDict[seqName]
Martin@911
   178
    n = seqName.split(".")[-1]
Martin@911
   179
    if n in cropDict:
Martin@911
   180
        return n, cropDict[n]
Martin@911
   181
    return seqName, []
Martin@911
   182
Martin@911
   183
def rangesForSecondaryAlignments(primaryRanges, seqLen):
Martin@911
   184
    if primaryRanges:
Martin@911
   185
        return primaryRanges
Martin@911
   186
    return [(0, seqLen)]
Martin@911
   187
Martin@911
   188
def readSecondaryAlignments(opts, cropRanges1, cropRanges2):
Martin@911
   189
    cropDict1 = dict(groupByFirstItem(cropRanges1))
Martin@911
   190
    cropDict2 = dict(groupByFirstItem(cropRanges2))
Martin@911
   191
Martin@911
   192
    alignments = []
Martin@911
   193
    seqRanges1 = []
Martin@911
   194
    seqRanges2 = []
Martin@911
   195
    coverDict1 = {}
Martin@911
   196
    coverDict2 = {}
Martin@911
   197
    lines = myOpen(opts.alignments)
Martin@911
   198
    for seqName1, seqLen1, seqName2, seqLen2, blocks in alignmentInput(lines):
Martin@911
   199
        seqName1, ranges1 = nameAndRangesFromDict(cropDict1, seqName1)
Martin@911
   200
        seqName2, ranges2 = nameAndRangesFromDict(cropDict2, seqName2)
Martin@911
   201
        if not ranges1 and not ranges2:
Martin@911
   202
            continue
Martin@911
   203
        r1 = rangesForSecondaryAlignments(ranges1, seqLen1)
Martin@911
   204
        r2 = rangesForSecondaryAlignments(ranges2, seqLen2)
Martin@911
   205
        b = list(croppedBlocks(list(blocks), r1, r2))
Martin@911
   206
        if not b: continue
Martin@911
   207
        aln = seqName1, seqName2, b
Martin@911
   208
        alignments.append(aln)
Martin@911
   209
        if not ranges1:
Martin@911
   210
            coveredRange1 = b[0][0], b[-1][0] + b[-1][2]
Martin@911
   211
            updateSeqs(coverDict1, seqRanges1, seqName1, r1, coveredRange1)
Martin@911
   212
        if not ranges2:
Martin@911
   213
            coveredRange2 = b[0][1], b[-1][1] + b[-1][2]
Martin@911
   214
            updateSeqs(coverDict2, seqRanges2, seqName2, r2, coveredRange2)
Martin@911
   215
    return alignments, seqRanges1, coverDict1, seqRanges2, coverDict2
Martin@911
   216
Martin@910
   217
def twoValuesFromOption(text, separator):
Martin@910
   218
    if separator in text:
Martin@910
   219
        return text.split(separator)
Martin@910
   220
    return text, text
Martin@910
   221
Martin@910
   222
def mergedRanges(ranges):
Martin@910
   223
    oldBeg, maxEnd = ranges[0]
Martin@910
   224
    for beg, end in ranges:
Martin@910
   225
        if beg > maxEnd:
Martin@910
   226
            yield oldBeg, maxEnd
Martin@910
   227
            oldBeg = beg
Martin@910
   228
            maxEnd = end
Martin@910
   229
        elif end > maxEnd:
Martin@910
   230
            maxEnd = end
Martin@910
   231
    yield oldBeg, maxEnd
Martin@910
   232
Martin@910
   233
def mergedRangesPerSeq(coverDict):
Martin@910
   234
    for k, v in coverDict.iteritems():
Martin@910
   235
        v.sort()
Martin@910
   236
        yield k, list(mergedRanges(v))
Martin@910
   237
Martin@910
   238
def coveredLength(mergedCoverDict):
Martin@910
   239
    return sum(sum(e - b for b, e in v) for v in mergedCoverDict.itervalues())
Martin@910
   240
Martin@910
   241
def trimmed(seqRanges, coverDict, minAlignedBases, maxGapFrac, endPad, midPad):
Martin@910
   242
    maxEndGapFrac, maxMidGapFrac = twoValuesFromOption(maxGapFrac, ",")
Martin@910
   243
    maxEndGap = max(float(maxEndGapFrac) * minAlignedBases, endPad * 1.0)
Martin@910
   244
    maxMidGap = max(float(maxMidGapFrac) * minAlignedBases, midPad * 2.0)
Martin@910
   245
Martin@910
   246
    for seqName, rangeBeg, rangeEnd in seqRanges:
Martin@910
   247
        seqBlocks = coverDict[seqName]
Martin@910
   248
        blocks = [i for i in seqBlocks if i[0] < rangeEnd and i[1] > rangeBeg]
Martin@910
   249
        if blocks[0][0] - rangeBeg > maxEndGap:
Martin@910
   250
            rangeBeg = blocks[0][0] - endPad
Martin@910
   251
        for j, y in enumerate(blocks):
Martin@910
   252
            if j:
Martin@910
   253
                x = blocks[j - 1]
Martin@910
   254
                if y[0] - x[1] > maxMidGap:
Martin@910
   255
                    yield seqName, rangeBeg, x[1] + midPad
Martin@910
   256
                    rangeBeg = y[0] - midPad
Martin@910
   257
        if rangeEnd - blocks[-1][1] > maxEndGap:
Martin@910
   258
            rangeEnd = blocks[-1][1] + endPad
Martin@910
   259
        yield seqName, rangeBeg, rangeEnd
Martin@910
   260
Martin@1
   261
def natural_sort_key(my_string):
Martin@1
   262
    '''Return a sort key for "natural" ordering, e.g. chr9 < chr10.'''
Martin@1
   263
    parts = re.split(r'(\d+)', my_string)
Martin@1
   264
    parts[1::2] = map(int, parts[1::2])
Martin@1
   265
    return parts
Martin@1
   266
Martin@907
   267
def nameKey(oneSeqRanges):
Martin@907
   268
    return natural_sort_key(oneSeqRanges[0][0])
Martin@907
   269
Martin@907
   270
def sizeKey(oneSeqRanges):
Martin@907
   271
    return sum(b - e for n, b, e in oneSeqRanges), nameKey(oneSeqRanges)
Martin@907
   272
Martin@914
   273
def alignmentKey(seqNamesToLists, oneSeqRanges):
Martin@914
   274
    seqName = oneSeqRanges[0][0]
Martin@914
   275
    alignmentsOfThisSequence = seqNamesToLists[seqName]
Martin@914
   276
    numOfAlignedLetterPairs = sum(i[3] for i in alignmentsOfThisSequence)
Martin@914
   277
    toMiddle = numOfAlignedLetterPairs // 2
Martin@914
   278
    for i in alignmentsOfThisSequence:
Martin@914
   279
        toMiddle -= i[3]
Martin@914
   280
        if toMiddle < 0:
Martin@914
   281
            return i[1:3]  # sequence-rank and "position" of this alignment
Martin@914
   282
Martin@914
   283
def alignmentSortData1(alignments, seqNamesToRanks2):
Martin@914
   284
    for seqName1, seqName2, blocks in alignments:
Martin@914
   285
        seqRank2 = seqNamesToRanks2[seqName2]
Martin@914
   286
        pos2 = abs(blocks[0][1] + blocks[-1][1] + blocks[-1][2])
Martin@914
   287
        numOfAlignedLetterPairs = sum(i[2] for i in blocks)
Martin@914
   288
        yield seqName1, seqRank2, pos2, numOfAlignedLetterPairs
Martin@914
   289
Martin@914
   290
def alignmentSortData2(alignments, seqNamesToRanks1):
Martin@914
   291
    for seqName1, seqName2, blocks in alignments:
Martin@914
   292
        seqRank1 = seqNamesToRanks1[seqName1]
Martin@914
   293
        pos1 = abs(blocks[0][0] + blocks[-1][0] + blocks[-1][2])
Martin@914
   294
        numOfAlignedLetterPairs = sum(i[2] for i in blocks)
Martin@914
   295
        yield seqName2, seqRank1, pos1, numOfAlignedLetterPairs
Martin@914
   296
Martin@914
   297
def mySortedRanges(seqRanges, sortOpt,
Martin@914
   298
                   alignmentSortDataFunc, alignments, otherRanges):
Martin@913
   299
    rangesGroupedBySeqName = itertools.groupby(seqRanges, itemgetter(0))
Martin@913
   300
    g = [list(ranges) for seqName, ranges in rangesGroupedBySeqName]
Martin@907
   301
    if sortOpt == "1":
Martin@907
   302
        g.sort(key=nameKey)
Martin@907
   303
    if sortOpt == "2":
Martin@907
   304
        g.sort(key=sizeKey)
Martin@914
   305
    if sortOpt == "3":
Martin@914
   306
        otherNameGroups = itertools.groupby(i[0] for i in otherRanges)
Martin@914
   307
        ranksAndNames = enumerate(i[0] for i in otherNameGroups)
Martin@914
   308
        otherNamesToRanks = dict((n, r) for r, n in ranksAndNames)
Martin@914
   309
        alns = sorted(alignmentSortDataFunc(alignments, otherNamesToRanks))
Martin@914
   310
        alnsGroupedBySeqName = itertools.groupby(alns, itemgetter(0))
Martin@914
   311
        seqNamesToLists = dict((k, list(v)) for k, v in alnsGroupedBySeqName)
Martin@914
   312
        g.sort(key=functools.partial(alignmentKey, seqNamesToLists))
Martin@907
   313
    return [j for i in g for j in i]
Martin@907
   314
Martin@914
   315
def allSortedRanges(opts, alignments, alignmentsB,
Martin@914
   316
                    seqRanges1, seqRangesB1, seqRanges2, seqRangesB2):
Martin@914
   317
    o1, oB1 = twoValuesFromOption(opts.sort1, ":")
Martin@914
   318
    o2, oB2 = twoValuesFromOption(opts.sort2, ":")
Martin@914
   319
    if o1 == "3" and o2 == "3":
Martin@914
   320
        raise Exception("the sort options have circular dependency")
Martin@914
   321
    if o1 != "3":
Martin@914
   322
        s1 = mySortedRanges(seqRanges1, o1, None, None, None)
Martin@914
   323
    if o2 != "3":
Martin@914
   324
        s2 = mySortedRanges(seqRanges2, o2, None, None, None)
Martin@914
   325
    if o1 == "3":
Martin@914
   326
        s1 = mySortedRanges(seqRanges1, o1, alignmentSortData1, alignments, s2)
Martin@914
   327
    if o2 == "3":
Martin@914
   328
        s2 = mySortedRanges(seqRanges2, o2, alignmentSortData2, alignments, s1)
Martin@914
   329
    t1 = mySortedRanges(seqRangesB1, oB1, alignmentSortData1, alignmentsB, s2)
Martin@914
   330
    t2 = mySortedRanges(seqRangesB2, oB2, alignmentSortData2, alignmentsB, s1)
Martin@914
   331
    return s1 + t1, s2 + t2
Martin@911
   332
Martin@898
   333
def prettyNum(n):
Martin@898
   334
    t = str(n)
Martin@898
   335
    groups = []
Martin@898
   336
    while t:
Martin@898
   337
        groups.append(t[-3:])
Martin@898
   338
        t = t[:-3]
Martin@904
   339
    return ",".join(reversed(groups))
Martin@898
   340
Martin@846
   341
def sizeText(size):
Martin@846
   342
    suffixes = "bp", "kb", "Mb", "Gb"
Martin@846
   343
    for i, x in enumerate(suffixes):
Martin@846
   344
        j = 10 ** (i * 3)
Martin@846
   345
        if size < j * 10:
Martin@846
   346
            return "%.2g" % (1.0 * size / j) + x
Martin@846
   347
        if size < j * 1000 or i == len(suffixes) - 1:
Martin@846
   348
            return "%.0f" % (1.0 * size / j) + x
Martin@846
   349
Martin@898
   350
def labelText(seqRange, labelOpt):
Martin@898
   351
    seqName, beg, end = seqRange
Martin@898
   352
    if labelOpt == 1:
Martin@898
   353
        return seqName + ": " + sizeText(end - beg)
Martin@898
   354
    if labelOpt == 2:
Martin@904
   355
        return seqName + ":" + prettyNum(beg) + ": " + sizeText(end - beg)
Martin@898
   356
    if labelOpt == 3:
Martin@904
   357
        return seqName + ":" + prettyNum(beg) + "-" + prettyNum(end)
Martin@898
   358
    return seqName
Martin@846
   359
Martin@899
   360
def rangeLabels(seqRanges, labelOpt, font, fontsize, image_mode, textRot):
Martin@899
   361
    if fontsize:
Martin@899
   362
        image_size = 1, 1
Martin@899
   363
        im = Image.new(image_mode, image_size)
Martin@899
   364
        draw = ImageDraw.Draw(im)
Martin@899
   365
    x = y = 0
Martin@899
   366
    for r in seqRanges:
Martin@899
   367
        text = labelText(r, labelOpt)
Martin@899
   368
        if fontsize:
Martin@899
   369
            x, y = draw.textsize(text, font=font)
Martin@899
   370
            if textRot:
Martin@899
   371
                x, y = y, x
Martin@899
   372
        yield text, x, y
Martin@899
   373
Martin@914
   374
def dataFromRanges(sortedRanges, font, fontSize, imageMode, labelOpt, textRot):
Martin@913
   375
    for i in sortedRanges:
Martin@907
   376
        warn("\t".join(map(str, i)))
Martin@866
   377
    warn("")
Martin@913
   378
    rangeSizes = [e - b for n, b, e in sortedRanges]
Martin@913
   379
    labs = list(rangeLabels(sortedRanges, labelOpt, font, fontSize,
Martin@913
   380
                            imageMode, textRot))
Martin@907
   381
    margin = max(i[2] for i in labs)
Martin@878
   382
    # xxx the margin may be too big, because some labels may get omitted
Martin@914
   383
    return rangeSizes, labs, margin
Martin@1
   384
Martin@1
   385
def div_ceil(x, y):
Martin@1
   386
    '''Return x / y rounded up.'''
Martin@1
   387
    q, r = divmod(x, y)
Martin@1
   388
    return q + (r != 0)
Martin@1
   389
Martin@896
   390
def get_bp_per_pix(rangeSizes, pixTweenRanges, maxPixels):
Martin@1
   391
    '''Get the minimum bp-per-pixel that fits in the size limit.'''
Martin@904
   392
    warn("choosing bp per pixel...")
Martin@896
   393
    numOfRanges = len(rangeSizes)
Martin@896
   394
    maxPixelsInRanges = maxPixels - pixTweenRanges * (numOfRanges - 1)
Martin@896
   395
    if maxPixelsInRanges < numOfRanges:
Martin@649
   396
        raise Exception("can't fit the image: too many sequences?")
Martin@896
   397
    negLimit = -maxPixelsInRanges
Martin@896
   398
    negBpPerPix = sum(rangeSizes) // negLimit
Martin@863
   399
    while True:
Martin@896
   400
        if sum(i // negBpPerPix for i in rangeSizes) >= negLimit:
Martin@863
   401
            return -negBpPerPix
Martin@863
   402
        negBpPerPix -= 1
Martin@1
   403
Martin@900
   404
def getRangePixBegs(rangePixLens, pixTweenRanges, margin):
Martin@900
   405
    '''Get the start pixel for each range.'''
Martin@900
   406
    rangePixBegs = []
Martin@896
   407
    pix_tot = margin - pixTweenRanges
Martin@900
   408
    for i in rangePixLens:
Martin@896
   409
        pix_tot += pixTweenRanges
Martin@900
   410
        rangePixBegs.append(pix_tot)
Martin@1
   411
        pix_tot += i
Martin@900
   412
    return rangePixBegs
Martin@1
   413
Martin@896
   414
def pixelData(rangeSizes, bp_per_pix, pixTweenRanges, margin):
Martin@900
   415
    '''Return pixel information about the ranges.'''
Martin@900
   416
    rangePixLens = [div_ceil(i, bp_per_pix) for i in rangeSizes]
Martin@900
   417
    rangePixBegs = getRangePixBegs(rangePixLens, pixTweenRanges, margin)
Martin@900
   418
    tot_pix = rangePixBegs[-1] + rangePixLens[-1]
Martin@900
   419
    return rangePixBegs, rangePixLens, tot_pix
Martin@1
   420
Martin@835
   421
def drawLineForward(hits, width, bp_per_pix, beg1, beg2, size):
Martin@639
   422
    while True:
Martin@639
   423
        q1, r1 = divmod(beg1, bp_per_pix)
Martin@639
   424
        q2, r2 = divmod(beg2, bp_per_pix)
Martin@835
   425
        hits[q2 * width + q1] |= 1
Martin@639
   426
        next_pix = min(bp_per_pix - r1, bp_per_pix - r2)
Martin@639
   427
        if next_pix >= size: break
Martin@639
   428
        beg1 += next_pix
Martin@639
   429
        beg2 += next_pix
Martin@639
   430
        size -= next_pix
Martin@639
   431
Martin@835
   432
def drawLineReverse(hits, width, bp_per_pix, beg1, beg2, size):
Martin@639
   433
    beg2 = -1 - beg2
Martin@639
   434
    while True:
Martin@639
   435
        q1, r1 = divmod(beg1, bp_per_pix)
Martin@639
   436
        q2, r2 = divmod(beg2, bp_per_pix)
Martin@835
   437
        hits[q2 * width + q1] |= 2
Martin@639
   438
        next_pix = min(bp_per_pix - r1, r2 + 1)
Martin@639
   439
        if next_pix >= size: break
Martin@639
   440
        beg1 += next_pix
Martin@639
   441
        beg2 -= next_pix
Martin@639
   442
        size -= next_pix
Martin@639
   443
Martin@905
   444
def findOrigin(ranges, beg, size):
Martin@905
   445
    if beg < 0:
Martin@905
   446
        beg = -(beg + size)
Martin@905
   447
    for rangeBeg, rangeEnd, origin in ranges:
Martin@905
   448
        if rangeEnd > beg:
Martin@905
   449
            return origin
Martin@905
   450
Martin@905
   451
def alignmentPixels(width, height, alignments, bp_per_pix,
Martin@905
   452
                    rangeDict1, rangeDict2):
Martin@640
   453
    hits = [0] * (width * height)  # the image data
Martin@640
   454
    for seq1, seq2, blocks in alignments:
Martin@905
   455
        beg1, beg2, size = blocks[0]
Martin@905
   456
        ori1 = findOrigin(rangeDict1[seq1], beg1, size)
Martin@905
   457
        ori2 = findOrigin(rangeDict2[seq2], beg2, size)
Martin@640
   458
        for beg1, beg2, size in blocks:
Martin@640
   459
            if beg1 < 0:
Martin@640
   460
                beg1 = -(beg1 + size)
Martin@640
   461
                beg2 = -(beg2 + size)
Martin@640
   462
            if beg2 >= 0:
Martin@835
   463
                drawLineForward(hits, width, bp_per_pix,
Martin@835
   464
                                beg1 + ori1, beg2 + ori2, size)
Martin@640
   465
            else:
Martin@835
   466
                drawLineReverse(hits, width, bp_per_pix,
Martin@835
   467
                                beg1 + ori1, beg2 - ori2, size)
Martin@640
   468
    return hits
Martin@1
   469
Martin@650
   470
def expandedSeqDict(seqDict):
Martin@650
   471
    '''Allow lookup by short sequence names, e.g. chr7 as well as hg19.chr7.'''
Martin@861
   472
    newDict = seqDict.copy()
Martin@650
   473
    for name, x in seqDict.items():
Martin@861
   474
        if "." in name:
Martin@861
   475
            base = name.split(".")[-1]
Martin@861
   476
            if base in newDict:  # an ambiguous case was found:
Martin@861
   477
                return seqDict   # so give up completely
Martin@861
   478
            newDict[base] = x
Martin@650
   479
    return newDict
Martin@650
   480
Martin@905
   481
def readBed(fileName, rangeDict):
Martin@845
   482
    for line in myOpen(fileName):
Martin@845
   483
        w = line.split()
Martin@847
   484
        if not w: continue
Martin@845
   485
        seqName = w[0]
Martin@905
   486
        if seqName not in rangeDict: continue
Martin@845
   487
        beg = int(w[1])
Martin@845
   488
        end = int(w[2])
Martin@857
   489
        layer = 900
Martin@895
   490
        color = "#fbf"
Martin@858
   491
        if len(w) > 4:
Martin@858
   492
            if w[4] != ".":
Martin@858
   493
                layer = float(w[4])
Martin@858
   494
            if len(w) > 5:
Martin@858
   495
                if len(w) > 8 and w[8].count(",") == 2:
Martin@858
   496
                    color = "rgb(" + w[8] + ")"
Martin@858
   497
                elif w[5] == "+":
Martin@895
   498
                    color = "#ffe8e8"
Martin@858
   499
                elif w[5] == "-":
Martin@895
   500
                    color = "#e8e8ff"
Martin@859
   501
        yield layer, color, seqName, beg, end
Martin@845
   502
Martin@860
   503
def commaSeparatedInts(text):
Martin@860
   504
    return map(int, text.rstrip(",").split(","))
Martin@860
   505
Martin@905
   506
def readGenePred(opts, fileName, rangeDict):
Martin@860
   507
    for line in myOpen(fileName):
Martin@860
   508
        fields = line.split()
Martin@860
   509
        if not fields: continue
Martin@860
   510
        if fields[2] not in "+-": fields = fields[1:]
Martin@860
   511
        seqName = fields[1]
Martin@905
   512
        if seqName not in rangeDict: continue
Martin@860
   513
        #strand = fields[2]
Martin@860
   514
        cdsBeg = int(fields[5])
Martin@860
   515
        cdsEnd = int(fields[6])
Martin@860
   516
        exonBegs = commaSeparatedInts(fields[8])
Martin@860
   517
        exonEnds = commaSeparatedInts(fields[9])
Martin@860
   518
        for beg, end in zip(exonBegs, exonEnds):
Martin@860
   519
            yield 300, opts.exon_color, seqName, beg, end
Martin@860
   520
            b = max(beg, cdsBeg)
Martin@860
   521
            e = min(end, cdsEnd)
Martin@860
   522
            if b < e: yield 400, opts.cds_color, seqName, b, e
Martin@860
   523
Martin@905
   524
def readRmsk(fileName, rangeDict):
Martin@860
   525
    for line in myOpen(fileName):
Martin@860
   526
        fields = line.split()
Martin@860
   527
        if len(fields) == 17:  # rmsk.txt
Martin@860
   528
            seqName = fields[5]
Martin@905
   529
            if seqName not in rangeDict: continue  # do this ASAP for speed
Martin@860
   530
            beg = int(fields[6])
Martin@860
   531
            end = int(fields[7])
Martin@860
   532
            strand = fields[9]
Martin@860
   533
            repeatClass = fields[11]
Martin@860
   534
        elif len(fields) == 15:  # .out
Martin@860
   535
            seqName = fields[4]
Martin@905
   536
            if seqName not in rangeDict: continue
Martin@860
   537
            beg = int(fields[5]) - 1
Martin@860
   538
            end = int(fields[6])
Martin@860
   539
            strand = fields[8]
Martin@860
   540
            repeatClass = fields[10]
Martin@860
   541
        else:
Martin@860
   542
            continue
Martin@860
   543
        if repeatClass in ("Low_complexity", "Simple_repeat"):
Martin@895
   544
            yield 200, "#fbf", seqName, beg, end
Martin@860
   545
        elif strand == "+":
Martin@895
   546
            yield 100, "#ffe8e8", seqName, beg, end
Martin@860
   547
        else:
Martin@895
   548
            yield 100, "#e8e8ff", seqName, beg, end
Martin@860
   549
Martin@650
   550
def isExtraFirstGapField(fields):
Martin@650
   551
    return fields[4].isdigit()
Martin@650
   552
Martin@905
   553
def readGaps(opts, fileName, rangeDict):
Martin@650
   554
    '''Read locations of unsequenced gaps, from an agp or gap file.'''
Martin@844
   555
    for line in myOpen(fileName):
Martin@650
   556
        w = line.split()
Martin@650
   557
        if not w or w[0][0] == "#": continue
Martin@650
   558
        if isExtraFirstGapField(w): w = w[1:]
Martin@650
   559
        if w[4] not in "NU": continue
Martin@650
   560
        seqName = w[0]
Martin@905
   561
        if seqName not in rangeDict: continue
Martin@650
   562
        end = int(w[2])
Martin@650
   563
        beg = end - int(w[5])  # zero-based coordinate
Martin@857
   564
        if w[7] == "yes":
Martin@859
   565
            yield 3000, opts.bridged_color, seqName, beg, end
Martin@857
   566
        else:
Martin@859
   567
            yield 2000, opts.unbridged_color, seqName, beg, end
Martin@650
   568
Martin@905
   569
def bedBoxes(beds, rangeDict, margin, edge, isTop, bpPerPix):
Martin@897
   570
    for layer, color, seqName, bedBeg, bedEnd in beds:
Martin@905
   571
        for rangeBeg, rangeEnd, origin in rangeDict[seqName]:
Martin@905
   572
            beg = max(bedBeg, rangeBeg)
Martin@905
   573
            end = min(bedEnd, rangeEnd)
Martin@905
   574
            if beg >= end: continue
Martin@905
   575
            if layer <= 1000:
Martin@905
   576
                # include partly-covered pixels
Martin@905
   577
                b = (origin + beg) // bpPerPix
Martin@905
   578
                e = div_ceil(origin + end, bpPerPix)
Martin@905
   579
            else:
Martin@905
   580
                # exclude partly-covered pixels
Martin@905
   581
                b = div_ceil(origin + beg, bpPerPix)
Martin@905
   582
                e = (origin + end) // bpPerPix
Martin@905
   583
                if e <= b: continue
Martin@912
   584
                if end == rangeEnd:  # include partly-covered end pixels
Martin@912
   585
                    e = div_ceil(origin + end, bpPerPix)
Martin@905
   586
            if isTop:
Martin@905
   587
                box = b, margin, e, edge
Martin@905
   588
            else:
Martin@905
   589
                box = margin, b, edge, e
Martin@905
   590
            yield layer, color, box
Martin@845
   591
Martin@857
   592
def drawAnnotations(im, boxes):
Martin@857
   593
    # xxx use partial transparency for different-color overlaps?
Martin@857
   594
    for layer, color, box in boxes:
Martin@650
   595
        im.paste(color, box)
Martin@650
   596
Martin@901
   597
def placedLabels(labels, rangePixBegs, rangePixLens, beg, end):
Martin@901
   598
    '''Return axis labels with endpoint & sort-order information.'''
Martin@901
   599
    maxWidth = end - beg
Martin@901
   600
    for i, j, k in zip(labels, rangePixBegs, rangePixLens):
Martin@901
   601
        text, textWidth, textHeight = i
Martin@901
   602
        if textWidth > maxWidth:
Martin@901
   603
            continue
Martin@901
   604
        labelBeg = j + (k - textWidth) // 2
Martin@901
   605
        labelEnd = labelBeg + textWidth
Martin@901
   606
        sortKey = textWidth - k
Martin@901
   607
        if labelBeg < beg:
Martin@904
   608
            sortKey += maxWidth * (beg - labelBeg)
Martin@901
   609
            labelBeg = beg
Martin@901
   610
            labelEnd = beg + textWidth
Martin@901
   611
        if labelEnd > end:
Martin@904
   612
            sortKey += maxWidth * (labelEnd - end)
Martin@901
   613
            labelEnd = end
Martin@901
   614
            labelBeg = end - textWidth
Martin@901
   615
        yield sortKey, labelBeg, labelEnd, text, textHeight
Martin@1
   616
Martin@897
   617
def nonoverlappingLabels(labels, minPixTweenLabels):
Martin@1
   618
    '''Get a subset of non-overlapping axis labels, greedily.'''
Martin@897
   619
    out = []
Martin@1
   620
    for i in labels:
Martin@897
   621
        beg = i[1] - minPixTweenLabels
Martin@897
   622
        end = i[2] + minPixTweenLabels
Martin@897
   623
        if all(j[2] <= beg or j[1] >= end for j in out):
Martin@897
   624
            out.append(i)
Martin@897
   625
    return out
Martin@1
   626
Martin@901
   627
def axisImage(labels, rangePixBegs, rangePixLens, textRot,
Martin@895
   628
              textAln, font, image_mode, opts):
Martin@1
   629
    '''Make an image of axis labels.'''
Martin@900
   630
    beg = rangePixBegs[0]
Martin@900
   631
    end = rangePixBegs[-1] + rangePixLens[-1]
Martin@901
   632
    margin = max(i[2] for i in labels)
Martin@901
   633
    labels = sorted(placedLabels(labels, rangePixBegs, rangePixLens, beg, end))
Martin@878
   634
    minPixTweenLabels = 0 if textRot else opts.label_space
Martin@897
   635
    labels = nonoverlappingLabels(labels, minPixTweenLabels)
Martin@897
   636
    image_size = (margin, end) if textRot else (end, margin)
Martin@895
   637
    im = Image.new(image_mode, image_size, opts.margin_color)
Martin@1
   638
    draw = ImageDraw.Draw(im)
Martin@1
   639
    for i in labels:
Martin@878
   640
        base = margin - i[4] if textAln else 0
Martin@878
   641
        position = (base, i[1]) if textRot else (i[1], base)
Martin@646
   642
        draw.text(position, i[3], font=font, fill=opts.text_color)
Martin@1
   643
    return im
Martin@1
   644
Martin@906
   645
def rangesWithOrigins(sortedRanges, rangePixBegs, bpPerPix):
Martin@906
   646
    for i, j in zip(sortedRanges, rangePixBegs):
Martin@906
   647
        seqName, rangeBeg, rangeEnd = i
Martin@906
   648
        origin = bpPerPix * j - rangeBeg
Martin@906
   649
        yield seqName, (rangeBeg, rangeEnd, origin)
Martin@906
   650
Martin@906
   651
def rangesPerSeq(sortedRanges, rangePixBegs, bpPerPix):
Martin@906
   652
    a = rangesWithOrigins(sortedRanges, rangePixBegs, bpPerPix)
Martin@906
   653
    for k, v in itertools.groupby(a, itemgetter(0)):
Martin@906
   654
        yield k, [i[1] for i in v]
Martin@836
   655
Martin@903
   656
def getFont(opts):
Martin@903
   657
    if opts.fontfile:
Martin@903
   658
        return ImageFont.truetype(opts.fontfile, opts.fontsize)
Martin@903
   659
    fileNames = []
Martin@903
   660
    try:
Martin@903
   661
        x = ["fc-match", "-f%{file}", "arial"]
Martin@903
   662
        p = subprocess.Popen(x, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Martin@903
   663
        out, err = p.communicate()
Martin@903
   664
        fileNames.append(out)
Martin@903
   665
    except OSError as e:
Martin@903
   666
        warn("fc-match error: " + str(e))
Martin@903
   667
    fileNames.append("/Library/Fonts/Arial.ttf")  # for Mac
Martin@903
   668
    for i in fileNames:
Martin@903
   669
        try:
Martin@903
   670
            font = ImageFont.truetype(i, opts.fontsize)
Martin@903
   671
            warn("font: " + i)
Martin@903
   672
            return font
Martin@903
   673
        except IOError as e:
Martin@903
   674
            warn("font load error: " + str(e))
Martin@903
   675
    return ImageFont.load_default()
Martin@903
   676
Martin@648
   677
def lastDotplot(opts, args):
Martin@903
   678
    font = getFont(opts)
Martin@643
   679
    image_mode = 'RGB'
Martin@643
   680
    forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
Martin@643
   681
    reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
Martin@643
   682
    zipped_colors = zip(forward_color, reverse_color)
Martin@643
   683
    overlap_color = tuple([(i + j) // 2 for i, j in zipped_colors])
Martin@641
   684
Martin@911
   685
    maxGap1, maxGapB1 = twoValuesFromOption(opts.max_gap1, ":")
Martin@911
   686
    maxGap2, maxGapB2 = twoValuesFromOption(opts.max_gap2, ":")
Martin@911
   687
Martin@644
   688
    warn("reading alignments...")
Martin@904
   689
    alnData = readAlignments(args[0], opts)
Martin@909
   690
    alignments, seqRanges1, coverDict1, seqRanges2, coverDict2 = alnData
Martin@649
   691
    if not alignments: raise Exception("there are no alignments")
Martin@910
   692
    warn("cutting...")
Martin@910
   693
    coverDict1 = dict(mergedRangesPerSeq(coverDict1))
Martin@910
   694
    coverDict2 = dict(mergedRangesPerSeq(coverDict2))
Martin@910
   695
    minAlignedBases = min(coveredLength(coverDict1), coveredLength(coverDict2))
Martin@910
   696
    pad = int(opts.pad * minAlignedBases)
Martin@910
   697
    cutRanges1 = list(trimmed(seqRanges1, coverDict1, minAlignedBases,
Martin@911
   698
                              maxGap1, pad, pad))
Martin@910
   699
    cutRanges2 = list(trimmed(seqRanges2, coverDict2, minAlignedBases,
Martin@911
   700
                              maxGap2, pad, pad))
Martin@911
   701
Martin@911
   702
    warn("reading secondary alignments...")
Martin@911
   703
    alnDataB = readSecondaryAlignments(opts, cutRanges1, cutRanges2)
Martin@911
   704
    alignmentsB, seqRangesB1, coverDictB1, seqRangesB2, coverDictB2 = alnDataB
Martin@911
   705
    warn("cutting...")
Martin@911
   706
    coverDictB1 = dict(mergedRangesPerSeq(coverDictB1))
Martin@911
   707
    coverDictB2 = dict(mergedRangesPerSeq(coverDictB2))
Martin@911
   708
    cutRangesB1 = trimmed(seqRangesB1, coverDictB1, minAlignedBases,
Martin@911
   709
                          maxGapB1, 0, 0)
Martin@911
   710
    cutRangesB2 = trimmed(seqRangesB2, coverDictB2, minAlignedBases,
Martin@911
   711
                          maxGapB2, 0, 0)
Martin@641
   712
Martin@914
   713
    warn("sorting...")
Martin@914
   714
    sortOut = allSortedRanges(opts, alignments, alignmentsB,
Martin@914
   715
                              cutRanges1, cutRangesB1, cutRanges2, cutRangesB2)
Martin@914
   716
    sortedRanges1, sortedRanges2 = sortOut
Martin@914
   717
Martin@878
   718
    textRot1 = "vertical".startswith(opts.rot1)
Martin@914
   719
    i1 = dataFromRanges(sortedRanges1, font,
Martin@908
   720
                        opts.fontsize, image_mode, opts.labels1, textRot1)
Martin@914
   721
    rangeSizes1, labelData1, tMargin = i1
Martin@846
   722
Martin@878
   723
    textRot2 = "horizontal".startswith(opts.rot2)
Martin@914
   724
    i2 = dataFromRanges(sortedRanges2, font,
Martin@908
   725
                        opts.fontsize, image_mode, opts.labels2, textRot2)
Martin@914
   726
    rangeSizes2, labelData2, lMargin = i2
Martin@641
   727
Martin@896
   728
    maxPixels1 = opts.width  - lMargin
Martin@896
   729
    maxPixels2 = opts.height - tMargin
Martin@897
   730
    bpPerPix1 = get_bp_per_pix(rangeSizes1, opts.border_pixels, maxPixels1)
Martin@897
   731
    bpPerPix2 = get_bp_per_pix(rangeSizes2, opts.border_pixels, maxPixels2)
Martin@855
   732
    bpPerPix = max(bpPerPix1, bpPerPix2)
Martin@855
   733
    warn("bp per pixel = " + str(bpPerPix))
Martin@641
   734
Martin@900
   735
    p1 = pixelData(rangeSizes1, bpPerPix, opts.border_pixels, lMargin)
Martin@900
   736
    rangePixBegs1, rangePixLens1, width = p1
Martin@906
   737
    rangeDict1 = dict(rangesPerSeq(sortedRanges1, rangePixBegs1, bpPerPix))
Martin@900
   738
Martin@900
   739
    p2 = pixelData(rangeSizes2, bpPerPix, opts.border_pixels, tMargin)
Martin@900
   740
    rangePixBegs2, rangePixLens2, height = p2
Martin@906
   741
    rangeDict2 = dict(rangesPerSeq(sortedRanges2, rangePixBegs2, bpPerPix))
Martin@900
   742
Martin@847
   743
    warn("width:  " + str(width))
Martin@847
   744
    warn("height: " + str(height))
Martin@839
   745
Martin@644
   746
    warn("processing alignments...")
Martin@911
   747
    hits = alignmentPixels(width, height, alignments + alignmentsB, bpPerPix,
Martin@905
   748
                           rangeDict1, rangeDict2)
Martin@641
   749
Martin@904
   750
    warn("reading annotations...")
Martin@134
   751
Martin@905
   752
    rangeDict1 = expandedSeqDict(rangeDict1)
Martin@905
   753
    beds1 = itertools.chain(readBed(opts.bed1, rangeDict1),
Martin@905
   754
                            readRmsk(opts.rmsk1, rangeDict1),
Martin@905
   755
                            readGenePred(opts, opts.genePred1, rangeDict1),
Martin@905
   756
                            readGaps(opts, opts.gap1, rangeDict1))
Martin@905
   757
    b1 = bedBoxes(beds1, rangeDict1, tMargin, height, True, bpPerPix)
Martin@845
   758
Martin@905
   759
    rangeDict2 = expandedSeqDict(rangeDict2)
Martin@905
   760
    beds2 = itertools.chain(readBed(opts.bed2, rangeDict2),
Martin@905
   761
                            readRmsk(opts.rmsk2, rangeDict2),
Martin@905
   762
                            readGenePred(opts, opts.genePred2, rangeDict2),
Martin@905
   763
                            readGaps(opts, opts.gap2, rangeDict2))
Martin@905
   764
    b2 = bedBoxes(beds2, rangeDict2, lMargin, width, False, bpPerPix)
Martin@857
   765
Martin@857
   766
    boxes = sorted(itertools.chain(b1, b2))
Martin@904
   767
Martin@904
   768
    warn("drawing...")
Martin@904
   769
Martin@904
   770
    image_size = width, height
Martin@904
   771
    im = Image.new(image_mode, image_size, opts.background_color)
Martin@904
   772
Martin@857
   773
    drawAnnotations(im, boxes)
Martin@650
   774
Martin@643
   775
    for i in range(height):
Martin@643
   776
        for j in range(width):
Martin@643
   777
            store_value = hits[i * width + j]
Martin@643
   778
            xy = j, i
Martin@643
   779
            if   store_value == 1: im.putpixel(xy, forward_color)
Martin@643
   780
            elif store_value == 2: im.putpixel(xy, reverse_color)
Martin@643
   781
            elif store_value == 3: im.putpixel(xy, overlap_color)
Martin@95
   782
Martin@643
   783
    if opts.fontsize != 0:
Martin@900
   784
        axis1 = axisImage(labelData1, rangePixBegs1, rangePixLens1,
Martin@895
   785
                          textRot1, False, font, image_mode, opts)
Martin@878
   786
        if textRot1:
Martin@878
   787
            axis1 = axis1.transpose(Image.ROTATE_90)
Martin@900
   788
        axis2 = axisImage(labelData2, rangePixBegs2, rangePixLens2,
Martin@895
   789
                          textRot2, textRot2, font, image_mode, opts)
Martin@878
   790
        if not textRot2:
Martin@878
   791
            axis2 = axis2.transpose(Image.ROTATE_270)
Martin@643
   792
        im.paste(axis1, (0, 0))
Martin@643
   793
        im.paste(axis2, (0, 0))
Martin@1
   794
Martin@900
   795
    for i in rangePixBegs1[1:]:
Martin@877
   796
        box = i - opts.border_pixels, tMargin, i, height
Martin@852
   797
        im.paste(opts.border_color, box)
Martin@1
   798
Martin@900
   799
    for i in rangePixBegs2[1:]:
Martin@877
   800
        box = lMargin, i - opts.border_pixels, width, i
Martin@852
   801
        im.paste(opts.border_color, box)
Martin@1
   802
Martin@643
   803
    im.save(args[1])
Martin@648
   804
Martin@648
   805
if __name__ == "__main__":
Martin@649
   806
    usage = """%prog --help
Martin@649
   807
   or: %prog [options] maf-or-tab-alignments dotplot.png
Martin@649
   808
   or: %prog [options] maf-or-tab-alignments dotplot.gif
Martin@649
   809
   or: ..."""
Martin@649
   810
    description = "Draw a dotplot of pair-wise sequence alignments in MAF or tabular format."
Martin@649
   811
    op = optparse.OptionParser(usage=usage, description=description)
Martin@866
   812
    op.add_option("-v", "--verbose", action="count",
Martin@866
   813
                  help="show progress messages & data about the plot")
Martin@651
   814
    op.add_option("-1", "--seq1", metavar="PATTERN", action="append",
Martin@840
   815
                  default=[],
Martin@651
   816
                  help="which sequences to show from the 1st genome")
Martin@651
   817
    op.add_option("-2", "--seq2", metavar="PATTERN", action="append",
Martin@840
   818
                  default=[],
Martin@651
   819
                  help="which sequences to show from the 2nd genome")
Martin@648
   820
    # Replace "width" & "height" with a single "length" option?
Martin@648
   821
    op.add_option("-x", "--width", type="int", default=1000,
Martin@648
   822
                  help="maximum width in pixels (default: %default)")
Martin@648
   823
    op.add_option("-y", "--height", type="int", default=1000,
Martin@648
   824
                  help="maximum height in pixels (default: %default)")
Martin@649
   825
    op.add_option("-c", "--forwardcolor", metavar="COLOR", default="red",
Martin@649
   826
                  help="color for forward alignments (default: %default)")
Martin@649
   827
    op.add_option("-r", "--reversecolor", metavar="COLOR", default="blue",
Martin@649
   828
                  help="color for reverse alignments (default: %default)")
Martin@911
   829
    op.add_option("--alignments", metavar="FILE", help="secondary alignments")
Martin@907
   830
    op.add_option("--sort1", default="1", metavar="N",
Martin@851
   831
                  help="genome1 sequence order: 0=input order, 1=name order, "
Martin@914
   832
                  "2=length order, 3=alignment order (default=%default)")
Martin@907
   833
    op.add_option("--sort2", default="1", metavar="N",
Martin@851
   834
                  help="genome2 sequence order: 0=input order, 1=name order, "
Martin@914
   835
                  "2=length order, 3=alignment order (default=%default)")
Martin@910
   836
    op.add_option("--max-gap1", metavar="FRAC", default="0.5,2", help=
Martin@910
   837
                  "maximum unaligned (end,mid) gap in genome1: "
Martin@910
   838
                  "fraction of aligned length (default=%default)")
Martin@910
   839
    op.add_option("--max-gap2", metavar="FRAC", default="0.5,2", help=
Martin@910
   840
                  "maximum unaligned (end,mid) gap in genome2: "
Martin@910
   841
                  "fraction of aligned length (default=%default)")
Martin@910
   842
    op.add_option("--pad", metavar="FRAC", type="float", default=0.04, help=
Martin@910
   843
                  "pad length when cutting unaligned gaps: "
Martin@910
   844
                  "fraction of aligned length (default=%default)")
Martin@852
   845
    op.add_option("--border-pixels", metavar="INT", type="int", default=1,
Martin@852
   846
                  help="number of pixels between sequences (default=%default)")
Martin@895
   847
    op.add_option("--border-color", metavar="COLOR", default="black",
Martin@852
   848
                  help="color for pixels between sequences (default=%default)")
Martin@911
   849
    # --break-color and/or --break-pixels for intra-sequence breaks?
Martin@895
   850
    op.add_option("--margin-color", metavar="COLOR", default="#dcdcdc",
Martin@895
   851
                  help="margin color")
Martin@846
   852
Martin@850
   853
    og = optparse.OptionGroup(op, "Text options")
Martin@850
   854
    og.add_option("-f", "--fontfile", metavar="FILE",
Martin@850
   855
                  help="TrueType or OpenType font file")
Martin@903
   856
    og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=14,
Martin@850
   857
                  help="TrueType or OpenType font size (default: %default)")
Martin@898
   858
    og.add_option("--labels1", type="int", default=0, metavar="N", help=
Martin@898
   859
                  "genome1 labels: 0=name, 1=name:length, "
Martin@898
   860
                  "2=name:start:length, 3=name:start-end (default=%default)")
Martin@898
   861
    og.add_option("--labels2", type="int", default=0, metavar="N", help=
Martin@898
   862
                  "genome2 labels: 0=name, 1=name:length, "
Martin@898
   863
                  "2=name:start:length, 3=name:start-end (default=%default)")
Martin@878
   864
    og.add_option("--rot1", metavar="ROT", default="h",
Martin@878
   865
                  help="text rotation for the 1st genome (default=%default)")
Martin@878
   866
    og.add_option("--rot2", metavar="ROT", default="v",
Martin@878
   867
                  help="text rotation for the 2nd genome (default=%default)")
Martin@850
   868
    op.add_option_group(og)
Martin@850
   869
Martin@860
   870
    og = optparse.OptionGroup(op, "Annotation options")
Martin@860
   871
    og.add_option("--bed1", metavar="FILE",
Martin@860
   872
                  help="read genome1 annotations from BED file")
Martin@860
   873
    og.add_option("--bed2", metavar="FILE",
Martin@860
   874
                  help="read genome2 annotations from BED file")
Martin@860
   875
    og.add_option("--rmsk1", metavar="FILE", help="read genome1 repeats from "
Martin@860
   876
                  "RepeatMasker .out or rmsk.txt file")
Martin@860
   877
    og.add_option("--rmsk2", metavar="FILE", help="read genome2 repeats from "
Martin@860
   878
                  "RepeatMasker .out or rmsk.txt file")
Martin@860
   879
    op.add_option_group(og)
Martin@860
   880
Martin@860
   881
    og = optparse.OptionGroup(op, "Gene options")
Martin@860
   882
    og.add_option("--genePred1", metavar="FILE",
Martin@860
   883
                  help="read genome1 genes from genePred file")
Martin@860
   884
    og.add_option("--genePred2", metavar="FILE",
Martin@860
   885
                  help="read genome2 genes from genePred file")
Martin@895
   886
    og.add_option("--exon-color", metavar="COLOR", default="PaleGreen",
Martin@860
   887
                  help="color for exons (default=%default)")
Martin@895
   888
    og.add_option("--cds-color", metavar="COLOR", default="LimeGreen",
Martin@860
   889
                  help="color for protein-coding regions (default=%default)")
Martin@860
   890
    op.add_option_group(og)
Martin@860
   891
Martin@650
   892
    og = optparse.OptionGroup(op, "Unsequenced gap options")
Martin@650
   893
    og.add_option("--gap1", metavar="FILE",
Martin@650
   894
                  help="read genome1 unsequenced gaps from agp or gap file")
Martin@650
   895
    og.add_option("--gap2", metavar="FILE",
Martin@650
   896
                  help="read genome2 unsequenced gaps from agp or gap file")
Martin@650
   897
    og.add_option("--bridged-color", metavar="COLOR", default="yellow",
Martin@650
   898
                  help="color for bridged gaps (default: %default)")
Martin@895
   899
    og.add_option("--unbridged-color", metavar="COLOR", default="orange",
Martin@650
   900
                  help="color for unbridged gaps (default: %default)")
Martin@650
   901
    op.add_option_group(og)
Martin@648
   902
    (opts, args) = op.parse_args()
Martin@648
   903
    if len(args) != 2: op.error("2 arguments needed")
Martin@648
   904
Martin@648
   905
    opts.text_color = "black"
Martin@648
   906
    opts.background_color = "white"
Martin@648
   907
    opts.label_space = 5     # minimum number of pixels between axis labels
Martin@648
   908
Martin@649
   909
    try: lastDotplot(opts, args)
Martin@649
   910
    except KeyboardInterrupt: pass  # avoid silly error message
Martin@903
   911
    except Exception as e:
Martin@649
   912
        prog = os.path.basename(sys.argv[0])
Martin@649
   913
        sys.exit(prog + ": error: " + str(e))